-
Notifications
You must be signed in to change notification settings - Fork 342
/
metadata.go
72 lines (64 loc) · 1.97 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
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
awsConfig *aws.Config
}
func NewMetadataProvider(logger logging.Logger, awsConfig *aws.Config) *MetadataProvider {
return &MetadataProvider{logger: logger, awsConfig: awsConfig}
}
func (m *MetadataProvider) GetMetadata() map[string]string {
// max number of retries on the client operation
const sessionMaxRetries = 0
// use a shorter timeout than default because the service can be inaccessible from networks which don't have internet connection
const sessionTimeout = 5 * time.Second
metaConfig := &aws.Config{
HTTPClient: &http.Client{
Timeout: sessionTimeout,
},
MaxRetries: aws.Int(sessionMaxRetries),
}
sess, err := session.NewSession(m.awsConfig.Copy(metaConfig))
if err != nil {
m.logger.WithError(err).Warn("Failed to create AWS session for BI")
return nil
}
sess.ClientConfig(s3.ServiceName)
stsClient := sts.New(sess)
const (
maxInterval = 200 * time.Millisecond
maxElapsedTime = 3 * time.Second
)
bo := backoff.NewExponentialBackOff()
bo.MaxInterval = maxInterval
bo.MaxElapsedTime = maxElapsedTime
var identity *sts.GetCallerIdentityOutput
err = backoff.Retry(func() error {
identity, err = stsClient.GetCallerIdentity(&sts.GetCallerIdentityInput{})
if err != nil {
m.logger.WithError(err).Warn("Tried to to get AWS account ID for BI")
return err
}
return 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",
}
}