forked from aws-samples/cloudfront-authorization-at-edge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yaml
506 lines (476 loc) · 18.6 KB
/
template.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: >
Protect downloads of your content hosted on CloudFront with Cognito authentication using Lambda@Edge
Metadata:
AWS::ServerlessRepo::Application:
Name: cloudfront-authorization-at-edge
Description: >
Protect downloads of your content hosted on CloudFront with Cognito authentication using Lambda@Edge.
Includes: S3 Bucket w. sample SPA, Cognito User Pool with hosted UI set up, Lambda@Edge functions to validate JWT's and handle OAuth2 redirects.
Author: AWS Samples
SpdxLicenseId: MIT-0
LicenseUrl: LICENSE
ReadmeUrl: SERVERLESS-REPO.md
Labels:
[
"cognito",
"lambda@edge",
"cloudfront",
"s3",
"react",
"angular",
"vue",
"amplify",
]
HomePageUrl: https://github.com/aws-samples/cloudfront-authorization-at-edge
SemanticVersion: 1.0.1
SourceCodeUrl: https://github.com/aws-samples/cloudfront-authorization-at-edge
Parameters:
EmailAddress:
Type: String
Description: The email address of the user that will be created in the Cognito User Pool. Leave empty to skip user creation.
Default: ""
RedirectPathSignIn:
Type: String
Description: The URL path that should handle the redirect from Cognito after sign-in
Default: /parseauth
RedirectPathSignOut:
Type: String
Description: The URL path that should handle the redirect from Cognito after sign-out
Default: /
RedirectPathAuthRefresh:
Type: String
Description: The URL path that should handle the JWT refresh request
Default: /refreshauth
SignOutUrl:
Type: String
Description: The URL path that you can visit to sign-out
Default: /signout
AlternateDomainNames:
Type: CommaDelimitedList
Description: "If you intend to use one or more custom domain names for the CloudFront distribution, please set that up yourself on the CloudFront distribution after deployment. If you provide those domain names now (comma-separated) the necessary Cognito configuration will already be done for you. Alternatively, update the Cognito configuration yourself after deployment: add sign in and sign out URLs for your custom domains to the user pool app client settings."
Default: ""
CookieSettings:
Type: String
Description: The settings for the cookies holding the JWT's. To be provided as a JSON object
Default: >-
{
"idToken": "Path=/; Secure; SameSite=Lax",
"accessToken": "Path=/; Secure; SameSite=Lax",
"refreshToken": "Path=/; Secure; SameSite=Lax",
"nonce": "Path=/; Secure; HttpOnly; Max-Age=300; SameSite=Lax"
}
OAuthScopes:
Type: String
Description: The OAuth scopes to request the User Pool to add to the access token JWT. To be provided as a JSON array
Default: >-
["phone", "email", "profile", "openid", "aws.cognito.signin.user.admin"]
HttpHeaders:
Type: String
Description: The HTTP headers to set on all responses from CloudFront. To be provided as a JSON object
Default: >-
{
"Content-Security-Policy": "default-src 'none'; img-src 'self'; script-src 'self'; style-src 'self'; object-src 'none'; connect-src 'self' https://*.amazonaws.com https://*.amazoncognito.com",
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
"Referrer-Policy": "same-origin",
"X-XSS-Protection": "1; mode=block",
"X-Frame-Options": "DENY",
"X-Content-Type-Options": "nosniff"
}
CreateCloudFrontDistribution:
Type: String
Description: Set to 'false' to skip the creation of a CloudFront distribution and associated resources, such as the private S3 bucket and the sample React app. This may be of use to you, if you just want to create the Lambda@Edge functions to use with your own CloudFront distribution.
Default: "true"
Conditions:
CreateUser: !Not [!Equals [!Ref EmailAddress, ""]]
CreateCloudFrontDistribution: !Not [!Equals [!Ref CreateCloudFrontDistribution, "false"]]
Globals:
Function:
Timeout: 60
Resources:
S3Bucket:
Type: AWS::S3::Bucket
Condition: CreateCloudFrontDistribution
Properties:
AccessControl: Private
CheckAuthHandler:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/lambda-edge/check-auth/
Handler: bundle.handler
Runtime: nodejs8.10 # Currently for Lambda@Edge still a faster runtime than nodejs10.x
Role: !GetAtt LambdaEdgeExecutionRole.Arn
Timeout: 5
ParseAuthHandler:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/lambda-edge/parse-auth/
Handler: bundle.handler
Runtime: nodejs8.10 # Currently for Lambda@Edge still a faster runtime than nodejs10.x
Role: !GetAtt LambdaEdgeExecutionRole.Arn
Timeout: 5
RefreshAuthHandler:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/lambda-edge/refresh-auth/
Handler: bundle.handler
Runtime: nodejs8.10 # Currently for Lambda@Edge still a faster runtime than nodejs10.x
Role: !GetAtt LambdaEdgeExecutionRole.Arn
Timeout: 5
HttpHeadersHandler:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/lambda-edge/http-headers/
Handler: bundle.handler
Runtime: nodejs8.10 # Currently for Lambda@Edge still a faster runtime than nodejs10.x
Role: !GetAtt LambdaEdgeExecutionRole.Arn
Timeout: 5
SignOutHandler:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/lambda-edge/sign-out/
Handler: bundle.handler
Runtime: nodejs8.10 # Currently for Lambda@Edge still a faster runtime than nodejs10.x
Role: !GetAtt LambdaEdgeExecutionRole.Arn
Timeout: 5
LambdaEdgeExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- edgelambda.amazonaws.com
- lambda.amazonaws.com
Action:
- sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
CloudFrontDistribution:
Type: AWS::CloudFront::Distribution
Condition: CreateCloudFrontDistribution
Properties:
DistributionConfig:
CacheBehaviors:
- PathPattern: !Ref RedirectPathSignIn
Compress: true
ForwardedValues:
QueryString: true
LambdaFunctionAssociations:
- EventType: viewer-request
LambdaFunctionARN: !GetAtt ParseAuthHandlerCodeUpdate.FunctionArn
TargetOriginId: dummy-origin
ViewerProtocolPolicy: redirect-to-https
- PathPattern: !Ref RedirectPathAuthRefresh
Compress: true
ForwardedValues:
QueryString: true
LambdaFunctionAssociations:
- EventType: viewer-request
LambdaFunctionARN: !GetAtt RefreshAuthHandlerCodeUpdate.FunctionArn
TargetOriginId: dummy-origin
ViewerProtocolPolicy: redirect-to-https
- PathPattern: !Ref SignOutUrl
Compress: true
ForwardedValues:
QueryString: true
LambdaFunctionAssociations:
- EventType: viewer-request
LambdaFunctionARN: !GetAtt SignOutHandlerCodeUpdate.FunctionArn
TargetOriginId: dummy-origin
ViewerProtocolPolicy: redirect-to-https
DefaultCacheBehavior:
Compress: true
ForwardedValues:
QueryString: true
LambdaFunctionAssociations:
- EventType: viewer-request
LambdaFunctionARN: !GetAtt CheckAuthHandlerCodeUpdate.FunctionArn
- EventType: origin-response
LambdaFunctionARN: !GetAtt HttpHeadersHandlerCodeUpdate.FunctionArn
TargetOriginId: protected-bucket
ViewerProtocolPolicy: redirect-to-https
Enabled: true
Origins:
- DomainName: !Sub "${S3Bucket}.s3.amazonaws.com"
Id: protected-bucket
S3OriginConfig:
OriginAccessIdentity:
!Sub "origin-access-identity/cloudfront/${CloudFrontOriginAccessIdentity}"
- DomainName: example.org
Id: dummy-origin
CustomOriginConfig:
OriginProtocolPolicy: match-viewer
CustomErrorResponses:
- ErrorCode: 403
ResponseCode: 200
ResponsePagePath: /index.html
- ErrorCode: 404
ResponseCode: 200
ResponsePagePath: /index.html
CloudFrontOriginAccessIdentity:
Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
Condition: CreateCloudFrontDistribution
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: "CloudFront OAI"
CloudfrontBucketPolicy:
Type: AWS::S3::BucketPolicy
Condition: CreateCloudFrontDistribution
Properties:
Bucket: !Ref S3Bucket
PolicyDocument:
Version: "2012-10-17"
Statement:
- Action:
- "s3:GetObject"
Effect: "Allow"
Resource: !Join ["/", [!GetAtt S3Bucket.Arn, "*"]]
Principal:
CanonicalUser: !GetAtt CloudFrontOriginAccessIdentity.S3CanonicalUserId
UserPool:
Type: AWS::Cognito::UserPool
Properties:
AdminCreateUserConfig:
AllowAdminCreateUserOnly: true
UserPoolName: !Sub "${AWS::Region}-${AWS::StackName}-${AWS::AccountId}"
UsernameAttributes:
- email
User:
Type: AWS::Cognito::UserPoolUser
Condition: CreateUser
Properties:
Username: !Ref EmailAddress
UserPoolId: !Ref UserPool
UserPoolClient:
Type: AWS::Cognito::UserPoolClient
Properties:
UserPoolId: !Ref UserPool
UserPoolDomain:
Type: Custom::UserPoolDomain
Properties:
ServiceToken: !GetAtt UserPoolDomainHandler.Arn
UserPoolId: !Ref UserPool
UserPoolDomainHandler:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/cfn-custom-resources/user-pool-domain/
Handler: index.handler
Runtime: nodejs10.x
Policies:
- Version: "2012-10-17"
Statement:
- Effect: Allow
Action: cognito-idp:DescribeUserPoolDomain
Resource: "*"
- Effect: Allow
Action:
- cognito-idp:CreateUserPoolDomain
- cognito-idp:DeleteUserPoolDomain
Resource: !GetAtt UserPool.Arn
UserPoolClientUpdate:
Type: Custom::UserPoolClientUpdate
Properties:
ServiceToken: !GetAtt UserPoolClientUpdateHandler.Arn
UserPoolId: !Ref UserPool
UserPoolClientId: !Ref UserPoolClient
CloudFrontDistributionDomainName:
!If [ CreateCloudFrontDistribution, !GetAtt CloudFrontDistribution.DomainName, "" ]
RedirectPathSignIn: !Ref RedirectPathSignIn
RedirectPathSignOut: !Ref RedirectPathSignOut
AlternateDomainNames: !Ref AlternateDomainNames
OAuthScopes: !Ref OAuthScopes
UserPoolClientUpdateHandler:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/cfn-custom-resources/user-pool-client/
Handler: index.handler
Runtime: nodejs10.x
Policies:
- Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- cognito-idp:UpdateUserPoolClient
Resource: !GetAtt UserPool.Arn
ReactApp:
Type: Custom::ReactApp
Condition: CreateCloudFrontDistribution
Properties:
ServiceToken: !GetAtt ReactAppHandler.Arn
BucketName: !Ref S3Bucket
UserPoolId: !Ref UserPool
ClientId: !Ref UserPoolClient
CognitoAuthDomain: !GetAtt UserPoolDomain.DomainName
RedirectPathSignIn: !Ref RedirectPathSignIn
RedirectPathSignOut: !Ref RedirectPathSignOut
OAuthScopes: !Ref OAuthScopes
SignOutUrl: !Ref SignOutUrl
ReactAppHandler:
Type: AWS::Serverless::Function
Condition: CreateCloudFrontDistribution
Properties:
CodeUri: src/cfn-custom-resources/react-app/
Handler: bundle.handler
Runtime: nodejs10.x
Timeout: 180
MemorySize: 2048
Policies:
- Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- s3:ListBucket
Resource:
- !GetAtt S3Bucket.Arn
- Effect: Allow
Action:
- s3:DeleteObject
- s3:PutObject
Resource:
- !GetAtt S3Bucket.Arn
- !Join ["/", [!GetAtt S3Bucket.Arn, "*"]]
ParseAuthHandlerCodeUpdate:
Type: Custom::LambdaCodeUpdate
Properties:
ServiceToken: !GetAtt LambdaCodeUpdateHandler.Arn
LambdaFunction: !GetAtt ParseAuthHandler.Arn
Configuration: !Sub >
{
"userPoolId": "${UserPool}",
"clientId": "${UserPoolClient}",
"oauthScopes": ${OAuthScopes},
"cognitoAuthDomain": "${UserPoolDomain.DomainName}",
"redirectPathSignIn": "${RedirectPathSignIn}",
"redirectPathSignOut": "${RedirectPathSignOut}",
"redirectPathAuthRefresh": "${RedirectPathAuthRefresh}",
"cookieSettings": ${CookieSettings},
"httpHeaders": ${HttpHeaders}
}
CheckAuthHandlerCodeUpdate:
Type: Custom::LambdaCodeUpdate
Properties:
ServiceToken: !GetAtt LambdaCodeUpdateHandler.Arn
LambdaFunction: !GetAtt CheckAuthHandler.Arn
Configuration: !Sub >
{
"userPoolId": "${UserPool}",
"clientId": "${UserPoolClient}",
"oauthScopes": ${OAuthScopes},
"cognitoAuthDomain": "${UserPoolDomain.DomainName}",
"redirectPathSignIn": "${RedirectPathSignIn}",
"redirectPathSignOut": "${RedirectPathSignOut}",
"redirectPathAuthRefresh": "${RedirectPathAuthRefresh}",
"cookieSettings": ${CookieSettings},
"httpHeaders": ${HttpHeaders}
}
HttpHeadersHandlerCodeUpdate:
Type: Custom::LambdaCodeUpdate
Properties:
ServiceToken: !GetAtt LambdaCodeUpdateHandler.Arn
LambdaFunction: !GetAtt HttpHeadersHandler.Arn
Configuration: !Ref HttpHeaders
RefreshAuthHandlerCodeUpdate:
Type: Custom::LambdaCodeUpdate
Properties:
ServiceToken: !GetAtt LambdaCodeUpdateHandler.Arn
LambdaFunction: !GetAtt RefreshAuthHandler.Arn
Configuration: !Sub >
{
"userPoolId": "${UserPool}",
"clientId": "${UserPoolClient}",
"oauthScopes": ${OAuthScopes},
"cognitoAuthDomain": "${UserPoolDomain.DomainName}",
"redirectPathSignIn": "${RedirectPathSignIn}",
"redirectPathSignOut": "${RedirectPathSignOut}",
"redirectPathAuthRefresh": "${RedirectPathAuthRefresh}",
"cookieSettings": ${CookieSettings},
"httpHeaders": ${HttpHeaders}
}
SignOutHandlerCodeUpdate:
Type: Custom::LambdaCodeUpdate
Properties:
ServiceToken: !GetAtt LambdaCodeUpdateHandler.Arn
LambdaFunction: !GetAtt SignOutHandler.Arn
Configuration: !Sub >
{
"userPoolId": "${UserPool}",
"clientId": "${UserPoolClient}",
"oauthScopes": ${OAuthScopes},
"cognitoAuthDomain": "${UserPoolDomain.DomainName}",
"redirectPathSignIn": "${RedirectPathSignIn}",
"redirectPathSignOut": "${RedirectPathSignOut}",
"redirectPathAuthRefresh": "${RedirectPathAuthRefresh}",
"cookieSettings": ${CookieSettings},
"httpHeaders": ${HttpHeaders}
}
LambdaCodeUpdateHandler:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/cfn-custom-resources/lambda-code-update/
Handler: index.handler
Runtime: nodejs10.x
Policies:
- Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- lambda:GetFunction
- lambda:UpdateFunctionCode
Resource:
- !GetAtt ParseAuthHandler.Arn
- !GetAtt CheckAuthHandler.Arn
- !GetAtt HttpHeadersHandler.Arn
- !GetAtt RefreshAuthHandler.Arn
- !GetAtt SignOutHandler.Arn
Outputs:
S3Bucket:
Description: The S3 Bucket where the SPA (React, Angular, Vue, ...) is uploaded to
Condition: CreateCloudFrontDistribution
Value: !Ref S3Bucket
WebsiteUrl:
Description: URL of the CloudFront distribution that serves your SPA from S3
Condition: CreateCloudFrontDistribution
Value: !Sub "https://${CloudFrontDistribution.DomainName}"
CloudFrontDistribution:
Description: ID of the CloudFront distribution that serves your SPA from S3
Condition: CreateCloudFrontDistribution
Value: !Ref CloudFrontDistribution
UserPoolId:
Description: The ID of the Cognito User Pool
Value: !Ref UserPool
ClientId:
Description: Client ID to use to interact with the User Pool
Value: !Ref UserPoolClient
CognitoAuthDomain:
Description: The domain where the Cognito Hosted UI is served
Value: !GetAtt UserPoolDomain.DomainName
RedirectUrisSignIn:
Description: The URI(s) that will handle the redirect from Cognito after successfull sign-in
Value: !GetAtt UserPoolClientUpdate.RedirectUrisSignIn
RedirectUrisSignOut:
Description: The URI(s) that will handle the redirect from Cognito after successfull sign-out
Value: !GetAtt UserPoolClientUpdate.RedirectUrisSignOut
ParseAuthHandler:
Description: The Lambda function ARN to use in Lambda@Edge for parsing the URL of the redirect from the Cognito hosted UI after succesful sign-in
Value: !GetAtt ParseAuthHandlerCodeUpdate.FunctionArn
CheckAuthHandler:
Description: The Lambda function ARN to use in Lambda@Edge for checking the presence of a valid JWT
Value: !GetAtt CheckAuthHandlerCodeUpdate.FunctionArn
HttpHeadersHandler:
Description: The Lambda function ARN to use in Lambda@Edge for setting HTTP security headers
Value: !GetAtt HttpHeadersHandlerCodeUpdate.FunctionArn
RefreshAuthHandler:
Description: The Lambda function ARN to use in Lambda@Edge for getting new JWT's using the refresh token
Value: !GetAtt RefreshAuthHandlerCodeUpdate.FunctionArn
SignOutHandler:
Description: The Lambda function ARN to use in Lambda@Edge for signing out
Value: !GetAtt SignOutHandlerCodeUpdate.FunctionArn
CodeUpdateHandler:
Description: The Lambda function ARN of the custom resource that adds configuration to a function and publishes a version for use as Lambda@Edge
Value: !GetAtt LambdaCodeUpdateHandler.Arn