Skip to content

Commit 6ff7964

Browse files
hummelm10hummelm10
authored andcommitted
added purging options, cleaned up api calls, added domains to service list prints
1 parent c5091ac commit 6ff7964

File tree

8 files changed

+207
-43
lines changed

8 files changed

+207
-43
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
from .listPublicIPs import listPublicIPs
1+
from .listPublicIPs import listPublicIPs
2+
from .purge import *
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import scripts
2+
import requests
3+
import pprint
4+
import pandas
5+
6+
def purgeKey(service, key, soft):
7+
if scripts.checkAPINoPrint():
8+
header={"Accept":"application/json"}
9+
header.update({"Fastly-Key":scripts.getKeyFromConfig()})
10+
if soft:
11+
header.update({"Fastly-Soft-Purge":"1"})
12+
input("Request to be made: " + "https://api.fastly.com/service/" + str(service) + "/purge/" + str(key) + "\nPress Enter to continue...")
13+
r=requests.post("https://api.fastly.com/service/" + str(service) + "/purge/" + str(key),headers=header)
14+
if r.status_code == 401:
15+
input(scripts.bcolors.WARNING + "Error with request. Press ENTER to continue..." + scripts.bcolors.ENDC)
16+
elif r.status_code == 200:
17+
pprint.pprint(r.json())
18+
input("Press ENTER to continue...")
19+
else:
20+
input(scripts.bcolors.WARNING + "Error with request. Press ENTER to continue..." + scripts.bcolors.ENDC)
21+
else:
22+
input(scripts.bcolors.WARNING + "Error with API Key, generate a new one. Press ENTER to continue..." + scripts.bcolors.ENDC)
23+
24+
def purgeKeyMenu():
25+
df = scripts.listServicesNoPrint()
26+
print(df)
27+
try:
28+
sernumber = int(input("\n\nEnter service to purge: "))
29+
except:
30+
input("Not a valid number. Press enter to continue...")
31+
scripts.clear()
32+
purgeKeyMenu()
33+
service = df['ID'].iloc(sernumber)
34+
key = input("Enter key to purge: ")
35+
while "Not a valid response.":
36+
reply = str(input("Soft purge [Y/n]")).lower().strip()
37+
if reply[0] == 'y':
38+
purgeKey(service, key, True)
39+
if reply[0] == 'n':
40+
purgeKey(service, key, False)
41+
42+
def purgeService(service):
43+
if scripts.checkAPINoPrint():
44+
header={"Accept":"application/json"}
45+
header.update({"Fastly-Key":scripts.getKeyFromConfig()})
46+
input("Request to be made: " + "https://api.fastly.com/service/" + str(service) + "/purge_all" + "\nPress enter to continue...")
47+
r=requests.post("https://api.fastly.com/service/" + str(service) + "/purge_all",headers=header)
48+
if r.status_code == 401:
49+
input(scripts.bcolors.WARNING + "Error with request. Press ENTER to continue..." + scripts.bcolors.ENDC)
50+
elif r.status_code == 200:
51+
pprint.pprint(r.json())
52+
input("Press ENTER to continue...")
53+
else:
54+
input(scripts.bcolors.WARNING + "Error with request. Press ENTER to continue..." + scripts.bcolors.ENDC)
55+
else:
56+
input(scripts.bcolors.WARNING + "Error with API Key, generate a new one. Press ENTER to continue..." + scripts.bcolors.ENDC)
57+
58+
def purgeServiceMenu():
59+
df = scripts.listServicesNoPrint()
60+
print(df)
61+
try:
62+
sernumber = int(input("\n\nEnter service to purge: "))
63+
except:
64+
input("Not a valid number. Press enter to continue...")
65+
scripts.clear()
66+
purgeKeyMenu()
67+
service = df['ID'].iloc(sernumber)
68+
purgeService(str(service))
69+
70+
def purgeURL(url):
71+
if scripts.checkAPINoPrint():
72+
header={"Accept":"application/json"}
73+
header.update({"Fastly-Key":scripts.getKeyFromConfig()})
74+
header.update({"Fastly-Soft-Purge":"1"})
75+
input("Purge URL: " + str(url) + "\nPress Enter to continue...")
76+
r=requests.request("PURGE", str(url), headers=header)
77+
if r.status_code == 401:
78+
input(scripts.bcolors.WARNING + "Error with request. Press ENTER to continue..." + scripts.bcolors.ENDC)
79+
elif r.status_code == 200:
80+
pprint.pprint(r.json())
81+
input("Press ENTER to continue...")
82+
else:
83+
input(scripts.bcolors.WARNING + "Error with request. Press ENTER to continue..." + scripts.bcolors.ENDC)
84+
else:
85+
input(scripts.bcolors.WARNING + "Error with API Key, generate a new one. Press ENTER to continue..." + scripts.bcolors.ENDC)
86+
87+
def purgeMenu():
88+
flag = True
89+
scripts.clear()
90+
while flag == True:
91+
scripts.clear()
92+
#Display menu options
93+
print(' ' + scripts.bcolors.BOLD + scripts.bcolors.UNDERLINE + scripts.bcolors.HEADER + 'PURGE MENU' + scripts.bcolors.ENDC + scripts.bcolors.ENDC + scripts.bcolors.ENDC)
94+
print(scripts.bcolors.HEADER + '===========' + scripts.bcolors.ENDC)
95+
print('1: Purge Key')
96+
print('2: Purge Service')
97+
print('3: Purge URL')
98+
print('B to go back')
99+
print(scripts.bcolors.HEADER + '===========' + scripts.bcolors.ENDC)
100+
print(' ')
101+
choice = input('Option: ') #get user's choice
102+
103+
if choice == '1':
104+
scripts.clear()
105+
purgeKeyMenu()
106+
elif choice == '2':
107+
scripts.clear()
108+
purgeServiceMenu()
109+
elif choice == '3':
110+
scripts.clear()
111+
try:
112+
url = str(input("Enter URL to purge: "))
113+
except:
114+
input("Error in input. Press Enter to continue...")
115+
scripts.clear()
116+
purgeMenu()
117+
purgeURL(url)
118+
elif choice == 'B' or choice == 'b':
119+
flag = False
120+
else:
121+
input('Not a valid choice. Hit enter to continue...')

