[e455b7]: / Allura / allura / webhooks.py  Maximize  Restore  History

Download this file

505 lines (447 with data), 18.2 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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import logging
import json
import hmac
import hashlib
import time
import socket
import ssl
import requests
from bson import ObjectId
from tg import expose, validate, redirect, flash, config
from tg.decorators import with_trailing_slash, without_trailing_slash
from tg import tmpl_context as c
from tg import response, request
from formencode import validators as fev, schema, Invalid
from ming.odm import session
from webob import exc
from pymongo.errors import DuplicateKeyError
from paste.deploy.converters import asint, aslist
from allura.app import AdminControllerMixin
from allura.controllers import BaseController
from allura.lib import helpers as h
from allura.lib import validators as v
from allura.lib.decorators import require_post, task
from allura.lib.utils import DateJSONEncoder
from allura import model as M
log = logging.getLogger(__name__)
class WebhookValidator(fev.FancyValidator):
def __init__(self, sender, app, **kw):
self.app = app
self.sender = sender
super().__init__(**kw)
def _convert_to_python(self, value, state):
wh = None
if isinstance(value, M.Webhook):
wh = value
elif isinstance(value, ObjectId):
wh = M.Webhook.query.get(_id=value)
else:
try:
wh = M.Webhook.query.get(_id=ObjectId(value))
except Exception:
pass
if wh and wh.type == self.sender.type and wh.app_config_id == self.app.config._id:
return wh
raise Invalid('Invalid webhook', value, state)
class WebhookCreateForm(schema.Schema):
url = fev.URL(not_empty=True)
secret = v.UnicodeString()
class WebhookEditForm(WebhookCreateForm):
def __init__(self, sender, app):
super().__init__()
self.add_field('webhook', WebhookValidator(
sender=sender, app=app, not_empty=True))
class WebhookControllerMeta(type):
def __call__(cls, sender, app, *args, **kw):
"""Decorate post handlers with a validator that references
the appropriate webhook sender for this controller.
"""
if hasattr(cls, 'create'):
cls.create = validate(
cls.create_form(),
error_handler=getattr(cls.index, '__func__', cls.index),
)(cls.create)
if hasattr(cls, 'edit'):
cls.edit = validate(
cls.edit_form(sender, app),
error_handler=getattr(cls._default, '__func__', cls._default),
)(cls.edit)
return type.__call__(cls, sender, app, *args, **kw)
class WebhookController(BaseController, AdminControllerMixin, metaclass=WebhookControllerMeta):
create_form = WebhookCreateForm
edit_form = WebhookEditForm
def __init__(self, sender, app):
super().__init__()
self.sender = sender()
self.app = app
def gen_secret(self):
return h.cryptographic_nonce(20)
def update_webhook(self, wh, url, secret=None):
if not secret:
secret = self.gen_secret()
wh.hook_url = url
wh.secret = secret
try:
session(wh).flush(wh)
except DuplicateKeyError:
session(wh).expunge(wh)
msg = '_the_form: "{}" webhook already exists for {} {}'.format(
wh.type, self.app.config.options.mount_label, url)
raise Invalid(msg, None, None)
@with_trailing_slash
@expose('jinja:allura:templates/webhooks/create_form.html')
def index(self, **kw):
if not request.validation.values and kw:
# Executes if update_webhook raises an error
request.validation.values = {'url': kw.get('url'),
'secret': kw.get('secret')}
return {'sender': self.sender,
'action': 'create',
'form': self.create_form()}
@expose('jinja:allura:templates/webhooks/create_form.html') # needed when we "return self.index(...)"
@require_post()
# @validate set dynamically in WebhookControllerMeta
def create(self, url, secret):
if self.sender.enforce_limit(self.app):
webhook = M.Webhook(
type=self.sender.type,
app_config_id=self.app.config._id)
try:
self.update_webhook(webhook, url, secret)
except Invalid as e:
# trigger error_handler directly
request.validation.errors['_the_form'] = e
return self.index(url=url, secret=secret)
M.AuditLog.log('add webhook %s %s %s',
webhook.type, webhook.hook_url,
webhook.app_config.url())
flash('Created successfully', 'ok')
else:
flash('You have exceeded the maximum number of webhooks '
'you are allowed to create for this project/app', 'error')
redirect(self.app.admin_url + 'webhooks')
@expose('jinja:allura:templates/webhooks/create_form.html') # needed when we "return self._default(...)"
@require_post()
# @validate set dynamically in WebhookControllerMeta
def edit(self, webhook, url, secret):
old_url = webhook.hook_url
old_secret = webhook.secret
try:
self.update_webhook(webhook, url, secret)
except Invalid as e:
# trigger error_handler directly
request.validation.errors['_the_form'] = e
return self._default(webhook=webhook, url=url, secret=secret)
M.AuditLog.log('edit webhook %s\n%s => %s\n%s',
webhook.type, old_url, url,
'secret changed' if old_secret != secret else '')
flash('Edited successfully', 'ok')
redirect(self.app.admin_url + 'webhooks')
@expose('json:')
@require_post()
def delete(self, webhook, **kw):
form = self.edit_form(self.sender, self.app)
try:
wh = form.fields['webhook'].to_python(webhook)
except Invalid:
raise exc.HTTPNotFound()
wh.delete()
M.AuditLog.log('delete webhook %s %s %s',
wh.type, wh.hook_url, wh.app_config.url())
return {'status': 'ok'}
@without_trailing_slash
@expose('jinja:allura:templates/webhooks/create_form.html')
def _default(self, webhook, **kw):
form = self.edit_form(self.sender, self.app)
try:
wh = form.fields['webhook'].to_python(webhook)
except Invalid:
raise exc.HTTPNotFound()
request.validation.values = {'url': kw.get('url') or wh.hook_url,
'secret': kw.get('secret') or wh.secret,
'webhook': str(wh._id)}
return {'sender': self.sender,
'action': 'edit',
'form': form}
class WebhookRestController(BaseController):
def __init__(self, sender, app):
super().__init__()
self.sender = sender()
self.app = app
self.create_form = WebhookController.create_form
self.edit_form = WebhookController.edit_form
def _error(self, e):
error = getattr(e, 'error_dict', None)
if error:
_error = {}
for k, val in error.items():
_error[k] = str(val)
return _error
error = getattr(e, 'msg', None)
if not error:
error = getattr(e, 'message', '')
return error
def update_webhook(self, wh, url, secret=None):
controller = WebhookController(self.sender.__class__, self.app)
controller.update_webhook(wh, url, secret)
@expose('json:')
@require_post()
def index(self, **kw):
response.content_type = 'application/json'
try:
params = {'secret': kw.pop('secret', ''),
'url': kw.pop('url', None)}
valid = self.create_form().to_python(params)
except Exception as e:
response.status_int = 400
return {'result': 'error', 'error': self._error(e)}
if self.sender.enforce_limit(self.app):
webhook = M.Webhook(
type=self.sender.type,
app_config_id=self.app.config._id)
try:
self.update_webhook(webhook, valid['url'], valid['secret'])
except Invalid as e:
response.status_int = 400
return {'result': 'error', 'error': self._error(e)}
M.AuditLog.log('add webhook %s %s %s',
webhook.type, webhook.hook_url,
webhook.app_config.url())
response.status_int = 201
# refetch updated values (e.g. mod_date)
session(webhook).expunge(webhook)
webhook = M.Webhook.query.get(_id=webhook._id)
return webhook.__json__()
else:
limits = {
'max': M.Webhook.max_hooks(
self.sender.type,
self.app.config.tool_name),
'used': M.Webhook.query.find({
'type': self.sender.type,
'app_config_id': self.app.config._id,
}).count(),
}
resp = {
'result': 'error',
'error': 'You have exceeded the maximum number of webhooks '
'you are allowed to create for this project/app',
'limits': limits,
}
response.status_int = 400
return resp
@expose('json:')
def _default(self, webhook, **kw):
form = self.edit_form(self.sender, self.app)
try:
wh = form.fields['webhook'].to_python(webhook)
except Invalid:
raise exc.HTTPNotFound()
if request.method == 'POST':
return self._edit(wh, form, **kw)
elif request.method == 'DELETE':
return self._delete(wh)
else:
return wh.__json__()
def _edit(self, webhook, form, **kw):
old_secret = webhook.secret
old_url = webhook.hook_url
try:
params = {'secret': kw.pop('secret', old_secret),
'url': kw.pop('url', old_url),
'webhook': str(webhook._id)}
valid = form.to_python(params)
except Exception as e:
response.status_int = 400
return {'result': 'error', 'error': self._error(e)}
try:
self.update_webhook(webhook, valid['url'], valid['secret'])
except Invalid as e:
response.status_int = 400
return {'result': 'error', 'error': self._error(e)}
M.AuditLog.log(
'edit webhook %s\n%s => %s\n%s',
webhook.type, old_url, valid['url'],
'secret changed' if old_secret != valid['secret'] else '')
# refetch updated values (e.g. mod_date)
session(webhook).expunge(webhook)
webhook = M.Webhook.query.get(_id=webhook._id)
return webhook.__json__()
def _delete(self, webhook):
webhook.delete()
M.AuditLog.log(
'delete webhook %s %s %s',
webhook.type,
webhook.hook_url,
webhook.app_config.url())
return {'result': 'ok'}
class SendWebhookHelper:
def __init__(self, webhook, payload):
self.webhook = webhook
self.payload = payload
@property
def timeout(self):
return asint(config.get('webhook.timeout', 30))
@property
def retries(self):
t = aslist(config.get('webhook.retry', [60, 120, 240]))
return list(map(int, t))
def sign(self, json_payload):
signature = hmac.new(
self.webhook.secret.encode('utf-8'),
json_payload.encode('utf-8'),
hashlib.sha1)
return 'sha1=' + signature.hexdigest()
def log_msg(self, msg, response=None):
message = '{}: {} {} {}'.format(
msg,
self.webhook.type,
self.webhook.hook_url,
self.webhook.app_config.url())
if response is not None:
message = '{} {} {} {}'.format(
message,
response.status_code,
response.text,
response.headers)
return message
def send(self):
json_payload = json.dumps(self.payload, cls=DateJSONEncoder)
signature = self.sign(json_payload)
headers = {'content-type': 'application/json',
'User-Agent': 'Allura Webhook (https://allura.apache.org/)',
'X-Allura-Signature': signature}
ok = self._send(self.webhook.hook_url, json_payload, headers)
if not ok:
log.info('Retrying webhook in: %s', self.retries)
for t in self.retries:
log.info('Retrying webhook in %s seconds', t)
time.sleep(t)
ok = self._send(self.webhook.hook_url, json_payload, headers)
if ok:
return
def _send(self, url, data, headers):
try:
r = requests.post(
url,
data=data,
headers=headers,
timeout=self.timeout)
except (requests.exceptions.RequestException,
socket.timeout,
ssl.SSLError):
log.exception(self.log_msg('Webhook send error'))
return False
if r.status_code >= 200 and r.status_code < 300:
log.info(self.log_msg('Webhook successfully sent'))
return True
else:
log.error(self.log_msg('Webhook send error', response=r))
return False
@task()
def send_webhook(webhook_id, payload):
webhook = M.Webhook.query.get(_id=webhook_id)
SendWebhookHelper(webhook, payload).send()
class WebhookSender:
"""Base class for webhook senders.
Subclasses are required to implement :meth:`get_payload()` and set
:attr:`type` and :attr:`triggered_by`.
"""
type = None
triggered_by = []
controller = WebhookController
api_controller = WebhookRestController
def get_payload(self, **kw):
"""Return a dict with webhook payload"""
raise NotImplementedError('get_payload')
def send(self, params_or_list):
"""Post a task that will send webhook payload
:param params_or_list: dict with keyword parameters to be passed to
:meth:`get_payload` or a list of such dicts. If it's a list for each
element appropriate payload will be submitted, but limit will be
enforced only once for each webhook.
"""
if not isinstance(params_or_list, list):
params_or_list = [params_or_list]
webhooks = M.Webhook.query.find(dict(
app_config_id=c.app.config._id,
type=self.type,
)).all()
if webhooks:
payloads = [self.get_payload(**params)
for params in params_or_list]
for webhook in webhooks:
if webhook.enforce_limit():
webhook.update_limit()
for payload in payloads:
send_webhook.post(webhook._id, payload)
else:
log.warning('Webhook fires too often: %s. Skipping', webhook)
def enforce_limit(self, app):
'''
Checks if limit of webhooks created for given project/app is reached.
Returns False if limit is reached, True otherwise.
'''
count = M.Webhook.query.find(dict(
app_config_id=app.config._id,
type=self.type,
)).count()
limit = M.Webhook.max_hooks(self.type, app.config.tool_name)
return count < limit
class RepoPushWebhookSender(WebhookSender):
type = 'repo-push'
triggered_by = ['git', 'hg', 'svn']
def _before(self, repo, commit_ids):
if len(commit_ids) > 0:
ci = commit_ids[-1]
parents = repo.commit(ci).parent_ids
if len(parents) > 0:
# Merge commit will have multiple parents. As far as I can tell
# the last one will be the branch head before merge
return self._convert_id(parents[-1])
return ''
def _after(self, commit_ids):
if len(commit_ids) > 0:
return self._convert_id(commit_ids[0])
return ''
def _convert_id(self, _id):
if ':' in _id:
_id = 'r' + _id.rsplit(':', 1)[1]
return _id
def get_payload(self, commit_ids, **kw):
app = kw.get('app') or c.app
commits = [app.repo.commit(ci).webhook_info for ci in commit_ids]
for ci in commits:
ci['id'] = self._convert_id(ci['id'])
before = self._before(app.repo, commit_ids)
after = self._after(commit_ids)
payload = {
'size': len(commits),
'commits': commits,
'before': before,
'after': after,
'repository': {
'name': app.config.options.mount_label,
'full_name': app.url,
'url': h.absurl(app.url),
},
}
if kw.get('ref'):
payload['ref'] = kw['ref']
return payload