Light, fluffy, and always free - The AWS Local Emulator alternative
chore(core): fail fast when an ARN is minted with a blank region (#5430)
Hector Venturacce4b5f6c082
diff --git a/pom.xml b/pom.xmlindex 1eb1d9472b..9ec18ccfbf 100644--- a/pom.xml+++ b/pom.xml@@ -470,6 +470,8 @@<argLine>@{argLine} -Xmx6g</argLine><systemPropertyVariables><java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>+ <!-- An ARN minted with a blank region fails the test that reached it (BlankRegionGuard). -->+ <io.github.hectorvent.floci.arn.fail-on-blank-region>true</io.github.hectorvent.floci.arn.fail-on-blank-region></systemPropertyVariables><!-- Abort a hung test with a thread dump instead of letting CI's job timeout killthe shard silently. Set here rather than in junit-platform.properties, whichdiff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsArnUtils.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsArnUtils.javaindex 338c22a0f1..cce9e23e21 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/AwsArnUtils.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsArnUtils.java@@ -31,9 +31,11 @@ public final class AwsArnUtils {* <p>A blank region says nothing about the partition, so it falls back to {@code aws}.* A regionless ARN is minted through {@link #global} or* {@link RegionResolver#buildGlobalArn} instead; {@code make partition-check} fails a call- * that passes a literal blank region here.+ * that passes a literal blank region here, and {@link BlankRegionGuard} one whose blank+ * region arrives at run time.*/public static Arn of(String service, String region, String accountId, String resource) {+ BlankRegionGuard.check(region, service, resource);return new Arn(AwsRegions.partitionFor(region), service, region, accountId, resource);}diff --git a/src/main/java/io/github/hectorvent/floci/core/common/BlankRegionGuard.java b/src/main/java/io/github/hectorvent/floci/core/common/BlankRegionGuard.javanew file mode 100644index 0000000000..ddecd5cd47--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/core/common/BlankRegionGuard.java@@ -0,0 +1,58 @@+package io.github.hectorvent.floci.core.common;++import org.jboss.logging.Logger;++import java.util.Set;+import java.util.concurrent.ConcurrentHashMap;++/**+ * Catches a regional ARN minted with a blank region. The partition is derived from the region, so+ * a blank one silently gives the commercial partition, which is wrong in every other partition.+ * {@code make partition-check} only sees a literal blank argument; this sees one that arrives+ * through a variable.+ *+ * <p>The test build sets {@value #FAIL_PROPERTY}, so such a mint throws and the test that reached+ * it fails. Anywhere else the ARN is still minted as before and the call site is logged once, so+ * an untested path keeps its response instead of turning into a 500.+ */+final class BlankRegionGuard {++ /**+ * System property that turns a blank-region mint into an {@link IllegalStateException}. It sits+ * outside {@code floci.*} on purpose: it is not emulator configuration, and SmallRye rejects an+ * unknown key under a mapped root.+ */+ static final String FAIL_PROPERTY = "io.github.hectorvent.floci.arn.fail-on-blank-region";++ private static final Logger LOG = Logger.getLogger(BlankRegionGuard.class);+ private static final Set<String> WARNED_SITES = ConcurrentHashMap.newKeySet();++ private BlankRegionGuard() {+ }++ static void check(String region, String service, String resource) {+ if (region != null && !region.isBlank()) {+ return;+ }+ String site = callSite();+ if (Boolean.getBoolean(FAIL_PROPERTY)) {+ throw new IllegalStateException("ARN for " + service + " resource '" + resource+ + "' minted with a blank region at " + site+ + "; mint a regionless ARN through Arn.global or RegionResolver.buildGlobalArn");+ }+ if (WARNED_SITES.add(site)) {+ LOG.warnv("ARN for {0} minted with a blank region at {1}; it falls back to the commercial partition",+ service, site);+ }+ }++ /** The first frame outside the ARN helpers: the code that asked for the ARN. */+ private static String callSite() {+ return StackWalker.getInstance().walk(frames -> frames+ .filter(frame -> !frame.getClassName().equals(BlankRegionGuard.class.getName())+ && !frame.getClassName().equals(AwsArnUtils.Arn.class.getName()))+ .findFirst()+ .map(frame -> frame.getClassName() + "." + frame.getMethodName() + ":" + frame.getLineNumber())+ .orElse("unknown"));+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/core/common/AwsArnUtilsTest.java b/src/test/java/io/github/hectorvent/floci/core/common/AwsArnUtilsTest.javaindex deedf24a3b..b8b99203d1 100644--- a/src/test/java/io/github/hectorvent/floci/core/common/AwsArnUtilsTest.java+++ b/src/test/java/io/github/hectorvent/floci/core/common/AwsArnUtilsTest.java@@ -143,13 +143,43 @@ class AwsArnUtilsTest {}/**- * A regionless {@code Arn.of} keeps the commercial partition: nothing in the region argument- * can say otherwise. Call sites that know the request's partition use {@link AwsArnUtils.Arn#global}.+ * A regionless {@code Arn.of} keeps the commercial partition outside the test build: nothing in+ * the region argument can say otherwise, and an untested path must keep its response. Call+ * sites that know the request's partition use {@link AwsArnUtils.Arn#global}.*/@Test- void aRegionlessArnStaysInTheCommercialPartition() {- assertEquals("arn:aws:iam::000000000000:role/r",- AwsArnUtils.Arn.of("iam", "", "000000000000", "role/r").toString());+ void aRegionlessArnStaysInTheCommercialPartitionOutsideTheTestBuild() {+ String previous = System.clearProperty(BlankRegionGuard.FAIL_PROPERTY);+ try {+ assertEquals("arn:aws:iam::000000000000:role/r",+ AwsArnUtils.Arn.of("iam", "", "000000000000", "role/r").toString());+ } finally {+ if (previous != null) {+ System.setProperty(BlankRegionGuard.FAIL_PROPERTY, previous);+ }+ }+ }++ /** The test build sets the guard's property, so a blank-region mint fails the test that reached it. */+ @Test+ void theTestBuildFailsABlankRegionMint() {+ assertEquals("true", System.getProperty(BlankRegionGuard.FAIL_PROPERTY));++ IllegalStateException blank = assertThrows(IllegalStateException.class,+ () -> AwsArnUtils.Arn.of("sqs", "", "000000000000", "q"));+ assertThrows(IllegalStateException.class, () -> AwsArnUtils.Arn.of("sqs", null, "000000000000", "q"));+ assertThrows(IllegalStateException.class, () -> AwsArnUtils.Arn.of("sqs", " ", "000000000000", "q"));+ assertTrue(blank.getMessage().contains(AwsArnUtilsTest.class.getName()), blank.getMessage());+ }++ /** A regionless ARN minted the intended way is not a blank-region mint. */+ @Test+ void globalArnsDoNotTripTheGuard() {+ assertEquals("arn:aws-cn:iam::000000000000:role/r",+ AwsArnUtils.Arn.global("aws-cn", "iam", "000000000000", "role/r").toString());+ RegionResolver resolver = new RegionResolver("us-gov-west-1", "000000000000");+ assertEquals("arn:aws-us-gov:iam::000000000000:role/r", resolver.buildGlobalArn("iam", "role/r"));+ assertEquals("arn:aws-us-gov:iam::000000000000:role/r", resolver.buildArn("iam", "", "role/r"));}@ParameterizedTestdiff --git a/src/test/java/io/github/hectorvent/floci/services/globalaccelerator/GlobalAcceleratorServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/globalaccelerator/GlobalAcceleratorServiceTest.javaindex 7afe571553..02500a5be7 100644--- a/src/test/java/io/github/hectorvent/floci/services/globalaccelerator/GlobalAcceleratorServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/globalaccelerator/GlobalAcceleratorServiceTest.java@@ -2,7 +2,6 @@ package io.github.hectorvent.floci.services.globalaccelerator;import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.ObjectMapper;-import io.github.hectorvent.floci.core.common.AwsArnUtils;import io.github.hectorvent.floci.core.common.AwsException;import io.github.hectorvent.floci.core.common.RegionResolver;import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;@@ -42,9 +41,10 @@ class GlobalAcceleratorServiceTest {StorageFactory storageFactory = mock(StorageFactory.class);RegionResolver regionResolver = mock(RegionResolver.class);when(regionResolver.getAccountId()).thenReturn(ACCOUNT);+ RegionResolver realResolver = new RegionResolver("us-east-1", ACCOUNT);when(regionResolver.buildArn(anyString(), anyString(), anyString())).thenAnswer(invocation ->- AwsArnUtils.Arn.of(invocation.getArgument(0), invocation.getArgument(1),- ACCOUNT, invocation.getArgument(2)).toString());+ realResolver.buildArn(invocation.getArgument(0), invocation.getArgument(1),+ ACCOUNT, invocation.getArgument(2)));when(storageFactory.create(anyString(), anyString(), any())).thenAnswer(invocation -> AccountAwareStorageBackend.inMemory(ACCOUNT));diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/EventSourceMappingSerializationTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/EventSourceMappingSerializationTest.javaindex d1925b323f..efd3da123e 100644--- a/src/test/java/io/github/hectorvent/floci/services/lambda/EventSourceMappingSerializationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/EventSourceMappingSerializationTest.java@@ -73,6 +73,7 @@ class EventSourceMappingSerializationTest {@Testvoid testBuildEsmResponseIncludesKafkaFieldsAndOmitsNullEventSourceArn() {EventSourceMapping esm = new EventSourceMapping();+ esm.setRegion("us-east-1");esm.setUuid("esm-kafka-001");esm.setFunctionArn("arn:aws:lambda:us-east-1:123456789012:function:kafkaConsumer");// Self-managed Kafka does not have EventSourceArn@@ -112,6 +113,7 @@ class EventSourceMappingSerializationTest {@Testvoid buildEsmResponseEmitsMaximumBatchingWindowOnlyWhenSet() {EventSourceMapping esm = new EventSourceMapping();+ esm.setRegion("us-east-1");esm.setUuid("esm-window-002");esm.setFunctionArn("arn:aws:lambda:us-east-1:123456789012:function:sqsConsumer");esm.setEventSourceArn("arn:aws:sqs:us-east-1:123456789012:my-queue");@@ -131,6 +133,7 @@ class EventSourceMappingSerializationTest {@Testvoid testBuildEsmResponseWithEventSourceArnPresent() {EventSourceMapping esm = new EventSourceMapping();+ esm.setRegion("us-east-1");esm.setUuid("esm-sqs-001");esm.setFunctionArn("arn:aws:lambda:us-east-1:123456789012:function:sqsConsumer");esm.setEventSourceArn("arn:aws:sqs:us-east-1:123456789012:my-queue");