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

Commit b70509b

Browse files
committed
firewall rules example
1 parent afa8cc0 commit b70509b

1 file changed

Lines changed: 97 additions & 0 deletions

File tree

examples/example_firewall_rules.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env python
2+
"""Cloudflare API code - example"""
3+
4+
import os
5+
import sys
6+
import re
7+
import json
8+
9+
sys.path.insert(0, os.path.abspath('.'))
10+
sys.path.insert(0, os.path.abspath('..'))
11+
import CloudFlare
12+
13+
def main():
14+
"""Cloudflare API code - example"""
15+
16+
cf = CloudFlare.CloudFlare()
17+
18+
try:
19+
zone_name = sys.argv[1]
20+
except IndexError:
21+
exit('usage: example_bot_management.py zone_name True/False')
22+
23+
# grab the zone identifier
24+
try:
25+
params = {'name': zone_name}
26+
zones = cf.zones.get(params=params)
27+
except CloudFlare.exceptions.CloudFlareAPIError as e:
28+
exit('/zone %d %s - api call failed' % (e, e))
29+
except Exception as e:
30+
exit('/zone.get - %s - api call failed' % (e))
31+
32+
if len(zones) == 0:
33+
exit('/zones.get - %s - zones not found' % (zone_name))
34+
35+
if len(zones) != 1:
36+
exit('/zones.get - %s - api call returned %d items' % (zone_name, len(zones)))
37+
38+
zone_id = zones[0]['id']
39+
40+
# SHOW EXISTSING FIREWALL RULES
41+
r = cf.zones.firewall.rules.get(zone_id)
42+
print('filewall rules =\n' + json.dumps(r, indent=4, sort_keys=False) + '\n')
43+
44+
# CREATE A FILTER & FIREWALL RULES
45+
46+
my_filter = {
47+
# 'id': '00000000000000000000000000000000',
48+
'expression': 'http.request.uri.path == "/private.html$"',
49+
'paused': True,
50+
'description': 'stop access to /foo.html',
51+
'ref': 'FILTER-1',
52+
}
53+
54+
my_data = [
55+
{
56+
'action': 'block',
57+
'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+
}
65+
]
66+
67+
try:
68+
r = cf.zones.firewall.rules.post(zone_id, data=my_data)
69+
except Exception as e:
70+
print(e)
71+
exit(1)
72+
73+
print('firewall rule created =\n' + json.dumps(r, indent=4, sort_keys=False) + '\n')
74+
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')
78+
79+
# DELETE EXISTSING FILTERS
80+
for f in r:
81+
print('id = ' + f['id'])
82+
r2 = cf.zones.filters.delete(zone_id, f['id'])
83+
print('deleted id = ' + r2['id'])
84+
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')
88+
89+
# DELETE EXISTSING FIREWALL RULES
90+
for f in r:
91+
print('id = ' + f['id'])
92+
r2 = cf.zones.firewall.rules.delete(zone_id, f['id'])
93+
print('deleted id = ' + r2['id'])
94+
95+
if __name__ == '__main__':
96+
main()
97+

0 commit comments

Comments
 (0)