Skip to content

Commit 0d2c787

Browse files
committed
[login] allow to run aws-vault login with non-temporary credentials in the environment
1 parent 71c729d commit 0d2c787

File tree

5 files changed

+123
-70
lines changed

5 files changed

+123
-70
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
@@ -93,13 +93,13 @@ func LoginCommand(input LoginCommandInput, f *vault.ConfigFile, keyring keyring.
9393
}
9494

9595
var credsProvider aws.CredentialsProvider
96+
var creds aws.Credentials
9697

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

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

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

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

go.mod

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
module github.com/99designs/aws-vault/v6
22

3-
go 1.19
3+
go 1.17
44

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.12
9-
github.com/aws/aws-sdk-go-v2/config v1.17.3
10-
github.com/aws/aws-sdk-go-v2/service/iam v1.18.15
11-
github.com/aws/aws-sdk-go-v2/service/sso v1.11.19
12-
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.13.1
13-
github.com/aws/aws-sdk-go-v2/service/sts v1.16.15
14-
github.com/google/go-cmp v0.5.8
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
15+
github.com/google/go-cmp v0.5.7
1516
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
16-
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261
17-
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035
18-
gopkg.in/ini.v1 v1.67.0
17+
golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a
18+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
19+
gopkg.in/ini.v1 v1.66.4
1920
)
2021

2122
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.12.16 // indirect
26-
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.13 // indirect
27-
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.19 // indirect
28-
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.13 // indirect
29-
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.20 // indirect
30-
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.13 // indirect
31-
github.com/aws/smithy-go v1.13.0 // 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

