Light, fluffy, and always free - The AWS Local Emulator alternative
fix: keep MSK configurations and RDS events in the request region (#5940)
Hector Ventura317074e3033c
diff --git a/src/main/java/io/github/hectorvent/floci/services/msk/MskService.java b/src/main/java/io/github/hectorvent/floci/services/msk/MskService.javaindex 530c1c1d40..5a64492002 100644--- a/src/main/java/io/github/hectorvent/floci/services/msk/MskService.java+++ b/src/main/java/io/github/hectorvent/floci/services/msk/MskService.java@@ -434,7 +434,9 @@ public class MskService implements ResourceProvider {if (serverProperties == null) {throw new AwsException("BadRequestException", "serverProperties is required.", 400);}- if (configurationStorage.scan(k -> true).stream().anyMatch(c -> c.getName().equals(name))) {+ if (configurationStorage.scan(k -> true).stream()+ .filter(this::isCurrentRegion)+ .anyMatch(c -> c.getName().equals(name))) {throw new AwsException("ConflictException", "Configuration already exists: " + name, 409);}@@ -455,13 +457,13 @@ public class MskService implements ResourceProvider {// NotFoundException. The message prefix is a compatibility contract: terraform-provider-aws// (and the Pulumi provider bridging it) substring-matches "Configuration ARN does not exist"// to detect that a configuration is gone, so its post-delete waiter can complete.- return configurationStorage.get(arn)+ return configurationStorage.get(arn).filter(this::isCurrentRegion).orElseThrow(() -> new AwsException("BadRequestException","Configuration ARN does not exist: " + arn, 400));}public PaginatedResult<MskConfiguration> listConfigurations(Integer maxResults, String nextToken) {- List<MskConfiguration> all = configurationStorage.scan(k -> true);+ List<MskConfiguration> all = configurationStorage.scan(k -> true).stream().filter(this::isCurrentRegion).toList();return Pagination.paginate(all, MskConfiguration::getArn, maxResults, nextToken, MAX_PAGE, "BadRequestException");}@@ -558,7 +560,7 @@ public class MskService implements ResourceProvider {if (cluster != null) {return cluster.getTags() != null ? cluster.getTags() : Map.of();}- MskConfiguration configuration = configurationStorage.get(arn).orElse(null);+ MskConfiguration configuration = configurationStorage.get(arn).filter(this::isCurrentRegion).orElse(null);if (configuration != null) {return configuration.getTags() != null ? configuration.getTags() : Map.of();}@@ -572,7 +574,7 @@ public class MskService implements ResourceProvider {storage.put(arn, cluster);return;}- MskConfiguration configuration = configurationStorage.get(arn).orElse(null);+ MskConfiguration configuration = configurationStorage.get(arn).filter(this::isCurrentRegion).orElse(null);if (configuration != null) {configuration.setTags(merged(configuration.getTags(), tags));configurationStorage.put(arn, configuration);@@ -588,7 +590,7 @@ public class MskService implements ResourceProvider {storage.put(arn, cluster);return;}- MskConfiguration configuration = configurationStorage.get(arn).orElse(null);+ MskConfiguration configuration = configurationStorage.get(arn).filter(this::isCurrentRegion).orElse(null);if (configuration != null) {configuration.setTags(without(configuration.getTags(), tagKeys));configurationStorage.put(arn, configuration);@@ -676,6 +678,14 @@ public class MskService implements ResourceProvider {return Set.of(new SupportedResourceType("kafka:cluster", "kafka", true));}+ /**+ * Configurations are regional like clusters. Their ARN has always carried the region they were+ * created in, so it decides; there are no older records to keep visible.+ */+ private boolean isCurrentRegion(MskConfiguration configuration) {+ return regionResolver.getRegion().equals(AwsArnUtils.regionOrDefault(configuration.getArn(), null));+ }+private boolean isCurrentRegion(MskCluster cluster) {if (cluster.getResourceRegion() != null) {return regionResolver.getRegion().equals(cluster.getResourceRegion());diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.javaindex 340e2ca554..4315eadc3d 100644--- a/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java@@ -651,7 +651,7 @@ public class RdsQueryHandler {service.refreshDbInstanceRuntimeHealth(instance);}}- List<RdsEvent> all = service.describeEvents(sourceIdentifier, sourceType, start, end, duration);+ List<RdsEvent> all = service.describeEvents(region, sourceIdentifier, sourceType, start, end, duration);int offset = parseMarker(params.getFirst("Marker"));int limit = maxRecords != null ? maxRecords : 100;int from = Math.min(offset, all.size());diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java b/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.javaindex 8371fa15bd..66022f9eca 100644--- a/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java+++ b/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java@@ -2936,7 +2936,11 @@ public class RdsService implements Resettable, ResourceProvider {List.of("availability"), Instant.now(), instance.getDbInstanceArn()));}- public List<RdsEvent> describeEvents(String sourceIdentifier, String sourceType,+ /**+ * The events of {@code region}: an event belongs to the region its source ARN names. An event+ * recorded without a source ARN stays visible everywhere, as before.+ */+ public List<RdsEvent> describeEvents(String region, String sourceIdentifier, String sourceType,Instant startTime, Instant endTime, Integer durationMinutes) {Instant now = Instant.now();pruneExpiredEvents(now);@@ -2944,6 +2948,8 @@ public class RdsService implements Resettable, ResourceProvider {Instant effectiveStart = startTime != null ? startTime: effectiveEnd.minusSeconds((durationMinutes != null ? durationMinutes : 60) * 60L);return events.scan(_ -> true).stream()+ .filter(event -> region == null || event.sourceArn() == null+ || region.equals(AwsArnUtils.regionOrDefault(event.sourceArn(), region))).filter(event -> sourceIdentifier == null || sourceIdentifier.equals(event.sourceIdentifier())).filter(event -> sourceType == null || sourceType.equals(event.sourceType())).filter(event -> !event.date().isBefore(effectiveStart) && !event.date().isAfter(effectiveEnd))diff --git a/src/test/java/io/github/hectorvent/floci/services/msk/MskServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/msk/MskServiceTest.javaindex 941c4eb64a..1b15025c89 100644--- a/src/test/java/io/github/hectorvent/floci/services/msk/MskServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/msk/MskServiceTest.java@@ -180,6 +180,24 @@ class MskServiceTest {assertEquals(0, mskService.listClusters().size());}+ @Test+ void configurationsAreRegionalLikeClusters() {+ MskConfiguration east = mskService.createConfiguration("shared-config", "desc", List.of("3.6.0"), "props");++ when(regionResolver.getRegion()).thenReturn("eu-west-1");+ // The same name is free in another region, and each region lists and reads only its own.+ MskConfiguration west = mskService.createConfiguration("shared-config", "desc", List.of("3.6.0"), "props");+ assertEquals(List.of(west), mskService.listConfigurations(null, null).items());+ AwsException describe = assertThrows(AwsException.class,+ () -> mskService.describeConfiguration(east.getArn()));+ assertEquals("BadRequestException", describe.getErrorCode());+ assertThrows(AwsException.class, () -> mskService.listTagsForResource(east.getArn()));++ when(regionResolver.getRegion()).thenReturn("us-east-1");+ assertEquals(List.of(east), mskService.listConfigurations(null, null).items());+ assertEquals(east, mskService.describeConfiguration(east.getArn()));+ }+@Testvoid deleteCluster() {MskCluster cluster = mskService.createCluster("test-cluster");diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsQueryHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsQueryHandlerTest.javaindex c4f493816f..d4d2517855 100644--- a/src/test/java/io/github/hectorvent/floci/services/rds/RdsQueryHandlerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsQueryHandlerTest.java@@ -292,7 +292,7 @@ class RdsQueryHandlerTest {void describeEvents_reconcilesOnlyTheRequestedDbInstance() {DbInstance requested = makeInstance("requested");when(service.listDbInstances("requested", null)).thenReturn(List.of(requested));- when(service.describeEvents("requested", "db-instance", null, null, null))+ when(service.describeEvents(null, "requested", "db-instance", null, null, null)).thenReturn(List.of());MultivaluedMap<String, String> p = params();p.add("SourceIdentifier", "requested");@@ -308,7 +308,7 @@ class RdsQueryHandlerTest {@Testvoid describeEvents_doesNotReconcileInstancesForAnotherSourceType() {- when(service.describeEvents("cluster", "db-cluster", null, null, null))+ when(service.describeEvents(null, "cluster", "db-cluster", null, null, null)).thenReturn(List.of());MultivaluedMap<String, String> p = params();p.add("SourceIdentifier", "cluster");diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.javaindex 70d7ff78f6..7994994082 100644--- a/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java@@ -3529,7 +3529,7 @@ class RdsServiceTest {listener.onAutoPause(AutoPauseListener.Event.RESUMED, start.plusSeconds(91));List<RdsEvent> events = rdsService.describeEvents(- "paused-1", "db-instance", start.minusSeconds(1), start.plusSeconds(120), null);+ "us-east-1", "paused-1", "db-instance", start.minusSeconds(1), start.plusSeconds(120), null);assertEquals(List.of("Initiated pause for the DB instance.","Successfully paused the DB instance.",@@ -9026,14 +9026,14 @@ class RdsServiceTest {assertEquals(DbInstanceStatus.FAILED, refreshed.getStatus());verify(proxyManager).stopProxy("rds-resource:" + refreshed.getDbInstanceArn());- List<RdsEvent> events = rdsService.describeEvents("dead-db", "db-instance", null, null, 60);+ List<RdsEvent> events = rdsService.describeEvents("us-east-1", "dead-db", "db-instance", null, null, 60);assertEquals(1, events.size());assertEquals(List.of("availability"), events.getFirst().eventCategories());assertEquals(refreshed.getDbInstanceArn(), events.getFirst().sourceArn());// Repeated health reads do not duplicate the transition event.rdsService.refreshDbInstanceRuntimeHealth(refreshed);- assertEquals(1, rdsService.describeEvents("dead-db", "db-instance", null, null, 60).size());+ assertEquals(1, rdsService.describeEvents("us-east-1", "dead-db", "db-instance", null, null, 60).size());}@Test@@ -9049,7 +9049,7 @@ class RdsServiceTest {assertEquals(DbInstanceStatus.AVAILABLE, refreshed.getStatus());verify(proxyManager, never()).stopProxy("rds-resource:" + refreshed.getDbInstanceArn());- assertTrue(rdsService.describeEvents("blip-db", "db-instance", null, null, 60).isEmpty());+ assertTrue(rdsService.describeEvents("us-east-1", "blip-db", "db-instance", null, null, 60).isEmpty());}@Test@@ -9071,7 +9071,7 @@ class RdsServiceTest {assertEquals(DbInstanceStatus.FAILED, afterDefiniteAnswer.getStatus());verify(proxyManager).stopProxy(proxyKey);verify(containerManager, times(2)).probeContainer("cont-id");- assertEquals(1, rdsService.describeEvents("recover-db", "db-instance", null, null, 60).size());+ assertEquals(1, rdsService.describeEvents("us-east-1", "recover-db", "db-instance", null, null, 60).size());}@Test@@ -9089,13 +9089,31 @@ class RdsServiceTest {eventStore.put(retained.id(), retained);List<RdsEvent> result = rdsService.describeEvents(- null, null, now.minus(Duration.ofDays(20)), now.plusSeconds(1), null);+ "us-east-1", null, null, now.minus(Duration.ofDays(20)), now.plusSeconds(1), null);assertEquals(List.of(retained), result);assertTrue(eventStore.get("retained").isPresent());assertTrue(eventStore.get("expired").isEmpty());}+ @Test+ void describeEventsListsOnlyTheRequestRegionsEvents() throws Exception {+ InMemoryStorage<String, RdsEvent> eventStore = new InMemoryStorage<>();+ Field eventsField = RdsService.class.getDeclaredField("events");+ eventsField.setAccessible(true);+ eventsField.set(rdsService, eventStore);+ Instant now = Instant.now();+ RdsEvent east = new RdsEvent("east", "db-east", "db-instance", "created", List.of("creation"),+ now.minusSeconds(60), "arn:aws:rds:us-east-1:123456789012:db:db-east");+ RdsEvent west = new RdsEvent("west", "db-west", "db-instance", "created", List.of("creation"),+ now.minusSeconds(60), "arn:aws:rds:eu-west-1:123456789012:db:db-west");+ eventStore.put(east.id(), east);+ eventStore.put(west.id(), west);++ assertEquals(List.of(east), rdsService.describeEvents("us-east-1", null, null, null, null, null));+ assertEquals(List.of(west), rdsService.describeEvents("eu-west-1", null, null, null, null, null));+ }+@Testvoid optionGroupsUseTheInjectedAccountAwareStorage() {String accountId = "123456789012";