This repository has been archived by the owner on Feb 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Cleanups and fixes #1
Open
harlowja
wants to merge
2
commits into
BonnyCI:master
Choose a base branch
from
harlowja:cleanups-and-fixes
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,16 +12,19 @@ | |
|
||
import argparse | ||
import asyncio | ||
import hashlib | ||
import hmac | ||
import logging | ||
import os | ||
import platform | ||
import signal | ||
import sys | ||
import time | ||
|
||
import aiohttp | ||
import aiohttp.web | ||
import ipaddress | ||
import munch | ||
import requests | ||
import voluptuous | ||
import yaml | ||
|
||
|
@@ -31,17 +34,19 @@ | |
'Content-Type', | ||
'Content-Length', | ||
'X-Github-Event', | ||
'X-Hub-Signature' | ||
'X-Hub-Signature', | ||
'X-GitHub-Delivery', | ||
]) | ||
|
||
GITHUB_META_URL = 'https://api.github.com/meta' | ||
|
||
GITHUB_META_CACHE_TTL = 3600 | ||
GITHUB_META_CACHE_TIMEOUT = 30 | ||
USER_AGENT = "github-webhook-proxy/{} aiohttp/{} {}/{}".format( | ||
version.version_string, | ||
aiohttp.__version__, | ||
platform.python_implementation(), | ||
platform.python_version()) | ||
|
||
PROXY_TIMEOUT = 10 | ||
LOG = logging.getLogger(__name__) | ||
|
||
|
||
|
@@ -50,12 +55,15 @@ class GithubWebhookProxy: | |
def __init__(self, config_file, loop=None): | ||
self.loop = loop or asyncio.get_event_loop() | ||
self.app = aiohttp.web.Application(loop=self.loop) | ||
self.app.router.add_post('/', self.handle_event) | ||
self.app.router.add_post('/github-webhook/', self.handle_event) | ||
self.config_file = config_file | ||
|
||
self.hook_blocks = munch.Munch({ | ||
'last_updated': None, | ||
'networks': [], | ||
}) | ||
self.load_config() | ||
|
||
def validate_signature(self, request): | ||
def validate_signature(self, request, request_body): | ||
key = self.config.get('webhook_key') | ||
signature = request.headers.get('X-Hub-Signature') | ||
|
||
|
@@ -69,36 +77,51 @@ def validate_signature(self, request): | |
digest, value = signature.split('=') | ||
|
||
if digest != 'sha1': | ||
raise web.HTTPForbidden() | ||
raise aiohttp.web.HTTPForbidden() | ||
|
||
mac = hmac.new(key, msg=request.body, digestmod=hashlib.sha1) | ||
key = key.encode("utf8") | ||
mac = hmac.new(key, msg=request_body, digestmod=hashlib.sha1) | ||
|
||
if not hmac.compare_digest(mac.hexdigest(), value): | ||
raise aiohttp.web.HTTPForbidden() | ||
|
||
def validate_ip(self, request): | ||
# request_ip = ipaddress.ip_address(request.client_addr.decode('utf-8')) | ||
# hook_blocks = requests.get(GITHUB_META_URL).json()['hooks'] | ||
pass | ||
|
||
async def handle_event(self, request): | ||
self.validate_signature(request) | ||
self.validate_ip(request) | ||
|
||
headers = {'User-Agent': USER_AGENT} | ||
waiting = [] | ||
|
||
for header in ALLOWED_HEADERS: | ||
def validate_ip(self, request_ip): | ||
if not self.config.get('validate_source_ips'): | ||
return | ||
now = time.monotonic() | ||
if (self.hook_blocks.last_updated is None or | ||
(now - self.hook_blocks.last_updated) > GITHUB_META_CACHE_TTL): | ||
resp = requests.get(GITHUB_META_URL, | ||
timeout=GITHUB_META_CACHE_TIMEOUT) | ||
try: | ||
headers[header] = request.headers[header] | ||
except KeyError: | ||
pass | ||
|
||
event_type = request.headers.get('X-Github-Event') | ||
request_body = await request.read() | ||
resp.raise_for_status() | ||
except Exception: | ||
LOG.exception("Failed calling into '%s'", GITHUB_META_URL) | ||
raise aiohttp.web.HTTPInternalServerError() | ||
hook_blocks = resp.json()['hooks'] | ||
LOG.debug("Valid github hook cidrs: %s", hook_blocks) | ||
hook_blocks = [ipaddress.ip_network(h) for h in hook_blocks] | ||
self.hook_blocks.networks = hook_blocks | ||
self.hook_blocks.last_updated = time.monotonic() | ||
valid = False | ||
for netblock in self.hook_blocks.networks: | ||
if request_ip in netblock: | ||
valid = True | ||
break | ||
if not valid and self.config.get("allowed_ips"): | ||
for tmp_ip in self.config.get("allowed_ips", []): | ||
ip = ipaddress.ip_address(tmp_ip) | ||
if ip == request_ip: | ||
valid = True | ||
break | ||
if not valid: | ||
raise aiohttp.web.HTTPForbidden() | ||
|
||
async def proxy(self, request_body, event_type, headers): | ||
waiting = [] | ||
waiting_urls = [] | ||
async with aiohttp.ClientSession(loop=self.loop) as session: | ||
for client_config in self.config.get('clients', []): | ||
for client_config in list(self.config.get('clients', [])): | ||
url = client_config.get('url') | ||
events = client_config.get('events') | ||
|
||
|
@@ -107,81 +130,138 @@ async def handle_event(self, request): | |
if events is not None and event_type not in events: | ||
continue | ||
|
||
resp = session.post(url, data=request_body, headers=headers) | ||
timeout = client_config.get("timeout", PROXY_TIMEOUT) | ||
resp = session.post(url, data=request_body, | ||
headers=headers, timeout=timeout) | ||
waiting.append(resp) | ||
waiting_urls.append(url) | ||
|
||
responses = await asyncio.gather(*waiting, | ||
loop=self.loop, | ||
return_exceptions=True) | ||
|
||
for resp in responses: | ||
for i, resp in enumerate(responses): | ||
url = waiting_urls[i] | ||
if isinstance(resp, aiohttp.ClientResponse): | ||
resp_text = await resp.text() | ||
|
||
if resp.status == 200: | ||
LOG.debug("Success: %s", resp_text) | ||
LOG.debug("Successfully proxied to '%s', %s, %s", | ||
url, resp.status, resp_text) | ||
else: | ||
LOG.info("Failure: %d, %s", resp.status, resp_text) | ||
LOG.warn("Failed proxy to '%s' %s, %s", url, | ||
resp.status, resp_text) | ||
elif isinstance(resp, aiohttp.ClientConnectionError): | ||
LOG.warn("Client connection error: %s, %s", url, resp) | ||
elif isinstance(resp, aiohttp.ClientOSError): | ||
LOG.warn("Client os error: %s, %s", url, resp) | ||
else: | ||
LOG.warn("Unknown %s error from call to %s: %s", | ||
type(resp), url, resp) | ||
|
||
elif isinstance(resp, errors.ClientOSError): | ||
LOG.warn(resp) | ||
def validate_event_type(self, request): | ||
event_type = request.headers.get('X-Github-Event') | ||
if not event_type: | ||
raise aiohttp.web.HTTPForbidden() | ||
return event_type | ||
|
||
else: | ||
LOG.warn("Unknown return: %s" % resp) | ||
async def handle_event(self, request): | ||
LOG.debug("Processing call from '%s'", request.remote) | ||
request_ip = ipaddress.ip_address(request.remote) | ||
self.validate_ip(request_ip) | ||
|
||
request_body = await request.read() | ||
self.validate_signature(request, request_body) | ||
event_type = self.validate_event_type(request) | ||
|
||
headers = { | ||
'User-Agent': USER_AGENT, | ||
} | ||
for header in ALLOWED_HEADERS: | ||
try: | ||
headers[header] = request.headers[header] | ||
except KeyError: | ||
pass | ||
|
||
LOG.debug("Received validated '%s' event from '%s'", event_type, | ||
request_ip) | ||
LOG.debug(request_body) | ||
asyncio.ensure_future(self.proxy(request_body, event_type, | ||
headers), loop=self.loop) | ||
|
||
if event_type == 'ping': | ||
return aiohttp.web.Response(text='pong') | ||
else: | ||
return aiohttp.web.Response(text='Hello world') | ||
return aiohttp.web.Response(text='') | ||
|
||
def load_config(self): | ||
with open(self.config_file, 'r') as f: | ||
config = yaml.safe_load(f) or {} | ||
|
||
validate(config) | ||
self.config = config | ||
|
||
|
||
def initialize_application(argv=None): | ||
parser = argparse.ArgumentParser() | ||
|
||
parser.add_argument('-c', '--config', | ||
dest='config', | ||
default=os.environ.get('GWP_CONFIG_FILE'), | ||
required=True, | ||
help='Configuration file') | ||
|
||
opts = parser.parse_args(sys.argv[1:] if argv is None else argv) | ||
|
||
def initialize_application(opts): | ||
if not os.path.exists(opts.config): | ||
LOG.error("Config file does not exist {}".format(opts.config)) | ||
return | ||
|
||
return GithubWebhookProxy(opts.config) | ||
|
||
|
||
def validate(config): | ||
client = voluptuous.Schema({ | ||
voluptuous.Required('url'): str, | ||
'events': list([str]) | ||
voluptuous.Optional('events'): list([str]), | ||
voluptuous.Optional("timeout"): int, | ||
}, extra=False) | ||
|
||
s = voluptuous.Schema({ | ||
'webhook_key': str, | ||
voluptuous.Optional("allowed_ips"): list([str]), | ||
voluptuous.Optional('validate_source_ips'): bool, | ||
'clients': list([client]), | ||
}, extra=False) | ||
|
||
s(config) | ||
|
||
|
||
if __name__ == '__main__': | ||
logging.basicConfig(level=logging.DEBUG) | ||
app = initialize_application() | ||
def main(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('-c', '--config', | ||
dest='config', | ||
default=os.environ.get('GWP_CONFIG_FILE'), | ||
required=True, | ||
help='Configuration file') | ||
parser.add_argument("-p", "--port", dest='port', | ||
default=8080, type=int, | ||
help='Port to run proxy on (default=%(default)s)') | ||
parser.add_argument("-e", "--expose", | ||
default=False, action="store_true", | ||
help="Expose port on '0.0.0.0' vs '127.0.0.1'") | ||
parser.add_argument("-v", "--verbose", default=0, | ||
action='count', help="Increase verbosity") | ||
|
||
opts = parser.parse_args() | ||
|
||
if opts.verbose == 0: | ||
logging.basicConfig(level=logging.WARN) | ||
elif opts.verbose == 1: | ||
logging.basicConfig(level=logging.INFO) | ||
else: | ||
logging.basicConfig(level=logging.DEBUG) | ||
|
||
app = initialize_application(opts) | ||
|
||
def sig_handler(): | ||
LOG.info("Reloading configuration from %s", app.config_file) | ||
app.load_config() | ||
|
||
if app: | ||
app.loop.add_signal_handler(signal.SIGHUP, sig_handler) | ||
aiohttp.web.run_app(app.app, host='127.0.0.1', port=8080) | ||
if opts.expose: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would prefer to specify a bind IP address, more generally useful. (although maybe less so in a docker world) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense. |
||
host = "0.0.0.0" | ||
else: | ||
host = "127.0.0.1" | ||
aiohttp.web.run_app(app.app, host=host, port=opts.port) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ pbr>=1.6 # Apache-2.0 | |
aiohttp | ||
PyYAML | ||
voluptuous | ||
munch | ||
requests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this would be a sync request in an async block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose that could be changed, though I'm hoping its once per hour so does it matter that much?