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.java index 3cc9aa2f2f..828181806f 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 @@ -225,7 +225,7 @@ public class AppConfigDataService { String name = parameter[0].trim(); String value = parameter[1].trim(); if (name.equalsIgnoreCase("type")) { - featureFlags = "AWS.AppConfig.FeatureFlags".equalsIgnoreCase(value); + featureFlags = "AWS.AppConfig.FeatureFlags".equalsIgnoreCase(parameterValue(value)); } else if (name.equalsIgnoreCase("q")) { acceptable = !value.matches("0(\\.0+)?"); } @@ -237,6 +237,33 @@ public class AppConfigDataService { return false; } + /** + * A media-type parameter value as RFC 9110 section 5.6.6 defines it: a token as is, or a quoted-string without its + * DQUOTEs and with each quoted-pair read as the character after its backslash (section 5.6.4). {@code null} for a + * quoted-string with no closing DQUOTE or with anything after it. + */ + private static String parameterValue(String value) { + if (!value.startsWith("\"")) { + return value; + } + StringBuilder unquoted = new StringBuilder(); + boolean escaped = false; + for (int i = 1; i < value.length(); i++) { + char c = value.charAt(i); + if (escaped) { + unquoted.append(c); + escaped = false; + } else if (c == '\\') { + escaped = true; + } else if (c == '"') { + return i == value.length() - 1 ? unquoted.toString() : null; + } else { + unquoted.append(c); + } + } + return null; + } + static byte[] transformFeatureFlags(byte[] content, ObjectMapper objectMapper) { if (content == null || content.length == 0) { return content; 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.java index 860d1fd788..bfa5fdab10 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 @@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.appconfig; import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; import io.quarkus.test.junit.QuarkusTest; import io.restassured.http.ContentType; +import io.restassured.response.ValidatableResponse; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.MethodOrderer; @@ -34,6 +35,7 @@ class AppConfigIntegrationTest { private static String emptyProfileId; private static String deploymentNextToken; private static String secondDeploymentNextToken; + private static String variantFlagsSessionBody; @BeforeAll static void setup() { @@ -1121,4 +1123,93 @@ class AppConfigIntegrationTest { .when().get("/configuration") .then().statusCode(404); } + + @Test @Order(52) + void getLatestConfigurationServesIonForAQuotedFeatureFlagsType() { + getLatestVariantFlagsAccepting("application/ion; type=\"AWS.AppConfig.FeatureFlags\", */*;q=0.1") + .header("Content-Type", startsWith("application/ion")) + .header("Content-Type", containsString("type=AWS.AppConfig.FeatureFlags")); + } + + @Test @Order(53) + void getLatestConfigurationServesIonForAnUnquotedFeatureFlagsType() { + getLatestVariantFlagsAccepting("application/ion; type=AWS.AppConfig.FeatureFlags, */*;q=0.1") + .header("Content-Type", startsWith("application/ion")) + .header("Content-Type", containsString("type=AWS.AppConfig.FeatureFlags")); + } + + @Test @Order(54) + void getLatestConfigurationServesIonForAQuotedFeatureFlagsTypeWithAQuotedPair() { + getLatestVariantFlagsAccepting("application/ion; type=\"AWS.AppConfig.Feature\\Flags\", */*;q=0.1") + .header("Content-Type", startsWith("application/ion")) + .header("Content-Type", containsString("type=AWS.AppConfig.FeatureFlags")); + } + + @Test @Order(55) + void getLatestConfigurationServesStoredJsonForAQuotedOtherType() { + getLatestVariantFlagsAccepting("application/ion; type=\"AWS.AppConfig.Other\", */*;q=0.1") + .header("Content-Type", startsWith("application/json")) + .body("values.promo._variants[0].name", equalTo("uk")); + } + + /** Polls a new session on a feature flag profile with variants, deployed on first use. */ + private static ValidatableResponse getLatestVariantFlagsAccepting(String accept) { + if (variantFlagsSessionBody == null) { + variantFlagsSessionBody = deployVariantFlags(); + } + String token = given() + .contentType(ContentType.JSON).body(variantFlagsSessionBody) + .when().post("/configurationsessions") + .then().statusCode(201) + .extract().path("InitialConfigurationToken"); + + return given() + .header("Accept", accept) + .queryParam("configuration_token", token) + .when().get("/configuration") + .then().statusCode(200) + .header("Version-Label", equalTo("1")); + } + + /** Deploys a feature flag profile whose one flag has variants; returns the StartConfigurationSession body. */ + private static String deployVariantFlags() { + String app = given() + .contentType(ContentType.JSON) + .body("{\"Name\":\"quoted-ion-type-app\"}") + .when().post("/applications") + .then().statusCode(201) + .extract().path("Id"); + String env = given() + .contentType(ContentType.JSON) + .body("{\"Name\":\"test\"}") + .when().post("/applications/" + app + "/environments") + .then().statusCode(201) + .extract().path("Id"); + String profile = given() + .contentType(ContentType.JSON) + .body("{\"Name\":\"flags\",\"LocationUri\":\"hosted\",\"Type\":\"AWS.AppConfig.FeatureFlags\"}") + .when().post("/applications/" + app + "/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\"}]}}}"; + given() + .header("Content-Type", "application/json") + .body(content.getBytes(StandardCharsets.UTF_8)) + .when().post("/applications/" + app + "/configurationprofiles/" + profile + + "/hostedconfigurationversions") + .then().statusCode(201); + given() + .contentType(ContentType.JSON) + .body("{\"ConfigurationProfileId\":\"" + profile + "\",\"ConfigurationVersion\":\"1\"," + + "\"DeploymentStrategyId\":\"AppConfig.AllAtOnce\"}") + .when().post("/applications/" + app + "/environments/" + env + "/deployments") + .then().statusCode(201); + + return "{\"ApplicationIdentifier\":\"" + app + "\",\"EnvironmentIdentifier\":\"" + env + "\"," + + "\"ConfigurationProfileIdentifier\":\"" + profile + "\"}"; + } } diff --git a/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigLegacyIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigLegacyIntegrationTest.java index 1fed057595..465c67d5a5 100644 --- a/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigLegacyIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigLegacyIntegrationTest.java @@ -6,6 +6,8 @@ import io.restassured.http.ContentType; import io.restassured.response.Response; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import java.net.URI; import java.net.http.HttpClient; @@ -228,8 +230,10 @@ class AppConfigLegacyIntegrationTest { .body("values", nullValue()); } - @Test - void variantFeatureFlagsAreIonForACallerThatAcceptsOnlyIon() { + @ParameterizedTest + @ValueSource(strings = {"application/ion; type=AWS.AppConfig.FeatureFlags", + "application/ion; type=\"AWS.AppConfig.FeatureFlags\""}) + void variantFeatureFlagsAreIonForACallerThatAcceptsOnlyIon(String accept) { String flags = createProfile(Map.of("Name", "variant-flags", "LocationUri", "hosted", "Type", "AWS.AppConfig.FeatureFlags")); createHostedVersion(flags, "{\"flags\":{},\"version\":\"1\",\"values\":{\"promo\":{\"_variants\":[" @@ -237,7 +241,7 @@ class AppConfigLegacyIntegrationTest { + "\"rule\":\"(eq $storeId \\\"uk\\\")\"},{\"enabled\":false,\"name\":\"default\"}]}}}"); deploy(environment, flags, "1"); - byte[] ion = given().accept("application/ion; type=AWS.AppConfig.FeatureFlags") + byte[] ion = given().accept(accept) .queryParam("client_id", "reader").get(path(environment, flags)) .then().statusCode(200) .header("Configuration-Version", "1") 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.java index 866f8f9767..4966c48ce2 100644 --- a/src/test/java/io/github/hectorvent/floci/services/appconfig/FeatureFlagIonEncoderTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/appconfig/FeatureFlagIonEncoderTest.java @@ -126,4 +126,30 @@ class FeatureFlagIonEncoderTest { assertFalse(AppConfigDataService.acceptsFeatureFlagIon("*/*")); assertFalse(AppConfigDataService.acceptsFeatureFlagIon(null)); } + + /** RFC 9110 section 5.6.6: a parameter value is a token or a quoted-string, and both forms are the same value. */ + @Test + void readsAQuotedTypeAsTheSameValueAsAToken() { + assertTrue(AppConfigDataService.acceptsFeatureFlagIon("application/ion; type=AWS.AppConfig.FeatureFlags, */*;q=0.1")); + assertTrue(AppConfigDataService.acceptsFeatureFlagIon( + "application/ion; type=\"AWS.AppConfig.FeatureFlags\", */*;q=0.1")); + assertTrue(AppConfigDataService.acceptsFeatureFlagIon("Application/ION;TYPE=\"aws.appconfig.featureflags\";q=1.0")); + assertFalse(AppConfigDataService.acceptsFeatureFlagIon("application/ion; type=\"AWS.AppConfig.FeatureFlags\";q=0")); + } + + /** RFC 9110 section 5.6.4: a quoted-pair stands for the character after its backslash. */ + @Test + void readsAQuotedPairInATypeAsTheCharacterItEscapes() { + assertTrue(AppConfigDataService.acceptsFeatureFlagIon("application/ion; type=\"AWS.AppConfig.Feature\\Flags\"")); + assertTrue(AppConfigDataService.acceptsFeatureFlagIon("application/ion; type=\"AWS\\.AppConfig\\.FeatureFlags\"")); + assertFalse(AppConfigDataService.acceptsFeatureFlagIon("application/ion; type=\"AWS.AppConfig.FeatureFlags\\\"\"")); + } + + @Test + void rejectsAQuotedTypeThatIsMalformedOrDifferent() { + assertFalse(AppConfigDataService.acceptsFeatureFlagIon("application/ion; type=\"AWS.AppConfig.Other\"")); + assertFalse(AppConfigDataService.acceptsFeatureFlagIon("application/ion; type=\" AWS.AppConfig.FeatureFlags\"")); + assertFalse(AppConfigDataService.acceptsFeatureFlagIon("application/ion; type=\"AWS.AppConfig.FeatureFlags")); + assertFalse(AppConfigDataService.acceptsFeatureFlagIon("application/ion; type=\"AWS.AppConfig.FeatureFlags\"x")); + } }