fix(iot): match topic rules outside the default region (#2169)

* fix(iot): match topic rules outside the default region

Topic rules are stored per region — topicRuleKey(region, ruleName) — but
handlePublish looked them up with a hardcoded config.defaultRegion(). A
rule created in any other region was stored, gettable and listable yet
never evaluated: republish/SQS/SNS/Kinesis/DynamoDB/Lambda actions were
silently dropped while Publish still returned 200.

IotDataController.publish now resolves the region from the request's
SigV4 credential, as the shadow endpoints in that controller already do,
and threads it through IotService.publish into handlePublish. A publish
that carries no region — MQTT, or an unsigned data-plane request —
evaluates every region's rules, mirroring the fallback
ApiGatewayService.resolveRestApiRegion applies to unsigned REST API
invocations.

executeTopicRule derives the rule's own region from its ARN and targets
the action sinks with it instead of the default region, so a rule created
in eu-west-1 delivers to eu-west-1 resources.

Closes #2065

* fix(iot): treat a non-SigV4 publish header as an unresolved region

IotDataController.publish skipped region resolution only for a missing
or blank Authorization header. Any other non-SigV4 value — a bearer JWT,
say — fell through to resolveRegionFromAuth, whose miss path returns the
configured default region. So a publish carrying such a header evaluated
only the default region's rules, while one carrying no header at all
evaluated every region's: the same absence of region information, two
different outcomes.

RegionResolver.resolveRegionFromAuthOrNull returns the credential's
region, or null when there is nothing to parse, and publish threads
that through — so every publish that names no region is treated the
same way.
This commit is contained in:
soreavis authored and GitHub committed 2026-08-07 17:33:01 -05:00
1 parent 0203bc7f7e
commit 9b451ccc82
7 files changed
+245 -14

No files matched your search

@@ -67,6 +67,21 @@ public class RegionResolver {
return defaultRegion;
}
/**
* Resolves the region from a SigV4 Authorization credential scope, or
* null when the header is absent or carries no parseable credential —
* for callers that treat an unresolved region differently from the
* default region.
*/
public String resolveRegionFromAuthOrNull(String authorizationHeader) {
if (authorizationHeader == null || authorizationHeader.isEmpty()) {
return null;
}
Matcher matcher = CREDENTIAL_REGION_PATTERN.matcher(authorizationHeader);
return matcher.find() ? matcher.group(1) : null;
}
public String getDefaultRegion() {
return defaultRegion;
}
@@ -84,11 +84,14 @@ public class IotDataController {
@POST
@Path("/topics/{topic: .+}")
@Consumes(MediaType.WILDCARD)
public Response publish(@PathParam("topic") String topic,
public Response publish(@Context HttpHeaders headers,
@PathParam("topic") String topic,
@QueryParam("retain") Boolean retain,
@QueryParam("qos") Integer qos,
byte[] payload) {
iotService.publish(topic, payload == null ? new byte[0] : payload, Boolean.TRUE.equals(retain), qos == null ? 0 : qos);
// Unsigned or non-SigV4 publishes carry no usable region; both count as unresolved
String region = regionResolver.resolveRegionFromAuthOrNull(headers.getHeaderString("Authorization"));
iotService.publish(topic, payload == null ? new byte[0] : payload, Boolean.TRUE.equals(retain), qos == null ? 0 : qos, region);
return Response.ok(objectMapper.createObjectNode()).build();
}
@@ -223,7 +223,7 @@ public class IotMqttBrokerService {
return;
}
iotService.get().publish(topic, payload, message.isRetain(), message.qosLevel().value());
iotService.get().publish(topic, payload, message.isRetain(), message.qosLevel().value(), null);
fanOut(topic, payload, false);
}
@@ -526,10 +526,10 @@ public class IotService {
}
public void publish(String topic, byte[] payload) {
publish(topic, payload, false, 0);
publish(topic, payload, false, 0, null);
}
public void publish(String topic, byte[] payload, boolean retain, int qos) {
public void publish(String topic, byte[] payload, boolean retain, int qos, String region) {
byte[] eventPayload = payload == null ? new byte[0] : payload;
if (retain) {
if (eventPayload.length == 0) {
@@ -543,7 +543,7 @@ public class IotService {
retainedMessageStore.put(retainedMessageKey(topic), retained);
}
}
handlePublish(topic, eventPayload, true);
handlePublish(topic, eventPayload, true, region);
}
public void deleteConnection(String clientId, boolean cleanSession) {
@@ -889,19 +889,29 @@ public class IotService {
topicRuleStore.put(topicRuleKey(region, ruleName), rule);
}
void handlePublish(String topic, byte[] payload, boolean evaluateRules) {
void handlePublish(String topic, byte[] payload, boolean evaluateRules, String region) {
byte[] eventPayload = payload == null ? new byte[0] : payload;
publishEventRecorder.record(topic, eventPayload);
if (!evaluateRules) {
return;
}
for (IotTopicRule rule : listTopicRules(config.defaultRegion())) {
for (IotTopicRule rule : rulesForPublish(region)) {
if (!rule.isRuleDisabled() && topicMatches(extractTopicPattern(rule.getSql()), topic)) {
executeTopicRule(rule, eventPayload);
}
}
}
private List<IotTopicRule> rulesForPublish(String region) {
if (region != null) {
return listTopicRules(region);
}
// MQTT and unsigned data-plane publishes carry no region, so every region's rules apply.
return topicRuleStore.scan(key -> true).stream()
.sorted(Comparator.comparing(IotTopicRule::getRuleName))
.toList();
}
void handleReservedMqttPublish(String topic, byte[] payload, BiConsumer<String, byte[]> publisher) {
ReservedShadowTopic shadowTopic = parseReservedShadowTopic(topic);
if (shadowTopic == null) {
@@ -990,6 +1000,7 @@ public class IotService {
}
private void executeTopicRule(IotTopicRule rule, byte[] payload) {
String ruleRegion = AwsArnUtils.regionOrDefault(rule.getRuleArn(), config.defaultRegion());
try {
JsonNode actions = objectMapper.readTree(rule.getActionsJson());
if (!actions.isArray()) {
@@ -1000,7 +1011,7 @@ public class IotService {
if (republish.isObject()) {
String targetTopic = republish.path("topic").asText(null);
if (targetTopic != null && !targetTopic.isBlank()) {
handlePublish(targetTopic, payload, false);
handlePublish(targetTopic, payload, false, ruleRegion);
mqttBrokerService.publish(targetTopic, payload);
}
}
@@ -1011,14 +1022,14 @@ public class IotService {
String body = sqs.path("useBase64").asBoolean(false)
? Base64.getEncoder().encodeToString(payload)
: new String(payload, StandardCharsets.UTF_8);
sqsService.sendMessage(queueUrl, body, 0, config.defaultRegion());
sqsService.sendMessage(queueUrl, body, 0, ruleRegion);
}
}
JsonNode sns = action.path("sns");
if (sns.isObject()) {
String targetArn = sns.path("targetArn").asText(null);
if (targetArn != null && !targetArn.isBlank()) {
snsService.publish(targetArn, null, new String(payload, StandardCharsets.UTF_8), null, config.defaultRegion());
snsService.publish(targetArn, null, new String(payload, StandardCharsets.UTF_8), null, ruleRegion);
}
}
JsonNode s3 = action.path("s3");
@@ -1034,21 +1045,21 @@ public class IotService {
String streamName = kinesis.path("streamName").asText(null);
String partitionKey = kinesis.path("partitionKey").asText("floci-iot");
if (streamName != null && !streamName.isBlank()) {
kinesisService.putRecord(streamName, payload, partitionKey, config.defaultRegion());
kinesisService.putRecord(streamName, payload, partitionKey, ruleRegion);
}
}
JsonNode dynamoDBv2 = action.path("dynamoDBv2");
if (dynamoDBv2.isObject()) {
String tableName = dynamoDBv2.path("putItem").path("tableName").asText(null);
if (tableName != null && !tableName.isBlank()) {
dynamoDbService.putItem(tableName, toDynamoDbItem(payload), config.defaultRegion());
dynamoDbService.putItem(tableName, toDynamoDbItem(payload), ruleRegion);
}
}
JsonNode lambda = action.path("lambda");
if (lambda.isObject()) {
String functionArn = lambda.path("functionArn").asText(null);
if (functionArn != null && !functionArn.isBlank()) {
lambdaService.invoke(config.defaultRegion(), functionArn, payload, InvocationType.Event);
lambdaService.invoke(ruleRegion, functionArn, payload, InvocationType.Event);
}
}
}