From 2367bb838a95d6ba23a33a0a1473c5ee43e40164 Mon Sep 17 00:00:00 2001 From: Janusz Marcinkiewicz Date: Wed, 20 Nov 2024 12:28:39 +0100 Subject: [PATCH] s3: fix loading credentials in TestS3ETag Signed-off-by: Janusz Marcinkiewicz --- ais/test/s3_compat_test.go | 20 ++++++++++++++++++-- go.mod | 2 +- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/ais/test/s3_compat_test.go b/ais/test/s3_compat_test.go index b09fae4612a..d7635b03da6 100644 --- a/ais/test/s3_compat_test.go +++ b/ais/test/s3_compat_test.go @@ -12,6 +12,7 @@ import ( "net" "net/http" "net/url" + "os" "strings" "testing" "time" @@ -27,6 +28,7 @@ import ( "github.com/NVIDIA/aistore/tools/tassert" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/config" + "github.com/aws/aws-sdk-go-v2/credentials" s3manager "github.com/aws/aws-sdk-go-v2/feature/s3/manager" "github.com/aws/aws-sdk-go-v2/service/s3" "github.com/aws/aws-sdk-go-v2/service/s3/types" @@ -123,6 +125,20 @@ func setBucketFeatures(t *testing.T, bck cmn.Bck, bprops *cmn.Bprops, nf feat.Fl }) } +func loadCredentials(t *testing.T) (f func(*config.LoadOptions) error) { + if os.Getenv("AWS_ACCESS_KEY_ID") != "" && os.Getenv("AWS_SECRET_ACCESS_KEY") != "" { + f = config.WithCredentialsProvider( + credentials.NewStaticCredentialsProvider(os.Getenv("AWS_ACCESS_KEY_ID"), os.Getenv("AWS_SECRET_ACCESS_KEY"), ""), + ) + } else if os.Getenv("AWS_PROFILE") != "" { + f = config.WithSharedConfigProfile(os.Getenv("AWS_PROFILE")) + } else { + t.Skip("Failed to load credentials, none of AWS_PROFILE, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY are set") + f = func(*config.LoadOptions) error { return nil } + } + return f +} + func TestS3PresignedPutGet(t *testing.T) { tools.CheckSkip(t, &tools.SkipTestArgs{Bck: cliBck, RequiresTLS: true, RequiredCloudProvider: apc.AWS}) @@ -288,7 +304,7 @@ func TestDisableColdGet(t *testing.T) { tassert.Fatalf(t, err != nil, "Expected GET to fail %v", err) } -// export AIS_ENDPOINT="http://localhost:8080"; export BUCKET="aws://..."; go test -v -run="TestS3ETag" -count=1 ./ais/test/. +// export AWS_PROFILE=default; export AIS_ENDPOINT="http://localhost:8080"; export BUCKET="aws://..."; go test -v -run="TestS3ETag" -count=1 ./ais/test/. func TestS3ETag(t *testing.T) { tools.CheckSkip(t, &tools.SkipTestArgs{Long: true, Bck: cliBck, RequiredCloudProvider: apc.AWS}) @@ -303,7 +319,7 @@ func TestS3ETag(t *testing.T) { cfg, err := config.LoadDefaultConfig( context.Background(), - config.WithSharedConfigProfile(cos.GetEnvOrDefault("AWS_PROFILE", "default")), + loadCredentials(t), ) tassert.CheckFatal(t, err) s3Client := s3.NewFromConfig(cfg) diff --git a/go.mod b/go.mod index a48d78eb48e..2af53fef7d2 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/OneOfOne/xxhash v1.2.8 github.com/aws/aws-sdk-go-v2 v1.32.5 github.com/aws/aws-sdk-go-v2/config v1.28.5 + github.com/aws/aws-sdk-go-v2/credentials v1.17.46 github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.17.39 github.com/aws/aws-sdk-go-v2/service/s3 v1.67.1 github.com/aws/smithy-go v1.22.1 @@ -56,7 +57,6 @@ require ( github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.49.0 // indirect github.com/andybalholm/brotli v1.1.1 // indirect github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.7 // indirect - github.com/aws/aws-sdk-go-v2/credentials v1.17.46 // indirect github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.20 // indirect github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.24 // indirect github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.24 // indirect