-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws.go
93 lines (81 loc) · 2.94 KB
/
aws.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
85
86
87
88
89
90
91
92
93
// Package claws provides the official AWS SDK (v2)
package claws
import (
"context"
"fmt"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/crewlinker/clgo/clconfig"
"go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
"go.uber.org/fx"
"go.uber.org/zap"
)
// Config configures this package.
type Config struct {
// LoadConfigTimeout bounds the time given to config loading
LoadConfigTimeout time.Duration `env:"LOAD_CONFIG_TIMEOUT" envDefault:"100ms"`
// OverwriteAccessKeyID can be set to overwrite regular credentials loading chain and just a static key/secret
OverwriteAccessKeyID string `env:"OVERWRITE_ACCESS_KEY_ID"`
// If OverwriteAccessKeyID this wil be used as the secret
OverwriteSecretAccessKey string `env:"OVERWRITE_SECRET_ACCESS_KEY"`
// If OverwriteAccessKeyID this wil be used as the session token
OverwriteSessionToken string `env:"OVERWRITE_SESSION_TOKEN"`
}
// New initialize an AWS config to be used to create clients for individual aws services. We would like
// run this during fx lifecycle phase to provide it with a context because it can block. But too many
// dependencies would have to wait for it.
func New(
cfg Config,
logs *zap.Logger,
trp trace.TracerProvider,
txtp propagation.TextMapPropagator,
) (aws.Config, error) {
logs.Info("loading config", zap.Duration("timeout", cfg.LoadConfigTimeout))
var (
acfg aws.Config
err error
)
ctx, cancel := context.WithTimeout(context.Background(), cfg.LoadConfigTimeout)
defer cancel()
opts := []func(*config.LoadOptions) error{}
if cfg.OverwriteAccessKeyID != "" {
opts = append(opts, config.WithCredentialsProvider(aws.NewCredentialsCache(
credentials.NewStaticCredentialsProvider(
cfg.OverwriteAccessKeyID,
cfg.OverwriteSecretAccessKey,
cfg.OverwriteSessionToken,
)),
),
)
}
if acfg, err = config.LoadDefaultConfig(ctx, opts...); err != nil {
return acfg, fmt.Errorf("failed to load default config: %w", err)
}
// if we have a tracing available, we instrument the aws client
if trp != nil {
logs.Info("tracing provided, instrumenting aws client")
otelaws.AppendMiddlewares(
&acfg.APIOptions,
otelaws.WithTracerProvider(trp),
otelaws.WithTextMapPropagator(txtp))
}
return acfg, nil
}
// moduleName for naming conventions.
const moduleName = "claws"
// Provide configures the DI for providng database connectivity.
func Provide() fx.Option {
return fx.Module(moduleName,
// provide the environment configuration
clconfig.Provide[Config](strings.ToUpper(moduleName)+"_"),
// the incoming logger will be named after the module
fx.Decorate(func(l *zap.Logger) *zap.Logger { return l.Named(moduleName) }),
// provide the actual aws config
fx.Provide(fx.Annotate(New, fx.ParamTags(``, ``, `optional:"true"`, `optional:"true"`))),
)
}