-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
executable file
·83 lines (66 loc) · 2.07 KB
/
functions.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
#!/usr/bin/env python3
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
import json
from pathlib import Path
# disable SSL warnings, because we're secure :p
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
# TOS constants
TOS_URL = "https://uvo1h5jsg8i6bcu23s8.vm.cld.sr"
# SecureTrack constants
ST_SESSION = requests.Session()
ST_SESSION.auth = ("admin", "password")
ST_SESSION.headers["Accept"] = "application/json"
ST_SESSION.verify = False
# SecureChange constants
SC_SESSION = requests.Session()
SC_SESSION.auth = ("r", "password")
SC_SESSION.headers["Accept"] = "application/json"
SC_SESSION.verify = False
def get_device(device_name: str):
"""
Get the details of a specific device
"""
r = ST_SESSION.get(
f"{TOS_URL}/securetrack/api/devices", params={"name": device_name}
)
if r.ok:
for device in r.json()["devices"]["device"]:
if device["name"] == device_name:
return device
else:
raise ValueError(f"Could not get {device_name}, got response {r.text}")
def get_rules(device_id: int):
"""
Get all rules for a specific device_id
"""
r = ST_SESSION.get(f"{TOS_URL}/securetrack/api/devices/{device_id}/rules")
if r.ok:
return r.json()["rules"]["rule"]
else:
raise ValueError(f"Could not get rules for {device_id}, got response {r.text}")
def load_ticket_template():
"""
Load a JSON ticket template
"""
return json.load(Path("post.json").open())
def post_ticket(ticket_dict):
"""
Post a ticket to SecureChange
"""
r = SC_SESSION.post(
f"{TOS_URL}/securechangeworkflow/api/securechange/tickets", json=ticket_dict
)
if r.ok:
return r.headers["Location"]
else:
raise ValueError("Could not post ticket, got response {r.text}")
def main():
"""
Tests
"""
assert get_device("SRX")["id"] == "5"
assert len(get_rules(5)) == 10
assert load_ticket_template()["ticket"]["subject"] == "Example Ticket"
if __name__ == "__main__":
main()