-
Notifications
You must be signed in to change notification settings - Fork 102
/
aws2tf.py
executable file
·492 lines (398 loc) · 16 KB
/
aws2tf.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
484
485
486
487
488
489
490
491
492
#!/usr/bin/env python3
import boto3
import signal
import argparse
import glob
import os
import sys
import shutil
import datetime
import json
sys.path.insert(0, './.python')
#from get_aws_resources import aws_s3
import common
import resources
import globals
import stacks
from fixtf_aws_resources import aws_dict
def extra_help():
print("\nExtra help\n")
print("Type codes supported - ./aws2tf.py -t [type code]:\n")
with open('.python/resources.py', 'r') as f:
for line in f.readlines():
line3=""
if "#" in line: line3=line.split("#")[1].strip()
line=line.strip().split(":")[0]
if "type ==" in line and "aws_" not in line:
line=line.split("==")[-1].strip().strip("'").strip('"')
if line3 != "":
if len(line) < 3:
print("./aws2tf.py -t "+line+" \t\t\t"+str(line3))
elif len(line) > 10:
print("./aws2tf.py -t "+line+" \t"+str(line3))
else:
print("./aws2tf.py -t "+line+" \t\t"+str(line3))
else:
print("./aws2tf.py -t "+line)
print("\nOr instead of the above type codes use the terraform type eg:\n\n./aws2tf.py -t aws_vpc\n")
print("\nTo get a deployed stack set:\n\n./aws2tf.py -t stack -i stackname\n")
exit()
def build_lists():
print("Building core resource lists ...")
## vpcs
client = boto3.client('ec2')
response=[]
paginator = client.get_paginator('describe_vpcs')
for page in paginator.paginate(): response = response + page['Vpcs']
for j in response: globals.vpclist[j['VpcId']]=True
response=[]
paginator = client.get_paginator('describe_security_groups')
for page in paginator.paginate(): response = response + page['SecurityGroups']
for j in response: globals.sglist[j['GroupId']]=True
response=[]
paginator = client.get_paginator('describe_subnets')
for page in paginator.paginate(): response = response + page['Subnets']
for j in response: globals.subnetlist[j['SubnetId']]=True
response=[]
paginator = client.get_paginator('describe_transit_gateways')
for page in paginator.paginate(): response = response + page['TransitGateways']
for j in response: globals.tgwlist[j['TransitGatewayId']]=True
## roles
client = boto3.client('iam')
response=[]
paginator = client.get_paginator('list_roles')
for page in paginator.paginate(): response = response + page['Roles']
with open('imported/roles.json', 'w') as f: json.dump(response, f, indent=2,default=str)
for j in response: globals.rolelist[j['RoleName']]=True
response=[]
#if globals.debug: print(str(globals.vpclist))
#if __name__ == '__main__':
def main():
now = datetime.datetime.now()
print("aws2tf started at %s" % now)
common.check_python_version()
# print("cwd=%s" % os.getcwd())
signal.signal(signal.SIGINT, common.ctrl_c_handler)
argParser = argparse.ArgumentParser()
argParser.add_argument("-l", "--list",help="List extra help information" , action='store_true')
argParser.add_argument("-t", "--type", help="resource type aws_s3, ec2 aws_vpc etc")
argParser.add_argument("-i", "--id", help="resource id")
argParser.add_argument("-r", "--region", help="region")
argParser.add_argument("-m", "--merge", help="merge", action='store_true')
argParser.add_argument("-d", "--debug", help="debug", action='store_true')
argParser.add_argument("-v", "--validate", help="validate and exit", action='store_true')
argParser.add_argument("-a", "--accept", help="expected plan changes accepted", action='store_true')
argParser.add_argument("-e", "--exclude", help="resource types to exclude")
argParser.add_argument("-b3", "--boto3error", help="exit on boto3 api error (for debugging)", action='store_true')
argParser.add_argument("-la", "--serverless", help="Lambda mode - when running in a Lambda container", action='store_true')
args = argParser.parse_args()
type=""
path = shutil.which("terraform")
if path is None:
print("no executable found for command 'terraform'")
exit()
globals.expected=args.accept
# print("args=%s" % args)
# print("args.bucket=%s" % args.bucket)
# print("args.type=%s" % args.type)
# print("args.id=%s" % args.id)
globals.merge=args.merge
if args.list: extra_help()
if args.debug: globals.debug = True
if args.validate: globals.validate = True
if args.type is None or args.type=="":
if args.serverless:
print("type is required eg: -t aws_vpc when in serverless mode, exiting ....")
exit()
print("type is required eg: -t aws_vpc")
print("setting to all")
args.type = "all"
else:
type = args.type
#print("args.exclude="+str(args.exclude))
# build exclusion list
if args.exclude is not None:
extypes=args.exclude
if "," in extypes:
extypes = extypes.split(",")
else:
extypes = [extypes]
globals.all_extypes=[]
for i in extypes:
globals.all_extypes = globals.all_extypes + resources.resource_types(i)
else:
globals.all_extypes=[]
if args.region is None:
com = "aws configure get region"
rout = common.rc(com)
el = len(rout.stderr.decode().rstrip())
if el != 0:
print("region is required eg: -r eu-west-1 [using eu-west-1 as default]")
region = "eu-west-1"
else:
region = rout.stdout.decode().rstrip()
if len(region) == 0:
region=os.getenv("AWS_REGION")
if region is None:
region=os.getenv("AWS_DEFAULT_REGION")
if region is None:
print("region is required - set in AWS cli or pass with -r")
exit()
print("region set from aws cli / environment variables as "+region)
else:
region = args.region
globals.region = region
globals.regionl = len(region)
#os.environ["AWS"] = "aws --region "+region+" "
# get the current env and set directory
if globals.debug: print("setting session region="+region)
try:
my_session = boto3.setup_default_session(region_name=region)
except Exception as e:
print("AWS Authorization Error: "+str(e))
if globals.debug: print("getting account")
try:
globals.acc = boto3.client('sts').get_caller_identity().get('Account')
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
exn=str(exc_type.__name__)
if "ExpiredToken" in str(e):
print("STS Authorization Error: ExpiredToken, exiting .....")
exit()
print('Using region: '+region + ' account: ' + globals.acc+"\n")
#### restore form S3 if merging & serverless
globals.region = region
globals.regionl = len(region)
if args.serverless:
globals.serverless=True
globals.path1="/tmp/aws2tf/generated/tf-"+globals.acc+"-"+region
globals.path2=globals.path1+"/imported"
else:
globals.serverless=False
globals.path1="generated/tf-"+globals.acc+"-"+region
globals.path2=globals.path1+"/imported"
if globals.serverless:
if args.merge: common.download_from_s3()
else: common.empty_and_delete_bucket()
if globals.merge is False:
#print("No merge - removing terraform.tfstate* and aws_*.tf *.out")
#com = "rm -f aws.tf terraform.tfstate* data_*.tf aws_*.tf s3-*.tf aws_*.zip tfplan *.out *.log aws_*.sh stacks.sh import*.tf imported/* main.tf plan1* plan2* *.txt *.json *.err"
#rout = common.rc(com)
#com = "rm -rf imported notimported"
#rout = common.rc(com)
#com = "mkdir -p imported notimported"
#rout = common.rc(com)
com = "rm -rf "+globals.path1
rout = common.rc(com)
if globals.serverless: common.empty_and_delete_bucket()
com = "mkdir -p "+globals.path2
rout = common.rc(com)
globals.cwd=os.getcwd()
os.chdir(globals.path1)
common.aws_tf(region)
if args.merge:
print("Merging "+str(globals.merge))
#print("Merging capability disabled for now - exiting")
#exit()
try:
file = open('pyprocessed.txt', 'r')
while True:
line = file.readline()
if not line:
break
line = line.strip()
globals.rproc[line] = True
if globals.debug:
print("Pre Processed:")
for i in globals.rproc.keys():
print(i)
com = "cp imported/*.tf ."
rout = common.rc(com)
except:
print("No pyprocessed.txt found")
pass
id = args.id
#### setup
build_lists()
if type == "" or type is None: type = "all"
print("---<><> "+ str(type),"Id="+str(id)," exclude="+str(globals.all_extypes))
################# -- now we are calling ---- ###############################
if "," in type:
if "stack" in type:
print("Cannot mix stack with other types")
exit()
if id is not None:
print("Cannot pass id with multiple types")
exit()
#if globals.serverless:
# print("Cannot pass multiple types when running on serverless")
# exit()
types = type.split(",")
all_types = []
for type1 in types: all_types = all_types + resources.resource_types(type1)
for type2 in all_types:
if type2 in aws_dict.aws_resources:
if type2 in globals.all_extypes:
print("Excluding", type2)
continue
common.call_resource(type2, id)
else:
print("Resource",type2," not found in aws_dict")
else:
all_types = resources.resource_types(type)
try:
lall=len(all_types)
except:
lall=0
if all_types is None: print("No resources found all_types=None")
if type == "stack":
if id is None:
print("Must pass a stack name as a parameter -i <stack name>")
exit()
else:
globals.expected=True
stacks.get_stacks(id)
elif type.startswith("aws_"):
if type in aws_dict.aws_resources:
if type in globals.all_extypes:
print("Excluding", type)
else:
common.call_resource(type, id)
else:
print("Resource",type," not found in aws_dict")
elif all_types != None and lall > 1:
#print("len all_types="+str(len(all_types))) # testing only
#id="foobar" # testing only
ic=0
istart=0
it=len(all_types)
globals.expected=True
for i in all_types:
ic=ic+1
if ic > it: break
if ic < istart: continue
print(str(ic)+" of "+str(it) +"\t"+i)
if i in globals.all_extypes:
print("Excluding", i)
continue
common.call_resource(i, id)
else:
if all_types is not None:
for type in all_types:
if type in aws_dict.aws_resources:
if type in globals.all_extypes:
print("Excluding", type)
continue
common.call_resource(type,id)
else:
print("Resource",type," not found in aws_dict")
else:
print("No resources found")
exit()
#########################################################################################################################
## Known dependancies section
for ti in list(globals.rdep):
if not globals.rdep[ti]:
i = ti.split(".")[0]
id = ti.split(".")[1]
if id not in str(globals.policyarns):
if globals.debug: print("type="+i+" id="+str(id))
common.call_resource(i, id)
#else:
# print("No Known Dependancies")
common.tfplan1()
common.tfplan2()
if ":" in globals.rproc:
print(": in rproc exiting")
exit()
print("Detected Dependancies -----------------------")
detdep=False
for ti in globals.rproc.keys():
if not globals.rproc[ti]:
#print(str(ti)+":"+str(globals.rproc[ti]))
print(str(ti))
detdep=True
if not detdep:
print("No Detected Dependancies")
lc=0
olddetdepstr=""
detdepstr=""
while detdep:
for ti in list(globals.rproc):
if not globals.rproc[ti]:
i = ti.split(".")[0]
id = ti.split(".",1)[1]
if globals.debug: print("DD calling getresource with type="+i+" id="+str(id))
#print("----- DD ---- calling getresource with type="+i+" id="+str(id))
common.call_resource(i, id)
detdep=False
lc = lc + 1
# go again plan and split / fix
print("Terraform Plan - Dependancies Detection Loop "+str(lc)+".....")
x=glob.glob("import__aws_*.tf")
#print(str(x))
#td=""
for fil in x:
tf=fil.split('__',1)[1]
#td=td+" "+tf
com = "mv "+tf +" imported/"+tf
rout = common.rc(com)
common.tfplan1()
common.tfplan2()
detdepstr=""
for ti in globals.rproc.keys():
if not globals.rproc[ti]:
detdep=True
print(str(ti)+" is False")
detdepstr=detdepstr+str(ti)+" "
print("----------- Completed "+str(lc)+" dependancy check loops --------------")
if olddetdepstr == detdepstr and detdepstr != "":
print("\nERROR: No change/progress in dependancies exiting... \n")
for ti in globals.rproc.keys():
if not globals.rproc[ti]:
print("ERROR: Not found "+str(ti)+" - check if this resource still exists in AWS. Also check what resource is using it - grep the *.tf files in the generated/tf.* subdirectory")
exit()
olddetdepstr=detdepstr
common.tfplan3()
if globals.validate is False:
now = datetime.datetime.now()
print("aws2tf wrap up started at %s" % now)
common.wrapup()
else:
print("\nValidation only - no files written")
exit()
##########################################################################
####### Finish up
#########################################################################
with open("pyprocessed.txt", "a") as f:
for i in globals.rproc.keys():
if globals.debug: print(str(i))
f.write(i+"\n")
com = "sort -u pyprocessed.txt -o pyprocessed.txt"
rout = common.rc(com)
#### Trivy - if installed
#
#
path = shutil.which("trivy")
if path is not None:
x = glob.glob("aws_*__*.tf")
awsf=len(x)
if awsf < 256:
print("\nRunning trivy security check .....")
common.trivy_check()
else:
print("\nSkipping security check - too many files.")
print("Use trivy manually if required")
else:
print("trivy not installed, skipping security check")
print("Terraform files & state in sub-directory: "+ globals.path1)
if globals.serverless: common.upload_directory_to_s3()
x = glob.glob("*.err")
awsf=len(x)
if awsf > 0:
print("\nErrors found - see *.err files, and please report via github issue")
now = datetime.datetime.now()
print("aws2tf finished at %s" % now)
exit(0)
if __name__ == '__main__':
main()