-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwsgi.py
131 lines (118 loc) · 4.6 KB
/
wsgi.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
#!/usr/bin/python3
# -*- encoding: utf8 -*-
#
# The Qubes OS Project, http://www.qubes-os.org
#
# Copyright (C) 2017 Marek Marczykowski-Górecki
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import os
import json
import subprocess
import sys
import check_git_signature
# defaults
config_defaults = {
'sig_checker_command': './check-git-signature',
'owner_whitelist': 'QubesOS',
'github_api_token': '',
'keyring': '',
'repo_whitelist': '',
'repo_blacklist': '',
}
config = {}
def response_wrapper(status, start_response):
start_response(status, [])
return iter([b''])
def app(environ, start_response):
global config
# input data
untrusted_obj = json.load(environ['wsgi.input'])
if 'pull_request' not in untrusted_obj:
return response_wrapper('200 OK', start_response)
if untrusted_obj['action'] not in ['opened', 'synchronize']:
return response_wrapper('200 OK', start_response)
try:
untrusted_repo_full_name = untrusted_obj['pull_request']['base']['repo']['full_name']
untrusted_pr_number = untrusted_obj['pull_request']['number']
(untrusted_repo_owner, untrusted_repo_name) = \
untrusted_repo_full_name.split('/', 1)
owner_whitelist = config.get('owner_whitelist').split(' ')
if untrusted_repo_owner not in owner_whitelist:
raise Exception('Repository owner not whitelisted')
repo_owner = untrusted_repo_owner
if '/' in untrusted_repo_name:
raise Exception('Invalid character in repository name')
repo_whitelist = config.get('repo_whitelist')
if repo_whitelist:
if untrusted_repo_name not in repo_whitelist.split(' '):
raise Exception('Repository not whitelistd')
repo_blacklist = config.get('repo_blacklist')
if repo_blacklist:
if untrusted_repo_name in repo_blacklist.split(' '):
raise Exception('Repository blacklistd')
except Exception as e:
print(str(e), file=sys.stderr)
return response_wrapper('204 No content', start_response)
# input data sanitized
repo_name = untrusted_repo_name
pr_number = int(untrusted_pr_number)
sig_checker_command = [
config.get('sig_checker_command'),
'--clone', 'https://github.com/{}/{}'.format(repo_owner, repo_name),
'--pull-request', str(pr_number),
'--download-keys',
'--set-commit-status',
'--verbose',
]
keyring = config.get('keyring')
if keyring:
sig_checker_command += ['--keyring', keyring]
command_env = os.environ.copy()
if config.get('github_api_token'):
command_env['GITHUB_API_TOKEN'] = \
config.get('github_api_token')
try:
check_git_signature.main(sig_checker_command[1:])
except Exception as e:
import traceback
traceback.print_exc(file=sys.stderr)
return response_wrapper('500 Error', start_response)
return response_wrapper('200 OK', start_response)
# load config
config = config_defaults.copy()
for key in config:
env_key = 'CHECKER_CONFIG_' + key
if env_key in os.environ:
config[key] = os.environ[env_key]
# check gpg presence & initialize its config
try:
gpg_bin = os.environ.get('GPG', 'gpg2')
subprocess.check_call([gpg_bin, '-k'],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
# load pre-fetched keys
if os.path.exists('keys'):
for f in os.listdir('keys'):
if f.endswith('.asc'):
args = ['--import', 'keys/' + f]
if config.get('keyring'):
args.insert(0, '--no-default-keyring')
args.insert(0, '--keyring')
args.insert(1, config.get('keyring'))
subprocess.call([gpg_bin] + args)
except subprocess.CalledProcessError:
print('No gpg found ({})!'.format(gpg_bin), file=sys.stderr)
sys.exit(1)