Skip to content

fix(auth): never cache ambient provider credentials in the keyring - #1

Closed
thejrose1984 wants to merge 5 commits into
mainfrom
claude/github-issue-2695-4d2p62
Closed

thejrose1984 wants to merge 5 commits into
mainfrom
claude/github-issue-2695-4d2p62

Conversation

@thejrose1984

Copy link
Copy Markdown
Owner

The gcp/adc provider is documented as stateless: it re-resolves the active
principal from the environment (GOOGLE_APPLICATION_CREDENTIALS, the gcloud
config, or the metadata server) on every call and mints a short-lived token
from it. The auth manager, however, persisted that token to the system keyring
and replayed it on the next login, so after switching accounts with
gcloud auth application-default login Atmos kept authenticating as the
previous principal until the user ran
atmos auth logout --provider gcp-adc --keychain --force.

Introduce types.AmbientProvider, an optional interface a provider implements to
declare that it resolves credentials from ambient environment state. The
manager now treats chains rooted at such a provider as non-cacheable:

  • findFirstValidCachedCredentials returns -1, forcing re-resolution from the
    provider on every authentication.
  • authenticateWithProvider, authenticateIdentityChain, and buildWhoamiInfo skip
    the keyring write, and purge any pre-existing entry so installations that
    already have a poisoned keyring self-heal without the workaround.
  • Logout, LogoutProvider, and LogoutAll clear those entries whether or not
    --keychain was passed; there is nothing durable to preserve.

Both gcp/adc and gcp/workload-identity-federation opt in — WIF has the same
shape, exchanging an OIDC token read from the environment on every call. The
process-level in-memory cache still applies, so a single Atmos command performs
at most one resolution. Providers that do not implement the interface are
cached exactly as before.

Also guard resolveProviderForIdentity against a nil config, which the new
buildWhoamiInfo call path can reach.

Fixes cloudposse#2695

Co-Authored-By: Claude Opus 5 noreply@anthropic.com
Claude-Session: https://claude.ai/code/session_018E2ZZsjCoEsJVF8mXgJwoN

claude added 5 commits July 25, 2026 16:47
The gcp/adc provider is documented as stateless: it re-resolves the active
principal from the environment (GOOGLE_APPLICATION_CREDENTIALS, the gcloud
config, or the metadata server) on every call and mints a short-lived token
from it. The auth manager, however, persisted that token to the system keyring
and replayed it on the next login, so after switching accounts with
`gcloud auth application-default login` Atmos kept authenticating as the
previous principal until the user ran
`atmos auth logout --provider gcp-adc --keychain --force`.

Introduce types.AmbientProvider, an optional interface a provider implements to
declare that it resolves credentials from ambient environment state. The
manager now treats chains rooted at such a provider as non-cacheable:

- findFirstValidCachedCredentials returns -1, forcing re-resolution from the
  provider on every authentication.
- authenticateWithProvider, authenticateIdentityChain, and buildWhoamiInfo skip
  the keyring write, and purge any pre-existing entry so installations that
  already have a poisoned keyring self-heal without the workaround.
- Logout, LogoutProvider, and LogoutAll clear those entries whether or not
  --keychain was passed; there is nothing durable to preserve.

Both gcp/adc and gcp/workload-identity-federation opt in — WIF has the same
shape, exchanging an OIDC token read from the environment on every call. The
process-level in-memory cache still applies, so a single Atmos command performs
at most one resolution. Providers that do not implement the interface are
cached exactly as before.

Also guard resolveProviderForIdentity against a nil config, which the new
buildWhoamiInfo call path can reach.

Fixes cloudposse#2695

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018E2ZZsjCoEsJVF8mXgJwoN
Follow-up from code review of the ambient-provider fix. No behavior change;
these close paths the first round left unverified.

- Add a realm-aware credential store double. The shared testStore keys entries
  by alias alone, so it could not distinguish a realm-scoped entry from the
  legacy pre-realm one — the exact distinction the self-healing purge depends
  on. The legacy purge was previously untested.
- Cover purgeCachedCredentials: realm + legacy deletion, the empty-realm case
  that must skip the redundant second delete, and the nil-store guard.
- Cover chainRootIsAmbient's guards (empty chain, chain root that is not a
  registered provider, as happens for standalone identities).
- Cover LogoutAll, which decides per provider, and add the missing non-ambient
  counterpart for LogoutProvider so every ambient fallback has a negative-path
  test as the repo conventions require.
- Cover a multi-hop `via` chain for identityChainRootIsAmbient and a 3-step
  ambient chain whose intermediate credentials must also be purged.
- Cover resolveProviderForIdentity's new nil-config guard directly.
- Move exhaustive ProviderIsAmbient coverage beside the code in
  pkg/auth/types, leaving pkg/auth with the one assertion that matters there:
  the shared testProvider double must stay non-ambient, or that package's
  non-ambient assertions are vacuous.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018E2ZZsjCoEsJVF8mXgJwoN
