Skip to content

Store tokens against the target -T names - #32

Merged
wayneeseguin merged 4 commits into
target-flag-selects-vaultfrom
token-follows-target
Jul 29, 2026
Merged

Store tokens against the target -T names#32
wayneeseguin merged 4 commits into
target-flag-selects-vaultfrom
token-follows-target

Conversation

@wayneeseguin

Copy link
Copy Markdown
Contributor

Stacked on #31, which adds the helper this uses. Merge #31 first, then
retarget this to develop before merging itgh pr edit 32 --base develop. A stacked PR merged while its base still names a branch that has
already merged lands on that branch instead of on develop; that is how the
documentation in #29 was lost, and #30 puts it back.

Why

Config.SetToken writes to the current target, because the current target
is the only one it knows about. Every command that stores a token calls it,
and -T names a different Vault without moving the current target. So the
token goes to the wrong entry in ~/.saferc.

logout is the one that costs something. Two targets, both with tokens:

$ safe -T beta logout
Successfully logged out of beta

  alpha:  token: ""            # <- wiped, and nobody asked
  beta:   token: token-beta    # <- still logged in

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.

init has the same shape without the second half: safe -T beta init
stores 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.

auth was already correct. It reached the right target by making it
current, setting the token, and switching back — a dance that only works
because Write happens to come after the restore. It now says what it
means.

The change

rc.Config gains SetTokenFor(alias, token): store a token against a named
target without making it current. SetToken becomes a call to it with the
current target, so the existing behaviour and its "No target selected"
error are unchanged.

logout, auth, and init name the target they mean. None of them moves
the current target, which matters because Write persists it, and -T is
supposed 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: SetTokenFor names its target, leaves the current one and its
token alone, and rejects both an unknown alias and an empty one.

internal/cli/token_target_test.go: logout clears the -T target's token
and only that one, with the current target unmoved; logout with nothing
targeted is an error; auth stores the token on the -T target (this one
passed before the change and guards the refactor); init stores the root
token on the -T target.

Verified live as well — safe -T beta logout against a Vault pair now
clears beta's token and leaves alpha's:

  alpha:  token: token-alpha
  beta:   token: ""

make check and go test -race ./... green.

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.
@wayneeseguin
wayneeseguin merged commit fcd7666 into target-flag-selects-vault Jul 29, 2026
@wayneeseguin

Copy link
Copy Markdown
Contributor Author

The merge landed on target-flag-selects-vault, not develop — that base branch had already merged via #31, so these commits never reached develop. Re-opened against develop as #34 with the commits cherry-picked unchanged.

@wayneeseguin
wayneeseguin deleted the token-follows-target branch July 29, 2026 16:39
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