-
Notifications
You must be signed in to change notification settings - Fork 0
/
lights_off_aws_prereq.yaml
246 lines (228 loc) · 9.99 KB
/
lights_off_aws_prereq.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
---
AWSTemplateFormatVersion: "2010-09-09"
Description: |-
Deployment role for
github.com/sqlxpert/lights-off-aws/ GPLv3 Copyright Paul Marcelin
Parameters:
PlaceholderHelp:
Type: String
Default: "https://github.com/sqlxpert/lights-off-aws#least-privilege"
LambdaSourceS3BucketNamePrefix:
Type: String
Description: >-
Exclude the region suffix, even though Lights Off REQUIRES a region
suffix in the S3 bucket name. For example, if you are creating this
stack in the US East (Northern Virginia) region and you set this to
"my-bucket", then the bucket named "my-bucket-us-east-1" must exist,
must have been created in the US East (Northern Virginia) region, and
must contain lights_off_aws.py.zip . These requirements apply in EVERY
region where you create a Lights Off CloudFormation stack. If you are
are creating a CloudFormation StackSet, then the file must also be
readable by every target AWS account, which generally requires that each
bucket have a bucket policy.
StackNameBase:
Type: String
Description: >-
When using the DeploymentRole provided by this stack, you MUST include
this string in the name of any Lights Off CloudFormation stack or
StackSet that you create. For security, no other stack or StackSet's
name should include this string. You may add prefixes and suffixes to
your future stack or StackSet's name. For example, if you set this to
"LightsOff", you can alternate between a "LightsOff1" stack and a
"LightsOff2" stack for blue/green deployment of updates, or you can
create a "TestLightsOff" stack marked for testing.
Default: "LightsOff"
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: Essentials
Parameters:
- PlaceholderHelp
- LambdaSourceS3BucketNamePrefix
- StackNameBase
ParameterLabels:
PlaceholderHelp:
default: For help with this stack, see
LambdaSourceS3BucketNamePrefix:
default: S3 bucket for AWS Lambda function source code
StackNameBase:
default: >-
Name of future stack or StackSet
Resources:
DeploymentRole:
Type: AWS::IAM::Role
Properties:
Description: >-
Resources in Lights Off CloudFormation stack: create, update, delete
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal: { Service: cloudformation.amazonaws.com }
Action: sts:AssumeRole
# In-line policies apply only to this role, which, in turn, can only be
# assumed by CloudFormation. Separate, "managed" policies could be
# attached to other roles or users, allowing permission escalation.
# Administrator should restrict iam:PassRole to prevent use of this role
# with arbitrary CloudFormation stacks.
Policies:
- PolicyName: LightsOffCloudFormationStackDeploy
PolicyDocument:
Version: "2012-10-17"
Statement:
# Sufficient only for the current AWS account. For a multi-account
# deployment, also allow access from all StackSets target accounts,
# in each S3 bucket's bucket policy.
- Effect: Allow
Action:
- s3:GetObject*
Resource:
- !Sub "arn:${AWS::Partition}:s3:::${LambdaSourceS3BucketNamePrefix}-*/*"
- Effect: Allow
Action:
- lambda:CreateFunction
- lambda:GetFunction
- lambda:DeleteFunction
- lambda:UpdateFunctionCode
- lambda:GetFunctionConfiguration
- lambda:UpdateFunctionConfiguration
- lambda:AddPermission
- lambda:RemovePermission
- lambda:PutFunctionConcurrency
- lambda:DeleteFunctionConcurrency
- lambda:TagResource
- lambda:UntagResource
Resource:
- !Sub "arn:${AWS::Partition}:lambda:*:${AWS::AccountId}:function:StackSet-*${StackNameBase}*"
- !Sub "arn:${AWS::Partition}:lambda:*:${AWS::AccountId}:function:*${StackNameBase}*"
- Effect: Allow
Action:
- lambda:CreateEventSourceMapping
- lambda:GetEventSourceMapping
- lambda:UpdateEventSourceMapping
- lambda:DeleteEventSourceMapping
Resource: "*"
Condition:
ArnLikeIfExists:
"lambda:FunctionArn":
- !Sub "arn:${AWS::Partition}:lambda:*:${AWS::AccountId}:function:StackSet-*${StackNameBase}*"
- !Sub "arn:${AWS::Partition}:lambda:*:${AWS::AccountId}:function:*${StackNameBase}*"
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:DeleteLogGroup
- logs:PutRetentionPolicy
- logs:DeleteRetentionPolicy
- logs:TagLogGroup
- logs:UntagLogGroup
Resource:
- !Sub "arn:${AWS::Partition}:logs:*:${AWS::AccountId}:log-group:/aws/lambda/StackSet-*${StackNameBase}*"
- !Sub "arn:${AWS::Partition}:logs:*:${AWS::AccountId}:log-group:/aws/lambda/*${StackNameBase}*"
- Effect: Allow
Action:
- logs:DescribeLogGroups
Resource: "*"
- Effect: Allow
Action:
- events:PutRule
- events:DescribeRule
- events:EnableRule
- events:DisableRule
- events:DeleteRule
- events:PutTargets
- events:ListTargetsByRule
- events:RemoveTargets
- events:TagResource
- events:UntagResource
Resource:
- !Sub "arn:${AWS::Partition}:events:*:${AWS::AccountId}:rule/StackSet-*${StackNameBase}*"
- !Sub "arn:${AWS::Partition}:events:*:${AWS::AccountId}:rule/*${StackNameBase}*"
- Effect: Allow
Action:
- sqs:CreateQueue
- sqs:SetQueueAttributes
- sqs:GetQueueAttributes
- sqs:GetQueueUrl
- sqs:ListDeadLetterSourceQueues
- sqs:DeleteQueue
- sqs:AddPermission
- sqs:RemovePermission
- sqs:TagQueue
- sqs:UntagQueue
Resource:
- !Sub "arn:${AWS::Partition}:sqs:*:${AWS::AccountId}:StackSet-*${StackNameBase}*"
- !Sub "arn:${AWS::Partition}:sqs:*:${AWS::AccountId}:*${StackNameBase}*"
- Effect: Allow
Action:
- sqs:ListQueues
Resource: "*"
- Effect: Allow
Action: iam:PassRole
Resource:
- !Sub "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/StackSet-*${StackNameBase}*"
- !Sub "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/*${StackNameBase}*"
- Effect: Allow
Action:
- iam:CreatePolicy
- iam:GetPolicy
- iam:DeletePolicy
- iam:CreatePolicyVersion
- iam:ListPolicyVersions
- iam:GetPolicyVersion
- iam:DeletePolicyVersion
- iam:TagPolicy
- iam:UntagPolicy
Resource:
- !Sub "arn:${AWS::Partition}:iam::${AWS::AccountId}:policy/StackSet-*${StackNameBase}*"
- !Sub "arn:${AWS::Partition}:iam::${AWS::AccountId}:policy/*${StackNameBase}*"
- Effect: Allow
Action:
- iam:CreateRole
- iam:GetRole
- iam:DeleteRole
- iam:UpdateAssumeRolePolicy
- iam:ListRolePolicies
- iam:GetRolePolicy
- iam:AttachRolePolicy
- iam:DetachRolePolicy
- iam:PutRolePolicy
- iam:GetRolePolicy
- iam:DeleteRolePolicy
- iam:ListEntitiesForPolicy
Resource:
- !Sub "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/StackSet-*${StackNameBase}*"
- !Sub "arn:${AWS::Partition}:iam::${AWS::AccountId}:policy/StackSet-*${StackNameBase}*"
- !Sub "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/*${StackNameBase}*"
- !Sub "arn:${AWS::Partition}:iam::${AWS::AccountId}:policy/*${StackNameBase}*"
- Effect: Allow
Action:
- iam:TagRole
- iam:UntagRole
Resource:
- !Sub "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/StackSet-*${StackNameBase}*"
- !Sub "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/*${StackNameBase}*"
- Effect: Allow
Action:
- iam:ListAttachedRolePolicies
Resource: "*"
- Effect: Allow
Action:
- kms:ListKeys
- kms:ListAliases
- kms:DescribeKey
Resource: "*"
SampleDeploymentRolePassRolePol:
Type: "AWS::IAM::ManagedPolicy"
Properties:
Description:
Fn::Sub: >-
${DeploymentRole}: pass to CloudFormation. Demonstrates a privilege
that non-adminstrators need before they can create a Lights Off
CloudFormation stack using the deployment role.
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action: "iam:PassRole"
Resource: !GetAtt DeploymentRole.Arn