Light, fluffy, and always free - The AWS Local Emulator alternative
fix(lambda): refuse function URLs in GovCloud and China (#5615)
Hector Ventura255162654f12
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaFunctionUrlTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaFunctionUrlTest.javaindex 5c1492d73c..494b7e4211 100644--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaFunctionUrlTest.java+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaFunctionUrlTest.java@@ -6,6 +6,8 @@ import software.amazon.awssdk.services.lambda.LambdaClient;import software.amazon.awssdk.services.lambda.model.*;import software.amazon.awssdk.services.lambda.model.Runtime;+import java.util.List;+import static org.assertj.core.api.Assertions.*;@DisplayName("Lambda Function URL Config")@@ -18,6 +20,8 @@ class LambdaFunctionUrlTest {@BeforeAllstatic void setup() {+ Assumptions.assumeFalse(List.of("aws-cn", "aws-us-gov").contains(TestFixtures.partition()),+ "Lambda function URLs are not available in the China and GovCloud partitions");lambda = TestFixtures.lambdaClient();lambda.createFunction(CreateFunctionRequest.builder().functionName(FUNCTION_NAME)diff --git a/docs/configuration/partitions.md b/docs/configuration/partitions.mdindex a050ce5f7f..d062c394c0 100644--- a/docs/configuration/partitions.md+++ b/docs/configuration/partitions.md@@ -125,6 +125,11 @@ back to `aws` for a label no partition publishes.refused outside the commercial partition (`BadRequestException`): GovCloud, EUSC and ISO have noCloudFront, and China has no edge-optimized API Gateway([China API Gateway](https://docs.amazonaws.cn/en_us/aws/latest/userguide/api-gateway.html)).+ A Lambda function URL is refused in GovCloud and China (`InvalidParameterValueException` on+ `CreateFunctionUrlConfig`), where the+ [GovCloud](https://docs.aws.amazon.com/govcloud-us/latest/UserGuide/govcloud-lambda.html) and+ [China](https://docs.amazonaws.cn/en_us/aws/latest/userguide/lambda.html) guides list function URLs+ as not available.- **VPC endpoint service names**: interface endpoints (as `DescribeVpcEndpointServices` lists them)are `com.amazonaws.<region>.<service>` everywhere, except the (region, service) pairs the CDKlists for China, ISO and EUSC, which reverse the DNS suffix (`cn.com.amazonaws.cn-north-1.lambda`).@@ -237,7 +242,8 @@ the commercial value or Floci's own base host until sourced:- the China CloudFront distribution domain suffix (only the API host is published);- 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;+- the Lambda function-URL host in the ISO and EUSC partitions (function URLs are refused in GovCloud+ and China, where the partition guides list them as not available);- whether AWS managed policy documents differ in content in China or GovCloud;- the API Gateway regional hosted zone per region;- the availability-zone id prefix of the ISO and EUSC regions: zone ids there keep the whole regiondiff --git a/docs/services/lambda.md b/docs/services/lambda.mdindex eed68ae1b9..bf8c34e735 100644--- a/docs/services/lambda.md+++ b/docs/services/lambda.md@@ -375,6 +375,13 @@ disabled fails with Lambda's error as the resource status reason.Function URLs are also reachable directly on `/{proxy:.*}` under the Lambda URL controller, which routes the request into the normal `Invoke` path.+Function URLs are not available in AWS GovCloud (US) or in the China Regions+([GovCloud guide](https://docs.aws.amazon.com/govcloud-us/latest/UserGuide/govcloud-lambda.html),+[China guide](https://docs.amazonaws.cn/en_us/aws/latest/userguide/lambda.html)), so+`CreateFunctionUrlConfig` is an `InvalidParameterValueException` in those regions, and an+`AWS::Lambda::Url` in a CloudFormation stack there fails with it. See+[AWS Partitions](../configuration/partitions.md).+**Versions:** `CreateFunction` and `UpdateFunctionCode` honour `Publish`, publishing a versionand reporting it in the response's `Version`. The two differ in the ARN they return, matching thelive service: `CreateFunction` keeps the unqualified `FunctionArn` while `UpdateFunctionCode`diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.javaindex 7215086605..5c99f84044 100644--- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java@@ -7,6 +7,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.core.common.AwsArnUtils;import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.core.common.AwsPartition;+import io.github.hectorvent.floci.core.common.AwsPartitions;import io.github.hectorvent.floci.core.common.AwsRegions;import io.github.hectorvent.floci.core.common.CustomResourceLiveness;import io.github.hectorvent.floci.core.common.Digests;@@ -3050,7 +3052,27 @@ public class LambdaService implements ResourceProvider {// ──────────────────────────── Function URL Config ────────────────────────────+ /** The partitions where AWS documents Lambda function URLs as not available. */+ private static final Set<String> FUNCTION_URL_UNAVAILABLE_PARTITIONS = Set.of("aws-us-gov", "aws-cn");++ /**+ * Function URLs are not available in AWS GovCloud (US) or in the China Regions: "AWS Lambda+ * Function URLs is not available"+ * (https://docs.aws.amazon.com/govcloud-us/latest/UserGuide/govcloud-lambda.html) and "Amazon+ * Lambda Function URLs is not available in the China Regions"+ * (https://docs.amazonaws.cn/en_us/aws/latest/userguide/lambda.html). No source shows AWS's+ * message for the refusal, so the wording is Floci's own.+ */+ private static void requireFunctionUrlsAvailable(String region) {+ AwsPartition partition = AwsPartitions.forRegionOrCommercial(region);+ if (FUNCTION_URL_UNAVAILABLE_PARTITIONS.contains(partition.id())) {+ throw new AwsException("InvalidParameterValueException",+ "Function URLs are not available in partition " + partition.id() + ".", 400);+ }+ }+public LambdaUrlConfig createFunctionUrlConfig(String region, String functionName, String qualifier, Map<String, Object> request) {+ requireFunctionUrlsAvailable(region);validateFunctionUrlQualifier(qualifier);LambdaArnUtils.ResolvedFunctionRef ref = resolveWithRegion(region, functionName, qualifier);functionName = ref.name();diff --git a/src/test/java/io/github/hectorvent/floci/core/common/PartitionRegionGatesIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/core/common/PartitionRegionGatesIntegrationTest.javaindex b1a0431a13..c8a5d32519 100644--- a/src/test/java/io/github/hectorvent/floci/core/common/PartitionRegionGatesIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/core/common/PartitionRegionGatesIntegrationTest.java@@ -148,6 +148,39 @@ class PartitionRegionGatesIntegrationTest {switchRestApiToEdge("us-east-1", commercialId).then().statusCode(200);}+ private Response createFunctionUrl(String region) {+ String name = "url-gate-" + Long.toString(System.nanoTime(), 36);+ // By name, before the create: a failure reading the response then leaves nothing behind.+ cleanup.register(() -> given()+ .header("Authorization", PartitionMatrix.sigV4Auth(region, "lambda"))+ .when().delete("/2015-03-31/functions/" + name));+ given()+ .header("Authorization", PartitionMatrix.sigV4Auth(region, "lambda"))+ .contentType("application/json")+ .body("{\"FunctionName\":\"" + name + "\",\"Runtime\":\"nodejs20.x\",\"Handler\":\"index.handler\","+ + "\"Role\":\"arn:" + AwsRegions.partitionFor(region) + ":iam::000000000000:role/lambda-role\"}")+ .when().post("/2015-03-31/functions")+ .then().statusCode(201);+ return given()+ .header("Authorization", PartitionMatrix.sigV4Auth(region, "lambda"))+ .contentType("application/json")+ .body("{\"AuthType\":\"NONE\"}")+ .when().post("/2021-10-31/functions/" + name + "/url");+ }++ /** GovCloud and China have Lambda but no function URLs, so creating one is refused there. */+ @Test+ void functionUrlsAreRejectedInGovCloudAndChina() {+ for (String region : List.of("us-gov-west-1", "cn-north-1")) {+ createFunctionUrl(region).then().statusCode(400)+ .header("x-amzn-ErrorType", containsString("InvalidParameterValueException"))+ .body(containsString("not available in partition " + AwsRegions.partitionFor(region)));+ }++ createFunctionUrl("us-east-1").then().statusCode(201)+ .body("FunctionUrl", containsString(".lambda-url.us-east-1."));+ }+@Testvoid cloudFrontScopeLivesInThePartitionsImplicitGlobalRegion() {String name = "cn-" + Long.toString(System.nanoTime(), 36);diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaFunctionUrlPartitionTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaFunctionUrlPartitionTest.javanew file mode 100644index 0000000000..e02519b6e6--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaFunctionUrlPartitionTest.java@@ -0,0 +1,94 @@+package io.github.hectorvent.floci.services.lambda;++import io.github.hectorvent.floci.config.EmulatorConfig;+import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.core.common.AwsRegions;+import io.github.hectorvent.floci.core.common.RegionResolver;+import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;+import io.github.hectorvent.floci.core.storage.InMemoryStorage;+import io.github.hectorvent.floci.core.storage.StorageFactory;+import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;+import io.github.hectorvent.floci.services.lambda.model.LambdaUrlConfig;+import io.github.hectorvent.floci.services.lambda.zip.CodeStore;+import io.github.hectorvent.floci.services.lambda.zip.ZipExtractor;+import org.junit.jupiter.api.BeforeEach;+import org.junit.jupiter.params.ParameterizedTest;+import org.junit.jupiter.params.provider.ValueSource;++import java.nio.file.Path;+import java.util.HashMap;+import java.util.Map;++import static org.hamcrest.MatcherAssert.assertThat;+import static org.hamcrest.Matchers.containsString;+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.mockito.ArgumentMatchers.any;+import static org.mockito.ArgumentMatchers.anyString;+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;+import static org.mockito.Mockito.mock;+import static org.mockito.Mockito.when;++/**+ * Lambda function URLs are not available in AWS GovCloud (US) or in the China Regions, so one+ * cannot be created there, while the commercial partition and the partitions no source covers+ * are unchanged.+ */+class LambdaFunctionUrlPartitionTest {++ private static final String ACCOUNT = "000000000000";+ private static final String FUNCTION_NAME = "url-gate-fn";++ private LambdaService service;++ @BeforeEach+ void setUp() {+ StorageFactory storageFactory = mock(StorageFactory.class);+ when(storageFactory.create(anyString(), anyString(), any()))+ .thenAnswer(invocation -> AccountAwareStorageBackend.inMemory(ACCOUNT));+ EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);+ when(config.effectiveBaseUrl()).thenReturn("http://localhost:4566");+ service = new LambdaService(new LambdaFunctionStore(new InMemoryStorage<String, LambdaFunction>()),+ new WarmPool(), new CodeStore(Path.of("target/test-data/lambda-code")), new ZipExtractor(),+ config, new RegionResolver("us-east-1", ACCOUNT), storageFactory);+ }++ private void createFunction(String region) {+ Map<String, Object> request = new HashMap<>();+ request.put("FunctionName", FUNCTION_NAME);+ request.put("Runtime", "nodejs20.x");+ request.put("Role", "arn:" + AwsRegions.partitionFor(region) + ":iam::" + ACCOUNT + ":role/test-role");+ request.put("Handler", "index.handler");+ service.createFunction(region, request);+ }++ @ParameterizedTest+ @ValueSource(strings = {"us-gov-west-1", "us-gov-east-1", "cn-north-1", "cn-northwest-1"})+ void aFunctionUrlIsRefusedInGovCloudAndChina(String region) {+ createFunction(region);++ AwsException error = assertThrows(AwsException.class,+ () -> service.createFunctionUrlConfig(region, FUNCTION_NAME, null, Map.of("AuthType", "NONE")));++ assertEquals("InvalidParameterValueException", error.getErrorCode());+ assertEquals(400, error.getHttpStatus());+ assertThat(error.getMessage(),+ containsString("not available in partition " + AwsRegions.partitionFor(region)));+ assertNull(service.getFunction(region, FUNCTION_NAME).getUrlConfig());+ }++ @ParameterizedTest+ @ValueSource(strings = {"us-east-1", "eu-west-1", "us-iso-east-1", "eusc-de-east-1"})+ void aFunctionUrlIsAcceptedWhereNoSourceRulesItOut(String region) {+ createFunction(region);++ LambdaUrlConfig created =+ service.createFunctionUrlConfig(region, FUNCTION_NAME, null, Map.of("AuthType", "NONE"));++ assertEquals("NONE", created.getAuthType());+ assertThat(created.getFunctionUrl(), containsString(".lambda-url." + region + "."));+ assertEquals(created.getFunctionUrl(),+ service.getFunctionUrlConfig(region, FUNCTION_NAME, null).getFunctionUrl());+ }+}