-
Notifications
You must be signed in to change notification settings - Fork 3
/
Central-SecurityAcnt-BaseTemplate.yaml
249 lines (249 loc) · 10.4 KB
/
Central-SecurityAcnt-BaseTemplate.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
AWSTemplateFormatVersion: 2010-09-09
Description: 'Step 1 - This template should be deployed in all region of the central audit account to create all the necessary resources for collecting Inspector findings from all application accounts.'
Parameters:
CentralSecurityAccountID:
Type: String
Description: Account ID of the Central Audit Account
PrimaryRegion:
Type: String
Description: Name of region in Central Audit Account where the central SQS Queue and lambda function to be deplyed (should be same as where ES domain exists)
Default: us-east-1
LambdaSourceCodeS3Bucket:
Type: String
Description: Name of the S3 Bucket in Central Audit Account where you uploaded the lambda package for sending findings from Inspector to Elasticsearch domain and S3
AllowedPattern: ^[0-9A-Za-z\.\-_]*(?<!\.)$
LambdaCodeS3Key:
Type: String
Description: Name of the S3 Key you uploaded the Inspector2S3ES lambda package as
Default: Inspector-to-S3ES-crossAcnt.zip
LambdaExecutionRoleName:
Type: String
Description: Name of the execution role (not ARN) that is assumed by Lambda function
Default: Inspector-to-S3-ES-LambdaExec.iamrole
CrossAccountRoleName:
Type: String
Description: Name of the role (not ARN) to be created (in the next template) in all application accounts that is assumed by Lambda role of audit account for fetching Inspector findings
Default: CrossAccountInspector.iamrole
LoggingS3Bucket:
Type: String
Description: Name of the S3 Bucket in Central Audit Account where all the Inspector findings need to be stored
AllowedPattern: ^[0-9A-Za-z\.\-_]*(?<!\.)$
ElasticsearchDomainName:
Type: String
Description: Name of the Elasticsearch (ES) domain in Central Audit Account where all the Inspector findings are to be forwarded
ElasticsearchEndpoint:
Type: String
Description: 'URL to reach the ES domain in Central Audit Account [ REMOVE the "https://" portion from the endpoint URL]'
ElasticsearchIndex:
Type: String
Description: Name of the ES index to which the Inspector findings are posted
Default: 'inspector-findings-index'
RegionalSNSTopicName:
Type: String
Description: Name of the regional SNS Topic that will notify SQS on Inspector findings reported in its region of all application accounts
Default: Inspector-to-SQS-topic
CentralDeadLetterQueueName:
Type: String
Description: Name of the Dead-Letter queue used for handling message failure to the main queue
Default: Inspector-to-S3-ES-DeadLetter-queue
CentralMainSQSQueueName:
Type: String
Description: Name of the Main SQS queue where all the Inspector findings collected across the fleet will be sent
Default: Inspector-to-S3-ES-Main-queue
Conditions:
CreatePrimaryRegionResources: !Equals [ !Ref PrimaryRegion, !Ref "AWS::Region" ]
Resources:
RegionalSNSTopic:
Type: 'AWS::SNS::Topic'
Properties:
TopicName: !Ref RegionalSNSTopicName
RegionalSNSTopicPolicy:
Type: 'AWS::SNS::TopicPolicy'
Properties:
PolicyDocument:
Version: 2008-10-17
Id: policy_ID
Statement:
- Sid: TopicAccessPolicyforInspector
Effect: Allow
Principal:
AWS:
- 'arn:aws:iam::316112463485:root' # Inspector account for N. Virginia
- 'arn:aws:iam::758058086616:root' # Inspector account for Oregon
- 'arn:aws:iam::646659390643:root' # Inspector account for Ohio
- 'arn:aws:iam::166987590008:root' # Inspector account for N. California
- 'arn:aws:iam::162588757376:root' # Inspector account for Mumbai
- 'arn:aws:iam::526946625049:root' # Inspector account for Seoul
- 'arn:aws:iam::454640832652:root' # Inspector account for Sydney
- 'arn:aws:iam::406045910587:root' # Inspector account for Tokyo
- 'arn:aws:iam::537503971621:root' # Inspector account for Frankfurt
- 'arn:aws:iam::357557129151:root' # Inspector account for Ireland
- 'arn:aws:iam::146838936955:root' # Inspector account for London
- 'arn:aws:iam::453420244670:root' # Inspector account for Stockholm
Action:
- 'SNS:Publish'
- 'SNS:Subscribe'
Resource: !Ref RegionalSNSTopic
- Sid: TopicAccessPolicyforCentralAcntSQS
Effect: Allow
Principal:
AWS: !Sub 'arn:aws:iam::${CentralSecurityAccountID}:root' # Central account where the SQS Queue is created
Action:
- 'SNS:Subscribe'
Resource: !Ref RegionalSNSTopic
Topics:
- !Ref RegionalSNSTopic
CentralDeadLetterQueue:
Condition: CreatePrimaryRegionResources
Type: 'AWS::SQS::Queue'
Properties:
QueueName: !Ref CentralDeadLetterQueueName
CentralSQSQueue:
Condition: CreatePrimaryRegionResources
Type: 'AWS::SQS::Queue'
Properties:
QueueName: !Ref CentralMainSQSQueueName
MessageRetentionPeriod: 86400
RedrivePolicy:
deadLetterTargetArn: !GetAtt CentralDeadLetterQueue.Arn
maxReceiveCount: 100
DependsOn:
- CentralDeadLetterQueue
CentralSQSQueuePolicy:
Condition: CreatePrimaryRegionResources
Type: 'AWS::SQS::QueuePolicy'
Properties:
PolicyDocument:
Version: 2012-10-17
Id: 'SQSDefaultPolicy'
Statement:
- Sid: 'Permission for Queue to receive messages from a specific SNS Topic'
Effect: Allow
Principal: '*'
Action:
- 'SQS:ReceiveMessage'
Resource: !GetAtt CentralSQSQueue.Arn
Condition:
ArnEquals:
'aws:SourceArn': !Sub 'arn:aws:sns:us-*:${CentralSecurityAccountID}:${RegionalSNSTopicName}'
- Sid: 'Permission for an SNS Topic to send messages to the Queue'
Effect: Allow
Principal: '*'
Action:
- 'SQS:SendMessage'
Resource: !GetAtt CentralSQSQueue.Arn
Condition:
ArnEquals:
'aws:SourceArn': !Sub 'arn:aws:sns:us-*:${CentralSecurityAccountID}:${RegionalSNSTopicName}'
Queues:
- !Ref CentralSQSQueue
SnsSubscription:
Type: AWS::SNS::Subscription
Properties:
Protocol: sqs
Endpoint: !Sub "arn:aws:sqs:${PrimaryRegion}:${CentralSecurityAccountID}:${CentralMainSQSQueueName}"
Region: !Ref "AWS::Region"
TopicArn: !Ref RegionalSNSTopic
LambdaExecutionRole:
Condition: CreatePrimaryRegionResources
Type: 'AWS::IAM::Role'
Properties:
RoleName: !Ref LambdaExecutionRoleName
Description: "This role is used for collecting Inspector results and sent it to a centralized S3 bucket and and Elasticsearch domain. Do not delete this role."
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: 'sts:AssumeRole'
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
LambdaExecutionRolePolicy:
Condition: CreatePrimaryRegionResources
Type: 'AWS::IAM::ManagedPolicy'
Properties:
ManagedPolicyName: 'Inspector-to-S3-ES-LambdaExec.iampolicy'
PolicyDocument:
Version: 2012-10-17
Statement:
- Sid: AddingSQSPermissions
Action:
- 'sqs:GetQueueUrl'
- 'sqs:ListDeadLetterSourceQueues'
- 'sqs:ReceiveMessage'
- 'sqs:DeleteMessage'
- 'sqs:GetQueueAttributes'
Effect: Allow
Resource:
- !GetAtt CentralDeadLetterQueue.Arn
- !GetAtt CentralSQSQueue.Arn
- Sid: AddingS3BucketPermissions
Action:
- 's3:ListBucket'
Effect: Allow
Resource: !Sub 'arn:aws:s3:::${LoggingS3Bucket}'
- Sid: AddingS3ObjectPermissions
Action:
- 's3:PutObject'
Effect: Allow
Resource: !Sub 'arn:aws:s3:::${LoggingS3Bucket}/*'
- Sid: AddingEC2AndInspectorAndSQSPermissionsforAll
Action:
- 'ec2:DescribeInstances'
- 'ec2:DescribeVpcAttribute'
- 'ec2:DescribeVpcs'
- 'ec2:DescribeSubnets'
- 'inspector:List*'
- 'inspector:PreviewAgents'
- 'inspector:AddAttributesToFindings'
- 'inspector:Describe*'
- 'inspector:GetTelemetryMetadata'
- 'sqs:ListQueues'
Effect: Allow
Resource:
- '*'
- Sid: CrossAccountLambdaPermission
Action:
- 'sts:AssumeRole'
Effect: Allow
Resource: !Sub 'arn:aws:iam::*:role/${CrossAccountRoleName}'
- Sid: ElasticsearchPermissions
Action:
- 'es:ESHttpPost'
- 'es:ESHttpPut'
Effect: Allow
Resource: !Sub 'arn:aws:es:${PrimaryRegion}:${CentralSecurityAccountID}:domain/${ElasticsearchDomainName}/*'
Roles:
- !Ref LambdaExecutionRole
Inspector2S3LambdaFunction:
Condition: CreatePrimaryRegionResources
Type: 'AWS::Lambda::Function'
Properties:
Code:
S3Bucket: !Ref LambdaSourceCodeS3Bucket
S3Key: !Ref LambdaCodeS3Key
Description: 'This function sends inspector findings with additional attributes to a S3 bucket and a Elasticsearch domain in Central Audit account for further analysis'
Environment:
Variables:
es_host: !Ref ElasticsearchEndpoint
es_index: !Ref ElasticsearchIndex
crossAccount_role: !Ref CrossAccountRoleName
s3_logging_bucket: !Ref LoggingS3Bucket
FunctionName: 'Inspector-to-S3ES-crossAcnt'
Handler: 'Inspector-to-S3ES-crossAcnt.lambda_handler'
Role: !GetAtt LambdaExecutionRole.Arn
Runtime: 'python3.7'
Timeout: 25
DependsOn:
- CentralSQSQueue
- LambdaExecutionRole
LambdaTrigger:
Condition: CreatePrimaryRegionResources
Type: 'AWS::Lambda::EventSourceMapping'
Properties:
BatchSize: 1 # This makes sure that each Inspector finding is processed as 1 event in Lambda and thus forward as 1 record
EventSourceArn: !GetAtt CentralSQSQueue.Arn
FunctionName: !GetAtt Inspector2S3LambdaFunction.Arn
DependsOn:
- LambdaExecutionRolePolicy