From 1101aac72783ecec41a007389dfa9b64425da40d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Fri, 25 Oct 2024 17:04:51 +0200 Subject: [PATCH 1/5] fix: resolve AWS deprecations for localstack --- modules/localstack/go.mod | 2 +- modules/localstack/go.sum | 4 ++-- modules/localstack/v2/s3_test.go | 25 +++++++++++++++---------- 3 files changed, 18 insertions(+), 13 deletions(-) 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..17dafe6333 100644 --- a/modules/localstack/v2/s3_test.go +++ b/modules/localstack/v2/s3_test.go @@ -10,6 +10,7 @@ import ( "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 +26,17 @@ const ( region = "us-east-1" ) +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 +55,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(fmt.Sprintf("http://%s:%d", host, mappedPort.Int())) + o.EndpointResolverV2 = &resolverV2{} o.UsePathStyle = true }) From f3e785cbd18f4e1b719e5008b48acec2b6f2d0c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Wed, 6 Nov 2024 13:33:02 +0100 Subject: [PATCH 2/5] docs: include resolver --- docs/modules/localstack.md | 1 + modules/localstack/v2/s3_test.go | 3 +++ 2 files changed, 4 insertions(+) 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/v2/s3_test.go b/modules/localstack/v2/s3_test.go index 17dafe6333..5452bff822 100644 --- a/modules/localstack/v2/s3_test.go +++ b/modules/localstack/v2/s3_test.go @@ -26,6 +26,7 @@ const ( region = "us-east-1" ) +// awsResolverV2 { type resolverV2 struct { // you could inject additional application context here as well } @@ -37,6 +38,8 @@ func (*resolverV2) ResolveEndpoint(ctx context.Context, params s3.EndpointParame 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")) From cbc70160a1f930acea56f28db1fb0b41273515a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Wed, 6 Nov 2024 22:50:44 +0100 Subject: [PATCH 3/5] chore: simplify fmt.Sprintf Co-authored-by: Steven Hartland --- modules/localstack/v2/s3_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/localstack/v2/s3_test.go b/modules/localstack/v2/s3_test.go index 5452bff822..c04a3c271a 100644 --- a/modules/localstack/v2/s3_test.go +++ b/modules/localstack/v2/s3_test.go @@ -68,7 +68,7 @@ func s3Client(ctx context.Context, l *localstack.LocalStackContainer) (*s3.Clien // 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(fmt.Sprintf("http://%s:%d", host, mappedPort.Int())) + o.BaseEndpoint = aws.String("http://"+ host +":"+ mappedPort.Port())) o.EndpointResolverV2 = &resolverV2{} o.UsePathStyle = true }) From 9f5ead349d4b0d904a05abd6e3196299b9824183 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Thu, 7 Nov 2024 11:28:53 +0100 Subject: [PATCH 4/5] fix: proper format Co-authored-by: Steven Hartland --- modules/localstack/v2/s3_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/localstack/v2/s3_test.go b/modules/localstack/v2/s3_test.go index c04a3c271a..fc0e06ef19 100644 --- a/modules/localstack/v2/s3_test.go +++ b/modules/localstack/v2/s3_test.go @@ -68,7 +68,7 @@ func s3Client(ctx context.Context, l *localstack.LocalStackContainer) (*s3.Clien // 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.BaseEndpoint = aws.String("http://" + host + ":" + mappedPort.Port()) o.EndpointResolverV2 = &resolverV2{} o.UsePathStyle = true }) From 4344ab2c6ff0a3bd2bea50383d1576806de78f99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Thu, 7 Nov 2024 12:30:01 +0100 Subject: [PATCH 5/5] fix: remove unused import --- modules/localstack/v2/s3_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/localstack/v2/s3_test.go b/modules/localstack/v2/s3_test.go index fc0e06ef19..2df71dcb39 100644 --- a/modules/localstack/v2/s3_test.go +++ b/modules/localstack/v2/s3_test.go @@ -3,7 +3,6 @@ package v2_test import ( "bytes" "context" - "fmt" "testing" "github.com/aws/aws-sdk-go-v2/aws"