Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit 6ba9ced

Browse files
committed
cleanup to show correct sequence of firewall and filter creation and deletion
1 parent 8a04e95 commit 6ba9ced

1 file changed

Lines changed: 48 additions & 28 deletions

File tree

examples/example_firewall_rules.py

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import sys
66
import re
77
import json
8+
import uuid
89

910
sys.path.insert(0, os.path.abspath('.'))
1011
sys.path.insert(0, os.path.abspath('..'))
@@ -18,7 +19,7 @@ def main():
1819
try:
1920
zone_name = sys.argv[1]
2021
except IndexError:
21-
exit('usage: example_bot_management.py zone_name True/False')
22+
exit('usage: example_firewall_rules.py zone_name')
2223

2324
# grab the zone identifier
2425
try:
@@ -37,60 +38,79 @@ def main():
3738

3839
zone_id = zones[0]['id']
3940

40-
# SHOW EXISTSING FIREWALL RULES
41+
# SHOW EXISTING FIREWALL RULES
4142
r = cf.zones.firewall.rules.get(zone_id)
42-
print('filewall rules =\n' + json.dumps(r, indent=4, sort_keys=False) + '\n')
43+
print('existing filewall rules =\n' + json.dumps(r, indent=4, sort_keys=False) + '\n')
44+
45+
# SHOW EXISTING FILTERS
46+
r = cf.zones.filters.get(zone_id)
47+
print('existing filters =\n' + json.dumps(r, indent=4, sort_keys=False) + '\n')
4348

4449
# CREATE A FILTER & FIREWALL RULES
4550

51+
reference_name = 'FILTER-' + str(uuid.uuid1())
52+
4653
my_filter = {
47-
# 'id': '00000000000000000000000000000000',
4854
'expression': 'http.request.uri.path == "/private.html$"',
4955
'paused': True,
50-
'description': 'stop access to /foo.html',
51-
'ref': 'FILTER-1',
56+
'description': 'stop access to /private.html',
57+
'ref': reference_name,
5258
}
5359

54-
my_data = [
60+
my_rule = [
5561
{
5662
'action': 'block',
5763
'filter': my_filter,
58-
# 'id': '00000000000000000000000000000000',
59-
# 'products': ['waf'],
60-
# 'priority': 1,
61-
# 'paused': True,
62-
# 'description': 'stop access to /foo.html',
63-
# 'ref': 'FILTER-1',
64+
'paused': True,
6465
}
6566
]
6667

6768
try:
68-
r = cf.zones.firewall.rules.post(zone_id, data=my_data)
69-
except Exception as e:
70-
print(e)
69+
r = cf.zones.firewall.rules.post(zone_id, data=my_rule)
70+
except CloudFlare.exceptions.CloudFlareAPIError as e:
71+
print('create zones.filewall.rules: %d %s' % (int(e), str(e)))
7172
exit(1)
7273

7374
print('firewall rule created =\n' + json.dumps(r, indent=4, sort_keys=False) + '\n')
7475

75-
# SHOW EXISTSING FILTERS
76-
r = cf.zones.filters.get(zone_id)
77-
print('filters =\n' + json.dumps(r, indent=4, sort_keys=False) + '\n')
76+
firewall_id = r[0]['id']
77+
filter_id = r[0]['filter']['id']
7878

79-
# DELETE EXISTSING FILTERS
79+
print('filewall_id = %s filter_id = %s' % (firewall_id, filter_id))
80+
81+
# SHOW PRESENT FIREWALL RULES
82+
r = cf.zones.firewall.rules.get(zone_id)
83+
print('present filewall rules =\n' + json.dumps(r, indent=4, sort_keys=False) + '\n')
84+
85+
# DELETE NEW FIREWALL RULES
8086
for f in r:
8187
print('id = ' + f['id'])
82-
r2 = cf.zones.filters.delete(zone_id, f['id'])
83-
print('deleted id = ' + r2['id'])
88+
try:
89+
r2 = cf.zones.firewall.rules.delete(zone_id, f['id'])
90+
print('deleted id = ' + r2['id'])
91+
except CloudFlare.exceptions.CloudFlareAPIError as e:
92+
print('zones.filewall.rules.delete: %d %s' % (int(e), str(e)))
8493

85-
# SHOW EXISTSING FIREWALL RULES
86-
r = cf.zones.firewall.rules.get(zone_id)
87-
print('filewall rules =\n' + json.dumps(r, indent=4, sort_keys=False) + '\n')
94+
# SHOW PRESENT FILTERS
95+
r = cf.zones.filters.get(zone_id)
96+
print('present filters =\n' + json.dumps(r, indent=4, sort_keys=False) + '\n')
8897

89-
# DELETE EXISTSING FIREWALL RULES
98+
# DELETE NEW FILTERS
9099
for f in r:
91100
print('id = ' + f['id'])
92-
r2 = cf.zones.firewall.rules.delete(zone_id, f['id'])
93-
print('deleted id = ' + r2['id'])
101+
try:
102+
r2 = cf.zones.filters.delete(zone_id, f['id'])
103+
print('deleted id = ' + r2['id'])
104+
except CloudFlare.exceptions.CloudFlareAPIError as e:
105+
print('zones.filters.delete: %d %s' % (int(e), str(e)))
106+
107+
# SHOW FINAL FIREWALL RULES
108+
r = cf.zones.firewall.rules.get(zone_id)
109+
print('final filewall rules =\n' + json.dumps(r, indent=4, sort_keys=False) + '\n')
110+
111+
# SHOW FINAL FILTERS
112+
r = cf.zones.filters.get(zone_id)
113+
print('final filters =\n' + json.dumps(r, indent=4, sort_keys=False) + '\n')
94114

95115
if __name__ == '__main__':
96116
main()

0 commit comments

Comments
 (0)