//go:build hostedlive package main import ( "context" "time" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/lambdacore" vms "github.com/aws/aws-sdk-go-v2/service/lambdamicrovms" "github.com/aws/aws-sdk-go-v2/service/sts" "github.com/aws/smithy-go/middleware" ) // callTimeout bounds each SDK request, the STS check included. A var so // the offline tests can shorten it. var callTimeout = 60 * time.Second // newClients builds the three clients from cfg, pinned so nothing in the // environment or the profile can steer them (livectl review round 1): // // - endpoints: every configured endpoint override is dropped // (AWS_ENDPOINT_URL, AWS_ENDPOINT_URL_, a profile's // endpoint_url or services section), FIPS and dual-stack are off and // the region is wantRegion, so each call goes to the standard // wantRegion endpoint the SDK's own resolver gives; the STS answer the // account guard reads therefore comes from the same AWS the Lambda // calls reach; // - retries: exactly one attempt per call, whatever AWS_MAX_ATTEMPTS or // the profile's max_attempts and retry_mode say, so a failed mutation // is never resent behind the plan's back; // - deadlines: each call gets callTimeout on its context. func newClients(cfg aws.Config) (*lambdacore.Client, *vms.Client, *sts.Client) { core := lambdacore.NewFromConfig(cfg, func(o *lambdacore.Options) { o.Region, o.BaseEndpoint = wantRegion, nil o.EndpointOptions = lambdacore.EndpointResolverOptions{} o.EndpointResolverV2 = lambdacore.NewDefaultEndpointResolverV2() o.Retryer, o.RetryMaxAttempts = aws.NopRetryer{}, 1 o.APIOptions = append(o.APIOptions, deadlineEach) }) vm := vms.NewFromConfig(cfg, func(o *vms.Options) { o.Region, o.BaseEndpoint = wantRegion, nil o.EndpointOptions = vms.EndpointResolverOptions{} o.EndpointResolverV2 = vms.NewDefaultEndpointResolverV2() o.Retryer, o.RetryMaxAttempts = aws.NopRetryer{}, 1 o.APIOptions = append(o.APIOptions, deadlineEach) }) st := sts.NewFromConfig(cfg, func(o *sts.Options) { o.Region, o.BaseEndpoint = wantRegion, nil o.EndpointOptions = sts.EndpointResolverOptions{} o.EndpointResolverV2 = sts.NewDefaultEndpointResolverV2() o.Retryer, o.RetryMaxAttempts = aws.NopRetryer{}, 1 o.APIOptions = append(o.APIOptions, deadlineEach) }) return core, vm, st } // deadlineEach adds callTimeout to the context of every operation, first in // the stack, so the whole call (signing, sending, reading the answer) is // bounded even when the caller's context is not. func deadlineEach(s *middleware.Stack) error { return s.Initialize.Add(middleware.InitializeMiddlewareFunc("LivectlCallDeadline", func(ctx context.Context, in middleware.InitializeInput, next middleware.InitializeHandler) (middleware.InitializeOutput, middleware.Metadata, error) { ctx, cancel := context.WithTimeout(ctx, callTimeout) defer cancel() return next.HandleInitialize(ctx, in) }), middleware.Before) }