-
-
Notifications
You must be signed in to change notification settings - Fork 338
/
Copy pathiam.tf
193 lines (163 loc) · 5.05 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
data "aws_caller_identity" "this" {}
data "aws_partition" "current" {}
data "aws_region" "this" {}
# ----------------------------------------------------------------------------
# Terminate Instances - IAM Resources
# ----------------------------------------------------------------------------
data "aws_iam_policy_document" "assume_role" {
statement {
actions = [
"sts:AssumeRole",
]
effect = "Allow"
principals {
identifiers = ["lambda.amazonaws.com"]
type = "Service"
}
}
}
resource "aws_iam_role" "lambda" {
name = "${var.name_iam_objects}-${var.name}"
description = "Role for executing the ${var.name} instance termination function"
path = "/"
permissions_boundary = var.role_permissions_boundary
assume_role_policy = data.aws_iam_policy_document.assume_role.json
force_detach_policies = true
tags = var.tags
}
resource "aws_iam_role_policy_attachment" "lambda_kms" {
count = var.kms_key_id != "" ? 1 : 0
role = aws_iam_role.lambda.name
policy_arn = aws_iam_policy.lambda_kms[0].arn
}
resource "aws_iam_policy" "lambda_kms" {
count = var.kms_key_id != "" ? 1 : 0
name = "${var.name_iam_objects}-${var.name}-lambda-kms"
path = "/"
policy = data.aws_iam_policy_document.kms_key[0].json
tags = var.tags
}
data "aws_iam_policy_document" "kms_key" {
count = var.kms_key_id != "" ? 1 : 0
# checkov:skip=CKV_AWS_111:Write access is limited to the resources needed
statement {
sid = "AllowKmsAccess"
actions = [
"kms:Decrypt", # to decrypt the Lambda environment variables
]
resources = [var.kms_key_id]
effect = "Allow"
}
}
data "aws_iam_policy_document" "lambda" {
# Permit the function to get a list of instances
statement {
sid = "GitLabRunnerLifecycleGetInstances"
actions = [
"ec2:DescribeInstances",
"ec2:DescribeTags",
"ec2:DescribeRegions",
"ec2:DescribeInstanceStatus",
]
resources = ["*"]
effect = "Allow"
}
# Permit the function to terminate instances with the 'gitlab-runner-parent-id'
# tag.
statement {
sid = "GitLabRunnerLifecycleTerminateInstances"
actions = [
"ec2:TerminateInstances"
]
resources = ["arn:${data.aws_partition.current.partition}:ec2:${data.aws_region.this.name}:${data.aws_caller_identity.this.account_id}:instance/*"]
condition {
test = "StringLike"
variable = "ec2:ResourceTag/gitlab-runner-parent-id"
values = ["i-*"]
}
effect = "Allow"
}
# Permit the function to execute the ASG lifecycle action
statement {
sid = "GitLabRunnerLifecycleTerminateEvent"
effect = "Allow"
actions = [
"autoscaling:CompleteLifecycleAction"
]
resources = [var.asg_arn]
}
statement {
sid = "GitLabRunnerLifecycleTerminateLogs"
actions = [
"logs:PutLogEvents",
"logs:CreateLogStream",
]
effect = "Allow"
# wildcard resources are ok as the log streams are created dynamically during runtime and are not known here
# tfsec:ignore:aws-iam-no-policy-wildcards
resources = [
aws_cloudwatch_log_group.lambda.arn,
"${aws_cloudwatch_log_group.lambda.arn}:log-stream:*"
]
}
statement {
sid = "SSHKeyHousekeepingList"
effect = "Allow"
actions = [
"ec2:DescribeKeyPairs"
]
resources = ["*"]
}
# separate statement due to the condition
statement {
sid = "SSHKeyHousekeepingDelete"
effect = "Allow"
actions = [
"ec2:DeleteKeyPair"
]
resources = ["*"]
condition {
test = "StringLike"
variable = "ec2:KeyPairName"
values = ["runner-*"]
}
}
}
data "aws_iam_policy_document" "spot_request_housekeeping" {
# checkov:skip=CKV_AWS_111:I didn't found any condition to limit the access.
# checkov:skip=CKV_AWS_356:False positive and fixed with version 2.3.293
statement {
sid = "SpotRequestHousekeepingList"
effect = "Allow"
actions = [
"ec2:CancelSpotInstanceRequests",
"ec2:DescribeSpotInstanceRequests"
]
# I didn't found any condition to limit the access
resources = ["*"]
}
}
resource "aws_iam_policy" "lambda" {
name = "${var.name_iam_objects}-${var.name}-lambda"
path = "/"
policy = data.aws_iam_policy_document.lambda.json
tags = var.tags
}
resource "aws_iam_role_policy_attachment" "lambda" {
role = aws_iam_role.lambda.name
policy_arn = aws_iam_policy.lambda.arn
}
resource "aws_iam_policy" "spot_request_housekeeping" {
name = "${var.name_iam_objects}-${var.name}-cancel-spot"
path = "/"
policy = data.aws_iam_policy_document.spot_request_housekeeping.json
tags = var.tags
}
resource "aws_iam_role_policy_attachment" "spot_request_housekeeping" {
role = aws_iam_role.lambda.name
policy_arn = aws_iam_policy.spot_request_housekeeping.arn
}
resource "aws_iam_role_policy_attachment" "aws_lambda_vpc_access_execution_role" {
role = aws_iam_role.lambda.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole"
}