Skip to content

login: Allow non-temporary credentials from the environment #886

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

Conversation

christophetd
Copy link
Contributor

  • f887580 fixed a bug introduced in 67d1aea that causes the AWS console to show "Invalid credentials" when running aws-vault login and only non-temporary credentials are available

  • 5798fba allows to use aws-vault login when credentials are in the environment, no matter their type

    • if temporary credentials, the behavior is unchanged
    • otherwise, sts:GetFederationToken is used to retrieve temporary credentials, then generate a sign-in link

This has the nice consequence of being able to run aws-vault login at any time, no matter which type of credentials is available in your current shell

@christophetd christophetd changed the title Login with environment variables login: Allow non-temporary credentials from the environment Mar 9, 2022
@christophetd christophetd force-pushed the login-with-environment-variables branch 2 times, most recently from e20d1e5 to 4786527 Compare March 9, 2022 08:01
@mtibben
Copy link
Member

mtibben commented Mar 10, 2022

f887580 fixed a bug #864 (comment) in 67d1aea that causes the AWS console to show "Invalid credentials" when running aws-vault login and only non-temporary credentials are available

This sounds like the correct behaviour to me. aws-vault tries to be predictable. If the credentials in the environment aren't able to be used, then isn't that a user error that should display an error message? "Magic" behaviour I typically try to stay away from

@mtibben
Copy link
Member

mtibben commented Mar 10, 2022

(but open to being convinced differently)

@christophetd
Copy link
Contributor Author

christophetd commented Mar 22, 2022

I see it as: aws-vault loginbehavior should be the same, no matter where the credentials are coming from. The previous behavior for keychain credentials was:

  • If long-lived IAM credentials, call sts:GetFederationToken first and use the subsequent STS creds
  • If already STS creds, do nothing
  • Then, generate a sign-in link

So I see no reason to change this behavior if credentials are sourced from the environment. We aren't changing the way aws-vault login works, just making sure it's consistent independently of where creds are sourced from

To make it more graphical, this is the current behavior:

flowchart TB
    A[aws-vault login] --> B{Profile name specified?}
    B -->|Yes| C[Retrieve credentials from the keychain]

    C --> D{Temporary credentials?}
    D -->|No|F[Call sts:GetFederationToken to retrieve temporary credentials]

    F --> E
    D -->|Yes|E[Generate magic sign-in link using temporary credentials]

    B -->|No| M[Read credentials from the environment]

    M --> N{Temporary credentials?}
    N -->|No|O[Error]

    N -->|Yes|E
Loading

This PR changes the behavior so it becomes the same no matter whether credentials are sourced from the keychain or from the environment:

flowchart TB
    A[aws-vault login] --> B{Profile name specified?}

    B -->|Yes| C[Retrieve credentials from the keychain]
   

    B -->|No| M[Read credentials from the environment]

    C --> D{Temporary credentials?}
    M --> D
    D -->|No|F[Call sts:GetFederationToken to retrieve temporary credentials]
    F --> E
    D -->|Yes|E[Generate magic sign-in link using temporary credentials]

   
Loading

@christophetd christophetd force-pushed the login-with-environment-variables branch 2 times, most recently from 869aa34 to 7847b9e Compare July 20, 2022 09:56
@christophetd
Copy link
Contributor Author

@mtibben What do you think of the above? The argument here is to make sure the behavior of aws-vault login is the same when running it against a profile vs. credentials in the environment.

@christophetd
Copy link
Contributor Author

@mtibben Any thoughts? I heard back from folks that this is the behavior they would expect

@sftim
Copy link

sftim commented Aug 3, 2022

If aws-vault login starts generating temporary credentials automatically, how does it decide what parameters (eg session duration) to use?

@christophetd
Copy link
Contributor Author

Note that aws-login is already generating temporary credentials automatically. The relevant existing parameters are:

$ aws-vault login --help
usage: aws-vault login [<flags>] [<profile>]

