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

@@ -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);
}
@@ -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"));
}
}