Skip to content

Commit

Permalink
beautiful configuration loader applied
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveDev42 committed Oct 22, 2016
1 parent e7d4145 commit dbc248a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 44 deletions.
8 changes: 8 additions & 0 deletions alertserver/config.py
Original file line number Diff line number Diff line change
@@ -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'])
8 changes: 8 additions & 0 deletions alertserver/config.sample.ini
Original file line number Diff line number Diff line change
@@ -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
83 changes: 39 additions & 44 deletions alertserver/server.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
click==6.6
easydict==1.6
Flask==0.11.1
itsdangerous==0.24
Jinja2==2.8
Expand Down

0 comments on commit dbc248a

Please sign in to comment.