mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-09-19 06:26:44 +00:00
Merge pull request #6923 from reasonerjt/aws-sdk-v2
Bump up aws sdk to aws-sdk-go-v2
This commit is contained in:
@@ -21,12 +21,10 @@ import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
goerr "errors"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws/credentials"
|
||||
"github.com/aws/aws-sdk-go/aws/endpoints"
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/aws/aws-sdk-go/service/s3/s3manager"
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
awsconfig "github.com/aws/aws-sdk-go-v2/config"
|
||||
s3manager "github.com/aws/aws-sdk-go-v2/feature/s3/manager"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
@@ -73,27 +71,28 @@ func GetS3ResticEnvVars(config map[string]string) (map[string]string, error) {
|
||||
|
||||
// GetS3Credentials gets the S3 credential values according to the information
|
||||
// of the provided config or the system's environment variables
|
||||
func GetS3Credentials(config map[string]string) (*credentials.Value, error) {
|
||||
func GetS3Credentials(config map[string]string) (*aws.Credentials, error) {
|
||||
if os.Getenv(awsRoleEnvVar) != "" {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
opts := session.Options{}
|
||||
var opts []func(*awsconfig.LoadOptions) error
|
||||
credentialsFile := config[CredentialsFileKey]
|
||||
if credentialsFile == "" {
|
||||
credentialsFile = os.Getenv(awsCredentialsFileEnvVar)
|
||||
}
|
||||
if credentialsFile != "" {
|
||||
opts.SharedConfigFiles = append(opts.SharedConfigFiles, credentialsFile)
|
||||
opts.SharedConfigState = session.SharedConfigEnable
|
||||
opts = append(opts, awsconfig.WithSharedCredentialsFiles([]string{credentialsFile}),
|
||||
// To support the existing use case where config file is passed
|
||||
// as credentials of a BSL
|
||||
awsconfig.WithSharedConfigFiles([]string{credentialsFile}))
|
||||
}
|
||||
|
||||
sess, err := session.NewSessionWithOptions(opts)
|
||||
cfg, err := awsconfig.LoadDefaultConfig(context.Background(), opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
creds, err := sess.Config.Credentials.Get()
|
||||
creds, err := cfg.Credentials.Retrieve(context.Background())
|
||||
|
||||
return &creds, err
|
||||
}
|
||||
@@ -101,33 +100,17 @@ func GetS3Credentials(config map[string]string) (*credentials.Value, error) {
|
||||
// GetAWSBucketRegion returns the AWS region that a bucket is in, or an error
|
||||
// if the region cannot be determined.
|
||||
func GetAWSBucketRegion(bucket string) (string, error) {
|
||||
sess, err := session.NewSession()
|
||||
cfg, err := awsconfig.LoadDefaultConfig(context.Background())
|
||||
if err != nil {
|
||||
return "", errors.WithStack(err)
|
||||
}
|
||||
|
||||
var region string
|
||||
var requestErrs []error
|
||||
|
||||
for _, partition := range endpoints.DefaultPartitions() {
|
||||
for regionHint := range partition.Regions() {
|
||||
region, err = s3manager.GetBucketRegion(context.Background(), sess, bucket, regionHint)
|
||||
if err != nil {
|
||||
requestErrs = append(requestErrs, errors.Wrapf(err, "error to get region with hint %s", regionHint))
|
||||
}
|
||||
|
||||
// we only need to try a single region hint per partition, so break after the first
|
||||
break
|
||||
}
|
||||
|
||||
if region != "" {
|
||||
return region, nil
|
||||
}
|
||||
client := s3.NewFromConfig(cfg)
|
||||
region, err := s3manager.GetBucketRegion(context.Background(), client, bucket)
|
||||
if err != nil {
|
||||
return "", errors.WithStack(err)
|
||||
}
|
||||
|
||||
if requestErrs == nil {
|
||||
return "", errors.Errorf("unable to determine region by bucket %s", bucket)
|
||||
} else {
|
||||
return "", errors.Wrapf(goerr.Join(requestErrs...), "error to get region by bucket %s", bucket)
|
||||
if region == "" {
|
||||
return "", errors.New("unable to determine bucket's region")
|
||||
}
|
||||
return region, nil
|
||||
}
|
||||
|
||||
@@ -433,7 +433,7 @@ func getStorageCredentials(backupLocation *velerov1api.BackupStorageLocation, cr
|
||||
|
||||
if credValue != nil {
|
||||
result[udmrepo.StoreOptionS3KeyID] = credValue.AccessKeyID
|
||||
result[udmrepo.StoreOptionS3Provider] = credValue.ProviderName
|
||||
result[udmrepo.StoreOptionS3Provider] = credValue.Source
|
||||
result[udmrepo.StoreOptionS3SecretKey] = credValue.SecretAccessKey
|
||||
result[udmrepo.StoreOptionS3Token] = credValue.SessionToken
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
awscredentials "github.com/aws/aws-sdk-go/aws/credentials"
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
"github.com/kopia/kopia/repo"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
@@ -45,7 +45,7 @@ func TestGetStorageCredentials(t *testing.T) {
|
||||
credFileStore *credmock.FileStore
|
||||
credStoreError error
|
||||
credStorePath string
|
||||
getS3Credentials func(map[string]string) (*awscredentials.Value, error)
|
||||
getS3Credentials func(map[string]string) (*aws.Credentials, error)
|
||||
getGCPCredentials func(map[string]string) string
|
||||
expected map[string]string
|
||||
expectedErr string
|
||||
@@ -89,8 +89,8 @@ func TestGetStorageCredentials(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
getS3Credentials: func(config map[string]string) (*awscredentials.Value, error) {
|
||||
return &awscredentials.Value{
|
||||
getS3Credentials: func(config map[string]string) (*aws.Credentials, error) {
|
||||
return &aws.Credentials{
|
||||
AccessKeyID: "from: " + config["credentialsFile"],
|
||||
}, nil
|
||||
},
|
||||
@@ -115,8 +115,8 @@ func TestGetStorageCredentials(t *testing.T) {
|
||||
},
|
||||
credFileStore: new(credmock.FileStore),
|
||||
credStorePath: "credentials-from-credential-key",
|
||||
getS3Credentials: func(config map[string]string) (*awscredentials.Value, error) {
|
||||
return &awscredentials.Value{
|
||||
getS3Credentials: func(config map[string]string) (*aws.Credentials, error) {
|
||||
return &aws.Credentials{
|
||||
AccessKeyID: "from: " + config["credentialsFile"],
|
||||
}, nil
|
||||
},
|
||||
@@ -138,7 +138,7 @@ func TestGetStorageCredentials(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
getS3Credentials: func(config map[string]string) (*awscredentials.Value, error) {
|
||||
getS3Credentials: func(config map[string]string) (*aws.Credentials, error) {
|
||||
return nil, errors.New("fake error")
|
||||
},
|
||||
credFileStore: new(credmock.FileStore),
|
||||
@@ -153,7 +153,7 @@ func TestGetStorageCredentials(t *testing.T) {
|
||||
Config: map[string]string{},
|
||||
},
|
||||
},
|
||||
getS3Credentials: func(config map[string]string) (*awscredentials.Value, error) {
|
||||
getS3Credentials: func(config map[string]string) (*aws.Credentials, error) {
|
||||
return nil, nil
|
||||
},
|
||||
credFileStore: new(credmock.FileStore),
|
||||
|
||||
Reference in New Issue
Block a user