8ee19ffe85
FortiOS REST API v2, 11 commands: ban/unban/list banned IPs (quarantine), firewall address object create/delete/list, address-group update/list, firewall policy and service listing. Bearer-token auth, multi-VDOM, stdlib-only. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
73 lines
2.2 KiB
Python
73 lines
2.2 KiB
Python
import json, os, sys, urllib.parse, urllib.request, urllib.error, ssl
|
|
|
|
|
|
def _cfg():
|
|
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
|
|
|
|
|
def _inputs():
|
|
return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
|
|
|
|
|
def _ctx():
|
|
cfg = _cfg()
|
|
if cfg.get("insecure"):
|
|
c = ssl.create_default_context()
|
|
c.check_hostname = False
|
|
c.verify_mode = ssl.CERT_NONE
|
|
return c
|
|
return None
|
|
|
|
|
|
def request(method, path, cfg, inputs, body=None, extra_params=None):
|
|
base = str(cfg.get("server", "")).rstrip("/") + "/api/v2"
|
|
url = base + path
|
|
params = {}
|
|
vdom = (inputs.get("vdom") if inputs else None) or cfg.get("vdom")
|
|
if vdom:
|
|
params["vdom"] = vdom
|
|
if extra_params:
|
|
params.update(extra_params)
|
|
if params:
|
|
url += "?" + urllib.parse.urlencode(params)
|
|
data = json.dumps(body).encode("utf-8") if body is not None else None
|
|
headers = {"Authorization": "Bearer " + str(cfg.get("api_key", "")), "Accept": "application/json"}
|
|
if data is not None:
|
|
headers["Content-Type"] = "application/json"
|
|
req = urllib.request.Request(url, data=data, headers=headers, method=method)
|
|
with urllib.request.urlopen(req, timeout=60, context=_ctx()) as r:
|
|
raw = r.read()
|
|
return json.loads(raw) if raw else {}
|
|
|
|
|
|
def _run(fn):
|
|
try:
|
|
cfg = _cfg()
|
|
inputs = _inputs()
|
|
print(json.dumps(fn(cfg, inputs)))
|
|
except urllib.error.HTTPError as e:
|
|
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
|
sys.exit(1)
|
|
except Exception as e:
|
|
print(json.dumps({"error": str(e)}))
|
|
sys.exit(1)
|
|
|
|
|
|
def main(cfg, inputs):
|
|
name = inputs.get("name")
|
|
if not name or not str(name).strip():
|
|
raise Exception("name is required")
|
|
subnet = inputs.get("subnet")
|
|
if not subnet or not str(subnet).strip():
|
|
raise Exception("subnet is required")
|
|
|
|
body = {"name": name, "type": "ipmask", "subnet": subnet}
|
|
comment = inputs.get("comment")
|
|
if comment and str(comment).strip():
|
|
body["comment"] = comment
|
|
|
|
return request("POST", "/cmdb/firewall/address", cfg, inputs, body=body)
|
|
|
|
|
|
_run(main)
|