-
Notifications
You must be signed in to change notification settings - Fork 351
/
metadata.go
95 lines (86 loc) · 2.79 KB
/
metadata.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
package aws
import (
"context"
"crypto/md5" //nolint:gosec
"fmt"
"time"
awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/cenkalti/backoff/v4"
"github.com/treeverse/lakefs/pkg/block/params"
"github.com/treeverse/lakefs/pkg/cloud"
"github.com/treeverse/lakefs/pkg/logging"
)
type MetadataProvider struct {
logger logging.Logger
stsClient *sts.Client
}
func NewMetadataProvider(logger logging.Logger, params params.S3) (*MetadataProvider, error) {
// set up a session with a shorter timeout and no retries
const sessionMaxRetries = 0 // max number of retries on the client operation
// use a shorter timeout than default
// because the service can be inaccessible from networks
// which don't have an internet connection
const sessionTimeout = 5 * time.Second
/// params
var opts []func(*config.LoadOptions) error
if params.Region != "" {
opts = append(opts, config.WithRegion(params.Region))
}
if params.Profile != "" {
opts = append(opts, config.WithSharedConfigProfile(params.Profile))
}
if params.CredentialsFile != "" {
opts = append(opts, config.WithSharedCredentialsFiles([]string{params.CredentialsFile}))
}
if params.Credentials.AccessKeyID != "" {
opts = append(opts, config.WithCredentialsProvider(
credentials.NewStaticCredentialsProvider(
params.Credentials.AccessKeyID,
params.Credentials.SecretAccessKey,
params.Credentials.SessionToken,
),
))
}
cfg, err := config.LoadDefaultConfig(context.Background(), opts...)
if err != nil {
return nil, err
}
stsClient := sts.NewFromConfig(cfg, func(options *sts.Options) {
options.RetryMaxAttempts = sessionMaxRetries
options.HTTPClient = awshttp.NewBuildableClient().
WithTimeout(sessionTimeout)
})
return &MetadataProvider{logger: logger, stsClient: stsClient}, nil
}
func (m *MetadataProvider) GetMetadata() map[string]string {
if m.stsClient == nil {
return nil
}
const (
maxInterval = 200 * time.Millisecond
maxElapsedTime = 3 * time.Second
)
bo := backoff.NewExponentialBackOff()
bo.MaxInterval = maxInterval
bo.MaxElapsedTime = maxElapsedTime
ctx := context.Background()
identity, err := backoff.RetryWithData(func() (*sts.GetCallerIdentityOutput, error) {
identity, err := m.stsClient.GetCallerIdentity(ctx, &sts.GetCallerIdentityInput{})
if err != nil {
m.logger.WithError(err).Warn("Tried to to get AWS account ID for BI")
return nil, err
}
return identity, nil
}, bo)
if err != nil {
m.logger.WithError(err).Warn("Failed to to get AWS account ID for BI")
return nil
}
return map[string]string{
cloud.IDKey: fmt.Sprintf("%x", md5.Sum([]byte(*identity.Account))), //nolint:gosec
cloud.IDTypeKey: "aws_account_id",
}
}