-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
216 lines (182 loc) · 5.68 KB
/
api.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
from flask import Flask, request
## https://github.com/flask-restful/flask-restful/pull/913
import flask.scaffold
flask.helpers._endpoint_from_view_func = flask.scaffold._endpoint_from_view_func
from flask_restful import Resource, Api
from subprocess import check_output
from multiprocessing import Process
import os
import json
import sys
import re
app = Flask(__name__)
api = Api(app)
def get_script_output (cmd):
print("[get_script_output] cmd = {}".format(cmd))
try:
return check_output(cmd, shell=True, text=True)
except:
return check_output(cmd, shell=True, universal_newlines=True)
def is_forbidden (var):
forbidden_chars = ["'" , "\"", "&", ";", "|", "\\", "$"]
return any(char in var for char in forbidden_chars)
def is_not_empty (var):
if (isinstance(var, bool)):
return var
elif (isinstance(var, int)):
return True
empty_chars = ["", "null", "nil", "false", "none"]
return var is not None and not any(c == var.lower() for c in empty_chars)
def is_empty (var):
return not is_not_empty(var)
def is_empty_request_field (name):
body = request.get_json(force=True)
return not name in body or is_empty(body[name])
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def is_not_ok(body):
return not "status" in body or body["status"] != "ok"
def check_mandatory_param(name):
if is_empty_request_field(name):
eprint("[check_mandatory_param] bad request : missing argument = {}, body = {}".format(name, request.data))
return {
"status": "bad_request",
"reason": "missing {} argument".format(name)
}
else:
return {
"status": "ok"
}
def run_cmd():
return get_script_output(os.environ['API_CMD'])
def run_cmd_async():
print("[run_cmd_async] output = {}".format(run_cmd()))
def run_cmd_argv():
body = request.get_json(force=True)
cmd = "{} {}".format(os.environ['API_CMD'], body['argv'])
return get_script_output(cmd)
def run_cmd_async_argv():
print("[run_cmd_async] output = {}".format(run_cmd_argv()))
def check_api_cmd_is_defined():
if is_empty(os.environ.get('API_CMD')):
return {
'status': 'forbidden',
'reason': 'API_CMD is not defined for this instance'
}
else:
return {
'status': 'ok'
}
def check_argv_is_enabled():
regexp_argv = os.environ.get('REGEXP_ARGV')
enable_argv = os.environ.get('ENABLE_ARGV')
body = request.get_json(force=True)
argv = body['argv']
if is_empty(enable_argv) or enable_argv != "enabled":
return {
'status': 'forbidden',
'reason': "ENABLE_ARGV is not enabled : value = {}".format(enable_argv)
}
elif is_not_empty(regexp_argv):
if re.match(regexp_argv, argv) and not is_forbidden(argv):
return {
'status': 'ok'
}
else:
return {
'status': 'forbidden',
'reason': "Args {} are not matching {}".format(argv, regexp_argv)
}
else:
return {
'status': 'ok'
}
class AsyncCmdApi(Resource):
def get(self):
c = check_api_cmd_is_defined()
if is_not_ok(c):
return c, 403
async_process = Process(
target=run_cmd_async,
daemon=True
)
async_process.start()
return {
'status': 'ok',
'executed': True,
'async': True
}
def post(self):
c = check_api_cmd_is_defined()
if is_not_ok(c):
return c, 403
c = check_mandatory_param('argv')
if is_not_ok(c):
return c, 400
c = check_argv_is_enabled()
if is_not_ok(c):
return c, 403
async_process = Process(
target=run_cmd_async_argv,
daemon=True
)
async_process.start()
return {
'status': 'ok',
'executed': True,
'async': True
}
class CmdApi(Resource):
def get(self):
c = check_api_cmd_is_defined()
if is_not_ok(c):
return c, 403
output = run_cmd()
return {
'status': 'ok',
'executed': True,
'details': output
}
def post(self):
c = check_api_cmd_is_defined()
if is_not_ok(c):
return c, 403
c = check_mandatory_param('argv')
if is_not_ok(c):
return c, 400
c = check_argv_is_enabled()
if is_not_ok(c):
return c, 403
output = run_cmd_argv()
return {
'status': 'ok',
'executed': True,
'details': output
}
class RootEndPoint(Resource):
def get(self):
return {
'status': 'ok',
'alive': True
}
class ManifestEndPoint(Resource):
def get(self):
try:
with open(os.environ['MANIFEST_FILE_PATH']) as manifest_file:
manifest = json.load(manifest_file)
return manifest
except IOError as err:
return {
'status': 'error',
'reason': err
}, 500
health_check_routes = ['/', '/health', '/health/']
cmd_routes = ['/cmd', '/cmd-api', '/cmd/', '/cmd-api/']
async_cmd_routes = ['/cmd/async', '/cmd-api/async', '/cmd/async/', '/cmd-api/async/']
manifest_routes = ['/manifest', '/manifest/']
api.add_resource(RootEndPoint, *health_check_routes)
api.add_resource(CmdApi, *cmd_routes)
api.add_resource(AsyncCmdApi, *async_cmd_routes)
api.add_resource(ManifestEndPoint, *manifest_routes)
if __name__ == '__main__':
app.run()