-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
84 lines (71 loc) · 2 KB
/
config.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
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"errors"
"os"
"path"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/caarlos0/env/v6"
"github.com/mackerelio-labs/mackerel-lambda-extension-agent/host/mackerel"
"github.com/mackerelio-labs/mackerel-lambda-extension-agent/lambda"
)
type Config struct {
MackerelConfig mackerel.MackerelConfig
AWSLambdaConfig lambda.AWSLambdaConfig
}
func GetConfig() (*Config, error) {
conf, err := parseEnv()
if err != nil {
return nil, err
}
if conf.MackerelConfig.ApiKey != "" && conf.MackerelConfig.ApiKeySSMParamName != "" {
return nil, errors.New("either EXT_MACKEREL_API_KEY or EXT_MACKEREL_API_KEY_SSM can be specified")
}
if conf.MackerelConfig.ApiKeySSMParamName != "" {
apiKey, err := fetchMackerelApiKeyFromSSM(conf.AWSLambdaConfig.Region, conf.MackerelConfig.ApiKeySSMParamName)
if err != nil {
return nil, err
}
conf.MackerelConfig.ApiKey = apiKey
}
environmentID, err := getEnvironmentID()
if err != nil {
return nil, err
}
conf.AWSLambdaConfig.EnvironmentID = environmentID
conf.AWSLambdaConfig.ExtensionName = getExtensionName()
return conf, nil
}
func parseEnv() (*Config, error) {
conf := &Config{}
if err := env.Parse(conf); err != nil {
return nil, err
}
return conf, nil
}
func fetchMackerelApiKeyFromSSM(region string, name string) (string, error) {
sess := session.Must(session.NewSession())
sess.Config.Region = aws.String(region)
client := ssm.New(sess)
res, err := client.GetParameter(&ssm.GetParameterInput{
Name: aws.String(name),
WithDecryption: aws.Bool(true),
})
if err != nil {
return "", err
}
return aws.StringValue(res.Parameter.Value), nil
}
func getEnvironmentID() (string, error) {
bytes, err := os.ReadFile("/proc/sys/kernel/random/boot_id")
if err != nil {
return "", err
}
environmentID := strings.TrimRight(string(bytes), "\r\n")
return environmentID, nil
}
func getExtensionName() string {
return path.Base(os.Args[0])
}