Skip to content

Commit

Permalink
Merge pull request #1274 from timharris777/master
Browse files Browse the repository at this point in the history
Fixing saved credential issue for --credential-process introduced by #896
  • Loading branch information
mapkon authored May 21, 2024
2 parents 48be77c + 54e1e97 commit 2951611
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
26 changes: 26 additions & 0 deletions cmd/saml2aws/commands/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,18 @@ func Login(loginFlags *flags.LoginExecFlags) error {
if err != nil {
return err
}
// Check if a custom credential file is used
customCredentialsFile, err := CustomCredentialsFile(sharedCreds.Filename)
if err != nil {
return err
}
// If a custom credential file is used then save credentials. This allows for autorefreshing of credentials, which is not supported with the default credential file. See https://github.com/Versent/saml2aws/issues/895
if customCredentialsFile {
err = saveCredentials(awsCreds, sharedCreds)
if err != nil {
return err
}
}
} else {
err = saveCredentials(awsCreds, sharedCreds)
if err != nil {
Expand Down Expand Up @@ -409,6 +421,20 @@ func CredentialsToCredentialProcess(awsCreds *awsconfig.AWSCredentials) (string,

}

func CustomCredentialsFile(credentialsFile string) (bool, error) {

homeDir, err := os.UserHomeDir()
if err != nil {
return false, err
}
defaultCredentialsFile := fmt.Sprintf("%s/.aws/credentials", homeDir)
if defaultCredentialsFile == credentialsFile {
return false, nil
}
return true, nil

}

// PrintCredentialProcess Prints a Json output that is compatible with the AWS credential_process
// https://github.com/awslabs/awsprocesscreds
func PrintCredentialProcess(awsCreds *awsconfig.AWSCredentials) error {
Expand Down
31 changes: 31 additions & 0 deletions cmd/saml2aws/commands/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commands

import (
"fmt"
"os"
"testing"
"time"

Expand Down Expand Up @@ -94,3 +95,33 @@ func TestCredentialsToCredentialProcess(t *testing.T) {
assert.Empty(t, err)
assert.Equal(t, aws_json_expected_output, json)
}

func TestCustomCredentialsFile(t *testing.T) {
homeDir, err := os.UserHomeDir()
if err != nil {
t.Error("Error getting os.UserHomeDir() in test")
}

defaultCredentialsFile := fmt.Sprintf("%s/.aws/credentials", homeDir)
customCredentialsFile := fmt.Sprintf("%s/.aws/saml2aws.creds", homeDir)

// Testing default credential file
got, err := CustomCredentialsFile(defaultCredentialsFile)
if err != nil {
t.Error("Error running CustomCredentialsFile in test")
}
want := false
if got != want {
t.Errorf("got %t, wanted %t", got, want)
}

// Testing custom credential file
got, err = CustomCredentialsFile(customCredentialsFile)
if err != nil {
t.Error("Error running CustomCredentialsFile in test")
}
want = true
if got != want {
t.Errorf("got %t, wanted %t", got, want)
}
}

0 comments on commit 2951611

Please sign in to comment.