Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid invoking token helper on login #23209

Closed
wants to merge 2 commits into from

Conversation

ruuda
Copy link

@ruuda ruuda commented Sep 21, 2023

This fixes #23194.

Problem

vault login used to call the token helper in get mode, because it constructs an HTTP client, and the client automatically loads the token. For almost everything that is the right thing to do, but for login, that is the thing that is supposed to retrieve the token, and login itself does not require the token. In fact, vault login would erase the token on the client later on.

Calling the token helper in get mode is a problem, because if the token helper fails, that blocks the login. But the token helper might fail because it doesn't have a token yet.

The call to Get originates here, in the construction of the http client:

token, err = helper.Get()

That is being called from here:

client, err := c.Client()

Solution

Split Client into two parts: a ClientWithoutToken that does most of what Client did previously, except for setting a token. And Client that also sets the token. This does change when the token gets set on the client for calls to Client, but as far as I can tell, the remainder of the former Client, now ClientWithToken method does not rely on the token being set.

For all existing code except login, Client still behaves the way it did and nothing changes. For login in particular, we can now call ClientWithoutToken. This ensures that the token helper does not get called when the http client is initialized. The token helper still gets called to store the token later.

Open questions, testing

There is still this snippet that happens after constructing the client:

vault/command/login.go

Lines 221 to 225 in 6ef2a60

// Evolving token formats across Vault versions have caused issues during CLI logins. Unless
// token auth is being used, omit any token picked up from TokenHelper.
if authMethod != "token" {
client.SetToken("")
}

I think this is now useless, the client will not have a token anyway so we don’t need to reset it. But it makes me wonder — if authMethod == "token", does my change break anything?

Also, I am not sure what the best way is to test this, if somebody can point me in the right direction, I would appreciate.

`vault login` used to call the token helper in `get` mode, because it
constructs an HTTP client, and the client automatically loads the token.
For almost everything that is the right thing to do, but for login, that
is the thing that is supposed to retrieve the token, and login itself
does not require the token. In fact, `vault login` would erase the token
on the client later on.

Calling the token helper in `get` mode is a problem, because if the
token helper fails, that blocks the login. But the token helper might
fail because it doesn't have a token yet.

This fixes #23194.
@hashicorp-cla
Copy link

hashicorp-cla commented Sep 21, 2023

CLA assistant check
All committers have signed the CLA.

@divyaac
Copy link
Contributor

divyaac commented Apr 18, 2024

HI @ruuda, thank you so much for this PR! In fact, we actually require the client to set the token (and therefore pull the token value using the token helper). The reason is that in case the authentication type is "token" (https://github.com/hashicorp/vault/blob/main/command/login.go#L170), we need the token value to be set.

@divyaac divyaac closed this Apr 18, 2024
@ruuda ruuda deleted the fix-token-helper branch April 27, 2024 11:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core/cli core/client needs-eng-review Community PR waiting for ENG review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

vault login calls the token helper with get and expects that to succeed
5 participants