chore(core): fail fast when an ARN is minted with a blank region (#5430)

* chore(core): fail fast when an ARN is minted with a blank region

Arn.of derives the partition from the region, so a blank region that arrives through a
variable silently mints arn:aws:, which the partition-check gate cannot see. The test build
now sets a JVM system property that makes such a mint throw, naming the call site; outside
it the ARN is minted as before and the call site is logged once, so production responses do
not change.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>

* test: give two fixtures the region the blank-region guard requires

GlobalAcceleratorServiceTest mocked RegionResolver.buildArn with Arn.of, which
the real resolver never calls for the blank region Global Accelerator passes:
it mints a global ARN. The mock now delegates to a real resolver.
EventSourceMappingSerializationTest built mappings with no region, which
LambdaService always sets on create; the fixtures set one.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>

---------

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-11 00:11:46 -04:00
1 parent 0ab58edb3c
commit cce4b5f6c0
6 files changed
+104 -9

No files matched your search

@@ -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"));
}
@ParameterizedTest
@@ -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));
@@ -73,6 +73,7 @@ class EventSourceMappingSerializationTest {
@Test
void 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 {
@Test
void 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 {
@Test
void 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");