Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fix awss3 client mock resolver #382

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 17 additions & 50 deletions aws/awss3/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@ import (
"encoding/gob"
"fmt"
"net"
"net/url"
"sync"

"github.com/88labs/go-utils/aws/awsconfig"
"github.com/88labs/go-utils/aws/awss3/options/global/s3dialer"
"github.com/88labs/go-utils/aws/ctxawslocal"
"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"
)

var (
Expand Down Expand Up @@ -70,47 +67,19 @@ 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 @@ -127,7 +96,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 @@ -140,9 +109,7 @@ func getClientLocal(ctx context.Context, localProfile LocalProfile) (*s3.Client,
return nil, fmt.Errorf("unable to load SDK config, %w", err)
}
customEndpoint = localProfile.Endpoint
return s3.NewFromConfig(awsCfg, func(o *s3.Options) {
o.EndpointResolverV2 = &staticResolver{}
}), nil
return s3.NewFromConfig(awsCfg), nil
}

type LocalProfile struct {
Expand Down
Loading