Skip to content

Commit 8568a56

Browse files
committed
fix: tranform func to methods, delete unused env vars, refactor code
1 parent 887c3b9 commit 8568a56

2 files changed

Lines changed: 40 additions & 41 deletions

File tree

aws/logs_monitoring_go/internal/config/apikey.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@ import (
1919
)
2020

2121
const (
22-
TIMEOUT = 5
22+
awsClientTimeout = 5 * time.Second
2323
)
2424

2525
type resolveOptions struct {
26-
AWSCfg aws.Config
27-
Value string
28-
UseFIPS bool
26+
AWSCfg aws.Config
27+
Value string
2928
}
3029

3130
type apiKeyResolver func(ctx context.Context, opts resolveOptions) (string, error)
@@ -40,26 +39,30 @@ var resolvers = []struct {
4039
{"DD_API_KEY", resolveFromEnv},
4140
}
4241

43-
func resolveAPIKey(ctx context.Context, useFIPS bool) (string, error) {
42+
func (c *Config) resolveAPIKey(ctx context.Context) error {
4443
awsCfg, err := awsconfig.LoadDefaultConfig(ctx,
45-
awsconfig.WithHTTPClient(awshttp.NewBuildableClient().WithTimeout(time.Second*TIMEOUT)),
44+
awsconfig.WithHTTPClient(awshttp.NewBuildableClient().WithTimeout(awsClientTimeout)),
4645
)
4746

4847
if err != nil {
49-
return "", fmt.Errorf("loading AWS config: %w", err)
48+
return fmt.Errorf("loading AWS config: %w", err)
5049
}
5150

5251
for _, resolver := range resolvers {
5352
if v, ok := os.LookupEnv(resolver.envVar); ok {
5453
slog.Debug("resolving API key", "source", resolver.envVar)
55-
resolver.resolve(ctx, resolveOptions{
56-
AWSCfg: awsCfg,
57-
Value: v,
58-
UseFIPS: useFIPS,
54+
key, err := resolver.resolve(ctx, resolveOptions{
55+
AWSCfg: awsCfg,
56+
Value: v,
5957
})
58+
if err != nil {
59+
return fmt.Errorf("resolving API key from %s: %w", resolver.envVar, err)
60+
}
61+
c.APIKey = key
62+
return nil
6063
}
6164
}
62-
return "", errors.New("no API key configured: set DD_API_KEY, DD_API_KEY_SECRET_ARN, DD_API_KEY_SSM_NAME, or DD_KMS_API_KEY. See: https://docs.datadoghq.com/serverless/forwarder/")
65+
return errors.New("no API key configured: set DD_API_KEY_SECRET_ARN, DD_API_KEY_SSM_NAME, DD_KMS_API_KEY, or DD_API_KEY. See: https://docs.datadoghq.com/serverless/forwarder/")
6366
}
6467

6568
func resolveFromSecretsManager(ctx context.Context, opts resolveOptions) (string, error) {
@@ -96,6 +99,11 @@ func resolveFromEnv(ctx context.Context, opts resolveOptions) (string, error) {
9699
return opts.Value, nil
97100
}
98101

99-
func validateAPIKey(cfg Config) {
100-
102+
// Note: the API key verification could fail (e.g. Datadog verification endpoint or network problem)
103+
// Instead of failing the whole lambda at startup, it should run up to the log sending part and verify the
104+
// key at this moment, adding the run to the future retry logic in such case.
105+
// The method may disappear in the future.
106+
func (c *Config) validateAPIKey() error {
107+
// TODO: implement validation against Datadog API
108+
return nil
101109
}

aws/logs_monitoring_go/internal/config/config.go

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,8 @@ type Config struct {
3333
URL string
3434
Port int
3535
APIURL string
36-
ForwardLog bool
37-
UseCompression bool
38-
CompressionLevel int
3936
NoSSL bool
4037
SkipSSLValidation bool
41-
Tags string
42-
Source string
4338
LogLevel string
4439
UseFIPS bool
4540
}
@@ -48,46 +43,34 @@ func Load(ctx context.Context) (*Config, error) {
4843
cfg := &Config{
4944
Site: envOrDefault("DD_SITE", "datadoghq.com"),
5045
Port: envOrDefaultInt("DD_PORT", 443),
51-
ForwardLog: envOrDefaultBool("DD_FORWARD_LOG", true),
52-
UseCompression: envOrDefaultBool("DD_USE_COMPRESSION", true),
53-
CompressionLevel: envOrDefaultInt("DD_COMPRESSION_LEVEL", 6),
5446
NoSSL: envOrDefaultBool("DD_NO_SSL", false),
5547
SkipSSLValidation: envOrDefaultBool("DD_SKIP_SSL_VALIDATION", false),
56-
Tags: envOrDefault("DD_TAGS", ""),
57-
Source: envOrDefault("DD_SOURCE", ""),
5848
LogLevel: envOrDefault("DD_LOG_LEVEL", "INFO"),
49+
UseFIPS: envOrDefaultBool("DD_USE_FIPS", false),
5950
}
6051

61-
scheme := "https"
62-
if cfg.NoSSL {
63-
scheme = "http"
64-
}
65-
cfg.URL = envOrDefault("DD_URL", "http-intake.logs."+cfg.Site)
66-
cfg.APIURL = envOrDefault("DD_API_URL", fmt.Sprintf("%s://api.%s", scheme, cfg.Site))
52+
cfg.deriveURLs()
6753

6854
logDroppedEnvVars()
6955

70-
useFIPS := envOrDefaultBool("DD_USE_FIPS", false)
71-
cfg.UseFIPS = useFIPS
72-
apiKey, err := resolveAPIKey(ctx, useFIPS)
73-
if err != nil {
56+
if err := cfg.resolveAPIKey(ctx); err != nil {
7457
return nil, fmt.Errorf("resolving API key: %w", err)
7558
}
76-
cfg.APIKey = apiKey
7759

78-
if err := validateAPIKey(cfg); err != nil {
60+
if err := cfg.validateAPIKey(); err != nil {
7961
return nil, fmt.Errorf("validating API key: %w", err)
8062
}
8163

8264
return cfg, nil
8365
}
8466

85-
func logDroppedEnvVars() {
86-
for _, name := range deprecatedEnvironmentVariables {
87-
if _, ok := os.LookupEnv(name); ok {
88-
slog.Warn("deprecated env var set, will be ignored", "name", name)
89-
}
67+
func (c *Config) deriveURLs() {
68+
scheme := "https"
69+
if c.NoSSL {
70+
scheme = "http"
9071
}
72+
c.URL = envOrDefault("DD_URL", "http-intake.logs."+c.Site)
73+
c.APIURL = envOrDefault("DD_API_URL", fmt.Sprintf("%s://api.%s", scheme, c.Site))
9174
}
9275

9376
func envOrDefault(key, fallback string) string {
@@ -117,3 +100,11 @@ func envOrDefaultInt(key string, fallback int) int {
117100
}
118101
return n
119102
}
103+
104+
func logDroppedEnvVars() {
105+
for _, name := range deprecatedEnvironmentVariables {
106+
if _, ok := os.LookupEnv(name); ok {
107+
slog.Warn("deprecated env var set, will be ignored", "name", name)
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)