diff --git a/integrations/mattermost/manifest.yaml b/integrations/mattermost/manifest.yaml new file mode 100644 index 0000000..e1a5f02 --- /dev/null +++ b/integrations/mattermost/manifest.yaml @@ -0,0 +1,80 @@ +id: mattermost +name: Mattermost +version: 1.0.0 +description: "Mattermost (REST API v4) — SOC ChatOps: create posts, look up channels by name, search posts, create channels, and look up users. Bearer-token authentication; stdlib-only, no extra Python dependencies." +changelog: "1.0.0 — Initial release: create post, get channel by name, search posts, create channel, get user." +category: notification + +# Per-instance configuration. Auth header 'Authorization: Bearer '. +config_schema: + properties: + base_url: + type: string + description: "Mattermost server URL (e.g. https://mm.example.com)" + token: + type: string + description: "Personal access / bot token" + x-soar-sensitive: true + required: + - base_url + - token + +commands: + - id: create_post + name: mattermost-create-post + description: "Create a post in a channel." + inputs_schema: + properties: + channel_id: { type: string, description: "Channel ID" } + message: { type: string, description: "Message text (Markdown supported)" } + required: [channel_id, message] + outputs_schema: { properties: {} } + - id: get_channel_by_name + name: mattermost-get-channel-by-name + description: "Get a channel by team name and channel name." + risk: read + inputs_schema: + properties: + team_name: { type: string, description: "Team name" } + channel_name: { type: string, description: "Channel name" } + required: [team_name, channel_name] + outputs_schema: { properties: {} } + - id: search_posts + name: mattermost-search-posts + description: "Search posts within a team." + risk: read + inputs_schema: + properties: + team_id: { type: string, description: "Team ID" } + terms: { type: string, description: "Search terms" } + required: [team_id, terms] + outputs_schema: { properties: {} } + - id: create_channel + name: mattermost-create-channel + description: "Create a channel." + inputs_schema: + properties: + team_id: { type: string, description: "Team ID" } + name: { type: string, description: "Channel URL name (lowercase)" } + display_name: { type: string, description: "Display name" } + channel_type: { type: string, description: "O (public) or P (private), default O" } + required: [team_id, name, display_name] + outputs_schema: { properties: {} } + - id: get_user + name: mattermost-get-user + description: "Look up a user by username." + risk: read + inputs_schema: + properties: + username: { type: string, description: "Username" } + required: [username] + outputs_schema: { properties: {} } + + - id: test_connection + name: mattermost-test-connection + description: "Verify connectivity and the token (used by the Test button)." + risk: read + inputs_schema: + properties: {} + required: [] + outputs_schema: { properties: {} } diff --git a/integrations/mattermost/scripts/create_channel.py b/integrations/mattermost/scripts/create_channel.py new file mode 100644 index 0000000..f7f3fce --- /dev/null +++ b/integrations/mattermost/scripts/create_channel.py @@ -0,0 +1,64 @@ +import json, os, sys, 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 request(method, path, cfg, body=None, params=None): + url = str(cfg.get("base_url", "")).rstrip("/") + "/api/v4" + 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/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) 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): + team_id = inputs.get("team_id") + name = inputs.get("name") + display_name = inputs.get("display_name") + channel_type = inputs.get("channel_type") + if not team_id: + raise Exception("team_id is required") + if not name: + raise Exception("name is required") + if not display_name: + raise Exception("display_name is required") + + return request( + "POST", + "/channels", + cfg, + body={ + "team_id": team_id, + "name": name, + "display_name": display_name, + "type": (channel_type or "O"), + }, + ) + + +_run(main) diff --git a/integrations/mattermost/scripts/create_post.py b/integrations/mattermost/scripts/create_post.py new file mode 100644 index 0000000..16d7059 --- /dev/null +++ b/integrations/mattermost/scripts/create_post.py @@ -0,0 +1,50 @@ +import json, os, sys, 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 request(method, path, cfg, body=None, params=None): + url = str(cfg.get("base_url", "")).rstrip("/") + "/api/v4" + 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/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) 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): + channel_id = inputs.get("channel_id") + message = inputs.get("message") + if not channel_id: + raise Exception("channel_id is required") + if not message: + raise Exception("message is required") + + return request("POST", "/posts", cfg, body={"channel_id": channel_id, "message": message}) + + +_run(main) diff --git a/integrations/mattermost/scripts/get_channel_by_name.py b/integrations/mattermost/scripts/get_channel_by_name.py new file mode 100644 index 0000000..f2d129f --- /dev/null +++ b/integrations/mattermost/scripts/get_channel_by_name.py @@ -0,0 +1,51 @@ +import json, os, sys, 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 request(method, path, cfg, body=None, params=None): + url = str(cfg.get("base_url", "")).rstrip("/") + "/api/v4" + 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/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) 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): + team_name = inputs.get("team_name") + channel_name = inputs.get("channel_name") + if not team_name: + raise Exception("team_name is required") + if not channel_name: + raise Exception("channel_name is required") + + q = lambda v: urllib.parse.quote(str(v), safe="") + return request("GET", "/teams/name/" + q(team_name) + "/channels/name/" + q(channel_name), cfg) + + +_run(main) diff --git a/integrations/mattermost/scripts/get_user.py b/integrations/mattermost/scripts/get_user.py new file mode 100644 index 0000000..1a993c5 --- /dev/null +++ b/integrations/mattermost/scripts/get_user.py @@ -0,0 +1,48 @@ +import json, os, sys, 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 request(method, path, cfg, body=None, params=None): + url = str(cfg.get("base_url", "")).rstrip("/") + "/api/v4" + 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/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) 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): + username = inputs.get("username") + if not username: + raise Exception("username is required") + + q = lambda v: urllib.parse.quote(str(v), safe="") + return request("GET", "/users/username/" + q(username), cfg) + + +_run(main) diff --git a/integrations/mattermost/scripts/search_posts.py b/integrations/mattermost/scripts/search_posts.py new file mode 100644 index 0000000..7d4f84b --- /dev/null +++ b/integrations/mattermost/scripts/search_posts.py @@ -0,0 +1,56 @@ +import json, os, sys, 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 request(method, path, cfg, body=None, params=None): + url = str(cfg.get("base_url", "")).rstrip("/") + "/api/v4" + 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/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) 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): + team_id = inputs.get("team_id") + terms = inputs.get("terms") + if not team_id: + raise Exception("team_id is required") + if not terms: + raise Exception("terms is required") + + q = lambda v: urllib.parse.quote(str(v), safe="") + return request( + "POST", + "/teams/" + q(team_id) + "/posts/search", + cfg, + body={"terms": terms, "is_or_search": True}, + ) + + +_run(main) diff --git a/integrations/mattermost/scripts/test_connection.py b/integrations/mattermost/scripts/test_connection.py new file mode 100644 index 0000000..6f2627b --- /dev/null +++ b/integrations/mattermost/scripts/test_connection.py @@ -0,0 +1,44 @@ +import json, os, sys, 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 request(method, path, cfg, body=None, params=None): + url = str(cfg.get("base_url", "")).rstrip("/") + "/api/v4" + 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/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) 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", "/users/me", cfg) + return {"ok": True, "user": resp.get("username")} + + +_run(main)