Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

private/model/api: Generating useful examples #1309

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
)

// 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.SharedCredentialsFilename()

return p.Filename, nil
}

Expand Down
20 changes: 20 additions & 0 deletions aws/credentials/shared_credentials_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"path/filepath"
"testing"

"github.com/aws/aws-sdk-go/internal/shareddefaults"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -97,6 +98,25 @@ func TestSharedCredentialsProviderColonInCredFile(t *testing.T) {
assert.Empty(t, creds.SessionToken, "Expect no token")
}

func TestSharedCredentialsProvider_DefaultFilename(t *testing.T) {
os.Clearenv()
os.Setenv("USERPROFILE", "profile_dir")
os.Setenv("HOME", "home_dir")

// default filename and profile
p := SharedCredentialsProvider{}

filename, err := p.filename()

if err != nil {
t.Fatalf("expect no error, got %v", err)
}

if e, a := shareddefaults.SharedCredentialsFilename(), filename; e != a {
t.Errorf("expect %q filename, got %q", e, a)
}
}

func BenchmarkSharedCredentialsProvider(b *testing.B) {
os.Clearenv()

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()
}
48 changes: 38 additions & 10 deletions aws/endpoints/defaults.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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