-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathiam_policies.tf
More file actions
190 lines (172 loc) · 5.28 KB
/
iam_policies.tf
File metadata and controls
190 lines (172 loc) · 5.28 KB
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
# Read-only policy for DynamoDB
data "aws_iam_policy_document" "dynamodb_read_policy_doc" {
statement {
actions = ["dynamodb:GetItem", "dynamodb:Query", "dynamodb:Scan"]
resources = [module.eligibility_status_table.arn]
}
}
# Attach dynamoDB read policy to Lambda role
resource "aws_iam_role_policy" "lambda_dynamodb_read_policy" {
name = "DynamoDBReadAccess"
role = aws_iam_role.eligibility_lambda_role.id
policy = data.aws_iam_policy_document.dynamodb_read_policy_doc.json
}
# Write-only policy for DynamoDB
data "aws_iam_policy_document" "dynamodb_write_policy_doc" {
statement {
actions = ["dynamodb:PutItem", "dynamodb:UpdateItem", "dynamodb:DeleteItem", "dynamodb:BatchWriteItem"]
resources = [module.eligibility_status_table.arn]
}
}
# Attach dynamoDB write policy to external write role
resource "aws_iam_role_policy" "external_dynamodb_write_policy" {
count = length(aws_iam_role.write_access_role)
name = "DynamoDBWriteAccess"
role = aws_iam_role.write_access_role[count.index].id
policy = data.aws_iam_policy_document.dynamodb_write_policy_doc.json
}
# Deny all S3 actions on the access logs bucket unless requests use secure (SSL) transport.
data "aws_iam_policy_document" "storage_bucket_access_logs_policy" {
statement {
sid = "AllowSSLRequestsOnly"
actions = [
"s3:*",
]
effect = "Deny"
resources = [
module.s3_rules_bucket.storage_bucket_access_logs_arn,
"${module.s3_rules_bucket.storage_bucket_access_logs_arn}/*",
]
principals {
type = "*"
identifiers = ["*"]
}
condition {
test = "Bool"
values = [
"false",
]
variable = "aws:SecureTransport"
}
}
}
resource "aws_s3_bucket_policy" "storage_bucket_access_logs_policy" {
bucket = module.s3_rules_bucket.storage_bucket_access_logs_id
policy = data.aws_iam_policy_document.storage_bucket_access_logs_policy.json
}
# Policy doc for S3 Rules bucket
data "aws_iam_policy_document" "s3_rules_bucket_policy" {
statement {
sid = "AllowSSLRequestsOnly"
actions = [
"s3:GetObject",
"s3:ListBucket",
]
resources = [
module.s3_rules_bucket.storage_bucket_arn,
"${module.s3_rules_bucket.storage_bucket_arn}/*",
]
condition {
test = "Bool"
values = ["true"]
variable = "aws:SecureTransport"
}
}
}
# Attach s3 read policy to Lambda role
resource "aws_iam_role_policy" "lambda_s3_read_policy" {
name = "S3ReadAccess"
role = aws_iam_role.eligibility_lambda_role.id
policy = data.aws_iam_policy_document.s3_rules_bucket_policy.json
}
# Attach AWSLambdaVPCAccessExecutionRole to Lambda
resource "aws_iam_role_policy_attachment" "AWSLambdaVPCAccessExecutionRole" {
role = aws_iam_role.eligibility_lambda_role.id
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole"
}
#Attach AWSLambdaBasicExecutionRole to Lambda
resource "aws_iam_role_policy_attachment" "lambda_logs_policy_attachment" {
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
role = aws_iam_role.eligibility_lambda_role.name
}
# Policy doc for S3 Audit bucket
data "aws_iam_policy_document" "s3_audit_bucket_policy" {
statement {
sid = "AllowSSLRequestsOnly"
actions = ["s3:*"]
resources = [
module.s3_audit_bucket.storage_bucket_arn,
"${module.s3_audit_bucket.storage_bucket_arn}/*",
]
condition {
test = "Bool"
values = ["true"]
variable = "aws:SecureTransport"
}
}
}
# Attach s3 write policy to external write role
resource "aws_iam_role_policy" "external_s3_write_policy" {
name = "S3WriteAccess"
role = aws_iam_role.eligibility_lambda_role.id
policy = data.aws_iam_policy_document.s3_audit_bucket_policy.json
}
## KMS
data "aws_iam_policy_document" "kms_key_policy" {
statement {
sid = "EnableIamUserPermissions"
effect = "Allow"
principals {
type = "AWS"
identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"]
}
actions = ["kms:*"]
resources = [
module.eligibility_status_table.dynamodb_kms_key_arn,
module.s3_rules_bucket.storage_bucket_kms_key_arn,
module.s3_audit_bucket.storage_bucket_kms_key_arn,
module.eligibility_signposting_api_gateway.kms_key_arn,
]
}
statement {
sid = "Allow lambda decrypt role"
effect = "Allow"
principals {
type = "AWS"
identifiers = [
aws_iam_role.eligibility_lambda_role.arn
]
}
actions = [
"kms:Decrypt"
]
resources = [
module.eligibility_status_table.dynamodb_kms_key_arn,
module.s3_rules_bucket.storage_bucket_kms_key_arn,
]
}
statement {
sid = "Allow lambda full write role"
effect = "Allow"
principals {
type = "AWS"
identifiers = [
aws_iam_role.eligibility_lambda_role.arn
]
}
actions = [
"kms:Decrypt",
"kms:Encrypt",
"kms:GenerateDataKey",
"kms:DescribeKey"
]
resources = [
module.s3_audit_bucket.storage_bucket_kms_key_arn
]
}
}
# attach kms decrypt policy kms key
resource "aws_kms_key_policy" "kms_key" {
key_id = module.eligibility_status_table.dynamodb_kms_key_id
policy = data.aws_iam_policy_document.kms_key_policy.json
}