v1.0.54: fix 4 audit bugs — _client auto-detect, _find_uid by key, tracker_add before params, flock LOCK_NB
This commit is contained in:
+14
-10
@@ -1,6 +1,6 @@
|
||||
from flask import Blueprint, current_app, jsonify, request
|
||||
|
||||
from api.http_client import HttpClient
|
||||
from api.http_client import HttpClient, detect_endpoint
|
||||
from operations.get_services import get_services, get_service_detail
|
||||
from operations.get_instances import get_instances
|
||||
from operations.tracker import add as tracker_add, list_all as tracker_list
|
||||
@@ -11,7 +11,8 @@ bp = Blueprint("api_test", __name__)
|
||||
|
||||
def _client():
|
||||
token = request.cookies.get("token") or current_app.config["NUBES_API_TOKEN"]
|
||||
return HttpClient(current_app.config["NUBES_API_ENDPOINT"], token)
|
||||
endpoint = detect_endpoint(token) or current_app.config["NUBES_API_ENDPOINT"]
|
||||
return HttpClient(endpoint, token)
|
||||
|
||||
|
||||
@bp.route("/api/services")
|
||||
@@ -118,12 +119,8 @@ def api_test():
|
||||
if not op_uid:
|
||||
return jsonify({"status": "FAIL", "error": "Не удалось получить opUid"}), 500
|
||||
|
||||
for pid, pval in params.items():
|
||||
client.post("/instanceOperationCfsParams",
|
||||
{"instanceOperationUid": op_uid, "svcOperationCfsParamId": int(pid), "paramValue": str(pval)})
|
||||
client.post(f"/instanceOperations/{op_uid}/run")
|
||||
|
||||
# Записать в трекер СРАЗУ, до фонового потока (поток может умереть под gunicorn)
|
||||
# Записать в трекер СРАЗУ после получения instanceUid, до params/run
|
||||
# (если params упадут — инстанс уже отслежен, сирот не будет)
|
||||
try:
|
||||
tracker_add(instance_uid, svc_id, display_name)
|
||||
except Exception as e:
|
||||
@@ -131,6 +128,11 @@ def api_test():
|
||||
print(f"[TRACKER ERROR] add failed: {e}", flush=True)
|
||||
traceback.print_exc()
|
||||
|
||||
for pid, pval in params.items():
|
||||
client.post("/instanceOperationCfsParams",
|
||||
{"instanceOperationUid": op_uid, "svcOperationCfsParamId": int(pid), "paramValue": str(pval)})
|
||||
client.post(f"/instanceOperations/{op_uid}/run")
|
||||
|
||||
# фоном ждать завершения
|
||||
threading.Thread(target=_finish_op, args=(client, op_uid, instance_uid, svc_id, display_name, op_name, svc_op_id, True), daemon=True).start()
|
||||
return jsonify({"status": "RUNNING", "opUid": op_uid, "instanceUid": instance_uid})
|
||||
@@ -239,9 +241,11 @@ def api_test_status(op_uid):
|
||||
|
||||
|
||||
def _find_uid(resp):
|
||||
"""Извлечь instanceUid или instanceOperationUid из ответа API."""
|
||||
if isinstance(resp, dict):
|
||||
for v in resp.values():
|
||||
if isinstance(v, str) and len(v) == 36 and v.count("-") == 4:
|
||||
for key in ("instanceOperationUid", "instanceUid", "uid", "Uid"):
|
||||
v = resp.get(key)
|
||||
if isinstance(v, str) and v:
|
||||
return v
|
||||
return None
|
||||
|
||||
|
||||
+2
-20
@@ -1,6 +1,6 @@
|
||||
from flask import Blueprint, current_app, render_template, request, make_response, jsonify
|
||||
|
||||
from api.http_client import HttpClient
|
||||
from api.http_client import HttpClient, detect_endpoint
|
||||
from operations.get_instances import get_organization, get_instances
|
||||
from operations.get_services import get_services, get_service_detail
|
||||
from operations.tracker import list_all as tracker_list
|
||||
@@ -8,24 +8,6 @@ from runner import load_config
|
||||
|
||||
bp = Blueprint("main", __name__)
|
||||
|
||||
STANDS = [
|
||||
"https://lk-api-gateway-dev.ngcloud.ru/api/v1/svc",
|
||||
"https://lk-api-gateway-test.ngcloud.ru/api/v1/svc",
|
||||
]
|
||||
|
||||
|
||||
def _detect_endpoint(token):
|
||||
"""Пробуем токен против dev и test стендов, возвращаем рабочий URL."""
|
||||
for ep in STANDS:
|
||||
try:
|
||||
c = HttpClient(ep, token)
|
||||
data = c.get("/instances", params={"pageSize": 1, "page": 1})
|
||||
if data.get("results") is not None:
|
||||
return ep
|
||||
except Exception:
|
||||
continue
|
||||
return None
|
||||
|
||||
|
||||
def _mask(s):
|
||||
if not s or len(s) < 8:
|
||||
@@ -113,7 +95,7 @@ def index():
|
||||
instance_groups = {}
|
||||
config = {}
|
||||
if active_token:
|
||||
endpoint = _detect_endpoint(active_token)
|
||||
endpoint = detect_endpoint(active_token)
|
||||
if endpoint:
|
||||
try:
|
||||
client = HttpClient(endpoint, active_token)
|
||||
|
||||
Reference in New Issue
Block a user