-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPUT_acp_new_rules.py
More file actions
76 lines (64 loc) · 2.45 KB
/
Copy pathPUT_acp_new_rules.py
File metadata and controls
76 lines (64 loc) · 2.45 KB
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
'''
Author: Dino Charalambous
Description: Partially update FTD rules, where Inspection is disabled on the rule.
PortLiteral Mappings
tcp: 6
udp: 17
icmp: protocol = 1 , "icmpType": "Any"
'''
import requests
import json
from generate_token import token
#from payload_generator import chgfile, newfile
import tkinter.filedialog as fd
from pathlib import Path
payloadfile = fd.askopenfilename(title="Select the payload file to load")
p = payloadfile.split('/')
p = p[-1]
chgfile = p.rstrip('.json')
fullname = chgfile.split('_')
rulename = fullname[1]
polid = fullname[0]
fmcip = "" #put your fmc ip here
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-auth-access-token': token
}
policy_dict = {'ftd_name':'ftd id'}
def blankrule(): #this function creates a blank rule, a partial rule update function will execute after this function.
global ruleid
url = f"https://{fmcip}/api/fmc_config/v1/domain/<domain_id>/policy/accesspolicies/{policy_dict[polid]}/accessrules"
payload = json.dumps({
"action": "ALLOW",
"enabled": True,
"name": rulename,
"type": "AccessRule",
"enableSyslog": True,
"sendEventsToFMC": True,
"logBegin": False,
"logEnd": True,
"newComments": [
"step 1 create new rule"
]})
response = requests.request("PUT", url, headers=headers, data=payload, verify=False)
response = response.content.decode('utf-8')
data = json.loads(response)
jsondata = json.dumps(data, indent=4)
ruleid = data['id']
print(ruleid)
def updaterule(): #this function will partial update the newly created rule with the required information.
url = f"https://{fmcip}/api/fmc_config/v1/domain/<domain_id>/policy/accesspolicies/{policy_dict[polid]}/accessrules/{ruleid}?partialUpdate=true"
with open(f'{payloadfile}', 'r+', encoding='utf-8') as json_file:
rule_list = json.load(json_file)
rule_list['id'] = ruleid
json_file.seek(0)
json.dump(rule_list, json_file, indent=4)
with open(f'{payloadfile}') as pload:
pl = json.load(pload)
payload = json.dumps(pl)
response = requests.request("PUT", url, headers=headers, data=payload, verify=False)
print(response.text)
blankrule()
updaterule()
input("Rule load complete, Press Enter to exit.")