diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudFrontFunctionTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudFrontFunctionTest.java new file mode 100644 index 0000000000..f9264edfd8 --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudFrontFunctionTest.java @@ -0,0 +1,68 @@ +package com.floci.test; + +import org.junit.jupiter.api.Test; +import software.amazon.awssdk.core.SdkBytes; +import software.amazon.awssdk.services.cloudfront.CloudFrontClient; +import software.amazon.awssdk.services.cloudfront.model.CreateFunctionResponse; +import software.amazon.awssdk.services.cloudfront.model.FunctionStage; +import software.amazon.awssdk.services.cloudfront.model.GetFunctionResponse; + +import java.nio.charset.StandardCharsets; + +import static org.assertj.core.api.Assertions.assertThat; + +class CloudFrontFunctionTest { + + @Test + void getFunctionReturnsTheDecodedFunctionCode() { + String name = "compat-function-" + TestFixtures.uniqueName(); + byte[] code = "function handler(event) { return event.request; }" + .getBytes(StandardCharsets.UTF_8); + byte[] updatedCode = "function handler(event) { return event.response; }" + .getBytes(StandardCharsets.UTF_8); + String etag = null; + String updatedEtag = null; + + try (CloudFrontClient cloudFront = TestFixtures.cloudFrontClient()) { + CreateFunctionResponse created = cloudFront.createFunction(request -> request + .name(name) + .functionConfig(config -> config + .comment("compat round trip") + .runtime("cloudfront-js-2.0")) + .functionCode(SdkBytes.fromByteArray(code))); + String createEtag = created.eTag(); + etag = createEtag; + + GetFunctionResponse function = cloudFront.getFunction(request -> request + .name(name) + .stage(FunctionStage.DEVELOPMENT)); + assertThat(function.functionCode().asByteArray()).isEqualTo(code); + assertThat(function.eTag()).isEqualTo(createEtag); + assertThat(function.contentType()).isEqualTo("application/octet-stream"); + + cloudFront.updateFunction(request -> request + .name(name) + .ifMatch(createEtag) + .functionConfig(config -> config + .comment("compat round trip") + .runtime("cloudfront-js-2.0")) + .functionCode(SdkBytes.fromByteArray(updatedCode))); + + GetFunctionResponse afterUpdate = cloudFront.getFunction(request -> request + .name(name) + .stage(FunctionStage.DEVELOPMENT)); + updatedEtag = afterUpdate.eTag(); + assertThat(afterUpdate.functionCode().asByteArray()).isEqualTo(updatedCode); + assertThat(afterUpdate.contentType()).isEqualTo("application/octet-stream"); + } finally { + try (CloudFrontClient cloudFront = TestFixtures.cloudFrontClient()) { + String currentEtag = updatedEtag != null ? updatedEtag : etag; + if (currentEtag != null) { + cloudFront.deleteFunction(request -> request + .name(name) + .ifMatch(currentEtag)); + } + } + } + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CloudFrontCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CloudFrontCfnProvisioner.java index 87914f0309..eff54d7225 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CloudFrontCfnProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CloudFrontCfnProvisioner.java @@ -21,8 +21,10 @@ import io.github.hectorvent.floci.services.cloudfront.model.ResponseHeadersPolic import jakarta.enterprise.context.ApplicationScoped; import org.jboss.logging.Logger; +import java.nio.charset.StandardCharsets; import java.time.Instant; import java.util.ArrayList; +import java.util.Base64; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; @@ -286,6 +288,9 @@ public class CloudFrontCfnProvisioner implements CfnResourceProvisioner { * one after the update. Any other change updates the DEVELOPMENT stage in place, and * {@code AutoPublish: true} then publishes it to LIVE, which {@code Fn::GetAtt Stage} reports. * Floci has no Key Value Store, so {@code FunctionConfig.KeyValueStoreAssociations} is ignored. + * The template's {@code FunctionCode} is the raw code text, as in the CloudFormation example + * templates, and it is stored in the base64 form the CreateFunction API carries on the wire, so + * {@code GetFunction} serves the template text back. * *
When publishing or tagging fails, a function this call created is deleted again. An
* in-place update is not reverted, the same limit as the policy types above.
@@ -305,7 +310,7 @@ public class CloudFrontCfnProvisioner implements CfnResourceProvisioner {
Map Records persisted by older Floci versions may hold plain code instead: the
+ * CloudFormation provisioner once wrote the template's raw body. Those still read back
+ * as their stored bytes rather than failing, and the next UpdateFunction rewrites them
+ * into the uniform base64 form.
+ */
+ private byte[] functionCodeBytes(CloudFrontFunction fn) {
+ String encoded = fn.getFunctionCode();
+ if (encoded == null || encoded.isBlank()) {
+ return new byte[0];
+ }
+ try {
+ return Base64.getDecoder().decode(encoded.trim());
+ } catch (IllegalArgumentException e) {
+ LOG.warnv("Function {0} holds non-base64 code persisted by an older Floci version; serving it as stored",
+ fn.getName());
+ return encoded.getBytes(StandardCharsets.UTF_8);
+ }
+ }
+
@GET
@Path("/function/{Name}/describe")
public Response describeFunction(@PathParam("Name") String name,
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationCloudFrontKeysAndFunctionsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationCloudFrontKeysAndFunctionsIntegrationTest.java
index 9d0a3af44f..7a63c7c38a 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationCloudFrontKeysAndFunctionsIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationCloudFrontKeysAndFunctionsIntegrationTest.java
@@ -17,6 +17,7 @@ import java.util.List;
import java.util.Map;
import static io.restassured.RestAssured.given;
+import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -65,15 +66,14 @@ class CloudFormationCloudFrontKeysAndFunctionsIntegrationTest {
assertEquals(functionArn, output(describe, "FunctionArn"));
assertEquals(functionArn, output(describe, "FunctionMetadataArn"));
assertEquals("LIVE", output(describe, "FunctionStage"));
- assertEquals("v1", cloudFrontService.describeFunction(functionName, "LIVE").getFunctionCode());
+ assertFunctionCode(functionName, "LIVE", "v1");
assertEquals(Map.of("Env", "test"), cloudFrontService.listTagsForResource(functionArn));
String otherStack = stackName + "-other";
stackCall("CreateStack", otherStack, template(suffix + "-other", rsaPublicKeyPem(), "other",
functionName, "other code"));
assertStatus(otherStack, "ROLLBACK_COMPLETE");
- assertEquals("v1", cloudFrontService.describeFunction(functionName, null).getFunctionCode(),
- "a second stack cannot take the name, and its rollback keeps the first function");
+ assertFunctionCode(functionName, "DEVELOPMENT", "v1");
stackCall("UpdateStack", stackName, template(suffix, pem, "second", functionName, "v2"));
assertStatus(stackName, "UPDATE_COMPLETE");
@@ -82,8 +82,8 @@ class CloudFormationCloudFrontKeysAndFunctionsIntegrationTest {
assertEquals("second", cloudFrontService.getPublicKey(publicKeyId).getComment());
assertEquals(keyGroupId, output(describe, "KeyGroupRef"));
assertEquals(functionArn, output(describe, "FunctionRef"), "a code change keeps the function");
- assertEquals("v2", cloudFrontService.describeFunction(functionName, null).getFunctionCode());
- assertEquals("v2", cloudFrontService.describeFunction(functionName, "LIVE").getFunctionCode());
+ assertFunctionCode(functionName, "DEVELOPMENT", "v2");
+ assertFunctionCode(functionName, "LIVE", "v2");
String renamed = functionName + "-renamed";
stackCall("UpdateStack", stackName, template(suffix, pem, "second", renamed, "v2"));
@@ -107,6 +107,16 @@ class CloudFormationCloudFrontKeysAndFunctionsIntegrationTest {
assertEquals("NoSuchFunctionExists", e.getErrorCode());
}
+ private void assertFunctionCode(String name, String stage, String expected) {
+ given()
+ .queryParam("Stage", stage)
+ .when()
+ .get("/2020-05-31/function/" + name)
+ .then()
+ .statusCode(200)
+ .body(equalTo(expected));
+ }
+
private static String rsaPublicKeyPem() throws Exception {
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(2048);
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CloudFrontCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CloudFrontCfnProvisionerTest.java
index b602acba30..0d5e437682 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CloudFrontCfnProvisionerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CloudFrontCfnProvisionerTest.java
@@ -20,7 +20,9 @@ import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
+import java.nio.charset.StandardCharsets;
import java.time.Instant;
+import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -96,6 +98,10 @@ class CloudFrontCfnProvisionerTest {
return mapper.readTree(json);
}
+ private static String wireCode(String code) {
+ return Base64.getEncoder().encodeToString(code.getBytes(StandardCharsets.UTF_8));
+ }
+
private static ResponseHeadersPolicy responseHeadersPolicy(String id, String etag) {
ResponseHeadersPolicy p = new ResponseHeadersPolicy();
p.setId(id);
@@ -831,7 +837,8 @@ class CloudFrontCfnProvisionerTest {
ArgumentCaptor