Generate a login link for the AWS Console

  -d, --duration=DURATION        Duration of the assume-role or federated session. Defaults to 1h
  -n, --no-session               Skip creating STS session with GetSessionToken

This PR is simply about making sure these are also generated when sourcing credentials from the environment rather than from the keychain.

@sftim
Copy link

sftim commented Aug 3, 2022

Fair enough, that's legit then!

@christophetd
Copy link
Contributor Author

👋🏼 @mtibben Any feedback on the above! Looking forward to get this merged!

@christophetd christophetd force-pushed the login-with-environment-variables branch from 7847b9e to 0d2c787 Compare September 21, 2022 08:33
@christophetd
Copy link
Contributor Author

Now rebased on latest master, waiting to be merged.

@christophetd christophetd force-pushed the login-with-environment-variables branch from 9f14dfb to 716009b Compare November 17, 2022 09:39
@christophetd
Copy link
Contributor Author

Rebased on latest master. @mtibben what's the best way to get this merged? Thanks!

@christophetd christophetd force-pushed the login-with-environment-variables branch from 716009b to 7de14f1 Compare November 30, 2022 08:23
@christophetd
Copy link
Contributor Author

Rebased on latest master again, eager to get it merged! It makes sure the aws-vault login behavior is consistent. And if I need it on a daily basis, I'm sure other people would benefit from it as well. :-)

@mtibben @lox Let me know if anything is blocking! Thank you

@christophetd christophetd force-pushed the login-with-environment-variables branch from 7de14f1 to cdb9e3e Compare November 30, 2022 08:29

return creds, nil
}

Copy link
Member

@mtibben mtibben Dec 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic here is not particularly clear to me.

Previously above we had a very clear section of code

	if input.ProfileName == "" {
		// When no profile is specified, source credentials from the environment
		credsProvider = vault.NewEnvironmentCredentialsProvider()
	} else {
		// Use a profile from the AWS config file
		ckr := &vault.CredentialKeyring{Keyring: keyring}
		if config.HasRole() || config.HasSSOStartURL() {
			// If AssumeRole or sso.GetRoleCredentials isn't used, GetFederationToken has to be used for IAM credentials
			credsProvider, err = vault.NewTempCredentialsProvider(config, ckr)
		} else {
			credsProvider, err = vault.NewFederationTokenCredentialsProvider(input.ProfileName, ckr, config)
		}
		if err != nil {
			return fmt.Errorf("profile %s: %w", input.ProfileName, err)
		}
	}

This retrieveTemporaryCredsFromEnvironment function makes things less understandable by separating out a new codepath and generating federated tokens separately from this. So we end up with functions at different levels of abstraction

Is it possible for the main logic around this to stay in one place?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me see what I can do!

@mtibben
Copy link
Member

mtibben commented Dec 19, 2022

This PR changes the behavior so it becomes the same no matter whether credentials are sourced from the keychain or from the environment:

The actual code path does not look like the diagram you posted

@christophetd
Copy link
Contributor Author

If aws-vault login is run without a profile:

If aws-vault login is run with a profile:

So yes, it looks like the code path does have the logic I was pointing out above, unless I missed something?

…rate a sign-in link

The way AWS works is that you need a temporary token for it to work.
Otherwise, it will show an "invalid credentials" message in the browser.
@christophetd christophetd force-pushed the login-with-environment-variables branch from cdb9e3e to bb837c8 Compare December 21, 2022 14:10
@mtibben
Copy link
Member

mtibben commented Feb 21, 2023

Hey @christophetd, does #1150 work for you to resolve this issue?

@christophetd
Copy link
Contributor Author

@mtibben Left some comments in #1150, thanks!

@christophetd
Copy link
Contributor Author

closing this PR as it's being addressed by #1150

@christophetd christophetd deleted the login-with-environment-variables branch February 21, 2023 09:00
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.

3 participants