-
Notifications
You must be signed in to change notification settings - Fork 4
/
hooknook.py
427 lines (357 loc) · 12.6 KB
/
hooknook.py
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
import flask
from flask import request
import threading
import queue
import click
import os
import subprocess
import traceback
import yaml
import datetime
import netaddr
import requests
import string
import random
import urllib.parse
import logging
app = flask.Flask(__name__)
app.config.update(
DATA_DIR=os.path.expanduser(os.path.join('~', '.hooknook')),
CONFIG_FILENAME='.hook.yaml',
CONFIG_DEFAULT={
'deploy': 'make deploy',
},
USERS=(),
PRIVATE_URL_FORMAT='git@github.com:{user}/{repo}.git',
PUBLIC_URL_FORMAT='https://github.com/{user}/{repo}.git',
GITHUB_ID=None,
GITHUB_SECRET=None,
GITHUB_HOOK_SUBNETS=None,
PROXIED=False,
)
app.config.from_pyfile('hooknook.cfg', silent=True)
app.config.from_envvar('HOOKNOOK_CFG', silent=True)
FILENAME_FORMAT = '{user}#{repo}'
TIMESTAMP_FORMAT = '%Y-%m-%d-%H-%M-%S-%f'
def load_config(repo_dir):
"""Load the repository's configuration as a dictionary. Defaults are
used for missing keys.
"""
# Provide defaults.
config = dict(app.config['CONFIG_DEFAULT'])
# Load the configuration file, if any.
config_fn = os.path.join(repo_dir, app.config['CONFIG_FILENAME'])
if os.path.exists(config_fn):
with open(config_fn) as f:
overlay = yaml.load(f)
config.update(overlay)
return config
def timestamp():
"""Get a string indicating the current time.
"""
now = datetime.datetime.now()
return now.strftime(TIMESTAMP_FORMAT)
def shell(command, logfile, cwd=None, shell=False):
"""Execute a command (via a shell or directly) and log the stdout
and stderr streams to a file-like object.
"""
if shell:
logline = command
else:
logline = ' '.join(command)
logfile.write('$ {}\n'.format(logline))
logfile.flush()
subprocess.check_call(
command,
cwd=cwd,
stdout=logfile,
stderr=subprocess.STDOUT,
shell=shell,
)
logfile.flush()
def random_string(length=20, chars=(string.ascii_letters + string.digits)):
return ''.join(random.choice(chars) for i in range(length))
def github_get(path, token=None, base='https://api.github.com'):
"""Make a request to the GitHub API."""
token = token or flask.session['github_token']
url = '{}/{}'.format(base, path)
return requests.get(url, params={
'access_token': token,
})
def update_repo(repo, url, log):
"""Clone or pull the repository. Return the updated repository
directory.
"""
# Create the parent directory for repositories.
parent = os.path.join(app.config['DATA_DIR'], 'repo')
if not os.path.exists(parent):
os.makedirs(parent)
# Clone the repository or update it.
repo_dir = os.path.join(parent, repo)
# FIXME log
if os.path.exists(repo_dir):
shell(['git', 'fetch'], log, repo_dir)
shell(['git', 'reset', '--hard', 'origin/master'], log, repo_dir)
else:
shell(['git', 'clone', url, repo_dir], log)
return repo_dir
def run_build(repo_dir, log):
"""Run the build in the repository direction.
"""
# Get the configuration.
config = load_config(repo_dir)
# Run the build.
try:
shell(config['deploy'], log, repo_dir, True)
except subprocess.CalledProcessError as exc:
app.logger.error(
'Deploy exited with status {}'.format(exc.returncode)
)
def log_dir():
"""The directory for storing logs."""
return os.path.join(app.config['DATA_DIR'], 'log')
def open_log(repo):
"""Open a log file for a build and return the open file.
`repo` is the (filename-safe) name of the repository.
"""
# Create the parent directory for the log files.
parent = log_dir()
if not os.path.exists(parent):
os.makedirs(parent)
# Get a log file for this build.
ts = timestamp()
log_fn = os.path.join(parent, '{}#{}.log'.format(repo, ts))
return open(log_fn, 'w')
def client_addr(request):
"""Get the remote address for a request.
If the application has proxying enabled, this might come from the
X-Forwarded-For header. Otherwise, it is the host on the other end
of the connection for the request.
"""
if app.config['PROXIED']:
return request.access_route[0]
else:
return request.remote_addr
class Worker(threading.Thread):
"""Thread used for invoking builds asynchronously.
"""
def __init__(self):
super(Worker, self).__init__()
self.daemon = True
self.queue = queue.Queue()
def run(self):
"""Wait for jobs and execute them with `handle`.
"""
while True:
try:
self.handle(*self.queue.get())
except:
app.logger.error(
'Worker exception:\n' + traceback.format_exc()
)
def handle(self, repo, url):
"""Execute a build.
`repo` is the (filename-safe) repository name. `url` is the git
clone URL for the repo.
"""
app.logger.info('Building {}'.format(repo))
with open_log(repo) as log:
repo_dir = update_repo(repo, url, log)
run_build(repo_dir, log)
def send(self, *args):
"""Add a job to the queue.
"""
self.queue.put(args)
@app.before_first_request
def app_setup():
"""Ensure that the application has some shared global attributes set
up:
- `worker` is a Worker thread
- `github_networks` is the list of valid origin IPNetworks
"""
# Set up logging.
if not app.debug:
# (Flask turns on logging itself in debug mode.)
app.logger.addHandler(logging.StreamHandler())
app.logger.setLevel(logging.INFO)
# Create a worker thread.
if not hasattr(app, 'worker'):
app.worker = Worker()
app.worker.start()
# Get the valid GitHub hook server IP ranges. This can either be fixed in
# the configuration file or loaded automatically from the server.
if not hasattr(app, 'github_networks'):
if app.config['GITHUB_HOOK_SUBNETS']:
cidrs = app.config['GITHUB_HOOK_SUBNETS']
else:
# Load the from the GitHub public API.
meta = requests.get('https://api.github.com/meta').json()
cidrs = meta['hooks']
app.github_networks = [netaddr.IPNetwork(cidr) for cidr in cidrs]
app.logger.info(
'Loaded GitHub networks: %s',
[str(n) for n in app.github_networks]
)
@app.route('/', methods=['POST'])
@app.route('/hook', methods=['POST']) # Backwards-compatibility.
def hook():
"""The web hook endpoint. This is the URL that GitHub uses to send
hooks.
"""
# Ensure that the request is from a GitHub server.
addr = client_addr(request)
for network in app.github_networks:
if addr in network:
break
else:
app.logger.info('Hook request from disallowed host %s', addr)
return flask.jsonify(status='you != GitHub'), 403
# Dispatch based on event type.
event_type = request.headers.get('X-GitHub-Event')
if not event_type:
app.logger.info('Received a non-hook request')
return flask.jsonify(status='not a hook'), 403
elif event_type == 'ping':
return flask.jsonify(status='pong')
elif event_type == 'push':
payload = request.get_json()
repo = payload['repository']
# If a user whitelist is specified, validate the owner.
owner = repo['owner']['name']
name = repo['name']
allowed_users = app.config['USERS']
if allowed_users and owner not in allowed_users:
return flask.jsonify(status='user not allowed', user=owner), 403
if repo['private']:
url_format = app.config['PRIVATE_URL_FORMAT']
else:
url_format = app.config['PUBLIC_URL_FORMAT']
app.worker.send(
FILENAME_FORMAT.format(user=owner, repo=name),
url_format.format(user=owner, repo=name),
)
return flask.jsonify(status='handled'), 202
else:
return flask.jsonify(status='unhandled event', event=event_type), 501
@app.route('/login')
def login():
"""Redirect to GitHub for authentication."""
if not app.config['GITHUB_ID']:
return 'GitHub API disabled', 501
auth_state = flask.session['auth_state'] = random_string()
auth_url = '{}?{}'.format(
'https://github.com/login/oauth/authorize',
urllib.parse.urlencode({
'client_id': app.config['GITHUB_ID'],
'scope': 'read:org,write:repo_hook',
'state': auth_state,
}),
)
app.logger.info(
'Authorizing with GitHub at {}'.format(auth_url),
)
return flask.redirect(auth_url)
@app.route('/auth')
def auth():
"""Receive a callback from GitHub's authentication."""
if not app.config['GITHUB_ID']:
return 'GitHub API disabled', 501
# Get the code from the callback.
if flask.session.get('auth_state') != request.args['state']:
app.logger.error(
'Invalid state from GitHub auth (possible CSRF)'
)
return 'invalid request state', 403
code = request.args['code']
# Turn this into an access token.
resp = requests.post(
'https://github.com/login/oauth/access_token',
data={
'client_id': app.config['GITHUB_ID'],
'client_secret': app.config['GITHUB_SECRET'],
'code': code,
}
)
token = urllib.parse.parse_qs(resp.text)['access_token'][0]
app.logger.info(
'Authorized token with GitHub: {}'.format(token),
)
# Check that the user is on the whitelist.
user_data = github_get('user', token=token).json()
username = user_data['login']
if username not in app.config['USERS']:
# Check if the user is in a whitelisted org.
# TODO: handle pagination if the user is in > 30 orgs
org_data = github_get('user/orgs', token=token).json()
for org in org_data:
if org['login'] in app.config['USERS']:
break
else:
app.logger.warn(
'GitHub user not allowed: {}'.format(username)
)
return 'you are not allowed', 403
# Mark the user as logged in.
flask.session['github_token'] = token
return flask.redirect('/')
@app.route('/')
def home():
token = flask.session.get('github_token')
if token:
# Parse each filename.
logs = []
parent = log_dir()
if os.path.exists(parent):
for fn in os.listdir(parent):
if fn.endswith('.log'):
name, _ = fn.rsplit('.', 1)
try:
user, repo, stamp = name.split('#')
dt = datetime.datetime.strptime(stamp,
TIMESTAMP_FORMAT)
except ValueError:
continue
logs.append({
'user': user,
'repo': repo,
'name': '{}/{}'.format(user, repo),
'dt': dt,
'file': fn,
})
# Sort by time.
logs.sort(key=lambda l: l['dt'], reverse=True)
return flask.render_template('logs.html', logs=logs[:10])
else:
return flask.render_template('login.html')
@app.route('/log/<name>')
def show_log(name):
token = flask.session.get('github_token')
if not token:
return 'no can do', 403
log_name = os.path.basename(name) # Avoid any directories in path.
log_path = os.path.join(log_dir(), log_name)
if not os.path.exists(log_path):
return 'no such log', 404
return flask.send_file(log_path, mimetype='text/plain')
@click.command()
@click.option('--host', '-h', default='0.0.0.0', help='server hostname')
@click.option('--port', '-p', default=5000, help='server port')
@click.option('--debug', '-d', is_flag=True, help='run in debug mode')
@click.option('--user', '-u', multiple=True, help='allowed GitHub users')
@click.option('--github', '-g', help='GitHub client id:secret')
@click.option('--secret', '-s', help='application secret key')
def run(host, port, debug, user, github, secret):
if debug:
app.config['DEBUG'] = debug
if user:
app.config['USERS'] = user
if github and ':' in github:
app.config['GITHUB_ID'], app.config['GITHUB_SECRET'] = \
github.split(':', 1)
if secret:
app.config['SECRET_KEY'] = secret
elif not app.config.get('SECRET_KEY'):
app.config['SECRET_KEY'] = random_string()
app.run(host=host, port=port)
if __name__ == '__main__':
run(auto_envvar_prefix='HOOKNOOK')