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 8877bc8083..ee8f3ef42a 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 @@ -7,8 +7,10 @@ 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.Application; 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.Environment; import io.github.hectorvent.floci.services.appconfig.model.HostedConfigurationVersion; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; @@ -41,9 +43,17 @@ public class AppConfigDataService { String envId = (String) request.get("EnvironmentIdentifier"); String profileId = (String) request.get("ConfigurationProfileIdentifier"); - // Validate resources exist - appConfigService.getEnvironment(appId, envId); - appConfigService.getConfigurationProfile(appId, profileId); + requireIdentifier(appId, "ApplicationIdentifier"); + requireIdentifier(envId, "EnvironmentIdentifier"); + requireIdentifier(profileId, "ConfigurationProfileIdentifier"); + + // Like AWS, each identifier is either the resource's ID or its name. + Application application = appConfigService.resolveApplication(appId); + Environment environment = appConfigService.resolveEnvironment(application.getId(), envId); + ConfigurationProfile profile = appConfigService.resolveConfigurationProfile(application.getId(), profileId); + appId = application.getId(); + envId = environment.getId(); + profileId = profile.getId(); ConfigurationSession session = new ConfigurationSession(); session.setId(UUID.randomUUID().toString()); @@ -74,6 +84,12 @@ public class AppConfigDataService { } } + private static void requireIdentifier(String value, String name) { + if (value == null || value.isBlank()) { + throw new AwsException("BadRequestException", name + " is required", 400); + } + } + private static AwsException invalidPollInterval() { return new AwsException("BadRequestException", "RequiredMinimumPollIntervalInSeconds must be an integer between 15 and 86400", 400); diff --git a/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigService.java b/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigService.java index dba040acfd..3e4f94df14 100644 --- a/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigService.java +++ b/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigService.java @@ -64,6 +64,13 @@ public class AppConfigService { return applicationStore.get(id).orElseThrow(() -> new AwsException("ResourceNotFoundException", "Application not found", 404)); } + /** The application whose ID or name is {@code idOrName}; the AppConfigData APIs accept either. */ + public Application resolveApplication(String idOrName) { + return applicationStore.get(idOrName) + .or(() -> listApplications().stream().filter(a -> idOrName.equals(a.getName())).findFirst()) + .orElseThrow(() -> new AwsException("ResourceNotFoundException", "Application not found", 404)); + } + public List listApplications() { return applicationStore.scan(k -> true); } @@ -92,6 +99,15 @@ public class AppConfigService { return env; } + /** The environment of {@code appId} whose ID or name is {@code idOrName}. */ + public Environment resolveEnvironment(String appId, String idOrName) { + // An exact ID always wins over another environment's name, and a direct lookup skips the scan. + return environmentStore.get(idOrName) + .filter(e -> appId.equals(e.getApplicationId())) + .or(() -> listEnvironments(appId).stream().filter(e -> idOrName.equals(e.getName())).findFirst()) + .orElseThrow(() -> new AwsException("ResourceNotFoundException", "Environment not found", 404)); + } + public List listEnvironments(String appId) { return environmentStore.scan(k -> true).stream() .filter(e -> e.getApplicationId().equals(appId)) @@ -119,6 +135,14 @@ public class AppConfigService { return profile; } + /** The configuration profile of {@code appId} whose ID or name is {@code idOrName}. */ + public ConfigurationProfile resolveConfigurationProfile(String appId, String idOrName) { + return profileStore.get(idOrName) + .filter(p -> appId.equals(p.getApplicationId())) + .or(() -> listConfigurationProfiles(appId).stream().filter(p -> idOrName.equals(p.getName())).findFirst()) + .orElseThrow(() -> new AwsException("ResourceNotFoundException", "Configuration profile not found", 404)); + } + public List listConfigurationProfiles(String appId) { return profileStore.scan(k -> true).stream() .filter(p -> p.getApplicationId().equals(appId)) 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 e97700b752..d05d5b5e1c 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 @@ -831,4 +831,114 @@ class AppConfigIntegrationTest { .body("disabled.enabled", equalTo(false)) .body("disabled.secret", nullValue()); } + + @Test @Order(44) + void startConfigurationSessionAcceptsResourceNames() { + String token = given() + .contentType(ContentType.JSON) + .body("{\"ApplicationIdentifier\": \"test-app\", \"EnvironmentIdentifier\": \"test-env\", " + + "\"ConfigurationProfileIdentifier\": \"test-profile\"}") + .when().post("/configurationsessions") + .then() + .statusCode(201) + .extract().path("InitialConfigurationToken"); + + given() + .queryParam("configuration_token", token) + .when().get("/configuration") + .then() + .statusCode(200) + .body("foo", notNullValue()); + } + + @Test @Order(45) + void startConfigurationSessionRejectsUnknownEnvironmentName() { + given() + .contentType(ContentType.JSON) + .body("{\"ApplicationIdentifier\": \"test-app\", \"EnvironmentIdentifier\": \"no-such-env\", " + + "\"ConfigurationProfileIdentifier\": \"test-profile\"}") + .when().post("/configurationsessions") + .then() + .statusCode(404) + .body("__type", equalTo("ResourceNotFoundException")); + } + + @Test @Order(46) + void startConfigurationSessionPrefersIdOverAnotherResourcesName() { + // A second environment and profile named after the first ones' IDs: the ID must still select the original. + given() + .contentType(ContentType.JSON) + .body("{\"Name\": \"" + envId + "\"}") + .when().post("/applications/" + appId + "/environments") + .then().statusCode(201); + given() + .contentType(ContentType.JSON) + .body("{\"Name\": \"" + profileId + "\", \"LocationUri\": \"hosted\", \"Type\": \"AWS.Freeform\"}") + .when().post("/applications/" + appId + "/configurationprofiles") + .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"); + + given() + .queryParam("configuration_token", token) + .when().get("/configuration") + .then() + .statusCode(200) + .body("foo", notNullValue()); + } + + @Test @Order(47) + void startConfigurationSessionSkipsResourcesWithoutAName() { + given() + .contentType(ContentType.JSON) + .body("{}") + .when().post("/applications/" + appId + "/environments") + .then().statusCode(201); + given() + .contentType(ContentType.JSON) + .body("{\"LocationUri\": \"hosted\", \"Type\": \"AWS.Freeform\"}") + .when().post("/applications/" + appId + "/configurationprofiles") + .then().statusCode(201); + given() + .contentType(ContentType.JSON) + .body("{}") + .when().post("/applications") + .then().statusCode(201); + + given() + .contentType(ContentType.JSON) + .body("{\"ApplicationIdentifier\": \"test-app\", \"EnvironmentIdentifier\": \"test-env\", " + + "\"ConfigurationProfileIdentifier\": \"test-profile\"}") + .when().post("/configurationsessions") + .then().statusCode(201); + given() + .contentType(ContentType.JSON) + .body("{\"ApplicationIdentifier\": \"test-app\", \"EnvironmentIdentifier\": \"no-such-env\", " + + "\"ConfigurationProfileIdentifier\": \"test-profile\"}") + .when().post("/configurationsessions") + .then().statusCode(404) + .body("__type", equalTo("ResourceNotFoundException")); + } + + @Test @Order(48) + void startConfigurationSessionWithoutAnIdentifierIsABadRequest() { + for (String missing : new String[] {"ApplicationIdentifier", "EnvironmentIdentifier", "ConfigurationProfileIdentifier"}) { + String body = "{\"ApplicationIdentifier\": \"test-app\", \"EnvironmentIdentifier\": \"test-env\", " + + "\"ConfigurationProfileIdentifier\": \"test-profile\"}"; + body = body.replaceFirst("\"" + missing + "\": \"[^\"]*\"(, )?", "").replaceFirst(", }$", "}"); + given() + .contentType(ContentType.JSON) + .body(body) + .when().post("/configurationsessions") + .then() + .statusCode(400) + .body("__type", equalTo("BadRequestException")); + } + } }