v4signer_adapter.go raw

   1  package smithy
   2  
   3  import (
   4  	"context"
   5  	"fmt"
   6  
   7  	v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
   8  	internalcontext "github.com/aws/aws-sdk-go-v2/internal/context"
   9  	"github.com/aws/aws-sdk-go-v2/internal/sdk"
  10  	"github.com/aws/smithy-go"
  11  	"github.com/aws/smithy-go/auth"
  12  	"github.com/aws/smithy-go/logging"
  13  	smithyhttp "github.com/aws/smithy-go/transport/http"
  14  )
  15  
  16  // V4SignerAdapter adapts v4.HTTPSigner to smithy http.Signer.
  17  type V4SignerAdapter struct {
  18  	Signer     v4.HTTPSigner
  19  	Logger     logging.Logger
  20  	LogSigning bool
  21  }
  22  
  23  var _ (smithyhttp.Signer) = (*V4SignerAdapter)(nil)
  24  
  25  // SignRequest signs the request with the provided identity.
  26  func (v *V4SignerAdapter) SignRequest(ctx context.Context, r *smithyhttp.Request, identity auth.Identity, props smithy.Properties) error {
  27  	ca, ok := identity.(*CredentialsAdapter)
  28  	if !ok {
  29  		return fmt.Errorf("unexpected identity type: %T", identity)
  30  	}
  31  
  32  	name, ok := smithyhttp.GetSigV4SigningName(&props)
  33  	if !ok {
  34  		return fmt.Errorf("sigv4 signing name is required")
  35  	}
  36  
  37  	region, ok := smithyhttp.GetSigV4SigningRegion(&props)
  38  	if !ok {
  39  		return fmt.Errorf("sigv4 signing region is required")
  40  	}
  41  
  42  	hash := v4.GetPayloadHash(ctx)
  43  	signingTime := sdk.NowTime()
  44  	skew := internalcontext.GetAttemptSkewContext(ctx)
  45  	signingTime = signingTime.Add(skew)
  46  	err := v.Signer.SignHTTP(ctx, ca.Credentials, r.Request, hash, name, region, signingTime, func(o *v4.SignerOptions) {
  47  		o.DisableURIPathEscaping, _ = smithyhttp.GetDisableDoubleEncoding(&props)
  48  
  49  		o.Logger = v.Logger
  50  		o.LogSigning = v.LogSigning
  51  	})
  52  	if err != nil {
  53  		return fmt.Errorf("sign http: %w", err)
  54  	}
  55  
  56  	return nil
  57  }
  58