Light, fluffy, and always free - The AWS Local Emulator alternative
test(partition): positive China Lambda layer test, partitions.md gaps, drop AwsRegions.ALL (#5023)
Hector Ventura6568700e02b5
diff --git a/docs/configuration/partitions.md b/docs/configuration/partitions.mdindex ba2d2a4b3d..1dbab80b30 100644--- a/docs/configuration/partitions.md+++ b/docs/configuration/partitions.md@@ -169,14 +169,19 @@ the commercial value or Floci's own base host until sourced:- the console device-authorization client ids (`arn:aws:signin:::devtools/...`) outside thecommercial partition;- the STS web-identity audience outside the commercial partition;+- the EKS Pod Identity token audience (`pods.eks.amazonaws.com`) outside the commercial partition;- the Lambda function-URL host outside the commercial partition;- whether AWS managed policy documents differ in content in China or GovCloud;- the API Gateway regional hosted zone per region;- S3 `LocationConstraint` enum values and Route 53 hosted zones for the ISO and EUSC regions;- the SAML sign-on URL in `aws-iso-e`, `aws-iso-f` and `aws-eusc`, where assertions keep being- checked against the commercial `https://signin.aws.amazon.com/saml`.+ checked against the commercial `https://signin.aws.amazon.com/saml`;+- the exact error AWS WAF returns for the `CLOUDFRONT` scope in a partition without CloudFront+ (GovCloud, the ISO partitions): Floci refuses it with `WAFInvalidParameterException` and its own+ message.## Related-- [Environment Variables](environment-variables.md): `FLOCI_DEFAULT_REGION`, `FLOCI_PARTITIONS_ID`, `FLOCI_PARTITIONS_STRICT`+- [Environment Variables](environment-variables.md): `FLOCI_DEFAULT_REGION`, `FLOCI_PARTITIONS_ID`,+ `FLOCI_PARTITIONS_STRICT`, `FLOCI_PARTITIONS_ALLOW_UNKNOWN_REGIONS`- [Multi-Account Isolation](multi-account.md): the account half of the credential scopediff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsRegions.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsRegions.javaindex a8ffc783b6..4306bce0b8 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/AwsRegions.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsRegions.java@@ -11,21 +11,15 @@ import java.util.Set;*/public final class AwsRegions {- /**- * The commercial partition's published regions, in botocore order. Callers that serve one- * request should prefer {@link #advertised(String)} with the request's partition; this list- * is what static, deployment-wide consumers (certificate SANs) enumerate.- */- public static final List<String> ALL = advertised(AwsPartitions.COMMERCIAL_ID);-/*** Every published AWS region id, across all partitions.*- * <p>Distinct from {@link #ALL}, and deliberately a superset of it. {@code ALL} is what- * this emulator <em>advertises</em> in the commercial partition. {@code KNOWN_IDS} is for- * <em>recognising</em> a region id that a client wrote, most importantly inside a hostname,- * where the alternative is a shape like {@code [a-z]{2}-[a-z-]+-\d+} that also matches- * perfectly ordinary strings ({@code my-cd-1}, {@code eu-team-2}).+ * <p>Distinct from {@link #advertised(String)}, and deliberately a superset of every advertised+ * list: {@code advertised} is what this emulator <em>advertises</em> in one partition, while+ * {@code KNOWN_IDS} is for <em>recognising</em> a region id that a client wrote, most+ * importantly inside a hostname, where the alternative is a shape like+ * {@code [a-z]{2}-[a-z-]+-\d+} that also matches perfectly ordinary strings ({@code my-cd-1},+ * {@code eu-team-2}).** <p>It follows botocore's published lists ({@code make aws-data-sync}); the cost of a* region newer than the vendored data is documented at each call site, because it differsdiff --git a/src/test/java/io/github/hectorvent/floci/config/TlsCertificateHostnameTest.java b/src/test/java/io/github/hectorvent/floci/config/TlsCertificateHostnameTest.javaindex e399befbe8..696b456e69 100644--- a/src/test/java/io/github/hectorvent/floci/config/TlsCertificateHostnameTest.java+++ b/src/test/java/io/github/hectorvent/floci/config/TlsCertificateHostnameTest.java@@ -485,7 +485,7 @@ class TlsCertificateHostnameTest {X509Certificate cert = parseCertificate(certFile);List<String> sans = extractSansFromCertificate(cert);- for (String region : AwsRegions.ALL) {+ for (String region : AwsRegions.advertised("aws")) {String expectedWildcard = "*.dkr.ecr." + region + ".localhost.floci.io";assertTrue(TlsConfigSource.DEFAULT_SAN_HOSTNAMES.contains(expectedWildcard),"DEFAULT_SAN_HOSTNAMES missing ECR wildcard for region " + region);diff --git a/src/test/java/io/github/hectorvent/floci/core/common/AwsRegionsTest.java b/src/test/java/io/github/hectorvent/floci/core/common/AwsRegionsTest.javaindex 7895df14c7..3e0b1d9ecf 100644--- a/src/test/java/io/github/hectorvent/floci/core/common/AwsRegionsTest.java+++ b/src/test/java/io/github/hectorvent/floci/core/common/AwsRegionsTest.java@@ -21,7 +21,7 @@ class AwsRegionsTest {*/@Testvoid everyAdvertisedRegionIsAKnownRegionId() {- for (String region : AwsRegions.ALL) {+ for (String region : AwsRegions.advertised("aws")) {assertTrue(AwsRegions.KNOWN_IDS.contains(region),region + " is advertised by DescribeRegions but is not a known region id");}@@ -111,8 +111,7 @@ class AwsRegionsTest {@Testvoid advertisedListsArePerPartition() {- assertEquals(AwsRegions.ALL, AwsRegions.advertised("aws"));- assertEquals(34, AwsRegions.ALL.size());+ assertEquals(34, AwsRegions.advertised("aws").size());assertEquals(List.of("cn-north-1", "cn-northwest-1"), AwsRegions.advertised("aws-cn"));assertTrue(AwsRegions.KNOWN_IDS.contains("eusc-de-east-1"));assertTrue(AwsRegions.KNOWN_IDS.contains("us-isof-south-1"));diff --git a/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.javaindex 1879bf674e..a84e89083d 100644--- a/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java@@ -169,17 +169,17 @@ class EksClusterManagerTest {@Testvoid registriesYamlMirrorsEveryRegionHostnameAndThePathStyleForm() {String yaml = EksClusterManager.buildRegistriesYaml(- "000000000000", AwsRegions.ALL, 4566, "http://floci:4566");+ "000000000000", AwsRegions.advertised("aws"), 4566, "http://floci:4566");assertTrue(yaml.startsWith("mirrors:\n"));- for (String region : AwsRegions.ALL) {+ for (String region : AwsRegions.advertised("aws")) {assertTrue(yaml.contains("\"000000000000.dkr.ecr." + region + ".localhost:4566\":"),"should mirror the " + region + " hostname");}assertTrue(yaml.contains("\"localhost:4566\":"), "should mirror the path-style form");assertFalse(yaml.contains("\"*\""), "must not catch-all public registries");long endpoints = yaml.lines().filter(l -> l.contains("- \"http://floci:4566\"")).count();- assertEquals(AwsRegions.ALL.size() + 1, endpoints,+ assertEquals(AwsRegions.advertised("aws").size() + 1, endpoints,"every mirror should point at Floci's in-network data plane");}@@ -197,9 +197,9 @@ class EksClusterManagerTest {void registriesYamlTlsAliasesPreserveLoopbackMirrorsAndInternalHttpEndpoint() {String endpoint = "http://host.docker.internal:4577";String yaml = EksClusterManager.buildRegistriesYaml(- "111122223333", AwsRegions.ALL, 4577, endpoint, true);+ "111122223333", AwsRegions.advertised("aws"), 4577, endpoint, true);- for (String region : AwsRegions.ALL) {+ for (String region : AwsRegions.advertised("aws")) {assertTrue(yaml.contains("\"111122223333.dkr.ecr." + region + ".localhost:4577\":"));assertTrue(yaml.contains("\"111122223333.dkr.ecr." + region + ".localhost.floci.io:4577\":"));}@@ -211,7 +211,7 @@ class EksClusterManagerTest {assertFalse(yaml.contains("ghcr.io"));assertFalse(yaml.contains("https://"), "internal pulls still use the HTTP data plane");long endpoints = yaml.lines().filter(line -> line.contains("- \"" + endpoint + "\"")).count();- assertEquals(2L * (AwsRegions.ALL.size() + 1), endpoints);+ assertEquals(2L * (AwsRegions.advertised("aws").size() + 1), endpoints);}@Testdiff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaLayerChinaIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaLayerChinaIntegrationTest.javanew file mode 100644index 0000000000..f63106fdda--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaLayerChinaIntegrationTest.java@@ -0,0 +1,107 @@+package io.github.hectorvent.floci.services.lambda;++import io.github.hectorvent.floci.testing.PartitionCleanup;+import io.github.hectorvent.floci.testing.PartitionMatrix;+import io.quarkus.test.junit.QuarkusTest;+import org.junit.jupiter.api.Test;+import org.junit.jupiter.api.extension.RegisterExtension;++import java.io.ByteArrayOutputStream;+import java.util.Base64;+import java.util.zip.ZipEntry;+import java.util.zip.ZipOutputStream;++import static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.equalTo;+import static org.hamcrest.Matchers.startsWith;++/**+ * The positive twin of the cross-partition layer cases: a layer published under a cn-north-1+ * scope carries an {@code arn:aws-cn:} ARN, and that ARN resolves for a caller in the same+ * partition, both through GetLayerVersionByArn and when a function attaches it.+ */+@QuarkusTest+class LambdaLayerChinaIntegrationTest {++ private static final String REGION = "cn-north-1";+ private static final String LAYER_NAME = "china-positive-layer";+ private static final String FUNCTION_NAME = "china-positive-layer-fn";++ @RegisterExtension+ final PartitionCleanup cleanup = new PartitionCleanup();++ private static String auth() {+ return PartitionMatrix.sigV4Auth(REGION, "lambda");+ }++ private static String zipBase64(String path, String content) throws Exception {+ ByteArrayOutputStream baos = new ByteArrayOutputStream();+ try (ZipOutputStream zos = new ZipOutputStream(baos)) {+ zos.putNextEntry(new ZipEntry(path));+ zos.write(content.getBytes());+ zos.closeEntry();+ }+ return Base64.getEncoder().encodeToString(baos.toByteArray());+ }++ @Test+ void aLayerPublishedInChinaResolvesByItsChinaArn() throws Exception {+ String layerVersionArn = given()+ .header("Authorization", auth())+ .contentType("application/json")+ .body("""+ {+ "Content": { "ZipFile": "%s" },+ "CompatibleRuntimes": ["python3.12"]+ }+ """.formatted(zipBase64("python/shared.py", "VALUE = 1")))+ .when()+ .post("/2018-10-31/layers/" + LAYER_NAME + "/versions")+ .then()+ .statusCode(201)+ .body("LayerVersionArn", startsWith("arn:aws-cn:lambda:cn-north-1:000000000000:layer:" + LAYER_NAME + ":"))+ .extract().path("LayerVersionArn");+ String version = layerVersionArn.substring(layerVersionArn.lastIndexOf(':') + 1);+ cleanup.register(() -> given()+ .header("Authorization", auth())+ .when()+ .delete("/2018-10-31/layers/" + LAYER_NAME + "/versions/" + version)+ .then()+ .statusCode(204));++ given()+ .header("Authorization", auth())+ .queryParam("find", "LayerVersion")+ .queryParam("Arn", layerVersionArn)+ .when()+ .get("/2018-10-31/layers")+ .then()+ .statusCode(200)+ .body("LayerVersionArn", equalTo(layerVersionArn));++ given()+ .header("Authorization", auth())+ .contentType("application/json")+ .body("""+ {+ "FunctionName": "%s",+ "Runtime": "python3.12",+ "Role": "arn:aws-cn:iam::000000000000:role/r",+ "Handler": "handler.handler",+ "Code": { "ZipFile": "%s" },+ "Layers": ["%s"]+ }+ """.formatted(FUNCTION_NAME, zipBase64("handler.py", "def handler(e, c): return {}"), layerVersionArn))+ .when()+ .post("/2015-03-31/functions")+ .then()+ .statusCode(201)+ .body("Layers[0].Arn", equalTo(layerVersionArn));+ cleanup.register(() -> given()+ .header("Authorization", auth())+ .when()+ .delete("/2015-03-31/functions/" + FUNCTION_NAME)+ .then()+ .statusCode(204));+ }+}