diff --git a/alertserver/config.py b/alertserver/config.py new file mode 100644 index 0000000..94f90ee --- /dev/null +++ b/alertserver/config.py @@ -0,0 +1,8 @@ +import configparser +from easydict import EasyDict + +_config = configparser.ConfigParser() +_config.read('config.sample.ini') + +Bitbucket = EasyDict(_config['Bitbucket']) +Trello = EasyDict(_config['Trello']) \ No newline at end of file diff --git a/alertserver/config.sample.ini b/alertserver/config.sample.ini new file mode 100644 index 0000000..e103eae --- /dev/null +++ b/alertserver/config.sample.ini @@ -0,0 +1,8 @@ +[Bitbucket] +TRUSTED_REMOTE_ADDRS = 104.192.143.192/28, 104.192.143.208/28 +WEBHOOK_PATHS = /webhook, /webhook2 +WEBHOOK_HOST = 0.0.0.0 +WEBHOOK_PORT = 5000 + +[Trello] +c = 30 \ No newline at end of file diff --git a/alertserver/server.py b/alertserver/server.py index aa2427f..8df5014 100644 --- a/alertserver/server.py +++ b/alertserver/server.py @@ -1,62 +1,57 @@ -from flask import Flask, abort, request +import re +from functools import reduce +from ipaddress import ip_network as parse_addr + +from flask import Flask +from flask import abort from flask import request +from alertserver.config import Bitbucket as config_bitbucket + app = Flask(__name__) -# 104.192.143.192/28 -# 104.192.143.208/28 -trusted_remote_addrs = list(map( - lambda last: '104.192.143.' + str(last), - list(range(192, 192 + 16)) + list(range(208, 208 + 16)) +trusted_remote_addrs = list(reduce( + lambda l1, l2: l1 + l2, + map( + lambda addr_str: [str(network) for network in parse_addr(addr_str)], + re.sub(r'\s', '', config_bitbucket.trusted_remote_addrs).split(',') + ) )) -BITBUCKET_WEBHOOK_PATH = '/webhook' -webhook_paths = [ - BITBUCKET_WEBHOOK_PATH, -] +def is_trusted_remote_addrs(addr): + return addr in trusted_remote_addrs -@app.before_request -def limit_remote_addr(): - """Block all request if request's path is heading to limited path with non-trusted remote address.""" - if (is_webhook_path(request.path) and - not is_trusted_remote_addrs(request.remote_addr)): +def webhook_routine(): + if not is_trusted_remote_addrs(request.remote_addr): abort(403) + data = request.get_json() -def is_webhook_path(path): - return path in webhook_paths - - -def is_trusted_remote_addrs(remote_addr): - return remote_addr in trusted_remote_addrs - - -@app.route(BITBUCKET_WEBHOOK_PATH, methods=['GET', 'POST']) -def tracking(): - if request.method == 'POST': - data = request.get_json() + commit_author = data['actor']['username'] - commit_author = data['actor']['username'] + is_branch_created = data['push']['changes'][0]['created'] + is_branch_closed = data['push']['changes'][0]['closed'] + if is_branch_created: + # TODO: Alert branch created. + print('Webhook received! %s created branch' % commit_author) + pass + elif is_branch_closed: + # TODO: Alert branch closed. + print('Webhook received! %s deleted branch' % commit_author) + else: + # TODO: Alert commits. + commit_hash = data['push']['changes'][0]['new']['target']['hash'][:7] + commit_url = data['push']['changes'][0]['new']['target']['links']['html']['href'] + print('Webhook received! %s committed %s' % (commit_author, commit_hash)) + return 'OK' - is_branch_created = data['push']['changes'][0]['created'] - is_branch_closed = data['push']['changes'][0]['closed'] - if is_branch_created: - # TODO: Alert branch created. - print('Webhook received! %s created branch' % commit_author) - pass - elif is_branch_closed: - # TODO: Alert branch closed. - print('Webhook received! %s deleted branch' % commit_author) - else: - # TODO: Alert commits. - commit_hash = data['push']['changes'][0]['new']['target']['hash'][:7] - commit_url = data['push']['changes'][0]['new']['target']['links']['html']['href'] - print('Webhook received! %s committed %s' % (commit_author, commit_hash)) - return 'OK' +webhook_paths = re.sub(r'\s', '', config_bitbucket.webhook_paths).split(',') +for webhook_path in webhook_paths: + app.route(webhook_path, methods=['POST'])(webhook_routine) if __name__ == '__main__': - app.run(host='0.0.0.0', port=5000, debug=True) + app.run(host=config_bitbucket.webhook_host, port=int(config_bitbucket.webhook_port), debug=True) diff --git a/requirements.txt b/requirements.txt index 71f70e1..2e06385 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ click==6.6 +easydict==1.6 Flask==0.11.1 itsdangerous==0.24 Jinja2==2.8