fix(cloudfront): return the decoded function code from GetFunction (#6798)

* fix(cloudfront): return the decoded function code from GetFunction

GetFunction wrote the stored base64 text into the response body where
AWS returns the code the request carried, so reading back what you sent
gave different bytes. The body is the decoded code now, with the
octet-stream content type and the ETag header unchanged; the
CloudFormation provisioner base64-encodes the template's plain code so
the stored form stays the wire form, and GetFunction is the single
decode point.

Closes #6749

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>

* fix(cloudfront): read a plain-code record persisted by an older Floci version

GetFunction served a 400 for a stored code that was not base64, and an
upgrade over persistent storage kept the records older Floci writes
wrote the CloudFormation provisioner as plain text, so those functions
stopped serving their code. A non-base64 record now reads back as its
stored bytes, and the next UpdateFunction rewrites it into the uniform
base64 form. The compat round-trip test captures the updated etag
before asserting so a failed assertion cannot hijack the cleanup's
delete.

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>

* test(cloudfront): read the ETag update header the model names and rebase the decoded-code management tests

The decoded-code round trip asserts UpdateFunction answers the version
in the ETag header the SDK models name, so it reads the value it was
extracted from instead of null. The compat round-trip test uses AssertJ
like the rest of the compatibility module.

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>

---------

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
Matheus Oliveira authored and GitHub committed 2026-10-30 14:57:30 -04:00
1 parent 3d455d02ad
commit 955a935bad
7 files changed
+270 -14

No files matched your search

@@ -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));
}
}
}
}
}