121 lines
5.4 KiB
Python
121 lines
5.4 KiB
Python
# #region Tooling.FullFlowClient [C:3] [TYPE Module] [SEMANTICS test,http,evidence,redaction]
|
|
# @BRIEF Persist real local HTTP outcomes with credential redaction and phase-scoped status.
|
|
import json
|
|
import os
|
|
import time
|
|
from urllib.parse import urlsplit
|
|
|
|
import httpx
|
|
|
|
from common import Blocked, artifact_path
|
|
|
|
|
|
# #region Tooling.FullFlowVerification.Redact [C:1] [TYPE Function]
|
|
# @BRIEF Remove credentials recursively before writing request or response evidence.
|
|
def redact(value, secrets=()):
|
|
if isinstance(value, dict):
|
|
return {k: ("[redacted]" if k.lower() == "pat" or any(s in k.lower() for s in
|
|
("password", "token", "secret", "authorization")) else redact(v, secrets))
|
|
for k, v in value.items()}
|
|
if isinstance(value, list):
|
|
return [redact(v, secrets) for v in value]
|
|
if isinstance(value, str):
|
|
for secret in secrets:
|
|
if secret:
|
|
value = value.replace(secret, "[redacted]")
|
|
return value
|
|
# #endregion Tooling.FullFlowVerification.Redact
|
|
|
|
|
|
# #region Tooling.FullFlowClient.HttpDriver [C:3] [TYPE Class]
|
|
# @BRIEF Keep the bounded verifier state and actual flow operations.
|
|
class HttpDriver:
|
|
# #region Tooling.FullFlowClient.init [C:3] [TYPE Function]
|
|
# @BRIEF Initialize the local client and private credential redaction set.
|
|
def __init__(self, config, output):
|
|
self.config, self.output = config, artifact_path(output)
|
|
self.output.mkdir(parents=True, exist_ok=True)
|
|
base = config.get("backend_url", "http://127.0.0.1:18103")
|
|
if urlsplit(base).hostname not in {"127.0.0.1", "localhost"}:
|
|
raise Blocked("Only isolated localhost backend is allowed")
|
|
self.password = os.environ["FIXTURE_PASSWORD"]
|
|
self.git_token = os.environ.get("PUBLISHED_CATALOG_GITEA_TOKEN", os.environ.get("GITEA_TOKEN", ""))
|
|
self.secrets = [self.password, self.git_token]
|
|
self.client = httpx.Client(base_url=base, timeout=180, trust_env=False)
|
|
self.steps = []
|
|
self.ids = {}
|
|
self.attempt = str(time.time_ns())
|
|
self.phase = "all"
|
|
|
|
# #endregion Tooling.FullFlowClient.init
|
|
|
|
# #region Tooling.FullFlowClient.record [C:3] [TYPE Function]
|
|
# @BRIEF Retain one actual verification step and its sanitized facts.
|
|
def record(self, name, status, **facts):
|
|
self.steps.append(redact({"attempt": self.attempt, "phase": self.phase,
|
|
"step": name, "status": status, **facts}, self.secrets))
|
|
self.flush()
|
|
print(f"{status}: {name}", flush=True)
|
|
|
|
# #endregion Tooling.FullFlowClient.record
|
|
|
|
# #region Tooling.FullFlowClient.flush [C:3] [TYPE Function]
|
|
# @BRIEF Persist phase-scoped outcomes and real object identities.
|
|
def flush(self):
|
|
self.output.joinpath("report.json").write_text(json.dumps({
|
|
"overall": "NO-GO", "steps": self.steps, "ids": self.ids,
|
|
"latest_phase": self.phase,
|
|
"latest_phase_result": "BLOCKED" if any(s["status"] in {"BLOCKED", "NEEDS_REVIEW"}
|
|
for s in self.steps if s.get("attempt") == self.attempt) else "PASS",
|
|
"claim_scope": "Local Docker integration evidence; not production acceptance",
|
|
}, indent=2, ensure_ascii=False) + "\n")
|
|
|
|
# #endregion Tooling.FullFlowClient.flush
|
|
|
|
# #region Tooling.FullFlowClient.call [C:3] [TYPE Function]
|
|
# @BRIEF Execute one HTTP boundary and reject an unexpected response.
|
|
def call(self, name, method, path, payload=None, expected=(200,), headers=None, form=None):
|
|
started = time.monotonic()
|
|
try:
|
|
response = self.client.request(method, path, json=payload if form is None else None,
|
|
data=form, headers=headers)
|
|
try:
|
|
body = response.json()
|
|
except ValueError:
|
|
body = {"text": response.text[:4000]}
|
|
except httpx.HTTPError as exc:
|
|
self.record(name, "BLOCKED", error=str(exc))
|
|
raise Blocked(name) from exc
|
|
self.record(name, "PASS" if response.status_code in expected else "BLOCKED",
|
|
method=method, path=path, request=payload,
|
|
http_status=response.status_code, response=body,
|
|
elapsed_seconds=round(time.monotonic()-started, 3))
|
|
if response.status_code not in expected:
|
|
raise Blocked(f"{name}: HTTP {response.status_code}")
|
|
return body
|
|
|
|
# #endregion Tooling.FullFlowClient.call
|
|
|
|
# #region Tooling.FullFlowClient.require [C:3] [TYPE Function]
|
|
# @BRIEF Record a falsifiable assertion and stop dependent operations on failure.
|
|
def require(self, name, condition, **facts):
|
|
self.record(name, "PASS" if condition else "BLOCKED", **facts)
|
|
if not condition:
|
|
raise Blocked(name)
|
|
|
|
# #endregion Tooling.FullFlowClient.require
|
|
|
|
# #region Tooling.FullFlowClient.login [C:3] [TYPE Function]
|
|
# @BRIEF Authenticate the isolated test human and redact the issued access token.
|
|
def login(self):
|
|
result = self.call("login", "POST", "/api/auth/login", form={
|
|
"username": self.config.get("username", "admin"), "password": self.password})
|
|
self.secrets.append(result["access_token"])
|
|
self.client.headers["Authorization"] = "Bearer " + result["access_token"]
|
|
|
|
# #endregion Tooling.FullFlowClient.login
|
|
|
|
# #endregion Tooling.FullFlowClient.HttpDriver
|
|
|
|
# #endregion Tooling.FullFlowClient
|