-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
426 lines (357 loc) · 16.8 KB
/
app.py
File metadata and controls
426 lines (357 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#!/usr/bin/env python3
"""
Slack-to-Glue Webhook Service
A simple Python web app that accepts POST requests to /services/<ID>
and forwards them to configured endpoints based on YAML configuration.
"""
import os
import yaml
import logging
from flask import Flask, request, jsonify
import requests
from typing import Dict, Any, Optional
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
app = Flask(__name__)
class ConfigError(Exception):
"""Raised when there's an issue with configuration"""
pass
class WebhookProcessor:
"""Handles webhook processing and forwarding"""
def __init__(self, config_path: str = "config.yml"):
self.config_path = config_path
self.config = self._load_config()
def _load_config(self) -> Dict[str, Any]:
"""Load configuration from YAML file"""
try:
with open(self.config_path, 'r') as file:
config = yaml.safe_load(file)
logger.info(f"Configuration loaded from {self.config_path}")
return config
except FileNotFoundError:
logger.error(f"Configuration file {self.config_path} not found")
raise ConfigError(f"Configuration file {self.config_path} not found")
except yaml.YAMLError as e:
logger.error(f"Error parsing YAML configuration: {e}")
raise ConfigError(f"Error parsing YAML configuration: {e}")
def reload_config(self) -> None:
"""Reload configuration from YAML file (useful in debug mode)"""
self.config = self._load_config()
def get_service_config(self, service_id: str) -> Optional[Dict[str, Any]]:
"""Get configuration for a specific service ID"""
# Reload config in debug mode for easier development
if app.debug or os.environ.get('DEBUG', 'false').lower() == 'true':
self.reload_config()
logger.debug(f"Config reloaded in debug mode")
services = self.config.get('services', {})
return services.get(service_id)
def _convert_slack_to_markdown(self, text: str) -> str:
"""Convert Slack markdown format to standard markdown"""
import re
# Convert Slack links: <url|text> to [text](url)
text = re.sub(r'<([^|>]+)\|([^>]+)>', r'[\2](\1)', text)
# Convert bare URLs: <url> to just url (or keep as-is for markdown)
text = re.sub(r'<(https?://[^>]+)>', r'\1', text)
return text
def _post_to_slack(self, payload: Dict[str, Any], slack_webhook: str) -> None:
"""
Post the original payload to a Slack webhook (best-effort).
Does not raise exceptions - only logs errors.
"""
try:
logger.info(f"Forwarding to Slack webhook")
response = requests.post(
slack_webhook,
json=payload,
headers={'Content-Type': 'application/json'},
timeout=self.config.get('global', {}).get('timeout_seconds', 30)
)
if response.status_code == 200:
logger.info(f"Successfully forwarded to Slack webhook")
else:
logger.error(f"Slack webhook returned status {response.status_code}: {response.text}")
except requests.exceptions.RequestException as e:
logger.error(f"Error forwarding to Slack webhook: {e}")
except Exception as e:
logger.error(f"Unexpected error posting to Slack webhook: {e}")
def process_webhook(self, service_id: str, payload: Dict[str, Any]) -> Dict[str, Any]:
"""Process webhook and forward to configured endpoint"""
service_config = self.get_service_config(service_id)
if not service_config:
logger.warning(f"No configuration found for service ID: {service_id}")
return {
'status': 'error',
'message': f'No configuration found for service ID: {service_id}'
}
# Get required configuration values
target = service_config.get('target')
webhook_url = service_config.get('webhook_url')
if not all([target, webhook_url]):
logger.error(f"Incomplete configuration for service {service_id}")
return {
'status': 'error',
'message': 'Incomplete service configuration (missing target or webhook_url)'
}
# Extract the thread subject from the top-level text field
thread_subject = payload.get('text', '').strip()
# Remove markdown heading symbols if present
if thread_subject.startswith('#'):
thread_subject = thread_subject.lstrip('#').strip()
# Extract the main message text from the attachments
message_text = ''
attachments = payload.get('attachments', [])
if attachments and len(attachments) > 0:
message_text = attachments[0].get('text', '')
# If no text in attachments, fall back to top-level text
if not message_text:
message_text = payload.get('text', '')
# If we used the text as subject, don't duplicate it
if message_text == payload.get('text', ''):
thread_subject = '' # Don't create a thread if there's only one piece of text
# If still no text, try other fields
if not message_text and 'message' in payload:
message_text = payload['message'].get('text', '')
if not message_text:
logger.warning(f"No text content found in webhook payload for service {service_id}")
message_text = str(payload) # Fallback to stringified payload
# Convert Slack markdown to standard markdown
message_text = self._convert_slack_to_markdown(message_text)
# Prepare the Glue-formatted payload
# Glue expects: { "text": "...", "target": "grp_xxx or thr_xxx" }
# Optionally with "threadSubject" to create a new thread
forward_payload = {
'text': message_text,
'target': target
}
# Add threadSubject if we have one (creates a new thread in Glue)
if thread_subject:
forward_payload['threadSubject'] = thread_subject
logger.info(f"Creating Glue thread with subject: {thread_subject}")
# Forward the request to Glue
try:
logger.info(f"Forwarding webhook for service {service_id} to {webhook_url}")
response = requests.post(
webhook_url,
json=forward_payload,
headers={'Content-Type': 'application/json'},
timeout=30
)
if response.status_code == 200:
logger.info(f"Successfully forwarded webhook for service {service_id}")
result = {
'status': 'success',
'message': 'Webhook forwarded successfully',
'response_code': response.status_code
}
else:
logger.warning(f"Target server returned status {response.status_code}")
result = {
'status': 'warning',
'message': f'Target server returned status {response.status_code}',
'response_code': response.status_code
}
except requests.exceptions.RequestException as e:
logger.error(f"Error forwarding webhook: {e}")
result = {
'status': 'error',
'message': 'Error forwarding webhook to target server'
}
# Optionally forward to Slack webhook (best-effort)
slack_webhook = service_config.get('slack_webhook')
if slack_webhook:
self._post_to_slack(payload, slack_webhook)
return result
def test_webhooks(self) -> None:
"""
Test mode: Send test messages to all configured webhooks and exit.
Sends to both Glue webhooks and Slack webhooks (if configured).
"""
print("\n=== Webhook Test Mode ===\n")
services = self.config.get('services', {})
if not services:
print("❌ No services configured in config file")
return
print(f"Found {len(services)} service(s) to test\n")
test_timestamp = __import__('datetime').datetime.now().strftime("%Y-%m-%d %H:%M:%S")
results = []
for service_id, service_config in services.items():
print(f"Testing service: {service_id}")
description = service_config.get('description', 'No description')
print(f" Description: {description}")
# Test Glue webhook
webhook_url = service_config.get('webhook_url')
target = service_config.get('target')
if webhook_url and target:
test_payload = {
'text': f'Test message from slack-to-glue-webhook\nTimestamp: {test_timestamp}',
'target': target,
'threadSubject': f'Test Message - {test_timestamp}'
}
try:
print(f" → Sending to Glue webhook: {webhook_url}")
response = requests.post(
webhook_url,
json=test_payload,
headers={'Content-Type': 'application/json'},
timeout=self.config.get('global', {}).get('timeout_seconds', 30)
)
if response.status_code == 200:
print(f" ✓ Glue webhook: SUCCESS (status {response.status_code})")
results.append((service_id, 'Glue', 'SUCCESS', response.status_code))
else:
print(f" ✗ Glue webhook: FAILED (status {response.status_code})")
print(f" Response: {response.text[:200]}")
results.append((service_id, 'Glue', 'FAILED', response.status_code))
except requests.exceptions.RequestException as e:
print(f" ✗ Glue webhook: ERROR - {e}")
results.append((service_id, 'Glue', 'ERROR', str(e)))
else:
print(f" ⊘ Glue webhook: SKIPPED (missing webhook_url or target)")
results.append((service_id, 'Glue', 'SKIPPED', 'Missing config'))
# Test Slack webhook (if configured)
slack_webhook = service_config.get('slack_webhook')
if slack_webhook:
slack_payload = {
'text': f'Test message from slack-to-glue-webhook\nTimestamp: {test_timestamp}'
}
try:
print(f" → Sending to Slack webhook: {slack_webhook}")
response = requests.post(
slack_webhook,
json=slack_payload,
headers={'Content-Type': 'application/json'},
timeout=self.config.get('global', {}).get('timeout_seconds', 30)
)
if response.status_code == 200:
print(f" ✓ Slack webhook: SUCCESS (status {response.status_code})")
results.append((service_id, 'Slack', 'SUCCESS', response.status_code))
else:
print(f" ✗ Slack webhook: FAILED (status {response.status_code})")
print(f" Response: {response.text[:200]}")
results.append((service_id, 'Slack', 'FAILED', response.status_code))
except requests.exceptions.RequestException as e:
print(f" ✗ Slack webhook: ERROR - {e}")
results.append((service_id, 'Slack', 'ERROR', str(e)))
else:
print(f" ⊘ Slack webhook: SKIPPED (not configured)")
results.append((service_id, 'Slack', 'SKIPPED', 'Not configured'))
print() # Blank line between services
# Print summary
print("=== Test Summary ===")
success_count = sum(1 for r in results if r[2] == 'SUCCESS')
failed_count = sum(1 for r in results if r[2] == 'FAILED')
error_count = sum(1 for r in results if r[2] == 'ERROR')
skipped_count = sum(1 for r in results if r[2] == 'SKIPPED')
print(f"Total webhooks tested: {len(results)}")
print(f" ✓ Success: {success_count}")
print(f" ✗ Failed: {failed_count}")
print(f" ✗ Errors: {error_count}")
print(f" ⊘ Skipped: {skipped_count}")
print()
# Exit with appropriate code
if error_count > 0 or failed_count > 0:
print("Test completed with errors")
exit(1)
elif success_count == 0:
print("No webhooks were successfully tested")
exit(1)
else:
print("All configured webhooks tested successfully!")
exit(0)
# Global processor instance (initialized lazily)
processor = None
def get_processor():
"""Get or create the webhook processor instance (lazy initialization)"""
global processor
if processor is None:
config_file = os.environ.get('CONFIG_FILE', 'config.yml')
logger.info(f"Using configuration file: {config_file}")
processor = WebhookProcessor(config_path=config_file)
return processor
@app.route('/health')
def health_check():
"""Health check endpoint"""
return jsonify({'status': 'healthy', 'service': 'slack-to-glue-webhook'})
@app.route('/services/<service_id>', methods=['POST'])
def handle_webhook(service_id: str):
"""Handle incoming webhook requests"""
try:
logger.info(f"Received request for service {service_id}")
logger.info(f"Content-Type: {request.content_type}")
payload = None
# Handle form-encoded data (common for Slack-style webhooks)
if request.content_type and 'application/x-www-form-urlencoded' in request.content_type:
# Data is form-encoded with JSON in a 'payload' field
form_data = request.form.get('payload')
if form_data:
try:
import json
payload = json.loads(form_data)
logger.info(f"Successfully parsed form-encoded JSON for service {service_id}")
except json.JSONDecodeError as e:
logger.warning(f"Failed to parse JSON from form payload: {e}")
return jsonify({
'status': 'error',
'message': 'Invalid JSON in payload field'
}), 400
else:
logger.warning(f"No 'payload' field found in form data")
return jsonify({
'status': 'error',
'message': 'Form data must contain a "payload" field with JSON'
}), 400
else:
# Try to parse as direct JSON
try:
payload = request.get_json(force=True)
except Exception as e:
logger.warning(f"Failed to parse JSON from request for service {service_id}: {e}")
return jsonify({
'status': 'error',
'message': 'Request body must be valid JSON'
}), 400
if not payload:
logger.warning(f"Empty payload received for service {service_id}")
return jsonify({
'status': 'error',
'message': 'Request body cannot be empty'
}), 400
logger.info(f"Successfully parsed webhook for service {service_id}")
# Process the webhook
result = get_processor().process_webhook(service_id, payload)
# Return appropriate status code based on result
if result['status'] == 'success':
return jsonify(result), 200
elif result['status'] == 'warning':
return jsonify(result), 202 # Accepted but with warnings
else:
return jsonify(result), 400
except Exception as e:
logger.error(f"Unexpected error processing webhook: {e}")
return jsonify({
'status': 'error',
'message': 'Internal server error'
}), 500
@app.errorhandler(404)
def not_found(error):
"""Handle 404 errors"""
return jsonify({
'status': 'error',
'message': 'Endpoint not found'
}), 404
if __name__ == '__main__':
# Check if running in test mode
test_mode = os.environ.get('TEST_MODE', 'false').lower() == 'true'
if test_mode:
# Run webhook tests and exit
get_processor().test_webhooks()
else:
# Development server
port = int(os.environ.get('PORT', 8080))
debug = os.environ.get('DEBUG', 'false').lower() == 'true'
logger.info(f"Starting webhook service on port {port}")
app.run(host='0.0.0.0', port=port, debug=debug)