Repository: Versent/saml2aws
Pull request #4 (2016-09-19 10:31:14)

Fixes AWS credentials file creation issue.


Patch:
diff --git a/awsconfig.go b/awsconfig.go
index 0071e0e..376bc67 100644
--- a/awsconfig.go
+++ b/awsconfig.go
@@ -2,6 +2,7 @@ package saml2aws
 
 import (
 	"errors"
+	"io/ioutil"
 	"os"
 	"path/filepath"
 
@@ -53,9 +54,37 @@ func (p *CredentialsProvider) Save(id, secret, token string) error {
 		return err
 	}
 
+	err = p.ensureConfigExists()
+	if err != nil {
+		return err
+	}
+
 	return saveProfile(filename, p.Profile, id, secret, token)
 }
 
+// ensureConfigExists verify that the config file exists
+func (p *CredentialsProvider) ensureConfigExists() error {
+	filename, err := p.filename()
+	if err != nil {
+		return err
+	}
+
+	if _, err := os.Stat(filename); err != nil {
+		if os.IsNotExist(err) {
+
+			// create an base config file
+			err = ioutil.WriteFile(filename, []byte("["+p.Profile+"]"), 0600)
+			if err != nil {
+				return err
+			}
+
+		}
+		return err
+	}
+
+	return nil
+}
+
 func (p *CredentialsProvider) filename() (string, error) {
 	if p.Filename == "" {
 		if p.Filename = os.Getenv("AWS_SHARED_CREDENTIALS_FILE"); p.Filename != "" {
diff --git a/cmd/saml2aws/commands/login.go b/cmd/saml2aws/commands/login.go
index f22168e..108d795 100644
--- a/cmd/saml2aws/commands/login.go
+++ b/cmd/saml2aws/commands/login.go
@@ -12,15 +12,9 @@ import (
 	"github.com/versent/saml2aws"
 )
 
+// Login login to ADFS
 func Login(profile string, skipVerify bool) error {
 
-	sharedCreds := saml2aws.NewSharedCredentials(profile)
-
-	err := sharedCreds.Exists()
-	if err != nil {
-		return errors.Wrap(err, "error loading aws credentials file")
-	}
-
 	config := saml2aws.NewConfigLoader("adfs")
 
 	username, err := config.LoadUsername()
@@ -104,6 +98,8 @@ func Login(profile string, skipVerify bool) error {
 
 	fmt.Println("Saving credentials")
 
+	sharedCreds := saml2aws.NewSharedCredentials(profile)
+
 	err = sharedCreds.Save(aws.StringValue(resp.Credentials.AccessKeyId), aws.StringValue(resp.Credentials.SecretAccessKey), aws.StringValue(resp.Credentials.SessionToken))
 	if err != nil {
 		return errors.Wrap(err, "error saving credentials")
diff --git a/config.go b/config.go
index bf35445..a6e9f63 100644
--- a/config.go
+++ b/config.go
@@ -41,7 +41,7 @@ func (p *ConfigLoader) ensureConfigExists() error {
 		if os.IsNotExist(err) {
 
 			// create an base config file
-			err = ioutil.WriteFile(filename, []byte("["+p.Profile+"]"), 0666)
+			err = ioutil.WriteFile(filename, []byte("["+p.Profile+"]"), 0600)
 			if err != nil {
 				return err
 			}


Test patch:
diff --git a/awsconfig_test.go b/awsconfig_test.go
index 3a1d339..0777881 100644
--- a/awsconfig_test.go
+++ b/awsconfig_test.go
@@ -1,7 +1,6 @@
 package saml2aws
 
 import (
-	"io/ioutil"
 	"os"
 	"testing"
 
@@ -10,12 +9,9 @@ import (
 
 func TestUpdateSamlConfig(t *testing.T) {
 
-	err := ioutil.WriteFile(".credentials", []byte("[saml]"), 0666)
-	assert.Nil(t, err)
-
 	sharedCreds := &CredentialsProvider{".credentials", "saml"}
 
-	err = sharedCreds.Save("testid", "testsecret", "testtoken")
+	err := sharedCreds.Save("testid", "testsecret", "testtoken")
 	assert.Nil(t, err)
 
 	os.Remove(".credentials")