mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(appconfig): resolve AppConfigData identifiers by name as well as ID (#4661)
* fix(appconfig): resolve AppConfigData identifiers by name as well as ID StartConfigurationSession looked the application, environment and profile up by ID only. AWS accepts either, and the AppConfig agent and Lambda extension are normally given names, so they got ResourceNotFoundException against Floci. Resolve each identifier by ID first, then by name. * fix(appconfig): prefer an exact ID over a name and skip nameless resources Resolve each AppConfigData identifier with a direct ID lookup first (kept within the parent application), so an ID is never shadowed by another resource's name and legacy unprefixed keys stay reachable; only a miss scans by name, and the name match is null-safe. * fix(appconfig): answer a missing session identifier with ResourceNotFoundException An omitted or null Application, Environment or ConfigurationProfile identifier reached the name fallback and threw a NullPointerException. It now returns ResourceNotFoundException, as the direct lookups did. * style(appconfig): brace the null-identifier guards * fix(appconfig): reject a missing session identifier with BadRequestException
This commit is contained in:
1 parent
e6d22e8bcf
commit
90c55114f9
3 files changed
+153
-3
No files matched your search
+19
-3
@@ -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);
|
||||
|
||||
@@ -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<Application> 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<Environment> 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<ConfigurationProfile> listConfigurationProfiles(String appId) {
|
||||
return profileStore.scan(k -> true).stream()
|
||||
.filter(p -> p.getApplicationId().equals(appId))
|
||||
|
||||
+110
@@ -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"));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user