From d77601cec4664c152267bdbe6fe76b4bfd2a754a Mon Sep 17 00:00:00 2001 From: xibz Date: Tue, 16 Oct 2018 13:40:27 -0700 Subject: [PATCH] Adding Has method and updating shared cred provider to use new ini package --- .../shared_credentials_provider.go | 30 +++++++++---------- internal/ini/visitor.go | 6 ++++ 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/aws/credentials/shared_credentials_provider.go b/aws/credentials/shared_credentials_provider.go index 51e21e0f38f..e1551495812 100644 --- a/aws/credentials/shared_credentials_provider.go +++ b/aws/credentials/shared_credentials_provider.go @@ -4,9 +4,8 @@ import ( "fmt" "os" - "github.com/go-ini/ini" - "github.com/aws/aws-sdk-go/aws/awserr" + "github.com/aws/aws-sdk-go/internal/ini" "github.com/aws/aws-sdk-go/internal/shareddefaults" ) @@ -77,36 +76,37 @@ func (p *SharedCredentialsProvider) IsExpired() bool { // The credentials retrieved from the profile will be returned or error. Error will be // returned if it fails to read from the file, or the data is invalid. func loadProfile(filename, profile string) (Value, error) { - config, err := ini.Load(filename) + config, err := ini.OpenFile(filename) if err != nil { return Value{ProviderName: SharedCredsProviderName}, awserr.New("SharedCredsLoad", "failed to load shared credentials file", err) } - iniProfile, err := config.GetSection(profile) - if err != nil { - return Value{ProviderName: SharedCredsProviderName}, awserr.New("SharedCredsLoad", "failed to get profile", err) + + iniProfile, ok := config.GetSection(profile) + if !ok { + return Value{ProviderName: SharedCredsProviderName}, awserr.New("SharedCredsLoad", "failed to get profile", nil) } - id, err := iniProfile.GetKey("aws_access_key_id") - if err != nil { + id := iniProfile.String("aws_access_key_id") + if len(id) == 0 { return Value{ProviderName: SharedCredsProviderName}, awserr.New("SharedCredsAccessKey", fmt.Sprintf("shared credentials %s in %s did not contain aws_access_key_id", profile, filename), - err) + nil) } - secret, err := iniProfile.GetKey("aws_secret_access_key") - if err != nil { + secret := iniProfile.String("aws_secret_access_key") + if len(secret) == 0 { return Value{ProviderName: SharedCredsProviderName}, awserr.New("SharedCredsSecret", fmt.Sprintf("shared credentials %s in %s did not contain aws_secret_access_key", profile, filename), nil) } // Default to empty string if not found - token := iniProfile.Key("aws_session_token") + token := iniProfile.String("aws_session_token") return Value{ - AccessKeyID: id.String(), - SecretAccessKey: secret.String(), - SessionToken: token.String(), + AccessKeyID: id, + SecretAccessKey: secret, + SessionToken: token, ProviderName: SharedCredsProviderName, }, nil } diff --git a/internal/ini/visitor.go b/internal/ini/visitor.go index c710170a069..99d4f19b2c7 100644 --- a/internal/ini/visitor.go +++ b/internal/ini/visitor.go @@ -128,6 +128,12 @@ type Section struct { values values } +// Has will return whether or not an entry exists in a given section +func (t Section) Has(k string) bool { + _, ok := t.values[k] + return ok +} + // ValueType will returned what type the union is set to. If // k was not found, the NoneType will be returned. func (t Section) ValueType(k string) (ValueType, bool) {