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

aws/session: Add Assume role for credential process from aws profile #2674

Merged
merged 26 commits into from
Jul 10, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
6fe148c
Added support for Credential Process to assume role for AWS-SDK-GO
Jun 28, 2019
9db3f0a
Added support for Credential Process to assume role for AWS-SDK-GO
Jun 28, 2019
4020e0d
Merge branch 'GoSDK-543' of github.com:skotambkar/aws-sdk-go into GoS…
Jun 28, 2019
f3b7a90
Modified config file to unable support for Windows as well as Linux.
Jun 28, 2019
d659cb3
code cleanup
Jun 28, 2019
f1658b4
Eliminated redundant code from credential process tests, formatting c…
Jul 1, 2019
0def7bf
Updated travis to support make for windows
Jul 1, 2019
8a9d14d
Updated travis configuration file to support unit test on Windows wit…
Jul 1, 2019
7a1f20e
Fixed syntax error in Travis config file
Jul 1, 2019
cb77c1e
resolved dependency issue for travis configuration
Jul 1, 2019
2ec081f
Updated environment stashing logic to preserve SYSTEMROOT settings fo…
Jul 1, 2019
44f511f
Added system root in environments to keep when stashing
Jul 2, 2019
0759f56
changes in stashing systemroot for windows
Jul 2, 2019
d9cb70f
removed additional print statements
Jul 2, 2019
b8555c4
Refactored code with sdktesting with platform specific config environ…
Jul 2, 2019
8150197
Fixed minor bugs for tests and config files
Jul 3, 2019
a0e6b4c
Fix for TestNoConnection retry bug on Windows OS
Jul 3, 2019
4e8dc55
Code cleanup, added unit test for request connection retry, custom ca…
Jul 3, 2019
f313908
Updated readme file for awstesting
Jul 3, 2019
a6e326a
Updated request retry test
Jul 3, 2019
3ea246b
updated test log for supporting Go version 1.5
Jul 3, 2019
2c88c3f
Fixed TestShouldRetry to solve nil pointer dereference issue with *ur…
Jul 3, 2019
a7dc165
code cleanup
Jul 8, 2019
3c5b464
Added suggested changes
Jul 9, 2019
26d07ad
Added suggested changes
Jul 9, 2019
2239254
Added case index number in t.Run() for env_config_test
Jul 9, 2019
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
14 changes: 9 additions & 5 deletions aws/session/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,15 @@ func resolveCredsFromProfile(cfg *aws.Config,
), nil

} else if len(sharedCfg.CredentialProcess) > 0 {
// Credential Process credentials from Shared Config/Credentials file.
return processcreds.NewCredentials(
sharedCfg.CredentialProcess,
), nil

//Get credentials from CredentialProcess
cred := processcreds.NewCredentials(sharedCfg.CredentialProcess)
//if RoleARN is provided, so the obtained cred from the Credential Process to assume the role using RoleARN
if len(sharedCfg.AssumeRole.RoleARN) > 0 {
cfgCp := *cfg
cfgCp.Credentials = cred
return credsFromAssumeRole(cfgCp, handlers, sharedCfg, sessOpts)
}
return cred, nil
} else if envCfg.EnableSharedConfig && len(sharedCfg.AssumeRole.CredentialSource) > 0 {
// Assume IAM Role with specific credential source.
return resolveCredsFromSource(cfg, envCfg, sharedCfg, handlers, sessOpts)
Expand Down
39 changes: 32 additions & 7 deletions aws/session/credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func setupCredentialsEndpoints(t *testing.T) (endpoints.Resolver, func()) {
w.Write([]byte(fmt.Sprintf(
assumeRoleRespMsg,
time.Now().
Add(15*time.Minute).
Add(15 * time.Minute).
Format("2006-01-02T15:04:05Z"))))
}))

Expand Down Expand Up @@ -145,12 +145,37 @@ func TestSharedConfigCredentialSource(t *testing.T) {
"assume_role_w_creds_role_arn_ec2",
},
},
{
name: "credential process with no ARN set",
profile: "cred_proc_no_arn_set",
expectedAccessKey: "accesskey",
expectedSecretKey: "secretkey",
},
{
name: "credential process with ARN set",
profile: "cred_proc_arn_set",
expectedAccessKey: "AKID",
expectedSecretKey: "SECRET",
expectedChain: [] string{
"assume_role_w_creds_proc_role_arn",
},
},
{
name: "chained assume role with credential process",
profile: "chained_cred_proc",
expectedAccessKey: "AKID",
expectedSecretKey: "SECRET",
expectedChain: [] string{
"assume_role_w_creds_proc_source_prof",
},
},
}

for i, c := range cases {
t.Run(fmt.Sprintf("%d %s", i, c.name),
func(t *testing.T) {
env := awstesting.StashEnv()
//Pass in arguments to retain certain environment variable values.
env := awstesting.StashEnv("PATH", "ComSpec", "SYSTEM32")
defer awstesting.PopEnv(env)

os.Setenv("AWS_REGION", "us-east-1")
Expand Down Expand Up @@ -262,7 +287,7 @@ func TestSessionAssumeRole(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(fmt.Sprintf(
assumeRoleRespMsg,
time.Now().Add(15*time.Minute).Format("2006-01-02T15:04:05Z"))))
time.Now().Add(15 * time.Minute).Format("2006-01-02T15:04:05Z"))))
}))

