mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -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
+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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user