407 lines
17 KiB
Python
407 lines
17 KiB
Python
#!/usr/bin/env python3
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import base64
|
|
import json
|
|
import os
|
|
import pathlib
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
from typing import Any
|
|
from urllib import error, parse, request
|
|
|
|
|
|
ROOT = pathlib.Path(__file__).resolve().parents[1]
|
|
DEFAULT_NAMESPACE = "droboticflow"
|
|
DEFAULT_SECRET_NAME = "droboticflow-dcloud-secret"
|
|
|
|
|
|
def first_non_empty(values: list[str]) -> str:
|
|
for value in values:
|
|
if value.strip():
|
|
return value.strip()
|
|
return ""
|
|
|
|
|
|
def decode_secret_data(secret: dict[str, Any]) -> dict[str, str]:
|
|
data = secret.get("data") if isinstance(secret.get("data"), dict) else {}
|
|
decoded: dict[str, str] = {}
|
|
for key, value in data.items():
|
|
if not isinstance(key, str) or not isinstance(value, str):
|
|
continue
|
|
try:
|
|
decoded[key] = base64.b64decode(value, validate=True).decode("utf-8", errors="replace").strip()
|
|
except Exception:
|
|
continue
|
|
return decoded
|
|
|
|
|
|
def load_kubernetes_secret(namespace: str, secret_name: str) -> dict[str, str]:
|
|
result = subprocess.run(
|
|
["kubectl", "-n", namespace, "get", "secret", secret_name, "-o", "json"],
|
|
text=True,
|
|
capture_output=True,
|
|
check=False,
|
|
)
|
|
if result.returncode != 0:
|
|
raise RuntimeError(f"Kubernetes Secret {namespace}/{secret_name} is missing or unreadable")
|
|
try:
|
|
payload = json.loads(result.stdout)
|
|
except json.JSONDecodeError as exc:
|
|
raise RuntimeError(f"Kubernetes Secret {namespace}/{secret_name} did not return valid JSON") from exc
|
|
if not isinstance(payload, dict):
|
|
raise RuntimeError(f"Kubernetes Secret {namespace}/{secret_name} response is not a JSON object")
|
|
return decode_secret_data(payload)
|
|
|
|
|
|
def env_context() -> dict[str, str]:
|
|
return {key: value.strip() for key, value in os.environ.items() if value.strip()}
|
|
|
|
|
|
def jwt_token_error(token: str) -> str:
|
|
normalized = token.strip()
|
|
if normalized.lower().startswith("bearer "):
|
|
normalized = normalized.split(None, 1)[1].strip()
|
|
parts = normalized.split(".")
|
|
if len(parts) < 3:
|
|
return ""
|
|
try:
|
|
payload_b64 = parts[1] + "=" * ((4 - len(parts[1]) % 4) % 4)
|
|
payload = json.loads(base64.urlsafe_b64decode(payload_b64).decode("utf-8"))
|
|
except Exception:
|
|
return "auth token looks like a JWT but cannot be decoded"
|
|
exp = payload.get("exp")
|
|
if exp is None:
|
|
return "JWT auth token has no exp claim"
|
|
try:
|
|
expires_at = float(exp)
|
|
except (TypeError, ValueError):
|
|
return "JWT auth token has an invalid exp claim"
|
|
if expires_at <= time.time():
|
|
return "JWT auth token is expired"
|
|
return ""
|
|
|
|
|
|
def context_value(context: dict[str, str], *keys: str) -> str:
|
|
return first_non_empty([context.get(key, "") for key in keys])
|
|
|
|
|
|
def normalize_base_url(value: str) -> str:
|
|
parsed = parse.urlparse(value)
|
|
if parsed.scheme not in {"http", "https"} or not parsed.netloc or any(ch.isspace() for ch in value):
|
|
raise RuntimeError("DCLOUD_FAT_BASE_URL must be an absolute http(s) URL without whitespace")
|
|
return value.rstrip("/")
|
|
|
|
|
|
def signin_token(context: dict[str, str], base_url: str, timeout: float) -> str:
|
|
username = context_value(context, "DCLOUD_FAT_USERNAME", "DCLOUD_USERNAME")
|
|
password = context_value(context, "DCLOUD_FAT_PASSWORD", "DCLOUD_PASSWORD")
|
|
if not username and not password:
|
|
return ""
|
|
if not username or not password:
|
|
raise RuntimeError("DCLOUD_FAT_USERNAME and DCLOUD_FAT_PASSWORD must be configured together")
|
|
signin_url = context_value(context, "DCLOUD_FAT_SIGNIN_URL")
|
|
if not signin_url:
|
|
endpoint = context_value(context, "DCLOUD_FAT_SIGNIN_ENDPOINT") or "/api/signin"
|
|
signin_url = base_url + "/" + endpoint.lstrip("/")
|
|
payload = json.dumps({"Username": username, "Password": password}, ensure_ascii=False).encode("utf-8")
|
|
req = request.Request(
|
|
signin_url,
|
|
data=payload,
|
|
method="POST",
|
|
headers={"Accept": "application/json", "Content-Type": "application/json"},
|
|
)
|
|
try:
|
|
with request.urlopen(req, timeout=timeout) as response:
|
|
raw = response.read().decode("utf-8", errors="replace")
|
|
except error.HTTPError as exc:
|
|
exc.read()
|
|
raise RuntimeError(f"D-Cloud signin failed with HTTP {exc.code}") from exc
|
|
try:
|
|
parsed = json.loads(raw)
|
|
except json.JSONDecodeError as exc:
|
|
raise RuntimeError("D-Cloud signin did not return JSON") from exc
|
|
token = find_token(parsed)
|
|
if not token:
|
|
raise RuntimeError("D-Cloud signin did not return a token")
|
|
return token
|
|
|
|
|
|
def find_token(value: Any) -> str:
|
|
if isinstance(value, str):
|
|
stripped = value.strip()
|
|
if stripped.count(".") >= 2 or stripped.lower().startswith("bearer "):
|
|
return stripped
|
|
return ""
|
|
if isinstance(value, dict):
|
|
for key in ("token", "accessToken", "access_token", "idToken", "id_token", "jwt", "authorization", "Authorization"):
|
|
token = find_token(value.get(key))
|
|
if token:
|
|
return token
|
|
for nested in value.values():
|
|
token = find_token(nested)
|
|
if token:
|
|
return token
|
|
if isinstance(value, list):
|
|
for nested in value:
|
|
token = find_token(nested)
|
|
if token:
|
|
return token
|
|
return ""
|
|
|
|
|
|
def load_operator_external_api(slug: str) -> dict[str, Any]:
|
|
path = ROOT / "backend" / "operators" / "builtin" / slug / "operator.json"
|
|
try:
|
|
payload = json.loads(path.read_text(encoding="utf-8"))
|
|
except FileNotFoundError as exc:
|
|
raise RuntimeError(f"operator metadata is missing: {path}") from exc
|
|
external_api = payload.get("externalApi") if isinstance(payload, dict) else None
|
|
if not isinstance(external_api, dict):
|
|
raise RuntimeError(f"operator metadata externalApi is missing: {path}")
|
|
probe = external_api.get("acceptanceProbe")
|
|
if not isinstance(probe, dict):
|
|
raise RuntimeError(f"operator metadata externalApi.acceptanceProbe is missing: {path}")
|
|
return external_api
|
|
|
|
|
|
def redact_text(value: str, redact_values: set[str]) -> str:
|
|
sanitized = value
|
|
for secret in sorted((item for item in redact_values if len(item) >= 4), key=len, reverse=True):
|
|
sanitized = sanitized.replace(secret, "<redacted>")
|
|
if len(sanitized) > 240:
|
|
sanitized = sanitized[:237] + "..."
|
|
return sanitized
|
|
|
|
|
|
def safe_scalar(value: Any, redact_values: set[str]) -> Any:
|
|
if isinstance(value, str):
|
|
return redact_text(value, redact_values)
|
|
if isinstance(value, (int, float, bool)) or value is None:
|
|
return value
|
|
return None
|
|
|
|
|
|
def response_summary(status: int, raw: bytes, elapsed: float, redact_values: set[str] | None = None) -> dict[str, Any]:
|
|
redact_values = redact_values or set()
|
|
text = raw.decode("utf-8", errors="replace")
|
|
body: Any = text
|
|
if text.strip().startswith(("{", "[")):
|
|
try:
|
|
body = json.loads(text)
|
|
except json.JSONDecodeError:
|
|
body = text
|
|
body_kind = "json-object" if isinstance(body, dict) else "json-array" if isinstance(body, list) else "text"
|
|
success_field = body.get("success") if isinstance(body, dict) else None
|
|
accepted = 200 <= status < 300 and success_field is not False
|
|
summary: dict[str, Any] = {
|
|
"status": status,
|
|
"accepted": accepted,
|
|
"elapsedSeconds": round(elapsed, 3),
|
|
"bodyKind": body_kind,
|
|
}
|
|
if isinstance(body, dict):
|
|
summary["bodyKeys"] = sorted(str(key) for key in body.keys())[:20]
|
|
if success_field is not None:
|
|
summary["successField"] = bool(success_field)
|
|
diagnostic_fields: dict[str, Any] = {}
|
|
for source_key, target_key in (
|
|
("status", "bodyStatus"),
|
|
("code", "bodyCode"),
|
|
("message", "message"),
|
|
("location", "location"),
|
|
("error", "error"),
|
|
("detail", "detail"),
|
|
):
|
|
if source_key not in body:
|
|
continue
|
|
safe_value = safe_scalar(body.get(source_key), redact_values)
|
|
if safe_value is not None:
|
|
diagnostic_fields[target_key] = safe_value
|
|
if diagnostic_fields:
|
|
summary["diagnostic"] = diagnostic_fields
|
|
elif isinstance(body, list):
|
|
summary["arrayLength"] = len(body)
|
|
else:
|
|
summary["bodyBytes"] = len(raw)
|
|
return summary
|
|
|
|
|
|
def probe_operator(slug: str, context: dict[str, str], base_url: str, token: str, timeout: float) -> dict[str, Any]:
|
|
external_api = load_operator_external_api(slug)
|
|
probe = external_api["acceptanceProbe"]
|
|
endpoint = str(probe.get("endpoint", "")).strip()
|
|
method = (str(probe.get("method", "")).strip() or "POST").upper()
|
|
request_json = probe.get("requestJson") if "requestJson" in probe else None
|
|
query_json = probe.get("queryJson") if "queryJson" in probe else None
|
|
if not endpoint:
|
|
raise RuntimeError(f"{slug} acceptanceProbe.endpoint is required")
|
|
url = base_url + "/" + endpoint.lstrip("/")
|
|
if query_json:
|
|
if not isinstance(query_json, dict):
|
|
raise RuntimeError(f"{slug} acceptanceProbe.queryJson must be an object")
|
|
url += ("&" if "?" in url else "?") + parse.urlencode({str(key): str(value) for key, value in query_json.items()})
|
|
body = None
|
|
headers = {"Accept": "application/json"}
|
|
if method not in {"GET", "HEAD"}:
|
|
body = json.dumps(request_json if request_json is not None else {}, ensure_ascii=False).encode("utf-8")
|
|
headers["Content-Type"] = "application/json"
|
|
if token:
|
|
headers["Authorization"] = token if token.lower().startswith("bearer ") else f"Bearer {token}"
|
|
cookie = context_value(context, "DCLOUD_FAT_COOKIE", "DCLOUD_COOKIE")
|
|
if cookie:
|
|
headers["Cookie"] = cookie
|
|
d_user_id = context_value(context, "DCLOUD_D_USER_ID", "D_USER_ID")
|
|
if d_user_id:
|
|
headers["d-user-id"] = d_user_id
|
|
org_symbol = context_value(context, "DCLOUD_ORG_SYMBOL", "ORG_SYMBOL")
|
|
if org_symbol:
|
|
headers["org-symbol"] = org_symbol
|
|
sourceapp = context_value(context, "DCLOUD_SOURCEAPP", "DCLOUD_SOURCE_APP", "SOURCEAPP", "SOURCE_APP")
|
|
if sourceapp:
|
|
headers["sourceapp"] = sourceapp
|
|
started = time.time()
|
|
req = request.Request(url, data=body, method=method, headers=headers)
|
|
try:
|
|
with request.urlopen(req, timeout=timeout) as response:
|
|
raw = response.read()
|
|
status = int(response.status)
|
|
except error.HTTPError as exc:
|
|
raw = exc.read()
|
|
status = int(exc.code)
|
|
redact_values = {
|
|
token,
|
|
cookie,
|
|
d_user_id,
|
|
context_value(context, "DCLOUD_FAT_USERNAME", "DCLOUD_USERNAME"),
|
|
context_value(context, "DCLOUD_FAT_PASSWORD", "DCLOUD_PASSWORD"),
|
|
context_value(context, "DCLOUD_API_TOKEN"),
|
|
context_value(context, "DCLOUD_FAT_AUTH_TOKEN"),
|
|
}
|
|
summary = response_summary(status, raw, time.time() - started, redact_values)
|
|
return {
|
|
"slug": slug,
|
|
"method": method,
|
|
"endpoint": endpoint,
|
|
"sourceRepo": str(external_api.get("sourceRepo", "")),
|
|
"fatRoute": str(external_api.get("fatRoute", "")),
|
|
"request": {
|
|
"hasBody": body is not None,
|
|
"headerNames": sorted(headers),
|
|
},
|
|
"response": summary,
|
|
"status": "passed" if summary["accepted"] else "failed",
|
|
}
|
|
|
|
|
|
def build_context_summary(context: dict[str, str], token: str) -> tuple[dict[str, Any], list[str]]:
|
|
missing: list[str] = []
|
|
base_url = context_value(context, "DCLOUD_FAT_BASE_URL", "DCLOUD_BASE_URL")
|
|
cookie = context_value(context, "DCLOUD_FAT_COOKIE", "DCLOUD_COOKIE")
|
|
username = context_value(context, "DCLOUD_FAT_USERNAME", "DCLOUD_USERNAME")
|
|
password = context_value(context, "DCLOUD_FAT_PASSWORD", "DCLOUD_PASSWORD")
|
|
d_user_id = context_value(context, "DCLOUD_D_USER_ID", "D_USER_ID")
|
|
org_symbol = context_value(context, "DCLOUD_ORG_SYMBOL", "ORG_SYMBOL")
|
|
sourceapp = context_value(context, "DCLOUD_SOURCEAPP", "DCLOUD_SOURCE_APP", "SOURCEAPP", "SOURCE_APP")
|
|
if not base_url:
|
|
missing.append("DCLOUD_FAT_BASE_URL")
|
|
if not (token or cookie or d_user_id or (username and password)):
|
|
missing.append("DCLOUD_FAT_AUTH_TOKEN/DCLOUD_API_TOKEN, DCLOUD_FAT_COOKIE/DCLOUD_COOKIE, DCLOUD_D_USER_ID/D_USER_ID, or username/password")
|
|
if (username or password) and not (username and password):
|
|
missing.append("both DCLOUD_FAT_USERNAME and DCLOUD_FAT_PASSWORD")
|
|
token_error = jwt_token_error(token) if token else ""
|
|
if token_error:
|
|
missing.append(token_error)
|
|
if not org_symbol:
|
|
missing.append("DCLOUD_ORG_SYMBOL")
|
|
if not sourceapp:
|
|
missing.append("DCLOUD_SOURCEAPP")
|
|
return (
|
|
{
|
|
"baseUrlConfigured": bool(base_url),
|
|
"tokenConfigured": bool(token),
|
|
"cookieConfigured": bool(cookie),
|
|
"usernamePasswordConfigured": bool(username and password),
|
|
"dUserIdConfigured": bool(d_user_id),
|
|
"orgSymbolConfigured": bool(org_symbol),
|
|
"sourceappConfigured": bool(sourceapp),
|
|
},
|
|
missing,
|
|
)
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(description="Probe D-Cloud FAT credentials without printing secret values.")
|
|
parser.add_argument("--from-kubernetes", action="store_true", help="read credentials from a Kubernetes Secret")
|
|
parser.add_argument("--namespace", default=os.getenv("K8S_NAMESPACE") or DEFAULT_NAMESPACE)
|
|
parser.add_argument("--secret-name", default=os.getenv("POC_ACCEPTANCE_DCLOUD_SECRET_NAME") or DEFAULT_SECRET_NAME)
|
|
parser.add_argument("--timeout-seconds", type=float, default=float(os.getenv("POC_ACCEPTANCE_DCLOUD_TIMEOUT_SECONDS", "30")))
|
|
parser.add_argument("--json", action="store_true", help="emit JSON output")
|
|
args = parser.parse_args()
|
|
|
|
try:
|
|
context = load_kubernetes_secret(args.namespace, args.secret_name) if args.from_kubernetes else env_context()
|
|
base_url_raw = context_value(context, "DCLOUD_FAT_BASE_URL", "DCLOUD_BASE_URL")
|
|
token = context_value(context, "DCLOUD_FAT_AUTH_TOKEN", "DCLOUD_API_TOKEN")
|
|
summary, missing = build_context_summary(context, token)
|
|
if missing:
|
|
payload = {
|
|
"status": "blocked",
|
|
"source": "kubernetes-secret" if args.from_kubernetes else "environment",
|
|
"namespace": args.namespace if args.from_kubernetes else "",
|
|
"secretName": args.secret_name if args.from_kubernetes else "",
|
|
"context": summary,
|
|
"missing": missing,
|
|
"probes": [],
|
|
}
|
|
print(json.dumps(payload, ensure_ascii=False, indent=2, sort_keys=True))
|
|
return 1
|
|
base_url = normalize_base_url(base_url_raw)
|
|
token = token or signin_token(context, base_url, args.timeout_seconds)
|
|
summary, missing = build_context_summary(context, token)
|
|
if missing:
|
|
payload = {
|
|
"status": "blocked",
|
|
"source": "kubernetes-secret" if args.from_kubernetes else "environment",
|
|
"namespace": args.namespace if args.from_kubernetes else "",
|
|
"secretName": args.secret_name if args.from_kubernetes else "",
|
|
"context": summary,
|
|
"missing": missing,
|
|
"probes": [],
|
|
}
|
|
print(json.dumps(payload, ensure_ascii=False, indent=2, sort_keys=True))
|
|
return 1
|
|
probes = [
|
|
probe_operator("dcloud_embodied_annotation", context, base_url, token, args.timeout_seconds),
|
|
probe_operator("dcloud_data_generation", context, base_url, token, args.timeout_seconds),
|
|
]
|
|
status = "passed" if all(item["status"] == "passed" for item in probes) else "failed"
|
|
payload = {
|
|
"status": status,
|
|
"source": "kubernetes-secret" if args.from_kubernetes else "environment",
|
|
"namespace": args.namespace if args.from_kubernetes else "",
|
|
"secretName": args.secret_name if args.from_kubernetes else "",
|
|
"context": summary,
|
|
"missing": [],
|
|
"probes": probes,
|
|
}
|
|
print(json.dumps(payload, ensure_ascii=False, indent=2 if args.json else None, sort_keys=True))
|
|
return 0 if status == "passed" else 1
|
|
except Exception as exc:
|
|
blocked = args.from_kubernetes and "missing or unreadable" in str(exc)
|
|
payload = {
|
|
"status": "blocked" if blocked else "failed",
|
|
"source": "kubernetes-secret" if args.from_kubernetes else "environment",
|
|
"namespace": args.namespace if args.from_kubernetes else "",
|
|
"secretName": args.secret_name if args.from_kubernetes else "",
|
|
"error": str(exc),
|
|
}
|
|
print(json.dumps(payload, ensure_ascii=False, indent=2, sort_keys=True))
|
|
return 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|