Skip to content

refactor(security): one keyring seam owns the keyring and the Secret Service lock check, with a shared keyring fake and its contract suite #1318

Description

@dan-petty

Problem

  • The keyring is imported inside seven functions. config/settings.py imports keyring at :136, :733, :756, :777, :813, :832 and :845. Backend selection (_ensure_keyring_backend :134), reads, writes and presence checks each reach the library on their own, with an in-memory fallback for headless runs (_EPHEMERAL_CI_SECRETS :729).
  • Callers reach past it. commands/config.py imports keyring and asks it for the active backend (:530, :535), imports the private _keyring_has (:22, :533), and devops config auth-headless writes the private _EPHEMERAL_CI_SECRETS directly (:433, :443). k8s/credentials.py:17 and commands/k8s/stack_lifecycle.py:456 import the private _keyring_set. commands/devcontainer.py talks to D-Bus itself: it connects to the session-bus socket (_is_session_bus_running :1279, AF_UNIX at :1283) and asks Secret Service whether the default collection is locked (_keyring_is_locked :1392, secretstorage at :1396).
  • SecretProvider is not this seam. The Protocol at security/secrets.py:116 is the secret resolver's read-only source interface; its KeyringProvider (:129) sits on top of the settings functions above.
  • Tests patch the library or the privates. The census counts about 66 patch sites at this edge [measured: census of 2026-10-07]. mock_keyring patches keyring.get_password and keyring.set_password [code: tests/conftest.py:710]; tests/test_config_cmd.py names the private _ensure_keyring_backend 13 times, and tests/test_headless_auth.py imports _EPHEMERAL_CI_SECRETS, _keyring_get and _keyring_set [measured: grep at 5c45b1e]. The one real fake, FakeKeyring [code: tests/cluster_secret_fakes.py:37], is an in-memory KeyringBackend that can be locked, but tests install it by patching keyring.get_keyring [code: tests/test_k8s_push_secrets.py:83].

Acceptance criteria

Seam

  • One module owns the keyring: choosing a usable encrypted backend (today _ensure_keyring_backend) and naming it; reading, writing and checking entries; the headless in-memory fallback; and the Secret Service state the devcontainer reads (whether the session bus answers, whether the default collection is locked). A Protocol names these operations. The real implementation is the keyring library's selected backend, with secretstorage for the lock state.
  • The settings functions (keyring_read, keyring_write, get_keyring_secret, set_keyring_secret, require_persistent_keyring) and their private helpers delegate to the seam, and no module in config/ other than the seam imports keyring.
  • The two D-Bus checks move into the seam from commands/devcontainer.py:1279 and :1392, and devcontainer.py calls the seam; secretstorage and the session-bus socket are reached only there.
  • The headless in-memory store moves into the seam with a public write, which devops config auth-headless can call, and a public reset, which the fixture below calls at teardown.
  • The remaining direct users (commands/config.py:22, :433, :443, :530, :533; k8s/credentials.py:17; commands/k8s/stack_lifecycle.py:456) are left to their package items.

Fake and fixture

  • tests/support/fakes/keyring.py promotes FakeKeyring: entries in memory; a locked state that raises KeyringLocked as the Secret Service backend does; a no-backend state; the session-bus and lock answers; and a record of reads and writes. Its two importers import it from the new module.
  • A fixture installs it through the seam and returns it, registered once from the root conftest. It replaces mock_keyring, whose 2 users (tests/test_ai_benchmark.py:231, test_embedding_benchmark.py:363) request the new fixture instead.
  • If test(cli): the hermetic floor fails a test that reaches an edge without a fake and resets all process-wide state after every test #1310 has landed, its deny backend is installed through the seam's hook instead of keyring.set_keyring, and each test that installs FakeKeyring with keyring.set_keyring requests the fixture instead, unless its code still imports keyring itself; those go with the package item that routes that code.

