-
Notifications
You must be signed in to change notification settings - Fork 21
/
cf.yml
205 lines (182 loc) · 6.48 KB
/
cf.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
AWSTemplateFormatVersion: '2010-09-09'
Description: AWS CloudFormation template to deploy Access Advisor Automation to assist with least privileged implementation
Parameters:
Environment:
Description: Please specify the target environment.
Type: String
Default: "prod"
AllowedValues:
- prod
- staging
- dev
AppName:
Description: Application environment name.
Type: String
Default: "AccessAdvisorAutomation"
S3Bucket:
Description: S3 bucket name with installation file
Type: String
Default: "ddmitriy-aapb"
S3Key:
Description: "S3Key, name of the function zip file with the script. File name must match - Lambda: Handler: accessadvisor_automation.lambda_handler"
Type: String
Default: "accessadvisor_automation.zip"
S3LambdaLayerKey:
Description: S3Key, name of the zip file with the script
Type: String
Default: "boto3layer.zip"
DoNotListBucket:
Description: S3 bucket with do not list (exception list), same bucket used for base permissions boundary actions list
Type: String
Default: "ddmitriy-aapb"
DoNotListKey:
Description: S3 key, do not list file name, just a list
Type: String
Default: "do_not_list.txt"
DaysExpire:
Description: Days for permissions to expire if not accessed, if services not accessed in more the X days, it will not be included in permissions boundary. This will remove access to the services.
Type: Number
Default: 180
ConstraintDescription: 'Must be in the range [1-365]'
MinValue: 1
MaxValue: 255
Enforce:
Description: Set to 'yes' to enforce permissions boundary, or 'no' to only audit, tag roles and users
Type: String
Default: 'yes'
BaseActions:
Description: S3 key, file with base "Actions" to populate base or defualt permissions boundary, just a list
Type: String
Default: "base_actions.txt"
Resources:
# CLOUDWATCH EVENT
CloudWatchEventAccessAdvisor:
Type: "AWS::Events::Rule"
Properties:
Description: "Scheduled cloudwatch event for Access Advisor "
Name: !Sub "AccessAdvisorAutomation"
State: "ENABLED"
EventPattern:
ScheduleExpression:
- "rate(90 day)"
Targets:
-
Arn: !GetAtt "AccessAdvisorAutomationLambda.Arn"
Id: "AccessAdvisorAutomationLambda"
# LAMBDA IAM PERMISSIONS
PermissionForEventsToInvokeLambda:
Type: 'AWS::Lambda::Permission'
Properties:
FunctionName: !GetAtt "AccessAdvisorAutomationLambda.Arn"
Action: "lambda:InvokeFunction"
Principal: "events.amazonaws.com"
SourceArn: !GetAtt "CloudWatchEventAccessAdvisor.Arn"
# ACCESS ADVISOR IAM LAMBDA ROLE
AccessAdvisorAutomationLambdaRole:
Type: 'AWS::IAM::Role'
Description: "Access Advisor Lambda IAM Role"
DependsOn:
- AccessAdvisorAutomation
Properties:
RoleName: AccessAdvisorAutomationLambdaRole
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
- events.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- !Ref 'AccessAdvisorAutomation'
- arn:aws:iam::aws:policy/AWSXrayWriteOnlyAccess
Path: '/'
# IAM POLICY - IAM POLICY MANAGEMENT
AccessAdvisorAutomation:
Type: 'AWS::IAM::ManagedPolicy'
Properties:
Description: 'Access Advisor Automation Management policy'
ManagedPolicyName: !Sub 'AccessAdvisor-Automation-Management'
PolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: AccessAdvisorAutomation
Effect: "Allow"
Action:
- "iam:GetServiceLastAccessedDetailsWithEntities"
- "iam:GenerateServiceLastAccessedDetails"
- "iam:GetServiceLastAccessedDetails"
- "iam:TagRole"
- "iam:ListUsers"
- "iam:ListRoles"
- "iam:ListGroups"
- "iam:TagUser"
- "iam:GetPolicy"
- "iam:GetPolicyVersion"
- "iam:CreatePolicy"
- "iam:PutUserPermissionsBoundary"
- "iam:PutRolePermissionsBoundary"
- "iam:CreatePolicyVersion"
- "iam:DeletePolicyVersion"
- "s3:List*"
- "s3:Get*"
- "kms:Decrypt"
Resource:
- "*"
- Sid: CloudwatchLogs
Effect: "Allow"
Action:
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
Resource:
- "*"
# LAMBDA FUNCTION
AccessAdvisorAutomationLambda:
Type: 'AWS::Lambda::Function'
DependsOn: AccessAdvisorAutomationLambdaRole
Properties:
Description: !Sub 'A custom Lambda function for Access Advisor automation'
FunctionName: !Sub '${AppName}-${Environment}-AccessAdvisor-automation'
Handler: accessadvisor_automation.lambda_handler
Role: !GetAtt 'AccessAdvisorAutomationLambdaRole.Arn'
Runtime: "python3.7"
Layers:
- !Ref LambdaLayer1
Code:
S3Bucket: !Ref 'S3Bucket'
S3Key: !Ref 'S3Key'
Timeout: 900
MemorySize: 128
Environment:
Variables:
DoNotListBucket: !Ref DoNotListBucket
DoNotListKey: !Ref DoNotListKey
DaysExpire: !Ref DaysExpire
Enforce: !Ref Enforce
BaseActions: !Ref BaseActions
TracingConfig:
Mode: "Active"
LambdaLayer1:
Type: AWS::Lambda::LayerVersion
Properties:
CompatibleRuntimes:
- python3.6
Content:
S3Bucket: !Ref S3Bucket
S3Key: !Ref S3LambdaLayerKey
Description: "boto3 layer"
LayerName: boto3layer
LicenseInfo: Apache2.0
Outputs:
AccessAdvisorAutomationLambda:
Description: "The Name of the Access Advisor automation Lambda Function"
Value: !Sub 'AccessAdvisorAutomationLambda'
Export:
Name: !Sub '${AppName}-${Environment}-Enforce-${Enforce}-AA-automation'
AccessAdvisorAutomationLambdaArn:
Description: "The ARN of the Access Advisor automation Lambda Function"
Value: !GetAtt 'AccessAdvisorAutomationLambda.Arn'
Export:
Name: !Sub '${AppName}-${Environment}-Enforce-${Enforce}-AA-automation-arn'