-
Notifications
You must be signed in to change notification settings - Fork 4
/
iam_resources.tf
162 lines (146 loc) · 4.28 KB
/
iam_resources.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
locals {
create_kms_policy = var.ec2_ebs_kms_arn != "" || var.secrets_kms_arn != ""
create_sidecar_role = var.custom_host_role == ""
}
# Gets the ARN from a resource that is deployed by this module in order to
# get the proper partition, region and account number for the aws account
# where the resources are actually deployed. This prevents issues with
# deployment pipelines that runs on AWS and deploys to different accounts.
data "aws_arn" "cw_lg" {
arn = aws_cloudwatch_log_group.lg.arn
}
data "aws_iam_policy_document" "init_script_policy" {
# Policy doc to allow sidecar to function inside an ASG
statement {
actions = [
"ec2:DescribeTags",
"autoscaling:CompleteLifecycleAction",
"autoscaling:SetInstanceHealth"
]
resources = [
"*"
]
}
# Cloudwatch permissions
statement {
actions = [
"logs:CreateLogStream",
"logs:PutLogEvents"
]
resources = [
"${aws_cloudwatch_log_group.lg.arn}:*"
]
}
# Secrets Manager permissions
statement {
actions = [
"secretsmanager:GetSecretValue"
]
resources = compact([
"arn:${data.aws_arn.cw_lg.partition}:secretsmanager:${data.aws_arn.cw_lg.region}:${data.aws_arn.cw_lg.account}:secret:/cyral/*",
local.secret_arn
])
}
dynamic "statement" {
for_each = (var.tls_certificate_secret_arn != "" && var.tls_certificate_role_arn == "") ? [1] : []
content {
actions = [
"secretsmanager:GetSecretValue"
]
resources = compact([
var.tls_certificate_secret_arn,
])
}
}
dynamic "statement" {
for_each = (var.ca_certificate_secret_arn != "" && var.ca_certificate_role_arn == "") ? [1] : []
content {
actions = [
"secretsmanager:GetSecretValue"
]
resources = compact([
var.ca_certificate_secret_arn,
])
}
}
dynamic "statement" {
for_each = (var.tls_certificate_secret_arn != "" && var.tls_certificate_role_arn != "") ? [1] : []
content {
actions = [
"sts:AssumeRole"
]
resources = compact([
var.tls_certificate_role_arn,
])
}
}
dynamic "statement" {
for_each = (var.ca_certificate_secret_arn != "" && var.ca_certificate_role_arn != "") ? [1] : []
content {
actions = [
"sts:AssumeRole"
]
resources = compact([
var.ca_certificate_role_arn,
])
}
}
source_policy_documents = [
data.aws_iam_policy_document.kms.json
]
}
data "aws_iam_policy_document" "kms" {
# KMS permissions
dynamic "statement" {
for_each = local.create_kms_policy ? [1] : []
content {
actions = [
"kms:Decrypt",
"kms:Encrypt",
"kms:GenerateDataKey"
]
resources = compact([
var.secrets_kms_arn,
var.ec2_ebs_kms_arn
])
}
}
}
data "aws_iam_policy_document" "sidecar" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
resource "aws_iam_instance_profile" "sidecar_profile" {
count = local.create_sidecar_role ? 1 : 0
name = "${local.name_prefix}-sidecar_profile"
role = local.create_sidecar_role ? aws_iam_role.sidecar_role[0].name : var.custom_host_role
tags = var.custom_tags
}
resource "aws_iam_role" "sidecar_role" {
count = local.create_sidecar_role ? 1 : 0
name = "${local.name_prefix}-sidecar_role"
path = "/"
assume_role_policy = data.aws_iam_policy_document.sidecar.json
tags = var.custom_tags
}
resource "aws_iam_policy" "init_script_policy" {
name = "${local.name_prefix}-init_script_policy"
path = "/"
description = "Allow EC2 to update ASG when init complete"
policy = data.aws_iam_policy_document.init_script_policy.json
tags = var.custom_tags
}
resource "aws_iam_role_policy_attachment" "init_script_policy" {
role = local.create_sidecar_role ? aws_iam_role.sidecar_role[0].name : var.custom_host_role
policy_arn = aws_iam_policy.init_script_policy.arn
}
resource "aws_iam_role_policy_attachment" "user_policies" {
count = length(var.iam_policies)
role = local.create_sidecar_role ? aws_iam_role.sidecar_role[0].name : var.custom_host_role
policy_arn = var.iam_policies[count.index]
}