feat(F556): ROCKETCHAT_SKIP_ADAPTER via kanonischem get_scoped_secret @implements: [F-556]

This commit is contained in:
Bernd (Hermes)
2026-09-26 11:31:27 +02:00
parent b96ac54770
commit 7aba317f04
3 changed files with 68 additions and 54 deletions
+49 -35
View File
@@ -56,21 +56,26 @@ class _FakeConfig:
self.extra = extra or {}
def _fake_secret_scope(return_value):
"""Installiere ein Fake-``agent.secret_scope``-Modul in sys.modules.
def _fake_scoped_secret(return_value, importable=True):
"""Installiere ein Fake-``gateway.platforms._shared``-Modul in sys.modules.
Das Plugin importiert ``from agent.secret_scope import get_secret`` innerhalb
von ``_skip_adapter_flag``. ``agent`` existiert im Test-venv nicht — wir
injizieren ein Fake-Modul, damit der ECHTE Import-Pfad des Plugins läuft
(und nicht ein gemockter Aufruf, der am fehlenden Modul scheitert).
Das Plugin importiert ``from gateway.platforms._shared import get_scoped_secret``
innerhalb von ``_skip_adapter_flag``. Im Test-venv existiert ``gateway`` nicht.
Wir injizieren ein Fake-Modul, damit der ECHTE Import-Pfad des Plugins läuft.
``importable=False`` lässt das Modul weg → das Plugin fällt auf os.environ zurück
(der single-profile/unit-test-Fallback).
"""
if not importable:
return {}
import types
fake = types.ModuleType("agent.secret_scope")
fake.get_secret = lambda name, default=None: return_value
parent = types.ModuleType("agent")
parent.secret_scope = fake
return {"agent": parent, "agent.secret_scope": fake}
fake = types.ModuleType("gateway.platforms._shared")
fake.get_scoped_secret = lambda name, default=None: return_value
plats = types.ModuleType("gateway.platforms")
plats._shared = fake
gw = types.ModuleType("gateway")
gw.platforms = plats
return {"gateway": gw, "gateway.platforms": plats, "gateway.platforms._shared": fake}
# ═══════════════════════════════════════════════════════════════════════════
@@ -96,58 +101,67 @@ class TestSkipAdapterFlag:
"""Kein Flag in irgendeiner Quelle → kein Skip."""
cfg = _FakeConfig()
os.environ.pop("ROCKETCHAT_SKIP_ADAPTER", None)
with patch.dict(sys.modules, _fake_secret_scope(None)):
with patch.dict(sys.modules, _fake_scoped_secret(None)):
assert _skip_adapter_flag(cfg) is False
def test_secret_scope_true_skips(self):
"""Satellit: Flag im secret_scope (.env) → Skip."""
cfg = _FakeConfig()
os.environ.pop("ROCKETCHAT_SKIP_ADAPTER", None)
with patch.dict(sys.modules, _fake_secret_scope("true")):
with patch.dict(sys.modules, _fake_scoped_secret("true")):
assert _skip_adapter_flag(cfg) is True
def test_secret_scope_false_does_not_skip(self):
"""Satellit: Flag=false im secret_scope → kein Skip (explizit deaktiviert)."""
cfg = _FakeConfig()
os.environ.pop("ROCKETCHAT_SKIP_ADAPTER", None)
with patch.dict(sys.modules, _fake_secret_scope("false")):
with patch.dict(sys.modules, _fake_scoped_secret("false")):
assert _skip_adapter_flag(cfg) is False
def test_secret_scope_missing_falls_through_to_env(self):
"""secret_scope liefert None → Fallthrough auf os.environ."""
cfg = _FakeConfig()
with patch.dict(sys.modules, _fake_secret_scope(None)):
with patch.dict(os.environ, {"ROCKETCHAT_SKIP_ADAPTER": "true"}):
assert _skip_adapter_flag(cfg) is True
def test_scoped_miss_fails_closed_does_not_borrow_env(self):
"""Scoped miss (get_scoped_secret → None) leiht NICHT os.environ aus (fail-closed).
def test_env_true_skips(self):
"""Default/Prozess: Flag im os.environ → Skip."""
Das ist der kanonische Multiplex-Vertrag: ein Satellit ohne eigenes Flag
darf NICHT das Flag eines anderen Profils aus os.environ übernehmen.
Das Plugin liest ausschließlich get_scoped_secret; ein None-Ergebnis
bedeutet „kein Flag“ → kein Skip (Adapter startet).
"""
cfg = _FakeConfig()
with patch.dict(sys.modules, _fake_secret_scope(None)):
with patch.dict(os.environ, {"ROCKETCHAT_SKIP_ADAPTER": "1"}):
assert _skip_adapter_flag(cfg) is True
with patch.dict(sys.modules, _fake_scoped_secret(None)):
with patch.dict(os.environ, {"ROCKETCHAT_SKIP_ADAPTER": "true"}):
assert _skip_adapter_flag(cfg) is False
def test_scoped_true_skips_default_profile(self):
"""get_scoped_secret liefert '1' (Default-Profil/scoped) → Skip."""
cfg = _FakeConfig()
with patch.dict(sys.modules, _fake_scoped_secret("1")):
assert _skip_adapter_flag(cfg) is True
def test_config_extra_true_skips(self):
"""config.yaml: extra['skip_adapter'] → Skip."""
cfg = _FakeConfig(extra={"skip_adapter": "yes"})
os.environ.pop("ROCKETCHAT_SKIP_ADAPTER", None)
with patch.dict(sys.modules, _fake_secret_scope(None)):
with patch.dict(sys.modules, _fake_scoped_secret(None)):
assert _skip_adapter_flag(cfg) is True
def test_secret_scope_takes_priority_over_env(self):
"""secret_scope=false gewinnt über os.environ=true."""
cfg = _FakeConfig()
with patch.dict(sys.modules, _fake_secret_scope("false")):
with patch.dict(sys.modules, _fake_scoped_secret("false")):
with patch.dict(os.environ, {"ROCKETCHAT_SKIP_ADAPTER": "true"}):
assert _skip_adapter_flag(cfg) is False
def test_secret_scope_import_error_degrades_to_env(self):
"""agent.secret_scope nicht importierbar → graceful Fallthrough auf env."""
def test_scoped_secret_import_error_degrades_to_env(self):
"""gateway.platforms._shared nicht importierbar → Fallback auf os.environ (single-profile/Unit-Test)."""
cfg = _FakeConfig()
# Kein Fake-Modul installiert → der try/except-Import im Plugin schlägt fehl
# und der Code fällt auf os.environ zurück.
with patch.dict(os.environ, {"ROCKETCHAT_SKIP_ADAPTER": "true"}):
assert _skip_adapter_flag(cfg) is True
# und der Code fällt auf os.environ zurück (single-profile-Pfad).
with patch.dict(sys.modules, {}, clear=False):
sys.modules.pop("gateway", None)
sys.modules.pop("gateway.platforms", None)
sys.modules.pop("gateway.platforms._shared", None)
with patch.dict(os.environ, {"ROCKETCHAT_SKIP_ADAPTER": "true"}):
assert _skip_adapter_flag(cfg) is True
# ═══════════════════════════════════════════════════════════════════════════
@@ -159,21 +173,21 @@ class TestValidateConfig:
"""Selbst mit gültigen Credentials liefert validate_config False bei Flag."""
cfg = _FakeConfig(extra={"base_url": "https://rc", "user": "b", "password": "p"})
os.environ.pop("ROCKETCHAT_SKIP_ADAPTER", None)
with patch.dict(sys.modules, _fake_secret_scope("true")):
with patch.dict(sys.modules, _fake_scoped_secret("true")):
assert validate_config(cfg) is False
def test_no_flag_with_credentials_is_true(self):
"""Kein Flag + gültige Credentials → True (Adapter darf starten)."""
cfg = _FakeConfig(extra={"base_url": "https://rc", "user": "b", "password": "p"})
os.environ.pop("ROCKETCHAT_SKIP_ADAPTER", None)
with patch.dict(sys.modules, _fake_secret_scope(None)):
with patch.dict(sys.modules, _fake_scoped_secret(None)):
assert validate_config(cfg) is True
def test_no_flag_without_credentials_is_false(self):
"""Kein Flag + fehlende Credentials → False (unverändertes Verhalten)."""
cfg = _FakeConfig()
os.environ.pop("ROCKETCHAT_SKIP_ADAPTER", None)
with patch.dict(sys.modules, _fake_secret_scope(None)):
with patch.dict(sys.modules, _fake_scoped_secret(None)):
os.environ.pop("ROCKETCHAT_BASE_URL", None)
os.environ.pop("ROCKETCHAT_USER", None)
os.environ.pop("ROCKETCHAT_PASSWORD", None)