Skip to content

Commit f22295c

Browse files
authored
Merge pull request #1150 from 99designs/login-with-master-creds
Allow login with master creds in environment
2 parents 9607c3b + 39456f4 commit f22295c

File tree

5 files changed

+24
-33
lines changed

5 files changed

+24
-33
lines changed

USAGE.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,12 @@ You can use the `aws-vault login` command to open a browser window and login to
331331
$ aws-vault login work
332332
```
333333

334-
If you have temporary STS credentials already available in your environment, you can have aws-vault use these credentials to sign you in.
335-
This is useful when you had to use something else than aws-vault to retrieve temporary credentials:
334+
If you have credentials already available in your environment, aws-vault will use these credentials to sign you in to the AWS console.
336335

337336
```shell
338-
# AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_SESSION_TOKEN must be set in your environment prior to running the below
337+
$ export AWS_ACCESS_KEY_ID=%%%
338+
$ export AWS_SECRET_ACCESS_KEY=%%%
339+
$ export AWS_SESSION_TOKEN=%%%
339340
$ aws-vault login
340341
```
341342

cli/login.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"github.com/99designs/keyring"
1616
"github.com/alecthomas/kingpin"
1717
"github.com/aws/aws-sdk-go-v2/aws"
18+
awsconfig "github.com/aws/aws-sdk-go-v2/config"
19+
"github.com/aws/aws-sdk-go-v2/credentials"
1820
"github.com/skratchdot/open-golang/open"
1921
)
2022

@@ -95,7 +97,17 @@ func LoginCommand(input LoginCommandInput, f *vault.ConfigFile, keyring keyring.
9597

9698
if input.ProfileName == "" {
9799
// When no profile is specified, source credentials from the environment
98-
credsProvider = vault.NewEnvironmentCredentialsProvider()
100+
configFromEnv, err := awsconfig.NewEnvConfig()
101+
if err != nil {
102+
return fmt.Errorf("unable to authenticate to AWS through your environment variables: %w", err)
103+
}
104+
credsProvider = credentials.StaticCredentialsProvider{Value: configFromEnv.Credentials}
105+
if configFromEnv.Credentials.SessionToken == "" {
106+
credsProvider, err = vault.NewFederationTokenProvider(context.TODO(), credsProvider, config)
107+
if err != nil {
108+
return err
109+
}
110+
}
99111
} else {
100112
// Use a profile from the AWS config file
101113
ckr := &vault.CredentialKeyring{Keyring: keyring}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/alecthomas/kingpin v0.0.0-20200323085623-b6657d9477a6
88
github.com/aws/aws-sdk-go-v2 v1.17.4
99
github.com/aws/aws-sdk-go-v2/config v1.18.13
10+
github.com/aws/aws-sdk-go-v2/credentials v1.13.13
1011
github.com/aws/aws-sdk-go-v2/service/iam v1.19.2
1112
github.com/aws/aws-sdk-go-v2/service/sso v1.12.2
1213
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.14.2
@@ -22,7 +23,6 @@ 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.13.13 // indirect
2626
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.22 // indirect
2727
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.28 // indirect
2828
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.22 // indirect

vault/environmentvariablescredentialsprovider.go

-22
This file was deleted.

vault/vault.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,13 @@ func NewFederationTokenCredentialsProvider(ctx context.Context, profileName stri
274274
if err != nil {
275275
return nil, err
276276
}
277-
278277
masterCreds := NewMasterCredentialsProvider(k, credentialsName)
279-
cfg := NewAwsConfigWithCredsProvider(masterCreds, config.Region, config.STSRegionalEndpoints)
278+
279+
return NewFederationTokenProvider(ctx, masterCreds, config)
280+
}
281+
282+
func NewFederationTokenProvider(ctx context.Context, credsProvider aws.CredentialsProvider, config *Config) (*FederationTokenProvider, error) {
283+
cfg := NewAwsConfigWithCredsProvider(credsProvider, config.Region, config.STSRegionalEndpoints)
280284

281285
currentUsername, err := GetUsernameFromSession(ctx, cfg)
282286
if err != nil {
@@ -291,10 +295,6 @@ func NewFederationTokenCredentialsProvider(ctx context.Context, profileName stri
291295
}, nil
292296
}
293297

294-
func NewEnvironmentCredentialsProvider() aws.CredentialsProvider {
295-
return &EnvironmentVariablesCredentialsProvider{}
296-
}
297-
298298
func FindMasterCredentialsNameFor(profileName string, keyring *CredentialKeyring, config *Config) (string, error) {
299299
hasMasterCreds, err := keyring.Has(profileName)
300300
if err != nil {

0 commit comments

Comments
 (0)