FastlyPythonCLI/scripts/CDNUtil.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def CDNMenu():
99
print(' ' + scripts.bcolors.BOLD + scripts.bcolors.UNDERLINE + scripts.bcolors.HEADER + 'CDN MENU' + scripts.bcolors.ENDC + scripts.bcolors.ENDC + scripts.bcolors.ENDC)
1010
print(scripts.bcolors.HEADER + '===========' + scripts.bcolors.ENDC)
1111
print('1: List Public IPs')
12+
print('2: Purge Options')
1213
print('B to go back')
1314
print(scripts.bcolors.HEADER + '===========' + scripts.bcolors.ENDC)
1415
print(' ')
@@ -17,6 +18,9 @@ def CDNMenu():
1718
if choice == '1':
1819
scripts.clear()
1920
scripts.CDN.listPublicIPs()
21+
if choice == '2':
22+
scripts.clear()
23+
scripts.CDN.purgeMenu()
2024
elif choice == 'B' or choice == 'b':
2125
flag = False
2226
else:

FastlyPythonCLI/scripts/WAF/getRuleByID.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def getRuleByID(ruleid):
1111
header.update({"Fastly-Key":scripts.getKeyFromConfig()})
1212
r=requests.get("https://api.fastly.com/wafs/rules/" + ruleid,headers=header)
1313
if r.status_code == 401:
14-
input(scripts.bcolors.WARNING + "Error with request. Press ENTER to continue..." + scripts.bcolors.ENDC)
14+
input(scripts.bcolors.WARNING + "Error with services request.\nStatus: " + str(r.status_code) + "\nPress ENTER to continue..." + scripts.bcolors.ENDC)
1515
elif r.status_code == 200:
1616
with scripts.utils.DataFrameFromDict(r.json()['data']) as df:
1717
df['ID'] = df['id']
@@ -21,4 +21,4 @@ def getRuleByID(ruleid):
2121
# print(df)
2222
return df
2323
else:
24-
input(scripts.bcolors.WARNING + "Error with request. Press ENTER to continue..." + scripts.bcolors.ENDC)
24+
input(scripts.bcolors.WARNING + "Error with services request.\nStatus: " + str(r.status_code) + "\nPress ENTER to continue..." + scripts.bcolors.ENDC)

