From a3f9f28145d8750d17a5c039782fb26c952fa374 Mon Sep 17 00:00:00 2001 From: tom twinkle Date: Wed, 22 Nov 2023 10:42:20 +0900 Subject: [PATCH] EndpointResolverWithOptionsFunc substitutes staticResolver for endpoint mock --- aws/awss3/client.go | 67 +++++++++++++++++++++++++++++++---------- aws/docker-compose.yaml | 2 ++ 2 files changed, 53 insertions(+), 16 deletions(-) diff --git a/aws/awss3/client.go b/aws/awss3/client.go index 1bce439..760f07d 100644 --- a/aws/awss3/client.go +++ b/aws/awss3/client.go @@ -4,20 +4,25 @@ import ( "context" "fmt" "net" + "net/url" "github.com/aws/aws-sdk-go-v2/aws" awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http" awsConfig "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/88labs/go-utils/aws/awsconfig" "github.com/88labs/go-utils/aws/awss3/options/global/s3dialer" "github.com/88labs/go-utils/aws/ctxawslocal" ) -// GlobalDialer Global http dialer settings for awss3 library -var GlobalDialer *s3dialer.ConfGlobalDialer +var ( + // GlobalDialer Global http dialer settings for awss3 library + GlobalDialer *s3dialer.ConfGlobalDialer + customEndpoint string +) // GetClient // Get s3 client for aws-sdk-go v2. @@ -52,20 +57,47 @@ func GetClient(ctx context.Context, region awsconfig.Region) (*s3.Client, error) return s3.NewFromConfig(awsCfg), nil } +type staticResolver struct{} + +// ResolveEndpoint +// Local test mocks endpoints to connect to minio +// +// FIXME: EndpointResolverWithOptionsFunc substitutes staticResolver for endpoint mock +func (*staticResolver) ResolveEndpoint(_ context.Context, p s3.EndpointParameters) ( + smithyendpoints.Endpoint, error, +) { + if customEndpoint != "" { + endpoint, err := url.Parse(customEndpoint) + if err != nil { + return smithyendpoints.Endpoint{}, fmt.Errorf("unable to parse endpoint, %w", err) + } + if p.Bucket != nil { + endpoint = endpoint.JoinPath(*p.Bucket) + } + // This value will be used as-is when making the request. + return smithyendpoints.Endpoint{ + URI: *endpoint, + }, nil + } + return smithyendpoints.Endpoint{}, &aws.EndpointNotFoundError{} +} + func getClientLocal(ctx context.Context, localProfile LocalProfile) (*s3.Client, error) { + // FIXME: EndpointResolverWithOptionsFunc substitutes staticResolver for endpoint mock + // because HostnameImmutable is not enabled. (github.com/aws/aws-sdk-go-v2/config v1.25.4) // https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/endpoints/ - customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) { - if service == s3.ServiceID { - return aws.Endpoint{ - PartitionID: "aws", - URL: localProfile.Endpoint, - SigningRegion: region, - HostnameImmutable: true, - }, nil - } - // returning EndpointNotFoundError will allow the service to fallback to it's default resolution - return aws.Endpoint{}, &aws.EndpointNotFoundError{} - }) + //customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) { + // if service == s3.ServiceID { + // return aws.Endpoint{ + // PartitionID: "aws", + // URL: localProfile.Endpoint, + // SigningRegion: region, + // HostnameImmutable: true, + // }, nil + // } + // // returning EndpointNotFoundError will allow the service to fallback to it's default resolution + // return aws.Endpoint{}, &aws.EndpointNotFoundError{} + //}) awsHttpClient := awshttp.NewBuildableClient() if GlobalDialer != nil { awsHttpClient.WithDialerOptions(func(dialer *net.Dialer) { @@ -82,7 +114,7 @@ func getClientLocal(ctx context.Context, localProfile LocalProfile) (*s3.Client, } awsCfg, err := awsConfig.LoadDefaultConfig(ctx, awsConfig.WithHTTPClient(awsHttpClient), - awsConfig.WithEndpointResolverWithOptions(customResolver), + //awsConfig.WithEndpointResolverWithOptions(customResolver), awsConfig.WithCredentialsProvider(credentials.StaticCredentialsProvider{ Value: aws.Credentials{ AccessKeyID: localProfile.AccessKey, @@ -94,7 +126,10 @@ func getClientLocal(ctx context.Context, localProfile LocalProfile) (*s3.Client, if err != nil { return nil, fmt.Errorf("unable to load SDK config, %w", err) } - return s3.NewFromConfig(awsCfg), nil + customEndpoint = localProfile.Endpoint + return s3.NewFromConfig(awsCfg, func(o *s3.Options) { + o.EndpointResolverV2 = &staticResolver{} + }), nil } type LocalProfile struct { diff --git a/aws/docker-compose.yaml b/aws/docker-compose.yaml index fb5ccc1..b8837d3 100644 --- a/aws/docker-compose.yaml +++ b/aws/docker-compose.yaml @@ -33,6 +33,8 @@ services: /bin/sh -c " /usr/bin/mc config host add s3 http://minio:29000 DUMMYACCESSKEYEXAMPLE DUMMYSECRETKEYEXAMPLE; /usr/bin/mc --quiet mb s3/test; + /usr/bin/mc --quiet anonymous set upload s3/test; + /usr/bin/mc --quiet anonymous set download s3/test; /usr/bin/mc --quiet policy set-json /policies/policy_test.json s3/test; exit 0; "