feat: bind evidence credentials through local files

This commit is contained in:
2026-08-09 19:03:36 +02:00
parent c7a369f436
commit 7126b567b0
11 changed files with 839 additions and 18 deletions
+17
View File
@@ -384,3 +384,20 @@ evidence:
legacy_source = build_evidence_sources(load_config(legacy))[0]
assert isinstance(legacy_source, FilesystemEvidenceSource)
assert legacy_source.root == (tmp_path / "curated").resolve()
def test_safe_validation_formatter_keeps_location_and_type_without_rejected_input(tmp_path):
workspace = tmp_path / "workspace.yaml"
workspace.write_text("""
dwh:
type: postgres_direct
connection: {database: analytics, schema: public, user: reader}
""")
with pytest.raises(ConfigError) as caught:
load_config(workspace)
message = str(caught.value)
assert "dwh.postgres_direct.connection.password" in message
assert "missing" in message
@@ -0,0 +1,280 @@
import json
import pytest
import yaml
from pydantic import SecretStr
from typer.testing import CliRunner
from tht.adapters.evidence import HttpManifestEvidenceSource
from tht.adapters.factory import build_evidence_sources
from tht.cli import app
from tht.config import ConfigError, load_config
SIGNED_CANARY = "SIGNED-CANARY-QUERY"
ACCESS_CANARY = "ACCESS-CANARY"
SECRET_CANARY = "SECRET-CANARY"
TOKEN_CANARY = "TOKEN-CANARY"
ALL_CANARIES = (SIGNED_CANARY, ACCESS_CANARY, SECRET_CANARY, TOKEN_CANARY)
def raw_runtime(source, *, vector=None):
value = {
"dwh": {
"type": "postgres_direct",
"connection": {
"database": "analytics", "schema": "public", "user": "reader",
"password": "not-a-canary",
},
},
"evidence": {"sources": [source]},
}
if vector is not None:
value["vector"] = vector
return value
def write_config(tmp_path, source, *, vector=None):
path = tmp_path / "runtime.yaml"
path.write_text(yaml.safe_dump(raw_runtime(source, vector=vector)))
return path
def assert_no_canaries(value):
text = str(value)
for canary in ALL_CANARIES:
assert canary not in text
def test_filesystem_config_does_not_touch_a_declared_source_root(tmp_path):
missing = tmp_path / "deliberately-missing"
cfg = load_config(write_config(tmp_path, {"type": "filesystem", "root": str(missing)}))
assert cfg.evidence.sources[0].root == missing
assert cfg.evidence.sources[0].patterns == ["**/*.md"]
assert cfg.evidence.sources[0].max_bytes == 10 * 1024 * 1024
assert not missing.exists()
def test_public_http_urls_are_secret_typed_without_adapter_construction(tmp_path):
cfg = load_config(write_config(tmp_path, {
"type": "http", "urls": ["https://evidence.example.test/guide.md"],
}))
source = cfg.evidence.sources[0]
assert isinstance(source.urls[0], SecretStr)
assert source.transport_urls() == ["https://evidence.example.test/guide.md"]
assert source.connect_timeout == 5
assert source.read_timeout == 30
assert source.max_bytes == 10 * 1024 * 1024
assert source.max_redirects == 5
assert source.allow_private_hosts is False
assert source.max_cache_bytes == 64 * 1024 * 1024
def test_signed_http_file_resolves_in_memory_and_preserves_provenance_order(tmp_path):
signed = [
f"https://evidence.example.test/guide.md?token={SIGNED_CANARY}",
"https://evidence.example.test/runbook.md?signature=second",
]
secret_file = tmp_path / "signed-urls.json"
secret_file.write_text(json.dumps(signed))
cfg = load_config(write_config(tmp_path, {
"type": "http",
"provenance_urls": [
"https://evidence.example.test/guide.md",
"https://evidence.example.test/runbook.md",
],
"signed_urls_file": str(secret_file),
"connect_timeout": 7,
"read_timeout": 41,
"max_bytes": 1234,
"max_redirects": 2,
"allow_private_hosts": True,
"max_cache_bytes": 5678,
}))
source = cfg.evidence.sources[0]
assert all(isinstance(url, SecretStr) for url in source.urls)
assert source.transport_urls() == signed
assert source.provenance_urls == [
"https://evidence.example.test/guide.md",
"https://evidence.example.test/runbook.md",
]
assert (source.connect_timeout, source.read_timeout) == (7, 41)
assert (source.max_bytes, source.max_redirects, source.max_cache_bytes) == (1234, 2, 5678)
assert source.allow_private_hosts is True
assert "signed_urls_file" not in repr(source)
assert_no_canaries(repr(cfg))
assert_no_canaries(cfg.model_dump_json())
adapter = build_evidence_sources(cfg)[0]
assert isinstance(adapter, HttpManifestEvidenceSource)
assert_no_canaries(repr(adapter))
@pytest.mark.parametrize("contents", [
"{malformed", json.dumps({"url": "https://evidence.example.test/guide.md"}),
json.dumps([]), json.dumps(["https://evidence.example.test/guide.md", 3]),
])
def test_signed_http_rejects_malformed_non_list_empty_or_non_string_files(tmp_path, contents):
secret_file = tmp_path / "signed-urls.json"
secret_file.write_text(contents)
path = write_config(tmp_path, {
"type": "http",
"provenance_urls": ["https://evidence.example.test/guide.md"],
"signed_urls_file": str(secret_file),
})
with pytest.raises(ConfigError) as caught:
load_config(path)
assert "signed URL file" in str(caught.value)
assert_no_canaries(caught.value)
def test_signed_http_rejects_missing_and_oversized_files_without_disclosure(tmp_path):
missing = tmp_path / "missing.json"
path = write_config(tmp_path, {
"type": "http",
"provenance_urls": ["https://evidence.example.test/guide.md"],
"signed_urls_file": str(missing),
})
with pytest.raises(ConfigError, match="signed URL file"):
load_config(path)
oversized = tmp_path / "oversized.json"
oversized.write_bytes(b"x" * (1024 * 1024 + 1))
path = write_config(tmp_path, {
"type": "http",
"provenance_urls": ["https://evidence.example.test/guide.md"],
"signed_urls_file": str(oversized),
})
with pytest.raises(ConfigError, match="signed URL file") as caught:
load_config(path)
assert_no_canaries(caught.value)
@pytest.mark.parametrize("provenance,signed", [
(
["https://evidence.example.test/a.md", "https://evidence.example.test/b.md"],
["https://evidence.example.test/b.md?sig=1", "https://evidence.example.test/a.md?sig=2"],
),
(["https://evidence.example.test/a.md"], [
"https://evidence.example.test/a.md?sig=1", "https://evidence.example.test/b.md?sig=2",
]),
(["https://evidence.example.test/a.md"], ["https://evidence.example.test/b.md"]),
(["https://evidence.example.test/a.md"], [f"https://user:{SIGNED_CANARY}@evidence.example.test/a.md"]),
(
["https://evidence.example.test/a.md", "https://evidence.example.test/a.md"],
["https://evidence.example.test/a.md?sig=1", "https://evidence.example.test/a.md?sig=2"],
),
])
def test_signed_http_rejects_reordered_extra_mismatch_userinfo_and_duplicate_provenance(
tmp_path, provenance, signed,
):
secret_file = tmp_path / "signed-urls.json"
secret_file.write_text(json.dumps(signed))
path = write_config(tmp_path, {
"type": "http", "provenance_urls": provenance, "signed_urls_file": str(secret_file),
})
with pytest.raises(ConfigError) as caught:
load_config(path)
assert "evidence.sources.0" in str(caught.value)
assert_no_canaries(caught.value)
def test_s3_ambient_and_static_file_credentials_are_secret_typed(tmp_path):
ambient = load_config(write_config(tmp_path, {
"type": "s3", "bucket": "clinical-evidence", "prefix": "published/",
})).evidence.sources[0]
assert ambient.access_key is None
assert ambient.secret_key is None
assert ambient.session_token is None
assert ambient.max_bytes == 10 * 1024 * 1024
assert ambient.max_objects == 10_000
assert ambient.max_pages == 100
assert ambient.page_size == 1000
files = {}
for name, canary in [
("access_key", ACCESS_CANARY), ("secret_key", SECRET_CANARY),
("session_token", TOKEN_CANARY),
]:
path = tmp_path / name
path.write_text(canary)
files[f"{name}_file"] = str(path)
cfg = load_config(write_config(tmp_path, {
"type": "s3", "bucket": "clinical-evidence", "prefix": "published/",
**files,
"endpoint_url": "https://s3.example.test",
"region": "eu-west-1",
"trusted_endpoint": True,
"allow_private_endpoint": True,
"allow_insecure_endpoint": False,
"max_bytes": 222,
"max_objects": 33,
"max_pages": 4,
"page_size": 5,
}))
source = cfg.evidence.sources[0]
assert all(isinstance(value, SecretStr) for value in (
source.access_key, source.secret_key, source.session_token,
))
assert (source.max_bytes, source.max_objects, source.max_pages, source.page_size) == (222, 33, 4, 5)
assert_no_canaries(repr(cfg))
assert_no_canaries(cfg.model_dump_json())
def test_evidence_policy_defaults_non_defaults_and_unknown_keys(tmp_path):
default = load_config(write_config(tmp_path, {
"type": "filesystem", "root": str(tmp_path / "missing"),
}))
assert default.vector.max_chunk_chars == 4000
assert default.vector.retain_published_generations == 3
explicit = load_config(write_config(tmp_path, {
"type": "filesystem", "root": str(tmp_path / "missing"),
"patterns": ["docs/*.md"], "max_bytes": 99,
}, vector={"max_chunk_chars": 123, "retain_published_generations": 7}))
assert explicit.evidence.sources[0].patterns == ["docs/*.md"]
assert explicit.vector.max_chunk_chars == 123
assert explicit.vector.retain_published_generations == 7
for mutation in [
{"type": "filesystem", "root": str(tmp_path), "unknown": SIGNED_CANARY},
{"type": "http", "urls": ["https://evidence.example.test/a"], "unknown": SIGNED_CANARY},
{"type": "s3", "bucket": "bucket-name", "unknown": SIGNED_CANARY},
]:
with pytest.raises(ConfigError) as caught:
load_config(write_config(tmp_path, mutation))
assert "extra_forbidden" in str(caught.value)
assert_no_canaries(caught.value)
def test_validation_repr_cli_and_exception_output_never_disclose_transport_secrets(tmp_path):
secret_file = tmp_path / "signed-urls.json"
secret_file.write_text(json.dumps([
f"https://evidence.example.test/other.md?token={SIGNED_CANARY}",
]))
path = write_config(tmp_path, {
"type": "http",
"provenance_urls": ["https://evidence.example.test/guide.md"],
"signed_urls_file": str(secret_file),
})
with pytest.raises(ConfigError) as caught:
load_config(path)
assert_no_canaries(caught.value)
valid_file = tmp_path / "valid-signed-urls.json"
valid_file.write_text(json.dumps([
f"https://evidence.example.test/guide.md?token={SIGNED_CANARY}",
]))
valid = write_config(tmp_path, {
"type": "http",
"provenance_urls": ["https://evidence.example.test/guide.md"],
"signed_urls_file": str(valid_file),
})
result = CliRunner().invoke(app, ["config", "check", "--config", str(valid)])
assert result.exit_code == 0
assert_no_canaries(result.stdout)
assert_no_canaries(result.stderr)
+1 -1
View File
@@ -64,7 +64,7 @@ def build_evidence_sources(cfg: Config):
case "http":
sources.append(
HttpManifestEvidenceSource(
[url.get_secret_value() for url in resource.urls],
resource.transport_urls(),
connect_timeout=resource.connect_timeout,
read_timeout=resource.read_timeout,
max_bytes=resource.max_bytes,
+128 -5
View File
@@ -1,5 +1,7 @@
import json
import os
import re
import stat
import warnings
from ipaddress import ip_address
from pathlib import Path
@@ -10,6 +12,7 @@ import yaml
from pydantic import BaseModel, Field, PrivateAttr, SecretStr, ValidationError, model_validator
from tht.config_compat import translate_legacy_config
from tht.ports.evidence import canonical_provenance_uri
_ENV_RE = re.compile(r"\$\{([A-Za-z_][A-Za-z0-9_]*)\}")
@@ -43,6 +46,51 @@ def _expand_env(value: Any) -> Any:
return value
_MAX_SIGNED_URL_FILE_BYTES = 1024 * 1024
def _resolve_http_signed_url_files(value: Any) -> Any:
"""Resolve only signed HTTP URL arrays, keeping their values out of public errors."""
if isinstance(value, dict):
resolved = {
key: _resolve_http_signed_url_files(item)
for key, item in value.items()
}
if resolved.get("type") != "http" or "signed_urls_file" not in resolved:
return resolved
if "urls" in resolved:
raise ConfigError("HTTP signed URL file cannot be combined with urls")
path_value = resolved.pop("signed_urls_file")
if not isinstance(path_value, str):
raise ConfigError("Invalid signed URL file reference")
path = Path(path_value)
try:
entry = path.lstat()
target = path.stat()
if stat.S_ISLNK(entry.st_mode) or not stat.S_ISREG(target.st_mode):
raise OSError
if target.st_size > _MAX_SIGNED_URL_FILE_BYTES:
raise OSError
with path.open("rb") as stream:
payload = stream.read(_MAX_SIGNED_URL_FILE_BYTES + 1)
if len(payload) > _MAX_SIGNED_URL_FILE_BYTES:
raise OSError
parsed = json.loads(payload.decode("utf-8"))
except (OSError, UnicodeError, json.JSONDecodeError) as exc:
raise ConfigError("Cannot read signed URL file") from exc
if (
not isinstance(parsed, list)
or not parsed
or any(not isinstance(item, str) or not item for item in parsed)
):
raise ConfigError("Invalid signed URL file")
resolved["urls"] = parsed
return resolved
if isinstance(value, list):
return [_resolve_http_signed_url_files(item) for item in value]
return value
def _resolve_secret_files(value: Any) -> Any:
if isinstance(value, dict):
resolved = {key: _resolve_secret_files(item) for key, item in value.items()}
@@ -228,12 +276,15 @@ class FilesystemEvidenceSourceConfig(BaseModel):
patterns: list[str] = ["**/*.md"]
max_bytes: int = Field(default=10 * 1024 * 1024, gt=0)
model_config = {"extra": "forbid"}
class HttpEvidenceSourceConfig(BaseModel):
type: Literal["http"]
# Manifest URLs may contain signed query parameters. Treat the complete transport URL as
# secret-bearing configuration; adapters derive a query-free provenance URI from it.
# Transport URLs are secret-bearing. Signed-file configurations retain only public,
# query-free provenance identities alongside the masked transport values.
urls: list[SecretStr] = Field(min_length=1)
provenance_urls: list[str] | None = Field(default=None, min_length=1)
connect_timeout: float = Field(default=5, gt=0)
read_timeout: float = Field(default=30, gt=0)
max_bytes: int = Field(default=10 * 1024 * 1024, gt=0)
@@ -241,6 +292,38 @@ class HttpEvidenceSourceConfig(BaseModel):
allow_private_hosts: bool = False
max_cache_bytes: int = Field(default=64 * 1024 * 1024, gt=0)
model_config = {"extra": "forbid"}
@model_validator(mode="after")
def validate_provenance_mapping(self):
transport_urls = [url.get_secret_value() for url in self.urls]
try:
canonical = [canonical_provenance_uri(url) for url in transport_urls]
except ValueError as exc:
raise ValueError("HTTP transport URL is invalid") from exc
if any(
urlparse(url).scheme not in ("http", "https") or not urlparse(url).hostname
for url in transport_urls
):
raise ValueError("HTTP transport URL must use http or https")
if self.provenance_urls is None:
return self
try:
provenance = [canonical_provenance_uri(url) for url in self.provenance_urls]
except ValueError as exc:
raise ValueError("HTTP provenance URL is invalid") from exc
if provenance != self.provenance_urls:
raise ValueError("HTTP provenance URLs must be canonical query-free identities")
if len(set(provenance)) != len(provenance):
raise ValueError("HTTP provenance URLs must not repeat")
if len(canonical) != len(provenance) or canonical != provenance:
raise ValueError("Signed HTTP URLs must map one-to-one to provenance URLs in order")
return self
def transport_urls(self) -> list[str]:
"""Expose secret transport values only at the adapter-construction boundary."""
return [url.get_secret_value() for url in self.urls]
class S3EvidenceSourceConfig(BaseModel):
type: Literal["s3"]
@@ -259,6 +342,16 @@ class S3EvidenceSourceConfig(BaseModel):
max_pages: int = Field(default=100, gt=0)
page_size: int = Field(default=1000, gt=0, le=1000)
model_config = {"extra": "forbid"}
@model_validator(mode="after")
def validate_static_credentials(self):
if (self.access_key is None) != (self.secret_key is None):
raise ValueError("S3 access_key and secret_key must be configured together")
if self.session_token is not None and self.access_key is None:
raise ValueError("S3 session_token requires static credentials")
return self
EvidenceSourceConfig = Annotated[
FilesystemEvidenceSourceConfig | HttpEvidenceSourceConfig | S3EvidenceSourceConfig,
@@ -282,6 +375,8 @@ class EvidenceSourcesConfig(BaseModel):
raise ValueError("evidence requires source_root or sources")
return self
model_config = {"extra": "forbid"}
class EmbeddingsConfig(BaseModel):
provider: str = "ollama_internal"
@@ -298,10 +393,12 @@ class EmbeddingsConfig(BaseModel):
class VectorConfig(BaseModel):
max_chunk_chars: int = 4000
max_chunk_chars: int = Field(default=4000, gt=0)
# ACTIVE plus the two most recent rollback generations by default.
retain_published_generations: int = Field(default=3, ge=1)
model_config = {"extra": "forbid"}
class SearchConfig(BaseModel):
rrf_k: int = 60
@@ -392,6 +489,31 @@ def workspace_id_for_config(config: Config, path: Path) -> str:
return workspace_id_from_path(path)
def _format_validation_error(error: ValidationError) -> str:
messages = {
"missing": "required field",
"extra_forbidden": "unknown field",
"greater_than": "value must be greater than the configured bound",
"greater_than_equal": "value must meet the configured lower bound",
"less_than_equal": "value exceeds the configured upper bound",
"literal_error": "unsupported literal value",
"union_tag_invalid": "unsupported discriminator",
"union_tag_not_found": "missing discriminator",
"string_too_short": "string is too short",
"too_short": "collection is too short",
"value_error": "configuration value is invalid",
}
lines = []
for issue in error.errors(include_input=False, include_url=False):
location = ".".join(str(part) for part in issue.get("loc", ())) or "configuration"
error_type = str(issue.get("type", "validation_error"))
message = messages.get(error_type, "invalid configuration value")
if location == "runtime_identity" and error_type == "value_error":
message = "source_identity does not match workspace identity"
lines.append(f"{location} [{error_type}]: {message}")
return "\n".join(lines)
def load_config(path: Path) -> Config:
if not path.exists():
raise ConfigError(f"File di configurazione non trovato: {path}")
@@ -401,7 +523,7 @@ def load_config(path: Path) -> Config:
raise ConfigError(f"Configurazione YAML non valida: {path}") from exc
if not isinstance(raw, dict):
raise ConfigError(f"Configurazione non valida (atteso un mapping YAML): {path}")
expanded = _resolve_secret_files(_expand_env(raw))
expanded = _resolve_secret_files(_resolve_http_signed_url_files(_expand_env(raw)))
_validate_internal_embedding_contract(expanded, path)
_validate_internal_vector_contract(expanded, path)
translated, used_legacy = translate_legacy_config(expanded)
@@ -409,7 +531,8 @@ def load_config(path: Path) -> Config:
try:
cfg = Config.model_validate(translated)
except ValidationError as e:
raise ConfigError(f"Configurazione non valida in {path}:\n{e}") from e
details = _format_validation_error(e)
raise ConfigError(f"Configurazione non valida in {path}:\n{details}") from e
env_profile = os.environ.get("THT_PROFILE")
if env_profile is not None:
if env_profile not in ("server", "workstation"):