|
| 1 | +package sqs |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/md5" |
| 6 | + "encoding/hex" |
| 7 | + "fmt" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 11 | + sqstypes "github.com/aws/aws-sdk-go-v2/service/sqs/types" |
| 12 | + "github.com/aws/smithy-go/middleware" |
| 13 | +) |
| 14 | + |
| 15 | +// addValidateSendMessageChecksum adds the ValidateMessageChecksum middleware |
| 16 | +// to the stack configured for the SendMessage Operation. |
| 17 | +func addValidateSendMessageChecksum(stack *middleware.Stack, o Options) error { |
| 18 | + return addValidateMessageChecksum(stack, o, validateSendMessageChecksum) |
| 19 | +} |
| 20 | + |
| 21 | +// validateSendMessageChecksum validates the SendMessage operation's input |
| 22 | +// message payload MD5 checksum matches that returned by the API. |
| 23 | +// |
| 24 | +// The input and output types must match the SendMessage operation. |
| 25 | +func validateSendMessageChecksum(input, output interface{}) error { |
| 26 | + in, ok := input.(*SendMessageInput) |
| 27 | + if !ok { |
| 28 | + return fmt.Errorf("wrong input type, expect %T, got %T", in, input) |
| 29 | + } |
| 30 | + out, ok := output.(*SendMessageOutput) |
| 31 | + if !ok { |
| 32 | + return fmt.Errorf("wrong output type, expect %T, got %T", out, output) |
| 33 | + } |
| 34 | + |
| 35 | + // Nothing to validate if the members aren't populated. |
| 36 | + if in.MessageBody == nil || out.MD5OfMessageBody == nil { |
| 37 | + return nil |
| 38 | + } |
| 39 | + |
| 40 | + if err := validateMessageChecksum(*in.MessageBody, *out.MD5OfMessageBody); err != nil { |
| 41 | + return messageChecksumError{ |
| 42 | + MessageID: aws.ToString(out.MessageId), |
| 43 | + Err: err, |
| 44 | + } |
| 45 | + } |
| 46 | + return nil |
| 47 | +} |
| 48 | + |
| 49 | +// addValidateSendMessageBatchChecksum adds the ValidateMessagechecksum |
| 50 | +// middleware to the stack configured for the SendMessageBatch operation. |
| 51 | +func addValidateSendMessageBatchChecksum(stack *middleware.Stack, o Options) error { |
| 52 | + return addValidateMessageChecksum(stack, o, validateSendMessageBatchChecksum) |
| 53 | +} |
| 54 | + |
| 55 | +// validateSendMessageBatchChecksum validates the SendMessageBatch operation's |
| 56 | +// input messages body MD5 checksum matches those returned by the API. |
| 57 | +// |
| 58 | +// The input and output types must match the SendMessageBatch operation. |
| 59 | +func validateSendMessageBatchChecksum(input, output interface{}) error { |
| 60 | + in, ok := input.(*SendMessageBatchInput) |
| 61 | + if !ok { |
| 62 | + return fmt.Errorf("wrong input type, expect %T, got %T", in, input) |
| 63 | + } |
| 64 | + out, ok := output.(*SendMessageBatchOutput) |
| 65 | + if !ok { |
| 66 | + return fmt.Errorf("wrong output type, expect %T, got %T", out, output) |
| 67 | + } |
| 68 | + |
| 69 | + outEntries := map[string]sqstypes.SendMessageBatchResultEntry{} |
| 70 | + for _, e := range out.Successful { |
| 71 | + outEntries[*e.Id] = e |
| 72 | + } |
| 73 | + |
| 74 | + var failedMessageErrs []messageChecksumError |
| 75 | + for _, inEntry := range in.Entries { |
| 76 | + outEntry, ok := outEntries[*inEntry.Id] |
| 77 | + // Nothing to validate if the members aren't populated. |
| 78 | + if !ok || inEntry.MessageBody == nil || outEntry.MD5OfMessageBody == nil { |
| 79 | + continue |
| 80 | + } |
| 81 | + |
| 82 | + if err := validateMessageChecksum(*inEntry.MessageBody, *outEntry.MD5OfMessageBody); err != nil { |
| 83 | + failedMessageErrs = append(failedMessageErrs, messageChecksumError{ |
| 84 | + MessageID: aws.ToString(outEntry.MessageId), |
| 85 | + Err: err, |
| 86 | + }) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if len(failedMessageErrs) != 0 { |
| 91 | + return batchMessageChecksumError{ |
| 92 | + Errs: failedMessageErrs, |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +// addValidateReceiveMessageChecksum adds the ValidateMessagechecksum |
| 100 | +// middleware to the stack configured for the ReceiveMessage operation. |
| 101 | +func addValidateReceiveMessageChecksum(stack *middleware.Stack, o Options) error { |
| 102 | + return addValidateMessageChecksum(stack, o, validateReceiveMessageChecksum) |
| 103 | +} |
| 104 | + |
| 105 | +// validateReceiveMessageChecksum validates the ReceiveMessage operation's |
| 106 | +// input messages body MD5 checksum matches those returned by the API. |
| 107 | +// |
| 108 | +// The input and output types must match the ReceiveMessage operation. |
| 109 | +func validateReceiveMessageChecksum(_, output interface{}) error { |
| 110 | + out, ok := output.(*ReceiveMessageOutput) |
| 111 | + if !ok { |
| 112 | + return fmt.Errorf("wrong output type, expect %T, got %T", out, output) |
| 113 | + } |
| 114 | + |
| 115 | + var failedMessageErrs []messageChecksumError |
| 116 | + for _, msg := range out.Messages { |
| 117 | + // Nothing to validate if the members aren't populated. |
| 118 | + if msg.Body == nil || msg.MD5OfBody == nil { |
| 119 | + continue |
| 120 | + } |
| 121 | + |
| 122 | + if err := validateMessageChecksum(*msg.Body, *msg.MD5OfBody); err != nil { |
| 123 | + failedMessageErrs = append(failedMessageErrs, messageChecksumError{ |
| 124 | + MessageID: aws.ToString(msg.MessageId), |
| 125 | + Err: err, |
| 126 | + }) |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + if len(failedMessageErrs) != 0 { |
| 131 | + return batchMessageChecksumError{ |
| 132 | + Errs: failedMessageErrs, |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return nil |
| 137 | +} |
| 138 | + |
| 139 | +// messageChecksumValidator provides the function signature for the operation's |
| 140 | +// validator. |
| 141 | +type messageChecksumValidator func(input, output interface{}) error |
| 142 | + |
| 143 | +// addValidateMessageChecksum adds the ValidateMessageChecksum middleware to |
| 144 | +// the stack with the passed in validator specified. |
| 145 | +func addValidateMessageChecksum(stack *middleware.Stack, o Options, validate messageChecksumValidator) error { |
| 146 | + if o.DisableMessageChecksumValidation { |
| 147 | + return nil |
| 148 | + } |
| 149 | + |
| 150 | + m := validateMessageChecksumMiddleware{ |
| 151 | + validate: validate, |
| 152 | + } |
| 153 | + err := stack.Initialize.Add(m, middleware.Before) |
| 154 | + if err != nil { |
| 155 | + return fmt.Errorf("failed to add %s middleware, %w", m.ID(), err) |
| 156 | + } |
| 157 | + |
| 158 | + return nil |
| 159 | +} |
| 160 | + |
| 161 | +// validateMessageChecksumMiddleware provides the Initialize middleware for |
| 162 | +// validating an operation's message checksum is validate. Needs to b |
| 163 | +// configured with the operation's validator. |
| 164 | +type validateMessageChecksumMiddleware struct { |
| 165 | + validate messageChecksumValidator |
| 166 | +} |
| 167 | + |
| 168 | +// ID returns the Middleware ID. |
| 169 | +func (validateMessageChecksumMiddleware) ID() string { return "SQSValidateMessageChecksum" } |
| 170 | + |
| 171 | +// HandleInitialize implements the InitializeMiddleware interface providing a |
| 172 | +// middleware that will validate an operation's message checksum based on |
| 173 | +// calling the validate member. |
| 174 | +func (m validateMessageChecksumMiddleware) HandleInitialize( |
| 175 | + ctx context.Context, input middleware.InitializeInput, next middleware.InitializeHandler, |
| 176 | +) ( |
| 177 | + out middleware.InitializeOutput, meta middleware.Metadata, err error, |
| 178 | +) { |
| 179 | + out, meta, err = next.HandleInitialize(ctx, input) |
| 180 | + if err != nil { |
| 181 | + return out, meta, err |
| 182 | + } |
| 183 | + |
| 184 | + err = m.validate(input.Parameters, out.Result) |
| 185 | + if err != nil { |
| 186 | + return out, meta, fmt.Errorf("message checksum validation failed, %w", err) |
| 187 | + } |
| 188 | + |
| 189 | + return out, meta, nil |
| 190 | +} |
| 191 | + |
| 192 | +// validateMessageChecksum compares the MD5 checksums of value parameter with |
| 193 | +// the expected MD5 value. Returns an error if the computed checksum does not |
| 194 | +// match the expected value. |
| 195 | +func validateMessageChecksum(value, expect string) error { |
| 196 | + msum := md5.Sum([]byte(value)) |
| 197 | + sum := hex.EncodeToString(msum[:]) |
| 198 | + if sum != expect { |
| 199 | + return fmt.Errorf("expected MD5 checksum %s, got %s", expect, sum) |
| 200 | + } |
| 201 | + |
| 202 | + return nil |
| 203 | +} |
| 204 | + |
| 205 | +// messageChecksumError provides an error type for invalid message checksums. |
| 206 | +type messageChecksumError struct { |
| 207 | + MessageID string |
| 208 | + Err error |
| 209 | +} |
| 210 | + |
| 211 | +func (e messageChecksumError) Error() string { |
| 212 | + prefix := "message" |
| 213 | + if e.MessageID != "" { |
| 214 | + prefix += " " + e.MessageID |
| 215 | + } |
| 216 | + return fmt.Sprintf("%s has invalid checksum, %v", prefix, e.Err.Error()) |
| 217 | +} |
| 218 | + |
| 219 | +// batchMessageChecksumError provides an error type for a collection of invalid |
| 220 | +// message checksum errors. |
| 221 | +type batchMessageChecksumError struct { |
| 222 | + Errs []messageChecksumError |
| 223 | +} |
| 224 | + |
| 225 | +func (e batchMessageChecksumError) Error() string { |
| 226 | + var w strings.Builder |
| 227 | + fmt.Fprintf(&w, "message checksum errors") |
| 228 | + |
| 229 | + for _, err := range e.Errs { |
| 230 | + fmt.Fprintf(&w, "\n\t%s", err.Error()) |
| 231 | + } |
| 232 | + |
| 233 | + return w.String() |
| 234 | +} |
0 commit comments