-
Notifications
You must be signed in to change notification settings - Fork 37
/
cfn-certificatevendor.yml
353 lines (338 loc) · 12.1 KB
/
cfn-certificatevendor.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
347
348
349
350
351
352
353
---
#-----------------------------------------------------------------------------------------------------------------------
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
# with the License. A copy of the License is located at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES
# OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions
# and limitations under the License.
#-----------------------------------------------------------------------------------------------------------------------
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: CDF Certificate Vendor Service
Parameters:
ApplicationConfigurationOverride:
Description: This allows you to override any application configuration. It should consists of a text-based content with a structure and syntax comprising key–value pairs for properties. Any configurations contained in this will override the configurations found and merged from the default .env files.
Type: String
Environment:
Description: Name of environment. Used to name the created resources.
Type: String
MinLength: 1
BucketName:
Description: Name of bucket of where certificates are stored.
Type: String
MinLength: 1
MQTTGetTopic:
Description: The MQTT topic where certificate requests are published by a device.
Default: cdf/certificates/+/get
Type: String
MQTTAckTopic:
Description: The MQTT topic where certificate acknowledgements are published by a device.
Default: cdf/certificates/+/ack
Type: String
ThingGroupName:
Description: Thing Group of devices requiring certification rotation
Type: String
Default: cdfRotateCertificates
KmsKeyId:
Description: The KMS key ID used to encrypt SSM parameters
Type: String
OpenSslLambdaLayerArn:
Description: OpenSSL lambda layer ARN
Type: String
MinLength: 1
AssetLibraryFunctionName:
Description: AssetLibrary REST Api function name
Type: String
Default: ''
CommandsFunctionName:
Description: Commands REST Api function name
Type: String
CustomResourceLambdaArn:
Description: Custom resource lambda arn
Type: String
MinLength: 1
CaCertificateId:
Type: String
Default: ''
RotatedCertificatePolicy:
Type: String
Default: ''
AcmpcaEnabled:
Type: String
Default: 'false'
AcmpcaCaArn:
Type: String
Default: ''
AcmpcaSigningAlgorithm:
Type: String
Default: 'SHA256WITHRSA'
ACMPCACrossAccountRoleArn:
Description: If ACM PCA functionality is to be used, and a ACM PCA is located within a different AWS Account, specify the cross-account IAM Role ARN.
Type: String
Default: ''
Conditions:
ThingGroupNameProvided: !Not [!Equals [!Ref ThingGroupName, '']]
KmsKeyProvided: !Not [!Equals [!Ref KmsKeyId, '']]
KmsKeyNotProvided: !Equals [!Ref KmsKeyId, '']
ACMPCACrossAccountAccessRequired: !Not [!Equals [!Ref ACMPCACrossAccountRoleArn, '']]
Resources:
MQTTGetRule:
Type: 'AWS::IoT::TopicRule'
Properties:
TopicRulePayload:
Actions:
- Lambda:
FunctionArn: !Sub 'arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${LambdaFunction}'
Description: 'Certificate requests (CDF Certificate Vendor service)'
AwsIotSqlVersion: '2016-03-23'
RuleDisabled: false
Sql: !Sub "SELECT topic(3) as deviceId, topic(4) as action, principal() as certId, * FROM '${MQTTGetTopic}'"
MQTTAckRule:
Type: 'AWS::IoT::TopicRule'
Properties:
TopicRulePayload:
Actions:
- Lambda:
FunctionArn: !Sub 'arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${LambdaFunction}'
Description: 'Certificate acknowledgements (CDF Certificate Vendor service)'
AwsIotSqlVersion: '2016-03-23'
RuleDisabled: false
Sql: !Sub "SELECT topic(3) as deviceId, topic(4) as action, principal() as certId, * FROM '${MQTTAckTopic}'"
LambdaFunctionGetInvocationPermission:
Type: AWS::Lambda::Permission
Properties:
SourceArn: !Sub 'arn:aws:iot:${AWS::Region}:${AWS::AccountId}:rule/${MQTTGetRule}'
Action: lambda:InvokeFunction
Principal: iot.amazonaws.com
FunctionName: !GetAtt LambdaFunction.Arn
SourceAccount: !Ref AWS::AccountId
LambdaFunctionAckInvocationPermission:
Type: AWS::Lambda::Permission
Properties:
SourceArn: !Sub 'arn:aws:iot:${AWS::Region}:${AWS::AccountId}:rule/${MQTTAckRule}'
Action: lambda:InvokeFunction
Principal: iot.amazonaws.com
FunctionName: !GetAtt LambdaFunction.Arn
SourceAccount: !Ref AWS::AccountId
LambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
- iot.amazonaws.com
Action: sts:AssumeRole
Path: '/'
ManagedPolicyArns:
- !Ref ApplicationPolicies
- arn:aws:iam::aws:policy/AWSLambdaExecute
- arn:aws:iam::aws:policy/service-role/AWSLambdaRole
- arn:aws:iam::aws:policy/AWSXrayWriteOnlyAccess
Condition: KmsKeyNotProvided
LambdaExecutionRoleWithCsr:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
- iot.amazonaws.com
Action: sts:AssumeRole
Path: '/'
ManagedPolicyArns:
- !Ref ApplicationPolicies
- !Ref ApplicationCsrPolicies
- !Ref ACMPolicy
- arn:aws:iam::aws:policy/AWSLambdaExecute
- arn:aws:iam::aws:policy/AWSXrayWriteOnlyAccess
Condition: KmsKeyProvided
ApplicationPolicies:
Type: 'AWS::IAM::ManagedPolicy'
Metadata:
cfn_nag:
rules_to_suppress:
- id: W13
reason: 'Lambda provided permissions to manage iot Core and s3 bucket'
Properties:
Description: 'cdf-certificatvendor application policies'
Path: '/'
PolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: 'iot'
Action:
- 'iot:UpdateCertificate'
Effect: Allow
Resource:
- !Sub 'arn:aws:iot:${AWS::Region}:${AWS::AccountId}:cert/*'
- Sid: 'iotgroup'
Action:
- 'iot:RemoveThingFromThingGroup'
Effect: Allow
Resource:
- !Sub 'arn:aws:iot:${AWS::Region}:${AWS::AccountId}:thinggroup/*'
- !Sub 'arn:aws:iot:${AWS::Region}:${AWS::AccountId}:thing/*'
- Sid: 'iotthing'
Action:
- 'iot:DescribeThing'
- 'iot:UpdateThing'
Effect: Allow
Resource:
- !Sub 'arn:aws:iot:${AWS::Region}:${AWS::AccountId}:thing/*'
- Sid: 's3bucket'
Action:
- 's3:ListBucket'
Effect: Allow
Resource:
- !Sub 'arn:aws:s3:::${BucketName}'
- Sid: 's3objects'
Action:
- 's3:Get*'
Effect: Allow
Resource:
- !Sub 'arn:aws:s3:::${BucketName}/*'
- Sid: 'mqtt'
Action:
- 'iot:Publish'
Effect: Allow
Resource:
- !Sub 'arn:aws:iot:${AWS::Region}:${AWS::AccountId}:topic/cdf/certificates/*'
- Sid: 'lambda'
Action:
- 'lambda:InvokeFunction'
- 'lambda:Invoke'
Effect: Allow
Resource:
- !Sub 'arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${AssetLibraryFunctionName}'
ApplicationCsrPolicies:
Type: 'AWS::IAM::ManagedPolicy'
Condition: KmsKeyProvided
Metadata:
cfn_nag:
rules_to_suppress:
- id: W13
reason: 'Policy needs to apply to IoT artifacts that are created by the CDF'
Properties:
Description: 'cdf-certificatvendor policies needed to generate certificates with a CSR'
Path: '/'
PolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: 'ssm'
Action:
- 'ssm:GetParameter*'
Effect: Allow
Resource: !Sub 'arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/cdf-ca-key-*'
- Sid: 'kms'
Action:
- 'kms:Decrypt'
Effect: Allow
Resource: !Sub 'arn:aws:kms:${AWS::Region}:${AWS::AccountId}:key/${KmsKeyId}'
- Sid: 'iotca'
Action:
- 'iot:DescribeCaCertificate'
Effect: Allow
Resource: !Sub 'arn:aws:iot:${AWS::Region}:${AWS::AccountId}:cacert/*'
- Sid: 'iot'
Action:
- 'iot:RegisterCertificate'
- 'iot:AttachThingPrincipal'
- 'iot:ListThingPrincipals'
- 'iot:DetachThingPrincipal'
- 'iot:ListPrincipalThings'
- 'iot:ListAttachedPolicies'
Effect: Allow
Resource: '*'
- Sid: 'iotcert'
Action:
- 'iot:AttachPolicy'
- 'iot:DetachPolicy'
- 'iot:UpdateCertificate'
- 'iot:DeleteCertificate'
- 'iot:GetEffectivePolicies'
Effect: Allow
Resource: !Sub 'arn:aws:iot:${AWS::Region}:${AWS::AccountId}:cert/*'
ACMPolicy:
Type: AWS::IAM::ManagedPolicy
Properties:
Description: 'cdf-bulkcerts policy for accessing ACM PCA'
Path: '/cdf/provisioning/'
PolicyDocument:
Version: '2012-10-17'
Statement:
- Action:
- 'acm-pca:IssueCertificate'
- 'acm-pca:GetCertificate'
Effect: Allow
Resource: '*'
# optional cross-account role access for ACM PCA
- Fn::If:
- ACMPCACrossAccountAccessRequired
- !Ref ACMPCACrossAccountRoleArn
- Ref: AWS::NoValue
LambdaFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub 'cdf-certificateVendor-${Environment}'
CodeUri: ../bundle.zip
Handler: lambda_proxy.handler
Layers:
- !Ref OpenSslLambdaLayerArn
MemorySize: 512
Role:
!If [
KmsKeyProvided,
!GetAtt LambdaExecutionRoleWithCsr.Arn,
!GetAtt LambdaExecutionRole.Arn,
]
Runtime: nodejs18.x
Timeout: 30
Environment:
Variables:
APP_CONFIG_DIR: 'config'
APP_CONFIG: !Ref ApplicationConfigurationOverride
ASSETLIBRARY_API_FUNCTION_NAME: !Ref AssetLibraryFunctionName
AWS_IOT_ENDPOINT: !GetAtt IotEndpoint.address
AWS_S3_CERTIFICATES_BUCKET: !Ref BucketName
CERTIFICATES_CACERTIFICATEID: !Ref CaCertificateId
POLICIES_ROTATEDCERTIFICATEPOLICY: !Ref RotatedCertificatePolicy
ACMPCA_ENABLED: !Ref AcmpcaEnabled
ACMPCA_CA_ARN: !Ref AcmpcaCaArn
ACMPCA_SIGNING_ALGORITHM: !Ref AcmpcaSigningAlgorithm
AWS_ACCOUNTID: !Ref AWS::AccountId
ACM_PCA_CROSS_ACCOUNT_ROLE_ARN: !Ref ACMPCACrossAccountRoleArn
Tracing: Active
IotEndpoint:
Type: Custom::IotEndpoint
Version: 1.0
Properties:
ServiceToken: !Ref CustomResourceLambdaArn
IotThingGroup:
Type: Custom::IotThingGroup
Version: 1.0
Properties:
ServiceToken: !Ref CustomResourceLambdaArn
ThingGroupName: !Ref ThingGroupName
Condition: ThingGroupNameProvided
RotateCertificatesJob:
Type: Custom::RotateCertificatesJob
Version: 1.0
Properties:
ServiceToken: !Ref CustomResourceLambdaArn
CommandsFunctionName: !Ref CommandsFunctionName
ThingGroupArn: !GetAtt IotThingGroup.thingGroupArn
MQTTGetTopic: !Ref MQTTGetTopic
MQTTAckTopic: !Ref MQTTAckTopic
Condition: ThingGroupNameProvided