Skip to content

Commit

Permalink
Allow fetching credentials from secretsmanager
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Duchesne committed Jun 21, 2019
1 parent 1357b7d commit 20d1651
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
12 changes: 8 additions & 4 deletions credentials/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,17 @@ func (credBase *Base) BaseValidate() bool {
if credBase.ID == "" {
log.Errorf("Credentials (%s) has no defined ID", credBase.BaseToString())
}
if credBase.Description == "" {
log.Errorf("Credentials (%s) has no defined description", credBase.ID)
}
if credBase.CredType == "" {
log.Errorf("Credentials (%s) has no type. This is probably a bug in the software", credBase.ID)
}
return credBase.ID != "" && credBase.Description != "" && credBase.CredType != ""
return credBase.ID != "" && credBase.CredType != ""
}

func (credBase *Base) GetDescriptionOrID() string {
if credBase.Description == "" {
return credBase.ID
}
return credBase.Description
}

// GetID returns a credentials' ID
Expand Down
6 changes: 4 additions & 2 deletions credentials/source_secretsmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package credentials

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/secretsmanager"
)
Expand All @@ -12,7 +13,8 @@ type AWSSecretsManagerSource struct {

func (source *AWSSecretsManagerSource) Credentials() ([]Credentials, error) {
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
SharedConfigState: session.SharedConfigEnable,
AssumeRoleTokenProvider: stscreds.StdinTokenProvider,
}))
client := secretsmanager.New(sess)
value, err := client.GetSecretValue(&secretsmanager.GetSecretValueInput{
Expand All @@ -21,7 +23,7 @@ func (source *AWSSecretsManagerSource) Credentials() ([]Credentials, error) {
if err != nil {
return nil, err
}
return getCredentialsFromBytes(value.SecretBinary)
return getCredentialsFromBytes([]byte(*value.SecretString))
}

func (source *AWSSecretsManagerSource) Type() string {
Expand Down
3 changes: 3 additions & 0 deletions credentials/sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ func (sc *SourcesConfiguration) AllSources() []Source {
for _, source := range sc.AWSS3Sources {
sources = append(sources, source)
}
for _, source := range sc.AWSSecretsManagerSource {
sources = append(sources, source)
}
for _, source := range sc.AWSSSMSources {
sources = append(sources, source)
}
Expand Down
2 changes: 1 addition & 1 deletion sync/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (config *Configuration) Sync() {
// Start reading credentials
creds, err := config.Sources.Credentials()
if err != nil {
log.Fatalf("Caught an error while fetching credentials")
log.Fatalf("Caught an error while fetching credentials: %v", err)
}

// Initialize targets
Expand Down
4 changes: 2 additions & 2 deletions targets/jenkins.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ func toJenkinsCredential(creds credentials.Credentials) interface{} {
castCreds := creds.(*credentials.SecretTextCredentials)
return &gojenkins.StringCredentials{
ID: creds.GetID(),
Description: castCreds.Description,
Description: castCreds.GetDescriptionOrID(),
Secret: castCreds.Secret,
}
case *credentials.UsernamePasswordCredentials:
castCreds := creds.(*credentials.UsernamePasswordCredentials)
return &gojenkins.UsernameCredentials{
ID: castCreds.GetID(),
Description: castCreds.Description,
Description: castCreds.GetDescriptionOrID(),
Username: castCreds.Username,
Password: castCreds.Password,
}
Expand Down

0 comments on commit 20d1651

Please sign in to comment.