mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(lambdaweb): serve the MultiRegion global domain in the default region (#6460)
* feat(lambdaweb): serve the MultiRegion global domain in the default region AWS serves the global domain from the region nearest the caller. Floci now takes floci.default-region for where the caller is. The global domain is served in that region when the endpoint lists it, and in the home region otherwise. The environment, logs, metrics, SigV4 and aws:RequestedRegion then use that region, as on AWS. * fix(lambdaweb): use the serving region as aws:RequestedRegion for CloudFront A CloudFront origin request got the home region from the endpoint ARN. On AWS it is the region that serves the request, for the global domain and for a regional domain alike.
This commit is contained in:
1 parent
f49376f538
commit
acb9fd2cd6
6 files changed
+88
-8
No files matched your search
+34
-1
@@ -17,6 +17,7 @@ import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.github.hectorvent.floci.testing.EnforcementFixtures.auth;
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.matchesPattern;
|
||||
@@ -331,6 +332,34 @@ class LambdaWebDataPlaneIntegrationTest {
|
||||
given().when().delete(BASE + "/web-functions/" + functionName).then().statusCode(204);
|
||||
}
|
||||
|
||||
@Test
|
||||
void theGlobalDomainOfAFunctionInAnotherRegionCountsAPageInTheDefaultRegion() {
|
||||
String functionName = "dp-global-" + Long.toString(System.nanoTime(), 36);
|
||||
createFunction("us-west-2", functionName, "ApplicationManaged", LambdaWebZips.indexJs(""), ROLE);
|
||||
String west = auth("test", "us-west-2", "lambda");
|
||||
String endpoints = BASE + "/web-functions/" + functionName + "/endpoints";
|
||||
String revisionId = given().header("Authorization", west).when().get(endpoints + "/dev").then().statusCode(200)
|
||||
.extract().path("revisionWeights[0].revisionId");
|
||||
String global = given().header("Authorization", west).contentType("application/json")
|
||||
.body("""
|
||||
{"endpointName": "global", "endpointType": "MultiRegion", "regions": ["us-east-1"],
|
||||
"authType": "IamAuth", "revisionWeights": [{"revisionId": "%s", "weight": 100}]}"""
|
||||
.formatted(revisionId))
|
||||
.when().put(endpoints)
|
||||
.then().statusCode(202)
|
||||
.extract().path("domainName");
|
||||
given().header("Host", global).when().get("/").then().statusCode(403);
|
||||
|
||||
Map<String, String> endpoint = Map.of("FunctionName", functionName, "EndpointName", "global");
|
||||
metrics.awaitPublished(Duration.ofSeconds(5));
|
||||
assertEquals(1, LambdaWebMetricStatistics.of("us-east-1", LambdaWebMetrics.NAMESPACE,
|
||||
"HTTPResponseStatusClass4XX", endpoint, "Sum"));
|
||||
assertEquals(1, LambdaWebMetricStatistics.of("us-west-2", LambdaWebMetrics.NAMESPACE,
|
||||
"HTTPResponseStatusClass4XX", endpoint, "Sum"));
|
||||
given().header("Authorization", west).when().delete(BASE + "/web-functions/" + functionName)
|
||||
.then().statusCode(204);
|
||||
}
|
||||
|
||||
@Test
|
||||
void otherHostsStillReachTheControlPlane() {
|
||||
given().when().get(BASE + "/web-account-settings").then().statusCode(200);
|
||||
@@ -349,10 +378,14 @@ class LambdaWebDataPlaneIntegrationTest {
|
||||
}
|
||||
|
||||
private String createFunction(String functionName, String authType, byte[] code, String role) {
|
||||
return createFunction("us-east-1", functionName, authType, code, role);
|
||||
}
|
||||
|
||||
private String createFunction(String region, String functionName, String authType, byte[] code, String role) {
|
||||
LambdaWebRoles.trustLambda(iamService, role);
|
||||
s3Service.createBucket(BUCKET, "us-east-1");
|
||||
s3Service.putObject(BUCKET, "app.zip", code, "application/zip", Map.of());
|
||||
return given().contentType("application/json")
|
||||
return given().header("Authorization", auth("test", region, "lambda")).contentType("application/json")
|
||||
.body("""
|
||||
{"functionName": "%s",
|
||||
"revisionConfig": {
|
||||
|
||||
+19
-2
@@ -280,10 +280,27 @@ class LambdaWebIamAuthTest {
|
||||
assertFalse(allowsCloudFront(iamAuth.cloudFrontSignature(DISTRIBUTION_ARN)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void underEnforcementCloudFrontRequestsTheRegionThatServes() {
|
||||
when(config.services().iam().enforcementEnabled()).thenReturn(true);
|
||||
Map<String, String> signature = iamAuth.cloudFrontSignature(DISTRIBUTION_ARN);
|
||||
String allow = cloudFrontStatement("Allow", "aws:SourceArn", DISTRIBUTION_ARN);
|
||||
function.policy = cloudFrontPolicy(allow, cloudFrontStatement("Deny", "aws:RequestedRegion", "us-west-2"));
|
||||
assertTrue(allowsCloudFront(signature));
|
||||
assertFalse(allowsCloudFront(signature, WEST_HOST, "us-west-2"));
|
||||
function.policy = cloudFrontPolicy(allow, cloudFrontStatement("Deny", "aws:RequestedRegion", "us-east-1"));
|
||||
assertFalse(allowsCloudFront(signature));
|
||||
assertTrue(allowsCloudFront(signature, WEST_HOST, "us-west-2"));
|
||||
}
|
||||
|
||||
private boolean allowsCloudFront(Map<String, String> signature) {
|
||||
MultiMap headers = MultiMap.caseInsensitiveMultiMap().add("Host", HOST).addAll(signature);
|
||||
return allowsCloudFront(signature, HOST, "us-east-1");
|
||||
}
|
||||
|
||||
private boolean allowsCloudFront(Map<String, String> signature, String host, String servingRegion) {
|
||||
MultiMap headers = MultiMap.caseInsensitiveMultiMap().add("Host", host).addAll(signature);
|
||||
return iamAuth.allows(new Inbound("POST", "/", null, headers, MultiMap.caseInsensitiveMultiMap()),
|
||||
new WebFunctionTarget(ACCOUNT, "us-east-1", function, endpoint, "us-east-1"));
|
||||
new WebFunctionTarget(ACCOUNT, "us-east-1", function, endpoint, servingRegion));
|
||||
}
|
||||
|
||||
private static String cloudFrontPolicy(String... statements) {
|
||||
|
||||
@@ -651,6 +651,28 @@ class LambdaWebServiceTest {
|
||||
assertTrue(service.endpointDomain(host).isEmpty(), "a deleted endpoint's domain is gone");
|
||||
}
|
||||
|
||||
@Test
|
||||
void theGlobalDomainIsServedInTheDefaultRegionWhenTheEndpointHasIt() {
|
||||
LambdaWebService service = service();
|
||||
service.createFunction("us-west-2", createRequest("app"));
|
||||
String revisionId = service.getEndpoint("us-west-2", "app", "dev").revisionWeights.get(0).revisionId;
|
||||
String near = service.createEndpoint("us-west-2", "app", multiRegion("near", revisionId, REGION)).domainName;
|
||||
String far = service.createEndpoint("us-west-2", "app", multiRegion("far", revisionId, "eu-west-1")).domainName;
|
||||
|
||||
LambdaWebService.WebFunctionTarget target = service.locate(near).orElseThrow();
|
||||
assertEquals("us-west-2", target.region());
|
||||
assertEquals(REGION, target.servingRegion());
|
||||
assertEquals("us-west-2", service.locate(far).orElseThrow().servingRegion());
|
||||
}
|
||||
|
||||
private ObjectNode multiRegion(String name, String revisionId, String region) {
|
||||
ObjectNode request = mapper.createObjectNode().put("endpointName", name).put("endpointType", "MultiRegion")
|
||||
.put("authType", "ApplicationManaged");
|
||||
request.putArray("regions").add(region);
|
||||
request.putArray("revisionWeights").addObject().put("revisionId", revisionId).put("weight", 100);
|
||||
return request;
|
||||
}
|
||||
|
||||
@Test
|
||||
void eachRevisionKeepsTheCodeTheObjectHadWhenItWasCreated() throws IOException {
|
||||
S3Service s3 = mock(S3Service.class);
|
||||
|
||||
Reference in new issue
Block a user