-
Notifications
You must be signed in to change notification settings - Fork 30
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
Added Secret Manager support #208
Changes from 2 commits
cd79e35
b869db3
2d09174
c9163eb
076c320
41ca136
fd0e76b
1fe0809
e67eb0a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,10 @@ import ( | |
"os" | ||
"strconv" | ||
"strings" | ||
"github.com/aws/aws-sdk-go/service/secretsmanager" | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"encoding/base64" | ||
) | ||
|
||
type extensionConfig struct { | ||
|
@@ -62,6 +66,39 @@ func getIntFromEnv(name string) (int, error) { | |
return value, nil | ||
} | ||
|
||
func getSecret(secretName string) (string, error) { | ||
region := os.Getenv("AWS_REGION") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. might want to check region != "" and return a descriptive error |
||
|
||
sess, err := session.NewSession() | ||
if err != nil { | ||
return "", err | ||
} | ||
svc := secretsmanager.New(sess, aws.NewConfig().WithRegion(region)) | ||
input := &secretsmanager.GetSecretValueInput{ | ||
SecretId: aws.String(secretName), | ||
VersionStage: aws.String("AWSCURRENT"), | ||
} | ||
|
||
result, err := svc.GetSecretValue(input) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
var secretString string | ||
if result.SecretString != nil { | ||
secretString = *result.SecretString | ||
} else { | ||
decodedBinarySecretBytes := make([]byte, base64.StdEncoding.DecodedLen(len(result.SecretBinary))) | ||
len, err := base64.StdEncoding.Decode(decodedBinarySecretBytes, result.SecretBinary) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if err != nil { | ||
return "", err | ||
} | ||
secretString = string(decodedBinarySecretBytes[:len]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. according to the docs, |
||
} | ||
|
||
return secretString, nil | ||
} | ||
|
||
// ProcessEnv extracts ENV variables into globals | ||
func ProcessEnv() *extensionConfig { | ||
dataReceiverTimeoutSeconds, err := getIntFromEnv("ELASTIC_APM_DATA_RECEIVER_TIMEOUT_SECONDS") | ||
|
@@ -82,7 +119,7 @@ func ProcessEnv() *extensionConfig { | |
normalizedApmLambdaServer = normalizedApmLambdaServer + "/" | ||
} | ||
|
||
logLevel, err := ParseLogLevel(os.Getenv("ELASTIC_APM_LOG_LEVEL")) | ||
logLevel, err := ParseLogLevel(strings.ToLower(os.Getenv("ELASTIC_APM_LOG_LEVEL"))) | ||
if err != nil { | ||
logLevel = zapcore.InfoLevel | ||
Log.Warnf("Could not read ELASTIC_APM_LOG_LEVEL, defaulting to %s", logLevel) | ||
|
@@ -95,10 +132,34 @@ func ProcessEnv() *extensionConfig { | |
normalizedSendStrategy = Background | ||
} | ||
|
||
apmServerApiKey := os.Getenv("ELASTIC_APM_API_KEY") | ||
apmServerApiKeySMSecretId := os.Getenv("ELASTIC_APM_SECRETS_MANAGER_API_KEY_ID") | ||
if apmServerApiKeySMSecretId != "" { | ||
result, err := getSecret(apmServerApiKeySMSecretId) | ||
if err != nil { | ||
Log.Fatalf("Failed loading APM Server ApiKey from Secrets Manager: %v", err) | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the |
||
Log.Infof("Using the APM API key retrieved from Secrets Manager.") | ||
apmServerApiKey = result | ||
} | ||
} | ||
|
||
apmServerSecretToken := os.Getenv("ELASTIC_APM_SECRET_TOKEN") | ||
apmServerSecretTokenSMSecretId := os.Getenv("ELASTIC_APM_SECRETS_MANAGER_SECRET_TOKEN_ID") | ||
if apmServerSecretTokenSMSecretId != "" { | ||
result, err := getSecret(apmServerSecretTokenSMSecretId) | ||
if err != nil { | ||
Log.Fatalf("Failed loading APM Server Secret Token from Secrets Manager: %v", err) | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
Log.Infof("Using the APM secret token retrieved from Secrets Manager.") | ||
apmServerSecretToken = result | ||
} | ||
} | ||
|
||
config := &extensionConfig{ | ||
apmServerUrl: normalizedApmLambdaServer, | ||
apmServerSecretToken: os.Getenv("ELASTIC_APM_SECRET_TOKEN"), | ||
apmServerApiKey: os.Getenv("ELASTIC_APM_API_KEY"), | ||
apmServerSecretToken: apmServerSecretToken, | ||
apmServerApiKey: apmServerApiKey, | ||
dataReceiverServerPort: fmt.Sprintf(":%s", os.Getenv("ELASTIC_APM_DATA_RECEIVER_SERVER_PORT")), | ||
SendStrategy: normalizedSendStrategy, | ||
dataReceiverTimeoutSeconds: dataReceiverTimeoutSeconds, | ||
|
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.
is there any sort of linting for import order? other projects separate out stdlib / elastic / 3rd party