|
1 | 1 | #pylint: disable=too-many-lines,too-few-public-methods,too-many-locals,too-many-statements,too-many-branches |
2 | 2 | import os |
3 | 3 | import json |
| 4 | +import time |
4 | 5 | import uuid |
5 | 6 | import copy |
6 | 7 | import urllib.request, urllib.parse, urllib.error |
@@ -43,6 +44,35 @@ def delete_file(path): |
43 | 44 | logger.warning("Failed to delete file: %s", error) |
44 | 45 |
|
45 | 46 |
|
| 47 | +def get_token_by_app_role(app_role_url, role_id, secret_id): |
| 48 | + app_role = {'role_id': role_id, 'secret_id': secret_id} |
| 49 | + json_data = json.dumps(app_role) |
| 50 | + for i in range(0, 10): |
| 51 | + res = requests.post(url=app_role_url, data=json_data, verify=False) |
| 52 | + if res.status_code == 200: |
| 53 | + json_res = json.loads(res.content) |
| 54 | + token = json_res['auth']['client_token'] |
| 55 | + return token |
| 56 | + time.sleep(5) |
| 57 | + err_msg = "Getting token from Vault error even tried 10 times, url is {}, API response is {}:{}".format(app_role_url, res.status_code, res.text) |
| 58 | + abort(400, err_msg) |
| 59 | + |
| 60 | + |
| 61 | +def get_value_from_vault(url, token, secret_key, verify): |
| 62 | + for i in range(0, 10): |
| 63 | + response = requests.get(url=url, headers={'X-Vault-Token': token}, verify=verify) |
| 64 | + if response.status_code == 200: |
| 65 | + json_res = json.loads(response.content) |
| 66 | + if json_res['data'].get('data') and isinstance(json_res['data'].get('data'), dict): |
| 67 | + value = json_res['data'].get('data').get(secret_key) |
| 68 | + else: |
| 69 | + value = json_res['data'].get(secret_key) |
| 70 | + return value |
| 71 | + time.sleep(5) |
| 72 | + err_msg = "Getting value from Vault error even tried 10 times, url is {}, API response is {}:{}".format(url, response.status_code, response.text) |
| 73 | + abort(400, err_msg) |
| 74 | + |
| 75 | + |
46 | 76 | @api.route("/api/job/job", doc=False) |
47 | 77 | class Job(Resource): |
48 | 78 |
|
@@ -295,34 +325,18 @@ def get_secret(name): |
295 | 325 | if validate_res == 'token': |
296 | 326 | logger.info('validate way is token') |
297 | 327 | elif validate_res == 'appRole': |
298 | | - app_role = {'role_id': role_id, 'secret_id': secret_id} |
299 | | - json_data = json.dumps(app_role) |
300 | 328 | app_role_url = result[0] + '/v1/' + namespace + '/auth/approle/login' if namespace else result[0] + '/v1/auth/approle/login' |
301 | | - res = requests.post(url=app_role_url, data=json_data, verify=False) |
302 | | - if res.status_code == 200: |
303 | | - json_res = json.loads(res.content) |
304 | | - token = json_res['auth']['client_token'] |
305 | | - else: |
306 | | - abort(400, "Getting value from vault error: url is '%s', validate way is appRole; API response: '%s'" % (app_role_url, res.text)) |
| 329 | + token = get_token_by_app_role(app_role_url, role_id, secret_id) |
307 | 330 | else: |
308 | 331 | abort(400, "Validate way is '%s' ! result is '%s' " % (validate_res, result)) |
309 | 332 |
|
310 | 333 | if not ca: |
311 | | - res = requests.get(url=url, headers={'X-Vault-Token': token}, verify=False) |
| 334 | + return get_value_from_vault(url, token, secret_key, False) |
312 | 335 | else: |
313 | 336 | with tempfile.NamedTemporaryFile(delete=False) as f: |
314 | 337 | f.write(ca) |
315 | 338 | f.flush() # ensure all data written |
316 | | - res = requests.get(url=url, headers={'X-Vault-Token': token}, verify=f.name) |
317 | | - if res.status_code == 200: |
318 | | - json_res = json.loads(res.content) |
319 | | - if json_res['data'].get('data') and isinstance(json_res['data'].get('data'), dict): |
320 | | - value = json_res['data'].get('data').get(secret_key) |
321 | | - else: |
322 | | - value = json_res['data'].get(secret_key) |
323 | | - return value |
324 | | - else: |
325 | | - abort(400, "Getting value from vault error: url is '%s', token is '%s' " % (url, result)) |
| 339 | + return get_value_from_vault(url, token, secret_key, f.name) |
326 | 340 | else: |
327 | 341 | if is_fork: |
328 | 342 | abort(400, 'Access to secret %s is not allowed from a fork' % name) |
|
0 commit comments