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:
@@ -1,5 +1,23 @@
|
||||
import requests
|
||||
|
||||
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
|
||||
|
||||
|
||||
class HttpClient:
|
||||
def __init__(self, endpoint, token):
|
||||
|
||||
+1
-1
@@ -6,7 +6,7 @@ from routes.main import bp as main_bp
|
||||
from routes.api import bp as api_bp
|
||||
from routes.api_test import bp as api_test_bp
|
||||
|
||||
VERSION = "1.0.53"
|
||||
VERSION = "1.0.54"
|
||||
|
||||
app = Flask(__name__, template_folder="templates", static_folder="static")
|
||||
app.config["NUBES_API_ENDPOINT"] = os.getenv("NUBES_API_ENDPOINT", "https://lk-api-gateway-dev.ngcloud.ru/api/v1/svc")
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
import fcntl
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
|
||||
_PATH = "/tmp/instances.json"
|
||||
|
||||
@@ -13,6 +14,19 @@ _INITIAL = {
|
||||
}
|
||||
|
||||
|
||||
def _acquire_lock(fd):
|
||||
"""LOCK_EX с таймаутом 2 секунды (LOCK_NB + retry)."""
|
||||
deadline = time.time() + 2
|
||||
while True:
|
||||
try:
|
||||
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
return True
|
||||
except BlockingIOError:
|
||||
if time.time() >= deadline:
|
||||
return False
|
||||
time.sleep(0.05)
|
||||
|
||||
|
||||
def _locked_read():
|
||||
"""Читает файл под эксклюзивной блокировкой, seed при отсутствии."""
|
||||
try:
|
||||
@@ -20,7 +34,8 @@ def _locked_read():
|
||||
except OSError:
|
||||
return dict(_INITIAL)
|
||||
try:
|
||||
fcntl.flock(fd, fcntl.LOCK_EX)
|
||||
if not _acquire_lock(fd):
|
||||
return dict(_INITIAL)
|
||||
try:
|
||||
data = os.read(fd, 65536)
|
||||
if data:
|
||||
@@ -45,7 +60,8 @@ def _locked_write(data):
|
||||
except OSError:
|
||||
return
|
||||
try:
|
||||
fcntl.flock(fd, fcntl.LOCK_EX)
|
||||
if not _acquire_lock(fd):
|
||||
return
|
||||
os.lseek(fd, 0, 0)
|
||||
os.ftruncate(fd, 0)
|
||||
os.write(fd, json.dumps(data, indent=2).encode("utf-8"))
|
||||
|
||||
+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