diff --git a/integrations/github/manifest.yaml b/integrations/github/manifest.yaml new file mode 100644 index 0000000..6e0d44e --- /dev/null +++ b/integrations/github/manifest.yaml @@ -0,0 +1,82 @@ +id: github +name: GitHub +version: 1.0.0 +description: "GitHub (REST API) — code security and issue tracking: list secret-scanning, code-scanning and Dependabot alerts for a repository, read a repository, and create an issue. Personal-access-token authentication; stdlib-only, no extra Python dependencies." +changelog: "1.0.0 — Initial release: list secret/code/Dependabot alerts, get repo, create issue." +category: devsecops + +# Per-instance configuration. Auth header 'Authorization: Bearer '. +config_schema: + properties: + token: + type: string + description: "GitHub personal access token (with security_events / repo scopes)" + x-soar-sensitive: true + required: + - token + +commands: + - id: list_secret_scanning_alerts + name: github-list-secret-scanning-alerts + description: "List secret-scanning alerts for a repository." + risk: read + inputs_schema: + properties: + owner: { type: string, description: "Repository owner (user or org)" } + repo: { type: string, description: "Repository name" } + state: { type: string, description: "Filter by state (open, resolved)" } + required: [owner, repo] + outputs_schema: { properties: {} } + - id: list_code_scanning_alerts + name: github-list-code-scanning-alerts + description: "List code-scanning alerts for a repository." + risk: read + inputs_schema: + properties: + owner: { type: string, description: "Repository owner" } + repo: { type: string, description: "Repository name" } + state: { type: string, description: "Filter by state (open, dismissed, fixed)" } + required: [owner, repo] + outputs_schema: { properties: {} } + - id: list_dependabot_alerts + name: github-list-dependabot-alerts + description: "List Dependabot (dependency) alerts for a repository." + risk: read + inputs_schema: + properties: + owner: { type: string, description: "Repository owner" } + repo: { type: string, description: "Repository name" } + state: { type: string, description: "Filter by state (open, dismissed, fixed)" } + required: [owner, repo] + outputs_schema: { properties: {} } + - id: get_repo + name: github-get-repo + description: "Get a repository's metadata." + risk: read + inputs_schema: + properties: + owner: { type: string, description: "Repository owner" } + repo: { type: string, description: "Repository name" } + required: [owner, repo] + outputs_schema: { properties: {} } + - id: create_issue + name: github-create-issue + description: "Create an issue in a repository." + inputs_schema: + properties: + owner: { type: string, description: "Repository owner" } + repo: { type: string, description: "Repository name" } + title: { type: string, description: "Issue title" } + body: { type: string, description: "Issue body" } + labels: { type: string, description: "Comma-separated labels (optional)" } + required: [owner, repo, title] + outputs_schema: { properties: {} } + + - id: test_connection + name: github-test-connection + description: "Verify the token (used by the Test button)." + risk: read + inputs_schema: + properties: {} + required: [] + outputs_schema: { properties: {} } diff --git a/integrations/github/scripts/create_issue.py b/integrations/github/scripts/create_issue.py new file mode 100644 index 0000000..032ca3b --- /dev/null +++ b/integrations/github/scripts/create_issue.py @@ -0,0 +1,82 @@ +import json, os, sys, urllib.parse, urllib.request, urllib.error + +BASE = "https://api.github.com" + + +def _cfg(): + return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}")) + + +def _inputs(): + return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}")) + + +def request(method, path, cfg, body=None, params=None): + url = BASE + path + if params: + clean = {k: v for k, v in params.items() if v not in (None, "")} + if clean: + url += "?" + urllib.parse.urlencode(clean) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = { + "Authorization": "Bearer " + str(cfg.get("token", "")), + "Accept": "application/vnd.github+json", + "X-GitHub-Api-Version": "2022-11-28", + "User-Agent": "Riposte-SOAR", + } + 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) 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) + + +q = lambda v: urllib.parse.quote(str(v), safe="") + + +def _repo(inputs): + o = inputs.get("owner") + r = inputs.get("repo") + if not o: + raise Exception("owner is required") + if not r: + raise Exception("repo is required") + return q(o), q(r) + + +def _csv(v): + return [s.strip() for s in str(v).split(",") if s.strip()] + + +def main(cfg, inputs): + o, r = _repo(inputs) + title = inputs.get("title") + if not title: + raise Exception("title is required") + + issue = {"title": title} + + body = inputs.get("body") + if body: + issue["body"] = body + + labels = inputs.get("labels") + if labels: + issue["labels"] = _csv(labels) + + return request("POST", "/repos/" + o + "/" + r + "/issues", cfg, body=issue) + + +_run(main) diff --git a/integrations/github/scripts/get_repo.py b/integrations/github/scripts/get_repo.py new file mode 100644 index 0000000..a99b985 --- /dev/null +++ b/integrations/github/scripts/get_repo.py @@ -0,0 +1,64 @@ +import json, os, sys, urllib.parse, urllib.request, urllib.error + +BASE = "https://api.github.com" + + +def _cfg(): + return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}")) + + +def _inputs(): + return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}")) + + +def request(method, path, cfg, body=None, params=None): + url = BASE + path + if params: + clean = {k: v for k, v in params.items() if v not in (None, "")} + if clean: + url += "?" + urllib.parse.urlencode(clean) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = { + "Authorization": "Bearer " + str(cfg.get("token", "")), + "Accept": "application/vnd.github+json", + "X-GitHub-Api-Version": "2022-11-28", + "User-Agent": "Riposte-SOAR", + } + 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) 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) + + +q = lambda v: urllib.parse.quote(str(v), safe="") + + +def _repo(inputs): + o = inputs.get("owner") + r = inputs.get("repo") + if not o: + raise Exception("owner is required") + if not r: + raise Exception("repo is required") + return q(o), q(r) + + +def main(cfg, inputs): + o, r = _repo(inputs) + return request("GET", "/repos/" + o + "/" + r, cfg) + + +_run(main) diff --git a/integrations/github/scripts/list_code_scanning_alerts.py b/integrations/github/scripts/list_code_scanning_alerts.py new file mode 100644 index 0000000..d9c897a --- /dev/null +++ b/integrations/github/scripts/list_code_scanning_alerts.py @@ -0,0 +1,69 @@ +import json, os, sys, urllib.parse, urllib.request, urllib.error + +BASE = "https://api.github.com" + + +def _cfg(): + return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}")) + + +def _inputs(): + return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}")) + + +def request(method, path, cfg, body=None, params=None): + url = BASE + path + if params: + clean = {k: v for k, v in params.items() if v not in (None, "")} + if clean: + url += "?" + urllib.parse.urlencode(clean) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = { + "Authorization": "Bearer " + str(cfg.get("token", "")), + "Accept": "application/vnd.github+json", + "X-GitHub-Api-Version": "2022-11-28", + "User-Agent": "Riposte-SOAR", + } + 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) 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) + + +q = lambda v: urllib.parse.quote(str(v), safe="") + + +def _repo(inputs): + o = inputs.get("owner") + r = inputs.get("repo") + if not o: + raise Exception("owner is required") + if not r: + raise Exception("repo is required") + return q(o), q(r) + + +def main(cfg, inputs): + o, r = _repo(inputs) + return request( + "GET", + "/repos/" + o + "/" + r + "/code-scanning/alerts", + cfg, + params={"state": inputs.get("state")}, + ) + + +_run(main) diff --git a/integrations/github/scripts/list_dependabot_alerts.py b/integrations/github/scripts/list_dependabot_alerts.py new file mode 100644 index 0000000..397a71d --- /dev/null +++ b/integrations/github/scripts/list_dependabot_alerts.py @@ -0,0 +1,69 @@ +import json, os, sys, urllib.parse, urllib.request, urllib.error + +BASE = "https://api.github.com" + + +def _cfg(): + return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}")) + + +def _inputs(): + return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}")) + + +def request(method, path, cfg, body=None, params=None): + url = BASE + path + if params: + clean = {k: v for k, v in params.items() if v not in (None, "")} + if clean: + url += "?" + urllib.parse.urlencode(clean) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = { + "Authorization": "Bearer " + str(cfg.get("token", "")), + "Accept": "application/vnd.github+json", + "X-GitHub-Api-Version": "2022-11-28", + "User-Agent": "Riposte-SOAR", + } + 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) 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) + + +q = lambda v: urllib.parse.quote(str(v), safe="") + + +def _repo(inputs): + o = inputs.get("owner") + r = inputs.get("repo") + if not o: + raise Exception("owner is required") + if not r: + raise Exception("repo is required") + return q(o), q(r) + + +def main(cfg, inputs): + o, r = _repo(inputs) + return request( + "GET", + "/repos/" + o + "/" + r + "/dependabot/alerts", + cfg, + params={"state": inputs.get("state")}, + ) + + +_run(main) diff --git a/integrations/github/scripts/list_secret_scanning_alerts.py b/integrations/github/scripts/list_secret_scanning_alerts.py new file mode 100644 index 0000000..c64903c --- /dev/null +++ b/integrations/github/scripts/list_secret_scanning_alerts.py @@ -0,0 +1,69 @@ +import json, os, sys, urllib.parse, urllib.request, urllib.error + +BASE = "https://api.github.com" + + +def _cfg(): + return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}")) + + +def _inputs(): + return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}")) + + +def request(method, path, cfg, body=None, params=None): + url = BASE + path + if params: + clean = {k: v for k, v in params.items() if v not in (None, "")} + if clean: + url += "?" + urllib.parse.urlencode(clean) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = { + "Authorization": "Bearer " + str(cfg.get("token", "")), + "Accept": "application/vnd.github+json", + "X-GitHub-Api-Version": "2022-11-28", + "User-Agent": "Riposte-SOAR", + } + 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) 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) + + +q = lambda v: urllib.parse.quote(str(v), safe="") + + +def _repo(inputs): + o = inputs.get("owner") + r = inputs.get("repo") + if not o: + raise Exception("owner is required") + if not r: + raise Exception("repo is required") + return q(o), q(r) + + +def main(cfg, inputs): + o, r = _repo(inputs) + return request( + "GET", + "/repos/" + o + "/" + r + "/secret-scanning/alerts", + cfg, + params={"state": inputs.get("state")}, + ) + + +_run(main) diff --git a/integrations/github/scripts/test_connection.py b/integrations/github/scripts/test_connection.py new file mode 100644 index 0000000..271385e --- /dev/null +++ b/integrations/github/scripts/test_connection.py @@ -0,0 +1,51 @@ +import json, os, sys, urllib.parse, urllib.request, urllib.error + +BASE = "https://api.github.com" + + +def _cfg(): + return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}")) + + +def _inputs(): + return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}")) + + +def request(method, path, cfg, body=None, params=None): + url = BASE + path + if params: + clean = {k: v for k, v in params.items() if v not in (None, "")} + if clean: + url += "?" + urllib.parse.urlencode(clean) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = { + "Authorization": "Bearer " + str(cfg.get("token", "")), + "Accept": "application/vnd.github+json", + "X-GitHub-Api-Version": "2022-11-28", + "User-Agent": "Riposte-SOAR", + } + 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) 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): + resp = request("GET", "/user", cfg) + return {"ok": True, "login": resp.get("login")} + + +_run(main)