Skip to content

Keep the saved login and the federation token cache in one store - #331

Open
rgarcia wants to merge 8 commits into
mainfrom
hypeship/cli-shared-secret-store
Open

rgarcia wants to merge 8 commits into
mainfrom
hypeship/cli-shared-secret-store

Conversation

@rgarcia

@rgarcia rgarcia commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Follows #330, which is merged.

What changed

The saved login and the workload identity token cache each had their own copy of "keychain first, an owner-only file when the keychain refuses, remove the other copy". They now share one type, secretStore (pkg/auth/secret_store.go), with read, save and per-copy delete.

  • storage.go: the saved login uses the store. The helpers that read and deleted the credentials file are gone; the one that wrote it is now writeCredentialsFile.
  • federation_cache.go: the token cache uses the store. Its own keychain handling is gone.
  • Which copy wins when both exist stays with each caller: the newer saved_at for the login, the later expiry for a token.
  • How the file is written also stays with each caller (secretStore.writeFile): the saved login is written in place, the token cache through a temporary file and a rename. Both are what main does today.

What changes for a CLI user

Nothing. Paths, keychain entries, file modes, the refresh lock, how each file is written and the rule for which copy is current are unchanged, including for files saved before saved_at existed.

An earlier version of this branch also wrote the credentials file through a temporary file and a rename. That is removed. A replacement file takes what its directory gives a new file rather than what the old file had, so the rename:

  • dropped an access control list set on the credentials file on Windows, and on macOS could give the new file an inherited one that grants read despite mode 0600;
  • replaced a symbolic link instead of writing through it, leaving the link's target with the old login (reproduced on Linux);
  • failed with device or resource busy when the credentials file is bind-mounted into a container (reproduced on Linux).

It bought little in return: every CLI read, save and refresh of the login already holds the credentials lock, so no CLI process reads a half-written file.

One error message differs in a case that needs the home directory lookup to start failing between two calls in the same function: a config directory error while loading the login is prefixed with failed to read tokens from file:. The no stored credentials found error for a missing login is unchanged.

Tests added

  • TestCredentialsFileOnDisk: the credentials file is at ~/.config/kernel/credentials, mode 0600 in a 0700 directory, written in place, with no other file left beside it and the lock.
  • TestSavedLoginKeepsItsKeychainEntry: the keychain entry is kernel-cli / oauth-tokens, by value.
  • TestLoadTokensWithoutALogin: the exact error a missing login produces.
  • TestDeleteTokens: both copies are removed, nothing in the keychain is not an error, and any other keychain error is returned.
  • TestGitHubActionsJobLeavesTheKeychainAlone: a GitHub Actions job neither uses nor removes a keychain entry of the same name.

Each was checked to fail when the behavior it covers is broken, by changing the source and re-running the package tests.

Verification

Run on the head commit, linux/amd64, Go 1.27.4:

  • make build
  • make test (go vet ./... and go test ./...)
  • go test -race -count=2 ./pkg/auth/
  • GOOS=windows go build ./... and GOOS=windows go vet ./pkg/auth/
  • GOOS=darwin GOARCH=arm64 go build ./... and GOOS=darwin GOARCH=arm64 go vet ./pkg/auth/

Not verified: a real OS keychain (the tests use the mock), and nothing was run on Windows or macOS beyond cross-compiling. The Windows and macOS access control points above come from the platforms' documented behavior and the Go standard library source, not from a run on those systems.

Commands now authenticate with KERNEL_API_KEY, then with the federation
rule in KERNEL_FEDERATION_RULE_ID, then with the saved login. A failed
federation exchange fails the command instead of falling back to the
saved login.

The access token is cached between CLI processes where it can be tied to
the workload that obtained it, and the AWS identity token source is
linked in.
Bind a token from a file source to the identity token that was exchanged
for it, hold a cached token to the 60 second margin when it is sent, make
the retry after a 401 final, and keep the token off requests to other
hosts.
After a 401, send a request again only when net/http itself would
replay it, so a vault write without a body is sent once as well.
Base automatically changed from hypeship/cli-workload-identity-auth to main November 29, 2026 12:58
…ecret-store

# Conflicts:
#	pkg/auth/federation_cache.go
#	pkg/auth/federation_test.go
A replacement file takes what its directory gives a new file rather than
what the old file had: an access control list set on the credentials
file is lost on Windows and macOS, a symbolic link is replaced instead
of followed, and a file mounted from elsewhere cannot be renamed over.
The credentials lock already keeps CLI processes from reading a file
that is being written, so the saved login goes back to being written in
place, as it was before this branch. Each store now names how its file
is written; the token cache keeps its temporary file and rename.

Add tests for the saved login's path, modes and keychain entry, the
error a missing login produces, DeleteTokens, and a GitHub Actions job
leaving the keychain alone.
@rgarcia
rgarcia requested a review from masnwilliams November 29, 2026 13:28
@rgarcia

rgarcia commented Nov 29, 2026

Copy link
Copy Markdown
Contributor Author

Picked masnwilliams to review: recent commits in auth; 10 pending

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.

1 participant