-
Notifications
You must be signed in to change notification settings - Fork 48
/
credentials.go
72 lines (56 loc) · 1.98 KB
/
credentials.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package credentials
import (
"encoding/json"
"github.com/newrelic/newrelic-lambda-extension/util"
"os"
"github.com/newrelic/newrelic-lambda-extension/config"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/secretsmanager"
"github.com/aws/aws-sdk-go/service/secretsmanager/secretsmanageriface"
)
type licenseKeySecret struct {
LicenseKey string
}
var sess = session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
const defaultSecretId = "NEW_RELIC_LICENSE_KEY"
func getLicenseKeySecretId(conf *config.Configuration) string {
if conf.LicenseKeySecretId != nil {
util.Logln("Fetching license key from secret id " + *conf.LicenseKeySecretId)
return *conf.LicenseKeySecretId
}
return defaultSecretId
}
func decodeLicenseKey(rawJson *string) (*string, error) {
var secrets licenseKeySecret
err := json.Unmarshal([]byte(*rawJson), &secrets)
if err != nil {
return nil, err
}
return &secrets.LicenseKey, nil
}
// GetLicenseKeyImpl retrieves the license key from AWS Secrets Manager
func GetLicenseKeyImpl(secrets secretsmanageriface.SecretsManagerAPI, conf *config.Configuration) (*string, error) {
secretId := getLicenseKeySecretId(conf)
secretValueInput := secretsmanager.GetSecretValueInput{SecretId: &secretId}
secretValueOutput, err := secrets.GetSecretValue(&secretValueInput)
if err != nil {
envLicenseKey, found := os.LookupEnv(defaultSecretId)
if found {
return &envLicenseKey, nil
}
return nil, err
}
return decodeLicenseKey(secretValueOutput.SecretString)
}
// GetNewRelicLicenseKey fetches the license key from AWS Secrets Manager, falling back
// to the NEW_RELIC_LICENSE_KEY environment varaible if set.
func GetNewRelicLicenseKey(conf *config.Configuration) (*string, error) {
if conf.LicenseKey != nil {
util.Logln("Using license key from environment variable")
return conf.LicenseKey, nil
}
secrets := secretsmanager.New(sess)
return GetLicenseKeyImpl(secrets, conf)
}