endpoints.go raw
1 // Code generated by smithy-go-codegen DO NOT EDIT.
2
3 package lightsail
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 internalConfig "github.com/aws/aws-sdk-go-v2/internal/configsources"
12 "github.com/aws/aws-sdk-go-v2/internal/endpoints"
13 "github.com/aws/aws-sdk-go-v2/internal/endpoints/awsrulesfn"
14 internalendpoints "github.com/aws/aws-sdk-go-v2/service/lightsail/internal/endpoints"
15 smithyauth "github.com/aws/smithy-go/auth"
16 smithyendpoints "github.com/aws/smithy-go/endpoints"
17 "github.com/aws/smithy-go/endpoints/private/rulesfn"
18 "github.com/aws/smithy-go/middleware"
19 "github.com/aws/smithy-go/ptr"
20 "github.com/aws/smithy-go/tracing"
21 smithyhttp "github.com/aws/smithy-go/transport/http"
22 "net/http"
23 "net/url"
24 "os"
25 "strings"
26 )
27
28 // EndpointResolverOptions is the service endpoint resolver options
29 type EndpointResolverOptions = internalendpoints.Options
30
31 // EndpointResolver interface for resolving service endpoints.
32 type EndpointResolver interface {
33 ResolveEndpoint(region string, options EndpointResolverOptions) (aws.Endpoint, error)
34 }
35
36 var _ EndpointResolver = &internalendpoints.Resolver{}
37
38 // NewDefaultEndpointResolver constructs a new service endpoint resolver
39 func NewDefaultEndpointResolver() *internalendpoints.Resolver {
40 return internalendpoints.New()
41 }
42
43 // EndpointResolverFunc is a helper utility that wraps a function so it satisfies
44 // the EndpointResolver interface. This is useful when you want to add additional
45 // endpoint resolving logic, or stub out specific endpoints with custom values.
46 type EndpointResolverFunc func(region string, options EndpointResolverOptions) (aws.Endpoint, error)
47
48 func (fn EndpointResolverFunc) ResolveEndpoint(region string, options EndpointResolverOptions) (endpoint aws.Endpoint, err error) {
49 return fn(region, options)
50 }
51
52 // EndpointResolverFromURL returns an EndpointResolver configured using the
53 // provided endpoint url. By default, the resolved endpoint resolver uses the
54 // client region as signing region, and the endpoint source is set to
55 // EndpointSourceCustom.You can provide functional options to configure endpoint
56 // values for the resolved endpoint.
57 func EndpointResolverFromURL(url string, optFns ...func(*aws.Endpoint)) EndpointResolver {
58 e := aws.Endpoint{URL: url, Source: aws.EndpointSourceCustom}
59 for _, fn := range optFns {
60 fn(&e)
61 }
62
63 return EndpointResolverFunc(
64 func(region string, options EndpointResolverOptions) (aws.Endpoint, error) {
65 if len(e.SigningRegion) == 0 {
66 e.SigningRegion = region
67 }
68 return e, nil
69 },
70 )
71 }
72
73 type ResolveEndpoint struct {
74 Resolver EndpointResolver
75 Options EndpointResolverOptions
76 }
77
78 func (*ResolveEndpoint) ID() string {
79 return "ResolveEndpoint"
80 }
81
82 func (m *ResolveEndpoint) HandleSerialize(ctx context.Context, in middleware.SerializeInput, next middleware.SerializeHandler) (
83 out middleware.SerializeOutput, metadata middleware.Metadata, err error,
84 ) {
85 if !awsmiddleware.GetRequiresLegacyEndpoints(ctx) {
86 return next.HandleSerialize(ctx, in)
87 }
88
89 req, ok := in.Request.(*smithyhttp.Request)
90 if !ok {
91 return out, metadata, fmt.Errorf("unknown transport type %T", in.Request)
92 }
93
94 if m.Resolver == nil {
95 return out, metadata, fmt.Errorf("expected endpoint resolver to not be nil")
96 }
97
98 eo := m.Options
99 eo.Logger = middleware.GetLogger(ctx)
100
101 var endpoint aws.Endpoint
102 endpoint, err = m.Resolver.ResolveEndpoint(awsmiddleware.GetRegion(ctx), eo)
103 if err != nil {
104 nf := (&aws.EndpointNotFoundError{})
105 if errors.As(err, &nf) {
106 ctx = awsmiddleware.SetRequiresLegacyEndpoints(ctx, false)
107 return next.HandleSerialize(ctx, in)
108 }
109 return out, metadata, fmt.Errorf("failed to resolve service endpoint, %w", err)
110 }
111
112 req.URL, err = url.Parse(endpoint.URL)
113 if err != nil {
114 return out, metadata, fmt.Errorf("failed to parse endpoint URL: %w", err)
115 }
116
117 if len(awsmiddleware.GetSigningName(ctx)) == 0 {
118 signingName := endpoint.SigningName
119 if len(signingName) == 0 {
120 signingName = "lightsail"
121 }
122 ctx = awsmiddleware.SetSigningName(ctx, signingName)
123 }
124 ctx = awsmiddleware.SetEndpointSource(ctx, endpoint.Source)
125 ctx = smithyhttp.SetHostnameImmutable(ctx, endpoint.HostnameImmutable)
126 ctx = awsmiddleware.SetSigningRegion(ctx, endpoint.SigningRegion)
127 ctx = awsmiddleware.SetPartitionID(ctx, endpoint.PartitionID)
128 return next.HandleSerialize(ctx, in)
129 }
130 func addResolveEndpointMiddleware(stack *middleware.Stack, o Options) error {
131 return stack.Serialize.Insert(&ResolveEndpoint{
132 Resolver: o.EndpointResolver,
133 Options: o.EndpointOptions,
134 }, "OperationSerializer", middleware.Before)
135 }
136
137 func removeResolveEndpointMiddleware(stack *middleware.Stack) error {
138 _, err := stack.Serialize.Remove((&ResolveEndpoint{}).ID())
139 return err
140 }
141
142 type wrappedEndpointResolver struct {
143 awsResolver aws.EndpointResolverWithOptions
144 }
145
146 func (w *wrappedEndpointResolver) ResolveEndpoint(region string, options EndpointResolverOptions) (endpoint aws.Endpoint, err error) {
147 return w.awsResolver.ResolveEndpoint(ServiceID, region, options)
148 }
149
150 type awsEndpointResolverAdaptor func(service, region string) (aws.Endpoint, error)
151
152 func (a awsEndpointResolverAdaptor) ResolveEndpoint(service, region string, options ...interface{}) (aws.Endpoint, error) {
153 return a(service, region)
154 }
155
156 var _ aws.EndpointResolverWithOptions = awsEndpointResolverAdaptor(nil)
157
158 // withEndpointResolver returns an aws.EndpointResolverWithOptions that first delegates endpoint resolution to the awsResolver.
159 // If awsResolver returns aws.EndpointNotFoundError error, the v1 resolver middleware will swallow the error,
160 // and set an appropriate context flag such that fallback will occur when EndpointResolverV2 is invoked
161 // via its middleware.
162 //
163 // If another error (besides aws.EndpointNotFoundError) is returned, then that error will be propagated.
164 func withEndpointResolver(awsResolver aws.EndpointResolver, awsResolverWithOptions aws.EndpointResolverWithOptions) EndpointResolver {
165 var resolver aws.EndpointResolverWithOptions
166
167 if awsResolverWithOptions != nil {
168 resolver = awsResolverWithOptions
169 } else if awsResolver != nil {
170 resolver = awsEndpointResolverAdaptor(awsResolver.ResolveEndpoint)
171 }
172
173 return &wrappedEndpointResolver{
174 awsResolver: resolver,
175 }
176 }
177
178 func finalizeClientEndpointResolverOptions(options *Options) {
179 options.EndpointOptions.LogDeprecated = options.ClientLogMode.IsDeprecatedUsage()
180
181 if len(options.EndpointOptions.ResolvedRegion) == 0 {
182 const fipsInfix = "-fips-"
183 const fipsPrefix = "fips-"
184 const fipsSuffix = "-fips"
185
186 if strings.Contains(options.Region, fipsInfix) ||
187 strings.Contains(options.Region, fipsPrefix) ||
188 strings.Contains(options.Region, fipsSuffix) {
189 options.EndpointOptions.ResolvedRegion = strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(
190 options.Region, fipsInfix, "-"), fipsPrefix, ""), fipsSuffix, "")
191 options.EndpointOptions.UseFIPSEndpoint = aws.FIPSEndpointStateEnabled
192 }
193 }
194
195 }
196
197 func resolveEndpointResolverV2(options *Options) {
198 if options.EndpointResolverV2 == nil {
199 options.EndpointResolverV2 = NewDefaultEndpointResolverV2()
200 }
201 }
202
203 func resolveBaseEndpoint(cfg aws.Config, o *Options) {
204 if cfg.BaseEndpoint != nil {
205 o.BaseEndpoint = cfg.BaseEndpoint
206 }
207
208 _, g := os.LookupEnv("AWS_ENDPOINT_URL")
209 _, s := os.LookupEnv("AWS_ENDPOINT_URL_LIGHTSAIL")
210
211 if g && !s {
212 return
213 }
214
215 value, found, err := internalConfig.ResolveServiceBaseEndpoint(context.Background(), "Lightsail", cfg.ConfigSources)
216 if found && err == nil {
217 o.BaseEndpoint = &value
218 }
219 }
220
221 func bindRegion(region string) (*string, error) {
222 if region == "" {
223 return nil, nil
224 }
225 if !rulesfn.IsValidHostLabel(region, true) {
226 return nil, fmt.Errorf("invalid input region %s", region)
227 }
228
229 return aws.String(endpoints.MapFIPSRegion(region)), nil
230 }
231
232 // EndpointParameters provides the parameters that influence how endpoints are
233 // resolved.
234 type EndpointParameters struct {
235 // The AWS region used to dispatch the request.
236 //
237 // Parameter is
238 // required.
239 //
240 // AWS::Region
241 Region *string
242
243 // When true, use the dual-stack endpoint. If the configured endpoint does not
244 // support dual-stack, dispatching the request MAY return an error.
245 //
246 // Defaults to
247 // false if no value is provided.
248 //
249 // AWS::UseDualStack
250 UseDualStack *bool
251
252 // When true, send this request to the FIPS-compliant regional endpoint. If the
253 // configured endpoint does not have a FIPS compliant endpoint, dispatching the
254 // request will return an error.
255 //
256 // Defaults to false if no value is
257 // provided.
258 //
259 // AWS::UseFIPS
260 UseFIPS *bool
261
262 // Override the endpoint used to send this request
263 //
264 // Parameter is
265 // required.
266 //
267 // SDK::Endpoint
268 Endpoint *string
269 }
270
271 // ValidateRequired validates required parameters are set.
272 func (p EndpointParameters) ValidateRequired() error {
273 if p.UseDualStack == nil {
274 return fmt.Errorf("parameter UseDualStack is required")
275 }
276
277 if p.UseFIPS == nil {
278 return fmt.Errorf("parameter UseFIPS is required")
279 }
280
281 return nil
282 }
283
284 // WithDefaults returns a shallow copy of EndpointParameterswith default values
285 // applied to members where applicable.
286 func (p EndpointParameters) WithDefaults() EndpointParameters {
287 if p.UseDualStack == nil {
288 p.UseDualStack = ptr.Bool(false)
289 }
290
291 if p.UseFIPS == nil {
292 p.UseFIPS = ptr.Bool(false)
293 }
294 return p
295 }
296
297 type stringSlice []string
298
299 func (s stringSlice) Get(i int) *string {
300 if i < 0 || i >= len(s) {
301 return nil
302 }
303
304 v := s[i]
305 return &v
306 }
307
308 // EndpointResolverV2 provides the interface for resolving service endpoints.
309 type EndpointResolverV2 interface {
310 // ResolveEndpoint attempts to resolve the endpoint with the provided options,
311 // returning the endpoint if found. Otherwise an error is returned.
312 ResolveEndpoint(ctx context.Context, params EndpointParameters) (
313 smithyendpoints.Endpoint, error,
314 )
315 }
316
317 // resolver provides the implementation for resolving endpoints.
318 type resolver struct{}
319
320 func NewDefaultEndpointResolverV2() EndpointResolverV2 {
321 return &resolver{}
322 }
323
324 // ResolveEndpoint attempts to resolve the endpoint with the provided options,
325 // returning the endpoint if found. Otherwise an error is returned.
326 func (r *resolver) ResolveEndpoint(
327 ctx context.Context, params EndpointParameters,
328 ) (
329 endpoint smithyendpoints.Endpoint, err error,
330 ) {
331 params = params.WithDefaults()
332 if err = params.ValidateRequired(); err != nil {
333 return endpoint, fmt.Errorf("endpoint parameters are not valid, %w", err)
334 }
335 _UseDualStack := *params.UseDualStack
336 _ = _UseDualStack
337 _UseFIPS := *params.UseFIPS
338 _ = _UseFIPS
339
340 if exprVal := params.Endpoint; exprVal != nil {
341 _Endpoint := *exprVal
342 _ = _Endpoint
343 if _UseFIPS == true {
344 return endpoint, fmt.Errorf("endpoint rule error, %s", "Invalid Configuration: FIPS and custom endpoint are not supported")
345 }
346 if _UseDualStack == true {
347 return endpoint, fmt.Errorf("endpoint rule error, %s", "Invalid Configuration: Dualstack and custom endpoint are not supported")
348 }
349 uriString := _Endpoint
350
351 uri, err := url.Parse(uriString)
352 if err != nil {
353 return endpoint, fmt.Errorf("Failed to parse uri: %s", uriString)
354 }
355
356 return smithyendpoints.Endpoint{
357 URI: *uri,
358 Headers: http.Header{},
359 }, nil
360 }
361 if exprVal := params.Region; exprVal != nil {
362 _Region := *exprVal
363 _ = _Region
364 if exprVal := awsrulesfn.GetPartition(_Region); exprVal != nil {
365 _PartitionResult := *exprVal
366 _ = _PartitionResult
367 if _UseFIPS == true {
368 if _UseDualStack == true {
369 if true == _PartitionResult.SupportsFIPS {
370 if true == _PartitionResult.SupportsDualStack {
371 uriString := func() string {
372 var out strings.Builder
373 out.WriteString("https://lightsail-fips.")
374 out.WriteString(_Region)
375 out.WriteString(".")
376 out.WriteString(_PartitionResult.DualStackDnsSuffix)
377 return out.String()
378 }()
379
380 uri, err := url.Parse(uriString)
381 if err != nil {
382 return endpoint, fmt.Errorf("Failed to parse uri: %s", uriString)
383 }
384
385 return smithyendpoints.Endpoint{
386 URI: *uri,
387 Headers: http.Header{},
388 }, nil
389 }
390 }
391 return endpoint, fmt.Errorf("endpoint rule error, %s", "FIPS and DualStack are enabled, but this partition does not support one or both")
392 }
393 }
394 if _UseFIPS == true {
395 if _PartitionResult.SupportsFIPS == true {
396 uriString := func() string {
397 var out strings.Builder
398 out.WriteString("https://lightsail-fips.")
399 out.WriteString(_Region)
400 out.WriteString(".")
401 out.WriteString(_PartitionResult.DnsSuffix)
402 return out.String()
403 }()
404
405 uri, err := url.Parse(uriString)
406 if err != nil {
407 return endpoint, fmt.Errorf("Failed to parse uri: %s", uriString)
408 }
409
410 return smithyendpoints.Endpoint{
411 URI: *uri,
412 Headers: http.Header{},
413 }, nil
414 }
415 return endpoint, fmt.Errorf("endpoint rule error, %s", "FIPS is enabled but this partition does not support FIPS")
416 }
417 if _UseDualStack == true {
418 if true == _PartitionResult.SupportsDualStack {
419 uriString := func() string {
420 var out strings.Builder
421 out.WriteString("https://lightsail.")
422 out.WriteString(_Region)
423 out.WriteString(".")
424 out.WriteString(_PartitionResult.DualStackDnsSuffix)
425 return out.String()
426 }()
427
428 uri, err := url.Parse(uriString)
429 if err != nil {
430 return endpoint, fmt.Errorf("Failed to parse uri: %s", uriString)
431 }
432
433 return smithyendpoints.Endpoint{
434 URI: *uri,
435 Headers: http.Header{},
436 }, nil
437 }
438 return endpoint, fmt.Errorf("endpoint rule error, %s", "DualStack is enabled but this partition does not support DualStack")
439 }
440 uriString := func() string {
441 var out strings.Builder
442 out.WriteString("https://lightsail.")
443 out.WriteString(_Region)
444 out.WriteString(".")
445 out.WriteString(_PartitionResult.DnsSuffix)
446 return out.String()
447 }()
448
449 uri, err := url.Parse(uriString)
450 if err != nil {
451 return endpoint, fmt.Errorf("Failed to parse uri: %s", uriString)
452 }
453
454 return smithyendpoints.Endpoint{
455 URI: *uri,
456 Headers: http.Header{},
457 }, nil
458 }
459 return endpoint, fmt.Errorf("Endpoint resolution failed. Invalid operation or environment input.")
460 }
461 return endpoint, fmt.Errorf("endpoint rule error, %s", "Invalid Configuration: Missing Region")
462 }
463
464 type endpointParamsBinder interface {
465 bindEndpointParams(*EndpointParameters)
466 }
467
468 func bindEndpointParams(ctx context.Context, input interface{}, options Options) (*EndpointParameters, error) {
469 params := &EndpointParameters{}
470
471 region, err := bindRegion(options.Region)
472 if err != nil {
473 return nil, err
474 }
475 params.Region = region
476
477 params.UseDualStack = aws.Bool(options.EndpointOptions.UseDualStackEndpoint == aws.DualStackEndpointStateEnabled)
478 params.UseFIPS = aws.Bool(options.EndpointOptions.UseFIPSEndpoint == aws.FIPSEndpointStateEnabled)
479 params.Endpoint = options.BaseEndpoint
480
481 if b, ok := input.(endpointParamsBinder); ok {
482 b.bindEndpointParams(params)
483 }
484
485 return params, nil
486 }
487
488 type resolveEndpointV2Middleware struct {
489 options Options
490 }
491
492 func (*resolveEndpointV2Middleware) ID() string {
493 return "ResolveEndpointV2"
494 }
495
496 func (m *resolveEndpointV2Middleware) HandleFinalize(ctx context.Context, in middleware.FinalizeInput, next middleware.FinalizeHandler) (
497 out middleware.FinalizeOutput, metadata middleware.Metadata, err error,
498 ) {
499 _, span := tracing.StartSpan(ctx, "ResolveEndpoint")
500 defer span.End()
501
502 if awsmiddleware.GetRequiresLegacyEndpoints(ctx) {
503 return next.HandleFinalize(ctx, in)
504 }
505
506 req, ok := in.Request.(*smithyhttp.Request)
507 if !ok {
508 return out, metadata, fmt.Errorf("unknown transport type %T", in.Request)
509 }
510
511 if m.options.EndpointResolverV2 == nil {
512 return out, metadata, fmt.Errorf("expected endpoint resolver to not be nil")
513 }
514
515 params, err := bindEndpointParams(ctx, getOperationInput(ctx), m.options)
516 if err != nil {
517 return out, metadata, fmt.Errorf("failed to bind endpoint params, %w", err)
518 }
519 endpt, err := timeOperationMetric(ctx, "client.call.resolve_endpoint_duration",
520 func() (smithyendpoints.Endpoint, error) {
521 return m.options.EndpointResolverV2.ResolveEndpoint(ctx, *params)
522 })
523 if err != nil {
524 return out, metadata, fmt.Errorf("failed to resolve service endpoint, %w", err)
525 }
526
527 span.SetProperty("client.call.resolved_endpoint", endpt.URI.String())
528
529 if endpt.URI.RawPath == "" && req.URL.RawPath != "" {
530 endpt.URI.RawPath = endpt.URI.Path
531 }
532 req.URL.Scheme = endpt.URI.Scheme
533 req.URL.Host = endpt.URI.Host
534 req.URL.Path = smithyhttp.JoinPath(endpt.URI.Path, req.URL.Path)
535 req.URL.RawPath = smithyhttp.JoinPath(endpt.URI.RawPath, req.URL.RawPath)
536 for k := range endpt.Headers {
537 req.Header.Set(k, endpt.Headers.Get(k))
538 }
539
540 rscheme := getResolvedAuthScheme(ctx)
541 if rscheme == nil {
542 return out, metadata, fmt.Errorf("no resolved auth scheme")
543 }
544
545 opts, _ := smithyauth.GetAuthOptions(&endpt.Properties)
546 for _, o := range opts {
547 rscheme.SignerProperties.SetAll(&o.SignerProperties)
548 }
549
550 span.End()
551 return next.HandleFinalize(ctx, in)
552 }
553