from __future__ import annotations import time from collections import defaultdict from typing import Any from yuxi.utils.logging_config import logger class SecretContract: _MAX_AUDIT_PER_KEY = 128 def __init__(self) -> None: self._secrets: dict[str, str] = {} self._audit_log: dict[str, list[dict[str, Any]]] = defaultdict(list) def register(self, label: str, description: str) -> None: self._secrets[label] = description logger.info("[BlueBubbles] Secret registered: %s (%s)", label, description) def audit(self, label: str, action: str, context: dict[str, Any] | None = None) -> None: if label not in self._secrets: logger.warning("[BlueBubbles] Secret audit for unregistered key: %s", label) return entry = { "label": label, "action": action, "timestamp": time.time(), "context": context or {}, } log_entries = self._audit_log[label] log_entries.append(entry) if len(log_entries) > self._MAX_AUDIT_PER_KEY: self._audit_log[label] = log_entries[-self._MAX_AUDIT_PER_KEY :] logger.debug("[BlueBubbles] Secret audit: %s -> %s", label, action) def list_secrets(self) -> list[dict[str, str]]: return [{"label": k, "description": v} for k, v in self._secrets.items()] def get_audit_log(self, label: str) -> list[dict[str, Any]]: return list(self._audit_log.get(label, [])) def clear(self) -> None: self._secrets.clear() self._audit_log.clear()