Contract suite

  • One suite in tests/config/ runs each operation against the fake and against the real seam over gnome-keyring-daemon on a private session bus, started with dbus-run-session under tmp_path, never the developer's bus. Cases: write then read, a missing entry, a locked collection, no usable backend, the active backend's name and the session-bus check. It is a live test, skipped only where gnome-keyring-daemon or dbus-run-session is missing; both are installed in the devcontainer [measured: which].

Migration

Key questions

  • Is SecretProvider the seam? (proposed) No. It is the resolver's read-only view over several sources (keyring, environment, settings, Vault), while devops-cli also writes keyring entries, picks the backend and reads the lock state. The keyring seam sits beneath KeyringProvider, whose tests move with test(security): scanner, secret and crypto tests run on the executor, process, http and keyring fakes, and scanners stop looking for mocks #1332.
  • Why a live test for the real side? (proposed) gnome-keyring-daemon on a private bus under tmp_path is a real local tool that touches no real secret, and the floor already keeps tests off the developer's bus [code: tests/conftest.py:325]. The daemon starts once per worker, so each test stays under 1 s.
  • May a test call keyring.set_keyring()? (proposed) No: only the seam selects the backend. A test installs the fake through the fixture, never by patching keyring.get_keyring.
  • Fixture name? (proposed) keyring_store.

Strategic alignment

Update 2026-10-08: refinement

#709 added keyring_delete (config/settings.py:856-869, used by commands/vault.py:538); the seam owns delete, and the contract suite adds 'write, delete, read finds nothing' and 'deleting a missing entry returns False'. commands/ai.py:416 also imports _keyring_has.

Proposed design

Problem statement

The keyring library is imported in seven functions across multiple modules. Callers reach past the settings module to private names. Tests patch the library or private names directly. This makes the keyring hard to test and maintain. One module must own all keyring operations. A shared fake and a contract suite replace the current patch-based tests.

Acceptance criteria

  • Criterion 1: One seam module owns backend selection, read, write, delete, presence check, headless fallback, and Secret Service lock state.
    Verification: No module in config/ other than the seam imports keyring. A Protocol in the seam names every operation.
  • Criterion 2: Settings functions delegate to the seam.
    Verification: keyring_read, keyring_write, get_keyring_secret, set_keyring_secret, require_persistent_keyring, and keyring_delete call the seam. grep for 'import keyring' in config/ returns only the seam module.
  • Criterion 3: D-Bus checks move into the seam from commands/devcontainer.py.
    Verification: devcontainer.py calls the seam for session-bus and lock state. secretstorage and the session-bus socket appear only in the seam module.
  • Criterion 4: The headless in-memory store moves into the seam with a public write and a public reset.
    Verification: devops config auth-headless calls the seam public write. The fixture calls the seam public reset at teardown.
  • Criterion 5: Remaining direct users import from the seam, not from keyring or private names.
    Verification: commands/config.py, k8s/credentials.py, commands/k8s/stack_lifecycle.py, and commands/ai.py import from the seam. grep for '_keyring_set', '_keyring_get', '_keyring_has', '_EPHEMERAL_CI_SECRETS' in these files returns no results.
  • Criterion 6: tests/support/fakes/keyring.py holds the promoted FakeKeyring.
    Verification: The fake has in-memory entries, a locked state that raises KeyringLocked, a no-backend state, session-bus and lock answers, and a read/write record. Its two importers import from the new module.
  • Criterion 7: A fixture named keyring_store installs the fake through the seam.
    Verification: The fixture is registered once from the root conftest. tests/test_ai_benchmark.py and tests/test_embedding_benchmark.py request keyring_store instead of mock_keyring.
  • Criterion 8: A contract suite in tests/config/ runs each operation against the fake.
    Verification: Cases include write-then-read, missing entry, locked collection, no usable backend, active backend name, session-bus check, write-delete-read-finds-nothing, and deleting a missing entry returns False.
  • Criterion 9: The real side of the contract suite is opt-in or removed.
    Verification: The default test run starts no system program. The real side is either an opt-in suite for the devcontainer or absent. The choice is recorded in the change.
  • Criterion 10: tests/test_headless_auth.py drives the seam public operations.
    Verification: The file imports no private name. grep for '_EPHEMERAL_CI_SECRETS', '_keyring_get', '_keyring_set' in the file returns no results.
  • Criterion 11: Backend-selection tests in tests/test_config_cmd.py use the fake or the contract suite.
    Verification: The tests patch no keyring name and no devops_cli private name. The 6 lines in tests/test_devcontainer.py that patch _keyring_is_locked use the fake.
  • Criterion 12: All tests run offline and each test runs under 1 second.
    Verification: uv run pytest tests/ --timeout=1 passes. No test starts a system program.
  • Criterion 13: uv run devops ci passes.
    Verification: The CI pipeline completes with all checks green.

