Skip to content

Commit

Permalink
aws/credential: Add credential_process provider (#2217)
Browse files Browse the repository at this point in the history
Adds support for the shared configuration file's `credential_process` property. This property allows the application to execute a command in order to retrieve AWS credentials for AWS service API request.  In order to use this feature your application must enable the SDK's support of the shared configuration file. See, https://docs.aws.amazon.com/sdk-for-go/api/aws/session/#hdr-Sessions_from_Shared_Config for more information on enabling shared config support.

Fixes #1834
  • Loading branch information
YakDriver authored and jasdel committed Dec 4, 2018
1 parent c0dd483 commit 275272f
Show file tree
Hide file tree
Showing 16 changed files with 1,077 additions and 0 deletions.
425 changes: 425 additions & 0 deletions aws/credentials/processcreds/provider.go

Large diffs are not rendered by default.

561 changes: 561 additions & 0 deletions aws/credentials/processcreds/provider_test.go

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions aws/credentials/processcreds/testdata/expired.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"Version": 1,
"AccessKeyId": "accessKey",
"SecretAccessKey": "secret",
"SessionToken": "tokenDefault",
"Expiration": "2000-01-01T00:00:00-00:00"
}
2 changes: 2 additions & 0 deletions aws/credentials/processcreds/testdata/malformed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
"Version": 1
4 changes: 4 additions & 0 deletions aws/credentials/processcreds/testdata/missingkey.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"Version": 1,
"AccessKeyId": "accesskey"
}
4 changes: 4 additions & 0 deletions aws/credentials/processcreds/testdata/missingsecret.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"Version": 1,
"SecretAccessKey": "secretkey"
}
6 changes: 6 additions & 0 deletions aws/credentials/processcreds/testdata/nonexpire.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"Version": 1,
"AccessKeyId": "accessKey",
"SecretAccessKey": "secret",
"SessionToken": "nonDefaultToken"
}
10 changes: 10 additions & 0 deletions aws/credentials/processcreds/testdata/shconfig.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[default]
credential_process = cat ./testdata/expired.json

[profile non_expire]
credential_process = cat ./testdata/nonexpire.json

[profile not_alone]
aws_access_key_id = notFromCredProcAccess
aws_secret_access_key = notFromCredProcSecret
credential_process = cat ./testdata/verybad.json
10 changes: 10 additions & 0 deletions aws/credentials/processcreds/testdata/shconfig_win.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[default]
credential_process = type .\testdata\expired.json

[profile non_expire]
credential_process = type .\testdata\nonexpire.json

[profile not_alone]
aws_access_key_id = notFromCredProcAccess
aws_secret_access_key = notFromCredProcSecret
credential_process = type .\testdata\verybad.json
10 changes: 10 additions & 0 deletions aws/credentials/processcreds/testdata/shcred.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[default]
credential_process = cat ./testdata/expired.json

[non_expire]
credential_process = cat ./testdata/nonexpire.json

[not_alone]
aws_access_key_id = notFromCredProcAccess
aws_secret_access_key = notFromCredProcSecret
credential_process = cat ./testdata/verybad.json
10 changes: 10 additions & 0 deletions aws/credentials/processcreds/testdata/shcred_win.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[default]
credential_process = type .\testdata\expired.json

[non_expire]
credential_process = type .\testdata\nonexpire.json

[not_alone]
aws_access_key_id = notFromCredProcAccess
aws_secret_access_key = notFromCredProcSecret
credential_process = type .\testdata\verybad.json
5 changes: 5 additions & 0 deletions aws/credentials/processcreds/testdata/static.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"Version":1,
"AccessKeyId":"accesskey",
"SecretAccessKey":"secretkey"
}
5 changes: 5 additions & 0 deletions aws/credentials/processcreds/testdata/verybad.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"Version":1,
"AccessKeyId":"veryBadAccessKeyID",
"SecretAccessKey":"veryBadSecretAccessKey"
}
3 changes: 3 additions & 0 deletions aws/credentials/processcreds/testdata/wrongversion.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"Version": 2
}
5 changes: 5 additions & 0 deletions aws/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/aws/aws-sdk-go/aws/client"
"github.com/aws/aws-sdk-go/aws/corehandlers"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/processcreds"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/csm"
"github.com/aws/aws-sdk-go/aws/defaults"
Expand Down Expand Up @@ -534,6 +535,10 @@ func mergeConfigSrcs(cfg, userCfg *aws.Config, envCfg envConfig, sharedCfg share
cfg.Credentials = credentials.NewStaticCredentialsFromCreds(
sharedCfg.Creds,
)
} else if len(sharedCfg.CredentialProcess) > 0 {
cfg.Credentials = processcreds.NewCredentials(
sharedCfg.CredentialProcess,
)
} else {
// Fallback to default credentials provider, include mock errors
// for the credential chain so user can identify why credentials
Expand Down
10 changes: 10 additions & 0 deletions aws/session/shared_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const (

// endpoint discovery group
enableEndpointDiscoveryKey = `endpoint_discovery_enabled` // optional
// External Credential Process
credentialProcessKey = `credential_process`

// DefaultSharedConfigProfile is the default profile to be used when
// loading configuration from the config files if another profile name
Expand Down Expand Up @@ -60,6 +62,9 @@ type sharedConfig struct {
AssumeRole assumeRoleConfig
AssumeRoleSource *sharedConfig

// An external process to request credentials
CredentialProcess string

// Region is the region the SDK should use for looking up AWS service endpoints
// and signing requests.
//
Expand Down Expand Up @@ -223,6 +228,11 @@ func (cfg *sharedConfig) setFromIniFile(profile string, file sharedConfigFile) e
}
}

// `credential_process`
if credProc := section.String(credentialProcessKey); len(credProc) > 0 {
cfg.CredentialProcess = credProc
}

// Region
if v := section.String(regionKey); len(v) > 0 {
cfg.Region = v
Expand Down

0 comments on commit 275272f

Please sign in to comment.