forked from aws-samples/aws-iam-access-key-auto-rotation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
iam-key-auto-rotation-and-notifier.yaml
362 lines (335 loc) · 11.3 KB
/
iam-key-auto-rotation-and-notifier.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
# (c) 2020 Amazon Web Services, Inc. or its affiliates. All Rights Reserved. This AWS Content
# is provided subject to the terms of the AWS Customer Agreement available at
# https://aws.amazon.com/agreement/ or other written agreement between Customer
# and Amazon Web Services, Inc.
AWSTemplateFormatVersion: '2010-09-09'
Description: "AWS CloudFormation template to set up Auto-rotation function for AWS IAM Access Keys."
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: Configure the Deployment
Parameters:
- S3BucketName
- S3BucketPrefix
- KMSKeyID
- Label:
default: Configure Notifier Tool
Parameters:
- CSVBucketName
- CSVFileName
- EmailTableName
- AdminEmailAddress
ParameterLabels:
# Deployment Configuration
S3BucketName:
default: CloudFormation S3 Bucket Name
S3BucketPrefix:
default: CloudFormation S3 Bucket Prefix
KMSKeyID:
default: KMS Key ID for SNS Encryption
# Notifier Settings
CSVBucketName:
default: NEW CSV Bucket Name
CSVFileName:
default: CSV File Name
EmailTableName:
default: Email Table Name
AdminEmailAddress:
default: Admin Email Address
Parameters:
S3BucketName:
Description: S3 Bucket Name where code is located.
Type: String
S3BucketPrefix:
Description: The prefix or directory where resources will be stored.
Type: String
Default: "iam-rotation"
KMSKeyID:
Description: The KMS Key ID you wish to use for encryption of your SNS Topic. (If using a custom Master Key, you must have permissions to utilize this key.)
Type: String
CSVBucketName:
Description: Name of a NEW S3 bucket that you will upload your CSV of email accounts.
Type: String
ConstraintDescription: Must be a valid bucket name
CSVFileName:
Description: Name of the S3 file (including suffix)
Type: String
Default: "csv-to-s3-account-emails.csv"
EmailTableName:
Description: Name of the dynamoDB table you will use
Type: String
Default: "aws-account-emails"
AdminEmailAddress:
Description: Email address that will be used in the "sent from" section of the email
Type: String
Resources:
# NOTIFIER SERVICE RESOURCES
EmailTable:
Type: "AWS::DynamoDB::Table"
Properties:
TableName: !Ref EmailTableName
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: uuid
AttributeType: S
KeySchema:
- AttributeName: uuid
KeyType: HASH
Tags:
- Key: Name
Value: !Ref EmailTableName
CsvToDDBLambdaRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
- s3.amazonaws.com
Action:
- "sts:AssumeRole"
ManagedPolicyArns:
- !Sub "arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
- !Sub "arn:${AWS::Partition}:iam::aws:policy/AWSLambdaInvocation-DynamoDB"
- !Sub "arn:${AWS::Partition}:iam::aws:policy/AmazonS3ReadOnlyAccess"
Policies:
- PolicyName: CsvToDDBPolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Resource: "*"
Action:
- "dynamodb:PutItem"
- "dynamodb:BatchWriteItem"
CsvToDDBLambdaFunction:
Type: "AWS::Lambda::Function"
Properties:
Handler: main.lambda_handler
Description: |
Imports CSV file from S3 to DynamoDB - specifically formatted
to work with "accountid, accountname, accountemail, accountowner"
FunctionName: CSV-to-DynamoDB-Import-Tool
Role: !GetAtt CsvToDDBLambdaRole.Arn
Code:
S3Bucket: !Ref S3BucketName
S3Key: !Sub ${S3BucketPrefix}/account_handler_0.83.0.zip
Runtime: python3.8
Timeout: 900
MemorySize: 3008
Environment:
Variables:
S3_BUCKET: !Ref CSVBucketName
S3_KEY: !Ref CSVFileName
DYNAMODB_TABLE_NAME: !Ref EmailTableName
CsvBucketPermission:
Type: "AWS::Lambda::Permission"
Properties:
Action: "lambda:InvokeFunction"
FunctionName: !Ref CsvToDDBLambdaFunction
Principal: s3.amazonaws.com
SourceAccount: !Ref "AWS::AccountId"
CsvS3Bucket:
DependsOn:
- CsvBucketPermission
Type: "AWS::S3::Bucket"
Properties:
BucketName: !Ref CSVBucketName
AccessControl: BucketOwnerFullControl
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: 'AES256'
VersioningConfiguration:
Status: Enabled
NotificationConfiguration:
LambdaConfigurations:
- Event: "s3:ObjectCreated:*"
Function: !GetAtt CsvToDDBLambdaFunction.Arn
NotifierFunctionExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: AllowExecutionPermissionsOnFunction
Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- !Sub "arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
Policies:
- PolicyName: AllowS3ZipRetrieval
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- s3:GetObject
Resource:
- !GetAtt CsvS3Bucket.Arn
- !Join
- ''
- - !GetAtt CsvS3Bucket.Arn
- /*
- Effect: Allow
Action:
- dynamodb:GetItem
Resource: !GetAtt EmailTable.Arn
- Effect: Allow
Action:
- ses:SendEmail
Resource: "*"
- Effect: Allow
Action:
- s3:GetObject
Resource:
- !Sub "arn:aws:s3:::${S3BucketName}/${S3BucketPrefix}"
- !Sub "arn:aws:s3:::${S3BucketName}/${S3BucketPrefix}/*"
NotifierLambdaFunction:
Type: AWS::Lambda::Function
Properties:
Description: Function that received SNS events from config rules and emails end users who own the account id of the resource violation.
FunctionName: Notifier
Handler: main.lambda_handler
Runtime: python3.8
Code:
S3Bucket: !Ref S3BucketName
S3Key: !Sub ${S3BucketPrefix}/notifier_0.83.0.zip
Role: !GetAtt NotifierFunctionExecutionRole.Arn
Timeout: 300
Environment:
Variables:
ADMIN_EMAIL: !Ref AdminEmailAddress
DYNAMODB_TABLE_NAME: !Ref EmailTableName
S3_BUCKET_NAME: !Ref S3BucketName
S3_BUCKET_PREFIX: !Sub ${S3BucketPrefix}
# IAM KEY ROTATION RESOURCES
RotationLambdaFunctionExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: AllowExecutionPermissionsOnFunction
Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
- events.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- !Sub "arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
Policies:
- PolicyName: AllowRotationFunctionPermissions
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- s3:GetObject
Resource:
- !Sub "arn:${AWS::Partition}:s3:::${S3BucketName}/*"
- Effect: Allow
Action:
- iam:List*
- iam:CreatePolicy
- iam:CreateAccessKey
- iam:DeleteAccessKey
- iam:UpdateAccessKey
- iam:PutUserPolicy
- iam:GetUserPolicy
Resource: "*"
- Effect: Allow
Action:
- iam:AttachUserPolicy
Resource:
- !Sub "arn:${AWS::Partition}:iam::${AWS::AccountId}:user/*"
- Effect: Allow
Action:
- secretsmanager:PutResourcePolicy
- secretsmanager:PutSecretValue
- secretsmanager:DescribeSecret
- secretsmanager:CreateSecret
- secretsmanager:GetResourcePolicy
Resource:
- !Sub "arn:${AWS::Partition}:secretsmanager:${AWS::Region}:${AWS::AccountId}:secret:*"
RotationSNSTopic:
Type: AWS::SNS::Topic
Properties:
TopicName: SNSNotificationForNewAccessKeyCreation
KmsMasterKeyId: !Ref KMSKeyID
Subscription:
- Endpoint: !GetAtt NotifierLambdaFunction.Arn
Protocol: lambda
RotationAccessKeyRotateLambdaFunction:
Type: AWS::Lambda::Function
Properties:
Description: Rotates IAM Access Keys on specified schedule
FunctionName: IAM-Access-Key-Rotation-LambdaFunctionName
Handler: main.lambda_handler
Runtime: python3.8
Role: !GetAtt RotationLambdaFunctionExecutionRole.Arn
Timeout: 240
Environment:
Variables:
RotationPeriod: 90
InactivePeriod: 100
RetentionPeriod: 110
Code:
S3Bucket: !Ref S3BucketName
S3Key: !Sub ${S3BucketPrefix}/access_key_auto_rotation_0.83.0.zip
RotationCloudWatchEventLambdaTrigger:
Type: AWS::Events::Rule
DependsOn:
- RotationLambdaFunctionExecutionRole
Properties:
Description: CloudWatch Event to trigger Access Key auto-rotation Lambda Function daily
ScheduleExpression: rate(24 hours)
State: ENABLED
Targets:
- Arn: !GetAtt RotationAccessKeyRotateLambdaFunction.Arn
Id: AccessKeyRotationFunction
RotationCloudWatchEventsLambdaPermissions:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !Ref NotifierLambdaFunction
Action: lambda:InvokeFunction
Principal: sns.amazonaws.com
SourceArn: !Ref RotationSNSTopic
RotationCloudWatchEventTopicPolicy:
Type: AWS::SNS::TopicPolicy
Properties:
PolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: events.amazonaws.com
Action: sns:Publish
Resource: "*"
Topics:
- !Ref RotationSNSTopic
RotationCloudWatchEventSNSTrigger:
Type: AWS::Events::Rule
Properties:
Description: CloudWatch Event to trigger SNS notification for IAM Access Key generation
EventPattern:
source:
- "aws.iam"
detail-type:
- "AWS API Call via CloudTrail"
detail:
eventSource:
- "iam.amazonaws.com"
eventName:
- "CreateAccessKey"
State: ENABLED
Targets:
- Arn: !Ref RotationSNSTopic
Id: "AccessKeyRotationSNSNotification"