import json, os, sys, urllib.parse, urllib.request, urllib.error BASE = "https://my.imperva.com/api/prov/v1" def _cfg(): return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}")) def _inputs(): return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}")) def post(path, cfg, fields=None): body = {"api_id": str(cfg.get("api_id", "")), "api_key": str(cfg.get("api_key", ""))} if fields: body.update({k: v for k, v in fields.items() if v not in (None, "")}) data = urllib.parse.urlencode(body).encode("utf-8") req = urllib.request.Request(BASE + path, data=data, headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST") with urllib.request.urlopen(req, timeout=60) as r: raw = r.read() return json.loads(raw) if raw else {} def _run(fn): try: 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): site_id = inputs.get("site_id") if not site_id: raise Exception("site_id is required") ips = inputs.get("ips") if not ips: raise Exception("ips is required") return post("/sites/configure/acl", cfg, { "site_id": site_id, "rule_id": "api.acl.blacklisted_ips", "ips": ips, }) _run(main)