mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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:
1 parent
0203bc7f7e
commit
9b451ccc82
7 files changed
+245
-14
No files matched your search
@@ -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("");
|
||||
|
||||
+180
@@ -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)) {
|
||||
|
||||
Reference in new issue
Block a user