+45-34
Original file line numberDiff line numberDiff line change
@@ -10,32 +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.16.12 h1:wbMYa2PlFysFx2GLIQojr6FJV5+OWCM/BwyHXARxETA=
14-
github.com/aws/aws-sdk-go-v2 v1.16.12/go.mod h1:C+Ym0ag2LIghJbXhfXZ0YEEp49rBWowxKzJLUoob0ts=
15-
github.com/aws/aws-sdk-go-v2/config v1.17.3 h1:s1As/fiVMmM3CObC4GcSaSbkhm88S6a5qn8St3wgal0=
16-
github.com/aws/aws-sdk-go-v2/config v1.17.3/go.mod h1:tRGUOfk9Rrf6UCJm5qDlL9AizSsgvteuKX4qajAV3pU=
17-
github.com/aws/aws-sdk-go-v2/credentials v1.12.16 h1:HXczS88Pg36j8dq0KSjtHBPFs8gdRyBSS1hueeG/rxA=
18-
github.com/aws/aws-sdk-go-v2/credentials v1.12.16/go.mod h1:eLJ+j1lwQdHJ0c56tRoDWcgss1e/laVmvW2AaOicuAw=
19-
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.13 h1:+uferi8SUDZtMloCDt24Zenyy/i71C/ua5mjUCpbpN0=
20-
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.13/go.mod h1:y0eXmsNBFIVjUE8ZBjES8myOHlMsXDz7qGT93+MVdjk=
21-
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.19 h1:gC5mudiFrWGhzcdoWj1iCGUfrzCpQG0MQIQf0CXFFQQ=
22-
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.19/go.mod h1:llxE6bwUZhuCas0K7qGiu5OgMis3N7kdWtFSxoHmJ7E=
23-
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.13 h1:qezY57na06d6kSE7uuB0N7XEflu914AXx/hg2L8Ykcw=
24-
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.13/go.mod h1:lB12mkZqCSo5PsdBFLNqc2M/OOYgNAy8UtaktyuWvE8=
25-
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.20 h1:GvszACAU8GSV3+Tant5GutW6smY8WavrP8ZuRS9Ku4Q=
26-
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.20/go.mod h1:bfTcsThj5a9P5pIGRy0QudJ8k4+issxXX+O6Djnd5Cs=
27-
github.com/aws/aws-sdk-go-v2/service/iam v1.18.15 h1:cW3Okx2MHPl/RDAy9kCJMO8bHsvOuzUVAfxY2tGT72g=
28-
github.com/aws/aws-sdk-go-v2/service/iam v1.18.15/go.mod h1:ArKxW0tjLJ/V3r9Go9zuMJ3lvP+5jH8eSmyMg+8lbWs=
29-
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.13 h1:ObfthqDyhe7rMAOa7pqft6974VHIk8BAJB7kYdoIfTA=
30-
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.13/go.mod h1:V390DK4MQxLpDdXxFqizyz8KUxuWImkW/xzgXMz0yyk=
31-
github.com/aws/aws-sdk-go-v2/service/sso v1.11.19 h1:WdCwfJmu23XiIDeZwclSyAorQe916M3LeHd53xqBjfA=
32-
github.com/aws/aws-sdk-go-v2/service/sso v1.11.19/go.mod h1:ytmEi5+qwcSNcV2pVA8PIb1DnKT/0Bu/K4nfJHwoM6c=
33-
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.13.1 h1:p48IfndYbRk3iDsoQAmVXdCKEM5+7Y50JAPikjwk8gI=
34-
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.13.1/go.mod h1:NY+G+8PW0ISyJ7/6t5mgOe6qpJiwZa9Jix05WPscJjg=
35-
github.com/aws/aws-sdk-go-v2/service/sts v1.16.15 h1:ApuR2BK9vf5/XXsImHBBsYJ6aUhmUhBHnZMPyhJo1jQ=
36-
github.com/aws/aws-sdk-go-v2/service/sts v1.16.15/go.mod h1:Y+BUV19q3OmQVqNUlbZ40zVi3NM6Biuxwkx/qdSD/CY=
37-
github.com/aws/smithy-go v1.13.0 h1:YfyEmSJLo7fAv8FbuDK4R8F9aAmi9DZ88Zb/KJJmUl0=
38-
github.com/aws/smithy-go v1.13.0/go.mod h1:Tg+OJXh4MB2R/uN61Ko2f6hTZwB/ZYGOtib8J3gBHzA=
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=
15+
github.com/aws/aws-sdk-go-v2 v1.15.0/go.mod h1:lJYcuZZEHWNIb6ugJjbQY1fykdoobWbOS7kJYb4APoI=
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=
24+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.6/go.mod h1:SSPEdf9spsFgJyhjrXvawfpyzrXHBCUe+2eQ1CjC1Ak=
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=
27+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.0/go.mod h1:viTrxhAuejD+LszDahzAE2x40YjYWhMqzHxv2ZiWaME=
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=
43+
github.com/aws/smithy-go v1.11.1/go.mod h1:3xHYmszWVx2c0kIwQeEVf9uSm4fYZt67FBJnwub1bgM=
3944
github.com/danieljoos/wincred v1.1.2 h1:QLdCxFs1/Yl4zduvBdcHB8goaYk9RARS2SgLLRuAyr0=
4045
github.com/danieljoos/wincred v1.1.2/go.mod h1:GijpziifJoIBfYh+S7BbkdUTU4LfM+QnGqR5Vl2tAx0=
4146
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -45,8 +50,8 @@ github.com/dvsekhvalnov/jose2go v1.5.0 h1:3j8ya4Z4kMCwT5nXIKFSV84YS+HdqSSO0VsTQx
4550
github.com/dvsekhvalnov/jose2go v1.5.0/go.mod h1:QsHjhyTlD/lAVqn/NSbVZmSCGeDehTB/mPZadG+mhXU=
4651
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 h1:ZpnhV/YsD2/4cESfV5+Hoeu/iUR3ruzNvZ+yQfO03a0=
4752
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2/go.mod h1:bBOAhwG1umN6/6ZUMtDFBMQR8jRg9O75tm9K00oMsK4=
48-
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
49-
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
53+
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
54+
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
5055
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c h1:6rhixN/i8ZofjG1Y75iExal34USq5p+wiN1tpie8IrU=
5156
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c/go.mod h1:NMPJylDgVpX0MLRlPy15sqSwOFv/U1GZ2m21JhFfek0=
5257
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
@@ -64,21 +69,27 @@ github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 h1:JIAuq3EE
6469
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog=
6570
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
6671
github.com/stretchr/objx v0.3.0 h1:NGXK3lHquSN08v5vWalVI/L8XU9hdzE/G6xsrze47As=
72+
github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
6773
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
74+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
6875
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
6976
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
7077
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
78+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
7179
golang.org/x/sys v0.0.0-20210819135213-f52c844e1c1c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
72-
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 h1:v6hYoSR9T5oet+pMXwUWkbiVqx/63mlHjefrHmxwfeY=
73-
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
74-
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 h1:Q5284mrmYTpACcm+eAKjKJH48BBwSyfJqmmGDTtT8Vc=
75-
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
80+
golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a h1:ppl5mZgokTT8uPkmYOyEUmPTr3ypaKkg5eFOGrAmxxE=
81+
golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
82+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
83+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
84+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
85+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
7686
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
7787
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b h1:QRR6H1YWRnHb4Y/HeNFCTJLFVxaq6wH4YuVdsUOr75U=
7888
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
79-
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
80-
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
89+
gopkg.in/ini.v1 v1.66.4 h1:SsAcf+mM7mRZo2nJNGt8mZCjG8ZRaNGMURJw7BsIST4=
90+
gopkg.in/ini.v1 v1.66.4/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
8191
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
8292
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
8393
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
8494
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
95+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

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)