mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudfront): serve a Lambda Web Function endpoint origin from Floci itself (#6022)
* fix(cloudfront): serve a Lambda Web Function endpoint origin from Floci itself A custom origin whose domain is an endpoint this Floci minted now goes to the listener the viewer reached, with the endpoint domain as Host. Before, the endpoint host did not resolve, and loopback failed the private address check. As on AWS, a forwarded viewer Host is a 502, because the endpoint's certificate does not name it. * fix(cloudfront): fetch the endpoint origin over plain HTTP for TLS viewers A viewer over TLS reached the HTTPS port, and Floci sent plain HTTP to it. The request now goes to the plain HTTP port at the same address. The endpoint domains are also indexed by the host a CloudFront origin names, so a base URL host with upper case or a trailing dot still matches.
This commit is contained in:
1 parent
2fd6f43836
commit
7fb649715c
9 files changed
+349
-19
No files matched your search
+130
@@ -0,0 +1,130 @@
|
||||
package io.github.hectorvent.floci.services.cloudfront;
|
||||
|
||||
import io.github.hectorvent.floci.services.cloudfront.model.DefaultCacheBehavior;
|
||||
import io.github.hectorvent.floci.services.cloudfront.model.Distribution;
|
||||
import io.github.hectorvent.floci.services.cloudfront.model.DistributionConfig;
|
||||
import io.github.hectorvent.floci.services.cloudfront.model.Origin;
|
||||
import io.github.hectorvent.floci.services.s3.S3Service;
|
||||
import io.github.hectorvent.floci.testing.PartitionCleanup;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.matchesPattern;
|
||||
|
||||
/**
|
||||
* Distributions whose origin is a Web Function endpoint of this Floci, answered by the platform
|
||||
* without an execution environment, so no Docker. No private origin host is allowed here.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class CloudFrontWebFunctionOriginServingTest {
|
||||
|
||||
private static final String BASE = "/2025-03-07";
|
||||
private static final String BUCKET = "web-code-cloudfront";
|
||||
private static final String ROLE = "arn:aws:iam::000000000000:role/web";
|
||||
private static final String CACHING_DISABLED = "4135ea2d-6df8-44a3-9df3-4b5a84be39ad";
|
||||
|
||||
@Inject
|
||||
CloudFrontService cloudFrontService;
|
||||
|
||||
@Inject
|
||||
S3Service s3Service;
|
||||
|
||||
@RegisterExtension
|
||||
final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
|
||||
@Test
|
||||
void anEndpointOriginIsReachedWhateverPortAndProtocolTheOriginNames() {
|
||||
String functionName = "cf-disabled-" + Long.toString(System.nanoTime(), 36);
|
||||
String domain = createFunction(s3Service, cleanup, functionName, "ApplicationManaged");
|
||||
given().contentType("application/json").body("{\"throttleConfig\": {\"rateLimit\": 0}}")
|
||||
.when().patch(BASE + "/web-functions/" + functionName + "/endpoints/dev")
|
||||
.then().statusCode(202);
|
||||
Distribution created = createDistribution(cloudFrontService, cleanup, domain,
|
||||
CloudFrontService.MANAGED_ALL_VIEWER_EXCEPT_HOST_HEADER_ORIGIN_REQUEST_POLICY_ID);
|
||||
|
||||
given().header("Host", created.getDomainName()).when().get("/")
|
||||
.then().statusCode(503)
|
||||
.header("lambda-web-error-type", "Platform.EndpointDisabled")
|
||||
.header("lambda-web-request-id", matchesPattern("[A-Z2-7]{26}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anIamAuthEndpointRefusesTheUnsignedRequestOfADistributionWithoutOriginAccessControl() {
|
||||
String domain = createFunction(s3Service, cleanup, "cf-iam-" + Long.toString(System.nanoTime(), 36), "IamAuth");
|
||||
Distribution created = createDistribution(cloudFrontService, cleanup, domain,
|
||||
CloudFrontService.MANAGED_ALL_VIEWER_EXCEPT_HOST_HEADER_ORIGIN_REQUEST_POLICY_ID);
|
||||
|
||||
given().header("Host", created.getDomainName()).when().get("/")
|
||||
.then().statusCode(403)
|
||||
.header("content-type", "text/html")
|
||||
.header("lambda-web-request-id", matchesPattern("[A-Z2-7]{26}"))
|
||||
.body(containsString("You are not authorized to access this endpoint."));
|
||||
}
|
||||
|
||||
@Test
|
||||
void forwardingTheViewerHostToAnEndpointIsABadGateway() {
|
||||
String domain = createFunction(s3Service, cleanup, "cf-host-" + Long.toString(System.nanoTime(), 36),
|
||||
"ApplicationManaged");
|
||||
Distribution created = createDistribution(cloudFrontService, cleanup, domain,
|
||||
CloudFrontService.MANAGED_ALL_VIEWER_ORIGIN_REQUEST_POLICY_ID);
|
||||
|
||||
given().header("Host", created.getDomainName()).when().get("/")
|
||||
.then().statusCode(502);
|
||||
}
|
||||
|
||||
static String createFunction(S3Service s3Service, PartitionCleanup cleanup, String functionName, String authType) {
|
||||
s3Service.createBucket(BUCKET, "us-east-1");
|
||||
s3Service.putObject(BUCKET, "app.zip", "zip".getBytes(StandardCharsets.UTF_8), "application/zip", Map.of());
|
||||
String domain = given().contentType("application/json")
|
||||
.body("""
|
||||
{"functionName": "%s",
|
||||
"revisionConfig": {
|
||||
"buildConfig": {"codeConfig": {"s3Object": {"bucket": "%s", "key": "app.zip"}},
|
||||
"runtimeConfig": {"runtime": "nodejs24.x"}},
|
||||
"serviceConfig": {"executionRoleArn": "%s"}},
|
||||
"endpointConfig": {"endpointName": "dev", "endpointType": "HomeRegion",
|
||||
"authType": "%s", "autoDeploymentMode": "LatestRevision"}}
|
||||
""".formatted(functionName, BUCKET, ROLE, authType))
|
||||
.when().put(BASE + "/web-functions")
|
||||
.then().statusCode(202)
|
||||
.extract().path("endpoint.domainName");
|
||||
cleanup.register(() -> given().when().delete(BASE + "/web-functions/" + functionName));
|
||||
return domain;
|
||||
}
|
||||
|
||||
/** The origin names the endpoint as on AWS: its host without a port, over HTTPS on 443. */
|
||||
static Distribution createDistribution(CloudFrontService cloudFrontService, PartitionCleanup cleanup,
|
||||
String endpointDomain, String originRequestPolicyId) {
|
||||
Origin origin = new Origin();
|
||||
origin.setId("web");
|
||||
origin.setDomainName(endpointDomain.substring(0, endpointDomain.lastIndexOf(':')));
|
||||
Map<String, Object> customOriginConfig = new LinkedHashMap<>();
|
||||
customOriginConfig.put("HTTPPort", "80");
|
||||
customOriginConfig.put("HTTPSPort", "443");
|
||||
customOriginConfig.put("OriginProtocolPolicy", "https-only");
|
||||
origin.setCustomOriginConfig(customOriginConfig);
|
||||
DefaultCacheBehavior behavior = new DefaultCacheBehavior();
|
||||
behavior.setTargetOriginId("web");
|
||||
behavior.setViewerProtocolPolicy("allow-all");
|
||||
behavior.setCachePolicyId(CACHING_DISABLED);
|
||||
behavior.setOriginRequestPolicyId(originRequestPolicyId);
|
||||
DistributionConfig config = new DistributionConfig();
|
||||
config.setEnabled(true);
|
||||
config.setOrigins(List.of(origin));
|
||||
config.setDefaultCacheBehavior(behavior);
|
||||
Distribution distribution = new Distribution();
|
||||
distribution.setConfig(config);
|
||||
Distribution created = cloudFrontService.createDistribution(distribution, Map.of());
|
||||
cleanup.register(() -> cloudFrontService.removeDistribution(created.getId()));
|
||||
return created;
|
||||
}
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package io.github.hectorvent.floci.services.cloudfront;
|
||||
|
||||
import io.github.hectorvent.floci.config.SelfSignedTlsProfile;
|
||||
import io.github.hectorvent.floci.services.cloudfront.model.Distribution;
|
||||
import io.github.hectorvent.floci.services.s3.S3Service;
|
||||
import io.github.hectorvent.floci.testing.PartitionCleanup;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import jakarta.inject.Inject;
|
||||
import org.eclipse.microprofile.config.inject.ConfigProperty;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
|
||||
import static io.github.hectorvent.floci.services.cloudfront.CloudFrontWebFunctionOriginServingTest.createDistribution;
|
||||
import static io.github.hectorvent.floci.services.cloudfront.CloudFrontWebFunctionOriginServingTest.createFunction;
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.matchesPattern;
|
||||
|
||||
/** A viewer over TLS reaches the HTTPS port, while Floci fetches its own endpoint origin over plain HTTP. */
|
||||
@QuarkusTest
|
||||
@TestProfile(SelfSignedTlsProfile.class)
|
||||
class CloudFrontWebFunctionOriginTlsIntegrationTest {
|
||||
|
||||
@ConfigProperty(name = "quarkus.http.test-ssl-port", defaultValue = "0")
|
||||
int sslPort;
|
||||
|
||||
@Inject
|
||||
CloudFrontService cloudFrontService;
|
||||
|
||||
@Inject
|
||||
S3Service s3Service;
|
||||
|
||||
@RegisterExtension
|
||||
final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
|
||||
@Test
|
||||
void aViewerOverTlsReachesAnEndpointOrigin() {
|
||||
String domain = createFunction(s3Service, cleanup, "cf-tls-" + Long.toString(System.nanoTime(), 36), "IamAuth");
|
||||
Distribution created = createDistribution(cloudFrontService, cleanup, domain,
|
||||
CloudFrontService.MANAGED_ALL_VIEWER_EXCEPT_HOST_HEADER_ORIGIN_REQUEST_POLICY_ID);
|
||||
|
||||
given().relaxedHTTPSValidation().baseUri("https://localhost").port(sslPort)
|
||||
.header("Host", created.getDomainName())
|
||||
.when().get("/")
|
||||
.then().statusCode(403)
|
||||
.header("lambda-web-request-id", matchesPattern("[A-Z2-7]{26}"))
|
||||
.body(containsString("You are not authorized to access this endpoint."));
|
||||
}
|
||||
}
|
||||
+51
@@ -1,6 +1,11 @@
|
||||
package io.github.hectorvent.floci.services.lambdaweb;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import io.github.hectorvent.floci.services.cloudfront.CloudFrontService;
|
||||
import io.github.hectorvent.floci.services.cloudfront.model.DefaultCacheBehavior;
|
||||
import io.github.hectorvent.floci.services.cloudfront.model.Distribution;
|
||||
import io.github.hectorvent.floci.services.cloudfront.model.DistributionConfig;
|
||||
import io.github.hectorvent.floci.services.cloudfront.model.Origin;
|
||||
import io.github.hectorvent.floci.services.lambdaweb.LambdaWebEnvironmentManager.Environment;
|
||||
import io.github.hectorvent.floci.services.s3.S3Service;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
@@ -25,6 +30,7 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -39,9 +45,11 @@ import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.endsWith;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.matchesPattern;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
@@ -119,6 +127,9 @@ class LambdaWebDataPlaneDockerIntegrationTest {
|
||||
@Inject
|
||||
LambdaWebMetrics metrics;
|
||||
|
||||
@Inject
|
||||
CloudFrontService cloudFrontService;
|
||||
|
||||
private String functionName;
|
||||
|
||||
@BeforeAll
|
||||
@@ -207,6 +218,46 @@ class LambdaWebDataPlaneDockerIntegrationTest {
|
||||
.body("headers.x-amz-date", equalTo("20260101T000000Z"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Timeout(180)
|
||||
void aCloudFrontDistributionReachesTheEndpointAsItsOrigin() throws Exception {
|
||||
String domain = createFunction(30);
|
||||
Origin origin = new Origin();
|
||||
origin.setId("web");
|
||||
origin.setDomainName(domain.substring(0, domain.lastIndexOf(':')));
|
||||
Map<String, Object> customOriginConfig = new LinkedHashMap<>();
|
||||
customOriginConfig.put("HTTPPort", "80");
|
||||
customOriginConfig.put("HTTPSPort", "443");
|
||||
customOriginConfig.put("OriginProtocolPolicy", "https-only");
|
||||
origin.setCustomOriginConfig(customOriginConfig);
|
||||
DefaultCacheBehavior behavior = new DefaultCacheBehavior();
|
||||
behavior.setTargetOriginId("web");
|
||||
behavior.setViewerProtocolPolicy("allow-all");
|
||||
behavior.setCachePolicyId("4135ea2d-6df8-44a3-9df3-4b5a84be39ad");
|
||||
behavior.setOriginRequestPolicyId("b689b0a8-53d0-40ab-baf2-68738e2966ac");
|
||||
DistributionConfig config = new DistributionConfig();
|
||||
config.setEnabled(true);
|
||||
config.setOrigins(List.of(origin));
|
||||
config.setDefaultCacheBehavior(behavior);
|
||||
Distribution distribution = new Distribution();
|
||||
distribution.setConfig(config);
|
||||
Distribution created = cloudFrontService.createDistribution(distribution, Map.of());
|
||||
try {
|
||||
given().header("Host", created.getDomainName()).header("x-custom", "1").when().get("/a/b?x=1")
|
||||
.then().statusCode(200)
|
||||
.header("lambda-web-request-id", matchesPattern(REQUEST_ID))
|
||||
.header("lambda-web-logstream", notNullValue())
|
||||
.body("url", equalTo("/a/b?x=1"))
|
||||
.body("headers.host", equalTo(domain))
|
||||
.body("headers.x-custom", equalTo("1"))
|
||||
.body("headers.via", endsWith(" (CloudFront)"))
|
||||
.body("headers.x-amz-cf-id", notNullValue())
|
||||
.body("headers.lambda-web-endpoint-name", equalTo("dev"));
|
||||
} finally {
|
||||
cloudFrontService.removeDistribution(created.getId());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Timeout(180)
|
||||
void theOutputGoesToTheLocalAndTheCentralizedLogGroupAsJsonEvents() throws Exception {
|
||||
|
||||
+19
-1
@@ -50,8 +50,12 @@ class LambdaWebServiceTest {
|
||||
}
|
||||
|
||||
private LambdaWebService service(StorageFactory storage) {
|
||||
return service(storage, "http://localhost:4566");
|
||||
}
|
||||
|
||||
private LambdaWebService service(StorageFactory storage, String baseUrl) {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
when(config.effectiveBaseUrl()).thenReturn("http://localhost:4566");
|
||||
when(config.effectiveBaseUrl()).thenReturn(baseUrl);
|
||||
LambdaWebService service = new LambdaWebService(new RegionResolver(REGION, ACCOUNT_ID), storage, config, null,
|
||||
mapper, null);
|
||||
service.initializeStorage();
|
||||
@@ -569,6 +573,20 @@ class LambdaWebServiceTest {
|
||||
assertTrue(second.locate(domain).isEmpty(), "a deleted endpoint's domain is gone");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aCloudFrontOriginNamesAnEndpointDomainWithoutCaseTrailingDotOrPort() {
|
||||
LambdaWebService service = service(new MemoryStorageFactory(), "http://Floci.Example.:4566");
|
||||
service.createFunction(REGION, createRequest("app"));
|
||||
String domain = service.getEndpoint(REGION, "app", "dev").domainName;
|
||||
assertTrue(domain.endsWith(".001.lambda-web.us-east-1.Floci.Example.:4566"), domain);
|
||||
String host = domain.substring(0, domain.indexOf('.')) + ".001.lambda-web.us-east-1.floci.example";
|
||||
|
||||
assertEquals(domain, service.endpointDomain(host).orElseThrow());
|
||||
assertTrue(service.endpointDomain("nope.001.lambda-web.us-east-1.floci.example").isEmpty());
|
||||
service.deleteEndpoint(REGION, "app", "dev");
|
||||
assertTrue(service.endpointDomain(host).isEmpty(), "a deleted endpoint's domain is gone");
|
||||
}
|
||||
|
||||
private static final class MemoryStorageFactory extends StorageFactory {
|
||||
private final Map<String, StorageBackend<String, ?>> stores = new HashMap<>();
|
||||
|
||||
|
||||
Reference in new issue
Block a user