-
Notifications
You must be signed in to change notification settings - Fork 0
/
enc.py
337 lines (272 loc) · 9.97 KB
/
enc.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
from flask import Flask, request, abort, Response
from flask_httpauth import HTTPBasicAuth
import os
from time import strftime
from werkzeug.security import generate_password_hash, check_password_hash
import yaml
_hosts = yaml.safe_load(open("data/hosts.yaml"))
_users = yaml.safe_load(open("data/users.yaml"))
_groups = yaml.safe_load(open("data/groups.yaml"))
app = Flask(__name__)
auth = HTTPBasicAuth()
def make_response(data):
"""Create yaml response."""
if data:
resp = Response(response=yaml.dump(data), status=200, mimetype="text/yaml")
else:
resp = Response(response="", status=200, mimetype="text/yaml")
# resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
def save_data(what, key, value):
"""Save value to the file, and return respone with value."""
if what == "users":
data = _users
elif what == "hosts":
data = _hosts
elif what == "groups":
data = _groups
else:
abort(500)
data = {}
if value:
data[key] = value
else:
del data[key]
with open(os.path.join("data", f"{what}.yaml"), "w") as fp:
yaml.dump(data, fp)
return make_response(value)
@auth.verify_password
def verify_password(username, password):
"""Check username/password, return username if found."""
if username in _users and check_password_hash(_users.get(username)["password"], password):
return username
@auth.get_user_roles
def get_user_roles(username):
"""Return list of roles for the user"""
if username in _users:
return _users.get(username)["roles"]
# ======================================================================
@app.after_request
def after_request(response):
if "/healthz" != request.path:
# 127.0.0.1 - Scott [10/Dec/2019:13:55:36 -0700] "GET /server-status HTTP/1.1" 200 2326
logline = f"{request.remote_addr} -"
if auth.current_user():
logline = logline + f" {auth.current_user()}"
else:
logline = logline + " -"
logline = logline + strftime(' [%Y/%b/%d:%H:%M:%S]')
logline = logline + f' "{request.method} {request.path} {request.environ["SERVER_PROTOCOL"]}"'
logline = logline + f" {response.status_code} {response.content_length}"
print(logline)
return response
# ======================================================================
@app.route("/healthz")
def root():
return "OK"
# ======================================================================
@app.route("/hosts", methods=['GET'])
@auth.login_required(role=['admin', 'user', 'viewer'])
def list_hosts():
return make_response(list(_hosts.keys()))
@app.route("/hosts/<fqdn>", methods=['GET'])
@auth.login_required(role=['admin', 'user', 'viewer'])
def get_host(fqdn):
host = _hosts.get(fqdn)
if host:
return make_response(host)
for (k, v) in _groups.items():
if k == "default":
continue
for h in v['hosts']:
if fqdn.startswith(h):
host = v.copy()
del host['hosts']
return make_response(host)
v = _groups.get("default")
if v:
host = v.copy()
del host['hosts']
return make_response(host)
return make_response(None)
@app.route("/hosts", methods=['POST'])
@auth.login_required(role=['admin', 'user'])
def add_host():
"""Add a new host (fqdn) with specified data (yaml format)"""
fqdn = request.form.get("fqdn")
if not fqdn:
abort(400)
data = request.form.get("data")
if not data:
abort(400)
return save_data("hosts", fqdn, yaml.safe_load(data))
@app.route("/hosts/<fqdn>", methods=['PUT'])
@auth.login_required(role=['admin', 'user'])
def update_host(fqdn):
host = _hosts.get(fqdn)
if not host:
abort(404)
for k in request.form.keys():
if k == "fqdn":
continue
if k == "environment":
host[k] = request.form.get(k)
elif k == "classes":
for v in request.form.getlist(k):
if v.startswith("-"):
if v[:1] in host["classes"]:
del host["classes"][v[:1]]
elif v not in host["classes"]:
host["classes"][v] = None
else:
if isinstance(host["parameters"].get(k, None), list):
for v in request.form.getlist(k):
if v.startswith("-"):
if v[:1] in host["parameters"][k]:
host["parameters"][k].remove(v[:1])
elif v not in host["parameters"][k]:
host["parameters"][k].append(v)
else:
v = request.form.get(k)
if v.startswith("-"):
if k in host["parameters"] and v[:1] == host["parameters"][k]:
del host["parameters"][k]
else:
host["parameters"][k] = v
return save_data("hosts", fqdn, host)
@app.route("/hosts/<fqdn>", methods=['DELETE'])
@auth.login_required(role=['admin'])
def delete_host(fqdn):
if fqdn not in _hosts:
abort(404)
return save_data("hosts", fqdn, None)
# ======================================================================
@app.route("/groups", methods=['GET'])
@auth.login_required(role=['admin', 'user', 'viewer'])
def list_groups():
return make_response(list(_groups.keys()))
@app.route("/groups/<name>", methods=['GET'])
@auth.login_required(role=['admin', 'user', 'viewer'])
def get_group(name):
if name not in _groups:
abort(404)
return make_response(_groups.get(name))
@app.route("/groups", methods=['POST'])
@auth.login_required(role=['admin'])
def add_group():
"""Add a new group with name and with specified data (yaml format)"""
name = request.form.get("name")
if not name:
abort(400)
data = request.form.get("data")
if not data:
abort(400)
return save_data("groups", name, yaml.safe_load(data))
@app.route("/groups/<name>", methods=['PUT'])
@auth.login_required(role=['admin'])
def update_group(name):
data = _groups.get(name, {})
if not data:
abort(404)
for k in request.form.keys():
if k == "name":
continue
if k == "environment":
data[k] = request.form.get(k)
elif k == "classes":
for v in request.form.getlist(k):
if v.startswith("-"):
if v[:1] in data["classes"]:
del data["classes"][v[:1]]
elif v not in data["classes"]:
data["classes"][v] = None
elif k == "hosts":
for v in request.form.getlist(k):
if v.startswith("-"):
if v[:1] in data["hosts"]:
data["hosts"].remove(v[:1])
elif v not in data["hosts"]:
data["hosts"].append(v)
else:
if isinstance(data["parameters"].get(k, None), list):
for v in request.form.getlist(k):
if v.startswith("-"):
if v[:1] in data["parameters"][k]:
data["parameters"][k].remove(v[:1])
elif v not in data["parameters"][k]:
data["parameters"][k].append(v)
else:
v = request.form.get(k)
if v.startswith("-"):
if k in data["parameters"] and v[:1] == data["parameters"][k]:
del data["parameters"][k]
else:
data["parameters"][k] = v
return save_data("groups", name, data)
@app.route("/groups/<name>", methods=['DELETE'])
@auth.login_required(role=['admin'])
def delete_group(name):
if name not in _groups:
abort(404)
if name == "default":
abort(403)
return save_data("users", name, None)
# ======================================================================
@app.route("/users", methods=['GET'])
@auth.login_required(role=['admin'])
def list_users():
return make_response(list(_users.keys()))
@app.route("/users/<username>", methods=['GET'])
@auth.login_required
def get_user(username):
if username != auth.current_user() and "admin" not in get_user_roles(auth.current_user()):
abort(403)
if username not in _users:
abort(404)
return make_response(_users.get(username))
@app.route("/users", methods=['POST'])
@auth.login_required(role=['admin'])
def add_user():
username = request.form.get("username")
if not username:
abort(400)
if username in _users:
abort(400)
password = request.form.get("password")
if not password:
abort(400)
password = generate_password_hash(password)
roles = request.form.getlist("role")
if not roles:
roles = ["viewer"]
user = {"password": password, "roles": roles}
return save_data("users", username, user)
@app.route("/users/<username>", methods=['PUT'])
@auth.login_required(role=['admin'])
def update_user(username):
user = _users.get(username, {})
if not user:
abort(404)
password = request.form.get("password")
if password:
user['password'] = generate_password_hash(password)
for v in request.form.getlist("roles"):
if v.startswith("-"):
if v[:1] in user["roles"]:
user["roles"].remove(v[:1])
elif v not in user["roles"]:
user["roles"].append(v)
return save_data('users', username, user)
@app.route("/users/<username>", methods=['DELETE'])
@auth.login_required(role=['admin'])
def delete_user(username):
if username not in _users:
abort(404)
return save_data("users", username, None)
# ======================================================================
def represent_none(self, _):
return self.represent_scalar('tag:yaml.org,2002:null', '')
# set yaml.dump to print empty value for None
yaml.add_representer(type(None), represent_none)
if __name__ == '__main__':
app.run(debug=True)