-
Notifications
You must be signed in to change notification settings - Fork 11
/
template.yml
284 lines (261 loc) · 8.15 KB
/
template.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
AWSTemplateFormatVersion: "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"
Description: >
GitHub Secret Scanner Auto Remediator (GSSAR)
Resources:
### Lambda Functions ###
## Authorizers
GitHubWebhookSecretValidator:
Type: "AWS::Serverless::Function"
Properties:
CodeUri: functions/authorizers/githubWebhookSecretValidator
Description: "A Lambda that validates a GitHub Secret"
Environment:
Variables:
NODE_ENV: Production
REGION: !Ref "AWS::Region"
EVENT_BUS_NAME: !Ref GSSAREventBus
Events:
GatewayEndpoint:
Properties:
ApiId: !Ref HttpApi
Method: POST
Path: /github/webhook/validate
Type: HttpApi
Handler: lib/main.handler
Policies:
- AmazonSSMReadOnlyAccess
- EventBridgePutEventsPolicy:
EventBusName: !Ref GSSAREventBus
Runtime: nodejs14.x
Timeout: 60
Tracing: Active
GitHubWebhookIPValidator:
Type: "AWS::Serverless::Function"
Properties:
CodeUri: functions/authorizers/githubWebhookIPValidator
Description: "A Lambda Function that validates the IP comes from GitHub"
Environment:
Variables:
NODE_ENV: Production
REGION: !Ref "AWS::Region"
Handler: lib/main.handler
Policies:
- AmazonSSMReadOnlyAccess
Runtime: nodejs14.x
Timeout: 60
Tracing: Active
## Helpers
GetSecretDetails:
Type: "AWS::Serverless::Function"
Properties:
CodeUri: functions/helpers/getSecretDetails
Description: "A Lambda function that calls the GitHub API to return details about the given Secret in question"
Environment:
Variables:
NODE_ENV: Production
REGION: !Ref "AWS::Region"
Handler: lib/main.handler
Policies:
- AmazonSSMReadOnlyAccess
Runtime: nodejs14.x
Timeout: 60
Tracing: Active
GitHubIssueNotifier:
Type: "AWS::Serverless::Function"
Properties:
CodeUri: functions/helpers/githubIssueNotifier
Description: "A Lambda function that creates a GitHub Issue to Notify the repository of the secret remediation"
Environment:
Variables:
NODE_ENV: Production
REGION: !Ref "AWS::Region"
Handler: lib/main.handler
Policies:
- AmazonSSMReadOnlyAccess
Runtime: nodejs14.x
Timeout: 60
Tracing: Active
CloseSecret:
Type: "AWS::Serverless::Function"
Properties:
CodeUri: functions/helpers/closeSecret
Description: "A Lambda function closes the GitHub Secret that has been remediated"
Environment:
Variables:
NODE_ENV: Production
REGION: !Ref "AWS::Region"
Handler: lib/main.handler
Policies:
- AmazonSSMReadOnlyAccess
Runtime: nodejs14.x
Timeout: 60
Tracing: Active
## Remediators
RemediateAWSAccessKey:
Type: "AWS::Serverless::Function"
Properties:
CodeUri: functions/remediators/remediateAWSAccessKey
Description: "A Lambda that remediated AWS Access Keys"
Environment:
Variables:
NODE_ENV: Production
REGION: !Ref "AWS::Region"
Handler: lib/main.handler
Policies:
- AmazonSSMReadOnlyAccess
- Statement:
- Sid: IAMGet
Effect: Allow
Action:
- iam:ListAccessKeys
- iam:UpdateAccessKey
- iam:DeleteAccessKey
- iam:CreateAccessKey
- iam:GetAccessKeyLastUsed
Resource: "*"
Runtime: nodejs14.x
Timeout: 60
Tracing: Active
RemediateGoogleAPIKey:
Type: "AWS::Serverless::Function"
Properties:
CodeUri: functions/remediators/remediateGoogleAPIKey
Description: "A Lambda that remediates Google API Keys"
Environment:
Variables:
NODE_ENV: Production
LOCATION: global
Handler: lib/main.handler
Policies:
- AmazonSSMReadOnlyAccess
- Statement:
- Sid: IAMGet
Effect: Allow
Action:
- iam:ListAccessKeys
- iam:UpdateAccessKey
- iam:DeleteAccessKey
- iam:CreateAccessKey
- iam:GetAccessKeyLastUsed
Resource: "*"
Runtime: nodejs14.x
Timeout: 60
Tracing: Active
### Event Bus ###
GSSAREventBus:
Type: AWS::Events::EventBus
Properties:
Name: !Join ["-", [GSSAR, !Ref AWS::StackName]]
### API Gateways ###
HttpApi:
Type: AWS::Serverless::HttpApi
Properties:
FailOnWarnings: True
RouteSettings:
"POST /github/webhook/validate":
ThrottlingBurstLimit: 10
Auth:
Authorizers:
LambdaAuthorizer:
AuthorizerPayloadFormatVersion: "2.0"
EnableSimpleResponses: true
FunctionArn: !GetAtt GitHubWebhookIPValidator.Arn
FunctionInvokeRole: !GetAtt LambdaInvokeRole.Arn
DefaultAuthorizer: LambdaAuthorizer
### IAM Roles & Policies ###
StepFunctionsApiRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- apigateway.amazonaws.com
Action:
- "sts:AssumeRole"
Policies:
- PolicyName: AllowSFNExec
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: "states:StartExecution"
Resource: !Ref SecretScannerStateMachine
LambdaInvokeRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- apigateway.amazonaws.com
Action:
- "sts:AssumeRole"
LambdaInvokePolicy:
Type: "AWS::IAM::Policy"
Properties:
PolicyName: "LambdaInvokePolicy"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action: "lambda:InvokeFunction"
Resource: !GetAtt GitHubWebhookIPValidator.Arn
Roles:
- Ref: "LambdaInvokeRole"
### State Machines (Step Functions) ###
SecretScannerStateMachine:
Type: AWS::Serverless::StateMachine
Properties:
DefinitionUri: stepfunctions/secretscanner.yml
DefinitionSubstitutions:
GitHubWebhookSecretValidatorFunctionArn: !GetAtt GitHubWebhookSecretValidator.Arn
RemediateAWSAccessKeyFunctionArn: !GetAtt RemediateAWSAccessKey.Arn
RemediateGoogleAPIKeyFunctionArn: !GetAtt RemediateGoogleAPIKey.Arn
GetSecretDetailsFunctionArn: !GetAtt GetSecretDetails.Arn
GitHubIssueNotifierFunctionArn: !GetAtt GitHubIssueNotifier.Arn
CloseSecretFunctionArn: !GetAtt CloseSecret.Arn
Type: EXPRESS
Events:
EBRule:
Type: EventBridgeRule
Properties:
EventBusName: !Ref GSSAREventBus
RetryPolicy:
MaximumRetryAttempts: 2
DeadLetterConfig:
Type: SQS
QueueLogicalId: SecretScannerDeadLetterQueue
Pattern:
source:
- custom.kickOffSecretScanRemediation
detail-type:
- transaction
Policies:
- LambdaInvokePolicy:
FunctionName: !Ref GitHubWebhookSecretValidator
- LambdaInvokePolicy:
FunctionName: !Ref RemediateAWSAccessKey
- LambdaInvokePolicy:
FunctionName: !Ref RemediateGoogleAPIKey
- LambdaInvokePolicy:
FunctionName: !Ref GetSecretDetails
- LambdaInvokePolicy:
FunctionName: !Ref GitHubIssueNotifier
- LambdaInvokePolicy:
FunctionName: !Ref CloseSecret
- CloudWatchLogsFullAccess
Outputs:
HttpApiUrl:
Description: URL of your API endpoint
Value:
Fn::Sub: "https://${HttpApi}.execute-api.${AWS::Region}.${AWS::URLSuffix}/"
HttpApiId:
Description: Api id of HttpApi
Value:
Ref: HttpApi