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
|
from __future__ import absolute_import
from .events import WebhookAction
from .parsers import get_service_handler
def WebhookRequestHandlerFactory(config, event_store, server_status, is_https=False):
"""Factory method for webhook request handler class"""
try:
from SimpleHTTPServer import SimpleHTTPRequestHandler
except ImportError as e:
from http.server import SimpleHTTPRequestHandler
class WebhookRequestHandler(SimpleHTTPRequestHandler, object):
"""Extends the BaseHTTPRequestHandler class and handles the incoming
HTTP requests."""
def __init__(self, *args, **kwargs):
self._config = config
self._event_store = event_store
self._server_status = server_status
self._is_https = is_https
super(WebhookRequestHandler, self).__init__(*args, **kwargs)
def end_headers(self):
self.send_header('Access-Control-Allow-Origin', '*')
SimpleHTTPRequestHandler.end_headers(self)
def do_HEAD(self):
# Web UI needs to be enabled
if not self.validate_web_ui_enabled():
return
# Web UI might require HTTPS
if not self.validate_web_ui_https():
return
# Client needs to be whitelisted
if not self.validate_web_ui_whitelist():
return
# Client needs to authenticate
if not self.validate_web_ui_basic_auth():
return
return SimpleHTTPRequestHandler.do_HEAD(self)
def do_GET(self):
# Web UI needs to be enabled
if not self.validate_web_ui_enabled():
return
# Web UI might require HTTPS
if not self.validate_web_ui_https():
return
# Client needs to be whitelisted
if not self.validate_web_ui_whitelist():
return
# Client needs to authenticate
if not self.validate_web_ui_basic_auth():
return
# Handle status API call
if self.path == "/api/status":
self.handle_status_api()
return
# Serve static file
return SimpleHTTPRequestHandler.do_GET(self)
def handle_status_api(self):
import json
from os import urandom
from base64 import b64encode
data = {
'events': self._event_store.dict_repr(),
'auth-key': self._server_status['auth-key']
}
data.update(self.get_server_status())
self.send_response(200, 'OK')
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(data).encode('utf-8'))
def do_POST(self):
"""Invoked on incoming POST requests"""
from threading import Timer
import logging
import json
import threading
logger = logging.getLogger()
content_length = int(self.headers.get('content-length'))
request_body = self.rfile.read(content_length).decode('utf-8')
# Extract request headers and make all keys to lowercase (makes them easier to compare)
request_headers = dict(self.headers)
request_headers = dict((k.lower(), v) for k, v in request_headers.items())
action = WebhookAction(self.client_address, request_headers, request_body)
self._event_store.register_action(action)
action.set_waiting(True)
action.log_info('Incoming request from %s:%s' % (self.client_address[0], self.client_address[1]))
# Test case debug data
test_case = {
'headers': dict(self.headers),
'payload': json.loads(request_body),
'config': {},
'expected': {'status': 200, 'data': [{'deploy': 0}]}
}
try:
# Will raise a ValueError exception if it fails
ServiceRequestHandler = get_service_handler(request_headers, request_body, action)
# Unable to identify the source of the request
if not ServiceRequestHandler:
self.send_error(400, 'Unrecognized service')
test_case['expected']['status'] = 400
action.log_error("Unable to find appropriate handler for request. The source service is not supported")
action.set_waiting(False)
action.set_success(False)
return
service_handler = ServiceRequestHandler(self._config)
action.log_info("Handling the request with %s" % ServiceRequestHandler.__name__)
# Could be GitHubParser, GitLabParser or other
projects = service_handler.get_matching_projects(request_headers, request_body, action)
action.log_info("%s candidates matches the request" % len(projects))
# request_filter = WebhookRequestFilter()
if len(projects) == 0:
self.send_error(400, 'Bad request')
test_case['expected']['status'] = 400
action.log_error("No matching projects")
action.set_waiting(False)
action.set_success(False)
return
# Apply filters
matching_projects = []
for project in projects:
if project.apply_filters(request_headers, request_body, action):
matching_projects.append(project)
# Only keep projects that matches
projects = matching_projects
action.log_info("%s candidates matches after applying filters" % len(projects))
if not service_handler.validate_request(request_headers, projects, action):
self.send_error(400, 'Bad request')
test_case['expected']['status'] = 400
action.log_warning("Request is not valid")
action.set_waiting(False)
action.set_success(False)
return
test_case['expected']['status'] = 200
self.send_response(200, 'OK')
self.send_header('Content-type', 'text/plain')
self.end_headers()
if len(projects) == 0:
action.set_waiting(False)
action.set_success(False)
return
action.log_info("Proceeding with %s candidates" % len(projects))
action.set_waiting(False)
action.set_success(True)
for project in projects:
# Schedule the execution of the webhook (git pull and trigger deploy etc)
thread = threading.Thread(target=project.execute_webhook, args=[self._event_store])
thread.start()
# Add additional test case data
test_case['config'] = {
'url': 'url' in project and project['url'],
'branch': 'branch' in project and project['branch'],
'remote': 'remote' in project and project['remote'],
'deploy': 'echo test!'
}
except ValueError as e:
self.send_error(400, 'Unprocessable request')
action.log_warning('Unable to process incoming request from %s:%s' % (self.client_address[0], self.client_address[1]))
test_case['expected']['status'] = 400
action.set_waiting(False)
action.set_success(False)
return
except Exception as e:
if 'detailed-response' in self._config and self._config['detailed-response']:
self.send_error(500, 'Unable to process request')
test_case['expected']['status'] = 500
action.log_warning("Unable to process request")
action.set_waiting(False)
action.set_success(False)
raise e
finally:
# Save the request as a test case
if 'log-test-case' in self._config and self._config['log-test-case']:
self.save_test_case(test_case)
def log_message(self, format, *args):
"""Overloads the default message logging method to allow messages to
go through our custom logger instead."""
import logging
logger = logging.getLogger()
logger.info("%s - %s" % (self.client_address[0], format%args))
def save_test_case(self, test_case):
"""Log request information in a way it can be used as a test case."""
import time
import json
import os
# Mask some header values
masked_headers = ['x-github-delivery', 'x-hub-signature']
for key in test_case['headers']:
if key in masked_headers:
test_case['headers'][key] = 'xxx'
target = '%s-%s.tc.json' % (self.client_address[0], time.strftime("%Y%m%d%H%M%S"))
if 'log-test-case-dir' in self._config and self._config['log-test-case-dir']:
target = os.path.join(self._config['log-test-case-dir'], target)
file = open(target, 'w')
file.write(json.dumps(test_case, sort_keys=True, indent=4))
file.close()
def get_server_status(self):
"""Generate a copy of the server status object that contains the public IP or hostname."""
server_status = {}
for item in self._server_status.items():
key, value = item
public_host = self.headers.get('host').split(':')[0]
if key == 'http-uri':
server_status[key] = value.replace(self._config['http-host'], public_host)
if key == 'https-uri':
server_status[key] = value.replace(self._config['https-host'], public_host)
if key == 'wss-uri':
server_status[key] = value.replace(self._config['wss-host'], public_host)
return server_status
def validate_web_ui_enabled(self):
"""Verify that the Web UI is enabled"""
if self._config['web-ui-enabled']:
return True
self.send_error(403, "Web UI is not enabled")
return False
def validate_web_ui_https(self):
"""Verify that the request is made over HTTPS"""
if self._is_https and self._config['web-ui-require-https']:
return True
# Attempt to redirect the request to HTTPS
server_status = self.get_server_status()
if 'https-uri' in server_status:
self.send_response(307)
self.send_header('Location', '%s%s' % (server_status['https-uri'], self.path))
self.end_headers()
return False
self.send_error(403, "Web UI is only accessible through HTTPS")
return False
def validate_web_ui_whitelist(self):
"""Verify that the client address is whitelisted"""
# Allow all if whitelist is empty
if len(self._config['web-ui-whitelist']) == 0:
return True
# Verify that client IP is whitelisted
if self.client_address[0] in self._config['web-ui-whitelist']:
return True
self.send_error(403, "%s is not allowed access" % self.client_address[0])
return False
def validate_web_ui_basic_auth(self):
"""Authenticate the user"""
import base64
if not self._config['web-ui-auth-enabled']:
return True
# Verify that a username and password is specified in the config
if self._config['web-ui-username'] is None or self._config['web-ui-password'] is None:
self.send_error(403, "Authentication credentials missing in config")
return False
# Verify that the provided username and password matches the ones in the config
key = base64.b64encode("%s:%s" % (self._config['web-ui-username'], self._config['web-ui-password']))
if self.headers.getheader('Authorization') == 'Basic ' + key:
return True
# Let the client know that authentication is required
self.send_response(401)
self.send_header('WWW-Authenticate', 'Basic realm=\"GAD\"')
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write('Not authenticated')
return False
return WebhookRequestHandler
|