Skip to content

Commit

Permalink
aws/defaults: Export Shared Config/Creds filename helpers (#1308)
Browse files Browse the repository at this point in the history
Exports the shared credentials and shared config filename helper functions to make it easier to get the filename of the credentials and config file.

Corrects a bug in the shared credentials and config HOME path that would return an invalid filename on windows if the HOME environment variable was defined.

Replaces #1293
  • Loading branch information
jasdel authored Jun 1, 2017
1 parent ef136b8 commit 5f43689
Show file tree
Hide file tree
Showing 13 changed files with 310 additions and 151 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
### SDK Features

### SDK Enhancements
* `aws/defaults`: Exports shared credentials and config default filenames used by the SDK. [#1308](https://github.com/aws/aws-sdk-go/pull/1308)
* Adds SharedCredentialsFilename and SharedConfigFilename functions to defaults package.

### SDK Bugs
* `aws/credentials`: Fixes shared credential provider's default filename on Windows. [#1308](https://github.com/aws/aws-sdk-go/pull/1308)
* The shared credentials provider would attempt to use the wrong filename on Windows if the `HOME` environment variable was defined.
33 changes: 16 additions & 17 deletions aws/credentials/shared_credentials_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@ package credentials
import (
"fmt"
"os"
"path/filepath"

"github.com/go-ini/ini"

"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/internal/shareddefaults"

This comment has been minimized.

Copy link
@rafaeljusto

rafaeljusto Jul 30, 2017

After adding this internal dependency I started receiving the message bellow when importing (vendor) the aws-sdk-go into my project.

vendor/github.com/aws/aws-sdk-go/aws/credentials/shared_credentials_provider.go:10:2: use of internal package not allowed

This comment has been minimized.

Copy link
@jasdel

jasdel Jul 31, 2017

Author Contributor

@rafaeljusto What vendoring tool do you use? It was my understanding most should be compatible with libraries using the internal folder. Does the tool rewrite the import paths?

This comment has been minimized.

Copy link
@rafaeljusto

rafaeljusto Aug 1, 2017

@jasdel The project use govendor, and it doesn't rewrite the import paths. I'm current trying to create a simple case to reproduce this problem, but curiously it doesn't happen with a small example. Let me find out exactly how this scenario was caused and I reach back here with the results.

This comment has been minimized.

Copy link
@jasdel

jasdel Aug 1, 2017

Author Contributor

Thanks for the update. Please open a github issue if you're able to reproduce the issue. One thing that could potentially be causing this is your Go workspace's pkg cache. We've seen issues where the pkg folder's cache can get stale and doesn't update properly. You could try emptying the $GOPATH/pkg folder to see if that helps.

This comment has been minimized.

Copy link
@rafaeljusto

rafaeljusto Aug 1, 2017

Clearing the $GOPATH/pkg works! Thanks for the advice. 😄

This comment has been minimized.

Copy link
@jasdel

jasdel Aug 1, 2017

Author Contributor

Great! glad that worked. let us know if you run into any additional issues, questions, or feedback.

)

// SharedCredsProviderName provides a name of SharedCreds provider
const SharedCredsProviderName = "SharedCredentialsProvider"

var (
// ErrSharedCredentialsHomeNotFound is emitted when the user directory cannot be found.
//
// @readonly
ErrSharedCredentialsHomeNotFound = awserr.New("UserHomeNotFound", "user home directory not found.", nil)
)

Expand Down Expand Up @@ -117,22 +115,23 @@ func loadProfile(filename, profile string) (Value, error) {
//
// Will return an error if the user's home directory path cannot be found.
func (p *SharedCredentialsProvider) filename() (string, error) {
if p.Filename == "" {
if p.Filename = os.Getenv("AWS_SHARED_CREDENTIALS_FILE"); p.Filename != "" {
return p.Filename, nil
}

homeDir := os.Getenv("HOME") // *nix
if homeDir == "" { // Windows
homeDir = os.Getenv("USERPROFILE")
}
if homeDir == "" {
return "", ErrSharedCredentialsHomeNotFound
}

p.Filename = filepath.Join(homeDir, ".aws", "credentials")
if len(p.Filename) != 0 {
return p.Filename, nil
}

if p.Filename = os.Getenv("AWS_SHARED_CREDENTIALS_FILE"); len(p.Filename) != 0 {
return p.Filename, nil
}

if home := shareddefaults.UserHomeDir(); len(home) == 0 {
// Backwards compatibility of home directly not found error being returned.
// This error is too verbose, failure when opening the file would of been
// a better error to return.
return "", ErrSharedCredentialsHomeNotFound
}

p.Filename = shareddefaults.SharedConfigFilename()

return p.Filename, nil
}

Expand Down
27 changes: 27 additions & 0 deletions aws/defaults/shared_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package defaults

import (
"github.com/aws/aws-sdk-go/internal/shareddefaults"
)

// SharedCredentialsFilename returns the SDK's default file path
// for the shared credentials file.
//
// Builds the shared config file path based on the OS's platform.
//
// - Linux/Unix: $HOME/.aws/credentials
// - Windows: %USERPROFILE%\.aws\credentials
func SharedCredentialsFilename() string {
return shareddefaults.SharedCredentialsFilename()
}

// SharedConfigFilename returns the SDK's default file path for
// the shared config file.
//
// Builds the shared config file path based on the OS's platform.
//
// - Linux/Unix: $HOME/.aws/config
// - Windows: %USERPROFILE%\.aws\config
func SharedConfigFilename() string {
return shareddefaults.SharedConfigFilename()
}
12 changes: 6 additions & 6 deletions aws/session/custom_ca_bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestMain(m *testing.M) {

func TestNewSession_WithCustomCABundle_Env(t *testing.T) {
oldEnv := initSessionTestEnv()
defer popEnv(oldEnv)
defer awstesting.PopEnv(oldEnv)

endpoint, err := awstesting.CreateTLSServer(TLSBundleCertFile, TLSBundleKeyFile, nil)
if err != nil {
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestNewSession_WithCustomCABundle_Env(t *testing.T) {

func TestNewSession_WithCustomCABundle_EnvNotExists(t *testing.T) {
oldEnv := initSessionTestEnv()
defer popEnv(oldEnv)
defer awstesting.PopEnv(oldEnv)

os.Setenv("AWS_CA_BUNDLE", "file-not-exists")

Expand All @@ -94,7 +94,7 @@ func TestNewSession_WithCustomCABundle_EnvNotExists(t *testing.T) {

func TestNewSession_WithCustomCABundle_Option(t *testing.T) {
oldEnv := initSessionTestEnv()
defer popEnv(oldEnv)
defer awstesting.PopEnv(oldEnv)

endpoint, err := awstesting.CreateTLSServer(TLSBundleCertFile, TLSBundleKeyFile, nil)
if err != nil {
Expand Down Expand Up @@ -129,7 +129,7 @@ func TestNewSession_WithCustomCABundle_Option(t *testing.T) {

func TestNewSession_WithCustomCABundle_OptionPriority(t *testing.T) {
oldEnv := initSessionTestEnv()
defer popEnv(oldEnv)
defer awstesting.PopEnv(oldEnv)

endpoint, err := awstesting.CreateTLSServer(TLSBundleCertFile, TLSBundleKeyFile, nil)
if err != nil {
Expand Down Expand Up @@ -172,7 +172,7 @@ func (m *mockRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {

func TestNewSession_WithCustomCABundle_UnsupportedTransport(t *testing.T) {
oldEnv := initSessionTestEnv()
defer popEnv(oldEnv)
defer awstesting.PopEnv(oldEnv)

s, err := NewSessionWithOptions(Options{
Config: aws.Config{
Expand All @@ -199,7 +199,7 @@ func TestNewSession_WithCustomCABundle_UnsupportedTransport(t *testing.T) {

func TestNewSession_WithCustomCABundle_TransportSet(t *testing.T) {
oldEnv := initSessionTestEnv()
defer popEnv(oldEnv)
defer awstesting.PopEnv(oldEnv)

endpoint, err := awstesting.CreateTLSServer(TLSBundleCertFile, TLSBundleKeyFile, nil)
if err != nil {
Expand Down
36 changes: 8 additions & 28 deletions aws/session/env_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package session

import (
"os"
"path/filepath"
"strconv"

"github.com/aws/aws-sdk-go/aws/credentials"
Expand Down Expand Up @@ -116,6 +115,12 @@ var (
"AWS_PROFILE",
"AWS_DEFAULT_PROFILE", // Only read if AWS_SDK_LOAD_CONFIG is also set
}
sharedCredsFileEnvKey = []string{
"AWS_SHARED_CREDENTIALS_FILE",
}
sharedConfigFileEnvKey = []string{
"AWS_CONFIG_FILE",
}
)

// loadEnvConfig retrieves the SDK's environment configuration.
Expand Down Expand Up @@ -165,8 +170,8 @@ func envConfigLoad(enableSharedConfig bool) envConfig {
setFromEnvVal(&cfg.Region, regionKeys)
setFromEnvVal(&cfg.Profile, profileKeys)

cfg.SharedCredentialsFile = sharedCredentialsFilename()
cfg.SharedConfigFile = sharedConfigFilename()
setFromEnvVal(&cfg.SharedCredentialsFile, sharedCredsFileEnvKey)
setFromEnvVal(&cfg.SharedConfigFile, sharedConfigFileEnvKey)

cfg.CustomCABundle = os.Getenv("AWS_CA_BUNDLE")

Expand All @@ -181,28 +186,3 @@ func setFromEnvVal(dst *string, keys []string) {
}
}
}

func sharedCredentialsFilename() string {
if name := os.Getenv("AWS_SHARED_CREDENTIALS_FILE"); len(name) > 0 {
return name
}

return filepath.Join(userHomeDir(), ".aws", "credentials")
}

func sharedConfigFilename() string {
if name := os.Getenv("AWS_CONFIG_FILE"); len(name) > 0 {
return name
}

return filepath.Join(userHomeDir(), ".aws", "config")
}

func userHomeDir() string {
homeDir := os.Getenv("HOME") // *nix
if len(homeDir) == 0 { // windows
homeDir = os.Getenv("USERPROFILE")
}

return homeDir
}
Loading

0 comments on commit 5f43689

Please sign in to comment.