Skip to content

Get config from env vars if no config.json #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,21 @@ with the following command:

git clone http://github.com/carlos-jenkins/python-github-webhooks.git
docker build -t carlos-jenkins/python-github-webhooks python-github-webhooks
docker run -d --name webhooks -p 5000:5000 carlos-jenkins/python-github-webhooks
docker run -d --name webhooks \
-e WEBHOOKS_GITHUB_IPS_ONLY=False \
-e WEBHOOKS_HOOKS_PATH=/src/hooks \
-e WEBHOOKS_ENFORCE_SECRET='' \
-e WEBHOOKS_RETURN_SCRIPTS_INFO=True \
-p 5000:5000 \
carlos-jenkins/python-github-webhooks


You can also mount volume to setup the ``hooks/`` directory, and the file
``config.json``:
You can also mount a volume to setup the ``hooks/`` directory:

::

docker run -d --name webhooks \
-v /path/to/my/hooks:/src/hooks \
-v /path/to/my/config.json:/src/config.json \
-p 5000:5000 python-github-webhooks


Expand Down
34 changes: 25 additions & 9 deletions webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from json import loads, dumps
from subprocess import Popen, PIPE
from tempfile import mkstemp
from os import access, X_OK, remove, fdopen
from os import access, X_OK, remove, fdopen, environ
from os.path import isfile, abspath, normpath, dirname, join, basename

import requests
Expand All @@ -34,6 +34,15 @@

application = Flask(__name__)

def env_var(key, default=None):
val = environ.get(key, default)
if val == 'True':
val = True
elif val == 'False':
val = False
elif val == 'None':
val = None
return val

@application.route('/', methods=['GET', 'POST'])
def index():
Expand All @@ -47,14 +56,23 @@ def index():
if request.method != 'POST':
abort(501)

# Load config
with open(join(path, 'config.json'), 'r') as cfg:
config = loads(cfg.read())

hooks = config.get('hooks_path', join(path, 'hooks'))
# Load config from file
try:
with open(join(path, 'config.json'), 'r') as cfg:
config = loads(cfg.read())
hooks = config.get('hooks_path', join(path, 'hooks'))
github_ips_only = config.get('github_ips_only', True)
secret = config.get('enforce_secret', '')
info = config.get('return_scripts_info', False)
# ... or from ENV vars
except Exception:
hooks = env_var('WEBHOOKS_HOOKS_PATH', join(path, 'hooks'))
github_ips_only = env_var('WEBHOOKS_GITHUB_IPS_ONLY', True)
secret = env_var('WEBHOOKS_ENFORCE_SECRET', '')
info = env_var('WEBHOOKS_RETURN_SCRIPTS_INFO', False)

# Allow Github IPs only
if config.get('github_ips_only', True):
if github_ips_only:
src_ip = ip_address(
u'{}'.format(request.remote_addr) # Fix stupid ipaddress issue
)
Expand All @@ -67,7 +85,6 @@ def index():
abort(403)

# Enforce secret
secret = config.get('enforce_secret', '')
if secret:
# Only SHA1 is supported
header_signature = request.headers.get('X-Hub-Signature')
Expand Down Expand Up @@ -190,7 +207,6 @@ def index():
# Remove temporal file
remove(tmpfile)

info = config.get('return_scripts_info', False)
if not info:
return dumps({'status': 'done'})

Expand Down