2020-07-20 19:59:28 +02:00
|
|
|
# Webhooks for external integrations.
|
2021-12-17 07:03:22 +01:00
|
|
|
from typing import Optional
|
2020-07-20 19:59:28 +02:00
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2020-08-20 00:32:15 +02:00
|
|
|
from zerver.decorator import webhook_view
|
2020-07-20 19:59:28 +02:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
|
|
|
from zerver.lib.response import json_success
|
2021-12-17 07:03:22 +01:00
|
|
|
from zerver.lib.validator import WildValue, check_string, to_wild_value
|
2020-07-20 19:59:28 +02:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
|
|
|
|
2021-12-16 21:34:08 +01:00
|
|
|
@webhook_view("Canarytokens")
|
2020-07-20 19:59:28 +02:00
|
|
|
@has_request_variables
|
|
|
|
def api_canarytoken_webhook(
|
2021-02-12 08:19:30 +01:00
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2021-12-17 07:03:22 +01:00
|
|
|
message: WildValue = REQ(argument_type="body", converter=to_wild_value),
|
2021-02-12 08:20:45 +01:00
|
|
|
user_specified_topic: Optional[str] = REQ("topic", default=None),
|
2020-07-20 19:59:28 +02:00
|
|
|
) -> HttpResponse:
|
|
|
|
"""
|
|
|
|
Construct a response to a webhook event from a Thinkst canarytoken from
|
|
|
|
canarytokens.org. Canarytokens from Thinkst's paid product have a different
|
|
|
|
schema and should use the "thinkst" integration. See linked documentation
|
|
|
|
below for a schema:
|
|
|
|
|
|
|
|
https://help.canary.tools/hc/en-gb/articles/360002426577-How-do-I-configure-notifications-for-a-Generic-Webhook-
|
|
|
|
"""
|
2021-02-12 08:20:45 +01:00
|
|
|
topic = "canarytoken alert"
|
2021-02-12 08:19:30 +01:00
|
|
|
body = (
|
2021-12-17 07:03:22 +01:00
|
|
|
f"**:alert: Canarytoken has been triggered on {message['time'].tame(check_string)}!**\n\n"
|
|
|
|
f"{message['memo'].tame(check_string)} \n\n"
|
|
|
|
f"[Manage this canarytoken]({message['manage_url'].tame(check_string)})"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2020-07-20 19:59:28 +02:00
|
|
|
|
|
|
|
if user_specified_topic:
|
|
|
|
topic = user_specified_topic
|
|
|
|
|
|
|
|
check_send_webhook_message(request, user_profile, topic, body)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|