From aceb7070e6bc78e8037485fd481a23ea15175a57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Thu, 7 Nov 2024 14:09:03 +0100 Subject: [PATCH] chore: resolve AWS deprecations for localstack (#2879) * fix: resolve AWS deprecations for localstack * docs: include resolver * chore: simplify fmt.Sprintf Co-authored-by: Steven Hartland * fix: proper format Co-authored-by: Steven Hartland * fix: remove unused import --------- Co-authored-by: Steven Hartland --- docs/modules/localstack.md | 1 + modules/localstack/go.mod | 2 +- modules/localstack/go.sum | 4 ++-- modules/localstack/v2/s3_test.go | 29 ++++++++++++++++++----------- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/docs/modules/localstack.md b/docs/modules/localstack.md index cf209162af..77cc522907 100644 --- a/docs/modules/localstack.md +++ b/docs/modules/localstack.md @@ -103,6 +103,7 @@ For further reference on the SDK v1, please check out the AWS docs [here](https: ### Using the AWS SDK v2 +[EndpointResolver](../../modules/localstack/v2/s3_test.go) inside_block:awsResolverV2 [AWS SDK v2](../../modules/localstack/v2/s3_test.go) inside_block:awsSDKClientV2 diff --git a/modules/localstack/go.mod b/modules/localstack/go.mod index 52c3e446cc..a0c994cc3a 100644 --- a/modules/localstack/go.mod +++ b/modules/localstack/go.mod @@ -8,6 +8,7 @@ require ( github.com/aws/aws-sdk-go-v2/config v1.27.5 github.com/aws/aws-sdk-go-v2/credentials v1.17.5 github.com/aws/aws-sdk-go-v2/service/s3 v1.51.2 + github.com/aws/smithy-go v1.21.0 github.com/docker/docker v27.1.1+incompatible github.com/docker/go-connections v0.5.0 github.com/stretchr/testify v1.9.0 @@ -32,7 +33,6 @@ require ( github.com/aws/aws-sdk-go-v2/service/sso v1.20.1 // indirect github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.1 // indirect github.com/aws/aws-sdk-go-v2/service/sts v1.28.2 // indirect - github.com/aws/smithy-go v1.20.1 // indirect github.com/cenkalti/backoff/v4 v4.2.1 // indirect github.com/containerd/containerd v1.7.18 // indirect github.com/containerd/log v0.1.0 // indirect diff --git a/modules/localstack/go.sum b/modules/localstack/go.sum index 265c675ed5..a6341a7c78 100644 --- a/modules/localstack/go.sum +++ b/modules/localstack/go.sum @@ -42,8 +42,8 @@ github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.1 h1:9/GylMS45hGGFCcMrUZDVayQ github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.1/go.mod h1:YjAPFn4kGFqKC54VsHs5fn5B6d+PCY2tziEa3U/GB5Y= github.com/aws/aws-sdk-go-v2/service/sts v1.28.2 h1:0YjXuWdYHvsm0HnT4vO8XpwG1D+i2roxSCBoN6deJ7M= github.com/aws/aws-sdk-go-v2/service/sts v1.28.2/go.mod h1:jI+FWmYkSMn+4APWmZiZTgt0oM0TrvymD51FMqCnWgA= -github.com/aws/smithy-go v1.20.1 h1:4SZlSlMr36UEqC7XOyRVb27XMeZubNcBNN+9IgEPIQw= -github.com/aws/smithy-go v1.20.1/go.mod h1:krry+ya/rV9RDcV/Q16kpu6ypI4K2czasz0NC3qS14E= +github.com/aws/smithy-go v1.21.0 h1:H7L8dtDRk0P1Qm6y0ji7MCYMQObJ5R9CRpyPhRUkLYA= +github.com/aws/smithy-go v1.21.0/go.mod h1:irrKGvNn1InZwb2d7fkIRNucdfwR8R+Ts3wxYa/cJHg= github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM= github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= github.com/containerd/containerd v1.7.18 h1:jqjZTQNfXGoEaZdW1WwPU0RqSn1Bm2Ay/KJPUuO8nao= diff --git a/modules/localstack/v2/s3_test.go b/modules/localstack/v2/s3_test.go index 10c608ed8f..2df71dcb39 100644 --- a/modules/localstack/v2/s3_test.go +++ b/modules/localstack/v2/s3_test.go @@ -3,13 +3,13 @@ package v2_test import ( "bytes" "context" - "fmt" "testing" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/credentials" "github.com/aws/aws-sdk-go-v2/service/s3" + smithyendpoints "github.com/aws/smithy-go/endpoints" "github.com/docker/go-connections/nat" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -25,6 +25,20 @@ const ( region = "us-east-1" ) +// awsResolverV2 { +type resolverV2 struct { + // you could inject additional application context here as well +} + +func (*resolverV2) ResolveEndpoint(ctx context.Context, params s3.EndpointParameters) ( + smithyendpoints.Endpoint, error, +) { + // delegate back to the default v2 resolver otherwise + return s3.NewDefaultEndpointResolverV2().ResolveEndpoint(ctx, params) +} + +// } + // awsSDKClientV2 { func s3Client(ctx context.Context, l *localstack.LocalStackContainer) (*s3.Client, error) { mappedPort, err := l.MappedPort(ctx, nat.Port("4566/tcp")) @@ -43,25 +57,18 @@ func s3Client(ctx context.Context, l *localstack.LocalStackContainer) (*s3.Clien return nil, err } - customResolver := aws.EndpointResolverWithOptionsFunc( - func(service, region string, opts ...interface{}) (aws.Endpoint, error) { - return aws.Endpoint{ - PartitionID: "aws", - URL: fmt.Sprintf("http://%s:%d", host, mappedPort.Int()), - SigningRegion: region, - }, nil - }) - awsCfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(region), - config.WithEndpointResolverWithOptions(customResolver), config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(accesskey, secretkey, token)), ) if err != nil { return nil, err } + // reference: https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/endpoints/#with-both client := s3.NewFromConfig(awsCfg, func(o *s3.Options) { + o.BaseEndpoint = aws.String("http://" + host + ":" + mappedPort.Port()) + o.EndpointResolverV2 = &resolverV2{} o.UsePathStyle = true })