Skip to content

Commit 7847b9e

Browse files
committed
[login] allow to run aws-vault login with non-temporary credentials in the environment
1 parent add7709 commit 7847b9e

File tree

5 files changed

+100
-57
lines changed

5 files changed

+100
-57
lines changed

USAGE.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,13 @@ You can use the `aws-vault login` command to open a browser window and login to
307307
$ aws-vault login work
308308
```
309309

310-
If you have temporary STS credentials already available in your environment, you can have aws-vault use these credentials to sign you in.
311-
This is useful when you had to use something else than aws-vault to retrieve temporary credentials:
310+
If you have credentials already available in your environment, you can have aws-vault use these credentials to sign you in.
311+
This is useful when you had to use something else than aws-vault to retrieve credentials:
312312

313313
```shell
314-
# AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_SESSION_TOKEN must be set in your environment prior to running the below
314+
# AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and optionally AWS_SESSION_TOKEN must be set in your environment prior to running the below
315+
# If AWS_SESSION_TOKEN is not set, a call to sts:GetFederationToken will be issued to retrieve temporary credentials,
316+
# require to be able to generate a sign-in link to the AWS console
315317
$ aws-vault login
316318
```
317319

cli/login.go

+38-12
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ func LoginCommand(input LoginCommandInput, f *vault.ConfigFile, keyring keyring.
9292
}
9393

9494
var credsProvider aws.CredentialsProvider
95+
var creds aws.Credentials
9596

97+
// Use a profile from the AWS config file
98+
ckr := &vault.CredentialKeyring{Keyring: keyring}
9699
if input.ProfileName == "" {
97-
// When no profile is specified, source credentials from the environment
98-
credsProvider = vault.NewEnvironmentCredentialsProvider()
100+
creds, err = retrieveTemporaryCredsFromEnvironment(config)
99101
} else {
100-
// Use a profile from the AWS config file
101-
ckr := &vault.CredentialKeyring{Keyring: keyring}
102102
if config.HasRole() || config.HasSSOStartURL() {
103103
// If AssumeRole or sso.GetRoleCredentials isn't used, GetFederationToken has to be used for IAM credentials
104104
credsProvider, err = vault.NewTempCredentialsProvider(config, ckr)
@@ -108,22 +108,15 @@ func LoginCommand(input LoginCommandInput, f *vault.ConfigFile, keyring keyring.
108108
if err != nil {
109109
return fmt.Errorf("profile %s: %w", input.ProfileName, err)
110110
}
111+
creds, err = credsProvider.Retrieve(context.TODO())
111112
}
112113

113-
creds, err := credsProvider.Retrieve(context.TODO())
114114
if err != nil {
115115
return fmt.Errorf("Failed to get credentials: %w", err)
116116
}
117117
if creds.AccessKeyID == "" && input.ProfileName == "" {
118118
return fmt.Errorf("argument 'profile' not provided, nor any AWS env vars found. Try --help")
119119
}
120-
if creds.SessionToken == "" {
121-
// When sourcing credentials from the environment, it's possible a session token wasn't set
122-
// Generating a sign-in link requires temporary credentials, so we return an error
123-
// NOTE: We deliberately chose to have this logic here rather than in 'EnvironmentVariablesCredentialsProvider'
124-
// to make it possible to reuse it for other commands than `aws-vault login` in the future
125-
return fmt.Errorf("failed to retrieve a session token. Cannot generate a login URL without it")
126-
}
127120

128121
jsonBytes, err := json.Marshal(map[string]string{
129122
"sessionId": creds.AccessKeyID,
@@ -191,6 +184,39 @@ func LoginCommand(input LoginCommandInput, f *vault.ConfigFile, keyring keyring.
191184
return nil
192185
}
193186

187+
// retrieveTemporaryCredsFromEnvironment contains the logic to retrieve the proper credentials
188+
// from the environment.
189+
// - Case 1: Temporary credentials are available - these are directly returned
190+
// - Case 2: Non-temporary credentials are available. A call to sts:GetFederation is made, and the resulting temporary
191+
// credentials returned
192+
func retrieveTemporaryCredsFromEnvironment(config *vault.Config) (aws.Credentials, error) {
193+
// When no profile is specified, source credentials from the environment
194+
credsProvider := vault.NewEnvironmentCredentialsProvider()
195+
creds, err := credsProvider.Retrieve(context.TODO())
196+
if err != nil {
197+
return aws.Credentials{}, fmt.Errorf("unable to find credentials in your environment")
198+
}
199+
200+
// If the credentials we found in the environment aren't temporary,
201+
// use sts:GetFederationToken to get temporary credentials
202+
// allowing to generate a sign-in link.
203+
// Non-temporary credentials cannot be used for this purpose
204+
if creds.SessionToken == "" {
205+
credsProvider, err := vault.NewFederationTokenCredentialsProviderFromCredentials(&creds, config)
206+
if err != nil {
207+
return aws.Credentials{}, err
208+
}
209+
210+
creds, err = credsProvider.Retrieve(context.TODO())
211+
if err != nil {
212+
err = fmt.Errorf("non-temporary credentials found in your environment, and calling GetFederationToken resulted in: " + err.Error())
213+
return aws.Credentials{}, err
214+
}
215+
}
216+
217+
return creds, nil
218+
}
219+
194220
func generateLoginURL(region string, path string) (string, string) {
195221
loginURLPrefix := "https://signin.aws.amazon.com/federation"
196222
destination := "https://console.aws.amazon.com/"

go.mod

+13-13
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ go 1.17
55
require (
66
github.com/99designs/keyring v1.2.1
77
github.com/alecthomas/kingpin v0.0.0-20200323085623-b6657d9477a6
8-
github.com/aws/aws-sdk-go-v2 v1.16.2
9-
github.com/aws/aws-sdk-go-v2/config v1.15.3
10-
github.com/aws/aws-sdk-go-v2/service/iam v1.18.3
11-
github.com/aws/aws-sdk-go-v2/service/sso v1.11.3
12-
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.12.3
13-
github.com/aws/aws-sdk-go-v2/service/sts v1.16.3
8+
github.com/aws/aws-sdk-go-v2 v1.15.0
9+
github.com/aws/aws-sdk-go-v2/config v1.14.0
10+
github.com/aws/aws-sdk-go-v2/credentials v1.9.0
11+
github.com/aws/aws-sdk-go-v2/service/iam v1.18.0
12+
github.com/aws/aws-sdk-go-v2/service/sso v1.11.0
13+
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.12.0
14+
github.com/aws/aws-sdk-go-v2/service/sts v1.15.0
1415
github.com/google/go-cmp v0.5.7
1516
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
1617
golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a
@@ -22,13 +23,12 @@ require (
2223
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
2324
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
2425
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
25-
github.com/aws/aws-sdk-go-v2/credentials v1.11.2 // indirect
26-
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.3 // indirect
27-
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.9 // indirect
28-
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.3 // indirect
29-
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.10 // indirect
30-
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.3 // indirect
31-
github.com/aws/smithy-go v1.11.2 // indirect
26+
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.11.0 // indirect
27+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.6 // indirect
28+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.0 // indirect
29+
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.6 // indirect
30+
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.8.0 // indirect
31+
github.com/aws/smithy-go v1.11.1 // indirect
3232
github.com/danieljoos/wincred v1.1.2 // indirect
3333
github.com/dvsekhvalnov/jose2go v1.5.0 // indirect
3434
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect

go.sum

+27-26
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,37 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuy
1010
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
1111
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 h1:s6gZFSlWYmbqAuRjVTiNNhvNRfY2Wxp9nhfyel4rklc=
1212
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
13+
github.com/aws/aws-sdk-go-v2 v1.14.0/go.mod h1:ZA3Y8V0LrlWj63MQAnRHgKf/5QB//LSZCPNWlWrNGLU=
14+
github.com/aws/aws-sdk-go-v2 v1.15.0 h1:f9kWLNfyCzCB43eupDAk3/XgJ2EpgktiySD6leqs0js=
1315
github.com/aws/aws-sdk-go-v2 v1.15.0/go.mod h1:lJYcuZZEHWNIb6ugJjbQY1fykdoobWbOS7kJYb4APoI=
14-
github.com/aws/aws-sdk-go-v2 v1.16.2 h1:fqlCk6Iy3bnCumtrLz9r3mJ/2gUT0pJ0wLFVIdWh+JA=
15-
github.com/aws/aws-sdk-go-v2 v1.16.2/go.mod h1:ytwTPBG6fXTZLxxeeCCWj2/EMYp/xDUgX+OET6TLNNU=
16-
github.com/aws/aws-sdk-go-v2/config v1.15.3 h1:5AlQD0jhVXlGzwo+VORKiUuogkG7pQcLJNzIzK7eodw=
17-
github.com/aws/aws-sdk-go-v2/config v1.15.3/go.mod h1:9YL3v07Xc/ohTsxFXzan9ZpFpdTOFl4X65BAKYaz8jg=
18-
github.com/aws/aws-sdk-go-v2/credentials v1.11.2 h1:RQQ5fzclAKJyY5TvF+fkjJEwzK4hnxQCLOu5JXzDmQo=
19-
github.com/aws/aws-sdk-go-v2/credentials v1.11.2/go.mod h1:j8YsY9TXTm31k4eFhspiQicfXPLZ0gYXA50i4gxPE8g=
20-
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.3 h1:LWPg5zjHV9oz/myQr4wMs0gi4CjnDN/ILmyZUFYXZsU=
21-
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.3/go.mod h1:uk1vhHHERfSVCUnqSqz8O48LBYDSC+k6brng09jcMOk=
16+
github.com/aws/aws-sdk-go-v2/config v1.14.0 h1:Yr8/7R6H8nqqfqgLATrcB83ax6FE2HcDXEB54XPhE98=
17+
github.com/aws/aws-sdk-go-v2/config v1.14.0/go.mod h1:GKDRrvsq/PTaOYc9252u8Uah1hsIdtor4oIrFvUNPNM=
18+
github.com/aws/aws-sdk-go-v2/credentials v1.9.0 h1:R3Q5s1uGLUg0aUzi+oRaUqRXhd17G/9+PiVnAwXp4sY=
19+
github.com/aws/aws-sdk-go-v2/credentials v1.9.0/go.mod h1:PyHKqk/+tJuDY7T8R580S1j/AcSD+ODeUZ99CAUKLqQ=
20+
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.11.0 h1:CkM4d3lNeMXMZ0BDX3BtCktnKA1Ftud84Hb6d+Ix4Rk=
21+
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.11.0/go.mod h1:rwdUKJV5rm+vHu1ncD1iGDqahBEL8O0tBjVqo9eO2N0=
22+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.5/go.mod h1:2hXc8ooJqF2nAznsbJQIn+7h851/bu8GVC80OVTTqf8=
23+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.6 h1:xiGjGVQsem2cxoIX61uRGy+Jux2s9C/kKbTrWLdrU54=
2224
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.6/go.mod h1:SSPEdf9spsFgJyhjrXvawfpyzrXHBCUe+2eQ1CjC1Ak=
23-
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.9 h1:onz/VaaxZ7Z4V+WIN9Txly9XLTmoOh1oJ8XcAC3pako=
24-
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.9/go.mod h1:AnVH5pvai0pAF4lXRq0bmhbes1u9R8wTE+g+183bZNM=
25+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.3.0/go.mod h1:miRSv9l093jX/t/j+mBCaLqFHo9xKYzJ7DGm1BsGoJM=
26+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.0 h1:bt3zw79tm209glISdMRCIVRCwvSDXxgAxh5KWe2qHkY=
2527
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.0/go.mod h1:viTrxhAuejD+LszDahzAE2x40YjYWhMqzHxv2ZiWaME=
26-
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.3 h1:9stUQR/u2KXU6HkFJYlqnZEjBnbgrVbG6I5HN09xZh0=
27-
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.3/go.mod h1:ssOhaLpRlh88H3UmEcsBoVKq309quMvm3Ds8e9d4eJM=
28-
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.10 h1:by9P+oy3P/CwggN4ClnW2D4oL91QV7pBzBICi1chZvQ=
29-
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.10/go.mod h1:8DcYQcz0+ZJaSxANlHIsbbi6S+zMwjwdDqwW3r9AzaE=
30-
github.com/aws/aws-sdk-go-v2/service/iam v1.18.3 h1:wllKL2fLtvfaNAVbXKMRmM/mD1oDNw0hXmDn8mE/6Us=
31-
github.com/aws/aws-sdk-go-v2/service/iam v1.18.3/go.mod h1:51xGfEjd1HXnTzw2mAp++qkRo+NyGYblZkuGTsb49yw=
32-
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.3 h1:Gh1Gpyh01Yvn7ilO/b/hr01WgNpaszfbKMUgqM186xQ=
33-
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.3/go.mod h1:wlY6SVjuwvh3TVRpTqdy4I1JpBFLX4UGeKZdWntaocw=
34-
github.com/aws/aws-sdk-go-v2/service/sso v1.11.3 h1:frW4ikGcxfAEDfmQqWgMLp+F1n4nRo9sF39OcIb5BkQ=
35-
github.com/aws/aws-sdk-go-v2/service/sso v1.11.3/go.mod h1:7UQ/e69kU7LDPtY40OyoHYgRmgfGM4mgsLYtcObdveU=
36-
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.12.3 h1:Sz69LcNwUgqpso47UM47ZoyX+DJ2oN/0NykiMokBk4o=
37-
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.12.3/go.mod h1:SkOxNZFD2bxcGrQzwac0ZTC9ewY8+3tWgMD8LyqM8mU=
38-
github.com/aws/aws-sdk-go-v2/service/sts v1.16.3 h1:cJGRyzCSVwZC7zZZ1xbx9m32UnrKydRYhOvcD1NYP9Q=
39-
github.com/aws/aws-sdk-go-v2/service/sts v1.16.3/go.mod h1:bfBj0iVmsUyUg4weDB4NxktD9rDGeKSVWnjTnwbx9b8=
28+
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.6 h1:c8s9EhIPVFMFS+R1+rtEghGrf7v83gSUWbcCYX/OPes=
29+
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.6/go.mod h1:o1ippSg3yJx5EuT4AOGXJCUcmt5vrcxla1cg6K1Q8Iw=
30+
github.com/aws/aws-sdk-go-v2/service/iam v1.18.0 h1:ZYpP40/QE7/R0zDxdrZyGGUijX26iB+Pint/NYzF/tQ=
31+
github.com/aws/aws-sdk-go-v2/service/iam v1.18.0/go.mod h1:9wRsXAkRJ7qBWIDTFYa66Cx+oQJsPEnBYCPrinanpS8=
32+
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.8.0 h1:JNMALY8/ZnFsfAzBHtC4gq8JeZPANmIoI2VaBgYzbf8=
33+
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.8.0/go.mod h1:rBDLgXDAwHOfxZKLRDl8OGTPzFDC+a2pLqNNj8+QwfI=
34+
github.com/aws/aws-sdk-go-v2/service/sso v1.10.0/go.mod h1:m1CRRFX7eH3EE6w0ntdu+lo+Ph9VS7y8qRV/vdym0ZY=
35+
github.com/aws/aws-sdk-go-v2/service/sso v1.11.0 h1:gZLEXLH6NiU8Y52nRhK1jA+9oz7LZzBK242fi/ziXa4=
36+
github.com/aws/aws-sdk-go-v2/service/sso v1.11.0/go.mod h1:d1WcT0OjggjQCAdOkph8ijkr5sUwk1IH/VenOn7W1PU=
37+
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.12.0 h1:4XqJtMG9sCqgmOpZXaZ7hP3pMi4xFvQgld5Ii4qQfUU=
38+
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.12.0/go.mod h1:tA3OAtm58sKX67hc188WPbcIgJpnPMDXC0R+3qRmBbs=
39+
github.com/aws/aws-sdk-go-v2/service/sts v1.15.0 h1:zC/vHxWTlqZ0tIPJItg0zWHsa25cH7tXsUknSGcH39o=
40+
github.com/aws/aws-sdk-go-v2/service/sts v1.15.0/go.mod h1:E264g2Gl5U9KTGzmd8ypGEAoh75VmqyuA/Ox5O1eRE4=
41+
github.com/aws/smithy-go v1.11.0/go.mod h1:3xHYmszWVx2c0kIwQeEVf9uSm4fYZt67FBJnwub1bgM=
42+
github.com/aws/smithy-go v1.11.1 h1:IQ+lPZVkSM3FRtyaDox41R8YS6iwPMYIreejOgPW49g=
4043
github.com/aws/smithy-go v1.11.1/go.mod h1:3xHYmszWVx2c0kIwQeEVf9uSm4fYZt67FBJnwub1bgM=
41-
github.com/aws/smithy-go v1.11.2 h1:eG/N+CcUMAvsdffgMvjMKwfyDzIkjM6pfxMJ8Mzc6mE=
42-
github.com/aws/smithy-go v1.11.2/go.mod h1:3xHYmszWVx2c0kIwQeEVf9uSm4fYZt67FBJnwub1bgM=
4344
github.com/danieljoos/wincred v1.1.2 h1:QLdCxFs1/Yl4zduvBdcHB8goaYk9RARS2SgLLRuAyr0=
4445
github.com/danieljoos/wincred v1.1.2/go.mod h1:GijpziifJoIBfYh+S7BbkdUTU4LfM+QnGqR5Vl2tAx0=
4546
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

vault/vault.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/99designs/aws-vault/v6/prompt"
1111
"github.com/99designs/keyring"
1212
"github.com/aws/aws-sdk-go-v2/aws"
13+
"github.com/aws/aws-sdk-go-v2/credentials"
1314
"github.com/aws/aws-sdk-go-v2/service/sso"
1415
"github.com/aws/aws-sdk-go-v2/service/ssooidc"
1516
"github.com/aws/aws-sdk-go-v2/service/sts"
@@ -276,16 +277,29 @@ func NewFederationTokenCredentialsProvider(profileName string, k *CredentialKeyr
276277
}
277278

278279
masterCreds := NewMasterCredentialsProvider(k, credentialsName)
279-
cfg := NewAwsConfigWithCredsProvider(masterCreds, config.Region, config.STSRegionalEndpoints)
280+
awsConfig := NewAwsConfigWithCredsProvider(masterCreds, config.Region, config.STSRegionalEndpoints)
280281

281-
currentUsername, err := GetUsernameFromSession(cfg)
282+
return newFederationTokenCredentialsProvider(awsConfig, config)
283+
}
284+
285+
func NewFederationTokenCredentialsProviderFromCredentials(creds *aws.Credentials, config *Config) (aws.CredentialsProvider, error) {
286+
credentialsProvider := credentials.NewStaticCredentialsProvider(creds.AccessKeyID, creds.SecretAccessKey, "")
287+
awsConfig := NewAwsConfigWithCredsProvider(credentialsProvider, config.Region, config.STSRegionalEndpoints)
288+
289+
return newFederationTokenCredentialsProvider(awsConfig, config)
290+
}
291+
292+
// utility function to avoid code duplication
293+
// in NewFederationTokenCredentialsProvider and NewFederationTokenCredentialsProviderFromCredentials
294+
func newFederationTokenCredentialsProvider(awsConfig aws.Config, config *Config) (aws.CredentialsProvider, error) {
295+
currentUsername, err := GetUsernameFromSession(awsConfig)
282296
if err != nil {
283297
return nil, err
284298
}
285299

286300
log.Printf("Using GetFederationToken for credentials")
287301
return &FederationTokenProvider{
288-
StsClient: sts.NewFromConfig(cfg),
302+
StsClient: sts.NewFromConfig(awsConfig),
289303
Name: currentUsername,
290304
Duration: config.GetFederationTokenDuration,
291305
}, nil

0 commit comments

Comments
 (0)