A provider audit for the gcp/adc fix found three more providers with the
identical bug. Each re-resolves its principal from ambient state on every
Authenticate, has a no-op Logout, and manages no credential files — yet the
auth manager persisted their short-lived tokens to the keyring and replayed
them:

- azure/cli shells out to `az account get-access-token`, so it reproduces
  issue cloudposse#2695 verbatim: `az login` as a different account, and Atmos keeps
  authenticating as the previous one.
- azure/oidc re-reads its federated token from token_file_path,
  AZURE_FEDERATED_TOKEN_FILE, or the GitHub Actions endpoint on every call.
- github/oidc requests a fresh token from the Actions token endpoint on every
  call. Its own Logout comment documented the anti-pattern outright
  ("Credentials are only stored in keyring") — corrected here.

All three now implement types.AmbientProvider, so the existing manager logic
covers them with no further changes.

Deliberately NOT marked, verified rather than assumed:

- atmos/pro mints a SINGLE-USE token that cannot be re-exchanged, so cached
  reuse is load-bearing for it (see the EnsureIdentityEnvironment contract in
  pkg/auth/types/interfaces.go). Marking it ambient would break authentication.
  Added TestProProvider_IsNotAmbient as a regression guard, since it is
  superficially similar to the ambient providers — short-lived token, no
  credential files. What makes a provider ambient is that it CAN re-resolve,
  not that its token is short-lived.
- aws/sso, aws/saml, and azure/device-code run interactive flows backed by
  their own purpose-built caches. The azure test asserts device-code stays
  non-ambient alongside its two ambient siblings.

Also generalizes the manager comments that enumerated the ambient providers by
name, so they do not go stale as more providers opt in.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018E2ZZsjCoEsJVF8mXgJwoN
Regression from the ambient-provider fix. Logout forces keyring deletion for
ambient chains inside the manager, but the dry-run preview runs in the cmd
layer and only sees the user's --keychain flag. Without --keychain it therefore
listed no keyring entries while the real run removed them — dry-run's one job
is an accurate preview.

The preview now asks whether the provider is ambient, via a new optional
interface types.AmbientProviderReporter plus a types.ManagerReportsAmbient
helper that falls back to false for implementations lacking it.

Deliberately NOT added to types.AuthManager. That interface is already ~35
methods with a generated mock and six hand-written doubles across the repo;
adding a method for a display concern churned all of them for no behavioral
gain (tried it first — it cascaded through five files before I stopped).
Optional interfaces are the established pattern here: types.AmbientProvider,
types.StandaloneIdentity, types.Provisioner, types.ConsoleAccessProvider.
The final change touches five files and neither the interface nor the mock.

Also fixes a double GetProviders() call the first attempt introduced in the
--all preview, by fetching once and sharing it with ambientProviderNames.

Tests use a decorator over the generated mock rather than a hand-written one,
with a compile-time assertion that it really satisfies the reporter — without
that, a rename would silently turn the assertions into no-ops, since the
helper falls back to false for types that do not implement it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018E2ZZsjCoEsJVF8mXgJwoN
…r fix

Ran the repo's actual lint gate for the first time in this branch. It needed a
custom-gcl binary built with the go1.26.4 toolchain (the stock one is built
with go1.25 and refuses the repo's go.mod target; the repo also wires in a
`lintroller` plugin, so the stock binary cannot load the config at all).

Diffing findings against the base commit showed six regressions from the
earlier commits, now fixed:

- godot x3: sentences opening with a lowercase identifier.
- revive add-constant x3: new log-key string literals crossing the
  four-occurrence threshold. Added logKeyChain / logKeyIdentityStep /
  logKeyRealm / logKeyName, placed in manager_chain.go rather than the shared
  block in manager.go so that file stays untouched by this branch.
- revive function-length in Logout and buildWhoamiInfo: extracted
  deleteIdentityKeyringEntries and cacheWhoamiCredentials. The former also
  removes a nestif finding.
- revive cyclomatic in performLogoutAll, from the ambient branch added to the
  --all preview: extracted printLogoutAllDryRun and
  printLogoutAllKeyringPreview.

Net effect on the package: 243 findings at the base commit, 239 now. The four
remaining deltas are all severity: warning revive/nestif findings on functions
this branch does not modify (NewAuthManager, (*manager).Authenticate,
executeAuthLogoutCommand), each paired with an equivalent function-length
warning that disappeared from the same function — golangci-lint's uniq-by-line
keeps one issue per line, so which of two co-located revive rules surfaces
shifts as the global issue set changes. The nestif entry is the
deleteKeychain -> deleteProviderKeychain rename, net zero.

No behavior change; all auth and cmd/auth tests pass.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018E2ZZsjCoEsJVF8mXgJwoN
@github-actions

Copy link
Copy Markdown

Warning

This PR is blocked from merging because a required semver label is missing.

major, minor, patch, no-release

You'll need to add one before this PR can be merged.

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

Labels

Projects

None yet

2 participants