-
Notifications
You must be signed in to change notification settings - Fork 1
/
serverless.yml
executable file
·299 lines (291 loc) · 8.92 KB
/
serverless.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
service: aws-certbot-security
provider:
name: aws
runtime: nodejs4.3
region: ${env:AWS_REGION}
stage: ${opt:stage, env:STAGE}
stackTags:
Type: service-security
resources:
Resources:
AppKey:
Type: AWS::KMS::Key
DependsOn:
- Role
- KeyUploadGroup
Properties:
Description: 'Key used to encrypt secrets for this stage'
Enabled: true
EnableKeyRotation: true
KeyPolicy:
Version: '2012-10-17'
Statement:
- Sid: Allow administration of the key by root
Effect: Allow
Principal:
AWS:
Fn::Join:
- ''
- - 'arn:aws:iam::'
- Ref: AWS::AccountId
- ':root'
Action:
- kms:*
Resource: '*'
- Sid: Allow admin by CodeBuild service role defined in pipelines security directory
Effect: Allow
Principal:
AWS:
Fn::ImportValue: codebuild-arn-${self:service}-${self:provider.stage}
Action:
- kms:*
Resource: '*'
- Sid: ${self:service} ${self:provider.stage} Allow Key usage to Lambda Execution Role
Effect: Allow
Principal:
AWS:
Fn::GetAtt: [ Role, Arn ]
Action:
- kms:Encrypt
- kms:Decrypt
- kms:ReEncrypt*
- kms:GenerateDataKey*
- kms:DescribeKey
Resource: '*'
- Sid: ${self:service} ${self:provider.stage} Allow Key usage to CodeBuild app role for environment variables
Effect: Allow
Principal:
AWS:
Fn::ImportValue: codebuild-arn-app-${self:service}-${self:provider.stage}
Action:
- kms:Encrypt
- kms:Decrypt
- kms:ReEncrypt*
- kms:GenerateDataKey*
- kms:DescribeKey
Resource: '*'
SecretsBucket:
Type: AWS::S3::Bucket
Properties:
VersioningConfiguration:
Status: Enabled
LoggingConfiguration:
DestinationBucketName:
Fn::ImportValue:
AppSecretLoggingBucket
LogFilePrefix: logs-${self:service}-${self:provider.stage}
SecretsBucketPolicy:
Type: AWS::S3::BucketPolicy
DependsOn:
- AppKey
- Role
- KeyUploadGroup
Properties:
Bucket:
Ref: SecretsBucket
PolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: Allow root to upload encrypted secrets using stack key
Action:
- s3:PutObject
Effect: Allow
Principal:
AWS:
Fn::Join:
- ''
- - 'arn:aws:iam::'
- Ref: AWS::AccountId
- ':root'
Resource:
Fn::Join:
- ''
- - 'arn:aws:s3:::'
- Ref: SecretsBucket
- '/*'
Condition:
StringEquals:
s3:x-amz-server-side-encryption: aws:kms
s3:x-amz-server-side-encryption-aws-kms-key-id:
Fn::GetAtt: [ AppKey, Arn ]
Bool:
aws:SecureTransport: true
- Sid: Do not allow other keys to be used in this bucket
Effect: Deny
Principal: '*'
Action:
- s3:PutObject
Resource:
Fn::Join:
- ''
- - 'arn:aws:s3:::'
- Ref: SecretsBucket
- '/*'
Condition:
StringNotEquals:
s3:x-amz-server-side-encryption-aws-kms-key-id:
Fn::GetAtt: [ AppKey, Arn ]
- Sid: Allow Lambda execution role to download and decrypt secrets
Effect: Allow
Action:
- s3:GetObject
Principal:
AWS:
Fn::GetAtt: [ Role, Arn ]
Resource:
Fn::Join:
- ''
- - 'arn:aws:s3:::'
- Ref: SecretsBucket
- '/*'
Condition:
Bool:
aws:SecureTransport: true
- Sid: Allow App CodeBuild role download and decrypt secrets
Effect: Allow
Action:
- s3:GetObject
Principal:
AWS:
Fn::ImportValue: codebuild-arn-app-${self:service}-${self:provider.stage}
Resource:
Fn::Join:
- ''
- - 'arn:aws:s3:::'
- Ref: SecretsBucket
- '/*'
Condition:
Bool:
aws:SecureTransport: true
ManagedKeyPolicy:
Type: AWS::IAM::ManagedPolicy
Properties:
Groups:
- Ref: KeyUploadGroup
PolicyDocument:
Version: '2012-10-17'
Statement:
-
Effect: Allow
Action:
- kms:Encrypt
- kms:Decrypt
- kms:ReEncrypt*
- kms:GenerateDataKey*
- kms:DescribeKey
Resource:
Fn::GetAtt: [ AppKey, Arn ]
-
Effect: Allow
Action:
- s3:PutObject
Resource:
Fn::Join:
- ''
- - 'arn:aws:s3:::'
- Ref: SecretsBucket
- '/*'
KeyUploadGroup:
Type: AWS::IAM::Group
Role:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: '/'
Policies:
- PolicyName: ${self:service}-${self:provider.stage}
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
- dynamodb:Scan
Resource:
- Fn::Join:
- ''
- - 'arn:aws:dynamodb:'
- Ref: AWS::Region
- ':'
- Ref: AWS::AccountId
- ':table/'
- ${env:SERVICE}
- '-'
- ${self:provider.stage}
- '.'
- ${self:provider.stage}
- Fn::Join:
- ''
- - 'arn:aws:dynamodb:'
- Ref: AWS::Region
- ':'
- Ref: AWS::AccountId
- ':table/'
- ${env:SERVICE}
- '-'
- ${self:provider.stage}
- '.nameEmailMapping'
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
- logs:DescribeLogStreams
Resource:
Fn::Join:
- ''
- - 'arn:aws:logs:'
- Ref: AWS::Region
- ':*:*'
- Effect: Allow
Action:
- lambda:InvokeAsync
- lambda:InvokeFunction
Resource:
Fn::Join:
- ''
- - 'arn:aws:lambda:'
- Ref: AWS::Region
- ':'
- Ref: AWS::AccountId
- ':function:'
- ${env:SERVICE}
- '-'
- ${self:provider.stage}
- '-*'
Outputs:
SecretsBucket:
Description: Bucket to store secrets for ${self:service}-${self:provider.stage}
Value:
Ref: SecretsBucket
Export:
Name: ${self:service}-secrets-${self:provider.stage}
KmsKeyId:
Description: Stage ${self:provider.stage} KMS Key Id
Value:
Ref: AppKey
RoleARN:
Description: Stage ${self:provider.stage} IAM Role ARN
Value:
Fn::GetAtt:
- Role
- Arn
Export:
Name: ${self:service}-${self:provider.stage}
KeyUploadGroupARN:
Description: Group ARN for key uploaders
Value:
Fn::GetAtt:
- KeyUploadGroup
- Arn