mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(appconfig): return feature flags in retrieval format (#3890)
Co-authored-by: xujiantop-crypto <265865031+xujiantop-crypto@users.noreply.github.com>
This commit is contained in:
1 parent
11d04276a3
commit
1370dc02fa
5 files changed
+317
-2
No files matched your search
@@ -9,6 +9,7 @@ import software.amazon.awssdk.services.appconfigdata.model.BadRequestException;
|
||||
import software.amazon.awssdk.services.appconfigdata.model.GetLatestConfigurationRequest;
|
||||
import software.amazon.awssdk.services.appconfigdata.model.GetLatestConfigurationResponse;
|
||||
import software.amazon.awssdk.services.appconfigdata.model.StartConfigurationSessionRequest;
|
||||
import software.amazon.awssdk.services.appconfigdata.model.StartConfigurationSessionResponse;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
@@ -473,4 +474,73 @@ class AppConfigTest {
|
||||
.deploymentStrategyId("AppConfig.AllAtOnce").build()).id())
|
||||
.isEqualTo("AppConfig.AllAtOnce");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(55)
|
||||
@DisplayName("GetLatestConfiguration resolves basic Feature Flags to retrieval-time format")
|
||||
void getLatestConfigurationReturnsFeatureFlagsRetrievalFormat() {
|
||||
String featureFlagsAppId = appConfig.createApplication(CreateApplicationRequest.builder()
|
||||
.name(TestFixtures.uniqueName("feature-flags-app"))
|
||||
.build()).id();
|
||||
String featureFlagsEnvId = appConfig.createEnvironment(CreateEnvironmentRequest.builder()
|
||||
.applicationId(featureFlagsAppId)
|
||||
.name("test")
|
||||
.build()).id();
|
||||
String featureFlagsProfileId = appConfig.createConfigurationProfile(
|
||||
CreateConfigurationProfileRequest.builder()
|
||||
.applicationId(featureFlagsAppId)
|
||||
.name("flags")
|
||||
.locationUri("hosted")
|
||||
.type("AWS.AppConfig.FeatureFlags")
|
||||
.build()).id();
|
||||
String deploymentContent = "{\"flags\":{\"enabled\":{\"name\":\"enabled\"},"
|
||||
+ "\"disabled\":{\"name\":\"disabled\"}},\"values\":{"
|
||||
+ "\"enabled\":{\"enabled\":true,\"number\":0,\"beta\":false,"
|
||||
+ "\"_createdAt\":\"created\",\"_updatedAt\":\"updated\"},"
|
||||
+ "\"disabled\":{\"enabled\":false,\"secret\":\"must-not-leak\"}},"
|
||||
+ "\"version\":\"1\"}";
|
||||
|
||||
CreateHostedConfigurationVersionResponse version = appConfig.createHostedConfigurationVersion(
|
||||
CreateHostedConfigurationVersionRequest.builder()
|
||||
.applicationId(featureFlagsAppId)
|
||||
.configurationProfileId(featureFlagsProfileId)
|
||||
.content(SdkBytes.fromString(deploymentContent, StandardCharsets.UTF_8))
|
||||
.contentType("application/json")
|
||||
.build());
|
||||
assertThat(version.versionNumber()).isEqualTo(1);
|
||||
|
||||
GetHostedConfigurationVersionResponse stored = appConfig.getHostedConfigurationVersion(
|
||||
GetHostedConfigurationVersionRequest.builder()
|
||||
.applicationId(featureFlagsAppId)
|
||||
.configurationProfileId(featureFlagsProfileId)
|
||||
.versionNumber(1)
|
||||
.build());
|
||||
assertThat(stored.content().asString(StandardCharsets.UTF_8)).isEqualTo(deploymentContent);
|
||||
|
||||
appConfig.startDeployment(StartDeploymentRequest.builder()
|
||||
.applicationId(featureFlagsAppId)
|
||||
.environmentId(featureFlagsEnvId)
|
||||
.configurationProfileId(featureFlagsProfileId)
|
||||
.configurationVersion("1")
|
||||
.deploymentStrategyId("AppConfig.AllAtOnce")
|
||||
.build());
|
||||
|
||||
StartConfigurationSessionResponse session = appConfigData.startConfigurationSession(
|
||||
StartConfigurationSessionRequest.builder()
|
||||
.applicationIdentifier(featureFlagsAppId)
|
||||
.environmentIdentifier(featureFlagsEnvId)
|
||||
.configurationProfileIdentifier(featureFlagsProfileId)
|
||||
.build());
|
||||
GetLatestConfigurationResponse response = appConfigData.getLatestConfiguration(
|
||||
GetLatestConfigurationRequest.builder()
|
||||
.configurationToken(session.initialConfigurationToken())
|
||||
.build());
|
||||
|
||||
assertThat(response.configuration().asString(StandardCharsets.UTF_8))
|
||||
.isEqualTo("{\"enabled\":{\"enabled\":true,\"number\":0,\"beta\":false},"
|
||||
+ "\"disabled\":{\"enabled\":false}}");
|
||||
assertThat(response.contentType()).startsWith("application/json");
|
||||
assertThat(response.versionLabel()).isEqualTo("1");
|
||||
assertThat(response.nextPollConfigurationToken()).isNotBlank();
|
||||
}
|
||||
}
|
||||
@@ -39,6 +39,10 @@ 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.
|
||||
|
||||
## Configuration
|
||||
|
||||
| Variable | Default | Description |
|
||||
|
||||
+69
-2
@@ -1,16 +1,22 @@
|
||||
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.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.appconfig.model.ConfigurationProfile;
|
||||
import io.github.hectorvent.floci.services.appconfig.model.ConfigurationSession;
|
||||
import io.github.hectorvent.floci.services.appconfig.model.HostedConfigurationVersion;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
@@ -20,11 +26,14 @@ public class AppConfigDataService {
|
||||
|
||||
private final StorageBackend<String, ConfigurationSession> sessionStore;
|
||||
private final AppConfigService appConfigService;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
@Inject
|
||||
public AppConfigDataService(StorageFactory storageFactory, AppConfigService appConfigService) {
|
||||
public AppConfigDataService(StorageFactory storageFactory, AppConfigService appConfigService,
|
||||
ObjectMapper objectMapper) {
|
||||
this.sessionStore = storageFactory.create("appconfigdata", "appconfigdata-sessions.json", new TypeReference<>() {});
|
||||
this.appConfigService = appConfigService;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
public String startConfigurationSession(Map<String, Object> request) {
|
||||
@@ -95,7 +104,7 @@ public class AppConfigDataService {
|
||||
sessionStore.delete(token); // Old token is invalid
|
||||
sessionStore.put(nextToken, session);
|
||||
|
||||
byte[] content = (version != null) ? version.getContent() : new byte[0];
|
||||
byte[] content = (version != null) ? resolveContent(session, version) : new byte[0];
|
||||
String contentType = (version != null) ? version.getContentType() : "application/octet-stream";
|
||||
String versionLabel = (version != null) ? String.valueOf(version.getVersionNumber()) : "";
|
||||
|
||||
@@ -103,6 +112,64 @@ public class AppConfigDataService {
|
||||
pollInterval);
|
||||
}
|
||||
|
||||
private byte[] resolveContent(ConfigurationSession session, HostedConfigurationVersion version) {
|
||||
ConfigurationProfile profile = appConfigService.getConfigurationProfile(
|
||||
session.getApplicationId(), session.getConfigurationProfileId());
|
||||
if (!"AWS.AppConfig.FeatureFlags".equals(profile.getType())) {
|
||||
return version.getContent();
|
||||
}
|
||||
return transformFeatureFlags(version.getContent(), objectMapper);
|
||||
}
|
||||
|
||||
static byte[] transformFeatureFlags(byte[] content, ObjectMapper objectMapper) {
|
||||
if (content == null || content.length == 0) {
|
||||
return content;
|
||||
}
|
||||
try {
|
||||
JsonNode document = objectMapper.readTree(content);
|
||||
if (document == null || !document.isObject()) {
|
||||
return content;
|
||||
}
|
||||
JsonNode values = document.get("values");
|
||||
if (values == null || !values.isObject()) {
|
||||
return content;
|
||||
}
|
||||
|
||||
ObjectNode retrieval = objectMapper.createObjectNode();
|
||||
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()) {
|
||||
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);
|
||||
} catch (IOException e) {
|
||||
LOG.debugv(e, "Could not convert AppConfig feature flags to retrieval format");
|
||||
return content;
|
||||
}
|
||||
}
|
||||
|
||||
static int normalizePollInterval(int interval) {
|
||||
return interval >= 15 && interval <= 86400 ? interval : 15;
|
||||
}
|
||||
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
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.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
class AppConfigDataServiceTest {
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
@Test
|
||||
void transformFeatureFlagsProducesRetrievalTimeValues() throws Exception {
|
||||
byte[] deploymentContent = """
|
||||
{
|
||||
"flags": {
|
||||
"enabled": {"name": "enabled"},
|
||||
"disabled": {"name": "disabled"}
|
||||
},
|
||||
"values": {
|
||||
"enabled": {
|
||||
"enabled": true,
|
||||
"number": 0,
|
||||
"beta": false,
|
||||
"empty": "",
|
||||
"choices": [],
|
||||
"details": {"tier": "gold"},
|
||||
"_createdAt": "created",
|
||||
"_updatedAt": "updated"
|
||||
},
|
||||
"disabled": {"enabled": false, "secret": "must-not-leak"}
|
||||
},
|
||||
"version": "1"
|
||||
}
|
||||
""".getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
byte[] retrievalContent = AppConfigDataService.transformFeatureFlags(deploymentContent, MAPPER);
|
||||
|
||||
assertEquals(MAPPER.readTree("""
|
||||
{
|
||||
"enabled": {
|
||||
"enabled": true,
|
||||
"number": 0,
|
||||
"beta": false,
|
||||
"empty": "",
|
||||
"choices": [],
|
||||
"details": {"tier": "gold"}
|
||||
},
|
||||
"disabled": {"enabled": false}
|
||||
}
|
||||
"""), MAPPER.readTree(retrievalContent));
|
||||
}
|
||||
|
||||
@Test
|
||||
void transformFeatureFlagsPreservesWholeMultiVariantDocument() {
|
||||
byte[] content = """
|
||||
{"values":{"basic":{"enabled":true},"variant":{"enabled":true,"_variants":[]}}}
|
||||
""".getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
assertArrayEquals(content, AppConfigDataService.transformFeatureFlags(content, MAPPER));
|
||||
}
|
||||
|
||||
@Test
|
||||
void transformFeatureFlagsPreservesInvalidDocuments() {
|
||||
List<String> documents = List.of(
|
||||
"{not-json",
|
||||
"[]",
|
||||
"{}",
|
||||
"{\"values\":[]}",
|
||||
"{\"values\":{\"flag\":true}}",
|
||||
"{\"values\":{\"flag\":{}}}",
|
||||
"{\"values\":{\"flag\":{\"enabled\":\"true\"}}}");
|
||||
|
||||
for (String document : documents) {
|
||||
byte[] content = document.getBytes(StandardCharsets.UTF_8);
|
||||
assertArrayEquals(content, AppConfigDataService.transformFeatureFlags(content, MAPPER));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void transformFeatureFlagsPreservesNullAndEmptyContent() {
|
||||
assertNull(AppConfigDataService.transformFeatureFlags(null, MAPPER));
|
||||
assertArrayEquals(new byte[0], AppConfigDataService.transformFeatureFlags(new byte[0], MAPPER));
|
||||
}
|
||||
}
|
||||
+85
@@ -10,6 +10,8 @@ import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
@@ -746,4 +748,87 @@ class AppConfigIntegrationTest {
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test @Order(43)
|
||||
void getLatestConfigurationReturnsFeatureFlagsRetrievalFormat() {
|
||||
String featureFlagsAppId = given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"Name\":\"feature-flags-app\"}")
|
||||
.when().post("/applications")
|
||||
.then().statusCode(201)
|
||||
.extract().path("Id");
|
||||
|
||||
String featureFlagsEnvId = given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"Name\":\"test\"}")
|
||||
.when().post("/applications/" + featureFlagsAppId + "/environments")
|
||||
.then().statusCode(201)
|
||||
.extract().path("Id");
|
||||
|
||||
String featureFlagsProfileId = given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"Name\":\"flags\",\"LocationUri\":\"hosted\","
|
||||
+ "\"Type\":\"AWS.AppConfig.FeatureFlags\"}")
|
||||
.when().post("/applications/" + featureFlagsAppId + "/configurationprofiles")
|
||||
.then().statusCode(201)
|
||||
.extract().path("Id");
|
||||
|
||||
String content = "{\"flags\":{\"enabled\":{\"name\":\"enabled\"},"
|
||||
+ "\"disabled\":{\"name\":\"disabled\"}},\"values\":{"
|
||||
+ "\"enabled\":{\"enabled\":true,\"number\":0,\"beta\":false,"
|
||||
+ "\"_createdAt\":\"created\",\"_updatedAt\":\"updated\"},"
|
||||
+ "\"disabled\":{\"enabled\":false,\"secret\":\"must-not-leak\"}},"
|
||||
+ "\"version\":\"1\"}";
|
||||
|
||||
given()
|
||||
.header("Content-Type", "application/json")
|
||||
.body(content.getBytes(StandardCharsets.UTF_8))
|
||||
.when().post("/applications/" + featureFlagsAppId + "/configurationprofiles/"
|
||||
+ featureFlagsProfileId + "/hostedconfigurationversions")
|
||||
.then().statusCode(201).header("Version-Number", equalTo("1"));
|
||||
|
||||
given()
|
||||
.when().get("/applications/" + featureFlagsAppId + "/configurationprofiles/"
|
||||
+ featureFlagsProfileId + "/hostedconfigurationversions/1")
|
||||
.then().statusCode(200)
|
||||
.body("flags.enabled.name", equalTo("enabled"))
|
||||
.body("values.enabled.enabled", equalTo(true))
|
||||
.body("version", equalTo("1"));
|
||||
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"ConfigurationProfileId\":\"" + featureFlagsProfileId + "\","
|
||||
+ "\"ConfigurationVersion\":\"1\","
|
||||
+ "\"DeploymentStrategyId\":\"AppConfig.AllAtOnce\"}")
|
||||
.when().post("/applications/" + featureFlagsAppId + "/environments/"
|
||||
+ featureFlagsEnvId + "/deployments")
|
||||
.then().statusCode(201);
|
||||
|
||||
String featureFlagsToken = given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"ApplicationIdentifier\":\"" + featureFlagsAppId + "\","
|
||||
+ "\"EnvironmentIdentifier\":\"" + featureFlagsEnvId + "\","
|
||||
+ "\"ConfigurationProfileIdentifier\":\"" + featureFlagsProfileId + "\"}")
|
||||
.when().post("/configurationsessions")
|
||||
.then().statusCode(201)
|
||||
.extract().path("InitialConfigurationToken");
|
||||
|
||||
given()
|
||||
.queryParam("configuration_token", featureFlagsToken)
|
||||
.when().get("/configuration")
|
||||
.then().statusCode(200)
|
||||
.header("Content-Type", startsWith("application/json"))
|
||||
.header("Version-Label", equalTo("1"))
|
||||
.header("Next-Poll-Configuration-Token", notNullValue())
|
||||
.body("flags", nullValue())
|
||||
.body("values", nullValue())
|
||||
.body("version", nullValue())
|
||||
.body("enabled.enabled", equalTo(true))
|
||||
.body("enabled.number", equalTo(0))
|
||||
.body("enabled.beta", equalTo(false))
|
||||
.body("enabled._createdAt", nullValue())
|
||||
.body("enabled._updatedAt", nullValue())
|
||||
.body("disabled.enabled", equalTo(false))
|
||||
.body("disabled.secret", nullValue());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user