Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
webhooks/thinkst: Use datetime_to_global_time and ISO 8601 syntax.
Niloth Pc6d7e42252ce
diff --git a/zerver/webhooks/thinkst/tests.py b/zerver/webhooks/thinkst/tests.pyindex 4256ab18240..af5fff13f5b 100644--- a/zerver/webhooks/thinkst/tests.py+++ b/zerver/webhooks/thinkst/tests.py@@ -12,7 +12,7 @@ class ThinkstHookTests(WebhookTestCase):"This is a dummy incident.\n\n""**Incident ID:** `aa875f255f94e3ffe40dc85cf1a8b1e0`\n""**Kind:** Dummy Incident\n"- "**Timestamp:** <time:2020-06-09 13:59:38Z>\n"+ "**Timestamp:** <time:2020-06-09T13:59:38+00:00>\n""**Canary IP:** `1.1.1.1`\n""**Source IP:** `2.2.2.2`\n""**Reverse DNS:** `attacker-ip.local`\n"@@ -35,7 +35,7 @@ class ThinkstHookTests(WebhookTestCase):"1.1.1.1.\n\n""**Incident ID:** `3f25fec9e18c7673dcc468800b7af0a6`\n""**Kind:** Consolidated Network Port Scan\n"- "**Timestamp:** <time:2020-07-20 16:18:40Z>\n"+ "**Timestamp:** <time:2020-07-20T16:18:40+00:00>\n""**Canary IP:** `1.1.1.1`\n""**Canary location:** dining room\n""**Source IP:** `1.1.1.1`\n"@@ -60,7 +60,7 @@ class ThinkstHookTests(WebhookTestCase):"(bar-foo) at 1.1.1.1.\n\n""**Incident ID:** `a7bb317ba2072415462233cef3bc615a`\n""**Kind:** Shared File (Re)Opened\n"- "**Timestamp:** <time:2020-07-20 16:27:20Z>\n"+ "**Timestamp:** <time:2020-07-20T16:27:20+00:00>\n""**Canary IP:** `1.1.1.1`\n""**Canary location:** dining room\n""**Source IP:** `1.1.1.1`\n"@@ -87,7 +87,7 @@ class ThinkstHookTests(WebhookTestCase):"(foo-bar) at 1.1.1.1.\n\n""**Incident ID:** `9060473d2da98afe494adad648495620`\n""**Kind:** Host Port Scan\n"- "**Timestamp:** <time:2020-07-20 16:26:30Z>\n"+ "**Timestamp:** <time:2020-07-20T16:26:30+00:00>\n""**Canary IP:** `1.1.1.1`\n""**Canary location:** dining room\n""**Source IP:** `1.1.1.1`\n"@@ -109,7 +109,7 @@ class ThinkstHookTests(WebhookTestCase):"(foo-bar) at 1.1.1.1.\n\n""**Incident ID:** `1ef86f5cf4090a5252c6f453c5cd46bd`\n""**Kind:** HTTP Login Attempt\n"- "**Timestamp:** <time:2020-07-20 14:55:45Z>\n"+ "**Timestamp:** <time:2020-07-20T14:55:45+00:00>\n""**Canary IP:** `1.1.1.1`\n""**Canary location:** dining room\n""**Source IP:** `1.1.1.1`\n"@@ -134,7 +134,7 @@ class ThinkstHookTests(WebhookTestCase):"(foo-bar) at 1.1.1.1.\n\n""**Incident ID:** `4ef51a936d05e0a6b4846378c8c38d2c`\n""**Kind:** SSH Login Attempt\n"- "**Timestamp:** <time:2020-07-20 16:26:22Z>\n"+ "**Timestamp:** <time:2020-07-20T16:26:22+00:00>\n""**Canary IP:** `1.1.1.1`\n""**Canary location:** dining room\n""**Source IP:** `1.1.1.1`\n"@@ -158,7 +158,7 @@ class ThinkstHookTests(WebhookTestCase):"This is a dummy incident.\n\n""**Incident ID:** `aa875f255f94e3ffe40dc85cf1a8b1e0`\n""**Kind:** Dummy Incident\n"- "**Timestamp:** <time:2020-06-09 13:59:38Z>\n"+ "**Timestamp:** <time:2020-06-09T13:59:38+00:00>\n""**Canary IP:** `1.1.1.1`\n""**Source IP:** `2.2.2.2`\n""**Reverse DNS:** `attacker-ip.local`\n"@@ -182,7 +182,7 @@ class ThinkstHookTests(WebhookTestCase):"**Incident ID:** `db6f9b5528c6c6c385cb3bb63f5949c8`\n""**Token:** `dbwx4d68flwh2u5zku56nogu6`\n""**Kind:** MS Word .docx Document\n"- "**Timestamp:** <time:2020-07-20 14:40:15Z>\n"+ "**Timestamp:** <time:2020-07-20T14:40:15+00:00>\n""**Triggered:** 5 times\n""**Accept:** `*/*`\n""**Accept-Encoding:** gzip, deflate\n"@@ -209,7 +209,7 @@ class ThinkstHookTests(WebhookTestCase):"**Incident ID:** `533395067f6d655cd19384bc6991cc0f`\n""**Token:** `ew5n8gqtb82m4uegrttarn2zu`\n""**Kind:** Remote Web Image\n"- "**Timestamp:** <time:2020-07-20 16:33:12Z>\n"+ "**Timestamp:** <time:2020-07-20T16:33:12+00:00>\n""**Triggered:** 37 times\n""**Accept:** `image/webp,*/*`\n""**Accept-Encoding:** gzip, deflate\n"@@ -240,7 +240,7 @@ class ThinkstHookTests(WebhookTestCase):"**Incident ID:** `db6f9b5528c6c6c385cb3bb63f5949c8`\n""**Token:** `dbwx4d68flwh2u5zku56nogu6`\n""**Kind:** MS Word .docx Document\n"- "**Timestamp:** <time:2020-07-20 14:40:15Z>\n"+ "**Timestamp:** <time:2020-07-20T14:40:15+00:00>\n""**Triggered:** 5 times\n""**Accept:** `*/*`\n""**Accept-Encoding:** gzip, deflate\n"diff --git a/zerver/webhooks/thinkst/view.py b/zerver/webhooks/thinkst/view.pyindex 8fc2d87f6c2..1738eaa5870 100644--- a/zerver/webhooks/thinkst/view.py+++ b/zerver/webhooks/thinkst/view.py@@ -1,9 +1,13 @@# Webhooks for external integrations.+from datetime import timezone++import dateutil.parserfrom django.http import HttpRequest, HttpResponsefrom zerver.decorator import webhook_viewfrom zerver.lib.response import json_success+from zerver.lib.timestamp import datetime_to_global_timefrom zerver.lib.typed_endpoint import JsonBodyPayload, typed_endpointfrom zerver.lib.validator import WildValue, check_int, check_string, check_unionfrom zerver.lib.webhooks.common import OptionalUserSpecifiedTopicStr, check_send_webhook_message@@ -75,9 +79,11 @@ def body(message: WildValue) -> str:if "Timestamp" in message:timestamp = message["Timestamp"].tame(check_string)# Thinkst datetime format: https://help.canary.tools/hc/en-gb/articles/360012727777-How-do-I-configure-notifications-for-a-Generic-Webhook#h_01K71QZ806C5D49RYB6RBXSZ1B- # Convert "YYYY-MM-DD HH:MM:SS (UTC)" -> "YYYY-MM-DD HH:MM:SSZ"- formatted_timestamp = timestamp.replace("(UTC)", "").strip() + "Z"- body += f"**Timestamp:** <time:{formatted_timestamp}>\n"+ # "YYYY-MM-DD HH:MM:SS (UTC)"+ formatted_timestamp = timestamp.replace("(UTC)", "").strip()+ dt = dateutil.parser.parse(formatted_timestamp).replace(tzinfo=timezone.utc)+ global_time = datetime_to_global_time(dt)+ body += f"**Timestamp:** {global_time}\n"if "CanaryIP" in message:body += f"**Canary IP:** `{message['CanaryIP'].tame(check_string)}`\n"