| 1 | //go:build hostedlive |
| 2 | |
| 3 | package main |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/aws/aws-sdk-go-v2/aws" |
| 10 | "github.com/aws/aws-sdk-go-v2/service/lambdacore" |
| 11 | vms "github.com/aws/aws-sdk-go-v2/service/lambdamicrovms" |
| 12 | "github.com/aws/aws-sdk-go-v2/service/sts" |
| 13 | "github.com/aws/smithy-go/middleware" |
| 14 | ) |
| 15 | |
| 16 | // callTimeout bounds each SDK request, the STS check included. A var so |
| 17 | // the offline tests can shorten it. |
| 18 | var callTimeout = 60 * time.Second |
| 19 | |
| 20 | // newClients builds the three clients from cfg, pinned so nothing in the |
| 21 | // environment or the profile can steer them (livectl review round 1): |
| 22 | // |
| 23 | // - endpoints: every configured endpoint override is dropped |
| 24 | // (AWS_ENDPOINT_URL, AWS_ENDPOINT_URL_<SERVICE>, a profile's |
| 25 | // endpoint_url or services section), FIPS and dual-stack are off and |
| 26 | // the region is wantRegion, so each call goes to the standard |
| 27 | // wantRegion endpoint the SDK's own resolver gives; the STS answer the |
| 28 | // account guard reads therefore comes from the same AWS the Lambda |
| 29 | // calls reach; |
| 30 | // - retries: exactly one attempt per call, whatever AWS_MAX_ATTEMPTS or |
| 31 | // the profile's max_attempts and retry_mode say, so a failed mutation |
| 32 | // is never resent behind the plan's back; |
| 33 | // - deadlines: each call gets callTimeout on its context. |
| 34 | func newClients(cfg aws.Config) (*lambdacore.Client, *vms.Client, *sts.Client) { |
| 35 | core := lambdacore.NewFromConfig(cfg, func(o *lambdacore.Options) { |
| 36 | o.Region, o.BaseEndpoint = wantRegion, nil |
| 37 | o.EndpointOptions = lambdacore.EndpointResolverOptions{} |
| 38 | o.EndpointResolverV2 = lambdacore.NewDefaultEndpointResolverV2() |
| 39 | o.Retryer, o.RetryMaxAttempts = aws.NopRetryer{}, 1 |
| 40 | o.APIOptions = append(o.APIOptions, deadlineEach) |
| 41 | }) |
| 42 | vm := vms.NewFromConfig(cfg, func(o *vms.Options) { |
| 43 | o.Region, o.BaseEndpoint = wantRegion, nil |
| 44 | o.EndpointOptions = vms.EndpointResolverOptions{} |
| 45 | o.EndpointResolverV2 = vms.NewDefaultEndpointResolverV2() |
| 46 | o.Retryer, o.RetryMaxAttempts = aws.NopRetryer{}, 1 |
| 47 | o.APIOptions = append(o.APIOptions, deadlineEach) |
| 48 | }) |
| 49 | st := sts.NewFromConfig(cfg, func(o *sts.Options) { |
| 50 | o.Region, o.BaseEndpoint = wantRegion, nil |
| 51 | o.EndpointOptions = sts.EndpointResolverOptions{} |
| 52 | o.EndpointResolverV2 = sts.NewDefaultEndpointResolverV2() |
| 53 | o.Retryer, o.RetryMaxAttempts = aws.NopRetryer{}, 1 |
| 54 | o.APIOptions = append(o.APIOptions, deadlineEach) |
| 55 | }) |
| 56 | return core, vm, st |
| 57 | } |
| 58 | |
| 59 | // deadlineEach adds callTimeout to the context of every operation, first in |
| 60 | // the stack, so the whole call (signing, sending, reading the answer) is |
| 61 | // bounded even when the caller's context is not. |
| 62 | func deadlineEach(s *middleware.Stack) error { |
| 63 | return s.Initialize.Add(middleware.InitializeMiddlewareFunc("LivectlCallDeadline", |
| 64 | func(ctx context.Context, in middleware.InitializeInput, next middleware.InitializeHandler) (middleware.InitializeOutput, middleware.Metadata, error) { |
| 65 | ctx, cancel := context.WithTimeout(ctx, callTimeout) |
| 66 | defer cancel() |
| 67 | return next.HandleInitialize(ctx, in) |
| 68 | }), middleware.Before) |
| 69 | } |
| 70 | |