-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathakamai-audit.py
1368 lines (1141 loc) · 47.5 KB
/
akamai-audit.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 argparse, time,re, os,csv,functools, signal,sys, json
import logging,datetime, threading,concurrent.futures
from logging import handlers
from time import gmtime, strftime
from urllib.parse import urlparse
from os.path import splitext
import pandas as pd
import numpy as np
# Local Imports
from Lib.GCS.wrapper import Wrapper
from Lib.GCS.origin_settings import Origin_Settings
from Lib.GCS.log import ConsoleLogging
def ArgsParser():
parser = argparse.ArgumentParser(description='',formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('--switchKey', type=str, help='Account_ID to Query for multi account management (switch key)',
required=False)
parser.add_argument('--verbose', action='store_true', help='Turn on Verbose Mode.')
parser.add_argument('--section', type=str, help='EdgeRc section to be used.',
required=False,default='papi')
parser.add_argument('--type', type=str.lower, choices=['as','os','har'], help='Type of report to be done [account-summary,offload,http-archive]]',
required=False,default='as')
parser.add_argument('--cpcodes', nargs='+', type=int, help='List of cpcodes to query. Used only in Offload Analysis.',
required=False)
parser.add_argument('--start', type=str, help='Report Start date in format YYYY-MM-DD", if not provided default is start of last month. Used only in Offload Analysis.',
required=False)
parser.add_argument('--end', type=str, help='Report Start date in format YYYY-MM-DD", if not provided default is start of last month. Used only in Offload Analysis.',
required=False)
parser.add_argument('--domain', type=str, help='Main Domain to be reviewed in HAR, usually it will be the same as the page view URL. Used only in Har Analysis.',
required=False)
parser.add_argument('--first-parties', nargs='+', type=str, help='List of first party domains --domain will be appended to this list. If only one domain is in quesion, --domain is all you need. Used only in Har Analysis.',
required=False)
parser.add_argument('--file', type=str, help='File location to be analysed. Used only in Har Analysis.',
required=False)
parser.add_argument('--groupby', type=str.lower, choices=['ext','url'], help='Used only in Offload Analysis. ',
required=False,default='ext')
args = vars(parser.parse_args())
return parser, args
class Aggregator:
def __init__(self,console,args,section_name):
self.args = None
self.parser = None
self.maxThreads = 5
self.outputdir = "None"
self.verbose = args['verbose']
self.log = console.log
self.wrapper = Wrapper(self.log,section_name)
self.accountId = None
self.wrapper.account = None
self.dfs = {}
self.startDate = None
self.endDate = None
self.accountName = None
self.productMap = None
self.reportType = "as"
self.groupby = args['groupby']
signal.signal(signal.SIGINT, self.signal_handler)
def signal_handler(self,sig, frame):
self.clear_cache()
self.log.critical("Forced Exit... Bye!..")
sys.exit(0)
def _validateDate(self, date):
"""Returns False if input date does not follow YYYY-MM-DD.
Keyword arguments:
date
Return type:
Boolean
"""
try:
datetime.datetime.strptime(str(date), '%Y-%m-%d')
return True
except ValueError:
return False
# raise ValueError("Incorrect data format, should be YYYY-MM-DD")
def createFolder(self,directoryName):
"""Creates directores to store outputs, takes the directory name. This value most of the time will be the
account Name.
Keyword arguments:
directoryName
Return type:
None
"""
self.outputdir = 'Reports'
# Create Audit Folder
try:
os.stat(self.outputdir)
except:
os.mkdir(self.outputdir)
self.outputdir = self.outputdir+'/'+directoryName.replace(' ','_')+'/'
# Create Account Folder under Audit
try:
os.stat(self.outputdir)
except:
os.mkdir(self.outputdir)
self.outputdir = self.outputdir + str(datetime.datetime.utcfromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')).replace(' ','_').replace(':','.') + '-'
def _getProducts(self, contractId):
"""
Return the set of products within a contract as a comma seperated list
Keyword arguments:
contractId
Return type:
list
"""
products = self.wrapper.getProducts(contractId)
productNames = []
if 'products' in products:
for product in products['products']['items']:
productNames.append(product['productName'])
new_row = {
'Product_ID':product['productId'],
'Product_Name':product['productName']
}
if len(productNames) > 1:
return ",".join(productNames)
else:
return []
def getAccountDetails(self):
"""
Gets Account Name from ID, also saves the groups for later functions.
Keyword arguments:
None
Return type:
Boolean, but also stores dataframe in self.dfs
"""
if args['switchKey']:
self.accountId = args['switchKey']
self.wrapper.account = args['switchKey']
self.groups = self.wrapper.getGroups()
if 'incidentId' in self.groups:
self.log.error('Account Not Found or insufficient privileges to complete the operation. Try "--section sectionName" o change edgerc section')
return False
if not args['switchKey']:
self.accountId = self.groups['accountId'][4:]
# self.wrapper.account = self.groups['accountId'][4:]
self.log.info("Account ID: {0}".format(self.accountId))
self.accountName = self.groups['accountName']
self.log.info("Account Name: {0}".format(self.accountName))
csv_file_path = self.createFolder(self.groups['accountName'])
columns = ["Account_Id", "Account_Name"]
df_acc= pd.DataFrame(columns=columns)
new_row = {
'Account_Id':self.groups['accountId'][4:],
'Account_Name':self.groups['accountName']
}
df_acc=df_acc.append(new_row, ignore_index=True)
self.dfs['account'] = df_acc
self._readProductMap()
return True
def accountSummary(self):
"""
Main function for AS report type, orchestrates function execution.
Keyword arguments:
None
Return type:
None
"""
self.log.info("Creating Contract summary table")
self.printContracts()
self.log.info("Creating Groups summary table")
self.printGroups()
self.log.info("Creating CP_Code summary table")
self.printCPcodes()
self.log.info("Creating edge host name summary table")
self.printEdgeHostNames()
self.log.info("Creating Application Security tables")
self.printAppSec()
if args['verbose']:
self.log.info("Creating Property summary. (It may take a while) ")
else:
self.log.info("Creating Property summary. (It may take a while, view more with '--verbose') ")
self.printPropertiesDetails()
self.log.info("Creating Certificate Table.")
self.getEnrollments()
self.log.info("Creating Summary by Hostname")
self.presentation()
self.log.info("Writing Files...")
self._writeFiles()
self.log.info("Report successfull, output can be found here:'Reports/{0}/'".format(self.accountName))
def printContracts(self):
"""
Gets Contracts within Account
Keyword arguments:
None
Return type:
None, but stores dataframe in self.dfs
"""
self.log.info("Creating Contracts table.")
columns = ["Contract_ID" , "Contract_Name", "Products"]
df_ctr= pd.DataFrame(columns=columns)
contracts = self.wrapper.getContractNames()
for contract in contracts['contracts']['items']:
products = self._getProducts(contract['contractId'])
self.log.debug("Found contract: '{0}' - '{1}'".format(contract['contractId'][4:], contract['contractTypeName']))
new_row = {
'Contract_ID': contract['contractId'][4:],
'Contract_Name':contract['contractTypeName'],
'Products':products
}
df_ctr=df_ctr.append(new_row, ignore_index=True)
self.dfs['contracts'] = df_ctr
def printGroups(self):
"""
Gets Groups in account
Keyword arguments:
None
Return type:
None, but stores dataframe in self.dfs
"""
self.log.info("Creating Groups table.")
columns = ["Group_ID", "Group_Name","Parent"]
df_grp = pd.DataFrame(columns=columns)
for group in self.groups['groups']['items']:
grp_id = int(group['groupId'][4:])
grp_name = group['groupName']
grp_parent = None
if 'parentGroupId' in group:
grp_parent = int(group['parentGroupId'][4:])
new_row = {
'Group_ID': grp_id,
'Group_Name':grp_name,
'Parent':grp_parent
}
df_grp=df_grp.append(new_row, ignore_index=True)
self.dfs['groups'] = df_grp
def printEdgeHostNames(self):
"""
Gets EdgeHostnames in account
Keyword arguments:
None
Return type:
None, but stores dataframe in self.dfs
"""
lst_eh = []
columns = ["Group_ID", "Contract_ID", "Edge_Host_ID", "Edge_Host_Name", "Edge_Host_Domain_Suffix", "Secure", "IPVersion","Product_ID","Map","Slot"]
df_eh = pd.DataFrame(columns=columns)
contracts = []
with concurrent.futures.ThreadPoolExecutor(max_workers=self.maxThreads) as executor:
for group in self.groups['groups']['items']:
groupId = group['groupId']
executor.submit(self.GroupsWorker,'edgehost',group,lst_eh,contracts)
df_eh= df_eh.append(lst_eh, ignore_index=True)
self.dfs['edgehostnames'] = df_eh
def PropertyWorker(self,list_grp_configs,list_grp_behaviors,config_details):
"""
Gets Property details,
Keyword arguments:
list_grp_configs
list_grp_behaviors
config_details
Return type:
None, but stores dataframe in self.dfs
"""
args = None
args = ['Prod_Version','Staging_Version', 'Latest_Version']
if 'propertyName' in config_details:
self.log.debug("Importing data for property: '{0}'".format(config_details['propertyName']))
# Assign values to variables here for readability and will be used in rest of function.
groupId = config_details['groupId']
contractId = config_details['contractId']
propertyId = config_details['propertyId']
productionVersion = config_details['productionVersion']
stgVersion = config_details['stagingVersion']
latestVersion = config_details['latestVersion']
productId = None
new_row = {
'Config_Name': config_details['propertyName'],
'Group_ID': int(groupId[4:]),
'Contract_ID': contractId[4:],
'Property_ID': int(propertyId[4:]),
'Prod_Version': productionVersion,
'Staging_Version': stgVersion,
'Latest_Version': latestVersion,
'Product': productId
}
if args:
for config_env in args:
config_version = new_row[config_env]
if config_version is not None:
get_version = self.wrapper.getVersionDetails(propertyId,groupId,contractId,str(config_version))
if 'versions' in get_version:
for item in get_version['versions']['items']:
new_row[config_env + '_Updated_User'] = item['updatedByUser']
new_row[config_env + '_Updated_Time'] = item['updatedDate']
if productId == None:
productId = item['productId'][4:]
else:
new_row[config_env + '_Updated_User'] = 'No_' + config_env
new_row[config_env + '_Updated_Time'] = 'No_' + config_env
new_row['Product'] = productId
version = new_row['Latest_Version']
if ('Prod_Version' in new_row) and (new_row['Prod_Version'] is not None):
version = new_row['Prod_Version']
else:
if ('Staging_Version' in new_row) and (new_row['Staging_Version'] is not None):
version = new_row['Staging_Version']
new_row['Hostnames'] = self.getPropertyHostDetails(new_row['Group_ID'],new_row['Contract_ID'],new_row['Property_ID'], str(version))
new_row['Origins'] = self.getPropertyOriginDetails(new_row['Group_ID'],new_row['Contract_ID'],new_row['Property_ID'], str(version))
new_row['Behaviors'] = self.getBehaviorDetails()
new_row['CP_Codes'] = '['+self.getCPCodeDetails()+']'
property_behaviors = new_row['Behaviors']
list_grp_configs.append(new_row)
if productionVersion is not None:
propertyVersion = productionVersion
elif stgVersion is not None:
propertyVersion = stgVersion
else :
propertyVersion = latestVersion
available_behaviors = self.wrapper.getavailableBehavior(propertyId, str(propertyVersion),contractId, groupId)
if 'behaviors' in available_behaviors:
for b in available_behaviors['behaviors']['items']:
enabled = False
if b['name'] in property_behaviors:
enabled = True
new_row = {
'Config_Name': config_details['propertyName'],
'Behaviors': b['name'],
'Enabled': enabled
}
list_grp_behaviors.append(new_row)
return
def GroupsWorker(self, workType,group,main_list=None,second_list=None):
"""
Worker for multithreads for property functions, cpcode functions, edgehosts due to high number of groups per
account,
Keyword arguments:
workType <= Type of function to be execute [property, cpcode , edgehosts]
group <= Dataframe containing list of account groups
main_list <= list passed down by maint thread to append results
second_list <= secondary list passed down by main thread to append results
Return type:
None
"""
groupId = group['groupId']
groupName = group['groupName']
if 'contractIds' in group:
for contractId in group['contractIds']:
if workType == 'properties':
location_result = self.wrapper.getProperties(groupId, contractId)
if 'properties' in location_result:
with concurrent.futures.ThreadPoolExecutor(max_workers=self.maxThreads) as executor:
for config_details in location_result['properties']['items']:
executor.submit(self.PropertyWorker,main_list,second_list,config_details)
elif workType == 'cpcodes':
cpcodes = self.wrapper.getCPCodes(groupId, contractId)
with concurrent.futures.ThreadPoolExecutor(max_workers=self.maxThreads) as executor:
for cp in cpcodes['cpcodes']['items']:
products = []
for product in cp['productIds']:
products.append(product[4:])
new_row = {
'Group_ID': int(groupId[4:]),
'Contract_ID': contractId[4:],
'CP_Code_ID': int(cp['cpcodeId'][4:]),
'CP_Code_Name': cp['cpcodeName'],
'CP_Code_Products': "|".join(products)
}
if new_row not in main_list:
self.log.debug("Fetched data for CPcode: '{0}'".format(cp['cpcodeId'][4:]))
main_list.append(new_row)
elif workType == 'edgehost':
if 'contractIds' in group:
for contractId in group['contractIds']:
if contractId in second_list:
break
second_list.append(contractId)
edgeHostNames = self.wrapper.getEdgeHostNames(groupId, contractId,'hapi')
for edgeHostName in edgeHostNames['edgeHostnames']:
slot = None
if 'slotNumber' in edgeHostName:
slot = edgeHostName['slotNumber']
productID = None
if 'productId' in edgeHostName:
productID = edgeHostName['productId']
IPv = None
if 'ipVersionBehavior' in edgeHostName:
IPv = edgeHostName['ipVersionBehavior']
eMap = None
if 'map' in edgeHostName:
eMap = edgeHostName['map']
new_row = {
'Group_ID': int(groupId[4:]),
'Contract_ID': contractId[4:],
'Edge_Host_ID': edgeHostName['edgeHostnameId'],
'Edge_Host_Name': edgeHostName['recordName']+'.'+edgeHostName['dnsZone'],
"Edge_Host_Domain_Suffix":edgeHostName['dnsZone'],
"Secure":edgeHostName['securityType'],
"IPVersion":IPv,
"Product_ID":productID,
"Map":eMap,
"Slot":slot
}
main_list.append(new_row)
self.log.debug("Fetched configs for group: '{0}' - '{1}'".format(groupId[4:], groupName))
return None
def printCPcodes(self):
"""
orchestrates mutlithreading by using the GroupsWorker function to populate CPcode data
Keyword arguments:
None
Return type:
None
"""
lst_cpcodes = []
columns = ["Group_ID", "Contract_ID", "CP_Code_ID", "CP_Code_Name", "CP_Code_Products"]
df_cpcodes = pd.DataFrame(columns=columns)
with concurrent.futures.ThreadPoolExecutor(max_workers=self.maxThreads) as executor:
for group in self.groups['groups']['items']:
groupId = group['groupId']
executor.submit(self.GroupsWorker,'cpcodes',group,lst_cpcodes)
df_cpcodes= df_cpcodes.append(lst_cpcodes, ignore_index=True)
self.dfs['cpcodes'] = df_cpcodes
def printPropertiesDetails(self, *args):
"""
orchestrates mutlithreading by using the GroupsWorker function to populate property data
Return type:
None
"""
self.log.debug('Start time is {0}'.format(strftime("%Y-%m-%d %H:%M:%S", gmtime())))
self.log.debug('generating config data.....')
columns = [
"Config_Name",
"Group_ID",
"Contract_ID",
"Property_ID",
"Prod_Version",
"Staging_Version",
"Latest_Version",
"Product",
"Prod_Version_Updated_User",
"Prod_Version_Updated_Time",
"Staging_Version_Updated_User",
"Staging_Version_Updated_Time",
"Latest_Version_Updated_User",
"Latest_Version_Updated_Time",
"Hostnames",
"Origins",
"Behaviors",
"CP_Codes"
]
list_properties = []
list_behavior = []
df_property = pd.DataFrame(columns=columns)
with concurrent.futures.ThreadPoolExecutor(max_workers=self.maxThreads) as executor:
for group in self.groups['groups']['items']:
executor.submit(self.GroupsWorker,'properties',group,list_properties,list_behavior)
df_property= df_property.append(list_properties, ignore_index=True)
tmp = df_property[ ['Config_Name' ,
'Property_ID',
"Group_ID",
"Contract_ID",
"Product" ,
"Prod_Version",
"Prod_Version_Updated_User",
"Prod_Version_Updated_Time",
"Latest_Version",
"Latest_Version_Updated_User",
"Latest_Version_Updated_Time",
"Staging_Version" ,
"Staging_Version_Updated_User" ,
"Staging_Version_Updated_Time",
"Behaviors",
"CP_Codes"
]]
self.log.debug('properties.csv generated')
self.dfs['properties']=tmp
columns = ["Config_Name", "Behaviors", "Enabled"]
df_behaviors = pd.DataFrame(columns=columns)
df_behaviors= df_behaviors.append(list_behavior, ignore_index=True)
self.dfs['propertiesBehaviors']=df_behaviors
self.log.debug('properties_behaviors.csv generated')
self.log.debug('Now fetching origin details...')
columns = ["Config_Name","Property_ID", "Group_ID", "Contract_ID","Origin_Host_Name", "Origin_Type"]
df_origins = pd.DataFrame(columns=columns)
for index, row in df_property.iterrows():
for o in row['Origins']:
new_row = {
'Config_Name':row['Config_Name'],
'Property_ID':row['Property_ID'],
'Group_ID':row['Group_ID'],
'Contract_ID':row['Contract_ID'],
'Origin_Host_Name':o['hostname'],
'Origin_Type':o['originType']
}
df_origins = df_origins.append(new_row, ignore_index=True)
self.dfs['origins'] = df_origins
self.log.debug('origins.csv generated')
self.log.debug('Fetching Origin details is now complete')
self.printPropertyHostNames(df_property)
return
@functools.lru_cache()
def _resource_path(self,grp_id, grp_path=None):
"""
Creates a directory like structure groups, to visualize resource location.
Keyword arguments:
grp_id
grp_path
Return type:
grp_path <= Resource Path within Account
"""
grp_id = int(grp_id)
grp_parent = self.groups[self.groups['Group_ID']== grp_id]['Parent'].item()
if grp_path == None:
grp_path = self.groups[self.groups['Group_ID']== grp_id]['Group_Name'].item()
else:
grp_path = "{0} > {1}".format(self.groups[self.groups['Group_ID']== grp_id]['Group_Name'].item(),grp_path)
if grp_parent != "None" and grp_parent != None and not np.isnan(grp_parent):
grp_path = self._resource_path(grp_parent,grp_path)
return grp_path
def printPropertyHostNames(self, df_property):
# now write the host name details
columns = ["Host_Name", "Defined_CNAMED", "Actual_CNAME"
, "Secure", "Akamaized","Slot","Config_Name","Property_ID", "Group_ID", "Contract_ID"]
df_hosts = pd.DataFrame(columns=columns)
for index, row in df_property.iterrows():
for host in row['Hostnames']:
new_row = {
'Host_Name':host['host'],
'Defined_CNAMED':host['cname_defined'],
'Actual_CNAME':host['cname_actual'],
'Secure':host["secure"],
'Akamaized':host["akamaized"],
'Slot':host['slot'],
'Config_Name':row['Config_Name'],
'Property_ID':int(row['Property_ID']),
'Group_ID':int(row['Group_ID']),
'Contract_ID':row['Contract_ID']
}
df_hosts = df_hosts.append(new_row, ignore_index=True)
self.dfs['hostnames']=df_hosts
def getPropertyHostDetails(self, groupId, contractId, propertyId, propertyVersion):
"""
for the property, get the host names, origin names and if the host names are CNAMED to Akamai
Keyword arguments:
grp_id
contractId
propertyId
propertyVersion
Return type:
hostnames
"""
hostdetailsJSON = self.wrapper.getPropertyHostNames(propertyId, propertyVersion, groupId, contractId)
hostnames = []
if 'hostnames' in hostdetailsJSON:
for hostname in hostdetailsJSON['hostnames']['items']:
host = ""
cname_defined = ""
if 'cnameFrom' in hostname:
host = hostname['cnameFrom']
if 'cnameTo' in hostname:
cname_defined = hostname['cnameTo']
cname_actual = str(self.getCNAME(host))
slot = None
# TODO: Not working properly
if cname_actual == "None":
isAkamaized = "Unknown"
secureHostName = "Unknown"
slot = "None"
else:
isAkamaized = self._isAkamaized(cname_actual)
secureHostName = self._isESSL(cname_actual)
if secureHostName is None:
slot = "None"
secureHostName = False
else:
slot = self.checkSlot(host)
secureHostName = True
new_row = { 'host': host,
'cname_defined': cname_defined,
'cname_actual': cname_actual,
'secure' : secureHostName,
'slot': slot,
'akamaized': isAkamaized
}
hostnames.append(new_row)
return hostnames
def getPropertyOriginDetails(self, groupId, contractId, propertyId, propertyVersion):
"""
Finds Origins from property and defines origin Type
returns
origin_details
"""
self.rules = self.wrapper.getConfigRuleTree(propertyId, propertyVersion, groupId, contractId)
self.origin = Origin_Settings()
origin_details = self.origin.findOrigins(self.rules)
#replace origin for GTM with the word GTM
for origin in origin_details:
if origin['hostname'].endswith('akadns.net'):
origin['originType'] = 'GTM'
return origin_details
def getPropertyCPCodeDetails(self, groupId, contractId, propertyId, propertyVersion):
self.cpcodes = Origin_Settings()
origin_details = self.cpcodes.findOrigins(self.rules, 'cpCode')
# now get the property's product type
return origin_details
def getEnrollments(self):
"""
get a list enrollments using CPS API for a contract and returns a list of enrollments
"""
contracts = self.wrapper.getContractNames()
columns = ["Contract_ID", "Common_Name","Enrollment_ID" ,"Slots","ALT_names", "MustHave_Ciphers", "Preferred_Ciphers", "Deployment_Location", "Certifcate_Type" , "Certifcate_Authority"]
df_certs = pd.DataFrame(columns=columns)
#TODO: print ciphers
for contract in contracts['contracts']['items']:
enrollment_results = self.wrapper.getEnrollements(contract['contractId'][4:])
if enrollment_results is not None:
if 'enrollments' in enrollment_results:
if len(enrollment_results['enrollments']) >0:
for i in enrollment_results['enrollments']:
self.log.info("Getting Enrollment ID '{0}'".format(str(i['location']).split('/')[4]))
Enrollment_ID = str(i['location']).split('/')[4]
new_row = {
'Contract_ID':contract['contractId'][4:],
'Common_Name':i['csr']['cn'],
'Enrollment_ID':int(Enrollment_ID),
'Slots': self.getSlotId(Enrollment_ID),
'ALT_names':i['csr']['sans'],
'MustHave_Ciphers':i['networkConfiguration']['mustHaveCiphers'],
'Preferred_Ciphers':i['networkConfiguration']['preferredCiphers'],
'Deployment_Location':i['networkConfiguration'],
'Certifcate_Authority':i['ra'],
'Certifcate_Type':i['certificateType']
}
df_certs = df_certs.append(new_row, ignore_index=True)
self.dfs['certs'] = df_certs
def getSlotId(self,enrollementID):
Enrollment = self.wrapper.getEnrollmentHistory(enrollementID)
slots = None
for c in Enrollment['certificates']:
if c['deploymentStatus'] == 'active':
slots = int(str(c['slots']).replace('[', '').replace(']', ''))
break
return slots
def printMatchTargets(self,matchTargets):
columns = ["Target_ID", "Type", "Config_ID", "Config_Version", "Default_File", "File_Paths", "APIs","Hostnames","Security_Policy", "Sequence"]
df_secMatch = pd.DataFrame(columns=columns)
for mt in matchTargets:
for webTarget in mt['matchTargets']['websiteTargets']:
mtype = None
if 'type' in webTarget: mtype = webTarget['type']
hostnames = None
if 'hostnames' in webTarget: hostnames = webTarget['hostnames']
configId = None
if 'configId' in webTarget: configId = webTarget['configId']
configVersion = None
if 'configVersion' in webTarget: configVersion = webTarget['configVersion']
defaultFile = None
if 'defaultFile' in webTarget: defaultFile = webTarget['defaultFile']
filePaths = None
if 'filePaths' in webTarget: filePaths = webTarget['filePaths']
targetId = None
if 'targetId' in webTarget: targetId = webTarget['targetId']
securityPolicy = None
if 'securityPolicy' in webTarget: securityPolicy = webTarget['securityPolicy']
sequence = None
if 'sequence' in webTarget: sequence = webTarget['sequence']
new_row = {
"Target_ID":targetId,
"Type":mtype,
"Config_ID":configId,
"Config_Version":configVersion,
"Default_File":defaultFile,
"File_Paths":filePaths,
"APIs":None,
"Hostnames":hostnames,
"Security_Policy":securityPolicy,
"Sequence":sequence
}
df_secMatch = df_secMatch.append(new_row, ignore_index=True)
self.dfs['secMatch'] = df_secMatch
return None
def printAppSec(self):
secConfigs = self.getSecConfigs()
matchTargets = []
columns = ["AppSec_Config_Name", "AppSec_Config_ID", "AppSec_Type", "AppSec_Target_Product", "AppSec_Hostnames", "AppSec_Production_Version", "AppSec_Staging_Version"]
df_configs = pd.DataFrame(columns=columns)
for secConfig in secConfigs['configurations']:
version = secConfig['latestVersion']
stg_version = None
prod_version = None
lst_version = None
prodHostnames = None
if ('productionVersion' in secConfig) and (secConfig['productionVersion'] is not None):
version = secConfig['productionVersion']
else:
if ('stagingVersion' in secConfig) and (secConfig['stagingVersion'] is not None):
version = secConfig['stagingVersion']
stg_version = secConfig['stagingVersion']
if 'productionVersion' in secConfig:
prod_version = secConfig['productionVersion']
if 'stagingVersion' in secConfig:
stg_version = secConfig['stagingVersion']
if 'latestVersion' in secConfig:
lst_version = secConfig['latestVersion']
if 'productionHostnames' in secConfig:
prodHostnames = secConfig['productionHostnames']
matchTargets.append(self.getSecMatchTargets(secConfig['id'],version ))
name = None
if 'name' in secConfig:
name = secConfig['name']
new_row = {
'AppSec_Config_Name':name,
'AppSec_Config_ID':secConfig['id'],
'AppSec_Type':secConfig['fileType'],
'AppSec_Target_Product':secConfig["targetProduct"],
'AppSec_Hostnames':prodHostnames,
'AppSec_Production_Version':prod_version,
'AppSec_Staging_Version':stg_version
}
df_configs = df_configs.append(new_row, ignore_index=True)
self.dfs['secConfigs'] = df_configs
self.printMatchTargets(matchTargets)
columns = ["Host_Name","AppSec_Config_Name", "AppSec_Config_ID", "AppSec_Type",
"AppSec_Target_Product", "AppSec_Production_Version","AppSec_Policy"]
df_configByHost = pd.DataFrame(columns=columns)
for secConfig in secConfigs['configurations']:
if 'productionHostnames' in secConfig:
for host in secConfig["productionHostnames"]:
name = None
mtype = None
configId = None
configVersion = None
defaultFile = None
filePaths = []
targetId = []
securityPolicies = "Not Protected"
if 'name' in secConfig:
name = secConfig['name']
for mt in matchTargets:
for webTarget in mt['matchTargets']['websiteTargets']:
if secConfig['id'] != webTarget['configId']:
continue
if 'hostnames' in webTarget:
if host not in webTarget['hostnames']:
continue
if securityPolicies == "Not Protected":
for sp in webTarget['securityPolicy']:
securityPolicies = []
securityPolicies.append(webTarget['securityPolicy']['policyId'])
elif 'securityPolicy' in webTarget:
for sp in webTarget['securityPolicy']:
if webTarget['securityPolicy'] not in securityPolicies:
if securityPolicies == "Not Protected":
securityPolicies = []
securityPolicies.append(webTarget['securityPolicy']['policyId'])
new_row = {
'Host_Name':host,
'AppSec_Config_Name':name,
'AppSec_Config_ID':secConfig['id'],
'AppSec_Type':secConfig['fileType'],
'AppSec_Target_Product':secConfig["targetProduct"],
'AppSec_Production_Version':secConfig["productionVersion"],
'AppSec_Policy':securityPolicies
}
df_configByHost = df_configByHost.append(new_row, ignore_index=True)
self.dfs['secConfigByHost'] = df_configByHost
return
def presentation(self,path=None):
#TODO: FiX: change product from ID to name
if path:
self.outputdir = path
properties = self.dfs['properties']
self.groups = self.dfs['groups']
hostnames = self.dfs['hostnames']
secbyHost = self.dfs['secConfigByHost']
dat = hostnames.merge(self.groups , on='Group_ID').fillna("None")
dat = hostnames.merge(properties[['Config_Name', 'Product', 'Prod_Version','Staging_Version']], on='Config_Name',how='left').fillna("None")
dat = dat.merge(secbyHost,on='Host_Name',how='left').fillna('Not Protected')
dat['Resource_Path'] = dat['Group_ID'].apply(self._resource_path)
dat = dat.rename(columns={"Product": "Product_ID"})
dat['Product'] = dat['Product_ID'].apply(self._translateProductID)
dat = dat[['Host_Name','Defined_CNAMED', 'Actual_CNAME', 'Secure','Slot', 'Akamaized', 'Group_ID','Resource_Path', 'Contract_ID', 'Config_Name', 'Property_ID', 'Product_ID', 'Product', 'Prod_Version', 'Staging_Version', 'AppSec_Config_Name', 'AppSec_Config_ID', 'AppSec_Type', 'AppSec_Target_Product', 'AppSec_Production_Version', 'AppSec_Policy', 'AppSec_Target_Product']]
self.dfs['ByHost'] = dat
def _readProductMap(self):
if self.productMap is None:
with open('Lib/GCS/productMap.json') as f:
self.productMap = json.load(f)
def mineHar(self,har,lst_firstparty):
columns = ['url','host','host-type','protocol','method','status','ext','cpcode','ttl','server',
'cdn-cache','cdn-cache-parent','cdn-cache-key','cdn-req-id','vary','appOrigin','content-type','content-length',
'content-length-origin','transfer-size','content-size','blocked','dns','ssl','connect','send','ttfb','receive',
'edgeTime','originTime']
dat_clean = pd.DataFrame(columns=columns)
for r in har['log']['entries']:
u = str(r['request']['url']).split('?')[0]
host = re.search('://(.+?)/', u, re.IGNORECASE).group(0).replace(':','').replace('/','')
cachekey = str(self._findHeader(r,'response','x-cache-key','eq'))
if not cachekey == 'None':
cachekey = cachekey.split('/')
cpcode = int(cachekey[3])
ttl = cachekey[4]
cdnCache = str(self._findHeader(r,'response','x-cache','eq')).split(' ')[0]
cdnCacheParent = str(self._findHeader(r,'response','x-cache-remote','eq')).split(' ')[0]
origin = str(self._findHeader(r,'response','x-cache-key','eq')).split('/')[5]
else:
cachekey = "None"
cpcode = "None"
ttl = "None"
cdnCache = "None"
cdnCacheParent = "None"
origin = "None"
ext = re.search(r'(\.[A-Za-z0-9]+$)', u, re.IGNORECASE)
if any(tld in host for tld in lst_firstparty):
hostType = 'First Party'
edgeTime = self._findHeader(r,'cdn-timing','edge','eq')
originTime = self._findHeader(r,'cdn-timing','origin','eq')
else:
hostType = 'Third Party'
edgeTime = -1
originTime = -1
if ext is None:
ext = "None"
else:
ext = ext.group(0).replace('.','')
ct = self._findHeader(r,'response','content-length','eq')
if ct == "None":
ct = 0
else:
ct = int(ct)
if ext in ['jpg','png']:
ct_origin = self._findHeader(r,'response','x-im-original-size','eq')
else:
ct_origin = self._findHeader(r,'response','x-akamai-ro-origin-size','eq')
if ct_origin == "None":
ct_origin = 0
else:
ct_origin = int(ct_origin)
new_row = {
'url':u,
'host':host,
'host-type':hostType,
'protocol':r['request']['httpVersion'],
'method':r['request']['method'],
'status':r['response']['status'],
'ext':ext,
'cpcode':cpcode,
'ttl':ttl,
'server':str(self._findHeader(r,'response','server','eq')),
'cdn-cache':cdnCache,
'cdn-cache-parent':cdnCacheParent,
'cdn-cache-key':str(self._findHeader(r,'response','x-true-cache-key','eq')),
'cdn-req-id':str(self._findHeader(r,'response','x-akamai-request-id','eq')),
'vary':str(self._findHeader(r,'response','vary','eq')),
'appOrigin':origin,
'content-type':str(self._findHeader(r,'response','content-type','eq')),
'content-length':ct,
'content-length-origin':ct_origin,
'transfer-size':r['response']['_transferSize'],
'content-size':r['response']['content']['size'],
'blocked':r['timings']['blocked'],
'dns':r['timings']['dns'],
'ssl':r['timings']['ssl'],
'connect':r['timings']['connect'],
'send':r['timings']['send'],
'ttfb':r['timings']['wait'],
'receive':r['timings']['receive'],
'edgeTime':edgeTime,
'originTime':originTime
}
dat_clean = dat_clean.append(new_row,ignore_index=True)