Repository navigation
fix(sqs): store and return message attributes (#461) - #465
Merged
Merged
Conversation
#454 parsed message attributes in order to measure them against `MaximumMessageSize` and then discarded them: `SQSMessage.MessageAttributes` existed and was never populated, and `receiveMessage` had no attribute handling at all. A consumer routing on an attribute -- a `messageType` discriminator, a trace ID, a tenant key -- saw every message fall to its default branch, with no error to explain why, because the attribute was simply not there. `SendMessage`, `SendMessageBatch` and `ReceiveMessage` now carry them under both the query and JSON protocols. Attributes are returned only for the names a receive asks for: `All` and `.*` for everything, a bare name for one, and the documented `prefix.*` form. Returning them unconditionally would fail in the permissive direction -- a consumer whose production caller never sets `MessageAttributeNames` would pass against substrate and read nothing from real SQS. `MD5OfMessageAttributes` is computed with the algorithm published in the developer guide and pinned against three real-AWS digests, each failing on a different mistake: the minimal single-attribute case, a custom data-type suffix hashed in full (`Number.java.lang.Long`, not its `Number` base), and a binary value hashed raw rather than base64 -- base64 yields 5ff413c9dc7bd18abea88ca05643f902 where AWS yields 049075255ebc53fb95f7f9f3cedf3c50. That makes #461's known-good-digest criterion satisfiable with no network access. The field is omitted entirely for a message with no attributes rather than reported as the MD5 of zero bytes, which a caller could compare against and "successfully" verify. On a receive the digest covers what is returned, not what was sent, so a named subset gets that subset's digest. A deduplicated FIFO send reports its own request's digest rather than the stored original's. Attributes are emitted in name order, since a Go map iterates randomly and two identical requests must not produce different responses. The 10-attribute maximum and the attribute-name character rules remain unenforced and are tracked separately; a 10-attribute message is accepted and pinned as the boundary. Closes #461
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #461
The defect
#454 parsed message attributes in order to measure them against
MaximumMessageSize, and then dropped them on the floor:SQSMessage.MessageAttributesexisted insqs_types.goand was never populated at any of the four message-construction sites.receiveMessagehad no attribute handling at all — no selector parsing, no response fields.A consumer routing on an attribute — a
messageTypediscriminator, a trace ID, a tenant key — saw every message fall to its default branch, with no error to explain why. The attribute was simply not there, which reads as "the producer didn't set it" rather than "the emulator dropped it."What now works
SendMessage,SendMessageBatchandReceiveMessageall carry attributes, under both the query and JSON protocols.Attributes are returned only for the names a receive asks for. This is the trap in the permissive direction: a consumer whose production caller never sets
MessageAttributeNameswould pass against an emulator that volunteered them, then read none from real SQS. All documented selector forms work:MessageAttributeNameAll/.*messageTypetrace.*trace.…A named attribute the message doesn't carry is absent rather than an error — the selector says what to return, not that it must exist.
MD5OfMessageAttributesImplemented from the developer guide's "Calculating the MD5 message digest for message attributes": sort by name, then per attribute a 4-byte big-endian length + UTF-8 name, the same for the data type, one transport byte (
1String/Number,2Binary), then the value's length and bytes.Pinned against three real-AWS digests, captured in moto's suite from recorded live responses. This is what makes #461's "verified against a known-good digest" acceptance criterion satisfiable with no network access — the vectors ship in the test file with their provenance, and they are not interchangeable:
36655e7e9d7c0e8479fa3f3f42247ae7b12289320bb6e494b18b645ef562b4a9Number.java.lang.Longis truncated toNumber049075255ebc53fb95f7f9f3cedf3c505ff413c9dc7bd18abea88ca05643f902Vector 3 is the same raw-versus-encoded distinction #454 drew for size measurement, except this time there's a hash to settle it rather than an argument from the docs. Base64 is what travels on the wire, so hashing it is the natural mistake.
Three further behaviours, each with its own test:
d41d8cd9…). A digest of nothing is a value a caller could compare against and "successfully" verify — worse than no value.Attributes are emitted in name order. Real SQS promises no order, but a Go map iterates randomly, so map order would make two identical requests produce different responses — the one property this emulator exists to provide.
Mutation-tested
All six mutants compiled (
go vetgate) and were caught:ReturnedOnlyWhenRequested/omitted_returns_none+ subset-digest testDigestIsOrderIndependent, subset-digest testReceiveDigestCoversTheReturnedSubsetBinaryValuepresence instead ofDataTypeBinaryIsIdentifiedByDataTypeThe last one initially survived — my first attempt at that test asserted an inequality that held for the wrong reason. It's now pinned to a digest derived from the published algorithm (
6f8879e6be14e9d1c45ef3e9631b86c9; the value-keyed variant yieldse275a101bd238d68e457c2f49e1ad0e7), and the mutant fails. Noting it because the test's value is entirely in that distinction: an empty-valuedBinaryattribute is the only input where the two rules diverge, and real SQS rejects it outright, so there's no captured digest to compare against.Tests
emulator/sqs_messageattributes_test.go— 10 tests, allbothProtocolswhere protocol-independent: the three known-good vectors, order-independence, omission with no attributes, full round-trip of String/Number/custom-Number/Binary (binary byte-identical after base64), the eight selection outcomes, receive-time subset digest, batch round-trip with per-entry digests, survival across redelivery, theBinary-by-data-type case, and a 10-attribute message accepted as the boundary.Verification
From the repo root:
gofmt -l .clean,go build ./...,go vet ./...,golangci-lint run ./...→ 0 issues,make test(race) green,make docs-reference-check docs-versionsgreen,test/e2egreen.Docs
docs/services.mdgains §"Message attributes" with the selector table, the digest algorithm and both load-bearing details, the omission rule, the receive-subset rule, and the ordering note. The stale "substrate does not yet store them or return them fromReceiveMessage" line in §"Message size enforcement" is replaced with a pointer. The three SQS operation rows link to the new section.Recorded as not enforced (follow-ups): the documented 10-attribute maximum, and the attribute-name character rules (no
AWS./Amazon.prefix, no leading/trailing/sequential periods). Both are newly reachable now that attributes are stored.