From 53034f35da1cf1a8f385889d485766f05fcf1ea1 Mon Sep 17 00:00:00 2001 From: Guillaume BOURGEOIS Date: Sat, 11 Jul 2026 23:13:34 +0200 Subject: [PATCH] feat(zscaler): new Zscaler Internet Access containment integration ZIA REST API, 12 commands: URL/IP block-list add/remove, block-list and allow-list read, allow-list add, category URL add, Sandbox report, activate changes. Session-based obfuscated-API-key auth, stdlib-only. Co-Authored-By: Claude Opus 4.8 (1M context) --- integrations/zscaler/manifest.yaml | 137 ++++++++++++++++++ .../zscaler/scripts/activate_changes.py | 99 +++++++++++++ integrations/zscaler/scripts/blacklist_ip.py | 111 ++++++++++++++ integrations/zscaler/scripts/blacklist_url.py | 111 ++++++++++++++ .../zscaler/scripts/category_add_url.py | 115 +++++++++++++++ integrations/zscaler/scripts/get_blacklist.py | 96 ++++++++++++ .../zscaler/scripts/get_categories.py | 100 +++++++++++++ integrations/zscaler/scripts/get_whitelist.py | 96 ++++++++++++ .../zscaler/scripts/sandbox_report.py | 102 +++++++++++++ .../zscaler/scripts/test_connection.py | 97 +++++++++++++ .../zscaler/scripts/undo_blacklist_ip.py | 111 ++++++++++++++ .../zscaler/scripts/undo_blacklist_url.py | 111 ++++++++++++++ integrations/zscaler/scripts/whitelist_url.py | 113 +++++++++++++++ 13 files changed, 1399 insertions(+) create mode 100644 integrations/zscaler/manifest.yaml create mode 100644 integrations/zscaler/scripts/activate_changes.py create mode 100644 integrations/zscaler/scripts/blacklist_ip.py create mode 100644 integrations/zscaler/scripts/blacklist_url.py create mode 100644 integrations/zscaler/scripts/category_add_url.py create mode 100644 integrations/zscaler/scripts/get_blacklist.py create mode 100644 integrations/zscaler/scripts/get_categories.py create mode 100644 integrations/zscaler/scripts/get_whitelist.py create mode 100644 integrations/zscaler/scripts/sandbox_report.py create mode 100644 integrations/zscaler/scripts/test_connection.py create mode 100644 integrations/zscaler/scripts/undo_blacklist_ip.py create mode 100644 integrations/zscaler/scripts/undo_blacklist_url.py create mode 100644 integrations/zscaler/scripts/whitelist_url.py diff --git a/integrations/zscaler/manifest.yaml b/integrations/zscaler/manifest.yaml new file mode 100644 index 0000000..1848be0 --- /dev/null +++ b/integrations/zscaler/manifest.yaml @@ -0,0 +1,137 @@ +id: zscaler +name: Zscaler Internet Access +version: 1.0.0 +description: "Zscaler Internet Access (ZIA REST API) — web-layer containment: block/unblock URLs and IPs (advanced block list), manage the allow list, add URLs to custom categories, read Sandbox reports, and activate configuration changes. Session-based API-key authentication; stdlib-only, no extra Python dependencies." +changelog: "1.0.0 — Initial release: URL/IP block list add/remove, block-list and allow-list read, allow-list add, category URL add, Sandbox report, activate changes." +category: network + +# Per-instance configuration. Authentication is session-based: each command logs +# in with the obfuscated API key + username/password, then reuses the session +# cookie for the request. +config_schema: + properties: + cloud: + type: string + description: "Zscaler cloud base URL (e.g. https://zsapi.zscalertwo.net)" + username: + type: string + description: "ZIA admin username" + password: + type: string + description: "ZIA admin password" + x-soar-sensitive: true + api_key: + type: string + description: "ZIA API key" + x-soar-sensitive: true + insecure: + type: boolean + description: "Trust any TLS certificate (not secure)" + default: false + required: + - cloud + - username + - password + - api_key + +commands: + - id: blacklist_url + name: zscaler-blacklist-url + description: "Add one or more URLs to the advanced block list." + inputs_schema: + properties: + urls: { type: string, description: "Comma-separated URLs to block" } + required: [urls] + outputs_schema: { properties: {} } + - id: undo_blacklist_url + name: zscaler-undo-blacklist-url + description: "Remove one or more URLs from the advanced block list." + inputs_schema: + properties: + urls: { type: string, description: "Comma-separated URLs to remove" } + required: [urls] + outputs_schema: { properties: {} } + - id: blacklist_ip + name: zscaler-blacklist-ip + description: "Add one or more IP addresses to the advanced block list." + inputs_schema: + properties: + ips: { type: string, description: "Comma-separated IP addresses to block" } + required: [ips] + outputs_schema: { properties: {} } + - id: undo_blacklist_ip + name: zscaler-undo-blacklist-ip + description: "Remove one or more IP addresses from the advanced block list." + inputs_schema: + properties: + ips: { type: string, description: "Comma-separated IP addresses to remove" } + required: [ips] + outputs_schema: { properties: {} } + - id: get_blacklist + name: zscaler-get-blacklist + description: "Get the current advanced block list (URLs and IPs)." + risk: read + inputs_schema: + properties: {} + required: [] + outputs_schema: { properties: {} } + - id: whitelist_url + name: zscaler-whitelist-url + description: "Add one or more URLs to the allow list." + inputs_schema: + properties: + urls: { type: string, description: "Comma-separated URLs to allow" } + required: [urls] + outputs_schema: { properties: {} } + - id: get_whitelist + name: zscaler-get-whitelist + description: "Get the current allow list." + risk: read + inputs_schema: + properties: {} + required: [] + outputs_schema: { properties: {} } + - id: category_add_url + name: zscaler-category-add-url + description: "Add one or more URLs to a custom URL category." + inputs_schema: + properties: + category_id: { type: string, description: "URL category id (from zscaler-get-categories)" } + urls: { type: string, description: "Comma-separated URLs to add to the category" } + required: [category_id, urls] + outputs_schema: { properties: {} } + - id: get_categories + name: zscaler-get-categories + description: "List URL categories (id and name)." + risk: read + inputs_schema: + properties: + custom_only: { type: boolean, description: "Return only custom categories (default false)" } + required: [] + outputs_schema: { properties: {} } + - id: sandbox_report + name: zscaler-sandbox-report + description: "Get the Zscaler Sandbox report for a file by MD5 hash." + risk: read + inputs_schema: + properties: + md5: { type: string, description: "MD5 hash of the file" } + details: { type: string, description: "Report detail level: 'full' or 'summary' (default full)" } + required: [md5] + outputs_schema: { properties: {} } + - id: activate_changes + name: zscaler-activate-changes + description: "Activate pending configuration changes in the Zscaler session." + inputs_schema: + properties: {} + required: [] + outputs_schema: { properties: {} } + + - id: test_connection + name: zscaler-test-connection + description: "Verify connectivity and credentials by opening a session (used by the Test button)." + risk: read + inputs_schema: + properties: {} + required: [] + outputs_schema: { properties: {} } diff --git a/integrations/zscaler/scripts/activate_changes.py b/integrations/zscaler/scripts/activate_changes.py new file mode 100644 index 0000000..95acac9 --- /dev/null +++ b/integrations/zscaler/scripts/activate_changes.py @@ -0,0 +1,99 @@ +import json, os, sys, time, urllib.parse, urllib.request, urllib.error, http.cookiejar, ssl + + +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 obfuscate_api_key(api_key): + now = str(int(time.time() * 1000)) + n = now[-6:] + r = str(int(n) >> 1).zfill(6) + key = "" + for i in range(len(n)): + key += api_key[int(n[i])] + for j in range(len(r)): + key += api_key[int(r[j]) + 2] + return now, key + + +class Client: + def __init__(self, cfg): + self.cfg = cfg + self.base = str(cfg.get("cloud", "")).rstrip("/") + "/api/v1" + self.ctx = _ctx(cfg) + self.opener = urllib.request.build_opener( + urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar()), + urllib.request.HTTPSHandler(context=self.ctx) if self.ctx else urllib.request.HTTPSHandler(), + ) + + def call(self, method, path, body=None, params=None): + url = self.base + path + if params: + url += "?" + urllib.parse.urlencode(params) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = {"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 self.opener.open(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + def login(self): + ts, obf = obfuscate_api_key(str(self.cfg.get("api_key", ""))) + body = { + "apiKey": obf, + "username": self.cfg.get("username", ""), + "password": self.cfg.get("password", ""), + "timestamp": ts, + } + return self.call("POST", "/authenticatedSession", body=body) + + def logout(self): + try: + self.call("DELETE", "/authenticatedSession") + 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): + result = client.call("POST", "/status/activate") + if not result: + return {"ok": True} + return result + + +_run(main) diff --git a/integrations/zscaler/scripts/blacklist_ip.py b/integrations/zscaler/scripts/blacklist_ip.py new file mode 100644 index 0000000..ce2aa33 --- /dev/null +++ b/integrations/zscaler/scripts/blacklist_ip.py @@ -0,0 +1,111 @@ +import json, os, sys, time, urllib.parse, urllib.request, urllib.error, http.cookiejar, ssl + + +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 obfuscate_api_key(api_key): + now = str(int(time.time() * 1000)) + n = now[-6:] + r = str(int(n) >> 1).zfill(6) + key = "" + for i in range(len(n)): + key += api_key[int(n[i])] + for j in range(len(r)): + key += api_key[int(r[j]) + 2] + return now, key + + +class Client: + def __init__(self, cfg): + self.cfg = cfg + self.base = str(cfg.get("cloud", "")).rstrip("/") + "/api/v1" + self.ctx = _ctx(cfg) + self.opener = urllib.request.build_opener( + urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar()), + urllib.request.HTTPSHandler(context=self.ctx) if self.ctx else urllib.request.HTTPSHandler(), + ) + + def call(self, method, path, body=None, params=None): + url = self.base + path + if params: + url += "?" + urllib.parse.urlencode(params) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = {"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 self.opener.open(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + def login(self): + ts, obf = obfuscate_api_key(str(self.cfg.get("api_key", ""))) + body = { + "apiKey": obf, + "username": self.cfg.get("username", ""), + "password": self.cfg.get("password", ""), + "timestamp": ts, + } + return self.call("POST", "/authenticatedSession", body=body) + + def logout(self): + try: + self.call("DELETE", "/authenticatedSession") + 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): + raw_ips = inputs.get("ips") + if not raw_ips: + raise Exception("ips is required") + ips = [s.strip() for s in str(raw_ips).split(",") if s.strip()] + if not ips: + raise Exception("ips is required") + + result = client.call( + "POST", + "/security/advanced/blacklistUrls", + params={"action": "ADD_TO_LIST"}, + body={"blacklistUrls": ips}, + ) + if not result: + return {"ok": True, "added": ips} + return result + + +_run(main) diff --git a/integrations/zscaler/scripts/blacklist_url.py b/integrations/zscaler/scripts/blacklist_url.py new file mode 100644 index 0000000..475e714 --- /dev/null +++ b/integrations/zscaler/scripts/blacklist_url.py @@ -0,0 +1,111 @@ +import json, os, sys, time, urllib.parse, urllib.request, urllib.error, http.cookiejar, ssl + + +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 obfuscate_api_key(api_key): + now = str(int(time.time() * 1000)) + n = now[-6:] + r = str(int(n) >> 1).zfill(6) + key = "" + for i in range(len(n)): + key += api_key[int(n[i])] + for j in range(len(r)): + key += api_key[int(r[j]) + 2] + return now, key + + +class Client: + def __init__(self, cfg): + self.cfg = cfg + self.base = str(cfg.get("cloud", "")).rstrip("/") + "/api/v1" + self.ctx = _ctx(cfg) + self.opener = urllib.request.build_opener( + urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar()), + urllib.request.HTTPSHandler(context=self.ctx) if self.ctx else urllib.request.HTTPSHandler(), + ) + + def call(self, method, path, body=None, params=None): + url = self.base + path + if params: + url += "?" + urllib.parse.urlencode(params) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = {"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 self.opener.open(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + def login(self): + ts, obf = obfuscate_api_key(str(self.cfg.get("api_key", ""))) + body = { + "apiKey": obf, + "username": self.cfg.get("username", ""), + "password": self.cfg.get("password", ""), + "timestamp": ts, + } + return self.call("POST", "/authenticatedSession", body=body) + + def logout(self): + try: + self.call("DELETE", "/authenticatedSession") + 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): + raw_urls = inputs.get("urls") + if not raw_urls: + raise Exception("urls is required") + urls = [s.strip() for s in str(raw_urls).split(",") if s.strip()] + if not urls: + raise Exception("urls is required") + + result = client.call( + "POST", + "/security/advanced/blacklistUrls", + params={"action": "ADD_TO_LIST"}, + body={"blacklistUrls": urls}, + ) + if not result: + return {"ok": True, "added": urls} + return result + + +_run(main) diff --git a/integrations/zscaler/scripts/category_add_url.py b/integrations/zscaler/scripts/category_add_url.py new file mode 100644 index 0000000..01dfd3d --- /dev/null +++ b/integrations/zscaler/scripts/category_add_url.py @@ -0,0 +1,115 @@ +import json, os, sys, time, urllib.parse, urllib.request, urllib.error, http.cookiejar, ssl + + +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 obfuscate_api_key(api_key): + now = str(int(time.time() * 1000)) + n = now[-6:] + r = str(int(n) >> 1).zfill(6) + key = "" + for i in range(len(n)): + key += api_key[int(n[i])] + for j in range(len(r)): + key += api_key[int(r[j]) + 2] + return now, key + + +class Client: + def __init__(self, cfg): + self.cfg = cfg + self.base = str(cfg.get("cloud", "")).rstrip("/") + "/api/v1" + self.ctx = _ctx(cfg) + self.opener = urllib.request.build_opener( + urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar()), + urllib.request.HTTPSHandler(context=self.ctx) if self.ctx else urllib.request.HTTPSHandler(), + ) + + def call(self, method, path, body=None, params=None): + url = self.base + path + if params: + url += "?" + urllib.parse.urlencode(params) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = {"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 self.opener.open(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + def login(self): + ts, obf = obfuscate_api_key(str(self.cfg.get("api_key", ""))) + body = { + "apiKey": obf, + "username": self.cfg.get("username", ""), + "password": self.cfg.get("password", ""), + "timestamp": ts, + } + return self.call("POST", "/authenticatedSession", body=body) + + def logout(self): + try: + self.call("DELETE", "/authenticatedSession") + 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): + category_id = inputs.get("category_id") + if not category_id: + raise Exception("category_id is required") + raw_urls = inputs.get("urls") + if not raw_urls: + raise Exception("urls is required") + urls = [s.strip() for s in str(raw_urls).split(",") if s.strip()] + if not urls: + raise Exception("urls is required") + + path = "/urlCategories/" + urllib.parse.quote(str(category_id), safe="") + result = client.call( + "PUT", + path, + params={"action": "ADD_TO_LIST"}, + body={"configuredUrls": urls}, + ) + if not result: + return {"ok": True} + return result + + +_run(main) diff --git a/integrations/zscaler/scripts/get_blacklist.py b/integrations/zscaler/scripts/get_blacklist.py new file mode 100644 index 0000000..2c311fe --- /dev/null +++ b/integrations/zscaler/scripts/get_blacklist.py @@ -0,0 +1,96 @@ +import json, os, sys, time, urllib.parse, urllib.request, urllib.error, http.cookiejar, ssl + + +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 obfuscate_api_key(api_key): + now = str(int(time.time() * 1000)) + n = now[-6:] + r = str(int(n) >> 1).zfill(6) + key = "" + for i in range(len(n)): + key += api_key[int(n[i])] + for j in range(len(r)): + key += api_key[int(r[j]) + 2] + return now, key + + +class Client: + def __init__(self, cfg): + self.cfg = cfg + self.base = str(cfg.get("cloud", "")).rstrip("/") + "/api/v1" + self.ctx = _ctx(cfg) + self.opener = urllib.request.build_opener( + urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar()), + urllib.request.HTTPSHandler(context=self.ctx) if self.ctx else urllib.request.HTTPSHandler(), + ) + + def call(self, method, path, body=None, params=None): + url = self.base + path + if params: + url += "?" + urllib.parse.urlencode(params) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = {"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 self.opener.open(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + def login(self): + ts, obf = obfuscate_api_key(str(self.cfg.get("api_key", ""))) + body = { + "apiKey": obf, + "username": self.cfg.get("username", ""), + "password": self.cfg.get("password", ""), + "timestamp": ts, + } + return self.call("POST", "/authenticatedSession", body=body) + + def logout(self): + try: + self.call("DELETE", "/authenticatedSession") + 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): + return client.call("GET", "/security/advanced") + + +_run(main) diff --git a/integrations/zscaler/scripts/get_categories.py b/integrations/zscaler/scripts/get_categories.py new file mode 100644 index 0000000..5b51ef1 --- /dev/null +++ b/integrations/zscaler/scripts/get_categories.py @@ -0,0 +1,100 @@ +import json, os, sys, time, urllib.parse, urllib.request, urllib.error, http.cookiejar, ssl + + +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 obfuscate_api_key(api_key): + now = str(int(time.time() * 1000)) + n = now[-6:] + r = str(int(n) >> 1).zfill(6) + key = "" + for i in range(len(n)): + key += api_key[int(n[i])] + for j in range(len(r)): + key += api_key[int(r[j]) + 2] + return now, key + + +class Client: + def __init__(self, cfg): + self.cfg = cfg + self.base = str(cfg.get("cloud", "")).rstrip("/") + "/api/v1" + self.ctx = _ctx(cfg) + self.opener = urllib.request.build_opener( + urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar()), + urllib.request.HTTPSHandler(context=self.ctx) if self.ctx else urllib.request.HTTPSHandler(), + ) + + def call(self, method, path, body=None, params=None): + url = self.base + path + if params: + url += "?" + urllib.parse.urlencode(params) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = {"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 self.opener.open(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + def login(self): + ts, obf = obfuscate_api_key(str(self.cfg.get("api_key", ""))) + body = { + "apiKey": obf, + "username": self.cfg.get("username", ""), + "password": self.cfg.get("password", ""), + "timestamp": ts, + } + return self.call("POST", "/authenticatedSession", body=body) + + def logout(self): + try: + self.call("DELETE", "/authenticatedSession") + 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): + custom_only = inputs.get("custom_only", False) + params = None + if str(custom_only).strip().lower() in ("1", "true", "yes"): + params = {"customOnly": "true"} + return client.call("GET", "/urlCategories", params=params) + + +_run(main) diff --git a/integrations/zscaler/scripts/get_whitelist.py b/integrations/zscaler/scripts/get_whitelist.py new file mode 100644 index 0000000..ebe79f4 --- /dev/null +++ b/integrations/zscaler/scripts/get_whitelist.py @@ -0,0 +1,96 @@ +import json, os, sys, time, urllib.parse, urllib.request, urllib.error, http.cookiejar, ssl + + +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 obfuscate_api_key(api_key): + now = str(int(time.time() * 1000)) + n = now[-6:] + r = str(int(n) >> 1).zfill(6) + key = "" + for i in range(len(n)): + key += api_key[int(n[i])] + for j in range(len(r)): + key += api_key[int(r[j]) + 2] + return now, key + + +class Client: + def __init__(self, cfg): + self.cfg = cfg + self.base = str(cfg.get("cloud", "")).rstrip("/") + "/api/v1" + self.ctx = _ctx(cfg) + self.opener = urllib.request.build_opener( + urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar()), + urllib.request.HTTPSHandler(context=self.ctx) if self.ctx else urllib.request.HTTPSHandler(), + ) + + def call(self, method, path, body=None, params=None): + url = self.base + path + if params: + url += "?" + urllib.parse.urlencode(params) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = {"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 self.opener.open(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + def login(self): + ts, obf = obfuscate_api_key(str(self.cfg.get("api_key", ""))) + body = { + "apiKey": obf, + "username": self.cfg.get("username", ""), + "password": self.cfg.get("password", ""), + "timestamp": ts, + } + return self.call("POST", "/authenticatedSession", body=body) + + def logout(self): + try: + self.call("DELETE", "/authenticatedSession") + 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): + return client.call("GET", "/security") + + +_run(main) diff --git a/integrations/zscaler/scripts/sandbox_report.py b/integrations/zscaler/scripts/sandbox_report.py new file mode 100644 index 0000000..a0597eb --- /dev/null +++ b/integrations/zscaler/scripts/sandbox_report.py @@ -0,0 +1,102 @@ +import json, os, sys, time, urllib.parse, urllib.request, urllib.error, http.cookiejar, ssl + + +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 obfuscate_api_key(api_key): + now = str(int(time.time() * 1000)) + n = now[-6:] + r = str(int(n) >> 1).zfill(6) + key = "" + for i in range(len(n)): + key += api_key[int(n[i])] + for j in range(len(r)): + key += api_key[int(r[j]) + 2] + return now, key + + +class Client: + def __init__(self, cfg): + self.cfg = cfg + self.base = str(cfg.get("cloud", "")).rstrip("/") + "/api/v1" + self.ctx = _ctx(cfg) + self.opener = urllib.request.build_opener( + urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar()), + urllib.request.HTTPSHandler(context=self.ctx) if self.ctx else urllib.request.HTTPSHandler(), + ) + + def call(self, method, path, body=None, params=None): + url = self.base + path + if params: + url += "?" + urllib.parse.urlencode(params) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = {"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 self.opener.open(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + def login(self): + ts, obf = obfuscate_api_key(str(self.cfg.get("api_key", ""))) + body = { + "apiKey": obf, + "username": self.cfg.get("username", ""), + "password": self.cfg.get("password", ""), + "timestamp": ts, + } + return self.call("POST", "/authenticatedSession", body=body) + + def logout(self): + try: + self.call("DELETE", "/authenticatedSession") + 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): + md5 = inputs.get("md5") + if not md5: + raise Exception("md5 is required") + details = inputs.get("details") or "full" + + path = "/sandbox/report/" + urllib.parse.quote(str(md5), safe="") + return client.call("GET", path, params={"details": details}) + + +_run(main) diff --git a/integrations/zscaler/scripts/test_connection.py b/integrations/zscaler/scripts/test_connection.py new file mode 100644 index 0000000..a26cc0e --- /dev/null +++ b/integrations/zscaler/scripts/test_connection.py @@ -0,0 +1,97 @@ +import json, os, sys, time, urllib.parse, urllib.request, urllib.error, http.cookiejar, ssl + + +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 obfuscate_api_key(api_key): + now = str(int(time.time() * 1000)) + n = now[-6:] + r = str(int(n) >> 1).zfill(6) + key = "" + for i in range(len(n)): + key += api_key[int(n[i])] + for j in range(len(r)): + key += api_key[int(r[j]) + 2] + return now, key + + +class Client: + def __init__(self, cfg): + self.cfg = cfg + self.base = str(cfg.get("cloud", "")).rstrip("/") + "/api/v1" + self.ctx = _ctx(cfg) + self.opener = urllib.request.build_opener( + urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar()), + urllib.request.HTTPSHandler(context=self.ctx) if self.ctx else urllib.request.HTTPSHandler(), + ) + + def call(self, method, path, body=None, params=None): + url = self.base + path + if params: + url += "?" + urllib.parse.urlencode(params) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = {"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 self.opener.open(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + def login(self): + ts, obf = obfuscate_api_key(str(self.cfg.get("api_key", ""))) + body = { + "apiKey": obf, + "username": self.cfg.get("username", ""), + "password": self.cfg.get("password", ""), + "timestamp": ts, + } + return self.call("POST", "/authenticatedSession", body=body) + + def logout(self): + try: + self.call("DELETE", "/authenticatedSession") + 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): + client.call("GET", "/status") + return {"ok": True} + + +_run(main) diff --git a/integrations/zscaler/scripts/undo_blacklist_ip.py b/integrations/zscaler/scripts/undo_blacklist_ip.py new file mode 100644 index 0000000..5125dd7 --- /dev/null +++ b/integrations/zscaler/scripts/undo_blacklist_ip.py @@ -0,0 +1,111 @@ +import json, os, sys, time, urllib.parse, urllib.request, urllib.error, http.cookiejar, ssl + + +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 obfuscate_api_key(api_key): + now = str(int(time.time() * 1000)) + n = now[-6:] + r = str(int(n) >> 1).zfill(6) + key = "" + for i in range(len(n)): + key += api_key[int(n[i])] + for j in range(len(r)): + key += api_key[int(r[j]) + 2] + return now, key + + +class Client: + def __init__(self, cfg): + self.cfg = cfg + self.base = str(cfg.get("cloud", "")).rstrip("/") + "/api/v1" + self.ctx = _ctx(cfg) + self.opener = urllib.request.build_opener( + urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar()), + urllib.request.HTTPSHandler(context=self.ctx) if self.ctx else urllib.request.HTTPSHandler(), + ) + + def call(self, method, path, body=None, params=None): + url = self.base + path + if params: + url += "?" + urllib.parse.urlencode(params) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = {"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 self.opener.open(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + def login(self): + ts, obf = obfuscate_api_key(str(self.cfg.get("api_key", ""))) + body = { + "apiKey": obf, + "username": self.cfg.get("username", ""), + "password": self.cfg.get("password", ""), + "timestamp": ts, + } + return self.call("POST", "/authenticatedSession", body=body) + + def logout(self): + try: + self.call("DELETE", "/authenticatedSession") + 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): + raw_ips = inputs.get("ips") + if not raw_ips: + raise Exception("ips is required") + ips = [s.strip() for s in str(raw_ips).split(",") if s.strip()] + if not ips: + raise Exception("ips is required") + + result = client.call( + "POST", + "/security/advanced/blacklistUrls", + params={"action": "REMOVE_FROM_LIST"}, + body={"blacklistUrls": ips}, + ) + if not result: + return {"ok": True, "removed": ips} + return result + + +_run(main) diff --git a/integrations/zscaler/scripts/undo_blacklist_url.py b/integrations/zscaler/scripts/undo_blacklist_url.py new file mode 100644 index 0000000..60b986f --- /dev/null +++ b/integrations/zscaler/scripts/undo_blacklist_url.py @@ -0,0 +1,111 @@ +import json, os, sys, time, urllib.parse, urllib.request, urllib.error, http.cookiejar, ssl + + +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 obfuscate_api_key(api_key): + now = str(int(time.time() * 1000)) + n = now[-6:] + r = str(int(n) >> 1).zfill(6) + key = "" + for i in range(len(n)): + key += api_key[int(n[i])] + for j in range(len(r)): + key += api_key[int(r[j]) + 2] + return now, key + + +class Client: + def __init__(self, cfg): + self.cfg = cfg + self.base = str(cfg.get("cloud", "")).rstrip("/") + "/api/v1" + self.ctx = _ctx(cfg) + self.opener = urllib.request.build_opener( + urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar()), + urllib.request.HTTPSHandler(context=self.ctx) if self.ctx else urllib.request.HTTPSHandler(), + ) + + def call(self, method, path, body=None, params=None): + url = self.base + path + if params: + url += "?" + urllib.parse.urlencode(params) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = {"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 self.opener.open(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + def login(self): + ts, obf = obfuscate_api_key(str(self.cfg.get("api_key", ""))) + body = { + "apiKey": obf, + "username": self.cfg.get("username", ""), + "password": self.cfg.get("password", ""), + "timestamp": ts, + } + return self.call("POST", "/authenticatedSession", body=body) + + def logout(self): + try: + self.call("DELETE", "/authenticatedSession") + 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): + raw_urls = inputs.get("urls") + if not raw_urls: + raise Exception("urls is required") + urls = [s.strip() for s in str(raw_urls).split(",") if s.strip()] + if not urls: + raise Exception("urls is required") + + result = client.call( + "POST", + "/security/advanced/blacklistUrls", + params={"action": "REMOVE_FROM_LIST"}, + body={"blacklistUrls": urls}, + ) + if not result: + return {"ok": True, "removed": urls} + return result + + +_run(main) diff --git a/integrations/zscaler/scripts/whitelist_url.py b/integrations/zscaler/scripts/whitelist_url.py new file mode 100644 index 0000000..ef97c46 --- /dev/null +++ b/integrations/zscaler/scripts/whitelist_url.py @@ -0,0 +1,113 @@ +import json, os, sys, time, urllib.parse, urllib.request, urllib.error, http.cookiejar, ssl + + +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 obfuscate_api_key(api_key): + now = str(int(time.time() * 1000)) + n = now[-6:] + r = str(int(n) >> 1).zfill(6) + key = "" + for i in range(len(n)): + key += api_key[int(n[i])] + for j in range(len(r)): + key += api_key[int(r[j]) + 2] + return now, key + + +class Client: + def __init__(self, cfg): + self.cfg = cfg + self.base = str(cfg.get("cloud", "")).rstrip("/") + "/api/v1" + self.ctx = _ctx(cfg) + self.opener = urllib.request.build_opener( + urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar()), + urllib.request.HTTPSHandler(context=self.ctx) if self.ctx else urllib.request.HTTPSHandler(), + ) + + def call(self, method, path, body=None, params=None): + url = self.base + path + if params: + url += "?" + urllib.parse.urlencode(params) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = {"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 self.opener.open(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + def login(self): + ts, obf = obfuscate_api_key(str(self.cfg.get("api_key", ""))) + body = { + "apiKey": obf, + "username": self.cfg.get("username", ""), + "password": self.cfg.get("password", ""), + "timestamp": ts, + } + return self.call("POST", "/authenticatedSession", body=body) + + def logout(self): + try: + self.call("DELETE", "/authenticatedSession") + 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): + raw_urls = inputs.get("urls") + if not raw_urls: + raise Exception("urls is required") + new_urls = [s.strip() for s in str(raw_urls).split(",") if s.strip()] + if not new_urls: + raise Exception("urls is required") + + sec = client.call("GET", "/security") + if not isinstance(sec, dict): + sec = {} + existing = sec.get("whitelistUrls") or [] + merged = existing + [u for u in new_urls if u not in existing] + sec["whitelistUrls"] = merged + + result = client.call("PUT", "/security", body=sec) + if not result: + return {"ok": True, "whitelistUrls": merged} + return result + + +_run(main)