-
Notifications
You must be signed in to change notification settings - Fork 17
/
content-localization-on-aws-auth.yaml
executable file
·320 lines (300 loc) · 10.7 KB
/
content-localization-on-aws-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
AWSTemplateFormatVersion: "2010-09-09"
Description: "Content Localization on AWS user authentication infastructure %%VERSION%%"
Parameters:
AdminEmail:
Description: Email address of the Content Localization Administrator
Type: String
WorkflowApiId:
Description: REST API ID of the Media Insights on AWS Workflow API
Type: String
DataplaneApiId:
Description: REST API ID of the Media Insights on AWS Dataplane API
Type: String
SearchDomainArn:
Description: ARN of the Opensearch Domain
Type: String
DataplaneBucket:
Description: Name of the Media Insights on AWS dataplane bucket
Type: String
ParentStackName:
Description: Name of the parent Cloud Formation stack
Type: String
MieKMSArn:
Description: ARN of the Media Insights KMS Key
Type: String
Resources:
ContentAnalysisUserPool:
Type: AWS::Cognito::UserPool
Properties:
AdminCreateUserConfig:
AllowAdminCreateUserOnly: True
InviteMessageTemplate:
EmailMessage: !Join ["", [
"Your username is {username} and temporary password is {####}<br>AWS CloudFormation stack:<br>",
"https://",
Ref: AWS::Region,
".console.aws.amazon.com/cloudformation/home?region=",
Ref: AWS::Region,
"#/stacks/stackinfo?stackId=",
Ref: ParentStackName
]]
EmailSubject: "Welcome to Content Localization on AWS"
EmailConfiguration:
EmailSendingAccount: 'COGNITO_DEFAULT'
AutoVerifiedAttributes: ['email']
ContentAnalysisWebAppClient:
Type: AWS::Cognito::UserPoolClient
Properties:
UserPoolId: !Ref ContentAnalysisUserPool
# Service - cognito / security infrastructure
# Super hacky lambda for formatting cognito role mapping since cognito is severely lacking in CF support
# 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: 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.11
Timeout: 30
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:CreateLogGroup'
- 'logs:CreateLogStream'
- 'logs:PutLogEvents'
Resource: 'arn:aws:logs:*:*:*'
# TODO: Do we even need this?
# ContentAnalysisCognitoDomain:
# Type: AWS::Cognito::UserPoolDomain
# Properties:
# Domain: !Ref # TODO: Figure out what to do here
# UserPoolId: !Ref ContentAnalysisUserPool
ContentAnalysisIdentityPool:
Type: AWS::Cognito::IdentityPool
Properties:
AllowUnauthenticatedIdentities: False
CognitoIdentityProviders:
- ClientId: !Ref ContentAnalysisWebAppClient
ProviderName: !GetAtt ContentAnalysisUserPool.ProviderName
# More hacky cfn for getting the role mapping
TransformedRoleMapping:
Type: Custom::TransformedRoleMapping
Properties:
ServiceToken: !GetAtt CognitoRoleMappingTransformer.Arn
Type: Token
AmbiguousRoleResolution: Deny
IdentityProvider:
'Fn::Join':
- ':'
- - 'Fn::GetAtt':
- ContentAnalysisUserPool
- ProviderName
- Ref: ContentAnalysisWebAppClient
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."
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 ContentAnalysisIdentityPool
"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 ContentAnalysisIdentityPool
"ForAnyValue:StringEquals":
"cognito-identity.amazonaws.com:amr": unauthenticated
ContentAnalysisIdentityPoolRoleMapping:
Type: AWS::Cognito::IdentityPoolRoleAttachment
Properties:
IdentityPoolId: !Ref ContentAnalysisIdentityPool
RoleMappings: !GetAtt TransformedRoleMapping.RoleMapping
Roles:
authenticated: !GetAtt CognitoStandardAuthDefaultRole.Arn
unauthenticated: !GetAtt CognitoStandardUnauthDefaultRole.Arn
ContentAnalysisAdminGroup:
Type: AWS::Cognito::UserPoolGroup
Properties:
Description: 'User group for Content Localization on AWS Admins'
RoleArn: !GetAtt ContentLocalizationAdminRole.Arn
UserPoolId: !Ref ContentAnalysisUserPool
GroupName: !Sub "${AWS::StackName}-Admins"
ContentAnalysisAdminAccount:
Type: AWS::Cognito::UserPoolUser
Properties:
DesiredDeliveryMediums:
- EMAIL
UserAttributes: [{"Name": "email", "Value": !Ref AdminEmail}]
Username: !Ref AdminEmail
UserPoolId: !Ref ContentAnalysisUserPool
# TODO: Need to add S3 put access to dataplane bucket on public/upload/*
ContentLocalizationAdminRole:
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 ContentAnalysisIdentityPool
"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}:${wfapi}/*", "arn:aws:execute-api:${region}:${account}:${dataapi}/*"]
},
{
"Action": [
"s3:PutObject"
],
"Effect": "Allow",
"Resource": [
"arn:aws:s3:::${dataplanebucket}/public/*"
]
},
{
"Action": [
"s3:ListBucket"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::${dataplanebucket}"
},
{
"Action": [
"es:ESHttpPost",
"es:ESHttpGet"
],
"Effect": "Allow",
"Resource": "${searchdomain}/*"
},
{
"Action": [
"kms:GenerateDataKey*",
"kms:DescribeKey",
"kms:Encrypt",
"kms:Decrypt",
"kms:ReEncrypt*"
],
"Effect": "Allow",
"Resource": "${kmskey}"
}
]
}
- {
region: !Ref "AWS::Region",
account: !Ref "AWS::AccountId",
wfapi: !Ref WorkflowApiId,
dataapi: !Ref DataplaneApiId,
searchdomain: !Ref SearchDomainArn,
dataplanebucket: !Ref DataplaneBucket,
kmskey: !Ref MieKMSArn
}
AddAdminUserToAdminGroup:
DependsOn: ContentAnalysisAdminAccount
Type: AWS::Cognito::UserPoolUserToGroupAttachment
Properties:
GroupName: !Ref ContentAnalysisAdminGroup
Username: !Ref AdminEmail
UserPoolId: !Ref ContentAnalysisUserPool
Outputs:
AdminRoleArn:
Value: !GetAtt ContentLocalizationAdminRole.Arn
UserPoolId:
Value: !Ref ContentAnalysisUserPool
IdentityPoolId:
Value: !Ref ContentAnalysisIdentityPool
UserPoolClientId:
Value: !Ref ContentAnalysisWebAppClient