-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
355 lines (260 loc) · 11.2 KB
/
app.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
from base64 import b64encode
from os import getenv
from flask import Flask, request
from requests import post, Session, adapters, get, delete
from requests import request as req_request
import json
import re
app = Flask(__name__)
dashboard_urls = {}
dashboard_uids = {}
dashboard_ids = {}
user_ids = {}
oidc_client_id = getenv("OIDC_CLIENT_ID", "sodalite-ide")
oidc_client_secret = getenv("OIDC_CLIENT_SECRET", "")
oidc_introspection_endpoint = getenv("OIDC_INTROSPECTION_ENDPOINT", "")
vault_endpoint = getenv("VAULT_ADDRESS", "") + ":" + getenv("VAULT_PORT", "8200")
vault_admin_token = getenv("VAULT_ADMIN_TOKEN", "")
session = Session()
adapter = adapters.HTTPAdapter(pool_connections=100, pool_maxsize=100)
for protocol in ['http:', 'https:']:
session.mount(protocol, adapter)
@app.route('/croupier', methods=['POST'])
def upload_croupier_secret():
json_data = request.json
if "host" in json_data:
host = json_data["host"]
else:
return "Request must include host or service (\"host\")\n", 403
host = _validate_host(host)
if not host:
return "Not a valid host", 403
json_secret = json_data
secret_endpoint = "http://" + vault_endpoint + "/v1/croupier/{0}/" + host
return _upload_secret(request, secret_endpoint, json_secret, "croupier")
@app.route('/ssh', methods=['POST'])
def upload_ssh_secret():
json_data = request.json
if "ssh_host" in json_data:
ssh_host = json_data["ssh_host"]
else:
return "Request must include SSH host (\"ssh_host\")\n", 403
ssh_host = _validate_host(ssh_host)
if not ssh_host:
return "Not a valid host", 403
if "ssh_user" in json_data:
ssh_user = json_data["ssh_user"]
else:
return "Request must include SSH user (\"ssh_user\")\n", 403
ssh_pw = json_data["ssh_password"] if "ssh_password" in json_data else ""
ssh_pkey = json_data["ssh_pkey"] if "ssh_pkey" in json_data else ""
if not ssh_pw and not ssh_pkey:
return "Request must include either SSH password (\"ssh_pw\") or SSH private key (\"ssh_pkey\")\n", 403
json_secret = {"ssh_user": ssh_user, "ssh_host": ssh_host}
if ssh_pw:
json_secret["ssh_password"] = ssh_pw
if ssh_pkey:
json_secret["ssh_pkey"] = ssh_pkey
secret_endpoint = "http://" + vault_endpoint + "/v1/ssh/{0}/" + ssh_host
return _upload_secret(request, secret_endpoint, json_secret, "ssh")
@app.route('/keycloak', methods=['POST'])
def upload_keycloak_secret():
json_data = request.json
if "password" in json_data:
password = json_data["password"]
else:
return "Request must include keycloak password (\"password\")\n", 403
json_secret = {"password": password}
secret_endpoint = "http://" + vault_endpoint + "/v1/keycloak/{0}"
return _upload_secret(request, secret_endpoint, json_secret, "keycloak")
@app.route('/ssh', methods=['GET'])
def list_ssh_secrets():
try:
jwt = _get_token(request)
user_info = _token_info(jwt)
except Exception as e:
return str(e), 500
if not user_info:
return "Unauthorized access\n", 401
username = user_info["preferred_username"]
vault_user_token = _get_vault_token(jwt, username)
if vault_user_token == "":
return ({}, 200)
secret_endpoint = "http://" + vault_endpoint + "/v1/ssh/" + username
auth_header = {"x-vault-token": vault_user_token}
vault_secret_response = req_request('LIST', secret_endpoint, headers=auth_header)
if not vault_secret_response.ok:
return ("There was a problem listing the secrets from vault:\n" + str(vault_secret_response.content) + "\n",
vault_secret_response.status_code)
list_secrets = {"list": vault_secret_response.json()["data"]["keys"]}
return list_secrets, 200
@app.route('/ssh/<ssh_host>', methods=['GET'])
def get_ssh_secret(ssh_host):
secret_endpoint = "http://" + vault_endpoint + "/v1/ssh/{0}/" + ssh_host
return _get_secret(request, secret_endpoint)
@app.route('/keycloak', methods=['GET'])
def get_keycloak_secret():
secret_endpoint = "http://" + vault_endpoint + "/v1/keycloak/{0}"
return _get_secret(request, secret_endpoint)
@app.route('/croupier', methods=['GET'])
def get_croupier_secret():
secret_endpoint = "http://" + vault_endpoint + "/v1/croupier/{0}"
return _get_secret(request, secret_endpoint)
@app.route('/ssh/<ssh_host>', methods=['DELETE'])
def delete_ssh_secret(ssh_host):
ssh_host = _validate_host(ssh_host)
if not ssh_host:
return "Not a valid host", 403
secret_endpoint = "http://" + vault_endpoint + "/v1/ssh/{0}/" + ssh_host
return _delete_secret(request, secret_endpoint)
@app.route('/keycloak', methods=['DELETE'])
def delete_keycloak_secret():
secret_endpoint = "http://" + vault_endpoint + "/v1/keycloak/{0}"
return _delete_secret(request, secret_endpoint)
@app.route('/croupier', methods=['DELETE'])
def delete_croupier_secret():
secret_endpoint = "http://" + vault_endpoint + "/v1/croupier/{0}"
return _delete_secret(request, secret_endpoint)
def _upload_secret(request, endpoint, json_secret, secret_type):
try:
user_info = _token_info(_get_token(request))
except Exception as e:
return str(e), 500
if not user_info:
return "Unauthorized access\n", 401
username = user_info["preferred_username"]
auth_header = {"x-vault-token": vault_admin_token}
json_policy = {
"policy": "path \"/" + secret_type + "/" + username + "/*\"\n"
"{\n"
" capabilities = [\"create\", \"read\", \"update\", \"delete\", \"list\"]\n"
"}\n"
"path \"/" + secret_type + "/" + username + "\"\n"
"{\n"
" capabilities = [\"create\", \"read\", \"update\", \"delete\", \"list\"]\n"
"}\n"
}
role_endpoint = "http://" + vault_endpoint + "/v1/auth/jwt/role/" + username
role_policies = _get_role_policies(role_endpoint)
if isinstance(role_policies, list):
role_policies.append(secret_type + "-" + username)
else:
return ("There was a problem retrieving roles for the user")
json_role = {
"policies": role_policies,
"role_type": "jwt",
"bound_audiences": "account",
"user_claim": "email",
"groups_claim": "",
"bound_claims": {
"preferred_username": username
}
}
policy_endpoint = "http://" + vault_endpoint + "/v1/sys/policy/" + secret_type + "-" + username
secret_endpoint = endpoint.format(username)
policy_response = post(policy_endpoint, data=json.dumps(json_policy), headers=auth_header)
if not policy_response.ok:
return ("There was a problem creating the policy:\n" + str(policy_response.content) + "\n",
policy_response.status_code)
role_response = post(role_endpoint, data=json.dumps(json_role), headers=auth_header)
if not role_response.ok:
return ("There was a problem binding the keycloak user to the policy:\n" + str(role_response.content) + "\n",
role_response.status_code)
secret_response = post(secret_endpoint, data=json.dumps(json_secret), headers=auth_header)
if not secret_response.ok:
return ("There was a problem creating the secret:\n" + str(secret_response.content) + "\n",
secret_response.status_code)
return "Secret uploaded correctly\n", 200
def _get_role_policies(endpoint):
auth_header = {"x-vault-token": vault_admin_token}
vault_response = get(endpoint, headers=auth_header)
if vault_response.status_code == 404:
return []
elif vault_response.ok:
response_json = vault_response.json()
if "policies" in response_json["data"]:
return response_json["data"]["policies"]
else:
return []
else:
return None
def _get_secret(request, endpoint):
try:
jwt = _get_token(request)
user_info = _token_info(jwt)
except Exception as e:
return str(e), 500
if not user_info:
return "Unauthorized access\n", 401
username = user_info["preferred_username"]
vault_user_token = _get_vault_token(jwt, username)
if vault_user_token == "":
return ({}, 200)
secret_endpoint = endpoint.format(username)
auth_header = {"x-vault-token": vault_user_token}
vault_secret_response = get(secret_endpoint, headers=auth_header)
if not vault_secret_response.ok:
return ("There was a problem getting the secret from vault:\n" + str(vault_secret_response.content) + "\n",
vault_secret_response.status_code)
json_data = vault_secret_response.json()["data"]
return json_data, 200
def _delete_secret(request, endpoint):
try:
jwt = _get_token(request)
user_info = _token_info(jwt)
except Exception as e:
return str(e), 500
if not user_info:
return "Unauthorized access\n", 401
username = user_info["preferred_username"]
vault_user_token = _get_vault_token(jwt, username)
if vault_user_token == "":
return ("No secrets found for the user", 404)
secret_endpoint = endpoint.format(username)
auth_header = {"x-vault-token": vault_user_token}
vault_secret_response = delete(secret_endpoint, headers=auth_header)
if not vault_secret_response.ok:
return ("There was a problem deleting the secret from vault:\n" + str(vault_secret_response.content) + "\n",
vault_secret_response.status_code)
return "Secret deleted successfully", 200
def _token_info(access_token) -> dict:
req = {'token': access_token}
headers = {'Content-type': 'application/x-www-form-urlencoded'}
if not oidc_introspection_endpoint:
raise Exception("No oidc_introspection_endpoint set on the server\n")
basic_auth_string = '{0}:{1}'.format(oidc_client_id, oidc_client_secret)
basic_auth_bytes = bytearray(basic_auth_string, 'utf-8')
headers['Authorization'] = 'Basic {0}'.format(b64encode(basic_auth_bytes).decode('utf-8'))
token_response = post(oidc_introspection_endpoint, data=req, headers=headers)
if not token_response.ok:
raise Exception("There was a problem trying to authenticate with keycloak:\n"
" HTTP code: " + str(token_response.status_code) + "\n"
" Content:" + str(token_response.content) + "\n")
json = token_response.json()
if "active" in json and json["active"] is False:
return {}
return json
def _get_token(r):
auth_header = r.environ["HTTP_AUTHORIZATION"].split()
if auth_header[0] == "Bearer":
return auth_header[1]
return ""
def _get_vault_token(jwt, username):
url = "http://" + vault_endpoint + "/v1/auth/jwt/login"
payload = {
"jwt": jwt,
"role": username
}
response = post(url, json=payload)
if response.ok:
json = response.json()
user_token = json["auth"]["client_token"]
return user_token
else:
return ""
def _validate_host(host):
pattern = re.compile("^(http://|https://)?([a-z0-9A-Z_-]+[/:.])*[a-zA-Z0-9_]+/?$")
if pattern.match(host):
return host.replace('/','').replace(':','')
else:
return None