-
Notifications
You must be signed in to change notification settings - Fork 0
/
BackendServices.yml
346 lines (346 loc) · 11.5 KB
/
BackendServices.yml
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
AWSTemplateFormatVersion: '2010-09-09'
Description: ServerlessPass services used by the backend.
Parameters:
ArtifactsBucketName:
Description: Bucket to store artifacts for the backend
Type: String
DomainName:
Description: Domain name used to communicate to the backend
Type: String
DomainHostedZoneId:
Description: Hosted zone id of domain used to communicate to the backend
Type: String
Stage:
Description: Stage is used to deploy multiple instances of ServerlessPass
Type: String
Default: Default
CrossOriginDomains:
Description: "Comma-separated domains allowed to make requests to the backend (example: https://example.com,http://localhost:5000)"
Type: String
Default: "chrome-extension://lcmbpoclaodbgkbjafnkbbinogcbnjih"
Resources:
Alias:
Type: AWS::SSM::Parameter
Properties:
Name: !Sub "ServerlessPassBackendServicesAlias${Stage}"
Type: String
Value: !Sub "ServerlessPass${Stage}"
Certificate:
Type: AWS::CertificateManager::Certificate
Properties:
DomainName: !Ref DomainName
DomainValidationOptions:
- DomainName: !Ref DomainName
HostedZoneId: !Ref DomainHostedZoneId
ValidationMethod: DNS
UserPool:
Type: AWS::Cognito::UserPool
Properties:
AccountRecoverySetting:
RecoveryMechanisms:
- Name: verified_email
Priority: 1
AutoVerifiedAttributes:
- email
Policies:
PasswordPolicy:
MinimumLength: 8
RequireLowercase: true
RequireNumbers: true
RequireSymbols: true
RequireUppercase: true
TemporaryPasswordValidityDays: 1
UsernameAttributes:
- email
UsernameConfiguration:
CaseSensitive: false
UserPoolName: !Sub "${Alias.Value}Users"
VerificationMessageTemplate:
DefaultEmailOption: CONFIRM_WITH_LINK
EmailMessageByLink: Please click the link below to verify your email address.
{##Verify email##}
EmailSubjectByLink: Email verification is required
UserPoolClient:
Type: AWS::Cognito::UserPoolClient
Properties:
ClientName: BackendUserPoolClient
ExplicitAuthFlows:
- ALLOW_REFRESH_TOKEN_AUTH
- ALLOW_USER_PASSWORD_AUTH
SupportedIdentityProviders:
- COGNITO
UserPoolId: !Ref UserPool
TokenValidityUnits:
AccessToken: days
IdToken: days
RefreshToken: days
RefreshTokenValidity: 3650
AccessTokenValidity: 1
IdTokenValidity: 1
Table:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: PartitionKey
AttributeType: S
- AttributeName: SortKey
AttributeType: S
BillingMode: PAY_PER_REQUEST
KeySchema:
- AttributeName: PartitionKey
KeyType: HASH
- AttributeName: SortKey
KeyType: RANGE
TableName: !Sub "${Alias.Value}PartitionedStorage"
LambdaFunctionRolePolicy:
Type: AWS::IAM::ManagedPolicy
Properties:
ManagedPolicyName: !Sub "${Alias.Value}LambdaPolicy"
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
Resource: "arn:aws:logs:*:*:*"
- Effect: Allow
Action:
- dynamodb:DescribeTable
- dynamodb:PutItem
- dynamodb:GetItem
- dynamodb:Query
- dynamodb:Scan
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource: !GetAtt Table.Arn
- Effect: "Allow"
Action:
- cognito-idp:AdminInitiateAuth
- cognito-idp:ListUsers
Resource: !GetAtt UserPool.Arn
LambdaFunctionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Action:
- sts:AssumeRole
Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
ManagedPolicyArns:
- !Ref LambdaFunctionRolePolicy
ArtifactsBucket:
Type: AWS::S3::Bucket
DeletionPolicy: Delete
Properties:
BucketName: !Ref ArtifactsBucketName
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
LifecycleConfiguration:
Rules:
- ExpirationInDays: 1
Status: Enabled
CodeBuildProject:
Type: AWS::CodeBuild::Project
Properties:
Artifacts:
Type: NO_ARTIFACTS
Environment:
ComputeType: BUILD_GENERAL1_SMALL
Image: aws/codebuild/standard:6.0
Type: LINUX_CONTAINER
LogsConfig:
CloudWatchLogs:
GroupName: !Ref CodeBuildLogs
Status: ENABLED
StreamName: CodeBuildProject
Name: !Sub "${Alias.Value}BackendDeploy"
ServiceRole: !Ref CodeBuildRole
Source:
Type: NO_SOURCE
BuildSpec:
!Sub |
version: 0.2
env:
shell: bash
phases:
build:
commands:
- wget https://github.com/Staticsoft/ServerlessPass/releases/latest/download/Backend.zip
- unzip Backend.zip
- aws cloudformation package --template-file $CODEBUILD_SRC_DIR/deploy/templates/BackendLambda.yml --s3-bucket ${ArtifactsBucket} --output-template-file BackendLambda.yml;
- |
if ! aws cloudformation describe-stacks --region ${AWS::Region} --stack-name ${Alias.Value}BackendLambda; then
aws cloudformation create-stack --region ${AWS::Region} --stack-name ${Alias.Value}BackendLambda --template-body file://BackendLambda.yml --parameters ParameterKey=Stage,ParameterValue=${Stage};
aws cloudformation wait stack-create-complete --stack-name ${Alias.Value}BackendLambda;
else
aws cloudformation update-stack --region ${AWS::Region} --stack-name ${Alias.Value}BackendLambda --template-body file://BackendLambda.yml --parameters ParameterKey=Stage,ParameterValue=${Stage} || exit 1;
aws cloudformation wait stack-update-complete --stack-name ${Alias.Value}BackendLambda;
fi
- aws apigateway create-deployment --rest-api-id ${ApiGateway} --stage-name ${Stage};
TimeoutInMinutes: 10
CodeBuildLogs:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "${Alias.Value}BackendCodeBuildLogs"
RetentionInDays: 90
CodeBuildPolicy:
Type: AWS::IAM::ManagedPolicy
Properties:
ManagedPolicyName: !Sub "${Alias.Value}BackendCodeBuildPolicy"
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- iam:PassRole
Resource:
- "*"
- Effect: Allow
Action:
- logs:CreateLogStream
- logs:DeleteLogStream
- logs:PutLogEvents
Resource: !GetAtt CodeBuildLogs.Arn
- Effect: Allow
Action:
- s3:GetObject
- s3:PutObject
- s3:DeleteObject
- s3:DeleteObjectVersion
Resource: !Sub "${ArtifactsBucket.Arn}/*"
- Effect: Allow
Action:
- lambda:GetFunction
- lambda:UpdateFunctionCode
- lambda:UpdateFunctionConfiguration
- lambda:CreateFunction
- lambda:DeleteFunction
- lambda:AddPermission
- lambda:RemovePermission
Resource: !Sub "arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${Alias.Value}BackendLambda*"
- Effect: Allow
Action:
- cloudformation:CreateStack
- cloudformation:DeleteStack
- cloudformation:UpdateStack
- cloudformation:DescribeStacks
Resource:
- !Sub "arn:aws:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/${Alias.Value}BackendLambda"
- !Sub "arn:aws:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/${Alias.Value}BackendLambda/*"
- Effect: Allow
Action:
- apigateway:GET
- apigateway:POST
- apigateway:PATCH
- apigateway:PUT
- apigateway:DELETE
- apigateway:TagResource
Resource:
- !Sub "arn:aws:apigateway:${AWS::Region}::/tags/*"
- !Sub "arn:aws:apigateway:${AWS::Region}::/restapis/${ApiGateway}/*"
- !Sub "arn:aws:apigateway:${AWS::Region}::/domainnames/${ApiGatewayDomain}/basepathmappings"
- !Sub "arn:aws:apigateway:${AWS::Region}::/domainnames/${ApiGatewayDomain}/basepathmappings/*"
- Effect: Allow
Action:
- events:DescribeRule
- events:PutRule
- events:DeleteRule
- events:EnableRule
- events:DisableRule
- events:PutTargets
- events:RemoveTargets
Resource:
Fn::Sub: "arn:aws:events:${AWS::Region}:${AWS::AccountId}:rule/${Alias.Value}StatusRule"
CodeBuildRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Action:
- sts:AssumeRole
Effect: Allow
Principal:
Service:
- codebuild.amazonaws.com
ManagedPolicyArns:
- !Ref CodeBuildPolicy
ApiGateway:
Type: AWS::ApiGateway::RestApi
Properties:
Name: !Sub "${Alias.Value}Api"
Authorizer:
Type: AWS::ApiGateway::Authorizer
Properties:
Name: !Sub "${Alias.Value}Authorizer"
RestApiId: !Ref ApiGateway
Type: COGNITO_USER_POOLS
ProviderARNs:
- !Sub "arn:aws:cognito-idp:${AWS::Region}:${AWS::AccountId}:userpool/${UserPool}"
IdentitySource: method.request.header.Authorization
AuthorizerResultTtlInSeconds: 3600
ApiGatewayDomain:
Type: AWS::ApiGateway::DomainName
Properties:
CertificateArn: !Ref Certificate
DomainName: !Ref DomainName
EndpointConfiguration:
Types:
- EDGE
SecurityPolicy: TLS_1_2
ApiDNSRecord:
Type: AWS::Route53::RecordSet
Properties:
HostedZoneId: !Ref DomainHostedZoneId
Name: !Ref DomainName
AliasTarget:
DNSName: !GetAtt ApiGatewayDomain.DistributionDomainName
EvaluateTargetHealth: false
HostedZoneId: Z2FDTNDATAQYW2
Type: A
Outputs:
LambdaFunctionRoleArn:
Value: !GetAtt LambdaFunctionRole.Arn
Export:
Name: !Sub "ServerlessPass${Stage}LambdaFunctionRoleArn"
UserPoolId:
Value: !Ref UserPool
Export:
Name: !Sub "ServerlessPass${Stage}UserPoolId"
UserPoolClientId:
Value: !Ref UserPoolClient
Export:
Name: !Sub "ServerlessPass${Stage}UserPoolClientId"
ApiGatewayId:
Value: !Ref ApiGateway
Export:
Name: !Sub "ServerlessPass${Stage}ApiGatewayId"
ApiGatewayRootResourceId:
Value: !GetAtt ApiGateway.RootResourceId
Export:
Name: !Sub "ServerlessPass${Stage}ApiGatewayRootResourceId"
AuthorizerId:
Value: !Ref Authorizer
Export:
Name: !Sub "ServerlessPass${Stage}AuthorizerId"
DomainName:
Value: !Ref ApiGatewayDomain
Export:
Name: !Sub "ServerlessPass${Stage}ApiGatewayDomainName"
ArtifactsBucketName:
Value: !Ref ArtifactsBucket
Export:
Name: !Sub "ServerlessPass${Stage}BackendArtifactsBucketName"
CrossOriginDomains:
Value: !Ref CrossOriginDomains
Export:
Name: !Sub "ServerlessPass${Stage}CrossOriginDomains"