s, err := NewSession(&aws.Config{
Expand Down Expand Up @@ -310,7 +335,7 @@ func TestSessionAssumeRole_WithMFA(t *testing.T) {

w.Write([]byte(fmt.Sprintf(
assumeRoleRespMsg,
time.Now().Add(15*time.Minute).Format("2006-01-02T15:04:05Z"))))
time.Now().Add(15 * time.Minute).Format("2006-01-02T15:04:05Z"))))
}))

customProviderCalled := false
Expand Down Expand Up @@ -440,7 +465,7 @@ func TestSessionAssumeRole_ExtendedDuration(t *testing.T) {

w.Write([]byte(fmt.Sprintf(
assumeRoleRespMsg,
time.Now().Add(15*time.Minute).Format("2006-01-02T15:04:05Z"))))
time.Now().Add(15 * time.Minute).Format("2006-01-02T15:04:05Z"))))
}))

s, err := NewSessionWithOptions(Options{
Expand Down Expand Up @@ -493,7 +518,7 @@ func TestSessionAssumeRole_WithMFA_ExtendedDuration(t *testing.T) {

w.Write([]byte(fmt.Sprintf(
assumeRoleRespMsg,
time.Now().Add(30*time.Minute).Format("2006-01-02T15:04:05Z"))))
time.Now().Add(30 * time.Minute).Format("2006-01-02T15:04:05Z"))))
}))

customProviderCalled := false
Expand Down Expand Up @@ -536,4 +561,4 @@ func TestSessionAssumeRole_WithMFA_ExtendedDuration(t *testing.T) {
if e, a := "AssumeRoleProvider", creds.ProviderName; !strings.Contains(a, e) {
t.Errorf("expect %v, to contain %v", e, a)
}
}
}
7 changes: 5 additions & 2 deletions aws/session/shared_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ func (cfg *sharedConfig) setAssumeRoleSource(origProfile string, files []sharedC
}

if cfg.AssumeRole.SourceProfile == origProfile || len(assumeRoleSrc.AssumeRole.SourceProfile) == 0 {
if len(assumeRoleSrc.AssumeRole.CredentialSource) == 0 && len(assumeRoleSrc.Creds.AccessKeyID) == 0 {
//Check if at least either Credential Source, static creds, or credential process is set to retain credentials.
if len(assumeRoleSrc.AssumeRole.CredentialSource) == 0 && len(assumeRoleSrc.Creds.AccessKeyID) == 0 && len(assumeRoleSrc.CredentialProcess) == 0{
return SharedConfigAssumeRoleError{RoleARN: cfg.AssumeRole.RoleARN}
}
}
Expand Down Expand Up @@ -226,7 +227,9 @@ func (cfg *sharedConfig) setFromIniFile(profile string, file sharedConfigFile) e
roleArn := section.String(roleArnKey)
srcProfile := section.String(sourceProfileKey)
credentialSource := section.String(credentialSourceKey)
hasSource := len(srcProfile) > 0 || len(credentialSource) > 0
credentialProcess := section.String(credentialProcessKey)
//Has source to make sure the Assume Role has at least either srcProfile, credential Source, or credential Process.
hasSource := len(srcProfile) > 0 || len(credentialSource) > 0 || len(credentialProcess) >0
if len(roleArn) > 0 && hasSource {
cfg.AssumeRole = assumeRoleConfig{
RoleARN: roleArn,
Expand Down
13 changes: 13 additions & 0 deletions aws/session/testdata/credential_source_config
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,16 @@ credential_source = EcsContainer
[chained_assume_role]
role_arn = assume_role_w_creds_role_arn_chain
source_profile = ec2metadata

[cred_proc_no_arn_set]
credential_process = echo "{\"Version\": 1, \"AccessKeyId\": \"accesskey\", \"SecretAccessKey\": \"secretkey\"}"

[cred_proc_arn_set]
role_arn = assume_role_w_creds_proc_role_arn
credential_process = echo "{\"Version\": 1, \"AccessKeyId\": \"accesskey\", \"SecretAccessKey\": \"secretkey\"}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will want to test this against windows to ensure tests work.


[chained_cred_proc]
role_arn = assume_role_w_creds_proc_source_prof
source_profile = cred_proc_no_arn_set


29 changes: 23 additions & 6 deletions awstesting/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,30 @@ func (c *FakeContext) Value(key interface{}) interface{} {
return nil
}

// StashEnv stashes the current environment variables and returns an array of
// all environment values as key=val strings.
func StashEnv() []string {
env := os.Environ()
os.Clearenv()
// StashEnv stashes the current environment variables except variables listed in envToKeep
// Returns an array of all environment values as key=val strings.
func StashEnv(envToKeep ...string) []string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest updating aws/credentials/processcreds test, preserveImportantStashEnv function to use this updated utility.


extraEnv := getEnvs(envToKeep)

originalEnv := os.Environ()
os.Clearenv() //clear env

for key, val := range extraEnv {
os.Setenv(key, val)
}

return env
return originalEnv
}

func getEnvs(envs []string) map[string]string {
extraEnvs := make(map[string]string)
for _, env := range envs {
if val, ok := os.LookupEnv(env); ok && len(val) > 0 {
extraEnvs[env] = val
}
}
return extraEnvs
}

// PopEnv takes the list of the environment values and injects them into the
Expand Down