@@ -7,23 +7,24 @@ package config
77
88import (
99 "compress/gzip"
10+ "context"
1011 "errors"
12+ "fmt"
1113 "log/slog"
1214 "os"
1315 "regexp"
1416
1517 "github.com/DataDog/datadog-serverless-functions/aws/logs_monitoring_go/internal/model"
18+ "github.com/DataDog/datadog-serverless-functions/aws/logs_monitoring_go/internal/sdkclient"
1619)
1720
1821const (
19- DefaultSite = "datadoghq.com"
20- DefaultPort = "443"
21- DefaultProtocol = "https"
22- DefaultLogLevel = "INFO"
23-
22+ DefaultSite = "datadoghq.com"
23+ DefaultPort = "443"
24+ DefaultProtocol = "https"
25+ DefaultLogLevel = "INFO"
2426 EnvAPIKey = "DD_API_KEY"
2527 EnvSite = "DD_SITE"
26- EnvURL = "DD_URL"
2728 EnvAPIURL = "DD_API_URL"
2829 EnvLogLevel = "DD_LOG_LEVEL"
2930 EnvPort = "DD_PORT"
@@ -45,6 +46,17 @@ const (
4546 ForwarderVersion = "6.0"
4647)
4748
49+ type apiKeyResolver struct {
50+ env string
51+ resolve func (ctx context.Context , value string ) (string , error )
52+ }
53+
54+ var apiKeyResolvers = []apiKeyResolver {
55+ {"DD_API_KEY_SECRET_ARN" , sdkclient .ResolveFromSecretsManager },
56+ {"DD_API_KEY_SSM_NAME" , sdkclient .ResolveFromSSM },
57+ {"DD_KMS_API_KEY" , sdkclient .ResolveFromKMS },
58+ }
59+
4860type Config struct {
4961 APIKey string
5062 APIURL string
@@ -92,6 +104,10 @@ func Load() (*Config, error) {
92104 }
93105 }
94106
107+ var resolutionErr error
108+ cfg .APIKey , resolutionErr = resolveAPIKey (context .Background (), apiKeyResolvers )
109+ errs = append (errs , resolutionErr )
110+
95111 return & cfg , errors .Join (errs ... )
96112}
97113
@@ -113,8 +129,7 @@ func (c *Config) loadEnv() {
113129 }
114130 c .CompressionLevel = compressionLevel
115131
116- c .IntakeURL = envOrDefault (EnvURL , protocol + "://http-intake.logs." + site + ":" + port + "/api/v2/logs" )
117- c .APIURL = envOrDefault (EnvAPIURL , protocol + "://api." + site )
132+ c .IntakeURL , c .APIURL = buildURLs (protocol , site , port )
118133
119134 c .Source = envOrDefault (EnvSource , "" )
120135 c .Host = envOrDefault (EnvHost , "" )
@@ -126,3 +141,27 @@ func (c *Config) loadEnv() {
126141 c .ScrubIP = envOrDefaultBool (EnvRedactIP , false )
127142 c .ScrubEmail = envOrDefaultBool (EnvRedactEmail , false )
128143}
144+
145+ func buildURLs (protocol , site , port string ) (intakeURL string , apiURL string ) {
146+ intakeURL = protocol + "://http-intake.logs." + site + ":" + port + "/api/v2/logs"
147+ apiURL = protocol + "://api." + site + "/api/v1/validate"
148+ return
149+ }
150+
151+ func resolveAPIKey (ctx context.Context , resolvers []apiKeyResolver ) (string , error ) {
152+ for _ , resolver := range resolvers {
153+ v , ok := os .LookupEnv (resolver .env )
154+ if ! ok {
155+ continue
156+ }
157+
158+ key , err := resolver .resolve (ctx , v )
159+ if err != nil {
160+ return "" , fmt .Errorf ("resolve: %w" , err )
161+ }
162+
163+ return key , nil
164+ }
165+
166+ return "" , errors .New ("no Datadog API key configured" )
167+ }
0 commit comments