-
Notifications
You must be signed in to change notification settings - Fork 17
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
Draft implementation for Issue #7 with aws secret manager #16
Merged
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
79570ad
Draft the codes of read credential from aws secret manager
cafeliker c50d5f6
Add more debug logs
cafeliker 0237b98
Resolve conflict
cafeliker dccbb10
Fix a typo
cafeliker 1804790
Apply the factory pattern to load the credential
cafeliker 8cc17c5
fix import cycle
cafeliker ef81060
cleanup import
cafeliker c6e1556
Fix typo
cafeliker 222e00b
Fix the credential factory usage
cafeliker 95da945
Change the method name to LoadFromCredentialStore
cafeliker 527fce3
Enable reading credential from aws secret manager
cafeliker c7ec090
Merge branch 'issue7-aws-secret-manager' of https://github.com/cafeli…
cafeliker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package credential | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/secretsmanager" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type AWSCredenitalLoader struct{} | ||
|
||
func (c *AWSCredenitalLoader) LoadFromCredentialStore(passwordConfig string) string { | ||
if strings.HasPrefix(passwordConfig, "aws:secretmanager") { | ||
log.Debug("Start to load password from AWS Secret Manager") | ||
value := getAWSSecret(passwordConfig) | ||
if value != "" { | ||
return value | ||
} | ||
} | ||
return passwordConfig | ||
} | ||
|
||
func getAWSSecret(configValue string) string { | ||
// The expected format is aws:secretmanager:<region>:<secret name>:<secret key> | ||
fileds := strings.Split(configValue, ":") | ||
region, name, key := fileds[2], fileds[3], fileds[4] | ||
|
||
log.WithFields(log.Fields{"region": region, "name": name, "key": key}).Debug("pass in secret manager parameters") | ||
|
||
//Create a Secrets Manager client | ||
svc := secretsmanager.New(session.New(), &aws.Config{Region: aws.String(region)}) | ||
input := &secretsmanager.GetSecretValueInput{ | ||
SecretId: aws.String(name), | ||
VersionStage: aws.String("AWSCURRENT"), // VersionStage defaults to AWSCURRENT if unspecified | ||
} | ||
|
||
result, err := svc.GetSecretValue(input) | ||
if err != nil { | ||
if aerr, ok := err.(awserr.Error); ok { | ||
switch aerr.Code() { | ||
case secretsmanager.ErrCodeDecryptionFailure: | ||
// Secrets Manager can't decrypt the protected secret text using the provided KMS key. | ||
fmt.Println(secretsmanager.ErrCodeDecryptionFailure, aerr.Error()) | ||
|
||
case secretsmanager.ErrCodeInternalServiceError: | ||
// An error occurred on the server side. | ||
fmt.Println(secretsmanager.ErrCodeInternalServiceError, aerr.Error()) | ||
|
||
case secretsmanager.ErrCodeInvalidParameterException: | ||
// You provided an invalid value for a parameter. | ||
fmt.Println(secretsmanager.ErrCodeInvalidParameterException, aerr.Error()) | ||
|
||
case secretsmanager.ErrCodeInvalidRequestException: | ||
// You provided a parameter value that is not valid for the current state of the resource. | ||
fmt.Println(secretsmanager.ErrCodeInvalidRequestException, aerr.Error()) | ||
|
||
case secretsmanager.ErrCodeResourceNotFoundException: | ||
// We can't find the resource that you asked for. | ||
fmt.Println(secretsmanager.ErrCodeResourceNotFoundException, aerr.Error()) | ||
} | ||
} else { | ||
// Print the error, cast err to awserr.Error to get the Code and | ||
// Message from an error. | ||
fmt.Println(err.Error()) | ||
} | ||
} else { | ||
// Decrypts secret using the associated KMS CMK. | ||
var secretString string | ||
if result.SecretString != nil { | ||
secretString = *result.SecretString | ||
// a map container to decode the JSON structure into | ||
kmap := make(map[string]string) | ||
json.Unmarshal([]byte(secretString), &kmap) | ||
return kmap[key] | ||
} | ||
} | ||
|
||
return "" | ||
} |
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,7 @@ | ||
package credential | ||
|
||
type DefaultCredenitalLoader struct{} | ||
|
||
func (c *DefaultCredenitalLoader) LoadFromCredentialStore(passwordConfig string) string { | ||
return passwordConfig | ||
} |
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,16 @@ | ||
package credential | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
type CredentialLoader interface { | ||
LoadFromCredentialStore(passwordConfig string) string | ||
} | ||
|
||
func CreateCredentialLoader(passwordConfig string) CredentialLoader { | ||
if strings.HasPrefix(passwordConfig, "aws:secretmanager") { | ||
return &AWSCredenitalLoader{} | ||
} | ||
return &DefaultCredenitalLoader{} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to make the secret/cred configuration more explicit rather than an encoded value in the password, but I think we can start with this as a draft and follow-up with another commit to change that behavior before a release is pushed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, we can talk after Feb 3 as I will be in Chinese new year holiday from next week. I'd like to hear more ideas from you ;-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have a great new year holiday! We'll sync up when you return.