-
Notifications
You must be signed in to change notification settings - Fork 5
/
auth.yaml
executable file
·355 lines (334 loc) · 11 KB
/
auth.yaml
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
AWSTemplateFormatVersion: "2010-09-09"
Description: "(SO0222auth) - Amazon Marketing Cloud Uploader from AWS. This AWS CloudFormation template defines Cognito resources. %%VERSION%%"
Parameters:
ParentStackName:
Description: Parent stack name
Type: String
AdminEmail:
Description: Email address of the solution administrator
Type: String
DataBucketName:
Description: Name of the first-party data source bucket
Type: String
RestApiId:
Description: REST API ID
Type: String
CognitoUISubdomain:
Description: Prefix for Cognito hosted UI
Type: String
CognitoCallbackURL:
Description: Redirect URL after successful sign-in
Type: String
CognitoLogoutURL:
Description: Redirect URL after successful logout
Type: String
Resources:
UserPool:
Type: AWS::Cognito::UserPool
Properties:
MfaConfiguration: OPTIONAL
UserPoolAddOns:
AdvancedSecurityMode: "ENFORCED"
EnabledMfas:
- SOFTWARE_TOKEN_MFA
AdminCreateUserConfig:
AllowAdminCreateUserOnly: True
InviteMessageTemplate:
EmailMessage: !Join ["", [
"<p>Please use the credentials below to login to the AMC Uploader console.</p>",
"<p>Console: <strong>",
!Ref CognitoCallbackURL,
"</strong></p>",
"<p>Username: <strong>{username}</strong></p>",
"<p>Password: <strong>{####}</strong></p>",
"<p>Stack name: <strong>",
!Ref ParentStackName,
"</strong></p>"
]]
EmailSubject: "Welcome to Amazon Marketing Cloud uploader from AWS"
EmailConfiguration:
EmailSendingAccount: 'COGNITO_DEFAULT'
AutoVerifiedAttributes: ['email']
UserPoolDomain:
Type: AWS::Cognito::UserPoolDomain
Properties:
Domain: !Ref CognitoUISubdomain
UserPoolId: !Ref UserPool
UserPoolRiskConfiguration:
Type: AWS::Cognito::UserPoolRiskConfigurationAttachment
Properties:
UserPoolId: !Ref UserPool
ClientId: "ALL"
AccountTakeoverRiskConfiguration:
Actions:
HighAction:
EventAction: "MFA_REQUIRED"
Notify: False
MediumAction:
EventAction: "MFA_IF_CONFIGURED"
Notify: False
LowAction:
EventAction: "MFA_IF_CONFIGURED"
Notify: False
WebAppClient:
Type: AWS::Cognito::UserPoolClient
Properties:
AllowedOAuthFlows:
- code
AllowedOAuthFlowsUserPoolClient: true
AllowedOAuthScopes:
- openid
- profile
UserPoolId: !Ref UserPool
CallbackURLs:
- !Ref CognitoCallbackURL
LogoutURLs:
- !Ref CognitoLogoutURL
SupportedIdentityProviders:
- COGNITO
# Service - cognito / security infrastructure
# CognitoRoleMappingTransformer is a hack meant to workaround
# Cognito's (current) lack of CF support. References:
# https://forums.aws.amazon.com/message.jspa?messageID=790437#790437
# https://stackoverflow.com/questions/53131052/aws-cloudformation-can-not-create-stack-when-awscognitoidentitypoolroleattac
CognitoRoleMappingTransformer:
Type: AWS::Lambda::Function
Metadata:
cfn_nag:
rules_to_suppress:
- id: W58
reason: "The role includes permission to write to CloudWatch Logs"
- id: W89
reason: "This resource does not need to access any other resource provisioned within a VPC."
- id: W92
reason: "This function does not performance optimization, so the default concurrency limits suffice."
Properties:
Code:
ZipFile: |
import json
import cfnresponse
def handler(event, context):
print("Event: %s" % json.dumps(event))
resourceProperties = event["ResourceProperties"]
responseData = {
"RoleMapping": {
resourceProperties["IdentityProvider"]: {
"Type": resourceProperties["Type"]
}
}
}
if resourceProperties["AmbiguousRoleResolution"]:
responseData["RoleMapping"][resourceProperties["IdentityProvider"]]["AmbiguousRoleResolution"] = \
resourceProperties["AmbiguousRoleResolution"]
print(responseData)
cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData)
Handler: !Join
- ''
- - index
- .handler
Role: !GetAtt CognitoRoleMapperLambdaExecutionRole.Arn
Runtime: python3.12
Timeout: 30
CognitoRoleMappingTransformerLogGroup:
Type: AWS::Logs::LogGroup
Metadata:
cfn_nag:
rules_to_suppress:
- id: W84
reason: "The data generated via this role does not need to be encrypted."
Properties:
LogGroupName: !Join ['/', ['/aws/lambda', !Ref CognitoRoleMappingTransformer]]
RetentionInDays: 3653
CognitoRoleMapperLambdaExecutionRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- 'sts:AssumeRole'
Path: /
Policies:
- PolicyName: root
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 'logs:CreateLogStream'
- 'logs:PutLogEvents'
Resource: 'arn:aws:logs:*:*:*'
Metadata:
guard:
SuppressedRules:
- IAM_NO_INLINE_POLICY_CHECK
IdentityPool:
Type: AWS::Cognito::IdentityPool
Properties:
AllowUnauthenticatedIdentities: False
CognitoIdentityProviders:
- ClientId: !Ref WebAppClient
ProviderName: !GetAtt UserPool.ProviderName
CognitoStandardAuthDefaultRole:
Type: "AWS::IAM::Role"
Metadata:
cfn_nag:
rules_to_suppress:
- id: F38
reason: "The wildcard is used for a deny action, not an allow action."
guard:
SuppressedRules:
- IAM_NO_INLINE_POLICY_CHECK
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Federated: "cognito-identity.amazonaws.com"
Action:
- "sts:AssumeRoleWithWebIdentity"
Condition:
StringEquals:
"cognito-identity.amazonaws.com:aud": !Ref IdentityPool
"ForAnyValue:StringEquals":
"cognito-identity.amazonaws.com:amr": authenticated
Policies:
- PolicyName: !Sub "${AWS::StackName}-AuthNoGroup"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Action: "*"
Resource: "*"
Effect: "Deny"
CognitoStandardUnauthDefaultRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Federated: "cognito-identity.amazonaws.com"
Action:
- "sts:AssumeRoleWithWebIdentity"
Condition:
StringEquals:
"cognito-identity.amazonaws.com:aud": !Ref IdentityPool
"ForAnyValue:StringEquals":
"cognito-identity.amazonaws.com:amr": unauthenticated
IdentityPoolRoleMapping:
Type: AWS::Cognito::IdentityPoolRoleAttachment
Properties:
IdentityPoolId: !Ref IdentityPool
RoleMappings:
TransformedRoleMapping:
IdentityProvider:
'Fn::Join':
- ':'
- - 'Fn::GetAtt':
- UserPool
- ProviderName
- Ref: WebAppClient
AmbiguousRoleResolution: Deny
Type: Token
Roles:
authenticated: !GetAtt CognitoStandardAuthDefaultRole.Arn
unauthenticated: !GetAtt CognitoStandardUnauthDefaultRole.Arn
AdminGroup:
Type: AWS::Cognito::UserPoolGroup
Properties:
Description: 'User group for solution administrators'
RoleArn: !GetAtt AdminRole.Arn
UserPoolId: !Ref UserPool
GroupName: !Sub "${AWS::StackName}-Admins"
AdminAccount:
Type: AWS::Cognito::UserPoolUser
Properties:
DesiredDeliveryMediums:
- EMAIL
UserAttributes: [{"Name": "email", "Value": !Ref AdminEmail}]
Username: !Ref AdminEmail
UserPoolId: !Ref UserPool
AdminRole:
Type: "AWS::IAM::Role"
Metadata:
guard:
SuppressedRules:
- IAM_NO_INLINE_POLICY_CHECK
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Federated: "cognito-identity.amazonaws.com"
Action:
- "sts:AssumeRoleWithWebIdentity"
Condition:
StringEquals:
"cognito-identity.amazonaws.com:aud": !Ref IdentityPool
"ForAnyValue:StringEquals":
"cognito-identity.amazonaws.com:amr": authenticated
Policies:
- PolicyName: !Sub "${AWS::StackName}-AdminPolicy"
PolicyDocument: !Sub
- |-
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"execute-api:Invoke"
],
"Effect": "Allow",
"Resource": [
"arn:aws:execute-api:${region}:${account}:${restApi}/*"
]
},
{
"Action": [
"s3:PutObject"
],
"Effect": "Allow",
"Resource": [
"arn:aws:s3:::${DataBucketName}/public/*"
]
},
{
"Action": [
"s3:ListBucket"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::${DataBucketName}"
}
]
}
- {
region: !Ref "AWS::Region",
account: !Ref "AWS::AccountId",
restApi: !Ref RestApiId,
DataBucketName: !Ref DataBucketName
}
AddAdminUserToAdminGroup:
DependsOn: AdminAccount
Type: AWS::Cognito::UserPoolUserToGroupAttachment
Properties:
GroupName: !Ref AdminGroup
Username: !Ref AdminEmail
UserPoolId: !Ref UserPool
Outputs:
AdminRoleArn:
Value: !GetAtt AdminRole.Arn
UserPoolId:
Value: !Ref UserPool
IdentityPoolId:
Value: !Ref IdentityPool
UserPoolClientId:
Value: !Ref WebAppClient
HostedUIDomain:
Value: !Sub "${CognitoUISubdomain}.auth.${AWS::Region}.amazoncognito.com"