FastlyPythonCLI/scripts/WAF/getWAFRuleset.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ def getWAFRuleset():
2323
# input("https://api.fastly.com/service/" + str(dfObj['Service ID'].iloc[inVar]) + "/wafs/" + str(dfObj['WAF ID'].iloc[inVar]) + "/ruleset")
2424
r=requests.get("https://api.fastly.com/service/" + str(dfObj['Service ID'].iloc[inVar]) + "/wafs/" + str(dfObj['WAF ID'].iloc[inVar]) + "/rule_statuses",headers=header)
2525
if r.status_code == 401:
26-
input(scripts.bcolors.WARNING + "Error with request. Press ENTER to continue..." + scripts.bcolors.ENDC)
26+
input(scripts.bcolors.WARNING + "Error with services request.\nStatus: " + str(r.status_code) + "\nPress ENTER to continue..." + scripts.bcolors.ENDC)
27+
elif r.status_code == 404:
28+
# * no waf for that service
29+
pass
2730
elif r.status_code == 200:
2831
with scripts.utils.DataFrameFromDict(r.json()['data']) as df:
2932
df['ID'] = df['attributes.unique_rule_id']
@@ -41,6 +44,6 @@ def getWAFRuleset():
4144
print(df)
4245
input("Press ENTER to continue...")
4346
else:
44-
input(scripts.bcolors.WARNING + "Error with request. Press ENTER to continue..." + scripts.bcolors.ENDC)
47+
input(scripts.bcolors.WARNING + "Error with services request.\nStatus: " + str(r.status_code) + "\nPress ENTER to continue..." + scripts.bcolors.ENDC)
4548
else:
4649
input(scripts.bcolors.WARNING + "Error with API Key, generate a new one. Press ENTER to continue..." + scripts.bcolors.ENDC)

