code.dwrz.net

Go monorepo.
Log | Files | Refs

endpoints.go (6711B)


      1 // Code generated by smithy-go-codegen DO NOT EDIT.
      2 
      3 package sts
      4 
      5 import (
      6 	"context"
      7 	"errors"
      8 	"fmt"
      9 	"github.com/aws/aws-sdk-go-v2/aws"
     10 	awsmiddleware "github.com/aws/aws-sdk-go-v2/aws/middleware"
     11 	internalendpoints "github.com/aws/aws-sdk-go-v2/service/sts/internal/endpoints"
     12 	"github.com/aws/smithy-go/middleware"
     13 	smithyhttp "github.com/aws/smithy-go/transport/http"
     14 	"net/url"
     15 	"strings"
     16 )
     17 
     18 // EndpointResolverOptions is the service endpoint resolver options
     19 type EndpointResolverOptions = internalendpoints.Options
     20 
     21 // EndpointResolver interface for resolving service endpoints.
     22 type EndpointResolver interface {
     23 	ResolveEndpoint(region string, options EndpointResolverOptions) (aws.Endpoint, error)
     24 }
     25 
     26 var _ EndpointResolver = &internalendpoints.Resolver{}
     27 
     28 // NewDefaultEndpointResolver constructs a new service endpoint resolver
     29 func NewDefaultEndpointResolver() *internalendpoints.Resolver {
     30 	return internalendpoints.New()
     31 }
     32 
     33 // EndpointResolverFunc is a helper utility that wraps a function so it satisfies
     34 // the EndpointResolver interface. This is useful when you want to add additional
     35 // endpoint resolving logic, or stub out specific endpoints with custom values.
     36 type EndpointResolverFunc func(region string, options EndpointResolverOptions) (aws.Endpoint, error)
     37 
     38 func (fn EndpointResolverFunc) ResolveEndpoint(region string, options EndpointResolverOptions) (endpoint aws.Endpoint, err error) {
     39 	return fn(region, options)
     40 }
     41 
     42 func resolveDefaultEndpointConfiguration(o *Options) {
     43 	if o.EndpointResolver != nil {
     44 		return
     45 	}
     46 	o.EndpointResolver = NewDefaultEndpointResolver()
     47 }
     48 
     49 // EndpointResolverFromURL returns an EndpointResolver configured using the
     50 // provided endpoint url. By default, the resolved endpoint resolver uses the
     51 // client region as signing region, and the endpoint source is set to
     52 // EndpointSourceCustom.You can provide functional options to configure endpoint
     53 // values for the resolved endpoint.
     54 func EndpointResolverFromURL(url string, optFns ...func(*aws.Endpoint)) EndpointResolver {
     55 	e := aws.Endpoint{URL: url, Source: aws.EndpointSourceCustom}
     56 	for _, fn := range optFns {
     57 		fn(&e)
     58 	}
     59 
     60 	return EndpointResolverFunc(
     61 		func(region string, options EndpointResolverOptions) (aws.Endpoint, error) {
     62 			if len(e.SigningRegion) == 0 {
     63 				e.SigningRegion = region
     64 			}
     65 			return e, nil
     66 		},
     67 	)
     68 }
     69 
     70 type ResolveEndpoint struct {
     71 	Resolver EndpointResolver
     72 	Options  EndpointResolverOptions
     73 }
     74 
     75 func (*ResolveEndpoint) ID() string {
     76 	return "ResolveEndpoint"
     77 }
     78 
     79 func (m *ResolveEndpoint) HandleSerialize(ctx context.Context, in middleware.SerializeInput, next middleware.SerializeHandler) (
     80 	out middleware.SerializeOutput, metadata middleware.Metadata, err error,
     81 ) {
     82 	req, ok := in.Request.(*smithyhttp.Request)
     83 	if !ok {
     84 		return out, metadata, fmt.Errorf("unknown transport type %T", in.Request)
     85 	}
     86 
     87 	if m.Resolver == nil {
     88 		return out, metadata, fmt.Errorf("expected endpoint resolver to not be nil")
     89 	}
     90 
     91 	eo := m.Options
     92 	eo.Logger = middleware.GetLogger(ctx)
     93 
     94 	var endpoint aws.Endpoint
     95 	endpoint, err = m.Resolver.ResolveEndpoint(awsmiddleware.GetRegion(ctx), eo)
     96 	if err != nil {
     97 		return out, metadata, fmt.Errorf("failed to resolve service endpoint, %w", err)
     98 	}
     99 
    100 	req.URL, err = url.Parse(endpoint.URL)
    101 	if err != nil {
    102 		return out, metadata, fmt.Errorf("failed to parse endpoint URL: %w", err)
    103 	}
    104 
    105 	if len(awsmiddleware.GetSigningName(ctx)) == 0 {
    106 		signingName := endpoint.SigningName
    107 		if len(signingName) == 0 {
    108 			signingName = "sts"
    109 		}
    110 		ctx = awsmiddleware.SetSigningName(ctx, signingName)
    111 	}
    112 	ctx = awsmiddleware.SetEndpointSource(ctx, endpoint.Source)
    113 	ctx = smithyhttp.SetHostnameImmutable(ctx, endpoint.HostnameImmutable)
    114 	ctx = awsmiddleware.SetSigningRegion(ctx, endpoint.SigningRegion)
    115 	ctx = awsmiddleware.SetPartitionID(ctx, endpoint.PartitionID)
    116 	return next.HandleSerialize(ctx, in)
    117 }
    118 func addResolveEndpointMiddleware(stack *middleware.Stack, o Options) error {
    119 	return stack.Serialize.Insert(&ResolveEndpoint{
    120 		Resolver: o.EndpointResolver,
    121 		Options:  o.EndpointOptions,
    122 	}, "OperationSerializer", middleware.Before)
    123 }
    124 
    125 func removeResolveEndpointMiddleware(stack *middleware.Stack) error {
    126 	_, err := stack.Serialize.Remove((&ResolveEndpoint{}).ID())
    127 	return err
    128 }
    129 
    130 type wrappedEndpointResolver struct {
    131 	awsResolver aws.EndpointResolverWithOptions
    132 	resolver    EndpointResolver
    133 }
    134 
    135 func (w *wrappedEndpointResolver) ResolveEndpoint(region string, options EndpointResolverOptions) (endpoint aws.Endpoint, err error) {
    136 	if w.awsResolver == nil {
    137 		goto fallback
    138 	}
    139 	endpoint, err = w.awsResolver.ResolveEndpoint(ServiceID, region, options)
    140 	if err == nil {
    141 		return endpoint, nil
    142 	}
    143 
    144 	if nf := (&aws.EndpointNotFoundError{}); !errors.As(err, &nf) {
    145 		return endpoint, err
    146 	}
    147 
    148 fallback:
    149 	if w.resolver == nil {
    150 		return endpoint, fmt.Errorf("default endpoint resolver provided was nil")
    151 	}
    152 	return w.resolver.ResolveEndpoint(region, options)
    153 }
    154 
    155 type awsEndpointResolverAdaptor func(service, region string) (aws.Endpoint, error)
    156 
    157 func (a awsEndpointResolverAdaptor) ResolveEndpoint(service, region string, options ...interface{}) (aws.Endpoint, error) {
    158 	return a(service, region)
    159 }
    160 
    161 var _ aws.EndpointResolverWithOptions = awsEndpointResolverAdaptor(nil)
    162 
    163 // withEndpointResolver returns an EndpointResolver that first delegates endpoint resolution to the awsResolver.
    164 // If awsResolver returns aws.EndpointNotFoundError error, the resolver will use the the provided
    165 // fallbackResolver for resolution.
    166 //
    167 // fallbackResolver must not be nil
    168 func withEndpointResolver(awsResolver aws.EndpointResolver, awsResolverWithOptions aws.EndpointResolverWithOptions, fallbackResolver EndpointResolver) EndpointResolver {
    169 	var resolver aws.EndpointResolverWithOptions
    170 
    171 	if awsResolverWithOptions != nil {
    172 		resolver = awsResolverWithOptions
    173 	} else if awsResolver != nil {
    174 		resolver = awsEndpointResolverAdaptor(awsResolver.ResolveEndpoint)
    175 	}
    176 
    177 	return &wrappedEndpointResolver{
    178 		awsResolver: resolver,
    179 		resolver:    fallbackResolver,
    180 	}
    181 }
    182 
    183 func finalizeClientEndpointResolverOptions(options *Options) {
    184 	options.EndpointOptions.LogDeprecated = options.ClientLogMode.IsDeprecatedUsage()
    185 
    186 	if len(options.EndpointOptions.ResolvedRegion) == 0 {
    187 		const fipsInfix = "-fips-"
    188 		const fipsPrefix = "fips-"
    189 		const fipsSuffix = "-fips"
    190 
    191 		if strings.Contains(options.Region, fipsInfix) ||
    192 			strings.Contains(options.Region, fipsPrefix) ||
    193 			strings.Contains(options.Region, fipsSuffix) {
    194 			options.EndpointOptions.ResolvedRegion = strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(
    195 				options.Region, fipsInfix, "-"), fipsPrefix, ""), fipsSuffix, "")
    196 			options.EndpointOptions.UseFIPSEndpoint = aws.FIPSEndpointStateEnabled
    197 		}
    198 	}
    199 
    200 }