-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into container-insights-sll
- Loading branch information
Showing
105 changed files
with
2,413 additions
and
4,637 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package aws | ||
|
||
import ( | ||
"os" | ||
"os/user" | ||
"path/filepath" | ||
"strconv" | ||
) | ||
|
||
const ( | ||
envAwsSdkLoadConfig = "AWS_SDK_LOAD_CONFIG" | ||
envAwsSharedCredentialsFile = "AWS_SHARED_CREDENTIALS_FILE" | ||
envAwsSharedConfigFile = "AWS_CONFIG_FILE" | ||
) | ||
|
||
// getFallbackSharedConfigFiles follows the same logic as the AWS SDK but takes a getUserHomeDir | ||
// function. | ||
func getFallbackSharedConfigFiles(userHomeDirProvider func() string) []string { | ||
var sharedCredentialsFile, sharedConfigFile string | ||
setFromEnvVal(&sharedCredentialsFile, envAwsSharedCredentialsFile) | ||
setFromEnvVal(&sharedConfigFile, envAwsSharedConfigFile) | ||
if sharedCredentialsFile == "" { | ||
sharedCredentialsFile = defaultSharedCredentialsFile(userHomeDirProvider()) | ||
} | ||
if sharedConfigFile == "" { | ||
sharedConfigFile = defaultSharedConfig(userHomeDirProvider()) | ||
} | ||
var cfgFiles []string | ||
enableSharedConfig, _ := strconv.ParseBool(os.Getenv(envAwsSdkLoadConfig)) | ||
if enableSharedConfig { | ||
cfgFiles = append(cfgFiles, sharedConfigFile) | ||
} | ||
return append(cfgFiles, sharedCredentialsFile) | ||
} | ||
|
||
func setFromEnvVal(dst *string, keys ...string) { | ||
for _, k := range keys { | ||
if v := os.Getenv(k); len(v) != 0 { | ||
*dst = v | ||
break | ||
} | ||
} | ||
} | ||
|
||
func defaultSharedCredentialsFile(dir string) string { | ||
return filepath.Join(dir, ".aws", "credentials") | ||
} | ||
|
||
func defaultSharedConfig(dir string) string { | ||
return filepath.Join(dir, ".aws", "config") | ||
} | ||
|
||
// backwardsCompatibleUserHomeDir provides the home directory based on | ||
// environment variables. | ||
// | ||
// Based on v1.44.106 of the AWS SDK. | ||
func backwardsCompatibleUserHomeDir() string { | ||
home, _ := os.UserHomeDir() | ||
return home | ||
} | ||
|
||
// currentUserHomeDir attempts to use the environment variables before falling | ||
// back on the current user's home directory. | ||
// | ||
// Based on v1.44.332 of the AWS SDK. | ||
func currentUserHomeDir() string { | ||
var home string | ||
|
||
home = backwardsCompatibleUserHomeDir() | ||
if len(home) > 0 { | ||
return home | ||
} | ||
|
||
currUser, _ := user.Current() | ||
if currUser != nil { | ||
home = currUser.HomeDir | ||
} | ||
|
||
return home | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package aws | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetFallbackSharedConfigFiles(t *testing.T) { | ||
noOpGetUserHomeDir := func() string { return "home" } | ||
t.Setenv(envAwsSdkLoadConfig, "true") | ||
t.Setenv(envAwsSharedCredentialsFile, "credentials") | ||
t.Setenv(envAwsSharedConfigFile, "config") | ||
|
||
got := getFallbackSharedConfigFiles(noOpGetUserHomeDir) | ||
assert.Equal(t, []string{"config", "credentials"}, got) | ||
|
||
t.Setenv(envAwsSdkLoadConfig, "false") | ||
got = getFallbackSharedConfigFiles(noOpGetUserHomeDir) | ||
assert.Equal(t, []string{"credentials"}, got) | ||
|
||
t.Setenv(envAwsSdkLoadConfig, "true") | ||
t.Setenv(envAwsSharedCredentialsFile, "") | ||
t.Setenv(envAwsSharedConfigFile, "") | ||
|
||
got = getFallbackSharedConfigFiles(noOpGetUserHomeDir) | ||
assert.Equal(t, []string{defaultSharedConfig("home"), defaultSharedCredentialsFile("home")}, got) | ||
} |
Oops, something went wrong.