-
Notifications
You must be signed in to change notification settings - Fork 3
/
format-egress-ips.py
558 lines (420 loc) · 21.7 KB
/
format-egress-ips.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
# Formats the json output to get all the egress IPs
# Author: TheScriptGuy
# Last modified: 2023-04-20
# Version: 0.13
# Changelog:
# Added some better error handling in case a json object is not returned.
import sys
import json
import argparse
from os import path
import os.path
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
scriptVersion = "0.13"
API_KEY_FILE = 'prisma-access-api.key'
# All Public IP addresses
EgressIPs = {"serviceType": "all", "addrType": "all", "location": "all"}
# Mobile Users IP Addresses
ActiveReservedOnboardedMobileUserLocations = {"serviceType": "gp_gateway", "addrType": "all", "location": "deployed"}
ActiveIPOnboardedMobileUserLocations = {"serviceType": "gp_gateway", "addrType": "active", "location": "deployed"}
ActiveMobileUserAddresses = {"serviceType": "gp_gateway", "addrType": "all", "location": "all"}
# Remote Network IP addresses
RemoteNetworkAddresses = {"serviceType": "remote_network", "addrType": "all", "location": "all"}
# Clean Pipe IP addresses
CleanPipeAddresses = {"serviceType": "clean_pipe", "addrType": "all", "location": "all"}
# Explicit Proxy IP addresses
ExplicitProxyAddresses = {"serviceType": "swg_proxy", "location": "deployed", "addrType": "auth_cache_service"}
# Loopback IP
addressType = "loopback_ip"
def parseArguments():
"""Create argument options and parse through them to determine what to do with script."""
# Instantiate the parser
global scriptVersion
parser = argparse.ArgumentParser(description='Format Egress IPs ' + scriptVersion)
# Optional arguments
parser.add_argument('--fileName', default='',
help='List of json formatted egress IPs to convert.')
parser.add_argument('--setAPIKey', default='',
help='Sets the API key into prisma-access-api.key file')
parser.add_argument('--showAPIKey', action='store_true',
help='Shows the Prisma Access API Key from the prisma-access-api.key file.')
parser.add_argument('--deleteAPIKey', action='store_true',
help='Deletes the Prisma Access API Key from prisma-access-api.key file.')
parser.add_argument('--apiKey', default='',
help='Use stdin to enter API Key')
parser.add_argument('--environment', default='prod',
help='By default, script queries prod environment.')
parser.add_argument('--allEgressIPs', action='store_true',
help='Shows all egress IPs for Prisma Access Service')
parser.add_argument('--allAROnboardedMobileUserLocations', action='store_true',
help='Retrieve all the active/reserved IP addresses for Mobile User Locations')
parser.add_argument('--allActiveIPOnboardedMobileUserLocations', action='store_true',
help='Retrieve all the active Mobile Users IP addresses')
parser.add_argument('--allActiveMobileUserAddresses', action='store_true',
help='Shows all Active Mobile User Addresses')
parser.add_argument('--allRemoteNetworkAddresses', action='store_true',
help='Shows all Remote Network Addresses')
parser.add_argument('--allCleanPipeAddresses', action='store_true',
help='Shows all Clean Pipe Addresses')
parser.add_argument('--allExplicitProxyAddresses', action='store_true',
help='Shows all Clean Pipe Addresses')
parser.add_argument('--allLoopbackIPAddresses', action='store_true',
help='Retrieves all the loopback addresses for each location.')
parser.add_argument('--outputJsonFile', default='',
help='Send json output to file.')
parser.add_argument('--outputCsvFile', default='',
help='Convert json output into comma separated values file.')
parser.add_argument('--outputEdlFile', default='',
help='Convert json into external dynamic list file.')
global args
args = parser.parse_args()
def setAPIKey(__APIKey):
"""This will set the API key to be used by the script."""
with open(API_KEY_FILE, 'w') as f_apikey:
f_apikey.write(__APIKey)
print('Success')
sys.exit(0)
def delAPIKey():
"""Delete the API key."""
if path.exists(API_KEY_FILE):
os.remove(API_KEY_FILE)
print('Success')
else:
print('API Key file does not exist.')
sys.exit(1)
def getAPIKey():
"""Get the API key from the API_KEY_FILE file."""
if args.apiKey:
__APIKey = args.apiKey
else:
if path.exists(API_KEY_FILE):
with open(API_KEY_FILE) as f_apikey:
__APIKey = f_apikey.readline().rstrip('\n')
else:
print('Cannot find key file. Please define with the --setAPIKey argument or use --apiKey argument and pass through stdin.')
sys.exit(1)
return __APIKey
def jsonConvert2Csv(__csvFile, __dataObject):
"""Convert Json object to into csv file format."""
__myDataObject = __dataObject
if not isinstance(__myDataObject, list):
# __dataObject is not a list (legacy loopback IP addresses)
if "status" in __myDataObject and __myDataObject["status"] == "success":
# Open the csv file name
# file contents will be overwritten.
with open(__csvFile, 'w') as csv:
# CSV headers
csvHeaders = ["Location", "serviceType", "egress IP", "Active/Reserved"]
# Write the CSV headers to the file.
csv.write(','.join(f'"{w}"' for w in csvHeaders) + '\n')
# Iterate through the json object and write the output into the csv file
for objEgressIps in __myDataObject["result"]:
for obj in objEgressIps["address_details"]:
strToWrite = [objEgressIps["zone"], obj["serviceType"], obj["address"], obj["addressType"]]
csv.write(','.join(f'"{w}"' for w in strToWrite) + '\n')
else:
print("This is not a valid json object to convert.")
sys.exit(1)
else:
# __myDataObject is a list (legacy loopback IP addresses)
with open(__csvFile, 'w') as csv:
# CSV Headers
csvHeaders = ["Type", "Location", "Loopback IP"]
csv.write(','.join(f'"{w}"' for w in csvHeaders) + '\n')
while len(__myDataObject) != 0:
dataItem = __myDataObject.pop()
print(dataItem)
if "status" in dataItem and dataItem["status"] == "success":
# Iterate through the json object and write the output to csv file.
for loopbackItem in dataItem["result"]["addrList"]:
locationInfo = loopbackItem.split(":")
strToWrite = [dataItem["result"]["fwType"], locationInfo[0], locationInfo[1]]
print(strToWrite)
csv.write(','.join(f'"{w}"' for w in strToWrite) + '\n')
def printJsonObject(__jsonObject):
"""Output the Json object in a tabulated format to stdout"""
try:
if isinstance(__jsonObject, list):
if "message" in __jsonObject[0] and __jsonObject[0]["message"] == "Unauthorized":
print("Unauthorized API usage. Using wrong environment or API key perhaps?")
sys.exit(1)
# Table looks a little different for loopback_ips
tableString = '{: <15}{: <20}{: <15}'
# Print headers
print(tableString.format("Type", "Location", "Loopback IP"))
# Iterate through the json object and print accordingly.
while len(__jsonObject) != 0:
jsonItem = __jsonObject.pop()
if "status" in jsonItem and jsonItem["status"] != "error":
for item in jsonItem["result"]["addrList"]:
locationInfo = item.split(":")
print(tableString.format(jsonItem["result"]["fwType"], locationInfo[0], locationInfo[1]))
else:
if "status" in __jsonObject and __jsonObject["status"] != "error":
# Set the table string format.
tableString = '{: <20}{: <18}{: <18}{: <18}'
print(tableString.format("Location", "serviceType", "egress IP", "Active/Reserved"))
# Iterate through the json object and print accordingly.
for objEgressIps in __jsonObject["result"]:
for obj in objEgressIps["address_details"]:
print(tableString.format(objEgressIps["zone"], obj["serviceType"], obj["address"], obj["addressType"]))
else:
# First check to see if the __jsonObject has an unauthorized message.
if "message" in __jsonObject and __jsonObject["message"] == "Unauthorized":
print("Unauthorized API usage. Using wrong environment or API key perhaps?")
sys.exit(1)
except TypeError:
print("Are you sure this is the right JSON object?")
print(__jsonObject)
sys.exit(1)
def getJsonObject(__jsonFile):
"""Get the json object from the __jsonFile object."""
if not path.exists(__jsonFile):
print('I cannot find file ' + __jsonFile)
sys.exit(1)
else:
f = open(__jsonFile)
egressIps = json.load(f)
f.close()
return egressIps
def getJsonObjectFromUrl(__jsonurl, __uriheaders, __uribody):
"""Disable certificate checks when establishing a connection."""
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
try:
if not args.allLoopbackIPAddresses:
# Json POST request
__jsonRequest = requests.post(__jsonurl, headers=__uriheaders, data=json.dumps(__uribody), verify=False)
# Convert result into a JSON object
__jsonObject = json.loads(__jsonRequest.text)
else:
__legacyPrismaAccessURI = __jsonurl + f'fwType={__uribody}&addrType={addressType}'
# Json GET request
__jsonRequest = requests.get(__legacyPrismaAccessURI, headers=__uriheaders, verify=False)
# Convert result into a JSON object
__jsonObject = json.loads(__jsonRequest.text)
return __jsonObject
except requests.exceptions.Timeout:
print('Timeout while retrieving URL.')
sys.exit(1)
except requests.exceptions.TooManyRedirects:
print('Too many redirects while accessing the URL.')
sys.exit(1)
except requests.exceptions.ConnectionError:
print('Could not connect to URL - ' + __jsonurl + '\n')
sys.exit(1)
except json.decoder.JSONDecodeError:
print('Not a valid json object returned.')
sys.exit(1)
def outputJsonFile(__jsonFileName, __jsonObject):
"""Write the JSON object into the __jsonFileName file."""
with open(__jsonFileName, 'w') as jsonFile:
jsonFile.write(json.dumps(__jsonObject))
def outputEdlFile(__edlFileName, __jsonObject):
"""Write the IP addresses from the jsonObject into the __edlFileName"""
# Create the ipEdl list
ipEdl = []
# First iterate through __jsonObject to find IP addresses
try:
if isinstance(__jsonObject, list):
# Table looks a little different for loopback_ips
# Iterate through the json object and append IP address to ipEdl.
while len(__jsonObject) != 0:
jsonItem = __jsonObject.pop()
if "status" in jsonItem and jsonItem["status"] != "error":
for item in jsonItem["result"]["addrList"]:
locationInfo = item.split(":")
ipEdl.append(locationInfo[1])
else:
if "status" in __jsonObject and \
__jsonObject["status"] == "success":
# Iterate through the json object and append IP address to ipEdl.
for objEgressIps in __jsonObject["result"]:
for obj in objEgressIps["address_details"]:
ipEdl.append(obj["address"])
else:
print(__jsonObject)
sys.exit(1)
except TypeError:
print("Are you sure this is the right JSON object?")
print(__jsonObject)
sys.exit(1)
# Write contents of ipEdl to __edlFileName
with open(__edlFileName, 'w') as edlFile:
edlFile.write('\n'.join(ipEdl))
def checkArgsJsonCsvEdl(__dataObject):
"""Check to see if the outputCsvFile is defined."""
if args.outputCsvFile:
# Convert the Json object into CSV format.
jsonConvert2Csv(args.outputCsvFile, __dataObject)
sys.exit(0)
if args.outputJsonFile:
# Write the Json object into json file.
outputJsonFile(args.outputJsonFile, __dataObject)
sys.exit(0)
if args.outputEdlFile:
# Write the IP addresses into the edl file.
outputEdlFile(args.outputEdlFile, __dataObject)
sys.exit(0)
def showAllEgressIps(__getPrismaAccessURI, __PrismaAccessHeaders):
"""Shows all egress IPs used in the Prisma Access Service."""
__AllEgressIps = getJsonObjectFromUrl(__getPrismaAccessURI,
__PrismaAccessHeaders,
EgressIPs)
checkArgsJsonCsvEdl(__AllEgressIps)
# Only reaches this stage if the outputJsonFile or outputCsvFile is not defined.
printJsonObject(__AllEgressIps)
def showAllActiveMobileUserAddresses(__getPrismaAccessURI, __PrismaAccessHeaders):
"""Shows all Active Mobile User Addresses used in the Prisma Access Service."""
__AllActiveMobileUserAddresses = getJsonObjectFromUrl(__getPrismaAccessURI,
__PrismaAccessHeaders,
ActiveMobileUserAddresses)
checkArgsJsonCsvEdl(__AllActiveMobileUserAddresses)
# Only reaches this stage if the outputJsonFile or outputCsvFile is not defined.
printJsonObject(__AllActiveMobileUserAddresses)
def showAllActiveReservedOnboardedMobileUserLocations(__getPrismaAccessURI, __PrismaAccessHeaders):
"""Shows all Active/Reserved for Onboarded Mobile User Locations IPs used in the Prisma Access Service."""
__AllActiveReservedOnboardedMobileUserLocations = getJsonObjectFromUrl(__getPrismaAccessURI,
__PrismaAccessHeaders,
ActiveReservedOnboardedMobileUserLocations)
checkArgsJsonCsvEdl(__AllActiveReservedOnboardedMobileUserLocations)
# Only reaches this stage if the outputJsonFile or outputCsvFile is not defined.
printJsonObject(__AllActiveReservedOnboardedMobileUserLocations)
def showActiveIPOnboardedMobileUserLocations(__getPrismaAccessURI, __PrismaAccessHeaders):
"""Shows all Active for Onboarded Mobile User Locations IPs used in the Prisma Access Service."""
__ActiveIPOnboardedMobileUserLocations = getJsonObjectFromUrl(__getPrismaAccessURI,
__PrismaAccessHeaders,
ActiveIPOnboardedMobileUserLocations)
checkArgsJsonCsvEdl(__ActiveIPOnboardedMobileUserLocations)
# Only reaches this stage if the outputJsonFile or outputCsvFile is not defined.
printJsonObject(__ActiveIPOnboardedMobileUserLocations)
def showRemoteNetworkAddresses(__getPrismaAccessURI, __PrismaAccessHeaders):
"""Shows all Remote Network IPs used in the Prisma Access Service."""
__RemoteNetworkAddresses = getJsonObjectFromUrl(__getPrismaAccessURI,
__PrismaAccessHeaders,
RemoteNetworkAddresses)
checkArgsJsonCsvEdl(__RemoteNetworkAddresses)
# Only reaches this stage if the outputJsonFile or outputCsvFile is not defined.
printJsonObject(__RemoteNetworkAddresses)
def showCleanPipeAddresses(__getPrismaAccessURI, __PrismaAccessHeaders):
"""Shows all Clean Pipe IPs used in the Prisma Access Service."""
__CleanPipeAddresses = getJsonObjectFromUrl(__getPrismaAccessURI,
__PrismaAccessHeaders,
CleanPipeAddresses)
checkArgsJsonCsvEdl(__CleanPipeAddresses)
# Only reaches this stage if the outputJsonFile or outputCsvFile is not defined.
printJsonObject(__CleanPipeAddresses)
def showExplicitProxyAddresses(__getPrismaAccessURI, __PrismaAccessHeaders):
"""Shows all Explicit Proxy IPs used in the Prisma Access Service."""
__ExplicitProxyAddresses = getJsonObjectFromUrl(__getPrismaAccessURI,
__PrismaAccessHeaders,
ExplicitProxyAddresses)
checkArgsJsonCsvEdl(__ExplicitProxyAddresses)
# Only reaches this stage if the outputJsonFile or outputCsvFile is not defined.
printJsonObject(__ExplicitProxyAddresses)
def showLoopbackIPAddresses(__getPrismaAccessURI, __PrismaAccessHeaders):
"""Shows all the loopback IP addresses of the tenant."""
fwTypes = ['gpcs_gp_gw', 'gpcs_gp_portal', 'gpcs_remote_network', 'gpcs_clean_pipe']
uriBody = ""
loopbackItems = []
for item in fwTypes:
uriBody = item
__LoopbackIPAddresses = getJsonObjectFromUrl(__getPrismaAccessURI,
__PrismaAccessHeaders,
uriBody)
loopbackItems.append(__LoopbackIPAddresses)
checkArgsJsonCsvEdl(loopbackItems)
# Only reaches this stage if the outputJsonFile or outputCsvFile is not defined.
printJsonObject(loopbackItems)
def argsMobileUsers(__getPrismaAccessURI, __PrismaAccessHeaders):
"""Parse through Mobile user arguments"""
if args.allActiveMobileUserAddresses:
showAllActiveMobileUserAddresses(__getPrismaAccessURI, __PrismaAccessHeaders)
sys.exit(0)
if args.allAROnboardedMobileUserLocations:
showAllActiveReservedOnboardedMobileUserLocations(__getPrismaAccessURI, __PrismaAccessHeaders)
sys.exit(0)
if args.allActiveIPOnboardedMobileUserLocations:
showActiveIPOnboardedMobileUserLocations(__getPrismaAccessURI, __PrismaAccessHeaders)
sys.exit(0)
def argsRemoteNetworks(__getPrismaAccessURI, __PrismaAccessHeaders):
"""Show the Remote Network Addresses"""
if args.allRemoteNetworkAddresses:
showRemoteNetworkAddresses(__getPrismaAccessURI, __PrismaAccessHeaders)
sys.exit(0)
def argsCleanPipe(__getPrismaAccessURI, __PrismaAccessHeaders):
"""Show the Clean Pipe Addresses"""
if args.allCleanPipeAddresses:
showCleanPipeAddresses(__getPrismaAccessURI, __PrismaAccessHeaders)
sys.exit(0)
def argsExplicitProxy(__getPrismaAccessURI, __PrismaAccessHeaders):
"""Show the Explicit Proxy Addresses"""
if args.allExplicitProxyAddresses:
showExplicitProxyAddresses(__getPrismaAccessURI, __PrismaAccessHeaders)
sys.exit(0)
def argsLoopbackIPAddresses(__getPrismaAccessURI, __PrismaAccessHeaders):
"""Checks to see if the --allLoopbackIPAddresses argument is set."""
if args.allLoopbackIPAddresses:
showLoopbackIPAddresses(__getPrismaAccessURI, __PrismaAccessHeaders)
sys.exit(0)
def apiArguments():
"""Parse through the API arguments"""
if args.setAPIKey:
setAPIKey(args.setAPIKey)
if args.showAPIKey:
print(getAPIKey())
sys.exit(0)
if args.deleteAPIKey:
delAPIKey()
sys.exit(0)
def apiQueryArguments(__getPrismaAccessURI):
"""Check to see if the API Key file is defined."""
API_KEY = getAPIKey()
PrismaAccessHeaders = {"header-api-key": API_KEY}
argsLoopbackIPAddresses(__getPrismaAccessURI, PrismaAccessHeaders)
if args.allEgressIPs:
showAllEgressIps(__getPrismaAccessURI, PrismaAccessHeaders)
sys.exit(0)
argsMobileUsers(__getPrismaAccessURI, PrismaAccessHeaders)
argsRemoteNetworks(__getPrismaAccessURI, PrismaAccessHeaders)
argsCleanPipe(__getPrismaAccessURI, PrismaAccessHeaders)
argsExplicitProxy(__getPrismaAccessURI, PrismaAccessHeaders)
def main():
"""Parse all the arguments for the script"""
parseArguments()
# Define the URL to query
getPrismaAccessURI = f'https://api.{args.environment}.datapath.prismaaccess.com/getPrismaAccessIP/v2'
# Check to see if the API arguments are defined.
apiArguments()
if args.allLoopbackIPAddresses:
legacyPrismaAccessURI = f'https://api.{args.environment}.datapath.prismaaccess.com/getAddrList/latest?'
apiQueryArguments(legacyPrismaAccessURI)
if (args.allEgressIPs or
args.allAROnboardedMobileUserLocations or
args.allActiveIPOnboardedMobileUserLocations or
args.allActiveMobileUserAddresses or
args.allRemoteNetworkAddresses or
args.allCleanPipeAddresses or
args.allExplicitProxyAddresses) and not args.allLoopbackIPAddresses:
apiQueryArguments(getPrismaAccessURI)
if args.fileName:
# If the --fileName argument is specified, the script will
# import the json object in the file.
myEgressIps = getJsonObject(args.fileName)
if args.outputCsvFile:
# Convert json into csv format.
jsonConvert2Csv(args.outputCsvFile, myEgressIps)
sys.exit(0)
# By default print the json object in tabulated form.
printJsonObject(myEgressIps)
sys.exit(0)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print('Interrupted')
print()
try:
sys.exit(0)
except SystemExit:
os.exit(0)