forked from awslabs/aws-transit-vpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransit-vpc-push-cisco-config.py
360 lines (331 loc) · 19.7 KB
/
transit-vpc-push-cisco-config.py
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
350
351
352
353
354
355
356
357
358
359
360
######################################################################################################################
# Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. #
# #
# Licensed under the Amazon Software License (the "License"). You may not use this file except in compliance #
# with the License. A copy of the License is located at #
# #
# http://aws.amazon.com/asl/ #
# #
# or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES #
# OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions #
# and limitations under the License. #
######################################################################################################################
import boto3
from botocore.client import Config
import paramiko
from xml.dom import minidom
import ast
import time
import os
import string
import logging
log = logging.getLogger()
log.setLevel(logging.INFO)
config_file='transit_vpc_config.txt'
#These S3 endpoint URLs are provided to support VPC endpoints for S3 in regions such as Frankfort that require explicit region endpoint definition
endpoint_url = {
"us-east-1" : "https://s3.amazonaws.com",
"us-east-2" : "https://s3-us-east-2.amazonaws.com",
"us-west-1" : "https://s3-us-west-1.amazonaws.com",
"us-west-2" : "https://s3-us-west-2.amazonaws.com",
"eu-west-1" : "https://s3-eu-west-1.amazonaws.com",
"eu-central-1" : "https://s3-eu-central-1.amazonaws.com",
"ap-northeast-1" : "https://s3-ap-northeast-1.amazonaws.com",
"ap-northeast-2" : "https://s3-ap-northeast-2.amazonaws.com",
"ap-south-1" : "https://s3-ap-south-1.amazonaws.com",
"ap-southeast-1" : "https://s3-ap-southeast-1.amazonaws.com",
"ap-southeast-2" : "https://s3-ap-southeast-2.amazonaws.com",
"sa-east-1" : "https://s3-sa-east-1.amazonaws.com"
}
#Logic to determine when the prompt has been discovered
def prompt(chan):
buff = ''
while not buff.endswith('#'):
resp = chan.recv(9999)
buff += resp
#log.debug("%s",resp)
return buff
# Logic to figure out the next availble tunnel
def getNextTunnelId(ssh):
log.debug('Start getNextTunnelId')
ssh.send('term len 0\n')
log.debug("%s",prompt(ssh))
ssh.send('config t\n')
log.debug("%s",prompt(ssh))
ssh.send('do show int summary | include Tunnel\n')
output = prompt(ssh)
log.debug("%s",output)
ssh.send('exit\n')
log.debug("%s",prompt(ssh))
lastTunnelNum=''
for line in output.split('\n'):
line=line.replace('* Tunnel','Tunnel')
log.debug("%s",line)
if line.strip()[:6] == 'Tunnel':
lastTunnelNum = line.strip().partition(' ')[0].replace('Tunnel','')
if lastTunnelNum == '':
return 1
return int(lastTunnelNum) + 1
# Logic to figure out existing tunnel IDs
def getExistingTunnelId(ssh,vpn_connection_id):
log.debug('Start getExistingTunnelId')
ssh.send('term len 0\n')
log.debug("%s",prompt(ssh))
#ssh.send('config t\n')
#log.debug("%s",prompt(ssh))
#Display keyrings so we can derive tunnelId
ssh.send('show run | include crypto keyring\n')
output = prompt(ssh)
log.debug("%s",output)
tunnelNum=0
#Now parse crypto keyring lines for keyring-vpn-connection_id-tunnelId
for line in output.split('\n'):
if vpn_connection_id in line:
tmpNum = int(line.split('-')[-1])
if tunnelNum < tmpNum:
tunnelNum = tmpNum
if tunnelNum == 0:
log.error('Unable to find existing tunnels for %s', vpn_connection_id)
return 0
#Parsing logic gets the greater of the two tunnel numbers, so return tunnelNum -1 to get the first tunnel number
return tunnelNum-1
#Generic logic to push pre-generated Cisco config to the router
def pushConfig(ssh,config):
#log.info("Starting to push config")
#ssh.send('term len 0\n')
#prompt(ssh)
ssh.send('config t\n')
log.debug("%s",prompt(ssh))
stime = time.time()
for line in config:
if line == "WAIT":
log.debug("Waiting 30 seconds...")
time.sleep(30)
else:
ssh.send(line+'\n')
log.debug("%s",prompt(ssh))
ssh.send('exit\n')
log.debug("%s",prompt(ssh))
log.debug(" --- %s seconds ---", (time.time() - stime))
log.info("Saving config!")
ssh.send('copy run start\n\n\n\n\n')
log.info("%s",prompt(ssh))
log.info("Update complete!")
#Logic to determine the bucket prefix from the S3 key name that was provided
def getBucketPrefix(bucket_name, bucket_key):
#Figure out prefix from known bucket_name and bucket_key
bucket_prefix = '/'.join(bucket_key.split('/')[:-2])
if len(bucket_prefix) > 0:
bucket_prefix += '/'
return bucket_prefix
#Logic to download the transit VPC configuration file from S3
def getTransitConfig(bucket_name, bucket_prefix, s3_url, config_file):
s3=boto3.client('s3', endpoint_url=s3_url,
config=Config(s3={'addressing_style': 'virtual'}, signature_version='s3v4'))
log.info("Downloading config file: %s/%s/%s%s", s3_url, bucket_name, bucket_prefix,config_file)
return ast.literal_eval(s3.get_object(Bucket=bucket_name,Key=bucket_prefix+config_file)['Body'].read())
#Logic to upload a new/updated transit VPC configuration file to S3 (not currently used)
def putTransitConfig(bucket_name, bucket_prefix, s3_url, config_file, config):
s3=boto3.client('s3', endpoint_url=s3_url,
config=Config(s3={'addressing_style': 'virtual'}, signature_version='s3v4'))
log.info("Uploading new config file: %s/%s/%s%s", s3_url,bucket_name, bucket_prefix,config_file)
s3.put_object(Bucket=bucket_name,Key=bucket_prefix+config_file,Body=str(config))
#Logic to download the SSH private key from S3 to be used for SSH public key authentication
def downloadPrivateKey(bucket_name, bucket_prefix, s3_url, prikey):
if os.path.exists('/tmp/'+prikey):
os.remove('/tmp/'+prikey)
s3=boto3.client('s3', endpoint_url=s3_url,
config=Config(s3={'addressing_style': 'virtual'}, signature_version='s3v4'))
log.info("Downloading private key: %s/%s/%s%s",s3_url, bucket_name, bucket_prefix, prikey)
s3.download_file(bucket_name,bucket_prefix+prikey, '/tmp/'+prikey)
#Logic to create the appropriate Cisco configuration
def create_cisco_config(bucket_name, bucket_key, s3_url, bgp_asn, ssh):
log.info("Processing %s/%s", bucket_name, bucket_key)
#Download the VPN configuration XML document
s3=boto3.client('s3',endpoint_url=s3_url,
config=Config(s3={'addressing_style': 'virtual'}, signature_version='s3v4'))
config=s3.get_object(Bucket=bucket_name,Key=bucket_key)
xmldoc=minidom.parseString(config['Body'].read())
#Extract transit_vpc_configuration values
vpn_config = xmldoc.getElementsByTagName("transit_vpc_config")[0]
account_id = vpn_config.getElementsByTagName("account_id")[0].firstChild.data
vpn_endpoint = vpn_config.getElementsByTagName("vpn_endpoint")[0].firstChild.data
vpn_status = vpn_config.getElementsByTagName("status")[0].firstChild.data
preferred_path = vpn_config.getElementsByTagName("preferred_path")[0].firstChild.data
#Extract VPN connection information
vpn_connection=xmldoc.getElementsByTagName('vpn_connection')[0]
vpn_connection_id=vpn_connection.attributes['id'].value
customer_gateway_id=vpn_connection.getElementsByTagName("customer_gateway_id")[0].firstChild.data
vpn_gateway_id=vpn_connection.getElementsByTagName("vpn_gateway_id")[0].firstChild.data
vpn_connection_type=vpn_connection.getElementsByTagName("vpn_connection_type")[0].firstChild.data
#Determine the VPN tunnels to work with
if vpn_status == 'create':
tunnelId=getNextTunnelId(ssh)
else:
tunnelId=getExistingTunnelId(ssh,vpn_connection_id)
if tunnelId == 0:
return
log.info("%s %s with tunnel #%s and #%s.",vpn_status, vpn_connection_id, tunnelId, tunnelId+1)
# Create or delete the VRF for this connection
if vpn_status == 'delete':
ipsec_tunnel = vpn_connection.getElementsByTagName("ipsec_tunnel")[0]
customer_gateway=ipsec_tunnel.getElementsByTagName("customer_gateway")[0]
customer_gateway_bgp_asn=customer_gateway.getElementsByTagName("bgp")[0].getElementsByTagName("asn")[0].firstChild.data
#Remove VPN configuration for both tunnels
config_text = ['router bgp {}'.format(customer_gateway_bgp_asn)]
config_text.append(' no address-family ipv4 vrf {}'.format(vpn_connection_id))
config_text.append('exit')
config_text.append('no ip vrf {}'.format(vpn_connection_id))
config_text.append('interface Tunnel{}'.format(tunnelId))
config_text.append(' shutdown')
config_text.append('exit')
config_text.append('no interface Tunnel{}'.format(tunnelId))
config_text.append('interface Tunnel{}'.format(tunnelId+1))
config_text.append(' shutdown')
config_text.append('exit')
config_text.append('no interface Tunnel{}'.format(tunnelId+1))
config_text.append('no route-map rm-{} permit'.format(vpn_connection_id))
# Cisco requires waiting 60 seconds before removing the isakmp profile
config_text.append('WAIT')
config_text.append('WAIT')
config_text.append('no crypto isakmp profile isakmp-{}-{}'.format(vpn_connection_id,tunnelId))
config_text.append('no crypto isakmp profile isakmp-{}-{}'.format(vpn_connection_id,tunnelId+1))
config_text.append('no crypto keyring keyring-{}-{}'.format(vpn_connection_id,tunnelId))
config_text.append('no crypto keyring keyring-{}-{}'.format(vpn_connection_id,tunnelId+1))
else:
# Create global tunnel configuration
config_text = ['ip vrf {}'.format(vpn_connection_id)]
config_text.append(' rd {}:{}'.format(bgp_asn, tunnelId))
config_text.append(' route-target export {}:0'.format(bgp_asn))
config_text.append(' route-target import {}:0'.format(bgp_asn))
config_text.append('exit')
# Check to see if a route map is needed for creating a preferred path
if preferred_path != 'none':
config_text.append('route-map rm-{} permit'.format(vpn_connection_id))
# If the preferred path is this transit VPC vpn endpoint, then set a shorter as-path prepend than if it is not
if preferred_path == vpn_endpoint:
config_text.append(' set as-path prepend {}'.format(bgp_asn))
else:
config_text.append(' set as-path prepend {} {}'.format(bgp_asn, bgp_asn))
config_text.append('exit')
# Create tunnel specific configuration
for ipsec_tunnel in vpn_connection.getElementsByTagName("ipsec_tunnel"):
customer_gateway=ipsec_tunnel.getElementsByTagName("customer_gateway")[0]
customer_gateway_tunnel_outside_address=customer_gateway.getElementsByTagName("tunnel_outside_address")[0].getElementsByTagName("ip_address")[0].firstChild.data
customer_gateway_tunnel_inside_address_ip_address=customer_gateway.getElementsByTagName("tunnel_inside_address")[0].getElementsByTagName("ip_address")[0].firstChild.data
customer_gateway_tunnel_inside_address_network_mask=customer_gateway.getElementsByTagName("tunnel_inside_address")[0].getElementsByTagName("network_mask")[0].firstChild.data
customer_gateway_tunnel_inside_address_network_cidr=customer_gateway.getElementsByTagName("tunnel_inside_address")[0].getElementsByTagName("network_cidr")[0].firstChild.data
customer_gateway_bgp_asn=customer_gateway.getElementsByTagName("bgp")[0].getElementsByTagName("asn")[0].firstChild.data
customer_gateway_bgp_hold_time=customer_gateway.getElementsByTagName("bgp")[0].getElementsByTagName("hold_time")[0].firstChild.data
vpn_gateway=ipsec_tunnel.getElementsByTagName("vpn_gateway")[0]
vpn_gateway_tunnel_outside_address=vpn_gateway.getElementsByTagName("tunnel_outside_address")[0].getElementsByTagName("ip_address")[0].firstChild.data
vpn_gateway_tunnel_inside_address_ip_address=vpn_gateway.getElementsByTagName("tunnel_inside_address")[0].getElementsByTagName("ip_address")[0].firstChild.data
vpn_gateway_tunnel_inside_address_network_mask=vpn_gateway.getElementsByTagName("tunnel_inside_address")[0].getElementsByTagName("network_mask")[0].firstChild.data
vpn_gateway_tunnel_inside_address_network_cidr=vpn_gateway.getElementsByTagName("tunnel_inside_address")[0].getElementsByTagName("network_cidr")[0].firstChild.data
vpn_gateway_bgp_asn=vpn_gateway.getElementsByTagName("bgp")[0].getElementsByTagName("asn")[0].firstChild.data
vpn_gateway_bgp_hold_time=vpn_gateway.getElementsByTagName("bgp")[0].getElementsByTagName("hold_time")[0].firstChild.data
ike=ipsec_tunnel.getElementsByTagName("ike")[0]
ike_authentication_protocol=ike.getElementsByTagName("authentication_protocol")[0].firstChild.data
ike_encryption_protocol=ike.getElementsByTagName("encryption_protocol")[0].firstChild.data
ike_lifetime=ike.getElementsByTagName("lifetime")[0].firstChild.data
ike_perfect_forward_secrecy=ike.getElementsByTagName("perfect_forward_secrecy")[0].firstChild.data
ike_mode=ike.getElementsByTagName("mode")[0].firstChild.data
ike_pre_shared_key=ike.getElementsByTagName("pre_shared_key")[0].firstChild.data
ipsec=ipsec_tunnel.getElementsByTagName("ipsec")[0]
ipsec_protocol=ipsec.getElementsByTagName("protocol")[0].firstChild.data
ipsec_authentication_protocol=ipsec.getElementsByTagName("authentication_protocol")[0].firstChild.data
ipsec_encryption_protocol=ipsec.getElementsByTagName("encryption_protocol")[0].firstChild.data
ipsec_lifetime=ipsec.getElementsByTagName("lifetime")[0].firstChild.data
ipsec_perfect_forward_secrecy=ipsec.getElementsByTagName("perfect_forward_secrecy")[0].firstChild.data
ipsec_mode=ipsec.getElementsByTagName("mode")[0].firstChild.data
ipsec_clear_df_bit=ipsec.getElementsByTagName("clear_df_bit")[0].firstChild.data
ipsec_fragmentation_before_encryption=ipsec.getElementsByTagName("fragmentation_before_encryption")[0].firstChild.data
ipsec_tcp_mss_adjustment=ipsec.getElementsByTagName("tcp_mss_adjustment")[0].firstChild.data
ipsec_dead_peer_detection_interval=ipsec.getElementsByTagName("dead_peer_detection")[0].getElementsByTagName("interval")[0].firstChild.data
ipsec_dead_peer_detection_retries=ipsec.getElementsByTagName("dead_peer_detection")[0].getElementsByTagName("retries")[0].firstChild.data
config_text.append('crypto keyring keyring-{}-{}'.format(vpn_connection_id,tunnelId))
config_text.append(' local-address GigabitEthernet1')
config_text.append(' pre-shared-key address {} key {}'.format(vpn_gateway_tunnel_outside_address, ike_pre_shared_key))
config_text.append('exit')
config_text.append('crypto isakmp profile isakmp-{}-{}'.format(vpn_connection_id,tunnelId))
config_text.append(' local-address GigabitEthernet1')
config_text.append(' match identity address {}'.format(vpn_gateway_tunnel_outside_address))
config_text.append(' keyring keyring-{}-{}'.format(vpn_connection_id,tunnelId))
config_text.append('exit')
config_text.append('interface Tunnel{}'.format(tunnelId))
config_text.append(' ip vrf forwarding {}'.format(vpn_connection_id))
config_text.append(' ip address {} 255.255.255.252'.format(customer_gateway_tunnel_inside_address_ip_address))
config_text.append(' ip virtual-reassembly')
config_text.append(' tunnel source GigabitEthernet1')
config_text.append(' tunnel destination {} '.format(vpn_gateway_tunnel_outside_address))
config_text.append(' tunnel mode ipsec ipv4')
config_text.append(' tunnel protection ipsec profile ipsec-vpn-aws')
config_text.append(' ip tcp adjust-mss 1387')
config_text.append(' no shutdown')
config_text.append('exit')
config_text.append('router bgp {}'.format(customer_gateway_bgp_asn))
config_text.append(' address-family ipv4 vrf {}'.format(vpn_connection_id))
config_text.append(' neighbor {} remote-as {}'.format(vpn_gateway_tunnel_inside_address_ip_address, vpn_gateway_bgp_asn))
if preferred_path != 'none':
config_text.append(' neighbor {} route-map rm-{} out'.format(vpn_gateway_tunnel_inside_address_ip_address, vpn_connection_id))
config_text.append(' neighbor {} timers 10 30 30'.format(vpn_gateway_tunnel_inside_address_ip_address))
config_text.append(' neighbor {} activate'.format(vpn_gateway_tunnel_inside_address_ip_address))
config_text.append(' neighbor {} as-override'.format(vpn_gateway_tunnel_inside_address_ip_address))
config_text.append(' neighbor {} soft-reconfiguration inbound'.format(vpn_gateway_tunnel_inside_address_ip_address))
config_text.append('exit')
config_text.append('exit')
#Increment tunnel ID for going onto the next tunnel
tunnelId+=1
log.debug("Conversion complete")
return config_text
def lambda_handler(event, context):
record=event['Records'][0]
bucket_name=record['s3']['bucket']['name']
bucket_key=record['s3']['object']['key']
bucket_region=record['awsRegion']
bucket_prefix=getBucketPrefix(bucket_name, bucket_key)
log.debug("Getting config")
stime = time.time()
config = getTransitConfig(bucket_name, bucket_prefix, endpoint_url[bucket_region], config_file)
if 'CSR1' in bucket_key:
csr_ip=config['PIP1']
csr_name='CSR1'
else:
csr_ip=config['PIP2']
csr_name='CSR2'
log.info("--- %s seconds ---", (time.time() - stime))
#Download private key file from secure S3 bucket
downloadPrivateKey(bucket_name, bucket_prefix, endpoint_url[bucket_region], config['PRIVATE_KEY'])
log.debug("Reading downloaded private key into memory.")
k = paramiko.RSAKey.from_private_key_file("/tmp/"+config['PRIVATE_KEY'])
#Delete the temp copy of the private key
os.remove("/tmp/"+config['PRIVATE_KEY'])
log.debug("Deleted downloaded private key.")
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
log.info("Connecting to %s (%s)", csr_name, csr_ip)
stime = time.time()
try:
c.connect( hostname = csr_ip, username = config['USER_NAME'], pkey = k )
PubKeyAuth=True
except paramiko.ssh_exception.AuthenticationException:
log.error("PubKey Authentication Failed! Connecting with password")
c.connect( hostname = csr_ip, username = config['USER_NAME'], password = config['PASSWORD'] )
PubKeyAuth=False
log.info("--- %s seconds ---", (time.time() - stime))
log.debug("Connected to %s",csr_ip)
ssh = c.invoke_shell()
log.debug("%s",prompt(ssh))
log.debug("Creating config.")
stime = time.time()
csr_config = create_cisco_config(bucket_name, bucket_key, endpoint_url[bucket_region], config['BGP_ASN'], ssh)
log.info("--- %s seconds ---", (time.time() - stime))
log.info("Pushing config to router.")
stime = time.time()
pushConfig(ssh,csr_config)
log.info("--- %s seconds ---", (time.time() - stime))
ssh.close()
return
{
'message' : "Script execution completed. See Cloudwatch logs for complete output"
}