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): raw_ips = inputs.get("ip_addresses") if not raw_ips or not str(raw_ips).strip(): raise Exception("ip_addresses is required") ip_list = [ip.strip() for ip in str(raw_ips).split(",") if ip.strip()] if not ip_list: raise Exception("ip_addresses is required") expiry_raw = inputs.get("expiry_seconds") if expiry_raw is None or str(expiry_raw).strip() == "": expiry = 0 else: expiry = int(expiry_raw) body = {"ip_addresses": ip_list, "expiry": expiry} return request("POST", "/monitor/user/banned/add_users", cfg, inputs, body=body) _run(main)