-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
executable file
·141 lines (111 loc) · 4.45 KB
/
run.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from aiohttp import web, BasicAuth, ClientSession, ClientConnectorError
from argparse import ArgumentParser
from asyncio import TimeoutError, set_event_loop_policy
from logging import error, getLogger
from os import path
from urllib.parse import parse_qs
from uvloop import EventLoopPolicy
from yajl import dumps, loads
config = dict()
version = dict()
routes = web.RouteTableDef()
@routes.get('/ping')
async def get_handler(request):
return web.Response(text = 'Pong')
@routes.post('/{project}')
async def get_handler(request):
username = None
password = None
project = request.match_info['project']
if project not in config:
return web.Response(text='Project now found', status=404)
data = (await request.read()).decode('UTF-8')
if not data:
return web.Response(text='Credentials are required', status=400)
try:
data = parse_qs(data)
except ValueError:
return web.Response(text='Wrong credentials format', status=400)
if 'username' in data:
username = data['username'][0]
if 'password' in data:
password = data['password'][0]
if not username or not password:
return web.Response(text='Username or password are not defined ', status=400)
data = {'grant_type': 'password',
'username': username,
'password': password}
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
url = config[project]['keyrock']+'/oauth2/token'
async with ClientSession() as session:
try:
auth = config[project]['auth']
timeout = config[project]['timeout']
async with session.post(url, auth=auth, data=data, headers=headers, timeout=timeout) as response:
status = response.status
text = loads(await response.text())
except ClientConnectorError:
return web.Response(text='Token request failed due to the connection problem', status=502)
except TimeoutError:
return web.Response(text='Token request failed due to the timeout', status=504)
except Exception as exception:
error('request_token, %s', exception)
return web.HTTPInternalServerError()
if status == 200:
return web.Response(text=text['access_token'])
else:
if 'message' in text['error']:
text = text['error']['message']
else:
text = text['error']
return web.Response(text="Token request failed due to the: " + text, status = status)
@routes.get('/version')
async def get_handler(request):
return web.Response(text=version)
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('--ip', default='0.0.0.0', help='ip to use, default is 0.0.0.0')
parser.add_argument('--port', default=8080, help="port to use, default is 8080")
parser.add_argument('--config', default='/opt/config.json', help='path to config file, default is /opt/config.json')
args = parser.parse_args()
getLogger().setLevel(40)
set_event_loop_policy(EventLoopPolicy())
version_path = './version'
if not path.isfile(version_path):
error('Version file not found')
exit(1)
try:
with open(version_path) as f:
version_file = f.read().split('\n')
version['build'] = version_file[0]
version['commit'] = version_file[1]
version = dumps(version)
except IndexError:
error('Unsupported version file type')
exit(1)
if not path.isfile(args.config):
error('Config file not found')
exit(1)
try:
with open(args.config) as file:
temp = loads(file.read())
except ValueError:
error('Unsupported config type')
exit(1)
try:
for element in temp['projects']:
config[element['project']] = dict()
config[element['project']]['keyrock'] = element['keyrock']
config[element['project']]['auth'] = BasicAuth(element['client_id'], element['client_secret'])
if 'timeout' in config[element['project']]:
config[element['project']]['timeout'] = element['timeout']
else:
config[element['project']]['timeout'] = None
except KeyError:
error('Config is not correct')
exit(1)
app = web.Application()
app.add_routes(routes)
web.run_app(app, host=args.ip, port=args.port)