generated from BCDevOps/bcgov-terraform-module-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
175 lines (160 loc) · 4.9 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
data "keycloak_realm" "kc-lz-sso-realm" {
realm = var.kc_realm
}
data "http" "saml_idp_metadata" {
url = "${var.kc_base_url}/realms/${var.kc_realm}/protocol/saml/descriptor"
}
resource "aws_iam_saml_provider" "default" {
name = var.aws_saml_idp_name
saml_metadata_document = data.http.saml_idp_metadata.response_body
}
resource "aws_iam_role" "admin_role" {
for_each = var.account_roles
name = each.key
max_session_duration = 21600
permissions_boundary = aws_iam_policy.bcgov_perm_boundary.arn
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::${data.aws_caller_identity.aws_context.account_id}:saml-provider/${var.aws_saml_idp_name}"
},
"Action": "sts:AssumeRoleWithSAML",
"Condition": {
"StringEquals": {
"SAML:aud": ${jsonencode(var.trusted_login_sources)}
}
}
}
]
}
EOF
}
resource "aws_iam_policy" "bcgov_perm_boundary" {
name = "BCGOV_Permission_Boundary"
description = "Policy to restrict actions on BCGov Resources"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "*"
Effect = "Allow"
Resource = "*"
Sid = "AllowAdminAccess"
},
{
Action = "iam:*Provider"
Effect = "Deny"
Resource = "*"
Sid = "DenyPermBoundaryBCGovIDPAlteration"
},
{
Action = "elasticloadbalancing:DeleteLoadBalancer"
Effect = "Deny"
Resource = "arn:aws:elasticloadbalancing:ca-central-1:*:loadbalancer/app/default/*"
Sid = "DenyPermBoundaryALBAlteration"
},
{
Action = [
"iam:Create*",
"iam:Update*",
"iam:Delete*",
"iam:DetachRolePolicy",
"iam:DeleteRolePolicy"
]
Effect = "Deny"
Resource = [
"arn:aws:iam::*:policy/BCGOV*",
"arn:aws:iam::*:role/CloudCustodian",
"arn:aws:iam::*:role/AWSCloudFormationStackSetExecutionRole",
"arn:aws:iam::*:role/*BCGOV*",
"arn:aws:iam::*:instance-profile/EC2-Default-SSM-AD-Role-ip"
]
Sid = "DenyPermBoundaryBCGovAlteration"
},
{
Action = [
"budgets:DeleteBudgetAction",
"budgets:UpdateBudgetAction",
"budgets:ModifyBudget"
]
Effect = "Deny"
Resource = "arn:aws:budgets::*:budget/Default*"
Sid = "DenyDefaultBudgetAlteration"
},
{
Action = [
"iam:DeleteInstanceProfile",
"iam:RemoveRoleFromInstanceProfile"
]
Effect = "Deny"
Resource = "arn:aws:iam::*:instance-profile/EC2-Default-SSM-AD-Role-ip"
Sid = "DenyDefaultInstanceProfileAlteration"
},
{
Action = [
"kms:ScheduleKeyDeletion",
"kms:DeleteAlias",
"kms:DisableKey",
"kms:UpdateAlias"
]
Effect = "Deny"
Resource = "*"
Condition = {
"ForAnyValue:StringEquals" = {
"aws:ResourceTag/Accelerator" = "PBMM"
}
}
Sid = "DenyDefaultKMSAlteration"
},
{
Action = [
"ssm:DeleteParameter*",
"ssm:PutParameter"
],
Effect = "Deny",
"Resource" : [
"arn:aws:ssm:*:*:parameter/cdk-bootstrap/pbmmaccel/*",
"arn:aws:ssm:*:*:parameter/octk/*"
],
Sid = "DenyDefaultParameterStoreAlteration"
},
{
Action = [
"secretsmanager:DeleteSecret",
"secretsmanager:CreateSecret",
"secretsmanager:UpdateSecret"
]
Effect = "Deny"
Resource = "arn:aws:secretsmanager:*:*:secret:accelerator*"
Sid = "DenyDefaultSecretManagerAlteration"
}
]
})
}
resource "aws_iam_role_policy_attachment" "role-policy-attach" {
depends_on = [aws_iam_role.admin_role]
for_each = var.account_roles
role = each.key
policy_arn = each.value
}
data "aws_caller_identity" "aws_context" {}
module "cloud_roles" {
source = "github.com/BCDevOps/terraform-keycloak-role-group-simplification"
realm = var.kc_realm
iam_auth_client_id = var.kc_iam_auth_client_id
// module operates on a list of accounts. this allows us to define a bunch of projects and get all the accounts created for them.
// @todo fix this so it supports distinct values for each of hte attributes insttead of va.raccount_name for everything
accounts = [{
project_identifier = var.account_name
project_name = var.account_name
name = var.account_name
environment = var.account_name
account_number = data.aws_caller_identity.aws_context.account_id
}]
role_names = [for role, arn in var.account_roles : role]
idp_name = var.aws_saml_idp_name
}