From 9b451ccc825b31dd099bc75413a49ec419cc9d54 Mon Sep 17 00:00:00 2001 From: soreavis <263610811+soreavis@users.noreply.github.com> Date: Sat, 8 Aug 2026 00:33:01 +0200 Subject: [PATCH] fix(iot): match topic rules outside the default region (#2169) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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. --- docs/services/iot.md | 2 + .../floci/core/common/RegionResolver.java | 15 ++ .../floci/services/iot/IotDataController.java | 7 +- .../services/iot/IotMqttBrokerService.java | 2 +- .../floci/services/iot/IotService.java | 33 ++-- .../floci/core/common/RegionResolverTest.java | 20 ++ .../iot/IotTopicRuleIntegrationTest.java | 180 ++++++++++++++++++ 7 files changed, 245 insertions(+), 14 deletions(-) diff --git a/docs/services/iot.md b/docs/services/iot.md index 0cf78d85b8..8bdec75ae8 100644 --- a/docs/services/iot.md +++ b/docs/services/iot.md @@ -123,6 +123,8 @@ Supported rule behavior: - SQL topic filter extraction for rules shaped like `SELECT * FROM 'topic/filter'`. - MQTT-style topic filter matching for exact topics, `+`, and terminal `#`. - IoT Data `Publish` and MQTT publishes use the same rule dispatch path. +- Rule matching is region-scoped: an IoT Data `Publish` evaluates the rules of the region named by its SigV4 credential, and a rule's actions target the rule's own region. +- Publishes that carry no region — MQTT, or an IoT Data `Publish` whose `Authorization` header is absent or not SigV4 — are evaluated against every region's rules. - `republish` action republishes the original payload to another MQTT topic through `IotMqttBrokerService`. - `sqs` action sends the original payload to an SQS queue through Floci's SQS service boundary. - `sns` action publishes the original payload to an SNS topic through Floci's SNS service boundary. diff --git a/src/main/java/io/github/hectorvent/floci/core/common/RegionResolver.java b/src/main/java/io/github/hectorvent/floci/core/common/RegionResolver.java index 49952f25ad..9204be7b31 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/RegionResolver.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/RegionResolver.java @@ -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; } diff --git a/src/main/java/io/github/hectorvent/floci/services/iot/IotDataController.java b/src/main/java/io/github/hectorvent/floci/services/iot/IotDataController.java index 71d9950f91..7613b26e43 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iot/IotDataController.java +++ b/src/main/java/io/github/hectorvent/floci/services/iot/IotDataController.java @@ -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(); } diff --git a/src/main/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerService.java b/src/main/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerService.java index 9bb58cd3fe..c5aab03718 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerService.java +++ b/src/main/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerService.java @@ -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); } diff --git a/src/main/java/io/github/hectorvent/floci/services/iot/IotService.java b/src/main/java/io/github/hectorvent/floci/services/iot/IotService.java index a962625d8d..8ac5c67bab 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iot/IotService.java +++ b/src/main/java/io/github/hectorvent/floci/services/iot/IotService.java @@ -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 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 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); } } } diff --git a/src/test/java/io/github/hectorvent/floci/core/common/RegionResolverTest.java b/src/test/java/io/github/hectorvent/floci/core/common/RegionResolverTest.java index f418f1d085..aa16934eb6 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/RegionResolverTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/RegionResolverTest.java @@ -42,6 +42,26 @@ class RegionResolverTest { assertEquals("us-east-1", resolver.resolveRegion(headers)); } + @Test + void resolveRegionFromAuthOrNullReturnsCredentialRegion() { + assertEquals("eu-west-1", resolver.resolveRegionFromAuthOrNull( + "AWS4-HMAC-SHA256 Credential=AKID/20260215/eu-west-1/iotdata/aws4_request, " + + "SignedHeaders=host, Signature=abc123")); + } + + @Test + void resolveRegionFromAuthOrNullIsNullWhereResolveReturnsDefault() { + assertNull(resolver.resolveRegionFromAuthOrNull("Bearer some-token")); + assertEquals("us-east-1", resolver.resolveRegionFromAuth("Bearer some-token")); + } + + @Test + void resolveRegionFromAuthOrNullIsNullForAbsentOrBlank() { + assertNull(resolver.resolveRegionFromAuthOrNull(null)); + assertNull(resolver.resolveRegionFromAuthOrNull("")); + assertNull(resolver.resolveRegionFromAuthOrNull(" ")); + } + @Test void fallsBackToDefaultWhenEmptyAuthHeader() { HttpHeaders headers = stubHeaders(""); diff --git a/src/test/java/io/github/hectorvent/floci/services/iot/IotTopicRuleIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iot/IotTopicRuleIntegrationTest.java index 41b1d14039..3c2a71693e 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iot/IotTopicRuleIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iot/IotTopicRuleIntegrationTest.java @@ -1,6 +1,7 @@ package io.github.hectorvent.floci.services.iot; import io.quarkus.test.junit.QuarkusTest; +import io.restassured.response.ValidatableResponse; import jakarta.inject.Inject; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -286,6 +287,124 @@ class IotTopicRuleIntegrationTest { .body(containsString("sns-rule-payload")); } + @Test + void topicRuleMatchesOnlyPublishesInItsOwnRegion() throws Exception { + createEuWest1RepublishRule("regionalMatchRule", "devices/regional/match", "devices/regional/match-republished"); + + given() + .header("Authorization", auth("eu-west-1", "iotdata")) + .contentType("text/plain") + .body("eu-west-1-payload") + .when() + .post("/topics/devices/regional/match") + .then() + .statusCode(200); + + awaitPublishedEvent("devices/regional/match-republished", "eu-west-1-payload".getBytes(StandardCharsets.UTF_8)); + + given() + .header("Authorization", auth("us-east-1", "iotdata")) + .contentType("text/plain") + .body("us-east-1-payload") + .when() + .post("/topics/devices/regional/match") + .then() + .statusCode(200); + + assertFalse(eventRecorder.recentEvents().stream() + .anyMatch(event -> "devices/regional/match-republished".equals(event.topic()) + && Arrays.equals("us-east-1-payload".getBytes(StandardCharsets.UTF_8), event.payload()))); + } + + @Test + void unsignedPublishEvaluatesRulesInEveryRegion() throws Exception { + createEuWest1RepublishRule("regionalUnsignedRule", "devices/regional/unsigned", "devices/regional/unsigned-republished"); + + given() + .contentType("text/plain") + .body("unsigned-payload") + .when() + .post("/topics/devices/regional/unsigned") + .then() + .statusCode(200); + + awaitPublishedEvent("devices/regional/unsigned-republished", "unsigned-payload".getBytes(StandardCharsets.UTF_8)); + } + + @Test + void regionlessPublishFiresEveryMatchingRegionsRule() throws Exception { + createRepublishRule("eu-west-1", "fanoutRuleEu", "devices/regional/fanout", "devices/regional/fanout-eu"); + createRepublishRule("ap-south-1", "fanoutRuleAp", "devices/regional/fanout", "devices/regional/fanout-ap"); + + given() + .contentType("text/plain") + .body("fanout-payload") + .when() + .post("/topics/devices/regional/fanout") + .then() + .statusCode(200); + + awaitPublishedEvent("devices/regional/fanout-eu", "fanout-payload".getBytes(StandardCharsets.UTF_8)); + awaitPublishedEvent("devices/regional/fanout-ap", "fanout-payload".getBytes(StandardCharsets.UTF_8)); + } + + @Test + void nonSigV4AuthorizationPublishEvaluatesRulesInEveryRegion() throws Exception { + createRepublishRule("eu-west-1", "bearerRule", "devices/regional/bearer", "devices/regional/bearer-republished"); + + given() + .header("Authorization", "Bearer not-sigv4") + .contentType("text/plain") + .body("bearer-payload") + .when() + .post("/topics/devices/regional/bearer") + .then() + .statusCode(200); + + awaitPublishedEvent("devices/regional/bearer-republished", "bearer-payload".getBytes(StandardCharsets.UTF_8)); + } + + @Test + void topicRuleActionTargetsTheRuleRegion() { + String queueUrl = createQueue("regional-iot-rule-queue", "eu-west-1"); + createQueue("regional-iot-rule-queue", "us-east-1"); + + given() + .header("Authorization", auth("eu-west-1", "iot")) + .contentType("application/json") + .body(""" + { + "topicRulePayload": { + "sql": "SELECT * FROM 'devices/regional/sqs'", + "actions": [ + { + "sqs": { + "roleArn": "arn:aws:iam::000000000000:role/iot-rule-role", + "queueUrl": "%s" + } + } + ] + } + } + """.formatted(queueUrl)) + .when() + .put("/rules/regionalSqsRule") + .then() + .statusCode(200); + + given() + .header("Authorization", auth("eu-west-1", "iotdata")) + .contentType("text/plain") + .body("regional-sqs-payload") + .when() + .post("/topics/devices/regional/sqs") + .then() + .statusCode(200); + + receiveMessage(queueUrl, "eu-west-1").body(containsString("regional-sqs-payload")); + receiveMessage(queueUrl, "us-east-1").body(not(containsString("regional-sqs-payload"))); + } + @Test void topicRuleConflictReplaceDeleteAndTagsMatchMvpLifecycle() { given() @@ -381,6 +500,67 @@ class IotTopicRuleIntegrationTest { .body("__type", equalTo("ResourceNotFoundException")); } + private static String auth(String region, String service) { + return "AWS4-HMAC-SHA256 Credential=AKID/20260215/" + region + "/" + service + + "/aws4_request, SignedHeaders=host, Signature=abc"; + } + + private void createEuWest1RepublishRule(String ruleName, String sourceTopic, String targetTopic) { + createRepublishRule("eu-west-1", ruleName, sourceTopic, targetTopic); + } + + private void createRepublishRule(String region, String ruleName, String sourceTopic, String targetTopic) { + given() + .header("Authorization", auth(region, "iot")) + .contentType("application/json") + .body(""" + { + "topicRulePayload": { + "sql": "SELECT * FROM '%s'", + "actions": [ + { + "republish": { + "roleArn": "arn:aws:iam::000000000000:role/iot-rule-role", + "topic": "%s" + } + } + ] + } + } + """.formatted(sourceTopic, targetTopic)) + .when() + .put("/rules/" + ruleName) + .then() + .statusCode(200) + .body("ruleArn", equalTo("arn:aws:iot:" + region + ":000000000000:rule/" + ruleName)); + } + + private String createQueue(String queueName, String region) { + return given() + .header("Authorization", auth(region, "sqs")) + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "CreateQueue") + .formParam("QueueName", queueName) + .when() + .post("/") + .then() + .statusCode(200) + .extract().xmlPath().getString("CreateQueueResponse.CreateQueueResult.QueueUrl"); + } + + private ValidatableResponse receiveMessage(String queueUrl, String region) { + return given() + .header("Authorization", auth(region, "sqs")) + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "ReceiveMessage") + .formParam("QueueUrl", queueUrl) + .formParam("MaxNumberOfMessages", "1") + .when() + .post("/") + .then() + .statusCode(200); + } + private void awaitPublishedEvent(String topic, byte[] payload) throws InterruptedException { Instant deadline = Instant.now().plus(Duration.ofSeconds(2)); while (Instant.now().isBefore(deadline)) {