-
Notifications
You must be signed in to change notification settings - Fork 1
/
s3.go
110 lines (86 loc) · 3 KB
/
s3.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package s3
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"os"
"github.com/alexfalkowski/go-service/file"
"github.com/alexfalkowski/go-service/meta"
source "github.com/alexfalkowski/konfig/source/configurator"
cerrors "github.com/alexfalkowski/konfig/source/configurator/errors"
"github.com/alexfalkowski/konfig/source/configurator/s3/telemetry/tracer"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)
// NewConfigurator for s3.
func NewConfigurator(cfg Config, t tracer.Tracer, client *http.Client) *Configurator {
return &Configurator{cfg: cfg, tracer: t, client: client}
}
// Configurator for s3.
type Configurator struct {
cfg Config
tracer tracer.Tracer
client *http.Client
}
// GetConfig for s3.
func (c *Configurator) GetConfig(ctx context.Context, params source.ConfigParams) (*source.Config, error) {
path := c.path(params.Application, params.Version, params.Environment, params.Continent, params.Country, params.Command, params.Kind)
ctx, span := c.tracer.Start(ctx, "get-config", trace.WithSpanKind(trace.SpanKindClient))
defer span.End()
resolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...any) (aws.Endpoint, error) {
url := os.Getenv("AWS_URL")
if url != "" {
return aws.Endpoint{PartitionID: "aws", URL: url, SigningRegion: region}, nil
}
return aws.Endpoint{}, &aws.EndpointNotFoundError{}
})
opts := []func(*config.LoadOptions) error{
config.WithEndpointResolverWithOptions(resolver),
config.WithHTTPClient(c.client),
}
cfg, err := config.LoadDefaultConfig(ctx, opts...)
if err != nil {
span.SetStatus(codes.Error, err.Error())
span.RecordError(err)
return nil, err
}
client := s3.NewFromConfig(cfg, func(o *s3.Options) {
o.UsePathStyle = true
})
out, err := client.GetObject(ctx, &s3.GetObjectInput{Bucket: &c.cfg.Bucket, Key: &path})
if err != nil {
meta.WithAttribute(ctx, "s3.get_object_error", err.Error())
var nerr *types.NoSuchKey
if errors.As(err, &nerr) {
span.SetStatus(codes.Error, err.Error())
span.RecordError(err)
return nil, cerrors.ErrNotFound
}
span.SetStatus(codes.Error, err.Error())
span.RecordError(err)
return nil, err
}
data, err := io.ReadAll(out.Body)
if err != nil {
meta.WithAttribute(ctx, "s3.read_all_error", err.Error())
span.SetStatus(codes.Error, err.Error())
span.RecordError(err)
return nil, err
}
return &source.Config{Kind: file.Extension(path), Data: data}, nil
}
func (c *Configurator) path(app, ver, env, continent, country, cmd, kind string) string {
if continent == "*" && country == "*" {
return fmt.Sprintf("%s/%s/%s/%s.%s", app, ver, env, cmd, kind)
}
if continent != "*" && country == "*" {
return fmt.Sprintf("%s/%s/%s/%s/%s.%s", app, ver, env, continent, cmd, kind)
}
return fmt.Sprintf("%s/%s/%s/%s/%s/%s.%s", app, ver, env, continent, country, cmd, kind)
}