1+ import json
2+ import boto3
3+ import os
4+ import time
5+
6+ ram = boto3 .client ('ram' )
7+ ec2 = boto3 .client ('ec2' )
8+
9+ blstsec_account = os .environ ['BLSTSECURITY_ACCOUNT' ]
10+ blstsec_destination = os .environ ['BLSTSECURITY_ROUTE_DESTINATION' ]
11+ source_instance_id = os .environ ['SOURCE_INSTANCE_ID' ]
12+
13+ def get_instance_data ():
14+ instance_data = ec2 .describe_instances (
15+ InstanceIds = [
16+ source_instance_id
17+ ]
18+ )['Reservations' ][0 ]['Instances' ][0 ]['NetworkInterfaces' ][0 ]
19+ source_cidr = ec2 .describe_vpcs (
20+ VpcIds = [
21+ instance_data ['VpcId' ]
22+ ]
23+ )['Vpcs' ][0 ]['CidrBlock' ]
24+ print ('Your CIDR Block is: ' + source_cidr )
25+ route_table_id = ''
26+ try :
27+ route_table_id = ec2 .describe_route_tables (
28+ Filters = [
29+ {
30+ 'Name' : 'association.subnet-id' ,
31+ 'Values' : [
32+ instance_data ['SubnetId' ]
33+ ]
34+ }
35+ ]
36+ )['RouteTables' ][0 ]['RouteTableId' ]
37+ except Exception as e :
38+ route_tables_data = ec2 .describe_route_tables (
39+ Filters = [
40+ {
41+ 'Name' : 'vpc-id' ,
42+ 'Values' : [
43+ instance_data ['VpcId' ]
44+ ]
45+ }
46+ ]
47+ )['RouteTables' ]
48+ is_main = False
49+ if route_tables_data [0 ]['Associations' ][0 ]['Main' ]:
50+ route_table_id = route_tables_data [0 ]['Associations' ][0 ]['RouteTableId' ]
51+ is_main = True
52+ route_tables_index = 1
53+ while not is_main and len (route_tables_data ) > route_tables_index :
54+ if route_tables_data [route_tables_index ]['Associations' ][0 ]['Main' ]:
55+ route_table_id = route_tables_data [route_tables_index ]['Associations' ][0 ]['RouteTableId' ]
56+ is_main = True
57+ if not is_main :
58+ print ('There was an error with finding your route table id, please contact blstsecurity' )
59+ return {'vpc_id' :instance_data ['VpcId' ], 'subnet_id' :instance_data ['SubnetId' ], 'network_interface_id' :instance_data ['NetworkInterfaceId' ], 'cidr' :source_cidr , 'route_table_id' :route_table_id }
60+
61+ def get_resource_share_list ():
62+ shared_resources_inv = ram .get_resource_share_invitations ()
63+ shared_resources_list = []
64+ for resource in shared_resources_inv ['resourceShareInvitations' ]:
65+ if resource ['senderAccountId' ] == blstsec_account :
66+ if resource ['status' ] != 'ACCEPTED' :
67+ ram .accept_resource_share_invitation (
68+ resourceShareInvitationArn = resource ['resourceShareInvitationArn' ]
69+ )
70+ shared_resources_list .append (resource ['resourceShareArn' ])
71+ return shared_resources_list
72+
73+ def get_resource_share_ids (shared_resources_list ):
74+ shared_resources_ids_list = {}
75+ shared_resources_ram_list = []
76+ if not len (shared_resources_list ):
77+ return shared_resources_ram_list
78+ while len (shared_resources_ram_list ) != 2 :
79+ shared_resources_ram_list = ram .list_resources (
80+ resourceOwner = 'OTHER-ACCOUNTS' ,
81+ resourceShareArns = shared_resources_list
82+ )['resources' ]
83+ time .sleep (0.05 )
84+ for resource in shared_resources_ram_list :
85+ if resource ['type' ] == 'ec2:TransitGateway' :
86+ shared_resources_ids_list ['tgw' ] = resource ['arn' ].split ('/' )[1 ]
87+ elif resource ['type' ] == 'ec2:TrafficMirrorTarget' :
88+ shared_resources_ids_list ['tmt' ] = resource ['arn' ].split ('/' )[1 ]
89+ return shared_resources_ids_list
90+
91+ def create_transit_gateway_attachment (tgw_id , source_vpc_id , source_subnet_id ):
92+ tgwa = ''
93+ try :
94+ tgwa = ec2 .create_transit_gateway_vpc_attachment (
95+ TransitGatewayId = tgw_id ,
96+ VpcId = source_vpc_id ,
97+ SubnetIds = [source_subnet_id ]
98+ )['TransitGatewayVpcAttachment' ]['TransitGatewayAttachmentId' ]
99+ except Exception as e :
100+ tgwa = e
101+
102+ def create_traffic_mirror_filter_rule (tmf_id , direction , number ):
103+ tmf_rule_id = ''
104+ while tmf_rule_id == '' :
105+ try :
106+ tmf_rule_id = ec2 .create_traffic_mirror_filter_rule (
107+ TrafficMirrorFilterId = tmf_id ,
108+ TrafficDirection = direction ,
109+ RuleNumber = number ,
110+ RuleAction = 'accept' ,
111+ Protocol = 6 ,
112+ DestinationCidrBlock = '0.0.0.0/0' ,
113+ SourceCidrBlock = '0.0.0.0/0'
114+ )['TrafficMirrorFilterRule' ]['TrafficMirrorFilterRuleId' ]
115+ except Exception as e :
116+ print (e )
117+ time .sleep (0.05 )
118+
119+ def create_traffic_mirror_filter ():
120+ traffic_mirror_filters = ec2 .describe_traffic_mirror_filters (
121+ Filters = [
122+ {
123+ 'Name' : 'description' ,
124+ 'Values' : [
125+ 'blstsecurity traffic mirror filter'
126+ ]
127+ },
128+ ]
129+ )['TrafficMirrorFilters' ]
130+ if len (traffic_mirror_filters ):
131+ return ''
132+
133+ tmf_id = ec2 .create_traffic_mirror_filter (
134+ Description = 'blstsecurity traffic mirror filter'
135+ )['TrafficMirrorFilter' ]['TrafficMirrorFilterId' ]
136+ create_traffic_mirror_filter_rule (tmf_id , 'ingress' , 100 )
137+ create_traffic_mirror_filter_rule (tmf_id , 'egress' , 100 )
138+ return tmf_id
139+
140+ def create_traffic_mirror_session (source_network_interface_id , tmt_id , tmf_id ):
141+ if tmf_id == '' :
142+ return
143+ tms = ec2 .create_traffic_mirror_session (
144+ NetworkInterfaceId = source_network_interface_id ,
145+ TrafficMirrorTargetId = tmt_id ,
146+ TrafficMirrorFilterId = tmf_id ,
147+ SessionNumber = 1 ,
148+ Description = 'blstsecurity traffic mirror session'
149+ )
150+
151+ def add_tgw_route_table (route_table_id , tgw_id ):
152+ try :
153+ ec2 .create_route (
154+ DestinationCidrBlock = blstsec_destination ,
155+ TransitGatewayId = tgw_id ,
156+ RouteTableId = route_table_id
157+ )
158+ return 'traffic mirroring integration installed successfully'
159+ except :
160+ return 'Wait until blstsecurity will accept the transit gateway attachment and then run the lambda again'
161+
162+ def lambda_handler (event , context ):
163+ source_instance_data = get_instance_data ()
164+ shared_resources_ids = get_resource_share_ids (get_resource_share_list ())
165+ if len (shared_resources_ids ):
166+ create_transit_gateway_attachment (shared_resources_ids ['tgw' ], source_instance_data ['vpc_id' ] , source_instance_data ['subnet_id' ])
167+ create_traffic_mirror_session (source_instance_data ['network_interface_id' ], shared_resources_ids ['tmt' ], create_traffic_mirror_filter ())
168+ return add_tgw_route_table (source_instance_data ['route_table_id' ], shared_resources_ids ['tgw' ])
169+ else :
170+ return 'Shared resources not found, please contact blstsecurity for more information'
0 commit comments