v4signer_adapter.go 1.4 KB

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