Skip to content

feat: add enclave secrets to manage API keys per service - #250

Open
kocher wants to merge 2 commits into
eclipse-enclave:mainfrom
kocher:feat/secrets-command
Open

kocher wants to merge 2 commits into
eclipse-enclave:mainfrom
kocher:feat/secrets-command

Conversation

@kocher

@kocher kocher commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

enclave secrets set|unset|list <key> writes the layered secrets files by service (anthropic, openai, gemini, mistral, copilot-github), so a key reaches every tool that declares it; --tool narrows it to one tool's file and --project to the current project. A new shared project layer, projects/.env, sits beside the per-tool directory so a tool named "global" cannot collide with it. Secret resolution moves to auth.ResolveSecret so list reports the source sessions actually use.

What it does

Adds enclave secrets set|unset|list <key>, so you can manage API keys by
service (anthropic, openai, gemini, mistral, copilot-github) instead
of editing the layered secrets files by hand. A key reaches every tool that
declares it, and only those.

  • set writes to the shared global.env by default. --tool writes one
    tool's file instead, and --project writes the current project's variant of
    either. The value comes from a no-echo prompt or from stdin, so it never
    shows up in argv or shell history. set also removes the key's other
    aliases from the file it writes, so one file never holds two disagreeing
    values. It then warns about anything that would stop a session from using
    the new value:
    • a different value from the shell, a higher layer, or a file source read
      first
    • the tool's secrets_scope
    • a key prefix that gateway protection would refuse under restricted
      networking
  • unset removes the key, plus saved copies of the removed value in each
    affected tool's env store. It skips projects whose own secrets files would
    restore that value anyway.
  • list resolves each key the way a session in this project would. It shows
    the masked value, where it comes from, and a note when a session would still
    not use it (no_api_key, ephemeral without pass_api_key, or a prefix
    gateway protection would refuse).
  • New shared project layer secrets/projects/<hash>.env, read by every tool in
    one project. It sits between global/<tool>.env and
    projects/<hash>/<tool>.env. --secrets-scope project and both include it.
  • Secret resolution moves from runtime into auth.ResolveSecret, so list
    and sessions share one implementation.
  • Edits are locked by the resolved target file, written atomically, and
    follow symlinks, so a secrets file kept in a dotfiles repository stays
    linked. Variables that another declared credential also uses are left alone,
    with a warning. Shared files are not edited while a tool extension fails to
    load.

Docs: docs/auth.md (new "Managing API keys" section, updated layer table),
docs/cli-reference.md, docs/ARCHITECTURE.md, gitlab-cli README.

How to test

make build
./enclave secrets list
echo sk-ant-test | ./enclave secrets set anthropic
./enclave secrets list anthropic        # source: global, masked value
./enclave secrets set openai --tool codex --project   # prompts without echo
./enclave secrets list --tool codex     # source: project/codex
ANTHROPIC_API_KEY=other ./enclave secrets set anthropic <<< sk-ant-x   # warns: shell wins
./enclave secrets unset anthropic       # also clears saved copies of that value

Unit tests cover target selection, claimed-variable handling, symlinked and
locked edits, env-store cleanup, and resolution precedence (internal/app,
internal/apikeys, internal/auth, internal/cli).

Follow-ups

  • The rules for when a key is withheld (no_api_key, ephemeral without
    pass_api_key) and when gateway protection refuses its prefix are in both
    internal/app/secrets_cmd.go and internal/runtime/auth_manager.go. Move
    them into one shared function so secrets list cannot drift from what
    sessions do.
  • enclave secrets reads and edits the persisted env store through
    hoststore directly, alongside the authManager's own store helpers. Share
    one env-store helper keyed by backend.EnvStoreKey.
  • The four-layer secrets layout is spelled out in auth.SecretsLayerPaths,
    resolveSecretsTarget, and secretsLayerLabel. Have SecretsLayer carry
    its label or kind so a new layer needs one change.

Changelog

- Add `enclave secrets set|unset|list` to manage API keys (anthropic, openai,
  gemini, mistral, copilot-github) per service; a key reaches every tool that
  uses it, or one tool with `--tool`, or only this project with `--project`.
- Add a shared per-project secrets file, `secrets/projects/<hash>.env`, read
  by every tool in that project.

Breaking changes

  • This PR introduces breaking changes and has been coordinated with maintainers.

Review checklist

`enclave secrets set|unset|list <key>` writes the layered secrets files
by service (anthropic, openai, gemini, mistral, copilot-github), so a key
reaches every tool that declares it; --tool narrows it to one tool's file
and --project to the current project. A new shared project layer,
projects/<hash>.env, sits beside the per-tool directory so a tool named
"global" cannot collide with it. Secret resolution moves to
auth.ResolveSecret so list reports the source sessions actually use.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tortmayr

tortmayr commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

@EclipseSourceAI

@EclipseSourceAI EclipseSourceAI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Autonomous AI review. May contain mistakes.

Useful comment? React 👍. Disagree? React 👎 to reject it for follow-up reviews. A reply explaining why helps too.

Agent approval means no blocking issues found. Unrelated and Follow-up comments are non-blocking. A maintainer must still sign off.

Only a maintainer can re-request a review from this agent.

Powered by Eclipse Enclave via review-guard-mcp

This PR adds enclave secrets set|unset|list <key>. It manages API keys by service (anthropic, openai, gemini, mistral, copilot-github) instead of by hand-edited .env files. internal/apikeys builds the key catalog from the tools' provider credentials and groups them by credential id. set and unset write one of four secrets layers, picked by --tool and --project. The file edits are locked, atomic and follow symlinks. Both commands skip variables that another declared credential also uses. unset also removes stored copies of the removed value from each affected tool's env store, in every project unless --project is given.

What a maintainer should focus on:

  • New session layer: secrets/projects/<hash>.env is a new layer that sessions read under --secrets-scope project|both, between global/<tool>.env and projects/<hash>/<tool>.env. This changes how sessions resolve secrets, not just the CLI.
  • Moved resolution logic: secret resolution moved from runtime/active_secrets.go into auth.ResolveSecret. The behavior is the same; drift is now returned and logged by the caller instead of inside the resolver. The existing runtime tests still run against the moved code.
  • Naming: the verb is secrets, but it only manages API keys. enclave secrets set GH_TOKEN fails with "unknown API key". The maintainers should confirm they're happy with that name before it becomes something users rely on.
  • Known duplication: the PR description already lists the remaining duplication (withheld/prefix rules, env-store helpers, layer labels) as follow-ups, so I haven't commented on it again.

I built the branch and ran go test for app, apikeys, auth, cli, config and runtime; all passed. I also ran secrets set and secrets list against a scratch home, and they behaved as documented. The inline comments are small reuse and docs nits; none of them blocks the merge.

Comment thread internal/auth/secret_resolve.go Resolved
Comment thread internal/config/profile.go Resolved
Comment thread internal/app/secrets_cmd.go Resolved
Comment thread docs/cli-reference.md Resolved
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tortmayr
tortmayr requested a review from xai December 1, 2026 17:09
@tortmayr

tortmayr commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

@xai Could you have a look this new proposed feature?

@xai

xai commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

I really like the feature! It provides good UX for something that is currently rather annoying for users. Great work, @kocher!

I'll do some testing and dig into this deeper and then come back with a review.

@fismif
fismif self-requested a review December 3, 2026 13:25

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants