/
akto-mirroring-k8s-public-template.yaml
350 lines (344 loc) · 11 KB
/
akto-mirroring-k8s-public-template.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
AWSTemplateFormatVersion: 2010-09-09
Description: AWS cloudFormation template to configure traffic mirroring sessions
Parameters:
SourceK8sClusterName:
Description: 'EKS Cluster name'
Type: String
Default: ''
SourceK8sNamespaces:
Description: 'Comma separated list of namespaces ( eg: staging, production, default ). Leave empty if you want to consider all namespaces'
Type: CommaDelimitedList
Default: ''
InstancesPerService:
Description: Number of instances to be mirrored per service in that namespace (To mirror traffic from all instances, enter any number less than 1. AWS currently limits traffic mirroring to 10 instances. If you want more source instances, please email to support@akto.io.)
Type: Number
Default: 1
TargetENI:
Description: ENI id of target interface
Type: String
Default: ''
TargetLB:
Description: Target Loadbalancer ARN
Type: String
Default: ''
MirroringSessionNumber:
Description: Unique mirroring session number to be used for mirroring session
Type: String
Default: '1'
Conditions:
CreateMirroringSession: !Not
- !Equals
- !Select
- 0
- !Ref SourceK8sNamespaces
- ''
isTargetNLB: !Not
- !Equals
- !Ref TargetLB
- ''
Resources:
TrafficMirrorTarget:
Type: 'AWS::EC2::TrafficMirrorTarget'
Properties:
Description: Traffic Mirror target set to network interface of Akto EC2 instance
Tags:
- Key: Name
Value: !Join
- ''
- - !Ref 'AWS::StackName'
- '-'
- Target
- Key: Deployment
Value: Akto-CloudFormation
NetworkInterfaceId: !If
- isTargetNLB
- !Ref AWS::NoValue
- !Ref TargetENI
NetworkLoadBalancerArn: !If
- isTargetNLB
- !Ref TargetLB
- !Ref AWS::NoValue
K8sTrafficMirrorFilter:
Type: 'AWS::EC2::TrafficMirrorFilter'
Properties:
Description: Traffic mirror filter for LBs and Target Groups
NetworkServices:
- amazon-dns
Tags:
- Key: Name
Value: !Join
- ''
- - !Ref 'AWS::StackName'
- '-'
- Filter
- Key: Deployment
Value: Akto-CloudFormation
LambdaLogGroup:
Type: 'AWS::Logs::LogGroup'
Condition: CreateMirroringSession
Properties:
LogGroupName: !Sub '/aws/lambda/${CreateMirrorSession}'
RetentionInDays: 7
LambdaBasicExecutionRole:
Type: 'AWS::IAM::Role'
Condition: CreateMirroringSession
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: 'sts:AssumeRole'
Path: /
Policies:
- PolicyName: K8sCreateTrafficMirrorSession
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 'ec2:GetCallerIdentity'
- 'ec2:DescribeNetworkInterfaces'
- 'ec2:DescribeTrafficMirrorSessions'
- 'eks:DescribeCluster'
- 'sts:GetCallerIdentity'
- 'ec2:DescribeInstances'
- 'ec2:DescribeTrafficMirrorFilters'
- 'ec2:DeleteTrafficMirrorFilterRule'
- 'ec2:CreateTrafficMirrorFilterRule'
Resource: '*'
- Effect: Allow
Action:
- 'ec2:CreateTrafficMirrorSession'
Resource:
- 'arn:aws:ec2:*:*:traffic-mirror-session/*'
- 'arn:aws:ec2:*:*:network-interface/*'
- !Join
- ''
- - 'arn:aws:ec2:*:*:traffic-mirror-target/'
- !Ref TrafficMirrorTarget
- !Join
- ''
- - 'arn:aws:ec2:*:*:traffic-mirror-filter/*'
- Effect: Allow
Action:
- 'ec2:DeleteTrafficMirrorSession'
Resource:
- 'arn:aws:ec2:*:*:traffic-mirror-session/*'
LambdaLogPermissions:
Type: 'AWS::IAM::Policy'
Condition: CreateMirroringSession
Properties:
Roles:
- !Ref LambdaBasicExecutionRole
PolicyName: !Sub '${AWS::Region}-LambdaLogGroup'
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 'logs:CreateLogStream'
- 'logs:PutLogEvents'
Resource:
- !Sub >-
arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${CreateMirrorSession}
- !Sub >-
arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${CreateMirrorSession}:*
- !Sub >-
arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${CreateMirrorSession}:*:*
CustomSourceENIs:
Type: 'AWS::CloudFormation::CustomResource'
Condition: CreateMirroringSession
Properties:
ServiceToken: !GetAtt
- CreateMirrorSession
- Arn
SourceK8sClusterName: !Ref SourceK8sClusterName
SourceK8sNamespaces: !Ref SourceK8sNamespaces
MirrorTarget: !Ref TrafficMirrorTarget
MirrorFilter: !Ref K8sTrafficMirrorFilter
SampleSize: !Ref InstancesPerService
TargetENI: !Ref TargetENI
DependsOn: LambdaLogGroup
CreateMirrorSession:
Type: 'AWS::Lambda::Function'
Condition: CreateMirroringSession
Properties:
Runtime: python3.9
Timeout: 60
Role: !GetAtt
- LambdaBasicExecutionRole
- Arn
Handler: main.handler
Environment:
Variables:
K8S_CLUSTER_NAME: !Ref SourceK8sClusterName
K8S_NAMESPACES: !Join
- ','
- !Ref SourceK8sNamespaces
TRAFFIC_MIRROR_FILTER_ID: !Ref K8sTrafficMirrorFilter
TRAFFIC_MIRROR_TARGET_ID: !Ref TrafficMirrorTarget
TRAFFIC_MIRROR_SESSION_NUMBER: !Ref MirroringSessionNumber
SAMPLE_SIZE: !Ref InstancesPerService
TARGET_ENI: !Ref TargetENI
Code:
S3Bucket: !Sub 'akto-setup-${AWS::Region}'
S3Key: 'templates/k8s_mirroring_public_lambda.zip'
Description: Auto create mirroring configuration
TracingConfig:
Mode: Active
PeriodicEventRule:
Type: 'AWS::Events::Rule'
Condition: CreateMirroringSession
Properties:
Description: Generate an event periodically
Name: !Join
- ''
- - !Ref 'AWS::StackName'
- '-'
- PeriodicRule
ScheduleExpression: rate(15 minutes)
State: ENABLED
Targets:
- Arn: !GetAtt
- CreateMirrorSession
- Arn
Id: !Ref CreateMirrorSession
PeriodicEventPermission:
Type: 'AWS::Lambda::Permission'
Condition: CreateMirroringSession
Properties:
FunctionName: !GetAtt
- CreateMirrorSession
- Arn
Action: 'lambda:InvokeFunction'
Principal: events.amazonaws.com
SourceAccount: !Ref 'AWS::AccountId'
SourceArn: !GetAtt
- PeriodicEventRule
- Arn
LambdaVPCAccessRole:
Type: 'AWS::IAM::Role'
Condition: CreateMirroringSession
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: 'sts:AssumeRole'
Path: /
Policies:
- PolicyName: LambdaBasicAccessVPCPolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 'ec2:DescribeNetworkInterfaces'
- 'ec2:DescribeInstances'
- 'ec2:CreateNetworkInterface'
- 'ec2:DeleteNetworkInterface'
- 'ec2:AttachNetworkInterface'
Resource: '*'
SaveCollectionNames:
Type: 'AWS::Lambda::Function'
Condition: CreateMirroringSession
Properties:
Runtime: nodejs12.x
Timeout: 60
Role: !GetAtt
- LambdaVPCAccessRole
- Arn
Handler: nodejs/index.handler
VpcConfig:
SecurityGroupIds: !GetAtt
- CustomSourceENIs
- SecurityGroups
SubnetIds: !GetAtt
- CustomSourceENIs
- SubnetId
Environment:
Variables:
PRIVATE_IP: !GetAtt
- CustomSourceENIs
- kafkaIp
SUCCESS_ENIS: !GetAtt
- CustomSourceENIs
- successEnis
Code:
S3Bucket: !Sub 'akto-setup-${AWS::Region}'
S3Key: 'templates/mirroring-collections.zip'
Description: Send collection name to id mapping to Akto modules
TracingConfig:
Mode: Active
LambdaLogGroupVPC:
Type: 'AWS::Logs::LogGroup'
Condition: CreateMirroringSession
Properties:
LogGroupName: !Sub '/aws/lambda/${SaveCollectionNames}'
RetentionInDays: 7
LambdaLogPermissionsVPC:
Type: 'AWS::IAM::Policy'
Condition: CreateMirroringSession
Properties:
Roles:
- !Ref LambdaVPCAccessRole
PolicyName: !Sub '${AWS::Region}-LambdaLogGroup'
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 'logs:CreateLogStream'
- 'logs:PutLogEvents'
Resource:
- !Sub >-
arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${SaveCollectionNames}
- !Sub >-
arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${SaveCollectionNames}:*
- !Sub >-
arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${SaveCollectionNames}:*:*
PeriodicEventRuleVPC:
Type: 'AWS::Events::Rule'
Condition: CreateMirroringSession
Properties:
Description: Generate an event periodically
Name: !Join
- ''
- - !Ref 'AWS::StackName'
- '-'
- PeriodicRuleVPC
ScheduleExpression: rate(15 minutes)
State: ENABLED
Targets:
- Arn: !GetAtt
- SaveCollectionNames
- Arn
Id: !Ref SaveCollectionNames
PeriodicEventPermissionVPC:
Type: 'AWS::Lambda::Permission'
Condition: CreateMirroringSession
Properties:
FunctionName: !GetAtt
- SaveCollectionNames
- Arn
Action: 'lambda:InvokeFunction'
Principal: events.amazonaws.com
SourceAccount: !Ref 'AWS::AccountId'
SourceArn: !GetAtt
- PeriodicEventRuleVPC
- Arn
Outputs:
successEniList:
Description: Successfull ENI list
Condition: CreateMirroringSession
Value: !GetAtt
- CustomSourceENIs
- successEnis
failedEniList:
Description: Failed ENI list
Condition: CreateMirroringSession
Value: !GetAtt
- CustomSourceENIs
- failedEnis