From ff80dac7809d2fe0703b9ab251e7bd1bf99c65a7 Mon Sep 17 00:00:00 2001 From: Yoshiyuki Ohta Date: Mon, 18 May 2026 01:24:30 +0900 Subject: [PATCH] fix(eventbridge): keep persisted rules from disappearing on reload (#878) Rule.getRegion() is a derived getter (computed from the rule's ARN) but has no matching setter. When EventBridgeService runs in any non-memory storage mode, PersistentStorage's ObjectMapper writes "region" into eventbridge-rules.json on save, then throws UnrecognizedPropertyException on the next startup because Rule cannot deserialize it. The exception is swallowed by PersistentStorage.load(), so the rule store comes up empty and every PutRule made before the restart silently disappears. Two changes on Rule: - @JsonIgnore on getRegion() so the derived field is no longer written to disk. - @JsonIgnoreProperties(ignoreUnknown = true) on the class to match the convention used by Target, InputTransformer, SqsParameters and most other persisted models, and to let a file that includes "region" (written before this change) still load cleanly after upgrade. Adds RulePersistenceTest covering the PersistentStorage round-trip and unknown-property tolerance. --- .../services/eventbridge/model/Rule.java | 4 + .../eventbridge/RulePersistenceTest.java | 84 +++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 src/test/java/io/github/hectorvent/floci/services/eventbridge/RulePersistenceTest.java diff --git a/src/main/java/io/github/hectorvent/floci/services/eventbridge/model/Rule.java b/src/main/java/io/github/hectorvent/floci/services/eventbridge/model/Rule.java index 0d91ca7132..2c195eb347 100644 --- a/src/main/java/io/github/hectorvent/floci/services/eventbridge/model/Rule.java +++ b/src/main/java/io/github/hectorvent/floci/services/eventbridge/model/Rule.java @@ -1,5 +1,7 @@ package io.github.hectorvent.floci.services.eventbridge.model; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.quarkus.runtime.annotations.RegisterForReflection; @@ -8,6 +10,7 @@ import java.util.HashMap; import java.util.Map; @RegisterForReflection +@JsonIgnoreProperties(ignoreUnknown = true) public class Rule { private String name; @@ -57,6 +60,7 @@ public class Rule { public Instant getCreatedAt() { return createdAt; } public void setCreatedAt(Instant createdAt) { this.createdAt = createdAt; } + @JsonIgnore public String getRegion() { return AwsArnUtils.regionOrDefault(arn, null); } diff --git a/src/test/java/io/github/hectorvent/floci/services/eventbridge/RulePersistenceTest.java b/src/test/java/io/github/hectorvent/floci/services/eventbridge/RulePersistenceTest.java new file mode 100644 index 0000000000..b252162bd7 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/eventbridge/RulePersistenceTest.java @@ -0,0 +1,84 @@ +package io.github.hectorvent.floci.services.eventbridge; + +import com.fasterxml.jackson.core.type.TypeReference; +import io.github.hectorvent.floci.core.storage.PersistentStorage; +import io.github.hectorvent.floci.services.eventbridge.model.Rule; +import io.github.hectorvent.floci.services.eventbridge.model.RuleState; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Map; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; + +class RulePersistenceTest { + + @TempDir + Path tempDir; + + @Test + void rulePersistsAcrossInstances() { + Path filePath = tempDir.resolve("eventbridge-rules.json"); + String key = "000000000000/rule:us-east-1:default/my-rule"; + + Rule rule = new Rule(); + rule.setName("my-rule"); + rule.setArn("arn:aws:events:us-east-1:000000000000:rule/my-rule"); + rule.setAccountId("000000000000"); + rule.setEventBusName("default"); + rule.setScheduleExpression("rate(5 minutes)"); + rule.setState(RuleState.ENABLED); + + var writer = new PersistentStorage( + filePath, new TypeReference>() {}); + writer.put(key, rule); + + var reader = new PersistentStorage( + filePath, new TypeReference>() {}); + reader.load(); + + Optional loaded = reader.get(key); + assertTrue(loaded.isPresent(), + "Rule should survive a fresh PersistentStorage instance + load()"); + assertEquals("my-rule", loaded.get().getName()); + assertEquals("arn:aws:events:us-east-1:000000000000:rule/my-rule", loaded.get().getArn()); + assertEquals("rate(5 minutes)", loaded.get().getScheduleExpression()); + assertEquals(RuleState.ENABLED, loaded.get().getState()); + } + + @Test + void ignoresUnknownPropertiesOnLoad() throws Exception { + // A persisted file may contain fields the current Rule class does not + // recognize: either a derived field written by an older Floci version + // (e.g. "region") or a brand new field from a future version. Loading + // must not fail in either case. + Path filePath = tempDir.resolve("eventbridge-rules.json"); + String key = "000000000000/rule:us-east-1:default/legacy-rule"; + String legacyJson = """ + { + "%s" : { + "name" : "legacy-rule", + "arn" : "arn:aws:events:us-east-1:000000000000:rule/legacy-rule", + "accountId" : "000000000000", + "eventBusName" : "default", + "scheduleExpression" : "rate(5 minutes)", + "state" : "ENABLED", + "fieldFromFutureVersion" : "whatever" + } + } + """.formatted(key); + Files.writeString(filePath, legacyJson); + + var store = new PersistentStorage( + filePath, new TypeReference>() {}); + store.load(); + + Optional loaded = store.get(key); + assertTrue(loaded.isPresent(), + "Rules with unknown extra fields should still load"); + assertEquals("legacy-rule", loaded.get().getName()); + } +}