|
1 | 1 | import json |
2 | | -from typing import Union, Dict, Any, cast |
| 2 | +from typing import Union, Dict, Any |
3 | 3 |
|
4 | | -import requests # type: ignore |
5 | 4 | from api.dependencies.rate_limits import get_limiter |
6 | 5 | from core.logging import get_module_logger |
7 | 6 | from fastapi import APIRouter, HTTPException, Request, Body |
8 | 7 | from integrations.sentinel import log_to_sentinel |
9 | 8 | from models.webhooks import ( |
10 | | - AwsSnsPayload, |
11 | 9 | WebhookPayload, |
12 | | - AccessRequest, |
13 | | - UpptimePayload, |
14 | | - WebhookResult, |
15 | 10 | ) |
16 | 11 | from modules.slack import webhooks |
17 | | -from modules.webhooks.base import validate_payload |
18 | | -from server.event_handlers import aws |
19 | | -from server.utils import log_ops_message |
| 12 | +from modules.webhooks.base import handle_webhook_payload |
20 | 13 |
|
21 | 14 |
|
22 | 15 | logger = get_module_logger() |
@@ -107,125 +100,6 @@ def handle_webhook( |
107 | 100 | return {"ok": True} |
108 | 101 |
|
109 | 102 |
|
110 | | -def handle_webhook_payload( |
111 | | - payload_dict: dict, |
112 | | - request: Request, |
113 | | -) -> WebhookResult: |
114 | | - """Process and validate the webhook payload. |
115 | | -
|
116 | | - Returns: |
117 | | - dict: A dictionary containing: |
118 | | - - status (str): The status of the operation (e.g., "success", "error"). |
119 | | - - action (Literal["post", "log", "none"]): The action to take. |
120 | | - - payload (Optional[WebhookPayload]): The payload to post, if applicable. |
121 | | - """ |
122 | | - logger.info("processing_webhook_payload", payload=payload_dict) |
123 | | - payload_validation_result = validate_payload(payload_dict) |
124 | | - |
125 | | - webhook_result = WebhookResult( |
126 | | - status="error", message="Failed to process payload for unknown reasons" |
127 | | - ) |
128 | | - if payload_validation_result is not None: |
129 | | - payload_type, validated_payload = payload_validation_result |
130 | | - else: |
131 | | - error_message = "No matching model found for payload" |
132 | | - return WebhookResult(status="error", message=error_message) |
133 | | - |
134 | | - match payload_type.__name__: |
135 | | - case "WebhookPayload": |
136 | | - webhook_result = WebhookResult( |
137 | | - status="success", action="post", payload=validated_payload |
138 | | - ) |
139 | | - case "AwsSnsPayload": |
140 | | - aws_sns_payload_instance = cast(AwsSnsPayload, validated_payload) |
141 | | - aws_sns_payload = aws.validate_sns_payload( |
142 | | - aws_sns_payload_instance, |
143 | | - request.state.bot.client, |
144 | | - ) |
145 | | - |
146 | | - if aws_sns_payload.Type == "SubscriptionConfirmation": |
147 | | - requests.get(aws_sns_payload.SubscribeURL, timeout=60) |
148 | | - logger.info( |
149 | | - "subscribed_webhook_to_topic", |
150 | | - webhook_id=aws_sns_payload.TopicArn, |
151 | | - subscribed_topic=aws_sns_payload.TopicArn, |
152 | | - ) |
153 | | - log_ops_message( |
154 | | - request.state.bot.client, |
155 | | - f"Subscribed webhook {id} to topic {aws_sns_payload.TopicArn}", |
156 | | - ) |
157 | | - webhook_result = WebhookResult( |
158 | | - status="success", action="log", payload=None |
159 | | - ) |
160 | | - |
161 | | - if aws_sns_payload.Type == "UnsubscribeConfirmation": |
162 | | - log_ops_message( |
163 | | - request.state.bot.client, |
164 | | - f"{aws_sns_payload.TopicArn} unsubscribed from webhook {id}", |
165 | | - ) |
166 | | - webhook_result = WebhookResult( |
167 | | - status="success", action="log", payload=None |
168 | | - ) |
169 | | - |
170 | | - if aws_sns_payload.Type == "Notification": |
171 | | - blocks = aws.parse(aws_sns_payload, request.state.bot.client) |
172 | | - if not blocks: |
173 | | - logger.info( |
174 | | - "payload_empty_message", |
175 | | - payload_type="AwsSnsPayload", |
176 | | - sns_type=aws_sns_payload.Type, |
177 | | - ) |
178 | | - return WebhookResult( |
179 | | - status="error", |
180 | | - action="none", |
181 | | - message="Empty AWS SNS Notification message", |
182 | | - ) |
183 | | - webhook_result = WebhookResult( |
184 | | - status="success", |
185 | | - action="post", |
186 | | - payload=WebhookPayload(blocks=blocks), |
187 | | - ) |
188 | | - |
189 | | - case "AccessRequest": |
190 | | - message = str(cast(AccessRequest, validated_payload).model_dump()) |
191 | | - webhook_result = WebhookResult( |
192 | | - status="success", |
193 | | - action="post", |
194 | | - payload=WebhookPayload(text=message), |
195 | | - ) |
196 | | - |
197 | | - case "UpptimePayload": |
198 | | - text = cast(UpptimePayload, validated_payload).text |
199 | | - header_text = "📈 Web Application Status Changed!" |
200 | | - blocks = [ |
201 | | - {"type": "section", "text": {"type": "mrkdwn", "text": " "}}, |
202 | | - { |
203 | | - "type": "header", |
204 | | - "text": {"type": "plain_text", "text": f"{header_text}"}, |
205 | | - }, |
206 | | - { |
207 | | - "type": "section", |
208 | | - "text": { |
209 | | - "type": "mrkdwn", |
210 | | - "text": f"{text}", |
211 | | - }, |
212 | | - }, |
213 | | - ] |
214 | | - webhook_result = WebhookResult( |
215 | | - status="success", |
216 | | - action="post", |
217 | | - payload=WebhookPayload(blocks=blocks), |
218 | | - ) |
219 | | - |
220 | | - case _: |
221 | | - webhook_result = WebhookResult( |
222 | | - status="error", |
223 | | - message="No matching model found for payload", |
224 | | - ) |
225 | | - |
226 | | - return webhook_result |
227 | | - |
228 | | - |
229 | 103 | def append_incident_buttons(payload: WebhookPayload, webhook_id) -> WebhookPayload: |
230 | 104 | if payload.attachments is None: |
231 | 105 | payload.attachments = [] |
|
0 commit comments