/
sisec2.py
executable file
·483 lines (415 loc) · 16.6 KB
/
sisec2.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
import ConfigParser
import pymysql
import boto3
import time
import boto.vpc
from botocore.exceptions import ClientError
from ipaddress import IPv4Network
import pprint
import CloudFlare
import string
import random
config = ConfigParser.ConfigParser()
config.read("/etc/ansible/.rundeck.conf")
hostname = 'il-rundeck01'
musername = config.get("rundeck", "username")
password = config.get("rundeck", "password")
database = config.get("rundeck", "database")
cfuser = config.get('cloudflare', 'cfuser')
cfpassword = config.get('cloudflare', 'cfpassword')
try:
dbc = pymysql.connect(host=hostname, user=musername, passwd=password, db=database)
cur = dbc.cursor()
except pymysql.InternalError as error:
code, message = error.args
print ">>>>>>>>>>>>>", code, message
def getNameFromReg(region):
regions = {"ap-northeast-1":"Tokyo", "ca-central-1":"Canada", "us-west-1":"California", "us-west-2":"Oregon", "us-east-1":"Virginia", "us-east-2":"Ohio", "eu-central-1":"Frankfurt", "eu-west-1":"Ireland", "eu-west-2":"London", "eu-west-3":"Paris", "ap-southeast-1":"Singapore", "ap-southeast-2":"Sydney", "ap-south-1":"Mumbai", "sa-east-1":"Sao Paulo"}
return (regions[region])
def getAMI(client, ownerid):
amiid = "NA"
response = client.describe_images(Owners=[ownerid])
images = response['Images']
for image in images:
if (image['Description'] == 'ready_4_ansible_and_AD'):
amiid = image['ImageId']
return (amiid)
else:
continue
def createInstance(customer,ec2,client,amiid,itype,region,subnetid,sgid,disksize):
disksize = int(disksize)
bdm = [
{
'DeviceName': '/dev/sda1',
'Ebs': {
'DeleteOnTermination': True,
'VolumeSize': disksize,
'VolumeType': "gp2"
},
},
]
instance = ec2.create_instances(ImageId=amiid,MinCount=1,MaxCount=1,InstanceType=itype,BlockDeviceMappings=bdm,NetworkInterfaces=[{'SubnetId': subnetid, 'DeviceIndex': 0, 'AssociatePublicIpAddress': True, 'Groups': [sgid]}])
ec2.create_tags(Resources=[instance[0].id], Tags=[{'Key':'Name', 'Value': customer},{'Key':'Status','Value':'prod'}])
volumes = instance[0].volumes.all()
tags = [{'Key':'Name','Value': customer}]
for volume in volumes:
myvolume = ec2.Volume(volume.id)
ec2.create_tags(Resources=[myvolume.id], Tags=tags)
eip = client.allocate_address (Domain='vpc')
instance[0].wait_until_running();
client.associate_address(InstanceId = instance[0].id, AllocationId = eip["AllocationId"])
client.modify_instance_attribute(InstanceId=instance[0].id,Attribute="disableApiTermination", Value="True")
return (instance[0], eip["PublicIp"])
def createCF(customer,eip):
print "Create record:",customer
new_record = {
'type':"A",
'content':eip,
'name':customer,
}
zone_name= "sisense.com"
cf = CloudFlare.CloudFlare(email=cfuser,token=cfpassword)
zones = cf.zones.get(params = {'name':zone_name})
zone_id = zones[0]['id']
zone_name = zones[0]['name']
print zone_id, zone_name
dns_records = cf.zones.dns_records.get(zone_id,params={'name':customer + '.' + zone_name})
if dns_records:
print "already exists",customer
else:
try:
r = cf.zones.dns_records.post(zone_id, data=new_record)
print "created",r['id']
return (r['id'])
except Exception as e:
print e
return (False)
def createIG(subnet_name,region):
ec2 = boto3.resource('ec2',region_name=region)
ig = ec2.create_internet_gateway()
ig.create_tags (Tags = [ { 'Key': 'Name', 'Value': subnet_name }, ] )
return (ig)
def createVPC(region,ig,cidr_vpc,subnet_name,vpcname,ec2,client):
#ec2 = boto3.resource('ec2',region_name=region)
vpc = ec2.create_vpc(CidrBlock=cidr_vpc,AmazonProvidedIpv6CidrBlock=True)
vpc.create_tags(Tags = [ { 'Key': 'Name', 'Value': vpcname }, ])
vpc.wait_until_available()
vpc.attach_internet_gateway(InternetGatewayId=ig.id)
client.modify_vpc_attribute( VpcId = vpc.id , EnableDnsSupport = { 'Value': True } )
client.modify_vpc_attribute( VpcId = vpc.id , EnableDnsHostnames = { 'Value': True } )
return (vpc)
def createSubnet(vpc,cidr_ext,subnet_name,region,az):
ec2 = boto3.resource('ec2',region_name=region)
subnet = vpc.create_subnet( AvailabilityZone=str(region+az),CidrBlock=cidr_ext )
subnet.create_tags (Tags = [ { 'Key': 'Name', 'Value': subnet_name }, ] )
return (subnet)
def addSubnet(ec2,vpc_id,cidr_ext,subnet_name,region,az):
for vpc in ec2.vpcs.all():
#print vpc_id,vpc.id
if vpc.id == vpc_id:
subnet = vpc.create_subnet( AvailabilityZone=str(region+az),CidrBlock=cidr_ext )
print subnet
subnet.create_tags (Tags = [ { 'Key': 'Name', 'Value': subnet_name }, ] )
return (subnet)
def createSec(region,vpc,subnet_name):
ec2 = boto3.resource('ec2',region_name=region)
sg = ec2.create_security_group(GroupName=subnet_name,Description=subnet_name,VpcId=vpc.id)
return (sg)
def addSGRules(sg,source,startport,endport):
result = sg.authorize_ingress(CidrIp = source,IpProtocol='tcp',FromPort=int(startport),ToPort=int(endport))
return (result)
def addSGICMP (sg,source):
result = sg.authorize_ingress(CidrIp = source,IpProtocol='icmp',FromPort=0,ToPort=8)
return (result)
def addSGRulesEgress(sg,source,startport,endport,proto):
startport = int(startport)
endport = int(endport)
result = sg.authorize_egress(
IpPermissions=[
{
'IpProtocol': proto,
'FromPort': startport,
'ToPort': endport,
'IpRanges': [
{'CidrIp': source}
]
}
]
)
return (result)
def revokeSGRulesEgress(sg):
result = sg.revoke_egress(
IpPermissions=[
{
'IpProtocol': "-1",
'IpRanges': [
{'CidrIp': "0.0.0.0/0"}
]
}
]
)
result = sg.revoke_egress(
IpPermissions=[
{
'IpProtocol': "-1",
'Ipv6Ranges': [
{'CidrIpv6': "::/0"}
]
}
]
)
return (result)
def addRoute(vpc,ig,subnet,subnet_name,region):
client = boto3.client('ec2',region_name=region)
ec2 = boto3.resource('ec2',region_name=region)
pubRouteTable = ec2.create_route_table( VpcId = vpc.id )
pubRouteTable.associate_with_subnet( SubnetId = subnet.id)
pubRouteTable.create_tags (Tags = [ { 'Key': 'Name', 'Value': subnet_name }, ])
#print 'RT:',pubRouteTable.id
time.sleep(1)
intRoute = client.create_route ( RouteTableId = pubRouteTable.id , DestinationCidrBlock = '0.0.0.0/0' , GatewayId = ig.id )
return (pubRouteTable)
def getSerial(region):
query = "select max(serial) from VPC where region=%s"
try:
cur.execute( query,(region))
data = cur.fetchone()
if ( data[0] == None):
cidr_serial = 0
else:
cidr_serial = int(data[0])
except pymysql.InternalError as error:
code, message = error.args
print "MYSQL Error>>>>>>>>>>>>>", code, message
return(cidr_serial)
def updateSerial(customer,region,vpcid,subnetid,cidr):
serial = getSerial(region)
serial = int( int(serial) + 1)
print "new serial",serial
query = "insert ignore into `VPC` (`vpcid`,`region`,`subnetid`,`subnet`,`customer`,`serial`) values (%s,%s,%s,%s,%s,%s);"
try:
cur.execute( query,(vpcid,region,subnetid,cidr,customer,serial))
dbc.commit()
return(True)
except pymysql.InternalError as error:
code, message = error.args
print "MYSQL Error>>>>>>>>>>>>>", code, message
return(False)
def createBasicSG(sg):
print "Adding SG rules"
#RDP
addSGRules(sg,"31.154.168.226/32","3389","3389")
addSGRules(sg,"82.81.87.228/32","3389","3389")
addSGRules(sg,"194.105.144.15/32","3389","3389")
addSGRules(sg,"81.218.254.2/32","3389","3389")
addSGRules(sg,"98.0.131.178/32","3389","3389")
addSGRules(sg,"216.2.142.242/32","3389","3389")
addSGRules(sg,"216.0.197.218/32","3389","3389")
#WINRM
addSGRules(sg,"82.81.87.228/32","5985","5985")
addSGRules(sg,"82.81.87.228/32","5986","5986")
#FTP
addSGRules(sg,"82.81.87.228/32","990","990")
addSGRules(sg,"31.154.168.226/32","990","990")
addSGRules(sg,"81.218.254.2/32","990","990")
addSGRules(sg,"194.105.144.15/32","990","990")
addSGRules(sg,"98.0.131.178/32","990","990")
addSGRules(sg,"216.2.142.242/32","990","990")
addSGRules(sg,"216.0.197.218/32","990","990")
#FTP4000
addSGRules(sg,"82.81.87.228/32","4000","4100")
addSGRules(sg,"31.154.168.226/32","4000","4100")
addSGRules(sg,"81.218.254.2/32","4000","4100")
addSGRules(sg,"194.105.144.15/32","4000","4100")
addSGRules(sg,"98.0.131.178/32","4000","4100")
addSGRules(sg,"216.2.142.242/32","4000","4100")
addSGRules(sg,"216.0.197.218/32","4000","4100")
#web
addSGRules(sg,"82.81.87.228/32","8443","8444")
addSGRules(sg,"31.154.168.226/32","8443","8444")
addSGRules(sg,"81.218.254.2/32","8443","8444")
addSGRules(sg,"194.105.144.15/32","8443","8444")
addSGRules(sg,"98.0.131.178/32","8443","8444")
addSGRules(sg,"216.2.142.242/32","8443","8444")
addSGRules(sg,"216.0.197.218/32","8443","8444")
#https
addSGRules(sg,"0.0.0.0/0","443","443")
#outbound
revokeSGRulesEgress(sg)
addSGRulesEgress(sg,"0.0.0.0/0","443","443","tcp")
addSGRulesEgress(sg,"0.0.0.0/0","80","80","tcp")
addSGRulesEgress(sg,"54.75.238.143/32","8080","8080","tcp") #GEO
addSGRulesEgress(sg,"172.31.24.57/32","0","65535","tcp")#DC
addSGRulesEgress(sg,"172.31.24.57/32","0","65535","udp")#DC
addSGRulesEgress(sg,"172.31.9.105/32","0","65535","tcp")#DC
addSGRulesEgress(sg,"172.31.9.105/32","0","65535","udp")#DC
#nagios
addSGICMP(sg,"69.9.33.76/32") #ICMP
addSGRules(sg,"69.9.33.76/32","8443","8444") #nagios
addSGRules(sg,"69.9.33.76/32","5666","5666") #nagios
addSGRules(sg,"69.9.33.76/32","12489","12489") #nrpe
addSGRules(sg,"69.9.33.76/32","5985","5986") #winrm
def updateLogins(customer,logins):
query = "update `rundeck`.`instances` set `logins`= %s where CustomerName = %s;"
try:
cur.execute( query,(logins,customer))
dbc.commit()
return(True)
except pymysql.InternalError as error:
code, message = error.args
print "MYSQL Error>>>>>>>>>>>>>", code, message
return(False)
def peerToAD(vpcid,region,ownerid):
client = boto3.client('ec2',region_name=region)
dest_client = boto3.client('ec2',region_name="us-east-2")
connectionid = client.create_vpc_peering_connection(VpcId=vpcid,PeerVpcId=str('vpc-9ae11ff3'),PeerOwnerId=str(ownerid),PeerRegion="us-east-2",)
pcx = connectionid['VpcPeeringConnection']['VpcPeeringConnectionId']
ex = False
while (not ex):
try:
status = client.describe_vpc_peering_connections(VpcPeeringConnectionIds=[pcx])['VpcPeeringConnections'][0]['Status']['Message']
print pcx,"connection status",status
ex = True
except Exception as e:
print pcx,"Not ready"
continue
ex = False
while (not ex):
try:
status = dest_client.describe_vpc_peering_connections(VpcPeeringConnectionIds=[pcx])['VpcPeeringConnections'][0]['Status']['Message']
print "DEST:",pcx,"connection status",status
ex = True
except Exception as e:
print "cant find ",pcx
continue
connect = dest_client.accept_vpc_peering_connection(VpcPeeringConnectionId=pcx)
print "PCX Accept status:",connect['VpcPeeringConnection']['Status']
return (pcx)
def get_main_route_table(self):
"""Return the main (default) route table for VPC."""
main_route_table = []
for route_table in list(self.route_tables.all()):
for association in list(route_table.associations.all()):
if association.main == True:
main_route_table.append(route_table)
if len(main_route_table) != 1:
raise Exception('cannot get main route table! {}'.format(main_route_table))
return main_route_table[0]
def addPeerRoute (main_cidr,vpc,pcx,ec2,ig,subnet,cidr_vpc):
route_tables_with_main_association = ec2.route_tables.filter(Filters=[{'Name': 'association.main', 'Values': ["true"]},{'Name':'vpc-id','Values':[vpc.id]}])
for rt in route_tables_with_main_association:
rtx = rt.create_route(DestinationCidrBlock=main_cidr, VpcPeeringConnectionId=pcx)
rtd = rt.create_route(DestinationCidrBlock = '0.0.0.0/0' , GatewayId = ig.id)
rt.associate_with_subnet(SubnetId=subnet.id)
print "peer",rtx,"default",rtd
ec2 = boto3.resource('ec2',region_name="us-east-2")
destvpc = "vpc-9ae11ff3"
destrt = ec2.route_tables.filter(Filters=[{'Name':'vpc-id','Values':[destvpc]}])
for rt in destrt:
if rt.id == "rtb-15c8317c" :
print "adding to rtb-15c8317c"
destrtx = rt.create_route(DestinationCidrBlock=cidr_vpc, VpcPeeringConnectionId=pcx)
print destrtx
return (rt)
def updateDb ( name,awsregion,regionName,itype,ip,vpc,sg,id,amiid,subnet,updated,disksize):
print "updating DB with: region",regionName,"name :",name#,itype,ip,vpc,sg,id,amiid,subnet,disk,snaps,az,version,updated,sfurl
indb = checkInDb(name,regionName)
try:
dbc = pymysql.connect( host=hostname, user=musername, passwd=password, db=database )
cur=dbc.cursor()
except pymysql.InternalError as error:
code, message = error.args
print ">>>>>>>>>>>>>", code, message
if indb:
print name,"Already in DB"
try:
query = "update `rundeck`.`instances` set `AWSRegion` = %s,`RegionName` = %s,`InstanceType`= %s, `ElasticIP` = %s,`VPC` = %s,`SecurityGroup` = %s,`InstanceID`= %s,`updated`= %s, `Disks`=%s where CustomerName = %s;"
cur.execute( query,(awsregion,regionName,itype,ip,vpc,sg,id,updated,disksize,name))
dbc.commit()
except pymysql.InternalError as error:
code, message = error.args
print ">>>>>>>>>>>>>", code, message
if not indb:
print "Will insert in DB ",name
try:
query = "insert into `rundeck`.`instances` (`CustomerName`,`AWSRegion`,`RegionName`,`InstanceType`,`ElasticIP`,`VPC`,`SecurityGroup`,`InstanceID`,`AMIID`,`SubnetID`,`updated`,`Disks`) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
cur.execute (query,(name,awsregion,regionName,itype,ip,vpc,sg,id,amiid,subnet,updated,disksize))
dbc.commit()
except pymysql.InternalError as error:
code, message = error.args
print ">>>>>>>>>>>>>", code, message
def setAnsibleHosts(eip):
fout = open ('/rundeck/ansible/hosts','w')
print >> fout,"[win]"
print >> fout,eip
print >> fout,"[ec2server]"
print >> fout,eip
fout.close()
def pw_gen(size = 11, chars=string.ascii_letters + string.digits):
schars = "!@#-=*"
pw = ''.join(random.choice(chars) for _ in range(size))
pws = ''.join(random.choice(schars) for _ in range(1))
pwn = "".join(random.choice(string.digits) for _ in range(1))
pwd = str(pw+pwn+pws)
return (pwd)
def checkInDb(vmname,regionName):
#print "Checking if already in DB ",vmname," Region ",regionName
try:
dbc = pymysql.connect( host=hostname, user=musername, passwd=password, db=database )
cur=dbc.cursor()
except pymysql.InternalError as error:
code, message = error.args
print ">>>>>>>>>>>>>", code, message
try:
query = "select `CustomerName` from instances where CustomerName= %s and RegionName = %s"
cur.execute (query,[vmname,regionName])
rows = cur.fetchall()
if not rows :
print "Not In DB"
return (False)
else:
print "Already in DB"
return (True)
except pymysql.InternalError as error:
code, message = error.args
print (">>>>>>>>>>>>>", code, message)
def getIGfromVPC(client,vpc):
try:
response = client.describe_internet_gateways()
for igw in response['InternetGateways']:
if (igw['Attachments'][0]['VpcId'] == vpc):
return (igw['InternetGatewayId'])
except Exception as e:
print "failed to get IGWs,",e
return False
def getSubnetFromVPC (ec2,client,vpc):
filters = [{'Name':'tag:Name', 'Values':['*']}]
vpc_subnets = []
subnets = list(ec2.subnets.filter(Filters=filters))
for subnet in subnets:
response = client.describe_subnets(SubnetIds=[subnet.id])
if ( response['Subnets'][0]['VpcId'] == vpc ):
subnetaz = str(response['Subnets'][0]['AvailabilityZone'])[-1:]
subnetcidr = str(response['Subnets'][0]['CidrBlock'])
vpc_subnets.append([subnet.id,subnetaz,subnetcidr])
return (vpc_subnets)
def checkIfSubnetExists(vpc_subnets,az):
isFound = False
for vpc_subnet in vpc_subnets:
if (vpc_subnet[1] == az):
isFound = True
if (isFound):
return (True)
else:
print "no subnets found in AZ", az
def getSGsfromVPC(client,customer):
response = client.describe_security_groups()['SecurityGroups']
sgs = []
basicsg = str (customer+"-basic-security-group")
custsg = str (customer+"-security-group")
for sg in response:
if (sg['GroupName'] == basicsg):
sgs.insert(0,sg['GroupId'])
if (sg['GroupName'] == custsg):
sgs.insert(1,sg['GroupId'])
return (sgs)