Light, fluffy, and always free - The AWS Local Emulator alternative
feat(appconfig): serve feature flag variants to the AppConfig Agent as Amazon Ion (#4674)
Goooooooooody2316b2fc28d6
diff --git a/docs/services/appconfig.md b/docs/services/appconfig.mdindex 5086067c3a..2b5edcb82f 100644--- a/docs/services/appconfig.md+++ b/docs/services/appconfig.md@@ -39,9 +39,16 @@ The data plane is used by applications to retrieve the active configuration for- `StartConfigurationSession`- `GetLatestConfiguration`-Basic `AWS.AppConfig.FeatureFlags` profiles are returned in AWS retrieval-time JSON format. Multi-variant-feature flags require context evaluation and Amazon Ion output, which Floci does not yet emulate. Floci-returns those configurations unchanged.+Basic `AWS.AppConfig.FeatureFlags` profiles are returned in AWS retrieval-time JSON format.++Feature flags with variants are returned, as AWS does, in Amazon Ion (`application/ion; type=AWS.AppConfig.FeatureFlags`)+to a caller whose `Accept` header asks for it, as the AWS AppConfig Agent's does: the caller evaluates the variants'+rules against its own context (`?flag=name&context=key=value`). The layout is the one AWS documents for the agent's+[local development mode](https://docs.aws.amazon.com/appconfig/latest/userguide/appconfig-agent-how-to-use-local-development-samples.html#appconfig-agent-how-to-use-local-development-samples-multi-variant):+each flag with variants is a list of its variants, and each basic flag in the same profile is its retrieval-time JSON+as a string. Rules of the form `(eq $attribute "value")` are+encoded; a profile with any other rule is returned unchanged, as JSON, and Floci logs a warning, as it does for a+caller that does not accept Ion.## Configurationdiff --git a/pom.xml b/pom.xmlindex 7832b9de00..2e026941c1 100644--- a/pom.xml+++ b/pom.xml@@ -346,6 +346,12 @@<version>4.3.0</version><scope>test</scope></dependency>+ <dependency>+ <groupId>com.amazon.ion</groupId>+ <artifactId>ion-java</artifactId>+ <version>1.12.1</version>+ <scope>test</scope>+ </dependency><dependency><groupId>io.fabric8</groupId><artifactId>kubernetes-server-mock</artifactId>diff --git a/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigDataController.java b/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigDataController.javaindex f534ccd3e8..233c897986 100644--- a/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigDataController.java+++ b/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigDataController.java@@ -42,8 +42,9 @@ public class AppConfigDataController {// without a token fall through to S3's /{bucket} handler.@GET@Path(AppConfigConfigurationRouteFilter.INTERNAL_PATH)- public Response getLatestConfiguration(@QueryParam("configuration_token") String token) {- ConfigurationData data = service.getLatestConfiguration(token);+ public Response getLatestConfiguration(@QueryParam("configuration_token") String token,+ @HeaderParam("Accept") String accept) {+ ConfigurationData data = service.getLatestConfiguration(token, accept);return Response.ok(data.content()).header("Content-Type", data.contentType()).header("Version-Label", data.configurationVersion())diff --git a/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigDataService.java b/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigDataService.javaindex ee8f3ef42a..a9c2b37600 100644--- a/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigDataService.java+++ b/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigDataService.java@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.appconfig;import com.fasterxml.jackson.core.type.TypeReference;import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.ObjectMapper;+import com.fasterxml.jackson.databind.node.JsonNodeFactory;import com.fasterxml.jackson.databind.node.ObjectNode;import io.github.hectorvent.floci.core.common.AwsException;import io.github.hectorvent.floci.core.storage.StorageBackend;@@ -96,6 +97,11 @@ public class AppConfigDataService {}public ConfigurationData getLatestConfiguration(String token) {+ return getLatestConfiguration(token, null);+ }++ /** {@code accept} is the caller's Accept header: the AppConfig Agent asks for Ion to evaluate variant flags itself. */+ public ConfigurationData getLatestConfiguration(String token, String accept) {ConfigurationSession session = sessionStore.get(token).orElseThrow(() -> new AwsException("BadRequestException", "Invalid configuration token", 400));@@ -120,21 +126,68 @@ public class AppConfigDataService {sessionStore.delete(token); // Old token is invalidsessionStore.put(nextToken, session);- byte[] content = (version != null) ? resolveContent(session, version) : new byte[0];- String contentType = (version != null) ? version.getContentType() : "application/octet-stream";+ byte[] content = new byte[0];+ String contentType = "application/octet-stream";+ if (version != null) {+ ResolvedContent resolved = resolveContent(session, version, accept);+ content = resolved.bytes();+ contentType = resolved.contentType();+ }String versionLabel = (version != null) ? String.valueOf(version.getVersionNumber()) : "";return new ConfigurationData(content, contentType, versionLabel, nextToken,pollInterval);}- private byte[] resolveContent(ConfigurationSession session, HostedConfigurationVersion version) {+ private ResolvedContent resolveContent(ConfigurationSession session, HostedConfigurationVersion version,+ String accept) {ConfigurationProfile profile = appConfigService.getConfigurationProfile(session.getApplicationId(), session.getConfigurationProfileId());if (!"AWS.AppConfig.FeatureFlags".equals(profile.getType())) {- return version.getContent();+ return new ResolvedContent(version.getContent(), version.getContentType());+ }+ // As AWS does: a caller that accepts Ion gets variant flags whole, to evaluate against its own context.+ if (acceptsFeatureFlagIon(accept) && FeatureFlagIonEncoder.hasVariants(version.getContent(), objectMapper)) {+ byte[] ion = FeatureFlagIonEncoder.encode(version.getContent(), objectMapper);+ if (ion != null) {+ return new ResolvedContent(ion, FeatureFlagIonEncoder.CONTENT_TYPE);+ }+ LOG.warnv("Feature flag profile {0} has variants Floci cannot encode as Ion; returning the stored JSON",+ profile.getId());+ }+ return new ResolvedContent(transformFeatureFlags(version.getContent(), objectMapper),+ version.getContentType());+ }++ static boolean acceptsFeatureFlagIon(String accept) {+ if (accept == null) {+ return false;+ }+ for (String range : accept.split(",")) {+ String[] parts = range.split(";");+ if (!"application/ion".equalsIgnoreCase(parts[0].trim())) {+ continue;+ }+ boolean featureFlags = false;+ boolean acceptable = true;+ for (int i = 1; i < parts.length; i++) {+ String[] parameter = parts[i].trim().split("=", 2);+ if (parameter.length != 2) {+ continue;+ }+ String name = parameter[0].trim();+ String value = parameter[1].trim();+ if (name.equalsIgnoreCase("type")) {+ featureFlags = "AWS.AppConfig.FeatureFlags".equalsIgnoreCase(value);+ } else if (name.equalsIgnoreCase("q")) {+ acceptable = !value.matches("0(\\.0+)?");+ }+ }+ if (featureFlags && acceptable) {+ return true;+ }}- return transformFeatureFlags(version.getContent(), objectMapper);+ return false;}static byte[] transformFeatureFlags(byte[] content, ObjectMapper objectMapper) {@@ -155,28 +208,12 @@ public class AppConfigDataService {Iterator<Map.Entry<String, JsonNode>> fields = values.fields();while (fields.hasNext()) {Map.Entry<String, JsonNode> entry = fields.next();- JsonNode definition = entry.getValue();- if (!definition.isObject()) {+ // Multi-variant feature flags require context evaluation and Amazon Ion output, which only a+ // caller that accepts Ion gets; preserve the stored bytes for any other.+ ObjectNode flag = retrievalFlag(entry.getValue());+ if (flag == null) {return content;}- // Multi-variant feature flags require context evaluation and Amazon Ion output.- // Preserve the stored bytes until that retrieval path is implemented.- if (definition.has("_variants")) {- return content;- }- JsonNode enabled = definition.get("enabled");- if (enabled == null || !enabled.isBoolean()) {- return content;- }-- if (!enabled.booleanValue()) {- retrieval.putObject(entry.getKey()).put("enabled", false);- continue;- }-- ObjectNode flag = definition.deepCopy();- flag.remove("_createdAt");- flag.remove("_updatedAt");retrieval.set(entry.getKey(), flag);}return objectMapper.writeValueAsBytes(retrieval);@@ -186,10 +223,33 @@ public class AppConfigDataService {}}+ /**+ * A basic flag's value in the retrieval-time format: only {@code enabled} if it is off, else its attributes too.+ * {@code null} for a flag with variants or one that is not a valid basic flag.+ */+ static ObjectNode retrievalFlag(JsonNode definition) {+ if (!definition.isObject() || definition.has("_variants")) {+ return null;+ }+ JsonNode enabled = definition.get("enabled");+ if (enabled == null || !enabled.isBoolean()) {+ return null;+ }+ if (!enabled.booleanValue()) {+ return JsonNodeFactory.instance.objectNode().put("enabled", false);+ }+ ObjectNode flag = definition.deepCopy();+ flag.remove("_createdAt");+ flag.remove("_updatedAt");+ return flag;+ }+static int normalizePollInterval(int interval) {return interval >= 15 && interval <= 86400 ? interval : 15;}+ private record ResolvedContent(byte[] bytes, String contentType) {}+public record ConfigurationData(byte[] content, String contentType, String configurationVersion,String nextPollConfigurationToken, int nextPollIntervalInSeconds) {}}diff --git a/src/main/java/io/github/hectorvent/floci/services/appconfig/FeatureFlagIonEncoder.java b/src/main/java/io/github/hectorvent/floci/services/appconfig/FeatureFlagIonEncoder.javanew file mode 100644index 0000000000..740f60daa4--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/appconfig/FeatureFlagIonEncoder.java@@ -0,0 +1,252 @@+package io.github.hectorvent.floci.services.appconfig;++import com.fasterxml.jackson.databind.JsonNode;+import com.fasterxml.jackson.databind.ObjectMapper;+import com.fasterxml.jackson.databind.node.ObjectNode;++import java.io.ByteArrayOutputStream;+import java.io.IOException;+import java.nio.charset.StandardCharsets;+import java.util.ArrayList;+import java.util.Iterator;+import java.util.LinkedHashMap;+import java.util.List;+import java.util.Map;+import java.util.regex.Matcher;+import java.util.regex.Pattern;++/**+ * Encodes an {@code AWS.AppConfig.FeatureFlags} document as the Amazon Ion AWS AppConfigData returns to a caller that+ * accepts {@code application/ion;type=AWS.AppConfig.FeatureFlags}, as the AppConfig Agent does. Unlike the JSON+ * retrieval format it keeps each flag's variants and rules, for the caller to evaluate against its own context.+ *+ * <p>The layout is the one documented for the agent's local development mode+ * (https://docs.aws.amazon.com/appconfig/latest/userguide/appconfig-agent-how-to-use-local-development-samples.html):+ * each flag is a value annotated with its key. A basic flag is its retrieval-time JSON as a string; a flag with+ * variants is a list of its variants, each a list holding its rule as an s-expression and its content as a JSON+ * string, then its default's content alone. The binary stream, like AWS's, also has a local symbol table importing+ * AWS's shared {@code ops} and {@code anns} tables and annotates each variant with its name.+ * Only {@code (eq $attribute "value")} rules are encoded; {@link #encode} returns {@code null} for any other.+ */+final class FeatureFlagIonEncoder {++ static final String CONTENT_TYPE = "application/ion; type=AWS.AppConfig.FeatureFlags";++ private static final int SYSTEM_SYMBOLS = 9;+ private static final int OPS_MAX_ID = 23;+ private static final int ANNS_MAX_ID = 12;+ private static final int FIRST_LOCAL_SID = SYSTEM_SYMBOLS + OPS_MAX_ID + ANNS_MAX_ID + 1;+ /** The fourth symbol of {@code ops}, which follows the system symbols. */+ private static final int OPS_EQ_SID = SYSTEM_SYMBOLS + 4;++ private static final Pattern EQ_RULE =+ Pattern.compile("^\\s*\\(\\s*eq\\s+\\$([A-Za-z_][A-Za-z0-9_]*)\\s+\"((?:[^\"\\\\]|\\\\.)*)\"\\s*\\)\\s*$");++ private final Map<String, Integer> symbols = new LinkedHashMap<>();++ private FeatureFlagIonEncoder() {}++ /** Whether any flag in the document has variants, which is when AWS answers in Ion rather than JSON. */+ static boolean hasVariants(byte[] content, ObjectMapper objectMapper) {+ try {+ JsonNode values = objectMapper.readTree(content).get("values");+ if (values == null || !values.isObject()) {+ return false;+ }+ for (JsonNode flag : values) {+ JsonNode variants = flag.get("_variants");+ if (variants != null && variants.isArray() && !variants.isEmpty()) {+ return true;+ }+ }+ } catch (IOException e) {+ return false;+ }+ return false;+ }++ /** The Ion stream for {@code content}, or {@code null} if it is not a feature-flag document this can encode. */+ static byte[] encode(byte[] content, ObjectMapper objectMapper) {+ try {+ JsonNode document = objectMapper.readTree(content);+ JsonNode values = document == null ? null : document.get("values");+ if (values == null || !values.isObject()) {+ return null;+ }+ FeatureFlagIonEncoder encoder = new FeatureFlagIonEncoder();+ List<byte[]> flags = new ArrayList<>();+ Iterator<Map.Entry<String, JsonNode>> fields = values.fields();+ while (fields.hasNext()) {+ Map.Entry<String, JsonNode> flag = fields.next();+ byte[] encoded = encoder.flag(flag.getKey(), flag.getValue(), objectMapper);+ if (encoded == null) {+ return null;+ }+ flags.add(encoded);+ }+ ByteArrayOutputStream out = new ByteArrayOutputStream();+ out.write(new byte[] {(byte) 0xE0, 0x01, 0x00, (byte) 0xEA});+ out.write(encoder.symbolTable());+ for (byte[] flag : flags) {+ out.write(flag);+ }+ return out.toByteArray();+ } catch (IOException e) {+ return null;+ }+ }++ private byte[] flag(String key, JsonNode flag, ObjectMapper objectMapper) throws IOException {+ if (!flag.isObject()) {+ return null;+ }+ int keySid = intern(key);+ JsonNode variants = flag.get("_variants");+ if (variants == null || !variants.isArray() || variants.isEmpty()) {+ // A basic flag is its value in the JSON retrieval format, as a string.+ ObjectNode retrieval = AppConfigDataService.retrievalFlag(flag);+ return retrieval == null ? null : annotated(keySid, string(objectMapper.writeValueAsString(retrieval)));+ }+ ByteArrayOutputStream items = new ByteArrayOutputStream();+ String defaultJson = null;+ for (JsonNode variant : variants) {+ if (!variant.path("enabled").isBoolean()) {+ return null;+ }+ if (variant.hasNonNull("rule")) {+ byte[] encoded = variant(variant, objectMapper);+ if (encoded == null) {+ return null;+ }+ items.write(encoded);+ } else {+ defaultJson = variantJson(variant.path("name").asText("default"), variant.get("enabled"),+ variant.get("attributeValues"), objectMapper);+ }+ }+ if (defaultJson == null) {+ return null;+ }+ items.write(string(defaultJson));+ return annotated(keySid, container(0xB0, items.toByteArray()));+ }++ private byte[] variant(JsonNode variant, ObjectMapper objectMapper) throws IOException {+ String name = variant.path("name").asText(null);+ Matcher rule = EQ_RULE.matcher(variant.path("rule").asText(""));+ if (name == null || !rule.matches()) {+ return null;+ }+ int nameSid = intern(name);+ int attributeSid = intern("$" + rule.group(1));+ ByteArrayOutputStream sexp = new ByteArrayOutputStream();+ sexp.write(symbol(OPS_EQ_SID));+ sexp.write(symbol(attributeSid));+ sexp.write(string(objectMapper.readValue("\"" + rule.group(2) + "\"", String.class)));++ ByteArrayOutputStream items = new ByteArrayOutputStream();+ items.write(container(0xC0, sexp.toByteArray()));+ items.write(string(variantJson(name, variant.get("enabled"), variant.get("attributeValues"), objectMapper)));+ return annotated(nameSid, container(0xB0, items.toByteArray()));+ }++ /** A variant's attributes as the JSON string AWS puts in the stream: its name, whether it is on and, if it is, its attribute values. */+ private static String variantJson(String name, JsonNode enabled, JsonNode attributeValues, ObjectMapper objectMapper)+ throws IOException {+ ObjectNode json = objectMapper.createObjectNode();+ json.put("_variant", name);+ json.set("enabled", enabled);+ // A variant that is off carries no attribute values, as in the JSON retrieval format.+ if (enabled.booleanValue() && attributeValues != null && attributeValues.isObject()) {+ attributeValues.fields().forEachRemaining(a -> json.set(a.getKey(), a.getValue()));+ }+ return objectMapper.writeValueAsString(json);+ }++ private int intern(String text) {+ return symbols.computeIfAbsent(text, t -> FIRST_LOCAL_SID + symbols.size());+ }++ private byte[] symbolTable() throws IOException {+ ByteArrayOutputStream imports = new ByteArrayOutputStream();+ imports.write(importOf("ops", OPS_MAX_ID));+ imports.write(importOf("anns", ANNS_MAX_ID));+ ByteArrayOutputStream local = new ByteArrayOutputStream();+ for (String symbol : symbols.keySet()) {+ local.write(string(symbol));+ }+ ByteArrayOutputStream table = new ByteArrayOutputStream();+ table.write(varUInt(6));+ table.write(container(0xB0, imports.toByteArray()));+ table.write(varUInt(7));+ table.write(container(0xB0, local.toByteArray()));+ return annotated(3, container(0xD0, table.toByteArray()));+ }++ private static byte[] importOf(String name, int maxId) throws IOException {+ ByteArrayOutputStream fields = new ByteArrayOutputStream();+ fields.write(varUInt(4));+ fields.write(string(name));+ fields.write(varUInt(5));+ fields.write(uint(1));+ fields.write(varUInt(8));+ fields.write(uint(maxId));+ return container(0xD0, fields.toByteArray());+ }++ private static byte[] annotated(int annotationSid, byte[] value) throws IOException {+ byte[] annotation = varUInt(annotationSid);+ ByteArrayOutputStream body = new ByteArrayOutputStream();+ body.write(varUInt(annotation.length));+ body.write(annotation);+ body.write(value);+ return container(0xE0, body.toByteArray());+ }++ private static byte[] string(String text) throws IOException {+ return container(0x80, text.getBytes(StandardCharsets.UTF_8));+ }++ private static byte[] symbol(int sid) throws IOException {+ return container(0x70, unsigned(sid));+ }++ private static byte[] uint(int value) throws IOException {+ return container(0x20, unsigned(value));+ }++ /** The length goes in the low nibble, or after the type byte from 14 up. */+ private static byte[] container(int typeNibble, byte[] body) throws IOException {+ ByteArrayOutputStream out = new ByteArrayOutputStream();+ if (body.length < 14) {+ out.write(typeNibble | body.length);+ } else {+ out.write(typeNibble | 0x0E);+ out.write(varUInt(body.length));+ }+ out.write(body);+ return out.toByteArray();+ }++ private static byte[] unsigned(int value) {+ int length = value < 0x100 ? 1 : value < 0x10000 ? 2 : value < 0x1000000 ? 3 : 4;+ byte[] out = new byte[length];+ for (int i = 0; i < length; i++) {+ out[length - 1 - i] = (byte) (value >>> (8 * i));+ }+ return out;+ }++ private static byte[] varUInt(int value) {+ int groups = 1;+ while ((value >>> (7 * groups)) != 0) {+ groups++;+ }+ byte[] out = new byte[groups];+ for (int i = 0; i < groups; i++) {+ out[i] = (byte) ((value >>> (7 * (groups - 1 - i))) & 0x7F);+ }+ out[groups - 1] |= (byte) 0x80;+ return out;+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigIntegrationTest.javaindex d05d5b5e1c..c44e918752 100644--- a/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigIntegrationTest.java@@ -14,6 +14,8 @@ import java.nio.charset.StandardCharsets;import static io.restassured.RestAssured.given;import static org.hamcrest.Matchers.*;+import static org.junit.jupiter.api.Assertions.assertArrayEquals;+import static org.junit.jupiter.api.Assertions.assertEquals;@QuarkusTest@TestMethodOrder(MethodOrderer.OrderAnnotation.class)@@ -941,4 +943,145 @@ class AppConfigIntegrationTest {.body("__type", equalTo("BadRequestException"));}}++ @Test @Order(49)+ void getLatestConfigurationReturnsVariantFlagsAsIonToACallerThatAcceptsIt() {+ String variantAppId = given()+ .contentType(ContentType.JSON)+ .body("{\"Name\":\"variant-flags-app\"}")+ .when().post("/applications")+ .then().statusCode(201)+ .extract().path("Id");++ String variantEnvId = given()+ .contentType(ContentType.JSON)+ .body("{\"Name\":\"test\"}")+ .when().post("/applications/" + variantAppId + "/environments")+ .then().statusCode(201)+ .extract().path("Id");++ String variantProfileId = given()+ .contentType(ContentType.JSON)+ .body("{\"Name\":\"flags\",\"LocationUri\":\"hosted\","+ + "\"Type\":\"AWS.AppConfig.FeatureFlags\"}")+ .when().post("/applications/" + variantAppId + "/configurationprofiles")+ .then().statusCode(201)+ .extract().path("Id");++ String content = "{\"flags\":{},\"version\":\"1\",\"values\":{\"promo\":{\"_variants\":["+ + "{\"attributeValues\":{\"storeId\":\"uk\"},\"enabled\":true,\"name\":\"uk\","+ + "\"rule\":\"(eq $storeId \\\"uk\\\")\"},"+ + "{\"enabled\":false,\"name\":\"default\"}]},"+ + "\"banner\":{\"enabled\":true,\"colour\":\"red\"}}}";++ given()+ .header("Content-Type", "application/json")+ .body(content.getBytes(StandardCharsets.UTF_8))+ .when().post("/applications/" + variantAppId + "/configurationprofiles/"+ + variantProfileId + "/hostedconfigurationversions")+ .then().statusCode(201).header("Version-Number", equalTo("1"));++ given()+ .contentType(ContentType.JSON)+ .body("{\"ConfigurationProfileId\":\"" + variantProfileId + "\","+ + "\"ConfigurationVersion\":\"1\","+ + "\"DeploymentStrategyId\":\"AppConfig.AllAtOnce\"}")+ .when().post("/applications/" + variantAppId + "/environments/" + variantEnvId + "/deployments")+ .then().statusCode(201);++ String sessionBody = "{\"ApplicationIdentifier\":\"" + variantAppId + "\","+ + "\"EnvironmentIdentifier\":\"" + variantEnvId + "\","+ + "\"ConfigurationProfileIdentifier\":\"" + variantProfileId + "\"}";++ String agentToken = given()+ .contentType(ContentType.JSON).body(sessionBody)+ .when().post("/configurationsessions")+ .then().statusCode(201)+ .extract().path("InitialConfigurationToken");++ byte[] ion = given()+ .header("Accept", "application/ion;type=AWS.AppConfig.FeatureFlags;q=1.0,*/*;q=0.1")+ .queryParam("configuration_token", agentToken)+ .when().get("/configuration")+ .then().statusCode(200)+ .header("Content-Type", startsWith("application/ion"))+ .header("Content-Type", containsString("type=AWS.AppConfig.FeatureFlags"))+ .header("Version-Label", equalTo("1"))+ .extract().asByteArray();+ assertEquals(IonTestSupport.parse("promo::[uk::[(eq $storeId \"uk\"), "+ + "'''{\"_variant\":\"uk\",\"enabled\":true,\"storeId\":\"uk\"}'''],"+ + "'''{\"_variant\":\"default\",\"enabled\":false}'''] "+ + "banner::'''{\"enabled\":true,\"colour\":\"red\"}'''"),+ IonTestSupport.decode(ion));++ // Without Accept for Ion, the stored document, as before.+ String plainToken = given()+ .contentType(ContentType.JSON).body(sessionBody)+ .when().post("/configurationsessions")+ .then().statusCode(201)+ .extract().path("InitialConfigurationToken");++ given()+ .queryParam("configuration_token", plainToken)+ .when().get("/configuration")+ .then().statusCode(200)+ .header("Content-Type", startsWith("application/json"))+ .body("values.promo._variants[0].name", equalTo("uk"));+ }++ @Test @Order(50)+ void aFreeformProfileKeepsItsOwnContentTypeWhateverTheCallerAccepts() {+ String appId = given()+ .contentType(ContentType.JSON)+ .body("{\"Name\":\"binary-app\"}")+ .when().post("/applications")+ .then().statusCode(201)+ .extract().path("Id");++ String envId = given()+ .contentType(ContentType.JSON)+ .body("{\"Name\":\"test\"}")+ .when().post("/applications/" + appId + "/environments")+ .then().statusCode(201)+ .extract().path("Id");++ String profileId = given()+ .contentType(ContentType.JSON)+ .body("{\"Name\":\"blob\",\"LocationUri\":\"hosted\",\"Type\":\"AWS.Freeform\"}")+ .when().post("/applications/" + appId + "/configurationprofiles")+ .then().statusCode(201)+ .extract().path("Id");++ byte[] stored = {(byte) 0xE0, 0x01, 0x00, (byte) 0xEA};+ given()+ .header("Content-Type", "application/octet-stream")+ .body(stored)+ .when().post("/applications/" + appId + "/configurationprofiles/" + profileId+ + "/hostedconfigurationversions")+ .then().statusCode(201);++ given()+ .contentType(ContentType.JSON)+ .body("{\"ConfigurationProfileId\":\"" + profileId + "\",\"ConfigurationVersion\":\"1\","+ + "\"DeploymentStrategyId\":\"AppConfig.AllAtOnce\"}")+ .when().post("/applications/" + appId + "/environments/" + envId + "/deployments")+ .then().statusCode(201);++ String token = given()+ .contentType(ContentType.JSON)+ .body("{\"ApplicationIdentifier\":\"" + appId + "\",\"EnvironmentIdentifier\":\"" + envId + "\","+ + "\"ConfigurationProfileIdentifier\":\"" + profileId + "\"}")+ .when().post("/configurationsessions")+ .then().statusCode(201)+ .extract().path("InitialConfigurationToken");++ byte[] body = given()+ .header("Accept", "application/ion;type=AWS.AppConfig.FeatureFlags;q=1.0,*/*;q=0.1")+ .queryParam("configuration_token", token)+ .when().get("/configuration")+ .then().statusCode(200)+ .header("Content-Type", startsWith("application/octet-stream"))+ .extract().asByteArray();+ assertArrayEquals(stored, body);+ }}diff --git a/src/test/java/io/github/hectorvent/floci/services/appconfig/FeatureFlagIonEncoderTest.java b/src/test/java/io/github/hectorvent/floci/services/appconfig/FeatureFlagIonEncoderTest.javanew file mode 100644index 0000000000..866f8f9767--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/appconfig/FeatureFlagIonEncoderTest.java@@ -0,0 +1,129 @@+package io.github.hectorvent.floci.services.appconfig;++import com.fasterxml.jackson.databind.ObjectMapper;+import org.junit.jupiter.api.Test;++import java.nio.charset.StandardCharsets;+import java.util.HexFormat;++import static org.junit.jupiter.api.Assertions.assertArrayEquals;+import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertFalse;+import static org.junit.jupiter.api.Assertions.assertNull;+import static org.junit.jupiter.api.Assertions.assertTrue;++class FeatureFlagIonEncoderTest {++ private static final ObjectMapper MAPPER = new ObjectMapper();++ /** {@code promo} has two rules and a default of off; {@code plain} is a variant flag with only a default. */+ private static final String VARIANT_FLAGS = "{\"flags\":{},\"version\":\"1\",\"values\":{"+ + "\"promo\":{\"_variants\":["+ + "{\"attributeValues\":{\"storeId\":\"uk\"},\"enabled\":true,\"name\":\"uk\",\"rule\":\"(eq $storeId \\\"uk\\\")\"},"+ + "{\"attributeValues\":{\"storeId\":\"ie\"},\"enabled\":false,\"name\":\"ie\",\"rule\":\"(eq $storeId \\\"ie\\\")\"},"+ + "{\"enabled\":false,\"name\":\"default\"}]},"+ + "\"plain\":{\"_variants\":[{\"enabled\":true,\"name\":\"default\"}]}}}";++ /**+ * The stream AWS AppConfigData sends for {@link #VARIANT_FLAGS}, in the layout of a captured AWS response: the+ * version marker, a symbol table importing AWS's {@code ops} and {@code anns} tables, then {@code promo} and+ * {@code plain}. Decoded, it reads+ * <pre>+ * promo::[uk::[(eq $storeId "uk"), "{\"_variant\":\"uk\",\"enabled\":true,\"storeId\":\"uk\"}"],+ * ie::[(eq $storeId "ie"), "{\"_variant\":\"ie\",\"enabled\":false}"],+ * "{\"_variant\":\"default\",\"enabled\":false}"]+ * plain::["{\"_variant\":\"default\",\"enabled\":true}"]+ * </pre>+ * where {@code eq} is symbol 13 (the {@code ops} table's fourth) and the rest are local symbols from 45.+ */+ private static final String EXPECTED_ION = "e00100ea"+ + "eebe8183deba86be99db84836f7073852101882117dc8484616e6e7385210188210c"+ + "87be9b8570726f6d6f82756b882473746f7265496482696585706c61696e"+ + "ee019d81adbe0198eebd81aebeb9c7710d712f82756b8eaf7b225f76617269616e74223a22756b222c22656e61626c6564223a747275652c2273746f72654964223a22756b227d"+ + "eeaf81b0beabc7710d712f8269658ea17b225f76617269616e74223a226965222c22656e61626c6564223a66616c73657d"+ + "8ea67b225f76617269616e74223a2264656661756c74222c22656e61626c6564223a66616c73657d"+ + "eeab81b1bea78ea57b225f76617269616e74223a2264656661756c74222c22656e61626c6564223a747275657d";++ private static final String EXPECTED_TEXT = "promo::["+ + "uk::[(eq $storeId \"uk\"), '''{\"_variant\":\"uk\",\"enabled\":true,\"storeId\":\"uk\"}'''],"+ + "ie::[(eq $storeId \"ie\"), '''{\"_variant\":\"ie\",\"enabled\":false}'''],"+ + "'''{\"_variant\":\"default\",\"enabled\":false}'''] "+ + "plain::['''{\"_variant\":\"default\",\"enabled\":true}''']";++ @Test+ void encodesVariantFlagsAsAwsDoes() {+ byte[] ion = FeatureFlagIonEncoder.encode(VARIANT_FLAGS.getBytes(StandardCharsets.UTF_8), MAPPER);++ assertArrayEquals(HexFormat.of().parseHex(EXPECTED_ION), ion);+ }++ @Test+ void readsBackAsIonWithTheSharedTablesImported() {+ byte[] ion = FeatureFlagIonEncoder.encode(VARIANT_FLAGS.getBytes(StandardCharsets.UTF_8), MAPPER);++ assertEquals(IonTestSupport.parse(EXPECTED_TEXT), IonTestSupport.decode(ion));+ }++ @Test+ void detectsVariants() {+ assertTrue(FeatureFlagIonEncoder.hasVariants(VARIANT_FLAGS.getBytes(StandardCharsets.UTF_8), MAPPER));+ assertFalse(FeatureFlagIonEncoder.hasVariants(+ "{\"values\":{\"basic\":{\"enabled\":true}}}".getBytes(StandardCharsets.UTF_8), MAPPER));+ assertFalse(FeatureFlagIonEncoder.hasVariants(+ "{\"values\":{\"variant\":{\"_variants\":[]}}}".getBytes(StandardCharsets.UTF_8), MAPPER));+ assertFalse(FeatureFlagIonEncoder.hasVariants("not json".getBytes(StandardCharsets.UTF_8), MAPPER));+ }++ @Test+ void encodesABasicFlagAsAStringOfItsRetrievalValue() {+ String mixed = "{\"values\":{"+ + "\"promo\":{\"_variants\":["+ + "{\"enabled\":true,\"name\":\"uk\",\"rule\":\"(eq $storeId \\\"uk\\\")\"},"+ + "{\"enabled\":false,\"name\":\"default\"}]},"+ + "\"basic\":{\"enabled\":true,\"limit\":5,\"_createdAt\":\"2026-01-01T00:00:00Z\"},"+ + "\"off\":{\"enabled\":false,\"limit\":5}}}";++ byte[] ion = FeatureFlagIonEncoder.encode(mixed.getBytes(StandardCharsets.UTF_8), MAPPER);++ assertEquals(IonTestSupport.parse("promo::["+ + "uk::[(eq $storeId \"uk\"), '''{\"_variant\":\"uk\",\"enabled\":true}'''],"+ + "'''{\"_variant\":\"default\",\"enabled\":false}'''] "+ + "basic::'''{\"enabled\":true,\"limit\":5}''' "+ + "off::'''{\"enabled\":false}'''"),+ IonTestSupport.decode(ion));+ }++ @Test+ void leavesRulesItCannotEncodeToTheJsonPath() {+ String other = "{\"values\":{\"f\":{\"_variants\":["+ + "{\"enabled\":true,\"name\":\"a\",\"rule\":\"(and (eq $storeId \\\"uk\\\") (eq $tier \\\"gold\\\"))\"},"+ + "{\"enabled\":false,\"name\":\"default\"}]}}}";++ assertNull(FeatureFlagIonEncoder.encode(other.getBytes(StandardCharsets.UTF_8), MAPPER));+ }++ @Test+ void refusesADocumentWithoutADefaultVariant() {+ String noDefault = "{\"values\":{\"f\":{\"_variants\":["+ + "{\"enabled\":true,\"name\":\"a\",\"rule\":\"(eq $storeId \\\"uk\\\")\"}]}}}";++ assertNull(FeatureFlagIonEncoder.encode(noDefault.getBytes(StandardCharsets.UTF_8), MAPPER));+ assertNull(FeatureFlagIonEncoder.encode("[]".getBytes(StandardCharsets.UTF_8), MAPPER));+ }++ @Test+ void recognisesTheAgentsAcceptHeader() {+ assertTrue(AppConfigDataService.acceptsFeatureFlagIon(+ "application/ion;type=AWS.AppConfig.FeatureFlags;q=1.0,*/*;q=0.1"));+ assertTrue(AppConfigDataService.acceptsFeatureFlagIon("application/ion; type=AWS.AppConfig.FeatureFlags"));+ assertTrue(AppConfigDataService.acceptsFeatureFlagIon("text/plain, Application/ION;Type=aws.appconfig.featureflags"));+ assertFalse(AppConfigDataService.acceptsFeatureFlagIon("application/ion;type=AWS.AppConfig.FeatureFlags;q=0"));+ assertFalse(AppConfigDataService.acceptsFeatureFlagIon("application/ion;type=AWS.AppConfig.FeatureFlags;q=0.0000"));+ assertTrue(AppConfigDataService.acceptsFeatureFlagIon("application/ion;type=AWS.AppConfig.FeatureFlags;q=0.5"));+ assertFalse(AppConfigDataService.acceptsFeatureFlagIon("application/ion;type=Other"));+ assertFalse(AppConfigDataService.acceptsFeatureFlagIon("application/ion"));+ assertFalse(AppConfigDataService.acceptsFeatureFlagIon("application/json"));+ assertFalse(AppConfigDataService.acceptsFeatureFlagIon("*/*"));+ assertFalse(AppConfigDataService.acceptsFeatureFlagIon(null));+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/appconfig/IonTestSupport.java b/src/test/java/io/github/hectorvent/floci/services/appconfig/IonTestSupport.javanew file mode 100644index 0000000000..7a99e6e60c--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/appconfig/IonTestSupport.java@@ -0,0 +1,45 @@+package io.github.hectorvent.floci.services.appconfig;++import com.amazon.ion.IonDatagram;+import com.amazon.ion.IonSystem;+import com.amazon.ion.SymbolTable;+import com.amazon.ion.system.IonSystemBuilder;+import com.amazon.ion.system.SimpleCatalog;++import java.util.List;+import java.util.stream.IntStream;++/**+ * Reads Ion with Amazon's own library, so the tests do not check the hand-written encoder against itself. AWS's shared+ * {@code ops} and {@code anns} tables are registered with placeholder names except {@code eq}, the operator the encoder+ * emits, so the stream's imports and symbol ids resolve exactly as in a real consumer.+ */+final class IonTestSupport {++ private static final IonSystem SYSTEM = system();++ private IonTestSupport() {}++ static IonDatagram decode(byte[] ion) {+ return SYSTEM.getLoader().load(ion);+ }++ static IonDatagram parse(String text) {+ return SYSTEM.getLoader().load(text);+ }++ private static IonSystem system() {+ IonSystem plain = IonSystemBuilder.standard().build();+ SimpleCatalog catalog = new SimpleCatalog();+ catalog.putTable(sharedTable(plain, "ops", 23, 4, "eq"));+ catalog.putTable(sharedTable(plain, "anns", 12, 0, null));+ return IonSystemBuilder.standard().withCatalog(catalog).build();+ }++ private static SymbolTable sharedTable(IonSystem ion, String name, int size, int namedPosition, String named) {+ List<String> symbols = IntStream.rangeClosed(1, size)+ .mapToObj(i -> i == namedPosition ? named : name + i)+ .toList();+ return ion.newSharedSymbolTable(name, 1, symbols.iterator());+ }+}