FastlyPythonCLI/scripts/WAF/listWAFIDs.py

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ def listWAFIDs():
1616
r=requests.get("https://api.fastly.com/service/" + str(services['ID'].iloc[x]) + "/version/" + str(services['Version'].iloc[x]) + "/wafs",headers=header)
1717
if r.status_code == 401:
1818
input(scripts.bcolors.WARNING + "Error with request. Press ENTER to continue..." + scripts.bcolors.ENDC)
19+
elif r.status_code == 404:
20+
# * no waf for that service
21+
pass
1922
elif r.status_code == 200:
2023
with scripts.utils.DataFrameFromDict(r.json()['data']) as df:
2124
if df.empty != True:
2225
df.insert(0, 'Name', str(services['Name'].iloc[x]))
2326
df.insert(1, 'Service ID', str(services['ID'].iloc[x]))
27+
df.insert(2, 'Domain(s)', str(services['Domain(s)'].iloc[x]))
2428
df['WAF ID'] = df['id']
2529
df['Version'] = df['attributes.version']
2630
df['Last Push'] = df['attributes.last_push']
@@ -30,12 +34,12 @@ def listWAFIDs():
3034
if df.empty != True:
3135
dfObj = dfObj.append(df)
3236
else:
33-
input(scripts.bcolors.WARNING + "Error with request. Press ENTER to continue..." + scripts.bcolors.ENDC)
37+
input(scripts.bcolors.WARNING + "Error with services request.\nStatus: " + str(r.status_code) + "\nPress ENTER to continue..." + scripts.bcolors.ENDC)
3438
print(dfObj)
3539
input("Press ENTER to continue...")
3640
return dfObj
3741
else:
38-
input(scripts.bcolors.WARNING + "Error with request. Press ENTER to continue..." + scripts.bcolors.ENDC)
42+
input(scripts.bcolors.WARNING + "Error with services request.\nStatus: " + str(r.status_code) + "\nPress ENTER to continue..." + scripts.bcolors.ENDC)
3943
else:
4044
input(scripts.bcolors.WARNING + "Error with API Key, generate a new one. Press ENTER to continue..." + scripts.bcolors.ENDC)
4145
return None
@@ -47,27 +51,32 @@ def listWAFIDsNoPrompt():
4751
if services is not None:
4852
dfObj = pandas.DataFrame()
4953
for x in range(len(services.index)):
50-
header={"Accept":"application/vnd.api+json"}
51-
header.update({"Fastly-Key":scripts.getKeyFromConfig()})
52-
# print("https://api.fastly.com/service/" + str(services['ID'].iloc[x]) + "/version/" + str(services['Version'].iloc[x]) + "/wafs")
53-
r=requests.get("https://api.fastly.com/service/" + str(services['ID'].iloc[x]) + "/version/" + str(services['Version'].iloc[x]) + "/wafs",headers=header)
54-
if r.status_code == 401:
55-
input(scripts.bcolors.WARNING + "Error with request. Press ENTER to continue..." + scripts.bcolors.ENDC)
56-
elif r.status_code == 200:
57-
with scripts.utils.DataFrameFromDict(r.json()['data']) as df:
54+
if not services['Version'].isnull().iloc[x]:
55+
header={"Accept":"application/vnd.api+json"}
56+
header.update({"Fastly-Key":scripts.getKeyFromConfig()})
57+
# print("https://api.fastly.com/service/" + str(services['ID'].iloc[x]) + "/version/" + str(services['Version'].iloc[x]) + "/wafs")
58+
r=requests.get("https://api.fastly.com/service/" + str(services['ID'].iloc[x]) + "/version/" + str(services['Version'].iloc[x]) + "/wafs",headers=header)
59+
if r.status_code == 401:
60+
input(scripts.bcolors.WARNING + "Error with services request.\nStatus: " + str(r.status_code) + "\nPress ENTER to continue..." + scripts.bcolors.ENDC)
61+
elif r.status_code == 404:
62+
# * no waf for that service
63+
pass
64+
elif r.status_code == 200:
65+
with scripts.utils.DataFrameFromDict(r.json()['data']) as df:
66+
if df.empty != True:
67+
df.insert(0, 'Name', str(services['Name'].iloc[x]))
68+
df.insert(1, 'Service ID', str(services['ID'].iloc[x]))
69+
df.insert(2, 'Domain(s)', str(services['Domain(s)'].iloc[x]))
70+
df['WAF ID'] = df['id']
71+
df['Version'] = df['attributes.version']
72+
df['Last Push'] = df['attributes.last_push']
73+
df['Logged Rules'] = df['attributes.rule_statuses_log_count']
74+
df['Blocked Rules'] = df['attributes.rule_statuses_block_count']
75+
df['Disabled Rules'] = df['attributes.rule_statuses_disabled_count']
5876
if df.empty != True:
59-
df.insert(0, 'Name', str(services['Name'].iloc[x]))
60-
df.insert(1, 'Service ID', str(services['ID'].iloc[x]))
61-
df['WAF ID'] = df['id']
62-
df['Version'] = df['attributes.version']
63-
df['Last Push'] = df['attributes.last_push']
64-
df['Logged Rules'] = df['attributes.rule_statuses_log_count']
65-
df['Blocked Rules'] = df['attributes.rule_statuses_block_count']
66-
df['Disabled Rules'] = df['attributes.rule_statuses_disabled_count']
67-
if df.empty != True:
68-
dfObj = dfObj.append(df)
69-
else:
70-
input(scripts.bcolors.WARNING + "Error with request. Press ENTER to continue..." + scripts.bcolors.ENDC)
77+
dfObj = dfObj.append(df)
78+
else:
79+
input(scripts.bcolors.WARNING + "Error with services request.\nStatus: " + str(r.status_code) + "\nPress ENTER to continue..." + scripts.bcolors.ENDC)
7180
print(dfObj)
7281
return dfObj
7382
else:

FastlyPythonCLI/scripts/WAF/listWAFRules.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def listWAFRules():
1010
header.update({"Fastly-Key":scripts.getKeyFromConfig()})
1111
r=requests.get("https://api.fastly.com/wafs/rules",headers=header)
1212
if r.status_code == 401:
13-
input(scripts.bcolors.WARNING + "Error with request. Press ENTER to continue..." + scripts.bcolors.ENDC)
13+
input(scripts.bcolors.WARNING + "Error with services request.\nStatus: " + str(r.status_code) + "\nPress ENTER to continue..." + scripts.bcolors.ENDC)
1414
elif r.status_code == 200:
1515
with scripts.utils.DataFrameFromDict(r.json()['data']) as df:
1616
df['ID'] = df['id']
@@ -21,6 +21,6 @@ def listWAFRules():
2121
print(df)
2222
input("Press ENTER to continue...")
2323
else:
24-
input(scripts.bcolors.WARNING + "Error with request. Press ENTER to continue..." + scripts.bcolors.ENDC)
24+
input(scripts.bcolors.WARNING + "Error with services request.\nStatus: " + str(r.status_code) + "\nPress ENTER to continue..." + scripts.bcolors.ENDC)
2525
else:
2626
input(scripts.bcolors.WARNING + "Error with API Key, generate a new one. Press ENTER to continue..." + scripts.bcolors.ENDC)

0 commit comments

Comments
 (0)