2475c4b56e
Stormshield SNS API, 5 commands: add/remove block host-group member (containment), list hosts, monitor query. Session (login) auth, stdlib-only. py_compile clean. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
87 lines
2.7 KiB
Python
87 lines
2.7 KiB
Python
import json, os, sys, ssl, base64, http.cookiejar
|
|
import urllib.parse, urllib.request, urllib.error
|
|
|
|
|
|
def _cfg():
|
|
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
|
|
|
|
|
def _inputs():
|
|
return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
|
|
|
|
|
def _ctx(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 _b64(s):
|
|
return base64.b64encode(str(s).encode("utf-8")).decode("ascii")
|
|
|
|
|
|
class Client:
|
|
def __init__(self, cfg):
|
|
self.cfg = cfg
|
|
self.base = str(cfg.get("base_url", "")).rstrip("/")
|
|
ctx = _ctx(cfg)
|
|
self.opener = urllib.request.build_opener(
|
|
urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar()),
|
|
urllib.request.HTTPSHandler(context=ctx) if ctx else urllib.request.HTTPSHandler(),
|
|
)
|
|
|
|
def login(self):
|
|
form = urllib.parse.urlencode({"uid": _b64(self.cfg.get("username", "")), "pswd": _b64(self.cfg.get("password", ""))}).encode("utf-8")
|
|
req = urllib.request.Request(self.base + "/api/auth/login", data=form,
|
|
headers={"Content-Type": "application/x-www-form-urlencoded"}, method="POST")
|
|
with self.opener.open(req, timeout=60) as r:
|
|
return r.read().decode("utf-8", "replace")
|
|
|
|
def command(self, cmd):
|
|
form = urllib.parse.urlencode({"cmd": cmd}).encode("utf-8")
|
|
req = urllib.request.Request(self.base + "/api/command", data=form,
|
|
headers={"Content-Type": "application/x-www-form-urlencoded"}, method="POST")
|
|
with self.opener.open(req, timeout=60) as r:
|
|
return r.read().decode("utf-8", "replace")
|
|
|
|
def logout(self):
|
|
try:
|
|
req = urllib.request.Request(self.base + "/api/auth/logout", data=b"", method="POST")
|
|
self.opener.open(req, timeout=30).read()
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
def _run(fn):
|
|
try:
|
|
cfg = _cfg()
|
|
inputs = _inputs()
|
|
client = Client(cfg)
|
|
client.login()
|
|
try:
|
|
result = fn(client, inputs)
|
|
finally:
|
|
client.logout()
|
|
print(json.dumps(result))
|
|
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(client, inputs):
|
|
command = inputs.get("command")
|
|
if not command:
|
|
raise Exception("command is required")
|
|
|
|
r = client.command(command)
|
|
return {"command": command, "result": r}
|
|
|
|
|
|
_run(main)
|