mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
refactor(apigateway): decide execute-api host routing before applying it (#6763)
* refactor(apigateway): decide execute-api host routing before applying it ApiGatewayExecuteApiHostFilter.decide() returns a RouteDecision for a host and path without touching the request. filter() applies it: account and region of the API owner, the REST or HTTP API rewrite, or the 404 for a disabled endpoint. The lookups for an API with an owner run under the owner's account, and the caller's account and region are restored afterwards. No change in behavior for execute-api requests. * test(apigateway): give the API owner its own region in the restore tests The owner and the caller used the same region, so the tests passed even if underAccountOf() did not restore the caller's region. The owner now uses eu-west-1, and each restore test asserts the caller's region. --------- Co-authored-by: liRONCO11 <7966063+liRONCO11@users.noreply.github.com>
This commit is contained in:
1 parent
4bf399b5a1
commit
41717057c5
2 files changed
+314
-47
No files matched your search
+204
@@ -0,0 +1,204 @@
|
||||
package io.github.hectorvent.floci.services.apigateway;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.RequestContext;
|
||||
import io.github.hectorvent.floci.services.apigatewayv2.ApiGatewayV2Service;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class ApiGatewayExecuteApiHostFilterDecisionTest {
|
||||
|
||||
private static final String API_ID = "abc123";
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String OWNER_REGION = "eu-west-1";
|
||||
private static final String VIEWER_ACCOUNT = "000000000000";
|
||||
private static final String OWNER_ACCOUNT = "111122223333";
|
||||
private static final String HOST = API_ID + ".execute-api." + REGION + ".amazonaws.com";
|
||||
|
||||
private final RequestContext viewer = viewerContext();
|
||||
private final StubLookup lookup = new StubLookup(viewer);
|
||||
private final ApiGatewayExecuteApiHostFilter filter = new ApiGatewayExecuteApiHostFilter(
|
||||
lookup, new RegionResolver(REGION, VIEWER_ACCOUNT), new ApiGatewayExecuteRouteContext(),
|
||||
"localhost", viewer);
|
||||
|
||||
@Test
|
||||
void httpApiOfAnotherAccountIsLookedUpUnderTheOwnerAndTheViewerContextIsRestored() {
|
||||
lookup.owner = new ApiGatewayV2Service.ApiOwner(OWNER_ACCOUNT, OWNER_REGION);
|
||||
lookup.httpApi = true;
|
||||
|
||||
ApiGatewayExecuteApiHostFilter.RouteDecision decision = filter.decide(HOST, "/dev/accounts", null);
|
||||
|
||||
assertEquals(ApiGatewayExecuteApiHostFilter.RouteKind.HTTP_API, decision.kind());
|
||||
assertEquals("dev", decision.stage());
|
||||
assertEquals("accounts", decision.remainingPath());
|
||||
assertEquals(List.of(OWNER_ACCOUNT, OWNER_ACCOUNT), lookup.accountsSeen);
|
||||
assertEquals(VIEWER_ACCOUNT, viewer.getAccountId());
|
||||
assertEquals(REGION, viewer.getRegion());
|
||||
}
|
||||
|
||||
@Test
|
||||
void viewerContextIsRestoredWhenALookupFails() {
|
||||
lookup.owner = new ApiGatewayV2Service.ApiOwner(OWNER_ACCOUNT, OWNER_REGION);
|
||||
|
||||
ApiGatewayExecuteApiHostFilter.RouteDecision decision = filter.decide(HOST, "/dev/accounts", null);
|
||||
|
||||
assertEquals(ApiGatewayExecuteApiHostFilter.RouteKind.NONE, decision.kind());
|
||||
assertEquals(VIEWER_ACCOUNT, viewer.getAccountId());
|
||||
assertEquals(REGION, viewer.getRegion());
|
||||
}
|
||||
|
||||
@Test
|
||||
void restApiIsDecidedWithoutAnOwner() {
|
||||
ApiGatewayExecuteApiHostFilter.RouteDecision decision = filter.decide(HOST, "/dev/", null);
|
||||
|
||||
assertEquals(ApiGatewayExecuteApiHostFilter.RouteKind.REST_API, decision.kind());
|
||||
assertEquals(API_ID, decision.apiId());
|
||||
assertNull(decision.owner());
|
||||
}
|
||||
|
||||
@Test
|
||||
void httpApiWithoutOwnerIsDecidedInTheResolvedRegion() {
|
||||
lookup.httpApi = true;
|
||||
|
||||
ApiGatewayExecuteApiHostFilter.RouteDecision decision = filter.decide(HOST, "/dev/accounts", null);
|
||||
|
||||
assertEquals(ApiGatewayExecuteApiHostFilter.RouteKind.HTTP_API, decision.kind());
|
||||
assertEquals(REGION, decision.region());
|
||||
assertNull(decision.owner());
|
||||
assertEquals(List.of(VIEWER_ACCOUNT, VIEWER_ACCOUNT), lookup.accountsSeen);
|
||||
}
|
||||
|
||||
@Test
|
||||
void stagelessPathFallsBackToTheDefaultStage() {
|
||||
lookup.owner = new ApiGatewayV2Service.ApiOwner(OWNER_ACCOUNT, REGION);
|
||||
lookup.httpApi = true;
|
||||
lookup.stage = "$default";
|
||||
|
||||
ApiGatewayExecuteApiHostFilter.RouteDecision decision = filter.decide(HOST, "/accounts", null);
|
||||
|
||||
assertEquals(ApiGatewayExecuteApiHostFilter.RouteKind.HTTP_API, decision.kind());
|
||||
assertEquals("$default", decision.stage());
|
||||
assertEquals("accounts", decision.remainingPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
void disabledEndpointIsDecidedAsDisabledWithTheOwner() {
|
||||
ApiGatewayV2Service.ApiOwner owner = new ApiGatewayV2Service.ApiOwner(OWNER_ACCOUNT, REGION);
|
||||
lookup.owner = owner;
|
||||
lookup.httpApi = true;
|
||||
lookup.disabled = true;
|
||||
|
||||
ApiGatewayExecuteApiHostFilter.RouteDecision decision = filter.decide(HOST, "/dev/accounts", null);
|
||||
|
||||
assertEquals(ApiGatewayExecuteApiHostFilter.RouteKind.DISABLED, decision.kind());
|
||||
assertEquals(owner, decision.owner());
|
||||
}
|
||||
|
||||
@Test
|
||||
void internalExecuteApiPathIsDecidedAsNoneWithoutLookingUpTheApi() {
|
||||
lookup.owner = new ApiGatewayV2Service.ApiOwner(OWNER_ACCOUNT, REGION);
|
||||
lookup.httpApi = true;
|
||||
|
||||
ApiGatewayExecuteApiHostFilter.RouteDecision decision =
|
||||
filter.decide(HOST, "/execute-api/" + API_ID + "/dev/", null);
|
||||
|
||||
assertEquals(ApiGatewayExecuteApiHostFilter.RouteKind.NONE, decision.kind());
|
||||
assertTrue(lookup.accountsSeen.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void nonExecuteApiHostIsDecidedAsNone() {
|
||||
ApiGatewayExecuteApiHostFilter.RouteDecision decision = filter.decide("example.com", "/dev/", null);
|
||||
|
||||
assertEquals(ApiGatewayExecuteApiHostFilter.RouteKind.NONE, decision.kind());
|
||||
assertNull(decision.apiId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void ambiguousApiIdFailsAndLeavesTheViewerContextUntouched() {
|
||||
lookup.ambiguous = true;
|
||||
|
||||
assertThrows(AwsException.class, () -> filter.decide(HOST, "/dev/accounts", null));
|
||||
assertEquals(VIEWER_ACCOUNT, viewer.getAccountId());
|
||||
assertEquals(REGION, viewer.getRegion());
|
||||
}
|
||||
|
||||
@Test
|
||||
void viewerContextIsRestoredWhenALookupThrowsUnexpectedly() {
|
||||
lookup.owner = new ApiGatewayV2Service.ApiOwner(OWNER_ACCOUNT, OWNER_REGION);
|
||||
lookup.crash = true;
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> filter.decide(HOST, "/dev/accounts", null));
|
||||
assertEquals(VIEWER_ACCOUNT, viewer.getAccountId());
|
||||
assertEquals(REGION, viewer.getRegion());
|
||||
}
|
||||
|
||||
private static RequestContext viewerContext() {
|
||||
RequestContext context = new RequestContext();
|
||||
context.setAccountId(VIEWER_ACCOUNT);
|
||||
context.setRegion(REGION);
|
||||
return context;
|
||||
}
|
||||
|
||||
private static final class StubLookup implements ApiGatewayExecuteApiHostFilter.ApiGatewayLookup {
|
||||
private final RequestContext viewer;
|
||||
private final List<String> accountsSeen = new ArrayList<>();
|
||||
private ApiGatewayV2Service.ApiOwner owner;
|
||||
private String stage = "dev";
|
||||
private boolean disabled;
|
||||
private boolean crash;
|
||||
private boolean httpApi;
|
||||
private boolean ambiguous;
|
||||
|
||||
private StubLookup(RequestContext viewer) {
|
||||
this.viewer = viewer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ApiGatewayV2Service.ApiOwner> findApiOwner(String apiId) {
|
||||
if (ambiguous) {
|
||||
throw new AwsException("ConflictException", "ambiguous", 409);
|
||||
}
|
||||
return Optional.ofNullable(owner);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String resolveApiRegion(String preferredRegion, String apiId) {
|
||||
return preferredRegion;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String protocolType(String region, String apiId) {
|
||||
accountsSeen.add(viewer.getAccountId());
|
||||
if (crash) {
|
||||
throw new IllegalStateException("storage failure");
|
||||
}
|
||||
if (!httpApi) {
|
||||
throw new AwsException("NotFoundException", "Invalid API id specified", 404);
|
||||
}
|
||||
return "HTTP";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean executeApiEndpointDisabled(String region, String apiId) {
|
||||
return disabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void requireStage(String region, String apiId, String stageName) {
|
||||
accountsSeen.add(viewer.getAccountId());
|
||||
if (!stage.equals(stageName)) {
|
||||
throw new AwsException("NotFoundException", "Stage not found", 404);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user