Store tokens against the target -T names - #32
Merged
Conversation
SetToken reaches only the current target, so a command told to act on another one with -T had to move the current target aside and put it back, and Write persists whatever it finds in between.
logout cleared the token of the current target and then reported that it had logged out of the one -T named, so it left that Vault logged in and signed the user out of another. A logout with no target selected at all reported success too, having dropped the error that says so.
auth reached the target -T named by making it current, setting the token, and switching back, which is what SetTokenFor now does without the detour. It also dropped the error from setting the token, so a target that had gone missing looked like a success.
init put the root token of the Vault it had just initialized onto the current target, leaving the initialized one with no token at all, and read its Strongbox flag from the current target too.
This was referenced Jul 29, 2026
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #31, which adds the helper this uses. Merge #31 first, then
retarget this to
developbefore merging it —gh pr edit 32 --base develop. A stacked PR merged while its base still names a branch that hasalready merged lands on that branch instead of on
develop; that is how thedocumentation in #29 was lost, and #30 puts it back.
Why
Config.SetTokenwrites to the current target, because the current targetis the only one it knows about. Every command that stores a token calls it,
and
-Tnames a different Vault without moving the current target. So thetoken goes to the wrong entry in
~/.saferc.logoutis the one that costs something. Two targets, both with tokens:It reports success against the Vault it did not log out of, leaves that
token live in the file, and signs the user out of a Vault they did not name.
Someone rotating a credential and then walking away has neither of the two
things they think they have.
inithas the same shape without the second half:safe -T beta initstores the root token of the Vault it just initialized against the current
target, leaving the initialized one with no token at all, and reads its
Strongbox flag from the current target too.
authwas already correct. It reached the right target by making itcurrent, setting the token, and switching back — a dance that only works
because
Writehappens to come after the restore. It now says what itmeans.
The change
rc.ConfiggainsSetTokenFor(alias, token): store a token against a namedtarget without making it current.
SetTokenbecomes a call to it with thecurrent target, so the existing behaviour and its "No target selected"
error are unchanged.
logout,auth, andinitname the target they mean. None of them movesthe current target, which matters because
Writepersists it, and-Tissupposed to last exactly one command.
Two errors that were being dropped now surface: logging out with nothing
targeted said "Successfully logged out of " and returned 0, and a token
stored against a target that had gone missing looked like a success.
Tests
pkg/rc:SetTokenFornames its target, leaves the current one and itstoken alone, and rejects both an unknown alias and an empty one.
internal/cli/token_target_test.go: logout clears the-Ttarget's tokenand only that one, with the current target unmoved; logout with nothing
targeted is an error; auth stores the token on the
-Ttarget (this onepassed before the change and guards the refactor); init stores the root
token on the
-Ttarget.Verified live as well —
safe -T beta logoutagainst a Vault pair nowclears beta's token and leaves alpha's:
make checkandgo test -race ./...green.