Key questions

  • Is SecretProvider the seam?: No. It is the resolver read-only view over several sources. The keyring seam sits beneath KeyringProvider. (proposed)
  • Why a live test for the real side?: The owner rule of 2026-10-24 states that tests start no system program and run the same in the devcontainer and on a stock GitHub runner. The real side is either an opt-in suite for the devcontainer or it goes. Record the choice when this item is built. (proposed)
  • May a test call keyring.set_keyring()?: No. Only the seam selects the backend. A test installs the fake through the fixture. (proposed)
  • Fixture name?: keyring_store. (proposed)

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    type/refactor
    scope/security
    scope/config
    priority/p2-medium
    on Oct 7, 2026
  2. 3 remaining items

  3. dan-petty-bot commented on Oct 8, 2026

    @dan-petty-bot
    Collaborator

    Intake placed this item to the backlog: at intake only a critical fix goes into a release; a release start pulls Ready items in, P0 features first.

    • Priority: P2-Medium. It improves testability and maintainability of a security-adjacent module but does not fix a defect or unblock a release, so it is important but not urgent.
    • Value: Medium. It reduces 66 test patch sites and eliminates private-helper coupling across five modules, making the keyring boundary easier to test and evolve, but it adds no new capability for end users.
    • Effort: High. It requires creating a new seam module, moving D-Bus and in-memory-store code, updating at least seven import sites, promoting FakeKeyring, adding a root-conftest fixture, and migrating roughly 66 test patch sites.
  4. dan-petty commented on Oct 24, 2026

    @dan-petty
    OwnerAuthor

    The owner's rule of 2026-10-24 replaces one criterion of this item: "tests are tests of my src python code" and start no system program, and every test runs the same in the devcontainer and on a stock GitHub runner with no package install. #2143 changes ADR 0007 and docs/TESTING.md to say so.

    So the real side of the keyring contract suite (gnome-keyring-daemon on a private session bus under dbus-run-session) cannot be part of the default test run. The fake side of the suite and the seam stay as written. The real side is either an opt-in suite for the devcontainer, outside the default run and outside CI, or it goes; record the choice when this item is built. The rest of this item is unchanged.

  5. dan-petty commented on Oct 26, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones on the board.

    • Priority: P2-Medium. This change improves code structure and testability but is not critical to core functionality.
    • Value: Medium. Improves developer experience and reduces complexity in secret management.
    • Effort: High. Requires refactoring multiple modules, changing how keyring is used throughout the codebase, and updating tests.
    • Related: fix(config): secrets report 'stored in keyring' when no keyring backend is available #454, which asks for a different change. Both items work on keyring functionality, but the candidate refactors the keyring seam while item #454 fixes a keyring backend availability issue.
  6. dan-petty commented on Oct 28, 2026

    @dan-petty
    OwnerAuthor

    Status set to Ready at 69636b6 (see proposed design in issue body).

  7. added
    status/ready
    value/medium
    effort/high
    on Oct 28, 2026
  8. added 2 commits that reference this issue on Oct 28, 2026
    866d2cb
    002d645
  9. removed
    status/ready
    on Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/highpriority/p2-mediumscope/configscope/securitytype/refactorvalue/medium

    Type

    No type

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions