mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 09:22:49 -05:00
fix(sns): give all subscribers of a message the same Timestamp (#7508)
This commit is contained in:
1 parent
ee78a41f16
commit
e657225711
4 files changed
+98
-35
No files matched your search
@@ -24,6 +24,7 @@ import java.security.PrivateKey;
|
||||
import java.security.Signature;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@@ -100,10 +101,18 @@ public class SnsMessageSigner implements Resettable {
|
||||
* {@code SigningCertURL} for HTTP and SQS envelopes, {@code SigningCertUrl} in Lambda records.
|
||||
*/
|
||||
public void sign(ObjectNode node, String signatureVersion, String certUrlField) {
|
||||
sign(node, signatureVersion, certUrlField, new HashMap<>());
|
||||
}
|
||||
|
||||
/** Like the three-argument form, reusing a signature already made for the same canonical string. */
|
||||
public void sign(ObjectNode node, String signatureVersion, String certUrlField,
|
||||
Map<String, String> signatures) {
|
||||
String version = normalizeSignatureVersion(signatureVersion);
|
||||
Material current = material();
|
||||
String canonical = canonicalString(node);
|
||||
node.put("SignatureVersion", version);
|
||||
node.put("Signature", signature(canonicalString(node), version, current.privateKey()));
|
||||
node.put("Signature", signatures.computeIfAbsent(current.fileName() + "\n" + version + "\n" + canonical,
|
||||
key -> signature(canonical, version, current.privateKey())));
|
||||
node.put(certUrlField, baseUrl + CERTIFICATE_PATH + current.fileName());
|
||||
}
|
||||
|
||||
|
||||
@@ -1126,6 +1126,8 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
}
|
||||
|
||||
String messageId = UUID.randomUUID().toString();
|
||||
Notification notification = new Notification(messageId,
|
||||
DateTimeFormatter.ISO_INSTANT.format(Instant.now()), signatureVersion(topic), new HashMap<>());
|
||||
JsonNode parsedBody = null;
|
||||
boolean bodyParseAttempted = false;
|
||||
for (Subscription sub : subscriptionsByTopic(effectiveArn, region)) {
|
||||
@@ -1140,8 +1142,8 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
if (!matchesFilterPolicy(sub, parsedBody, messageAttributes)) {
|
||||
continue;
|
||||
}
|
||||
deliverMessage(sub, message, subject, messageAttributes, messageId, effectiveArn, messageGroupId, dedupId, messageStructure,
|
||||
signatureVersion(topic));
|
||||
deliverMessage(sub, message, subject, messageAttributes, notification, effectiveArn, messageGroupId, dedupId,
|
||||
messageStructure);
|
||||
}
|
||||
LOG.infov("Published message {0} to topic {1}", messageId, effectiveArn);
|
||||
return messageId;
|
||||
@@ -1562,6 +1564,8 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
boolean groupScopedDedup = isGroupScopedDeduplication(topic);
|
||||
List<String[]> successful = new ArrayList<>();
|
||||
List<String[]> failed = new ArrayList<>();
|
||||
String timestamp = DateTimeFormatter.ISO_INSTANT.format(Instant.now());
|
||||
String version = signatureVersion(topic);
|
||||
for (Map<String, Object> entry : entries) {
|
||||
String id = (String) entry.get("Id");
|
||||
String message = (String) entry.get("Message");
|
||||
@@ -1598,6 +1602,7 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
}
|
||||
|
||||
String messageId = UUID.randomUUID().toString();
|
||||
Notification notification = new Notification(messageId, timestamp, version, new HashMap<>());
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, MessageAttributeValue> attrs = (Map<String, MessageAttributeValue>) entry.get("MessageAttributes");
|
||||
JsonNode parsedBody = null;
|
||||
@@ -1609,8 +1614,8 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
bodyParseAttempted = true;
|
||||
}
|
||||
if (!matchesFilterPolicy(sub, parsedBody, attrs)) continue;
|
||||
deliverMessage(sub, message, subject, attrs, messageId, topicArn, messageGroupId, messageDeduplicationId, messageStructure,
|
||||
signatureVersion(topic));
|
||||
deliverMessage(sub, message, subject, attrs, notification, topicArn, messageGroupId, messageDeduplicationId,
|
||||
messageStructure);
|
||||
}
|
||||
LOG.debugv("Batch published message {0} (id={1}) to {2}", messageId, id, topicArn);
|
||||
successful.add(new String[]{id, messageId});
|
||||
@@ -2490,10 +2495,15 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
return result;
|
||||
}
|
||||
|
||||
/** What every subscriber of one published message shares. AWS stamps and signs a publish once. */
|
||||
private record Notification(String messageId, String timestamp, String signatureVersion,
|
||||
Map<String, String> signatures) { }
|
||||
|
||||
private void deliverMessage(Subscription sub, String message, String subject,
|
||||
Map<String, MessageAttributeValue> messageAttributes, String messageId,
|
||||
Map<String, MessageAttributeValue> messageAttributes, Notification notification,
|
||||
String topicArn, String messageGroupId, String messageDeduplicationId,
|
||||
String messageStructure, String signatureVersion) {
|
||||
String messageStructure) {
|
||||
String messageId = notification.messageId();
|
||||
try {
|
||||
// Under MessageStructure="json" every subscriber gets the value under its own
|
||||
// protocol key, falling back to "default". The application case resolves against
|
||||
@@ -2511,8 +2521,8 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
boolean rawDelivery = "true".equalsIgnoreCase(sub.getAttributes().get("RawMessageDelivery"));
|
||||
String body = rawDelivery
|
||||
? protocolMessage
|
||||
: buildSnsEnvelope(protocolMessage, subject, messageAttributes, topicArn, messageId,
|
||||
sub.getSubscriptionArn(), signatureVersion);
|
||||
: buildSnsEnvelope(protocolMessage, subject, messageAttributes, topicArn, notification,
|
||||
sub.getSubscriptionArn());
|
||||
Map<String, MessageAttributeValue> sqsAttributes = rawDelivery
|
||||
? toSqsMessageAttributes(messageAttributes)
|
||||
: Collections.emptyMap();
|
||||
@@ -2521,8 +2531,8 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
}
|
||||
case "lambda" -> {
|
||||
String region = extractRegionFromArn(sub.getEndpoint());
|
||||
String eventJson = buildSnsLambdaEvent(topicArn, messageId, protocolMessage,
|
||||
subject, messageAttributes, sub.getSubscriptionArn(), signatureVersion);
|
||||
String eventJson = buildSnsLambdaEvent(topicArn, notification, protocolMessage,
|
||||
subject, messageAttributes, sub.getSubscriptionArn());
|
||||
lambdaService.invoke(region, sub.getEndpoint(), eventJson.getBytes(), InvocationType.Event);
|
||||
LOG.debugv("Delivered SNS message to Lambda: {0}", sub.getEndpoint());
|
||||
}
|
||||
@@ -2533,8 +2543,8 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
boolean rawDelivery = "true".equalsIgnoreCase(sub.getAttributes().get("RawMessageDelivery"));
|
||||
String body = rawDelivery
|
||||
? protocolMessage
|
||||
: buildSnsHttpNotification(protocolMessage, subject, messageAttributes, topicArn, messageId,
|
||||
sub.getSubscriptionArn(), signatureVersion);
|
||||
: buildSnsHttpNotification(protocolMessage, subject, messageAttributes, topicArn, notification,
|
||||
sub.getSubscriptionArn());
|
||||
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder()
|
||||
.uri(URI.create(sub.getEndpoint()))
|
||||
.timeout(Duration.ofSeconds(5))
|
||||
@@ -2609,8 +2619,8 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
&& "true".equalsIgnoreCase(sub.getAttributes().get("RawMessageDelivery"));
|
||||
String body = rawDelivery
|
||||
? protocolMessage
|
||||
: buildSnsEnvelope(protocolMessage, subject, messageAttributes, topicArn, messageId,
|
||||
null, null);
|
||||
: buildSnsEnvelope(protocolMessage, subject, messageAttributes, topicArn, notification,
|
||||
null);
|
||||
byte[] data = body.getBytes(StandardCharsets.UTF_8);
|
||||
firehoseService.putRecord(accountId, region, streamName, new Record(data));
|
||||
LOG.debugv("Delivered SNS message to Firehose: {0} ({1}) raw={2}", sub.getEndpoint(), streamName, rawDelivery);
|
||||
@@ -2702,14 +2712,13 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
private String buildSnsLambdaEvent(String topicArn, String messageId, String message,
|
||||
private String buildSnsLambdaEvent(String topicArn, Notification notification, String message,
|
||||
String subject, Map<String, MessageAttributeValue> messageAttributes,
|
||||
String subscriptionArn, String signatureVersion) {
|
||||
String subscriptionArn) {
|
||||
try {
|
||||
String timestamp = DateTimeFormatter.ISO_INSTANT.format(Instant.now());
|
||||
ObjectNode snsNode = objectMapper.createObjectNode();
|
||||
snsNode.put("Type", "Notification");
|
||||
snsNode.put("MessageId", messageId);
|
||||
snsNode.put("MessageId", notification.messageId());
|
||||
snsNode.put("TopicArn", topicArn);
|
||||
if (subject != null) {
|
||||
snsNode.put("Subject", subject);
|
||||
@@ -2717,8 +2726,8 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
snsNode.putNull("Subject");
|
||||
}
|
||||
snsNode.put("Message", message);
|
||||
snsNode.put("Timestamp", timestamp);
|
||||
messageSigner.sign(snsNode, signatureVersion, "SigningCertUrl");
|
||||
snsNode.put("Timestamp", notification.timestamp());
|
||||
messageSigner.sign(snsNode, notification.signatureVersion(), "SigningCertUrl", notification.signatures());
|
||||
snsNode.put("UnsubscribeUrl", unsubscribeUrl(subscriptionArn));
|
||||
ObjectNode attrs = snsNode.putObject("MessageAttributes");
|
||||
if (messageAttributes != null) {
|
||||
@@ -2768,21 +2777,19 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
*/
|
||||
private String buildSnsEnvelope(String message, String subject,
|
||||
Map<String, MessageAttributeValue> messageAttributes,
|
||||
String topicArn, String messageId, String subscriptionArn,
|
||||
String signatureVersion) {
|
||||
String topicArn, Notification notification, String subscriptionArn) {
|
||||
try {
|
||||
String timestamp = DateTimeFormatter.ISO_INSTANT.format(Instant.now());
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("Type", "Notification");
|
||||
node.put("MessageId", messageId);
|
||||
node.put("MessageId", notification.messageId());
|
||||
node.put("TopicArn", topicArn);
|
||||
node.put("Timestamp", timestamp);
|
||||
node.put("Timestamp", notification.timestamp());
|
||||
if (subject != null) {
|
||||
node.put("Subject", subject);
|
||||
}
|
||||
node.put("Message", message);
|
||||
if (subscriptionArn != null) {
|
||||
messageSigner.sign(node, signatureVersion, "SigningCertURL");
|
||||
messageSigner.sign(node, notification.signatureVersion(), "SigningCertURL", notification.signatures());
|
||||
node.put("UnsubscribeURL", unsubscribeUrl(subscriptionArn));
|
||||
}
|
||||
ObjectNode attrs = node.putObject("MessageAttributes");
|
||||
@@ -2800,7 +2807,7 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
}
|
||||
return objectMapper.writeValueAsString(node);
|
||||
} catch (JsonProcessingException e) {
|
||||
LOG.warnv("Failed to build SNS envelope for message {0}: {1}", messageId, e.getMessage());
|
||||
LOG.warnv("Failed to build SNS envelope for message {0}: {1}", notification.messageId(), e.getMessage());
|
||||
return "{}";
|
||||
}
|
||||
}
|
||||
@@ -2816,20 +2823,18 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
|
||||
private String buildSnsHttpNotification(String message, String subject,
|
||||
Map<String, MessageAttributeValue> messageAttributes,
|
||||
String topicArn, String messageId, String subscriptionArn,
|
||||
String signatureVersion) {
|
||||
String topicArn, Notification notification, String subscriptionArn) {
|
||||
try {
|
||||
String timestamp = DateTimeFormatter.ISO_INSTANT.format(Instant.now());
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("Type", "Notification");
|
||||
node.put("MessageId", messageId);
|
||||
node.put("MessageId", notification.messageId());
|
||||
node.put("TopicArn", topicArn);
|
||||
node.put("Timestamp", timestamp);
|
||||
node.put("Timestamp", notification.timestamp());
|
||||
if (subject != null) {
|
||||
node.put("Subject", subject);
|
||||
}
|
||||
node.put("Message", message);
|
||||
messageSigner.sign(node, signatureVersion, "SigningCertURL");
|
||||
messageSigner.sign(node, notification.signatureVersion(), "SigningCertURL", notification.signatures());
|
||||
node.put("UnsubscribeURL", unsubscribeUrl(subscriptionArn));
|
||||
ObjectNode attrs = node.putObject("MessageAttributes");
|
||||
if (messageAttributes != null) {
|
||||
|
||||
Reference in new issue
Block a user