-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.tf
176 lines (151 loc) · 4.5 KB
/
main.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
/**
* Configures IAM policy to enforce MFA when accessing the AWS API.
*
* This configured policy also requires users to assume a role for most API calls.
*
* Creates the following resources:
*
* * IAM policy requiring a valid MFA security token for all API calls except those needed for managing a user's own IAM user.
* * IAM group policy attachment for defining which IAM groups to enforce MFA on.
* * IAM user policy attachment for defining which IAM users to enforce MFA on.
*
* ## Usage
*
* ```hcl
* module "aws_mfa" {
* source = "trussworks/mfa/aws"
*
* iam_groups = ["engineers"]
* iam_users = ["jill"]
* }
* ```
*/
data "aws_partition" "current" {
}
data "aws_iam_policy_document" "main" {
statement {
sid = "AllowAllUsersToListAccounts"
effect = "Allow"
actions = [
"iam:ListAccountAliases",
"iam:ListUsers",
"iam:ListVirtualMFADevices",
"iam:GetAccountPasswordPolicy",
"iam:GetAccountSummary",
]
resources = [
"*",
]
}
statement {
sid = "AllowIndividualUserToSeeAndManageOnlyTheirOwnAccountInformation"
effect = "Allow"
actions = [
"iam:CreateAccessKey",
"iam:DeleteAccessKey",
"iam:DeleteLoginProfile",
"iam:GetLoginProfile",
"iam:ListAccessKeys",
"iam:UpdateAccessKey",
"iam:ListSigningCertificates",
"iam:DeleteSigningCertificate",
"iam:UpdateSigningCertificate",
"iam:UploadSigningCertificate",
]
resources = [
"arn:${data.aws_partition.current.partition}:iam::*:user/&{aws:username}",
]
}
statement {
sid = "AllowIndividualUserToListOnlyTheirOwnMFA"
effect = "Allow"
actions = [
"iam:ListMFADevices",
]
resources = [
"arn:${data.aws_partition.current.partition}:iam::*:mfa/*",
"arn:${data.aws_partition.current.partition}:iam::*:user/&{aws:username}",
]
}
statement {
sid = "AllowIndividualUserToManageTheirOwnMFA"
effect = "Allow"
actions = [
"iam:CreateVirtualMFADevice",
"iam:DeleteVirtualMFADevice",
"iam:EnableMFADevice",
"iam:ResyncMFADevice",
]
resources = [
"arn:${data.aws_partition.current.partition}:iam::*:mfa/*",
"arn:${data.aws_partition.current.partition}:iam::*:user/&{aws:username}",
]
}
statement {
sid = "AllowIndividualUserToDeactivateOnlyTheirOwnMFAOnlyWhenUsingMFA"
effect = "Allow"
actions = [
"iam:DeactivateMFADevice",
]
resources = [
"arn:${data.aws_partition.current.partition}:iam::*:user/&{aws:username}",
]
condition {
test = "Bool"
variable = "aws:MultiFactorAuthPresent"
values = ["true"]
}
}
statement {
# Since this statement uses not_actions, it effectively blocks some statements
# that do not support MFA, such as sts:GetFederationToken.
# Therefore, this policy effectively forbids directly calling AWS APIs
# without assuming a role first.
sid = "BlockMostAccessUnlessSignedInWithMFA"
effect = "Deny"
# not_actions is a list of actions that this statement does not apply to.
# Used to apply a policy statement to all actions except those listed.
not_actions = [
"iam:ChangePassword",
"iam:CreateLoginProfile",
"iam:CreateVirtualMFADevice",
"iam:DeleteVirtualMFADevice",
"iam:ListVirtualMFADevices",
"iam:EnableMFADevice",
"iam:ResyncMFADevice",
"iam:ListAccountAliases",
"iam:ListUsers",
"iam:ListSSHPublicKeys",
"iam:ListAccessKeys",
"iam:ListServiceSpecificCredentials",
"iam:ListMFADevices",
"iam:GetAccountSummary",
"sts:GetSessionToken",
]
resources = [
"*",
]
condition {
test = "BoolIfExists"
variable = "aws:MultiFactorAuthPresent"
values = ["false"]
}
}
}
resource "aws_iam_policy" "main" {
#Use alphanumeric and '+=,.@-_' characters. Maximum 128 characters.
name = "enforce-mfa"
path = "/"
description = "Requires valid MFA security token for all API calls except those needed for managing a user's own IAM user."
policy = data.aws_iam_policy_document.main.json
}
resource "aws_iam_group_policy_attachment" "main" {
count = length(var.iam_groups)
group = element(var.iam_groups, count.index)
policy_arn = aws_iam_policy.main.arn
}
resource "aws_iam_user_policy_attachment" "main" {
count = length(var.iam_users)
user = element(var.iam_users, count.index)
policy_arn = aws_iam_policy.main.arn
}