mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(core): share URI rewrite workaround with Lambda and Cognito (#7610)
This commit is contained in:
1 parent
918ce95b53
commit
2c49f5ecb5
5 files changed
+155
-18
No files matched your search
+2
-1
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.cognito;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AccountContextFilter;
|
||||
import io.github.hectorvent.floci.core.common.RequestHost;
|
||||
import io.github.hectorvent.floci.core.common.RequestUriRewrite;
|
||||
import io.github.hectorvent.floci.services.cognito.model.UserPoolDomain;
|
||||
import jakarta.annotation.Priority;
|
||||
import jakarta.inject.Inject;
|
||||
@@ -112,7 +113,7 @@ public class CognitoCustomDomainFilter implements ContainerRequestFilter {
|
||||
LOG.debugv("Cognito custom domain routing: {0}{1} -> {2}", host, path, newUri.getPath());
|
||||
requestContext.setProperty(POOL_PROPERTY, domain.get().getUserPoolId());
|
||||
requestContext.setProperty(AccountContextFilter.PINNED_ACCOUNT_PROPERTY, domain.get().getAwsAccountId());
|
||||
requestContext.setRequestUri(newUri);
|
||||
RequestUriRewrite.apply(requestContext, newUri);
|
||||
}
|
||||
|
||||
private static String stripPort(String host) {
|
||||
|
||||
+2
-1
@@ -1,6 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.lambda;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.RequestHost;
|
||||
import io.github.hectorvent.floci.core.common.RequestUriRewrite;
|
||||
import jakarta.annotation.Priority;
|
||||
import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
import jakarta.ws.rs.container.ContainerRequestFilter;
|
||||
@@ -48,7 +49,7 @@ public class LambdaUrlRoutingFilter implements ContainerRequestFilter {
|
||||
.build();
|
||||
|
||||
LOG.debugv("Routing Lambda URL: {0} -> {1}", host, newUri.getPath());
|
||||
requestContext.setRequestUri(newUri);
|
||||
RequestUriRewrite.apply(requestContext, newUri);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
-2
@@ -58,13 +58,14 @@ class CognitoCustomDomainFilterTest {
|
||||
void rewritesManagedLoginPathsOnACustomDomainAndPinsThePool() {
|
||||
when(cognitoService.findCustomDomain(DOMAIN)).thenReturn(Optional.of(domain("us-east-1_abc")));
|
||||
for (String path : new String[] {"/login", "/logout"}) {
|
||||
ContainerRequestContext request = request("GET", "https://" + DOMAIN + path + "?client_id=c&state=a%26b", DOMAIN);
|
||||
String query = "?client_id=c&state=a%2525%26b%2F";
|
||||
ContainerRequestContext request = request("GET", "https://" + DOMAIN + path + query, DOMAIN);
|
||||
|
||||
filter.filter(request);
|
||||
|
||||
verify(request).setProperty(CognitoCustomDomainFilter.POOL_PROPERTY, "us-east-1_abc");
|
||||
verify(request).setProperty(AccountContextFilter.PINNED_ACCOUNT_PROPERTY, ACCOUNT);
|
||||
verify(request).setRequestUri(URI.create("https://" + DOMAIN + "/cognito-idp" + path + "?client_id=c&state=a%26b"));
|
||||
verify(request).setRequestUri(URI.create("https://" + DOMAIN + "/cognito-idp" + path + query));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+51
-14
@@ -8,6 +8,8 @@ import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -105,8 +107,43 @@ class CognitoCustomDomainRoutingIntegrationTest {
|
||||
assertNull(expect(200, DOMAIN.toUpperCase() + ":4566", clientA, secretA, "/oauth2/token"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"x%25y", "trailing%25", "a%252Fb", "a%2Fb", "a%20b"})
|
||||
@Order(4)
|
||||
void unknownEncodedOauthPathsKeepTheJsonNotFoundResponse(String suffix) {
|
||||
given().urlEncodingEnabled(false).header("Host", DOMAIN)
|
||||
.when()
|
||||
.get("/oauth2/" + suffix)
|
||||
.then()
|
||||
.statusCode(404)
|
||||
.contentType(containsString("application/json"))
|
||||
.body("error", equalTo("This URL doesn't exist on the authorization server."));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
void managedLoginPreservesEncodedQueryValues() throws Exception {
|
||||
String clientId = cognitoJson("CreateUserPoolClient", """
|
||||
{"UserPoolId":"%s","ClientName":"encoded-query-client",
|
||||
"AllowedOAuthFlowsUserPoolClient":true,"AllowedOAuthFlows":["code"],
|
||||
"AllowedOAuthScopes":["openid"],"CallbackURLs":["https://app.example.test/callback"],
|
||||
"SupportedIdentityProviders":["COGNITO"]}
|
||||
""".formatted(poolA)).path("UserPoolClient").path("ClientId").asText();
|
||||
|
||||
given().urlEncodingEnabled(false).header("Host", DOMAIN)
|
||||
.when()
|
||||
.get("/login?response_type=code&client_id=" + clientId
|
||||
+ "&redirect_uri=https%3A%2F%2Fapp.example.test%2Fcallback&scope=openid"
|
||||
+ "&state=x%25y%252F%26z%2B")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.contentType(startsWith("text/html"))
|
||||
.body(containsString("name=\"state\" value=\"x%y%2F&z+\""))
|
||||
.body(containsString("name=\"redirect_uri\" value=\"https://app.example.test/callback\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void clientOfAnotherPoolIsRefusedOnThisDomain() {
|
||||
given()
|
||||
.header("Host", DOMAIN)
|
||||
@@ -120,13 +157,13 @@ class CognitoCustomDomainRoutingIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
@Order(7)
|
||||
void sameClientStillWorksOnTheGenericPath() {
|
||||
assertNull(expect(200, null, clientB, secretB, "/cognito-idp/oauth2/token"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(8)
|
||||
void emptyPostOnTheCustomDomainAnswersInvalidRequest() {
|
||||
given()
|
||||
.header("Host", DOMAIN)
|
||||
@@ -140,7 +177,7 @@ class CognitoCustomDomainRoutingIntegrationTest {
|
||||
|
||||
/** Without a matching Host nothing is rewritten: /oauth2/token is S3's /{bucket}/{key}. */
|
||||
@Test
|
||||
@Order(7)
|
||||
@Order(9)
|
||||
void unknownHostIsLeftUntouched() {
|
||||
given()
|
||||
.header("Host", "nobody-" + System.nanoTime() + ".example.com")
|
||||
@@ -153,7 +190,7 @@ class CognitoCustomDomainRoutingIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
@Order(10)
|
||||
void userInfoWithoutBearerTokenIsRoutedToo() {
|
||||
given()
|
||||
.header("Host", DOMAIN)
|
||||
@@ -165,7 +202,7 @@ class CognitoCustomDomainRoutingIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(9)
|
||||
@Order(11)
|
||||
void userInfoOnTheCustomDomainServesThePoolsOwnUsers() {
|
||||
given()
|
||||
.header("Host", DOMAIN)
|
||||
@@ -181,7 +218,7 @@ class CognitoCustomDomainRoutingIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(10)
|
||||
@Order(12)
|
||||
void userInfoOnTheCustomDomainRefusesATokenOfAnotherPool() {
|
||||
given()
|
||||
.header("Host", DOMAIN)
|
||||
@@ -201,7 +238,7 @@ class CognitoCustomDomainRoutingIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(11)
|
||||
@Order(13)
|
||||
void openIdConfigurationAdvertisesTheCustomDomain() {
|
||||
given()
|
||||
.when()
|
||||
@@ -224,7 +261,7 @@ class CognitoCustomDomainRoutingIntegrationTest {
|
||||
|
||||
/** Domain names are one global namespace on AWS, whichever account asks. */
|
||||
@Test
|
||||
@Order(12)
|
||||
@Order(14)
|
||||
void anotherAccountCannotReuseTheDomainName() throws Exception {
|
||||
String poolInB = awsJsonAs(ACCOUNT_B, COGNITO, "CreateUserPool", "{\"PoolName\": \"RoutingPoolB2\"}")
|
||||
.path("UserPool").path("Id").asText();
|
||||
@@ -238,7 +275,7 @@ class CognitoCustomDomainRoutingIntegrationTest {
|
||||
|
||||
/** The domain's account, not the default one, serves a request on that domain. */
|
||||
@Test
|
||||
@Order(13)
|
||||
@Order(15)
|
||||
void tokenOnAnotherAccountsDomainRunsInThatAccount() throws Exception {
|
||||
String poolInB = awsJsonAs(ACCOUNT_B, COGNITO, "CreateUserPool", "{\"PoolName\": \"RoutingPoolB3\"}")
|
||||
.path("UserPool").path("Id").asText();
|
||||
@@ -262,7 +299,7 @@ class CognitoCustomDomainRoutingIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(14)
|
||||
@Order(16)
|
||||
void replacingTheCertificateKeepsTheDomainRouted() throws Exception {
|
||||
cognitoJson("UpdateUserPoolDomain", customDomain(DOMAIN, poolA, requestCertificate(DOMAIN)));
|
||||
|
||||
@@ -270,7 +307,7 @@ class CognitoCustomDomainRoutingIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(15)
|
||||
@Order(17)
|
||||
void deletedDomainIsNoLongerRouted() throws Exception {
|
||||
cognitoJson("DeleteUserPoolDomain", """
|
||||
{"Domain": "%s", "UserPoolId": "%s"}
|
||||
@@ -296,7 +333,7 @@ class CognitoCustomDomainRoutingIntegrationTest {
|
||||
|
||||
/** After a delete the name is free again, and it follows its new pool. */
|
||||
@Test
|
||||
@Order(16)
|
||||
@Order(18)
|
||||
void domainNameFollowsItsNewPoolAfterReassignment() throws Exception {
|
||||
cognitoJson("CreateUserPoolDomain", customDomain(DOMAIN, poolB, requestCertificate(DOMAIN)));
|
||||
|
||||
@@ -313,7 +350,7 @@ class CognitoCustomDomainRoutingIntegrationTest {
|
||||
|
||||
/** Two accounts racing for one name: exactly one create succeeds, so the name never turns ambiguous. */
|
||||
@Test
|
||||
@Order(17)
|
||||
@Order(19)
|
||||
void concurrentCreatesAcrossAccountsLeaveOneOwner() throws Exception {
|
||||
String contested = "auth-race-" + System.nanoTime() + ".teos.localhost.floci.io";
|
||||
String defaultAccount = "000000000000";
|
||||
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
package io.github.hectorvent.floci.services.lambda;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvokeResult;
|
||||
import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
|
||||
import io.quarkus.test.InjectMock;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.Response;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** Exercises real HTTP routing and event construction, with the Lambda service mocked. */
|
||||
@QuarkusTest
|
||||
class LambdaUrlRoutingIntegrationTest {
|
||||
|
||||
private static final String URL_ID = "encoded-path-url";
|
||||
private static final String HOST = URL_ID + ".lambda-url.us-east-1.localhost";
|
||||
private static final String FUNCTION_ARN = "arn:aws:lambda:us-east-1:000000000000:function:encoded-path";
|
||||
private static final String RAW_QUERY = "q=a%2525";
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
@InjectMock
|
||||
LambdaService lambdaService;
|
||||
|
||||
@BeforeEach
|
||||
void echoInvocationEvent() {
|
||||
LambdaFunction function = new LambdaFunction();
|
||||
function.setFunctionName("encoded-path");
|
||||
function.setFunctionArn(FUNCTION_ARN);
|
||||
function.setAccountId("000000000000");
|
||||
when(lambdaService.getTargetByUrlId(URL_ID)).thenReturn(function);
|
||||
when(lambdaService.invokeArn(eq(FUNCTION_ARN), any(byte[].class), eq(InvocationType.RequestResponse)))
|
||||
.thenAnswer(call -> {
|
||||
String event = new String(call.getArgument(1, byte[].class), StandardCharsets.UTF_8);
|
||||
byte[] payload = MAPPER.writeValueAsBytes(MAPPER.createObjectNode()
|
||||
.put("statusCode", 200).put("body", event));
|
||||
return new InvokeResult(200, null, payload, null, "encoded-path-request");
|
||||
});
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"/, /",
|
||||
"/plain, /plain",
|
||||
"/x%25y, /x%y",
|
||||
"/trailing%25, /trailing%",
|
||||
"/a%252Fb, /a%2Fb",
|
||||
"/a%2Fb, /a/b",
|
||||
"/a%20b, /a b",
|
||||
"/a%3Fb%23c, /a?b#c",
|
||||
"/a+b, /a+b",
|
||||
"/caf%C3%A9, /café"
|
||||
})
|
||||
void hostRewriteMatchesPathStyleEventPaths(String path, String expectedPath) throws Exception {
|
||||
Response hosted = given().urlEncodingEnabled(false).header("Host", HOST)
|
||||
.when().get(path + "?" + RAW_QUERY);
|
||||
assertEvent(hosted, expectedPath, "GET");
|
||||
|
||||
Response pathStyle = given().urlEncodingEnabled(false)
|
||||
.when().get("/lambda-url/" + URL_ID + path + "?" + RAW_QUERY);
|
||||
assertEvent(pathStyle, expectedPath, "GET");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"POST", "PUT", "DELETE", "PATCH"})
|
||||
void hostRewritePreservesPercentPathsForBodyMethods(String method) throws Exception {
|
||||
Response response = given().urlEncodingEnabled(false).header("Host", HOST)
|
||||
.contentType("text/plain").body("request body")
|
||||
.when().request(method, "/x%25y?" + RAW_QUERY);
|
||||
|
||||
JsonNode event = assertEvent(response, "/x%y", method);
|
||||
assertEquals("request body", event.path("body").asText());
|
||||
}
|
||||
|
||||
private static JsonNode assertEvent(Response response, String expectedPath, String method) throws Exception {
|
||||
assertEquals(200, response.statusCode(), response::asString);
|
||||
JsonNode event = MAPPER.readTree(response.asString());
|
||||
assertEquals(expectedPath, event.path("rawPath").asText());
|
||||
assertEquals(expectedPath, event.path("requestContext").path("http").path("path").asText());
|
||||
assertEquals(method, event.path("requestContext").path("http").path("method").asText());
|
||||
assertEquals(RAW_QUERY, event.path("rawQueryString").asText());
|
||||
assertEquals("a%25", event.path("queryStringParameters").path("q").asText());
|
||||
return event;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user