-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_cloudformation.yaml
216 lines (215 loc) · 6.72 KB
/
auth_cloudformation.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
AWSTemplateFormatVersion: 2010-09-09
Resources:
TokenGeneratorFunction:
Type: 'AWS::Lambda::Function'
Properties:
Code:
S3Bucket: wpnauthconfig
S3Key: authlambda.zip
Handler: index.handler
Role: !GetAtt LambdaExecutionRole.Arn
Runtime: nodejs20.x
TokenValidatorFunction:
Type: 'AWS::Lambda::Function'
Properties:
Code:
S3Bucket: wpnauthconfig
S3Key: accesslambda.zip
Handler: index.handler
Role: !GetAtt LambdaExecutionRole.Arn
Runtime: nodejs20.x
LambdaExecutionRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
- edgelambda.amazonaws.com
Action: 'sts:AssumeRole'
Policies:
- PolicyName: LambdaExecutionPolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 'logs:*'
- 'dynamodb:*'
- 's3:GetObject'
- 'ses:SendEmail'
- 'ses:SendRawEmail'
Resource: '*'
TokenGeneratorFunctionInvokePermission:
Type: 'AWS::Lambda::Permission'
Properties:
Action: 'lambda:InvokeFunction'
FunctionName: !GetAtt TokenGeneratorFunction.Arn
Principal: apigateway.amazonaws.com
SourceArn: !Sub >-
arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${APIGateway}/*/POST/
APIDeployment:
Type: 'AWS::ApiGateway::Deployment'
DependsOn:
- EmailPostMethod
- RootOptionsMethod
Properties:
RestApiId: !Ref APIGateway
StageName: prod
APIGateway:
Type: 'AWS::ApiGateway::RestApi'
Properties:
Name: TokenBasedAuthAPI
EmailPostMethod:
Type: 'AWS::ApiGateway::Method'
Properties:
RestApiId: !Ref APIGateway
ResourceId: !GetAtt APIGateway.RootResourceId
HttpMethod: POST
AuthorizationType: NONE
Integration:
Type: AWS_PROXY
IntegrationHttpMethod: POST
Uri: !Sub >-
arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${TokenGeneratorFunction.Arn}/invocations
RootOptionsMethod:
Type: 'AWS::ApiGateway::Method'
Properties:
RestApiId: !Ref APIGateway
ResourceId: !GetAtt APIGateway.RootResourceId
HttpMethod: OPTIONS
AuthorizationType: NONE
Integration:
Type: MOCK
RequestTemplates:
application/json: |
{
"statusCode": 200
}
IntegrationResponses:
- StatusCode: 200
ResponseParameters:
method.response.header.Access-Control-Allow-Headers: >-
'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'
method.response.header.Access-Control-Allow-Methods: '''POST,GET,OPTIONS'''
method.response.header.Access-Control-Allow-Origin: '''*'''
ResponseTemplates:
application/json: ''
MethodResponses:
- StatusCode: 200
ResponseModels:
application/json: Empty
ResponseParameters:
method.response.header.Access-Control-Allow-Headers: true
method.response.header.Access-Control-Allow-Methods: true
method.response.header.Access-Control-Allow-Origin: true
MyCloudFrontOAI:
Type: 'AWS::CloudFront::CloudFrontOriginAccessIdentity'
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: OAI for accessing S3 content exclusively via CloudFront
TokenValidatorFunctionVersion:
Type: 'AWS::Lambda::Version'
Properties:
FunctionName: !Ref TokenValidatorFunction
CloudFrontDistribution:
Type: 'AWS::CloudFront::Distribution'
Properties:
DistributionConfig:
Origins:
- DomainName: !GetAtt PrivateS3Bucket.DomainName
Id: S3Origin
S3OriginConfig:
OriginAccessIdentity: !Sub 'origin-access-identity/cloudfront/${MyCloudFrontOAI}'
DefaultCacheBehavior:
TargetOriginId: S3Origin
ViewerProtocolPolicy: redirect-to-https
LambdaFunctionAssociations:
- EventType: viewer-request
LambdaFunctionARN: !Ref TokenValidatorFunctionVersion
ForwardedValues:
QueryString: false
Cookies:
Forward: none
Headers:
- 'Origin'
- 'Authorization'
- 'Access-Control-Request-Method'
- 'Access-Control-Request-Headers'
AllowedMethods:
- GET
- HEAD
- OPTIONS
CachedMethods:
- GET
- HEAD
- OPTIONS
Enabled: true
Comment: >-
CloudFront distribution for S3 bucket with Lambda@Edge for
authentication
PrivateS3Bucket:
Type: 'AWS::S3::Bucket'
Properties:
BucketName: wpn-exp-cat
AccessControl: Private
CorsConfiguration:
CorsRules:
- AllowedOrigins:
- '*'
AllowedMethods:
- GET
- HEAD
AllowedHeaders:
- '*'
MaxAge: 3000
MyS3BucketPolicy:
Type: 'AWS::S3::BucketPolicy'
Properties:
Bucket: !Ref PrivateS3Bucket
PolicyDocument:
Statement:
- Effect: Allow
Principal:
AWS: !Sub >-
arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity
${MyCloudFrontOAI}
Action: 's3:GetObject'
Resource: !Sub 'arn:aws:s3:::${PrivateS3Bucket}/*'
TokenTable:
Type: 'AWS::DynamoDB::Table'
Properties:
TableName: TokenStorage
AttributeDefinitions:
- AttributeName: email
AttributeType: S
- AttributeName: token
AttributeType: S
- AttributeName: generationDate
AttributeType: S
KeySchema:
- AttributeName: token
KeyType: HASH
GlobalSecondaryIndexes:
- IndexName: EmailIndex
KeySchema:
- AttributeName: email
KeyType: HASH
- AttributeName: generationDate
KeyType: RANGE
Projection:
ProjectionType: ALL
- IndexName: GenerationDateIndex
KeySchema:
- AttributeName: generationDate
KeyType: HASH
Projection:
ProjectionType: ALL
BillingMode: PAY_PER_REQUEST
Outputs:
ApiUrl:
Description: API Gateway endpoint URL for Prod stage
Value: !Sub 'https://${APIGateway}.execute-api.${AWS::Region}.amazonaws.com/prod'