|
| 1 | +package v4 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/sha256" |
| 6 | + "encoding/hex" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + |
| 10 | + v4Internal "github.com/aws/aws-sdk-go-v2/aws/signer/internal/v4" |
| 11 | + "github.com/aws/aws-sdk-go-v2/internal/sdk" |
| 12 | + "github.com/awslabs/smithy-go/middleware" |
| 13 | + smithyHTTP "github.com/awslabs/smithy-go/transport/http" |
| 14 | +) |
| 15 | + |
| 16 | +// HashComputationError indicates an error occurred while computing the signing hash |
| 17 | +type HashComputationError struct { |
| 18 | + Err error |
| 19 | +} |
| 20 | + |
| 21 | +// Error is the error message |
| 22 | +func (e *HashComputationError) Error() string { |
| 23 | + return fmt.Sprintf("failed to compute payload hash: %v", e.Err) |
| 24 | +} |
| 25 | + |
| 26 | +// Unwrap returns the underlying error if one is set |
| 27 | +func (e *HashComputationError) Unwrap() error { |
| 28 | + return e.Err |
| 29 | +} |
| 30 | + |
| 31 | +// SigningError indicates an error condition occurred while performing SigV4 signing |
| 32 | +type SigningError struct { |
| 33 | + Err error |
| 34 | +} |
| 35 | + |
| 36 | +func (e *SigningError) Error() string { |
| 37 | + return fmt.Sprintf("failed to sign request: %v", e.Err) |
| 38 | +} |
| 39 | + |
| 40 | +// Unwrap returns the underlying error cause |
| 41 | +func (e *SigningError) Unwrap() error { |
| 42 | + return e.Err |
| 43 | +} |
| 44 | + |
| 45 | +// UnsignedPayloadMiddleware sets the SigV4 request payload hash to unsigned |
| 46 | +type UnsignedPayloadMiddleware struct{} |
| 47 | + |
| 48 | +// ID returns the UnsignedPayloadMiddleware identifier |
| 49 | +func (m *UnsignedPayloadMiddleware) ID() string { |
| 50 | + return "SigV4UnsignedPayloadMiddleware" |
| 51 | +} |
| 52 | + |
| 53 | +// HandleFinalize sets the payload hash to be an unsigned payload |
| 54 | +func (m *UnsignedPayloadMiddleware) HandleFinalize(ctx context.Context, in middleware.FinalizeInput, next middleware.FinalizeHandler) ( |
| 55 | + out middleware.FinalizeOutput, metadata middleware.Metadata, err error, |
| 56 | +) { |
| 57 | + ctx = SetPayloadHash(ctx, v4Internal.UnsignedPayload) |
| 58 | + return next.HandleFinalize(ctx, in) |
| 59 | +} |
| 60 | + |
| 61 | +// ComputePayloadSHA256Middleware computes sha256 payload hash to sign |
| 62 | +type ComputePayloadSHA256Middleware struct{} |
| 63 | + |
| 64 | +// ID is the middleware name |
| 65 | +func (m *ComputePayloadSHA256Middleware) ID() string { |
| 66 | + return "ComputePayloadSHA256Middleware" |
| 67 | +} |
| 68 | + |
| 69 | +// HandleFinalize compute the payload hash for the request payload |
| 70 | +func (m *ComputePayloadSHA256Middleware) HandleFinalize(ctx context.Context, in middleware.FinalizeInput, next middleware.FinalizeHandler) ( |
| 71 | + out middleware.FinalizeOutput, metadata middleware.Metadata, err error, |
| 72 | +) { |
| 73 | + req, ok := in.Request.(*smithyHTTP.Request) |
| 74 | + if !ok { |
| 75 | + return out, metadata, &HashComputationError{Err: fmt.Errorf("unexpected request middleware type %T", in.Request)} |
| 76 | + } |
| 77 | + |
| 78 | + hash := sha256.New() |
| 79 | + _, err = io.Copy(hash, req.GetStream()) |
| 80 | + if err != nil { |
| 81 | + return out, metadata, &HashComputationError{Err: fmt.Errorf("failed to compute payload hash, %w", err)} |
| 82 | + } |
| 83 | + |
| 84 | + if err := req.RewindStream(); err != nil { |
| 85 | + return out, metadata, &HashComputationError{Err: fmt.Errorf("failed to seek body to start, %w", err)} |
| 86 | + } |
| 87 | + |
| 88 | + ctx = SetPayloadHash(ctx, hex.EncodeToString(hash.Sum(nil))) |
| 89 | + |
| 90 | + return next.HandleFinalize(ctx, in) |
| 91 | +} |
| 92 | + |
| 93 | +// SignHTTPRequestMiddleware is a `FinalizeMiddleware` implementation for SigV4 HTTP Signing |
| 94 | +type SignHTTPRequestMiddleware struct { |
| 95 | + signer HTTPSigner |
| 96 | +} |
| 97 | + |
| 98 | +// NewSignHTTPRequestMiddleware constructs a SignHTTPRequestMiddleware using the given Signer for signing requests |
| 99 | +func NewSignHTTPRequestMiddleware(signer HTTPSigner) *SignHTTPRequestMiddleware { |
| 100 | + return &SignHTTPRequestMiddleware{signer: signer} |
| 101 | +} |
| 102 | + |
| 103 | +// ID is the SignHTTPRequestMiddleware identifier |
| 104 | +func (s *SignHTTPRequestMiddleware) ID() string { |
| 105 | + return "SigV4SignHTTPRequestMiddleware" |
| 106 | +} |
| 107 | + |
| 108 | +// HandleFinalize will take the provided input and sign the request using the SigV4 authentication scheme |
| 109 | +func (s *SignHTTPRequestMiddleware) HandleFinalize(ctx context.Context, in middleware.FinalizeInput, next middleware.FinalizeHandler) ( |
| 110 | + out middleware.FinalizeOutput, metadata middleware.Metadata, err error, |
| 111 | +) { |
| 112 | + req, ok := in.Request.(*smithyHTTP.Request) |
| 113 | + if !ok { |
| 114 | + return out, metadata, &SigningError{Err: fmt.Errorf("unexpected request middleware type %T", in.Request)} |
| 115 | + } |
| 116 | + |
| 117 | + signingMetadata := GetSigningMetadata(ctx) |
| 118 | + payloadHash := GetPayloadHash(ctx) |
| 119 | + if len(payloadHash) == 0 { |
| 120 | + return out, metadata, &SigningError{Err: fmt.Errorf("computed payload hash missing from context")} |
| 121 | + } |
| 122 | + |
| 123 | + err = s.signer.SignHTTP(ctx, req.Request, payloadHash, signingMetadata.SigningName, signingMetadata.SigningRegion, sdk.NowTime()) |
| 124 | + if err != nil { |
| 125 | + return out, metadata, &SigningError{Err: fmt.Errorf("failed to sign http request, %w", err)} |
| 126 | + } |
| 127 | + |
| 128 | + return next.HandleFinalize(ctx, in) |
| 129 | +} |
| 130 | + |
| 131 | +// SigningMetadata contains the signing name and signing region to be used when signing |
| 132 | +// with SigV4 authentication scheme. |
| 133 | +type SigningMetadata struct { |
| 134 | + SigningName string |
| 135 | + SigningRegion string |
| 136 | +} |
| 137 | + |
| 138 | +type signingMetadataKey struct{} |
| 139 | + |
| 140 | +// GetSigningMetadata retrieves the SigningMetadata from context. If there is no SigningMetadata attached to the context |
| 141 | +// an zero-value SigningMetadata will be returned. |
| 142 | +func GetSigningMetadata(ctx context.Context) (v SigningMetadata) { |
| 143 | + v, _ = ctx.Value(signingMetadataKey{}).(SigningMetadata) |
| 144 | + return v |
| 145 | +} |
| 146 | + |
| 147 | +// SetSigningMetadata adds the provided metadata to the context |
| 148 | +func SetSigningMetadata(ctx context.Context, metadata SigningMetadata) context.Context { |
| 149 | + ctx = context.WithValue(ctx, signingMetadataKey{}, metadata) |
| 150 | + return ctx |
| 151 | +} |
| 152 | + |
| 153 | +type payloadHashKey struct{} |
| 154 | + |
| 155 | +// GetPayloadHash retrieves the payload hash to use for signing |
| 156 | +func GetPayloadHash(ctx context.Context) (v string) { |
| 157 | + v, _ = ctx.Value(payloadHashKey{}).(string) |
| 158 | + return v |
| 159 | +} |
| 160 | + |
| 161 | +// SetPayloadHash sets the payload hash to be used for signing the request |
| 162 | +func SetPayloadHash(ctx context.Context, hash string) context.Context { |
| 163 | + ctx = context.WithValue(ctx, payloadHashKey{}, hash) |
| 164 | + return ctx |
| 165 | +} |
0 commit comments