Skip to content

Commit

Permalink
EndpointResolverWithOptionsFunc substitutes staticResolver for endpoi…
Browse files Browse the repository at this point in the history
…nt mock
  • Loading branch information
tomtwinkle committed Nov 22, 2023
1 parent 5315f19 commit a3f9f28
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
67 changes: 51 additions & 16 deletions aws/awss3/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
Expand All @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions aws/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
"
Expand Down

0 comments on commit a3f9f28

Please sign in to comment.