-
Notifications
You must be signed in to change notification settings - Fork 342
/
metadata.go
78 lines (69 loc) · 2.12 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
package aws
import (
"crypto/md5" //nolint:gosec
"fmt"
"net/http"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/cenkalti/backoff/v4"
"github.com/treeverse/lakefs/pkg/cloud"
"github.com/treeverse/lakefs/pkg/logging"
)
type MetadataProvider struct {
logger logging.Logger
stsClient *sts.STS
}
func NewMetadataProvider(logger logging.Logger, awsConfig *aws.Config) *MetadataProvider {
// 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
metaConfig := &aws.Config{
HTTPClient: &http.Client{
Timeout: sessionTimeout,
},
MaxRetries: aws.Int(sessionMaxRetries),
}
var stsClient *sts.STS
sess, err := session.NewSession(awsConfig.Copy(metaConfig))
if err != nil {
logger.WithError(err).Warn("Failed to create AWS session for BI")
} else {
sess.ClientConfig(s3.ServiceName)
stsClient = sts.New(sess)
}
return &MetadataProvider{logger: logger, stsClient: stsClient}
}
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
identity, err := backoff.RetryWithData(func() (*sts.GetCallerIdentityOutput, error) {
identity, err := m.stsClient.GetCallerIdentity(&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",
}
}