-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathiam.tf
290 lines (270 loc) · 10.4 KB
/
iam.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# Create EC2 Instance Profile
resource "aws_iam_instance_profile" "bastion_ssm_profile" {
name = "${var.name}-ssm-profile"
role = aws_iam_role.bastion_ssm_role.name
tags = var.tags
}
# Create EC2 Instance Role
resource "aws_iam_role" "bastion_ssm_role" {
name = local.role_name
permissions_boundary = var.permissions_boundary
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Sid = "EC2AssumeRole"
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "ec2.amazonaws.com"
}
},
]
})
tags = var.tags
}
# Attach AmazonSSMManagedInstanceCore policy to role
data "aws_iam_policy" "AmazonSSMManagedInstanceCore" {
arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/AmazonSSMManagedInstanceCore"
}
resource "aws_iam_role_policy_attachment" "bastion-ssm-aws-ssm-policy-attach" {
role = aws_iam_role.bastion_ssm_role.name
policy_arn = data.aws_iam_policy.AmazonSSMManagedInstanceCore.arn
}
# Attach AmazonElasticFileSystemFullAccess policy to role
data "aws_iam_policy" "AmazonElasticFileSystemFullAccess" {
arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/AmazonElasticFileSystemFullAccess"
}
resource "aws_iam_role_policy_attachment" "bastion-ssm-aws-efs-policy-attach" {
role = aws_iam_role.bastion_ssm_role.name
policy_arn = data.aws_iam_policy.AmazonElasticFileSystemFullAccess.arn
}
data "aws_iam_policy" "CloudWatchLogsFullAccess" {
arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/CloudWatchLogsFullAccess"
}
resource "aws_iam_role_policy_attachment" "bastion-ssm-s3-cwl-policy-attach" {
role = aws_iam_role.bastion_ssm_role.name
policy_arn = data.aws_iam_policy.CloudWatchLogsFullAccess.arn
}
# Create custom policy and attachment
resource "aws_iam_policy" "custom" {
count = local.add_custom_policy_to_role ? 1 : 0
name = "${local.role_name}-policy"
path = "/"
description = "Custom policy for EC2 instance"
policy = var.policy_content
tags = var.tags
}
resource "aws_iam_role_policy_attachment" "custom" {
count = local.add_custom_policy_to_role ? 1 : 0
role = aws_iam_role.bastion_ssm_role.name
policy_arn = aws_iam_policy.custom[0].arn
}
# Additional policy attachments if needed
resource "aws_iam_role_policy_attachment" "managed" {
count = length(var.policy_arns)
role = aws_iam_role.bastion_ssm_role.name
policy_arn = var.policy_arns[count.index]
}
# Terraform policy and attachment
resource "aws_iam_role_policy_attachment" "terraform" {
count = var.enable_bastion_terraform_permissions ? 1 : 0
role = aws_iam_role.bastion_ssm_role.name
policy_arn = aws_iam_policy.terraform_policy[count.index].arn
}
resource "aws_iam_policy" "terraform_policy" {
# checkov:skip=CKV_AWS_286: TODO: Tighten this policy up. It currently allows actions that can be used for privilege escalation without constraint. Ref: https://docs.bridgecrew.io/docs/ensure-iam-policies-does-not-allow-privilege-escalation
# checkov:skip=CKV_AWS_110: TODO: Fix CKV_AWS_286. It is identical to this policy.
# checkov:skip=CKV_AWS_287: TODO: Tighten this policy up. It currently allows actions that can be used for credentials exposure without constraint. Ref: https://docs.bridgecrew.io/docs/ensure-iam-policies-do-not-allow-credentials-exposure
# checkov:skip=CKV_AWS_107: TODO: Fix CKV_AWS_287. It is identical to this policy.
# checkov:skip=CKV_AWS_288: TODO: Tighten this policy up. It currently allows actions that can be used to exfiltrate secrets or other data without constraint. https://docs.bridgecrew.io/docs/ensure-iam-policies-do-not-allow-data-exfiltration
# checkov:skip=CKV_AWS_108: TODO: Fix CKV_AWS_288. It is identical to this policy.
# checkov:skip=CKV_AWS_289: TODO: Tighten this policy up. It currently allows actions that can be used for permissions management and/or resource exposure without constraint. Ref: https://docs.bridgecrew.io/docs/ensure-iam-policies-do-not-allow-permissions-management-resource-exposure-without-constraint
# checkov:skip=CKV_AWS_109: TODO: Fix CKV_AWS_289. It is identical to this policy.
# checkov:skip=CKV_AWS_290: TODO: Tighten this policy up. It currently allows actions that can be used to for resource exposure without constraint. Ref: https://docs.bridgecrew.io/docs/ensure-iam-policies-do-not-allow-write-access-without-constraint
# checkov:skip=CKV_AWS_111: TODO: Fix CKV_AWS_290. It is identical to this policy.
# checkov:skip=CKV_AWS_355: "Ensure no IAM policies documents allow "*" as a statement's resource for restrictable actions" -- TODO: Update this policy to be more least-priviledge-y
count = var.enable_bastion_terraform_permissions ? 1 : 0
name = "${var.name}-terraform-policy"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"autoscaling:*",
"aws-marketplace-management:*",
"aws-marketplace:*",
"cloudformation:*",
"cloudtrail:*",
"cloudwatch:*",
"events:*",
"logs:*",
"dynamodb:*",
"glacier:*",
"dms:*",
"iam:GetPolicyVersion",
"iam:GetRole",
"iam:ListInstance*",
"iam:CreateInstanceProfile",
"iam:UploadServerCertificate",
"iam:UpdateServerCertificate",
"iam:GetServerCertificate",
"iam:DeleteServerCertificate",
"iam:DetachRolePolicy",
"iam:DeleteRolePolicy",
"iam:PutRolePermissionsBoundary",
"iam:PutRolePolicy",
"iam:CreatePolicy",
"iam:CreatePolicyVersion",
"iam:AttachRolePolicy",
"iam:*InstanceProfile",
"iam:Generate*",
"iam:Get*",
"iam:List*",
"iam:Sim*",
"iam:Tag*",
"iam:Untag*",
"iam:*ServiceLinkedRole",
"iam:CreateOpenIDConnectProvider",
"iam:DeleteOpenIDConnectProvider",
"iam:UpdateAssumeRolePolicy",
"ec2:*",
"elasticbeanstalk:*",
"elasticache:*",
"elasticloadbalancing:*",
"elasticmapreduce:*",
"events:*",
"glacier:*",
"kinesis:*",
"kms:*",
"lambda:*",
"logs:*",
"ram:*",
"rds:*",
"redshift:*",
"s3:*",
"sns:*",
"sqs:*",
"swf:*",
"tag:*",
"workspaces:*",
"ecs:*",
"ecr:*",
"inspector:Create*",
"inspector:Delete*",
"inspector:DescribeCrossAccountAccessRole",
"inspector:DescribeAssessmentRuns",
"inspector:DescribeAssessmentTargets",
"inspector:DescribeAssessmentTemplates",
"inspector:DescribeFindings",
"inspector:DescribeResourceGroups",
"inspector:DescribeRulesPackages",
"inspector:List*",
"inspector:PreviewAgents",
"inspector:RemoveAttributesFromFindings",
"inspector:SetTagsForResource",
"inspector:StartAssessmentRun",
"inspector:StopAssessmentRun",
"inspector:SubscribeToEvent",
"inspector:UnsubscribeFromEvent",
"inspector:UpdateAssessmentTarget",
"secretsmanager:GetSecretValue"
],
"Resource": "*",
"Effect": "Allow"
},
{
"Action": [
"iam:PassRole"
],
"Resource": "*",
"Effect": "Allow"
},
{
"Sid": "EKSPermisssions",
"Effect": "Allow",
"Action": [
"eks:UpdateNodegroupVersion",
"eks:UpdateNodegroupConfig",
"eks:UpdateClusterVersion",
"eks:UpdateClusterConfig",
"eks:UntagResource",
"eks:TagResource",
"eks:ListUpdates",
"eks:ListTagsForResource",
"eks:ListNodegroups",
"eks:ListFargateProfiles",
"eks:ListClusters",
"eks:DescribeUpdate",
"eks:DescribeNodegroup",
"eks:DescribeFargateProfile",
"eks:DescribeCluster",
"eks:DeleteNodegroup",
"eks:DeleteFargateProfile",
"eks:DeleteCluster",
"eks:CreateNodegroup",
"eks:CreateFargateProfile",
"eks:CreateCluster",
"eks:CreateAddon",
"eks:DeleteAddon",
"eks:UpdateAddon",
"eks:DescribeAddon",
"eks:DescribeAddonVersions",
"eks:ListAddons"
],
"Resource": "*"
},
{
"Action": [
"iam:DeletePolicy",
"iam:DeletePolicyVersion"
],
"Resource": "*",
"Effect": "Allow"
},
{
"Action": [
"iam:DeleteRole"
],
"Resource": [
"arn:${data.aws_partition.current.partition}:iam::*:role/*-rke2-*",
"arn:${data.aws_partition.current.partition}:iam::*:role/*"
],
"Effect": "Allow"
},
{
"Action": [
"iam:CreateRole"
],
"Resource": "*",
"Effect": "Allow"
},
{
"Action": [
"ssm:*"
],
"Resource": "*",
"Effect": "Allow"
},
{
"Action": [
"sts:AssumeRole"
],
"Resource": [
"arn:${data.aws_partition.current.partition}:iam::*:role/s3-dataRefresh"
],
"Effect": "Allow"
},
{
"Action": [
"aws-portal:*Billing"
],
"Resource": "*",
"Effect": "Deny"
}
]
}
EOF
tags = var.tags
}