mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(appconfig): serve feature flag variants to the AppConfig Agent as Amazon Ion (#4674)
* feat(appconfig): serve feature flag variants to the AppConfig Agent as Amazon Ion The AWS AppConfig Agent evaluates multi-variant feature flags itself: it asks AppConfigData for application/ion;type=AWS.AppConfig.FeatureFlags and gets each flag's variants and rules back as Ion. Floci returned the stored JSON document unchanged, which the agent cannot read, so every variant flag failed with NoSuchFlag. A caller that accepts that type now gets the Ion stream AWS sends: a symbol table importing AWS's ops and anns tables, then a list per flag holding its variants (name, rule, attributes) and its default. Rules of the form (eq $attribute "value") are encoded; any other is left to the existing JSON path with a warning. The encoding reproduces a captured AWS response for a real 22-flag profile byte for byte. * fix(appconfig): honour q=0 for Ion, label the stored content type, read the stream back in tests An Accept of application/ion;type=AWS.AppConfig.FeatureFlags;q=0.0000 was treated as acceptable; any run of zeros now means the caller rejects it. The content type of a response was inferred from its first bytes. It now comes from the path that produced them, so a profile that is not a feature flag profile keeps its stored type even if it holds Ion and the caller accepts the feature flag type. The tests decode the stream with Amazon's ion-java (test scope), with the ops and anns shared tables registered, instead of checking the encoder against itself. * fix(appconfig): encode a basic flag in an Ion profile as an annotated string AWS sends a flag without variants in the Ion format as its retrieval-time JSON, annotated with its key, not as a list holding a default variant. The encoder now does the same, reusing the JSON path's per-flag conversion, so a profile that mixes variant and basic flags reads as AWS's. The encoder and docs cite the layout AWS documents for the agent's local development mode rather than the captured responses.
This commit is contained in:
1 parent
8d056e9456
commit
2316b2fc28
8 files changed
+673
-30
No files matched your search
+143
@@ -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);
|
||||
}
|
||||
}
|
||||
+129
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user