-
Notifications
You must be signed in to change notification settings - Fork 23
/
aws_sso.tf
226 lines (189 loc) · 7.62 KB
/
aws_sso.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
# vim:ts=2:sts=2:sw=2:et
#
# Author: Hari Sekhon
# Date: 2022-09-02 09:54:20 +0100 (Fri, 02 Sep 2022)
#
# https://github.com/HariSekhon/Terraform
#
# License: see accompanying Hari Sekhon LICENSE file
#
# If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish
#
# https://www.linkedin.com/in/HariSekhon
#
# ============================================================================ #
# A W S S S O
# ============================================================================ #
# https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ssoadmin_account_assignment
# Data source to get the SSO ARN + IDP ID
data "aws_ssoadmin_instances" "my" {}
# ====================
locals {
# Convenience variables for shortness - would use var instead of local if this wasn't really tied to the specific account, modules across accounts won't add much here
sso_arn = tolist(data.aws_ssoadmin_instances.my.arns)[0]
sso_idp = tolist(data.aws_ssoadmin_instances.my.identity_store_ids)[0]
# get the IDP ID from:
#
# https://eu-west-2.console.aws.amazon.com/singlesignon/identity/home?region=eu-west-2#!/settings
#
# get the group list via this AWS CLI command:
#
# aws identitystore list-groups --identity-store-id d-1a234567b8 | jq -r '.Groups[].DisplayName' | sort | sed 's/^/ "/; s/$/",/'
#
groups = [
"admins",
]
}
# ====================
# Output all available SSO + IDP
#
output "aws-sso-arns" {
value = data.aws_ssoadmin_instances.my.arns # a set
}
output "aws-sso-identity_store_ids" {
value = data.aws_ssoadmin_instances.my.identity_store_ids # a set
}
# ====================
# Show the SSO + IDP we're working on
#
output "aws-sso-arn" {
value = local.sso_arn
}
output "aws-sso-idp" {
value = local.sso_idp
}
# =======================
# Create a Permission Set
resource "aws_ssoadmin_permission_set" "admin" {
name = "AWSAdministratorAccess"
description = "Provides full access to AWS services and resources"
instance_arn = local.sso_arn
#relay_state = "https://s3.console.aws.amazon.com/s3/home?region=us-east-1#"
session_duration = "PT12H" # valid duration, default: PT1H
}
resource "aws_ssoadmin_permission_set" "poweruser" {
name = "AWSPowerUserAccess"
description = "Provides full access to AWS services and resources, but does not allow management of Users and groups"
instance_arn = local.sso_arn
#relay_state = "https://s3.console.aws.amazon.com/s3/home?region=us-east-1#"
session_duration = "PT12H" # valid duration, default: PT1H
}
resource "aws_ssoadmin_permission_set" "readonly" {
name = "AWSReadOnlyAccess"
description = "This policy grants permissions to view resources and basic metadata across all AWS services"
instance_arn = local.sso_arn
#relay_state = "https://s3.console.aws.amazon.com/s3/home?region=us-east-1#"
session_duration = "PT12H" # valid duration, default: PT1H
}
resource "aws_ssoadmin_permission_set" "org-admin" {
name = "AWSOrganizationsFullAccess"
instance_arn = local.sso_arn
#relay_state = "https://s3.console.aws.amazon.com/s3/home?region=us-east-1#"
session_duration = "PT12H" # valid duration, default: PT1H
}
resource "aws_ssoadmin_permission_set" "catalog-admin" {
name = "AWSServiceCatalogAdminFullAccess"
instance_arn = local.sso_arn
#relay_state = "https://s3.console.aws.amazon.com/s3/home?region=us-east-1#"
session_duration = "PT12H" # valid duration, default: PT1H
}
resource "aws_ssoadmin_permission_set" "catalog-user" {
name = "AWSServiceCatalogEndUserAccess"
instance_arn = local.sso_arn
#relay_state = "https://s3.console.aws.amazon.com/s3/home?region=us-east-1#"
session_duration = "PT12H" # valid duration, default: PT1H
}
resource "aws_ssoadmin_permission_set" "billing" {
name = "Billing"
description = "Grants Billing access"
instance_arn = local.sso_arn
#relay_state = "https://s3.console.aws.amazon.com/s3/home?region=us-east-1#"
session_duration = "PT12H" # valid duration, default: PT1H
}
# ===================
# Get the permission set's arn
#data "aws_ssoadmin_permission_set" "admin" {
# instance_arn = local.sso_arn
# name = "AWSAdministratorAccess"
#}
#
#data "aws_ssoadmin_permission_set" "poweruser" {
# instance_arn = local.sso_arn
# name = "AWSPowerUserAccess"
#}
#
#data "aws_ssoadmin_permission_set" "readonly" {
# instance_arn = local.sso_arn
# name = "AWSReadOnlyAccess"
#}
data "aws_ssoadmin_permission_set" "org-admin" {
instance_arn = local.sso_arn
name = "AWSOrganizationsFullAccess"
}
data "aws_ssoadmin_permission_set" "catalog-admin" {
instance_arn = local.sso_arn
name = "AWSServiceCatalogAdminFullAccess"
}
data "aws_ssoadmin_permission_set" "catalog-user" {
instance_arn = local.sso_arn
name = "AWSServiceCatalogEndUserAccess"
}
# =====================
# Get the group's ID by name filter
#
# aws identitystore list-groups --identity-store-id d-1a234567b8 | jq -r '.Groups[].DisplayName' | sort
data "aws_identitystore_group" "my-group" { # XXX: Edit
identity_store_id = local.sso_idp
filter {
attribute_path = "DisplayName"
attribute_value = "My Group" # XXX: Edit
}
}
# =========================================
# Assign
resource "aws_ssoadmin_account_assignment" "admins" {
#instance_arn = data.aws_ssoadmin_permission_set.my-permset.instance_arn # should be same as local.sso_arn
instance_arn = local.sso_arn
permission_set_arn = data.aws_ssoadmin_permission_set.my-permset.arn
principal_id = data.aws_identitystore_group.my-group.group_id
principal_type = "GROUP" # should always be group - don't assign things on a per user basis - that is against best practice and maintainability
target_id = "012347678910" # Required: AWS Account ID
target_type = "AWS_ACCOUNT" # Optional: AWS_ACCOUNT is the only valid value, but if you omit this and then later backport eg. using terraform_import_aws_sso_account_assignment.sh in DevOps Bash tools repo, then this missing field will force replacement unless you add it back in
}
# ==============================
# Attach AWS Managed Policy
resource "aws_ssoadmin_managed_policy_attachment" "my-policy-attachment" {
instance_arn = local.sso_arn
managed_policy_arn = "arn:aws:iam::aws:policy/AWSAdministratorAccess"
permission_set_arn = aws_ssoadmin_permission_set.my-permset.arn
}
# ==============================
# Attach Customer Managed Policy - must already be created in each AWS account where you want this applied
resource "aws_ssoadmin_customer_managed_policy_attachment" "my-customer-policy-attachment" {
instance_arn = local.sso_arn
permission_set_arn = aws_ssoadmin_permission_set.my-permset.arn
customer_managed_policy_reference {
name = aws_iam_policy.my-policy.name # define this aws_iam_policy elsewhere
path = "/"
}
}
# =============================
# Inline Policy on a Permissions Set - can only be 1 per permission set
data "aws_iam_policy_document" "my-inline-policy-document" {
statement {
sid = "1"
actions = [
"s3:ListAllMyBuckets",
"s3:GetBucketLocation",
]
resources = [
"arn:aws:s3:::*",
]
}
}
resource "aws_ssoadmin_permission_set_inline_policy" "my-inline-policy" {
inline_policy = data.aws_iam_policy_document.my-inline-policy-document.json
#inline_policy = file("${path.module}/sso_poweruser_policy.json") # for example to lock out access to Secrets Manager
instance_arn = local.sso_arn
permission_set_arn = aws_ssoadmin_permission_set.my-permset.arn
}