mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(apigateway): fall back to REST API region scan for non-SigV4 Authorization headers (#2149)
* fix(apigatewayv2): resolve HTTP API region across regions like REST APIs do Unsigned requests (and requests carrying a non-SigV4 Authorization header, e.g. a Cognito bearer JWT) resolve to RegionResolver's configured default region rather than a region the caller specified. For v1 REST APIs, resolveRestApiRegion already falls back to scanning all regions for the apiId when that happens. HTTP API (v2) dispatch had no equivalent, so any HTTP API deployed outside the default region returned 404 "Invalid API id specified" for every unsigned or bearer-token-authenticated request - which is effectively every request a browser, curl, or non-SigV4 client sends. Adds ApiGatewayV2Service#resolveHttpApiRegion, mirroring ApiGatewayService#resolveRestApiRegion exactly (same region::apiId key shape), and RegionResolver#isRegionUnresolved to give dispatch() a single correct signal for "region resolution silently fell back to the default" that covers both the blank-header and non-SigV4-header cases the previous blank-check missed. * test(apigateway): cover HTTP API cross-region resolution and non-SigV4 fallback Adds unit coverage for the dispatch() region-resolution fix: - An unsigned request for a v2 HTTP API deployed outside the default region must fall back to resolveHttpApiRegion and find it there. - A correctly-resolved (signed) request must not pay for the fallback scan. - A non-SigV4 Authorization header (e.g. a Cognito bearer JWT) on a v1 REST API must also trigger the region scan, not just a blank/missing header — using a real RegionResolver rather than a mock so the test exercises the actual header-parsing logic in isRegionUnresolved. Verified each test fails when the corresponding fix is reverted: the v2 test and the bearer-JWT test both fail (2 of 12) when dispatch()'s region-fallback logic is rolled back to the pre-fix version, confirming they aren't vacuously passing. * chore: trigger CI * refactor(apigateway): drop duplicate RegionResolver.isRegionUnresolved #2169 (merged) added resolveRegionFromAuthOrNull, the same predicate as isRegionUnresolved added here. Collapse onto the former per hectorvent's review note on #2169 rather than let both live in core/common. * test(apigateway): update mocks for merged v1-first dispatch flow Finish resolving the PR #2149 merge conflict — the test file's edits for the merged dispatch() ordering weren't included in the merge commit. * test(apigateway): note that v2 region tests pin #2054, not this PR Per review feedback on #2149: unsignedRequestFindsV2ApiDeployedOutsideDefaultRegion and signedV2RequestDoesNotConsultRegionFallback exercise v2 cross-region dispatch behavior that landed independently in #2054, not the v1 Authorization-header fix this PR makes. Kept as the only controller-level coverage of that behavior, with a comment so they aren't read as regression guards for this change.
This commit is contained in:
3 files changed
+124
-2
No files matched your search
+6
-2
@@ -256,8 +256,12 @@ public class ApiGatewayExecuteController {
|
||||
}
|
||||
|
||||
String preferredRegion = region;
|
||||
String auth = headers.getHeaderString("Authorization");
|
||||
if (auth == null || auth.isBlank()) {
|
||||
// True for SigV4-unsigned requests, and also for requests whose Authorization header
|
||||
// isn't a SigV4 credential at all (e.g. a Cognito bearer JWT) - resolveRegion silently
|
||||
// fell back to defaultRegion in both cases, so the resolved region is a guess.
|
||||
boolean regionUnresolved = regionResolver.resolveRegionFromAuthOrNull(
|
||||
headers == null ? null : headers.getHeaderString("Authorization")) == null;
|
||||
if (regionUnresolved) {
|
||||
region = apiGatewayService.resolveRestApiRegion(region, apiId);
|
||||
}
|
||||
|
||||
|
||||
+5
@@ -127,6 +127,11 @@ public class ApiGatewayV2Service {
|
||||
.orElseThrow(() -> new AwsException("NotFoundException", "Invalid API id specified", 404));
|
||||
}
|
||||
|
||||
/**
|
||||
* Mirrors ApiGatewayService#resolveRestApiRegion: unsigned data-plane requests carry no
|
||||
* region, so preferredRegion is whatever RegionResolver defaults to, which need not match
|
||||
* where the API was actually created. Falls back to scanning stored keys for the apiId.
|
||||
*/
|
||||
public String resolveApiRegion(String preferredRegion, String apiId) {
|
||||
if (apiStore.get(apiKey(preferredRegion, apiId)).isPresent()) {
|
||||
return preferredRegion;
|
||||
|
||||
+113
@@ -2,7 +2,10 @@ package io.github.hectorvent.floci.services.apigateway;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.services.apigatewayv2.ApiGatewayV2Service;
|
||||
import io.github.hectorvent.floci.services.apigatewayv2.model.Api;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvokeResult;
|
||||
import jakarta.ws.rs.core.HttpHeaders;
|
||||
import jakarta.ws.rs.core.MultivaluedHashMap;
|
||||
@@ -16,7 +19,11 @@ import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.atLeastOnce;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** Unit coverage for API Gateway request-event construction and HTTP API v2 route matching. */
|
||||
@@ -193,4 +200,110 @@ class ApiGatewayExecuteControllerTest {
|
||||
"""));
|
||||
assertEquals("application/xml", response.getMediaType().toString());
|
||||
}
|
||||
|
||||
// ── HTTP API (v2) region resolution for unsigned / non-SigV4 requests ──────
|
||||
//
|
||||
// The two tests below (unsignedRequestFindsV2ApiDeployedOutsideDefaultRegion and
|
||||
// signedV2RequestDoesNotConsultRegionFallback) exercise the v2 cross-region dispatch
|
||||
// behavior that #2054 introduced on `resolveApiRegion` — not this PR's change, which is
|
||||
// scoped to the v1 REST path's Authorization-header check below. They're kept here because
|
||||
// they're the only controller-level coverage of that v2 behavior; treat them as pinning
|
||||
// #2054, not as regression guards for this PR.
|
||||
|
||||
@Test
|
||||
void unsignedRequestFindsV2ApiDeployedOutsideDefaultRegion() {
|
||||
RegionResolver regionResolver = mock(RegionResolver.class);
|
||||
ApiGatewayV2Service apiGatewayV2Service = mock(ApiGatewayV2Service.class);
|
||||
ApiGatewayService apiGatewayService = mock(ApiGatewayService.class);
|
||||
HttpHeaders headers = mock(HttpHeaders.class);
|
||||
|
||||
when(regionResolver.resolveRegion(headers)).thenReturn("us-east-1");
|
||||
// resolveRegionFromAuthOrNull is unstubbed and returns null by default, i.e. unresolved.
|
||||
// Not a v1 API at the default region...
|
||||
when(apiGatewayService.resolveRestApiRegion("us-east-1", "abc123")).thenReturn("us-east-1");
|
||||
when(apiGatewayService.getRestApi("us-east-1", "abc123")).thenThrow(
|
||||
new AwsException("NotFoundException", "Invalid REST API id specified", 404));
|
||||
// The API was actually created in eu-west-1; the default-region lookup must miss...
|
||||
// ...so resolveApiRegion is consulted and finds the real region.
|
||||
when(apiGatewayV2Service.resolveApiRegion("us-east-1", "abc123")).thenReturn("eu-west-1");
|
||||
when(apiGatewayV2Service.getApi("eu-west-1", "abc123")).thenReturn(new Api());
|
||||
// No route configured — dispatchV2 returns 404, but that's downstream of the region fix;
|
||||
// what this test asserts is which region the API/route lookups actually ran against.
|
||||
when(apiGatewayV2Service.findMatchingRoute("eu-west-1", "abc123", "GET", "/hello"))
|
||||
.thenReturn(null);
|
||||
|
||||
ApiGatewayExecuteController controller = new ApiGatewayExecuteController(
|
||||
apiGatewayService, apiGatewayV2Service, null,
|
||||
regionResolver, new ObjectMapper(), null,
|
||||
null, null, null, null, new ApiGatewayExecuteRouteContext());
|
||||
|
||||
Response response = controller.dispatch("GET", "abc123", "prod", "hello", headers, null, null);
|
||||
|
||||
assertEquals(404, response.getStatus());
|
||||
verify(apiGatewayV2Service).findMatchingRoute("eu-west-1", "abc123", "GET", "/hello");
|
||||
}
|
||||
|
||||
@Test
|
||||
void signedV2RequestDoesNotConsultRegionFallback() {
|
||||
RegionResolver regionResolver = mock(RegionResolver.class);
|
||||
ApiGatewayV2Service apiGatewayV2Service = mock(ApiGatewayV2Service.class);
|
||||
ApiGatewayService apiGatewayService = mock(ApiGatewayService.class);
|
||||
HttpHeaders headers = mock(HttpHeaders.class);
|
||||
|
||||
when(regionResolver.resolveRegion(headers)).thenReturn("us-east-1");
|
||||
when(headers.getHeaderString("Authorization")).thenReturn(
|
||||
"AWS4-HMAC-SHA256 Credential=AKID/20260215/us-east-1/execute-api/aws4_request");
|
||||
when(regionResolver.resolveRegionFromAuthOrNull(anyString())).thenReturn("us-east-1");
|
||||
// Not a v1 API — falls through to v2 at the already-resolved (signed) region.
|
||||
when(apiGatewayService.getRestApi("us-east-1", "abc123")).thenThrow(
|
||||
new AwsException("NotFoundException", "Invalid REST API id specified", 404));
|
||||
when(apiGatewayV2Service.resolveApiRegion("us-east-1", "abc123")).thenReturn("us-east-1");
|
||||
when(apiGatewayV2Service.getApi("us-east-1", "abc123")).thenReturn(new Api());
|
||||
when(apiGatewayV2Service.findMatchingRoute("us-east-1", "abc123", "GET", "/hello"))
|
||||
.thenReturn(null);
|
||||
|
||||
ApiGatewayExecuteController controller = new ApiGatewayExecuteController(
|
||||
apiGatewayService, apiGatewayV2Service, null,
|
||||
regionResolver, new ObjectMapper(), null,
|
||||
null, null, null, null, new ApiGatewayExecuteRouteContext());
|
||||
|
||||
controller.dispatch("GET", "abc123", "prod", "hello", headers, null, null);
|
||||
|
||||
// A correctly-signed (or otherwise resolved) request must not pay for the v1 region
|
||||
// scan — resolveRestApiRegion is only for the "region is a guess" case.
|
||||
verify(apiGatewayService, never()).resolveRestApiRegion(anyString(), anyString());
|
||||
verify(apiGatewayV2Service, atLeastOnce()).getApi("us-east-1", "abc123");
|
||||
}
|
||||
|
||||
@Test
|
||||
void nonSigV4AuthorizationHeaderFallsBackToRestApiRegionScan() {
|
||||
// A Cognito bearer JWT (or any Authorization header without a SigV4 Credential=...)
|
||||
// must be treated the same as a missing header: resolveRegion silently defaulted,
|
||||
// so the v1 REST path also needs to fall back to scanning for the real region. Uses
|
||||
// the real RegionResolver (not a mock) so the test exercises the actual header-parsing
|
||||
// logic in resolveRegionFromAuthOrNull, not just a stubbed answer.
|
||||
RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000");
|
||||
ApiGatewayV2Service apiGatewayV2Service = mock(ApiGatewayV2Service.class);
|
||||
ApiGatewayService apiGatewayService = mock(ApiGatewayService.class);
|
||||
HttpHeaders headers = mock(HttpHeaders.class);
|
||||
when(headers.getHeaderString("Authorization")).thenReturn("Bearer eyJhbGciOiJIUzI1NiJ9.fake.jwt");
|
||||
|
||||
when(apiGatewayService.resolveRestApiRegion("us-east-1", "restapi1")).thenReturn("ap-southeast-2");
|
||||
when(apiGatewayService.getRestApi("ap-southeast-2", "restapi1")).thenThrow(
|
||||
new AwsException("NotFoundException", "Invalid REST API id specified", 404));
|
||||
// Not a v1 API either — falls through to v2, which must also scan.
|
||||
when(apiGatewayV2Service.resolveApiRegion("us-east-1", "restapi1")).thenReturn("us-east-1");
|
||||
when(apiGatewayV2Service.getApi("us-east-1", "restapi1")).thenThrow(
|
||||
new AwsException("NotFoundException", "Invalid API id specified", 404));
|
||||
|
||||
ApiGatewayExecuteController controller = new ApiGatewayExecuteController(
|
||||
apiGatewayService, apiGatewayV2Service, null,
|
||||
regionResolver, new ObjectMapper(), null,
|
||||
null, null, null, null, new ApiGatewayExecuteRouteContext());
|
||||
|
||||
controller.dispatch("GET", "restapi1", "prod", "hello", headers, null, null);
|
||||
|
||||
verify(apiGatewayService).resolveRestApiRegion("us-east-1", "restapi1");
|
||||
verify(apiGatewayService).getRestApi("ap-southeast-2", "restapi1");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user