-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathorgtool.py
1113 lines (1052 loc) · 61.1 KB
/
orgtool.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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import boto3
import json
import logging
import sys
import getopt
from graphviz import Digraph
import os
from tqdm import tqdm
FORMAT = '%(asctime)-15s %(message)s'
logging.basicConfig(format=FORMAT, level=logging.INFO, filename='orgtool.log')
logger = logging.getLogger('oustructure')
def visualize_organization_diagrams(file, org):
logger.info(f'Import Json file: {file}')
f = open(file, )
data = json.load(f)
csvfile = 'organizations.csv'
if os.path.exists(csvfile):
print("Remove old CSV File.")
os.remove(csvfile)
csvfile = open(csvfile, "w")
csv = """##
## Example CSV import. Use ## for comments and # for configuration. Paste CSV below.
## The following names are reserved and should not be used (or ignored):
## id, tooltip, placeholder(s), link and label (see below)
##
#
#
# label: %ou%<br><i style="color:black;fontSize=8"> %scps% </i><br>
## Node style (placeholders are replaced once).
## Default is the current style for nodes.
#
# style: label;image=%image%;whiteSpace=wrap;html=1;rounded=1;fillColor=#fff;strokeColor=#545B64;
#
## Parent style for nodes with child nodes (placeholders are replaced once).
#
# parentstyle: swimlane;whiteSpace=wrap;html=1;childLayout=stackLayout;horizontal=1;horizontalStack=0;resizeParent=1;resizeLast=0;collapsible=1;
#
## Optional column name that contains a reference to a named style in styles.
## Default is the current style for nodes.
#
# stylename: -
#
## JSON for named styles of the form {"name": "style", "name": "style"} where style is a cell style with
## placeholders that are replaced once.
#
# styles: -
#
## JSON for variables in styles of the form {"name": "value", "name": "value"} where name is a string
## that will replace a placeholder in a style.
#
# vars: -
#
## Optional column name that contains a reference to a named label in labels.
## Default is the current label.
#
# labelname: -
#
## JSON for named labels of the form {"name": "label", "name": "label"} where label is a cell label with
## placeholders.
#
# labels: -
#
## Uses the given column name as the identity for cells (updates existing cells).
## Default is no identity (empty value or -).
#
# identity: -
#
## Uses the given column name as the parent reference for cells. Default is no parent (empty or -).
## The identity above is used for resolving the reference so it must be specified.
#
# parent: -
#
## Adds a prefix to the identity of cells to make sure they do not collide with existing cells (whose
## IDs are numbers from 0..n, sometimes with a GUID prefix in the context of realtime collaboration).
## Default is csvimport-.
#
# namespace: csvimport-
#
## Connections between rows ("from": source colum, "to": target column).
## Label, style and invert are optional. Defaults are '', current style and false.
## If placeholders are used in the style, they are replaced with data from the source.
## An optional placeholders can be set to target to use data from the target instead.
## In addition to label, an optional fromlabel and tolabel can be used to name the column
## that contains the text for the label in the edges source or target (invert ignored).
## The label is concatenated in the form fromlabel + label + tolabel if all are defined.
## Additional labels can be added by using an optional labels array with entries of the
## form {"label": string, "x": number, "y": number, "dx": number, "dy": number} where
## x is from -1 to 1 along the edge, y is orthogonal, and dx/dy are offsets in pixels.
## The target column may contain a comma-separated list of values.
## Multiple connect entries are allowed.
#
# connect: {"from": "refs", "to": "id", "style": "edgeStyle=orthogonalEdgeStyle;html=1;endArrow=block;elbow=vertical;startArrow=none;endFill=1;strokeColor=#545B64;rounded=0;"}
#
## Node x-coordinate. Possible value is a column name. Default is empty. Layouts will
## override this value.
#
# left:
#
## Node y-coordinate. Possible value is a column name. Default is empty. Layouts will
## override this value.
#
# top:
#
## Node width. Possible value is a number (in px), auto or an @ sign followed by a column
## name that contains the value for the width. Default is auto.
#
# width: auto
#
## Node height. Possible value is a number (in px), auto or an @ sign followed by a column
## name that contains the value for the height. Default is auto.
#
# height: auto
#
## Padding for autosize. Default is 0.
#
# padding: 0
#
## Comma-separated list of ignored columns for metadata. (These can be
## used for connections and styles but will not be added as metadata.)
#
#
## Column to be renamed to link attribute (used as link).
#
# ignore: id, image, refs
#
## Spacing between nodes. Default is 40.
#
# nodespacing: 40
#
## Spacing between levels of hierarchical layouts. Default is 100.
#
# levelspacing: 100
#
## Spacing between parallel edges. Default is 40. Use 0 to disable.
#
# edgespacing: 10
#
## Name or JSON of layout. Possible values are auto, none, verticaltree, horizontaltree,
## verticalflow, horizontalflow, organic, circle or a JSON string as used in Layout, Apply.
## Default is auto.
#
# layout: verticalflow
#
## ---- CSV below this line. First line are column names."""
root_id = org.list_roots()['Roots'][0]['Id']
csv += f"\nid, ou, scps, refs, image\n{root_id}, 'ManagementAccount', , , https://raw.githubusercontent.com/daknhh/aws-orgtool/68de9477ed0fa9ac3dda1beea938b7453d44480e/static/AWS-Organizations_Management-Account.svg"
print("Generating visualization of Organization.")
for firstlevel in tqdm(data['Ous']):
scps = ""
if (firstlevel['SCPs'] != []):
scps += "Attached SCPs: "
for scp in firstlevel['SCPs']:
scps += f"{scp['Name']} "
csv += f"\n{firstlevel['Id']}, {firstlevel['Name']}, {scps}, {root_id}, https://raw.githubusercontent.com/daknhh/aws-orgtool/68de9477ed0fa9ac3dda1beea938b7453d44480e/static/AWS-Organizations_Organizational-Unit.svg"
if firstlevel['Children'] == 'No-Children':
logger.info(f"{firstlevel['Name']} has no No-Children")
else:
for secondlevel in firstlevel['Children']:
scps = ""
if (secondlevel['SCPs'] != []):
scps += "Attached SCPs: "
for scp in secondlevel['SCPs']:
scps += f"{scp['Name']} "
csv += f"\n{secondlevel['Id']}, {secondlevel['Name']}, {scps}, {firstlevel['Id']}, https://raw.githubusercontent.com/daknhh/aws-orgtool/68de9477ed0fa9ac3dda1beea938b7453d44480e/static/AWS-Organizations_Organizational-Unit.svg"
if secondlevel['Children'] == 'No-Children':
logger.info(f"{secondlevel['Name']} has no No-Children")
else:
for thirdlevel in secondlevel['Children']:
scps = ""
if (thirdlevel['SCPs'] != []):
scps += "Attached SCPs: "
for scp in thirdlevel['SCPs']:
scps += f"{scp['Name']} "
csv += f"\n{thirdlevel['Id']}, {thirdlevel['Name']}, {scps}, {secondlevel['Id']}, https://raw.githubusercontent.com/daknhh/aws-orgtool/68de9477ed0fa9ac3dda1beea938b7453d44480e/static/AWS-Organizations_Organizational-Unit.svg"
if thirdlevel['Children'] == 'No-Children':
logger.info(f"{thirdlevel['Name']} has no No-Children")
else:
for fourlevel in thirdlevel['Children']:
scps = ""
if (fourlevel['SCPs'] != []):
scps += "Attached SCPs: "
for scp in fourlevel['SCPs']:
scps += f"{scp['Name']} "
csv += f"\n{fourlevel['Id']}, {fourlevel['Name']}, {scps}, {thirdlevel['Id']}, https://raw.githubusercontent.com/daknhh/aws-orgtool/68de9477ed0fa9ac3dda1beea938b7453d44480e/static/AWS-Organizations_Organizational-Unit.svg"
if fourlevel['Children'] == 'No-Children':
logger.info(f"{fourlevel['Name']} has no No-Children")
else:
for fivelevel in fourlevel['Children']:
scps = ""
if (fivelevel['SCPs'] != []):
scps += "Attached SCPs: "
for scp in fivelevel['SCPs']:
scps += f"{scp['Name']} "
csv += f"\n{fivelevel['Id']}, {fivelevel['Name']}, {scps}, {fourlevel['Id']}, https://raw.githubusercontent.com/daknhh/aws-orgtool/68de9477ed0fa9ac3dda1beea938b7453d44480e/static/AWS-Organizations_Organizational-Unit.svg"
csvfile.write(csv)
csvfile.close
def visualize_organization_graphviz(file, org):
logger.info(f'Import Json file: {file}')
f = open(file, )
data = json.load(f)
dot = Digraph(comment='Organization', edge_attr={'arrowhead': 'none'})
root_id = org.list_roots()['Roots'][0]['Id']
dot.node('O', f"Organization: \n{root_id}")
print("Generating visualization of Organization.")
for firstlevel in tqdm(data['Ous']):
dot.node(f"{firstlevel['Id']}", f"{firstlevel['Name']}", shape='box')
dot.edge("O", f"{firstlevel['Id']}")
if firstlevel['Children'] == 'No-Children':
logger.info(f"{firstlevel['Name']} has no No-Children")
else:
for secondlevel in firstlevel['Children']:
dot.node(f"{secondlevel['Id']}", f"{secondlevel['Name']}", shape='box')
dot.edge(f"{firstlevel['Id']}", f"{secondlevel['Id']}")
if secondlevel['Children'] == 'No-Children':
logger.info(f"{secondlevel['Name']} has no No-Children")
else:
for thirdlevel in secondlevel['Children']:
dot.node(f"{thirdlevel['Id']}", f"{thirdlevel['Name']}", shape='box')
dot.edge(f"{secondlevel['Id']}", f"{thirdlevel['Id']}")
if thirdlevel['Children'] == 'No-Children':
logger.info(f"{thirdlevel['Name']} has no No-Children")
else:
for fourlevel in thirdlevel['Children']:
dot.node(f"{fourlevel['Id']}", f"{fourlevel['Name']}", shape='box')
dot.edge(f"{fourlevel['Id']}", f"{thirdlevel['Id']}")
if fourlevel['Children'] == 'No-Children':
logger.info(f"{fourlevel['Name']} has no No-Children")
else:
for fivelevel in fourlevel['Children']:
logger.info(f"{fourlevel['Name']} has no No-Children")
dot.node(f"{fivelevel['Id']}", f"{fivelevel['Name']}", shape='box')
dot.edge(f"{fourlevel['Id']}", f"{fivelevel['Id']}")
dot.graph_attr['nodesep'] = '1.0'
dot.render('organization.gv', view=True, format='png')
f.close()
def export_policies(file, org):
response = org.list_policies(
Filter='SERVICE_CONTROL_POLICY')
logger.info('Inititalize Dict for Policies')
policies = {}
if os.path.isdir('policies'):
logger.info('policies directory exist.')
else:
os.mkdir('policies')
print("\nDirectory policies created")
logger.info('Directory policies created.')
print("\n\n⌛️ Check if SCPs exist.")
if response['Policies'] == []:
print("ℹ️ No SCPs.")
logger.info('No SCPs.')
else:
print("Exporting SCPs...")
if os.path.isdir('policies/scp'):
logger.info('scp directory exist.')
else:
os.mkdir('policies/scp')
print("\nDirectory policies/scp created")
logger.info('Directory policies/scp created.')
for scp in tqdm(response['Policies']):
if scp['Name'] == 'FullAWSAccess':
print("\n FullAWSAccess default SCP")
logger.info('FullAWSAccess default SCP')
else:
contentfile = f"policies/scp/{scp['Name']}.json"
responsepolicy = org.describe_policy(
PolicyId=scp['Id']
)
content = responsepolicy['Policy']['Content']
scpcontent = open(contentfile, "w")
json.dump(json.loads(content), scpcontent, indent=6)
scpcontent.close()
logger.info(f'Created SCP Content File: {contentfile} in scps directory 🗂.')
print(f'\n\nCreated SCP Content File: {contentfile} in scps directory 🗂.')
policies.setdefault('Scps', []).append({'Id': scp['Id'], 'Name': scp['Name'], 'Description': scp['Description'], 'ContentFile': contentfile})
logger.info(f'Add SCP {scp} to policies Dict.')
print(f"Add SCP {scp['Name']} to policies Dict.")
out_file = open(file, "w")
json.dump(policies, out_file, indent=6)
out_file.close()
response = org.list_policies(Filter='TAG_POLICY')
print("\n\n⌛️ Check if Tag Policies exist.")
if response['Policies'] == []:
print("ℹ️ No Tag Policies.")
else:
print("Exporting Tag Policies...")
if os.path.isdir('policies/tag_policies'):
logger.info('tag_policies directory exist.')
else:
os.mkdir('policies/tag_policies')
print("\n Directory policies/tag_policies created")
logger.info(' Directory policies/tag_policies created.')
for tag in tqdm(response['Policies']):
contentfile = f"policies/tag_policies/{tag['Name']}.json"
responsepolicy = org.describe_policy(
PolicyId=tag['Id']
)
content = responsepolicy['Policy']['Content']
tagcontent = open(contentfile, "w")
json.dump(json.loads(content), tagcontent, indent=6)
tagcontent.close()
logger.info(f'Created Tag Content File: {contentfile} in tags directory 🗂.')
print(f'\n\nCreated Tag Content File: {contentfile} in tags directory 🗂.')
policies.setdefault('Tags', []).append({'Id': tag['Id'], 'Name': scp['Name'], 'Description': scp['Description'], 'ContentFile': contentfile})
logger.info(f'Add Tag {tag} to policies Dict.')
print(f"Add Tag {tag['Name']} to policies Dict.")
response = org.list_policies(Filter='BACKUP_POLICY')
print("\n\n⌛️ Check if Backup Policies exist.")
if response['Policies'] == []:
print("ℹ️ No Backup Policies.")
else:
print("Exporting Backup Policies...")
if os.path.isdir('policies/backup_policies'):
logger.info('policies/backup_policies directory exist.')
else:
os.mkdir('policies/backup_policies')
print("\n Directory policies/backup_policies created")
logger.info(' Directory policies/backup_policies created.')
for policy in tqdm(response['Policies']):
contentfile = f"policies/backup_policies/{policy['Name']}.json"
responsepolicy = org.describe_policy(PolicyId=policy['Id'])
content = responsepolicy['Policy']['Content']
tagcontent = open(contentfile, "w")
json.dump(json.loads(content), tagcontent, indent=6)
tagcontent.close()
logger.info(f'Created Tag Content File: {contentfile} in tags directory 🗂.')
print(f'\n\nCreated Tag Content File: {contentfile} in tags directory 🗂.')
policies.setdefault('Backup', []).append({'Id': policy['Id'], 'Name': policy['Name'], 'Description': policy['Description'], 'ContentFile': contentfile})
logger.info(f'Add Tag {policy} to policies Dict.')
print(f"Add Tag {policy['Name']} to policies Dict.")
response = org.list_policies(Filter='AISERVICES_OPT_OUT_POLICY')
print("\n\n⌛️ Check if AI services opt-out exist.")
if response['Policies'] == []:
print("ℹ️ No AI services opt-out Policies.")
else:
print("Exporting AI services opt-out Policies...")
if os.path.isdir('policies/ai_services_opt_out_policies'):
logger.info('backup_policies directory exist.')
else:
os.mkdir('policies/ai_services_opt_out_policies')
print("\n Directory policies/ai_services_opt_out_policies created")
logger.info(' Directory policies/ai_services_opt_out_policies created.')
for policy in tqdm(response['Policies']):
contentfile = f"policies/ai_services_opt_out_policies/{policy['Name']}.json"
responsepolicy = org.describe_policy(
PolicyId=policy['Id']
)
content = responsepolicy['Policy']['Content']
tagcontent = open(contentfile, "w")
json.dump(json.loads(content), tagcontent, indent=6)
tagcontent.close()
logger.info(f'Created Tag Content File: {contentfile} in tags directory 🗂.')
print(f'\n\nCreated Tag Content File: {contentfile} in tags directory 🗂.')
policies.setdefault('AI', []).append({'Id': policy['Id'], 'Name': policy['Name'], 'Description': policy['Description'], 'ContentFile': contentfile})
logger.info(f'Add Tag {policy} to policies Dict.')
print(f"Add Tag {policy['Name']} to policies Dict.")
out_file = open(file, "w")
json.dump(policies, out_file, indent=6)
out_file.close()
logger.info(f'Created Policies File: {file}.')
print("\n************************")
print(f'Policies have been written to File: {file} 🗃.')
def import_policies(file, org, exclude):
logger.info(f'Import Json file: {file}')
f = open(file, )
alldata = json.load(f)
print("\n************************")
print("\nImport-Policies:")
print("\n\n⌛️ Check if SCPs to import.")
key = 'Scps'
if((key in alldata) and (key != exclude)):
currentscps = {policy["Name"]: policy["Id"] for policy in org.list_policies(Filter='SERVICE_CONTROL_POLICY').get("Policies", [])}
print("\nImport-SCPs:")
for scp in tqdm(alldata['Scps']):
print(f"- {scp['Name']}")
f = open(scp['ContentFile'], )
print(f" - Import Json file: {scp['ContentFile']}.")
logger.info(f"Import Json file: {scp['ContentFile']}.")
data = json.load(f)
scp_id = currentscps.get(scp['Name'])
if scp_id:
try:
response = org.update_policy(
Content=json.dumps(data),
Description=scp['Description'],
Name=scp['Name'],
PolicyId=scp_id)
print(f"\n\nℹ SCP with Name: {scp['Name']} - already exist - ✅ Policy was updated. \n\n")
except org.exceptions as e:
logger.info(f"Error: {e} while updating SCP: {scp['Name']}.")
print(f"\n\n🚨 Error: {e} while updating SCP: {scp['Name']}.\n\n")
else:
try:
response = org.create_policy(
Content=json.dumps(data),
Description=scp['Description'],
Name=scp['Name'],
Type='SERVICE_CONTROL_POLICY')
logger.info(f"Created SCP with Name: {scp['Name']} - Id: {response['Policy']['PolicySummary']['Id']}.")
print(f"\n\n✅ Created SCP with Name: {scp['Name']} - Id: {response['Policy']['PolicySummary']['Id']}. \n\n")
except org.exceptions as e:
logger.info(f"Error: {e} while creating SCP: {scp['Name']}.")
print(f"\n\n🚨 Error: {e} while creating SCP: {scp['Name']}.\n\n")
print('\n ✅ SCPs have been imported.')
else:
print(" ℹ️ No SCPs to import.")
print("\n\n⌛️ Check if Tag Policies to import.")
key = 'Tags'
if((key in alldata) and (key != exclude)):
currenttags = {policy["Name"]: policy["Id"] for policy in org.list_policies(Filter='TAG_POLICY').get("Policies", [])}
print("\nImport-Tag Policies:")
for policy in tqdm(alldata['Tags']):
print(f"- {policy['Name']}")
f = open(policy['ContentFile'], )
print(f" - Import Json file: {policy['ContentFile']}.")
logger.info(f"Import Json file: {policy['ContentFile']}.")
data = json.load(f)
tag_id = currenttags.get(policy['Name'])
if tag_id:
try:
response = org.update_policy(
Content=json.dumps(data),
Description=policy['Description'],
Name=policy['Name'],
PolicyId=tag_id)
print(f"\n\nℹ Tag Policy with Name: {policy['Name']} - already exist - ✅ Policy was updated. \n\n")
except org.exceptions as e:
logger.info(f"Error: {e} while updating Tag Policy: {policy['Name']}.")
print(f"\n\n🚨 Error: {e} while updating Tag Policy: {policy['Name']}.\n\n")
else:
try:
response = org.create_policy(
Content=json.dumps(data),
Description=policy['Description'],
Name=policy['Name'],
Type='TAG_POLICY')
logger.info(f"Created Tag Policy with Name: {policy['Name']} - Id: {response['Policy']['PolicySummary']['Id']}.")
print(f"\n\n✅ Created Tag Policy with Name: {policy['Name']} - Id: {response['Policy']['PolicySummary']['Id']}. \n\n")
except org.exceptions as e:
logger.info(f"Error: {e} while creating Tag Policy: {policy['Name']}.")
print(f"\n\n🚨 Error: {e} while creating Tag Policy: {policy['Name']}.\n\n")
print('\n ✅ Tag Policies have been imported.')
else:
print(" ℹ️ No Tag Policies to import.")
print("\n\n⌛️ Check if Backup Policies to import.")
key = 'Backup'
if((key in alldata) and (key != exclude)):
currentbackup = {policy["Name"]: policy["Id"] for policy in org.list_policies(Filter='BACKUP_POLICY').get("Policies", [])}
print("\nImport Backup Policies:")
for policy in tqdm(alldata['Backup']):
print(f"- {policy['Name']}")
f = open(policy['ContentFile'], )
print(f" - Import Json file: {policy['ContentFile']}.")
logger.info(f"Import Json file: {policy['ContentFile']}.")
data = json.load(f)
backup_id = currentbackup.get(policy['Name'])
if backup_id:
try:
response = org.update_policy(
Content=json.dumps(data),
Description=policy['Description'],
Name=policy['Name'],
PolicyId=backup_id)
print(f"\n\nℹ Tag Policy with Name: {policy['Name']} - already exist - ✅ Policy was updated. \n\n")
except org.exceptions as e:
logger.info(f"Error: {e} while updating Backup Policy: {policy['Name']}.")
print(f"\n\n🚨 Error: {e} while updating Backup Policy: {policy['Name']}.\n\n")
else:
try:
response = org.create_policy(
Content=json.dumps(data),
Description=policy['Description'],
Name=policy['Name'],
Type='BACKUP_POLICY')
logger.info(f"Created Backup Policy with Name: {policy['Name']} - Id: {response['Policy']['PolicySummary']['Id']}.")
print(f"\n\n✅ Created Backup Policy with Name: {policy['Name']} - Id: {response['Policy']['PolicySummary']['Id']}. \n\n")
except org.exceptions as e:
logger.info(f"Error: {e} while creating Backup Policy: {policy['Name']}.")
print(f"\n\n🚨 Error: {e} while creating Backup Policy: {policy['Name']}.\n\n")
print('\n ✅ Backup Policies have been imported.')
else:
print(" ℹ️ No Backup Policies to import.")
print("\n\n⌛️ Check if AI services opt-out Policies to import.")
key = 'AI'
if((key in alldata) and (key != exclude)):
currentais = {policy["Name"]: policy["Id"] for policy in org.list_policies(Filter='AISERVICES_OPT_OUT_POLICY').get("Policies", [])}
print("\nImport AI services opt-out Policies:")
for policy in tqdm(alldata['AI']):
print(f"- {policy['Name']}")
f = open(policy['ContentFile'], )
print(f" - Import Json file: {policy['ContentFile']}.")
logger.info(f"Import Json file: {policy['ContentFile']}.")
data = json.load(f)
ai_id = currentais.get(policy['Name'])
if ai_id:
try:
response = org.update_policy(
Content=json.dumps(data),
Description=policy['Description'],
Name=policy['Name'],
PolicyId=ai_id)
print(f"\n\nℹ AI services opt-out Policy with Name: {policy['Name']} - already exist - ✅ Policy was updated. \n\n")
except org.exceptions as e:
logger.info(f"Error: {e} while updating AI services opt-out Policy: {policy['Name']}.")
print(f"\n\n🚨 Error: {e} while updating AI services opt-out Policy: {policy['Name']}.\n\n")
else:
try:
response = org.create_policy(
Content=json.dumps(data),
Description=policy['Description'],
Name=policy['Name'],
Type='AISERVICES_OPT_OUT_POLICY')
logger.info(f"Created AI services opt-out Policy with Name: {policy['Name']} - Id: {response['Policy']['PolicySummary']['Id']}.")
print(f"\n\n✅ Created AI services opt-out Policy with Name: {policy['Name']} - Id: {response['Policy']['PolicySummary']['Id']}. \n\n")
except org.exceptions as e:
logger.info(f"Error: {e} while creating AI services opt-out Policy: {policy['Name']}.")
print(f"\n\n🚨 Error: {e} while creating AI services opt-out Policy: {policy['Name']}.\n\n")
print('\n ✅ AI services opt-out Policies have been imported.')
else:
print(" ℹ️ AI services opt-out Policies to import.")
print("\n************************")
def validate_policies(file, accessanalyzer):
logger.info(f'Load Json file: {file}')
f = open(file, )
data = json.load(f)
print("\n************************")
print("Validate Policies \n")
for scp in tqdm(data['Scps']):
print("\n------------------------------------------")
print(f"🔍 Findings for: \n{scp['Name']} SCP \n\n")
logger.info(f"Validate SCP with Name: {scp['Name']}")
response = accessanalyzer.validate_policy(
locale='EN',
policyDocument=scp['ContentFile'],
policyType='SERVICE_CONTROL_POLICY')
for finding in response['findings']:
if finding['findingDetails'] == 'Fix the JSON syntax error at index 0 line 1 column 0.':
print("🥳 No Finding.")
else:
if finding['findingType'] == 'ERROR':
findingtype = '❗️'
if finding['findingType'] == 'SECURITY_WARNING':
findingtype = '🚨'
if finding['findingType'] == 'WARNING':
findingtype = '⚠️'
if finding['findingType'] == 'SUGGESTION':
findingtype = '💡'
print(f"{findingtype}{finding['findingType']}")
print(f"Details: {finding['findingDetails']}")
print(f"Code: {finding['issueCode']}")
print(f"🔗 Learn more: \x1b]8;;{finding['learnMoreLink']}\aCtrl+Click here\x1b]8;;\a \n")
logger.info(f"Finding in {scp['Name']} - Type:{finding['findingType']} - Details:{finding['findingDetails']} - Code:{finding['issueCode']} - Learn more:{finding['learnMoreLink']}")
def get_ou_stucture(parent_id, org):
paginator = org.get_paginator('list_organizational_units_for_parent')
page_iterator = paginator.paginate(ParentId=parent_id)
ous = {}
ou_secondlevel = {}
ou_thirdlevel = {}
ou_fivelevel = {}
ou_sixlevel = {}
ou_sevenlevel = {}
print("\n************************")
print("\nOrganization-Structure: ")
print("%s" % (parent_id))
for page in page_iterator:
ous.setdefault('Ous', [])
for ou in page['OrganizationalUnits']:
logger.info(f'Inititalize Dict for {ou}')
tags = get_tagsforou(ou['Id'], org)
accounts = get_accounts_for_ou(ou['Id'], org)
ous['Ous'].append({'Id': ou['Id'], 'Name': ou['Name'], 'Tags': tags['Tags'], 'Accounts': accounts['Accounts'], 'Children': {}})
for idx, ou in enumerate(ous['Ous']):
page_iterator = paginator.paginate(ParentId=ou['Id'])
logger.info(f'Check {ou} for Children')
ou_secondlevel.clear()
ou_secondlevel.setdefault('Children', [])
for page in page_iterator:
scp = get_scpforou(ou['Id'], org)
tags = get_tagsforou(ou['Id'], org)
accounts = get_accounts_for_ou(ou['Id'], org)
if page['OrganizationalUnits'] == []:
ou_secondlevel = {'Children': 'No-Children'}
print(" - %s" % (ou['Name']))
else:
print(" - %s" % (ou['Name']))
for ou_2l in page['OrganizationalUnits']:
print(" - - %s" % (ou_2l['Name']))
ou_secondlevel['Children'].append({'Id': ou_2l['Id'], 'Name': ou_2l['Name'], 'SCP': scp, 'Tags': tags, 'Accounts': accounts, 'Children': {}})
ous['Ous'][idx] = {'Id': ou['Id'], 'Name': ou['Name'], 'SCPs': scp['SCPs'], 'Tags': tags['Tags'], 'Accounts': accounts['Accounts'], 'Children': ou_secondlevel['Children']}
if ou_secondlevel == 'No-Children':
ou_secondlevel = {}
if ous['Ous'][idx]['Children'] == 'No-Children':
logger.info('No-Children')
else:
for idx2, ou3 in enumerate(ous['Ous'][idx]['Children']):
scp = get_scpforou(ou3['Id'], org)
tags = get_tagsforou(ou3['Id'], org)
accounts = get_accounts_for_ou(ou3['Id'], org)
page_iterator2 = paginator.paginate(ParentId=ou3['Id'])
logger.info(f'Check {ou3} for Children')
ou_thirdlevel.clear()
ou_thirdlevel.setdefault('Children', [])
for page in page_iterator2:
if page['OrganizationalUnits'] == []:
ou_thirdlevel = {'Children': 'No-Children'}
logger.info('No-Children')
print(" - - - %s" % (ou3['Name']))
else:
logger.info(page['OrganizationalUnits'])
print(" - - - %s" % (ou3['Name']))
for ou_3l in page['OrganizationalUnits']:
print(" - - - - %s" % (ou_3l['Name']))
ou_thirdlevel['Children'].append({'Id': ou_3l['Id'], 'Name': ou_3l['Name'], 'Tags': tags, 'Accounts': accounts, 'Children': {}})
ous['Ous'][idx]['Children'][idx2] = {'Id': ou3['Id'], 'Name': ou3['Name'], 'SCPs': scp['SCPs'], 'Tags': tags['Tags'], 'Accounts': accounts['Accounts'], 'Children': ou_thirdlevel['Children']}
if ou_thirdlevel == {'Children': 'No-Children'}:
ou_thirdlevel = {}
if ous['Ous'][idx]['Children'][idx2]['Children'] == 'No-Children':
logger.info('No-Children')
else:
for idx3, ou4 in enumerate(ous['Ous'][idx]['Children'][idx2]['Children']):
scp = get_scpforou(ou4['Id'], org)
tags = get_tagsforou(ou4['Id'], org)
accounts = get_accounts_for_ou(ou4['Id'], org)
page_iterator3 = paginator.paginate(ParentId=ou4['Id'])
logger.info(f'Check {ou4} for Children')
ou_fivelevel.clear()
ou_fivelevel.setdefault('Children', [])
for page in page_iterator3:
if page['OrganizationalUnits'] == []:
ou_fivelevel = {'Children': 'No-Children'}
logger.info('No-Children')
print(" - - - - - %s" % (ou4['Name']))
else:
logger.info(page['OrganizationalUnits'])
print(" - - - - - %s" % (ou4['Name']))
for ou_4l in page['OrganizationalUnits']:
print(" - - - - - - %s" % (ou_4l['Name']))
ou_fivelevel['Children'].append({'Id': ou_4l['Id'], 'Name': ou_4l['Name'], 'Tags': tags, 'Accounts': accounts, 'Children': {}})
ous['Ous'][idx]['Children'][idx2]['Children'][idx3] = {'Id': ou4['Id'], 'Name': ou4['Name'], 'SCPs': scp['SCPs'], 'Tags': tags['Tags'], 'Accounts': accounts['Accounts'], 'Children': ou_fivelevel['Children']}
if ou_fivelevel == {'Children': 'No-Children'}:
ou_fivelevel = {}
if ous['Ous'][idx]['Children'][idx2]['Children'][idx3]['Children'] == 'No-Children':
logger.info('No-Children')
else:
for idx4, ou5 in enumerate(ous['Ous'][idx]['Children'][idx2]['Children'][idx3]['Children']):
scp = get_scpforou(ou5['Id'], org)
tags = get_tagsforou(ou5['Id'], org)
accounts = get_accounts_for_ou(ou5['Id'], org)
page_iterator4 = paginator.paginate(ParentId=ou5['Id'])
logger.info(f'Check {ou5} for Children')
ou_sixlevel.clear()
ou_sixlevel.setdefault('Children', [])
for page in page_iterator4:
if page['OrganizationalUnits'] == []:
ou_sixlevel = {'Children': 'No-Children'}
logger.info('No-Children')
print(" - - - - - %s" % (ou5['Name']))
else:
logger.info(page['OrganizationalUnits'])
print(" - - - - - %s" % (ou5['Name']))
for ou_5l in page['OrganizationalUnits']:
print(" - - - - - - %s" % (ou_5l['Name']))
ou_sixlevel['Children'].append({'Id': ou_5l['Id'], 'Name': ou_5l['Name'], 'Tags': tags, 'Accounts': accounts, 'Children': {}})
ous['Ous'][idx]['Children'][idx2]['Children'][idx3]['Children'][idx4] = {'Id': ou5['Id'], 'Name': ou5['Name'], 'SCPs': scp['SCPs'], 'Tags': tags['Tags'], 'Accounts': accounts['Accounts'], 'Children': ou_sixlevel['Children']}
if ou_sixlevel == {'Children': 'No-Children'}:
ou_sixlevel = {}
if ous['Ous'][idx]['Children'][idx2]['Children'][idx3]['Children'][idx4]['Children'] == 'No-Children':
logger.info('No-Children')
else:
for idx5, ou6 in enumerate(ous['Ous'][idx]['Children'][idx2]['Children'][idx3]['Children'][idx4]['Children']):
scp = get_scpforou(ou6['Id'], org)
tags = get_tagsforou(ou6['Id'], org)
accounts = get_accounts_for_ou(ou6['Id'], org)
page_iterator5 = paginator.paginate(ParentId=ou6['Id'])
ou_sevenlevel.clear()
ou_sevenlevel.setdefault('Children', [])
logger.info(f'Check {ou6} for Children')
for page in page_iterator5:
if page['OrganizationalUnits'] == []:
ou_sevenlevel = {'Children': 'No-Children'}
logger.info('No-Children')
print(" - - - - - - %s" % (ou6['Name']))
else:
logger.info(page['OrganizationalUnits'])
print(" - - - - - - %s" % (ou6['Name']))
for ou_6l in page['OrganizationalUnits']:
print(" - - - - - - - %s" % (ou_6l['Name']))
ou_sevenlevel['Children'].append({'Id': ou_6l['Id'], 'Name': ou_6l['Name'], 'Tags': tags, 'Accounts': accounts, 'Children': {'Children': 'No-Children'}})
ous['Ous'][idx]['Children'][idx2]['Children'][idx3]['Children'][idx4]['Children'][idx5] = {'Id': ou6['Id'], 'Name': ou6['Name'], 'SCPs': scp['SCPs'], 'Tags': tags['Tags'], 'Accounts': accounts['Accounts'], 'Children': ou_sevenlevel['Children']}
if ou_sevenlevel == {'Children': 'No-Children'}:
ou_sevenlevel = {}
return ous
def export_structure(file, org):
root_id = org.list_roots()['Roots'][0]['Id']
logger.info('Query first level OUs')
ous = get_ou_stucture(root_id, org)
out_file = open(file, "w")
json.dump(ous, out_file, indent=6)
out_file.close()
print("\n************************")
logger.info(f'\n Write Ous to file: {file}')
print(f'Ous have been written to {file}.')
def get_scpforou(ou_id, org):
response = org.list_policies_for_target(
TargetId=ou_id,
Filter='SERVICE_CONTROL_POLICY')
scp = {}
scp.setdefault('SCPs', [])
for policy in response['Policies']:
if policy['Name'] == 'FullAWSAccess':
logger.info('\n AWS SCP Found: FullAWSAccess')
else:
scp.setdefault('SCPs', []).append({'Name': policy['Name']})
return scp
def get_tagsforou(ressource_id, org):
response = org.list_tags_for_resource(
ResourceId=ressource_id)
tags = {}
tags.setdefault('Tags', [])
for tag in response['Tags']:
tags.setdefault('Tags', []).append(tag)
return tags
def get_all_scps(org):
response = org.list_policies(
Filter='SERVICE_CONTROL_POLICY')
allscps = ""
for Scp in response['Policies']:
allscps += f"\"{Scp['Name']}\": \"{Scp['Id']}\", "
lenght = len(allscps)
allscps = allscps[:lenght-2]
allscpsstring = "{" + allscps + "}"
convertedDict = json.loads(allscpsstring)
return convertedDict
def get_accounts_for_ou(ou, org):
paginator = org.get_paginator('list_accounts_for_parent')
page_iterator = paginator.paginate(ParentId=ou)
accounts = {}
accounts.setdefault('Accounts', [])
for page in page_iterator:
for account in page['Accounts']:
accounttags = get_tagsforou(account['Id'], org)
accounts['Accounts'].append({'Id': account['Id'], 'Tags': accounttags['Tags'],'Email': account['Email'], 'Name': account['Name']})
return accounts
def attach_policies(file, org):
scps_in_org = get_all_scps(org)
logger.info(f'Import Json file: {file}')
f = open(file, )
data = json.load(f)
root_id = org.list_roots()['Roots'][0]['Id']
print("\n************************")
print("\nAttach-SCPs:")
for firstlevel in tqdm(data['Ous']):
firstlevelname = firstlevel['Name']
firstlevelou_id = get_ou_id_by_name(firstlevelname, root_id, org)
if (firstlevel['SCPs'] == []):
logger.info(f'No SCP to attach for: {firstlevelou_id} - {firstlevelname} in {root_id}')
print(f'\nℹ️ No SCPs for OU: {firstlevelname}.\n')
else:
print(f'\n\nAttaching SCPs to OU {firstlevelname}: ')
for scp in firstlevel['SCPs']:
policyid = scps_in_org.get(scp['Name'])
try:
org.attach_policy(
PolicyId=policyid,
TargetId=firstlevelou_id)
logger.info(f'Attached: {policyid} to {firstlevelou_id}')
print(f"✅ {scp['Name']} - {policyid}")
except org.exceptions.DuplicatePolicyAttachmentException:
logger.info(f'Already attached: {policyid} to {firstlevelou_id}')
print(f"✅ {scp['Name']} - {policyid}")
if firstlevel['Children'] == 'No-Children':
logger.info(f'{firstlevelname} has no No-Children')
else:
for secondlevel in firstlevel['Children']:
secondlevelname = secondlevel['Name']
secondlevelou_id = get_ou_id_by_name(secondlevelname, firstlevelou_id, org)
if (secondlevel['SCPs'] == []):
logger.info(f'No SCP to attach for: {secondlevelou_id} - {secondlevelname} in {firstlevelou_id}')
print(f'\nℹ️ No SCPs for OU: {secondlevelname}.')
else:
print(f'\n\nAttaching SCPs to OU {secondlevelname}: ')
for scp in secondlevel['SCPs']:
policyid = scps_in_org.get(scp['Name'])
try:
org.attach_policy(
PolicyId=policyid,
TargetId=secondlevelou_id)
logger.info(f'Attached: {policyid} to {secondlevelou_id}')
print(f"✅ {scp['Name']} - {policyid}")
except org.exceptions.DuplicatePolicyAttachmentException:
logger.info(f'Already attached: {policyid} to {secondlevelou_id}')
print(f"✅ {scp['Name']} - {policyid}")
if secondlevel['Children'] == 'No-Children':
logger.info(f'{secondlevelname} has no No-Children')
else:
for thirdlevel in secondlevel['Children']:
thirdlevelname = thirdlevel['Name']
thirdlevelou_id = get_ou_id_by_name(thirdlevelname, secondlevelou_id, org)
if (thirdlevel['SCPs'] == []):
logger.info(f'No SCPs to attach for: {thirdlevelou_id} - {thirdlevelname} in {secondlevelou_id}')
print(f'\nℹ️ No SCPs for OU: {thirdlevelname}.')
else:
print(f'\n\nAttaching SCPs to OU {thirdlevelname}: ')
for scp in thirdlevel['SCPs']:
policyid = scps_in_org.get(scp['Name'])
try:
org.attach_policy(
PolicyId=policyid,
TargetId=thirdlevelou_id)
logger.info(f'Attached: {policyid} to {thirdlevelou_id}')
print(f"✅ {scp['Name']} - {policyid}")
except org.exceptions.DuplicatePolicyAttachmentException:
logger.info(f'Already attached: {policyid} to {thirdlevelou_id}')
print(f"✅ {scp['Name']} - {policyid}")
if thirdlevel['Children'] == 'No-Children':
logger.info(f'{thirdlevelname} has no No-Children')
else:
for fourlevel in thirdlevel['Children']:
fourlevelname = thirdlevel['Name']
fourlevelou_id = get_ou_id_by_name(fourlevelname, thirdlevelou_id, org)
if (fourlevel['SCPs'] == []):
logger.info(f'No SCPs to attach for: {fourlevelou_id} - {fourlevelname} in {thirdlevelou_id}')
print(f'\nℹ️ No SCPs for OU: {fourlevelname}.')
else:
print(f'\n\nAttaching SCPs to OU {fourlevelname}: ')
for scp in fourlevel['SCPs']:
policyid = scps_in_org.get(scp['Name'])
try:
org.attach_policy(
PolicyId=policyid,
TargetId=fourlevelou_id)
logger.info(f'Attached: {policyid} to {fourlevelou_id}')
print(f"✅ {scp['Name']} - {policyid}")
except org.exceptions.DuplicatePolicyAttachmentException:
logger.info(f'Already attached: {policyid} to {fourlevelou_id}')
print(f"✅ {scp['Name']} - {policyid}")
if fourlevel['Children'] == 'No-Children':
logger.info(f'{fourlevelname} has no No-Children')
else:
for fivelevel in fourlevel['Children']:
fivelevelname = fivelevel['Name']
fivelevelou_id = get_ou_id_by_name(fivelevelname, fourlevelou_id, org)
if (fivelevel['SCPs'] == []):
logger.info(f'No SCPs to attach for: {fivelevelou_id} - {fivelevelname} in {fourlevelou_id}')
print(f'\nℹ️ No SCPs for OU: {fivelevelname}.')
else:
print(f'\n\nAttaching SCPs to OU {fourlevelname}: ')
for scp in fivelevel['SCPs']:
policyid = scps_in_org.get(scp['Name'])
try:
org.attach_policy(
PolicyId=policyid,
TargetId=fivelevelou_id,
)
logger.info(f'Attached: {policyid} to {fivelevelou_id}')
print(f"✅ {scp['Name']} - {policyid}")
except org.exceptions.DuplicatePolicyAttachmentException:
logger.info(f'Already attached: {policyid} to {fivelevelou_id}')
print(f"✅ {scp['Name']} - {policyid}")
def get_ou_id_by_name(name, parent_id, org):
paginator = org.get_paginator('list_organizational_units_for_parent')
page_iterator = paginator.paginate(ParentId=parent_id)
search_key = name
logger.info(f'Search for OuID by name: {name}')
for page in page_iterator:
for ou in page['OrganizationalUnits']:
for (key, value) in ou.items():
if value == search_key:
res = ou['Id']
logger.info(f'Got OuID: {res}')
return (res)
def import_structure(file, org):
logger.info(f'Import Json file: {file}')
f = open(file, )
data = json.load(f)
root_id = org.list_roots()['Roots'][0]['Id']
print("\n************************")
print("\nOrganization-Structure: ")
print(f'{root_id}')
for firstlevel in data['Ous']:
try:
response = org.create_organizational_unit(
ParentId=root_id,
Name=firstlevel['Name'])
firstlevelou_id = response['OrganizationalUnit']['Id']
firstlevelname = firstlevel['Name']
logger.info(f'Created OU: {firstlevelou_id} - {firstlevelname} in {root_id}')
print(f' - {firstlevelname}')
except (org.exceptions.DuplicateOrganizationalUnitException):
logger.info('OU already exist')
firstlevelname = firstlevel['Name']
print(f' - {firstlevelname}')
firstlevelou_id = get_ou_id_by_name(firstlevel['Name'], root_id, org)
if firstlevel['Children'] == 'No-Children':
logger.info(f'{firstlevelname} has no No-Children')
else:
for secondlevel in firstlevel['Children']:
try:
response2 = org.create_organizational_unit(
ParentId=firstlevelou_id,
Name=secondlevel['Name'])
secondlevelou_id = response2['OrganizationalUnit']['Id']
secondlevelname = secondlevel['Name']
logger.info(f'Created OU: {secondlevelname} with Id: {secondlevelou_id} {firstlevelname} in {firstlevelou_id}')
print(f' - - {secondlevelname}')
except (org.exceptions.DuplicateOrganizationalUnitException):
secondlevelname = secondlevel['Name']
print(f' - - {secondlevelname}')
secondlevelou_id = get_ou_id_by_name(secondlevel['Name'], firstlevelou_id, org)
if secondlevel['Children'] == 'No-Children':
logger.info(f'{secondlevelname} has no No-Children')
else:
for thirdlevel in secondlevel['Children']:
try:
response3 = org.create_organizational_unit(
ParentId=secondlevelou_id,
Name=thirdlevel['Name'])
thirdlevellevelou_id = response3['OrganizationalUnit']['Id']
thirdlevelname = thirdlevel['Name']
logger.info(f'Created OU: {thirdlevelname} with Id: {thirdlevellevelou_id} {secondlevelname} in {secondlevelou_id}')
print(f' - - - {thirdlevelname}')
except (org.exceptions.DuplicateOrganizationalUnitException):
thirdlevelname = thirdlevel['Name']
print(f' - - - {thirdlevelname}')
thirdlevellevelou_id = get_ou_id_by_name(thirdlevel['Name'], secondlevelou_id, org)
if thirdlevel['Children'] == 'No-Children':
logger.info(f'{thirdlevelname} has no No-Children')
else:
for fourlevel in thirdlevel['Children']:
try:
response4 = org.create_organizational_unit(
ParentId=thirdlevellevelou_id,
Name=fourlevel['Name'])
fourlevelou_id = response4['OrganizationalUnit']['Id']
fourlevelname = fourlevel['Name']
logger.info(f'Created OU: {fourlevelname} with Id: {fourlevelou_id} in {thirdlevellevelou_id}')
print(f' - - - - {fourlevelname}')
except (org.exceptions.DuplicateOrganizationalUnitException):
fourlevelname = fourlevel['Name']
print(f' - - - - {fourlevelname}')
fourlevelou_id = get_ou_id_by_name(fourlevel['Name'], thirdlevellevelou_id, org)
if fourlevel['Children'] == 'No-Children':
logger.info(f'{fourlevelname} has no No-Children')
else:
for fivelevel in fourlevel['Children']: