fix(appconfig): keep environment monitors and report deployment sources (#6665)

CreateEnvironment dropped the Monitors it was sent, and StartDeployment
and GetDeployment never reported the deployed profile's name or
location. AWS documents all three on these operations.

- CreateEnvironment stores Monitors and environment reads return them.
  They are checked against AWS's limits (at most five; AlarmArn of 1 to
  2048 characters; AlarmRoleArn an IAM role ARN in any partition) and
  refused with BadRequestException before anything is stored. An
  environment created without monitors returns no Monitors member, as
  AWS's own example shows.
- StartDeployment records the profile's ConfigurationName and
  ConfigurationLocationUri, and GetDeployment returns them.
- Records stored by earlier builds load unchanged: the new members are
  omitted when absent, so their responses stay byte-identical.

A configuration profile created without Type still reports none: AWS
documents no default, and its CreateConfigurationProfile example returns
a null Type.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>
This commit is contained in:
Jackson Price authored and GitHub committed 2026-10-26 14:24:27 -04:00
1 parent 4506645d0f
commit b280a7764a
9 files changed
+521 -2

No files matched your search

@@ -0,0 +1,171 @@
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 org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
@QuarkusTest
class AppConfigResourceFieldsIntegrationTest {
private static final String ALARM_ARN = "arn:aws:cloudwatch:us-east-1:000000000000:alarm:configuration-errors";
private static final String SECOND_ALARM_ARN = "arn:aws:cloudwatch:us-east-1:000000000000:alarm:configuration-latency";
private static final String ALARM_ROLE_ARN = "arn:aws:iam::000000000000:role/appconfig-alarm-monitor";
@BeforeAll
static void setup() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@Test
void environmentReturnsTheMonitorsItWasCreatedWith() {
String applicationId = createApplication("monitored-app");
String environmentId = given()
.contentType(ContentType.JSON)
.body("{\"Name\":\"monitored\",\"Monitors\":["
+ "{\"AlarmArn\":\"" + ALARM_ARN + "\",\"AlarmRoleArn\":\"" + ALARM_ROLE_ARN + "\"},"
+ "{\"AlarmArn\":\"" + SECOND_ALARM_ARN + "\"}]}")
.when().post("/applications/" + applicationId + "/environments")
.then().statusCode(201)
.body("Monitors.AlarmArn", contains(ALARM_ARN, SECOND_ALARM_ARN))
.body("Monitors[0].AlarmRoleArn", equalTo(ALARM_ROLE_ARN))
.body("Monitors[1]", not(hasKey("AlarmRoleArn")))
.extract().path("Id");
given()
.when().get("/applications/" + applicationId + "/environments/" + environmentId)
.then().statusCode(200)
.body("Monitors", equalTo(List.of(
Map.of("AlarmArn", ALARM_ARN, "AlarmRoleArn", ALARM_ROLE_ARN),
Map.of("AlarmArn", SECOND_ALARM_ARN))));
given()
.when().get("/applications/" + applicationId + "/environments")
.then().statusCode(200)
.body("Items.find { it.Id == '" + environmentId + "' }.Monitors.AlarmArn",
contains(ALARM_ARN, SECOND_ALARM_ARN));
}
@Test
void environmentCreatedWithoutMonitorsReturnsNone() {
String applicationId = createApplication("unmonitored-app");
String environmentId = given()
.contentType(ContentType.JSON)
.body("{\"Name\":\"unmonitored\"}")
.when().post("/applications/" + applicationId + "/environments")
.then().statusCode(201)
.body("$", not(hasKey("Monitors")))
.extract().path("Id");
given()
.when().get("/applications/" + applicationId + "/environments/" + environmentId)
.then().statusCode(200)
.body("$", not(hasKey("Monitors")));
}
@Test
void invalidMonitorsAreRefusedAndNoEnvironmentIsCreated() {
String applicationId = createApplication("refused-monitors-app");
String monitor = "{\"AlarmArn\":\"" + ALARM_ARN + "\"}";
List<String> invalid = List.of(
"\"not-a-list\"",
"[" + String.join(",", Collections.nCopies(6, monitor)) + "]",
"[\"not-a-monitor\"]",
"[null]",
"[{}]",
"[{\"AlarmArn\":\"\"}]",
"[{\"AlarmArn\":1}]",
"[{\"AlarmArn\":\"" + "a".repeat(2049) + "\"}]",
"[{\"AlarmArn\":\"" + ALARM_ARN + "\",\"AlarmRoleArn\":1}]",
"[{\"AlarmArn\":\"" + ALARM_ARN + "\",\"AlarmRoleArn\":\"appconfig-alarm-monitor\"}]",
"[{\"AlarmArn\":\"" + ALARM_ARN + "\",\"AlarmRoleArn\":\"arn:aws:iam::000000000000:user/monitor\"}]",
"[{\"AlarmArn\":\"" + ALARM_ARN + "\",\"AlarmRoleArn\":\"" + ALARM_ROLE_ARN + "x".repeat(2048) + "\"}]");
for (String monitors : invalid) {
given()
.contentType(ContentType.JSON)
.body("{\"Name\":\"refused\",\"Monitors\":" + monitors + "}")
.when().post("/applications/" + applicationId + "/environments")
.then().statusCode(400)
.body("__type", equalTo("BadRequestException"));
}
given()
.when().get("/applications/" + applicationId + "/environments")
.then().statusCode(200)
.body("Items", empty());
}
@Test
void deploymentReturnsTheDeployedProfileNameAndLocation() {
String applicationId = createApplication("deployment-source-app");
String environmentId = given()
.contentType(ContentType.JSON)
.body("{\"Name\":\"deployment-source-env\"}")
.when().post("/applications/" + applicationId + "/environments")
.then().statusCode(201)
.extract().path("Id");
String profileId = given()
.contentType(ContentType.JSON)
.body("{\"Name\":\"service-settings\",\"LocationUri\":\"ssm-parameter://service-settings\","
+ "\"Type\":\"AWS.Freeform\"}")
.when().post("/applications/" + applicationId + "/configurationprofiles")
.then().statusCode(201)
.extract().path("Id");
String deployments = "/applications/" + applicationId + "/environments/" + environmentId + "/deployments";
int deploymentNumber = given()
.contentType(ContentType.JSON)
.body("{\"ConfigurationProfileId\":\"" + profileId + "\",\"ConfigurationVersion\":\"1\","
+ "\"DeploymentStrategyId\":\"AppConfig.AllAtOnce\"}")
.when().post(deployments)
.then().statusCode(201)
.body("ConfigurationName", equalTo("service-settings"))
.body("ConfigurationLocationUri", equalTo("ssm-parameter://service-settings"))
.extract().path("DeploymentNumber");
given()
.when().get(deployments + "/" + deploymentNumber)
.then().statusCode(200)
.body("ConfigurationName", equalTo("service-settings"))
.body("ConfigurationLocationUri", equalTo("ssm-parameter://service-settings"));
}
@Test
void profileCreatedWithoutTypeReturnsNoType() {
String applicationId = createApplication("untyped-profile-app");
String profileId = given()
.contentType(ContentType.JSON)
.body("{\"Name\":\"untyped\",\"LocationUri\":\"hosted\"}")
.when().post("/applications/" + applicationId + "/configurationprofiles")
.then().statusCode(201)
.body("Type", nullValue())
.extract().path("Id");
given()
.when().get("/applications/" + applicationId + "/configurationprofiles/" + profileId)
.then().statusCode(200)
.body("Type", nullValue());
}
private static String createApplication(String name) {
return given()
.contentType(ContentType.JSON)
.body("{\"Name\":\"" + name + "\"}")
.when().post("/applications")
.then().statusCode(201)
.extract().path("Id");
}
}
@@ -0,0 +1,109 @@
package io.github.hectorvent.floci.services.appconfig;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.ServiceConfigAccess;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.appconfig.model.Deployment;
import io.github.hectorvent.floci.services.appconfig.model.Environment;
import io.github.hectorvent.floci.services.appconfig.model.Monitor;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class AppConfigServicePersistenceTest {
private static final String ALARM_ARN = "arn:aws:cloudwatch:us-east-1:000000000000:alarm:configuration-errors";
private static final String SECOND_ALARM_ARN = "arn:aws:cloudwatch:us-east-1:000000000000:alarm:configuration-latency";
private static final String ALARM_ROLE_ARN = "arn:aws:iam::000000000000:role/appconfig-alarm-monitor";
private static final ObjectMapper MAPPER = new ObjectMapper();
@TempDir
Path dir;
@Test
void stateStoredWithoutMonitorsOrDeploymentLocationsStillLoads() throws IOException {
// Written the way earlier builds persisted these records: no Monitors, no ConfigurationLocationUri.
Files.writeString(dir.resolve("appconfig-applications.json"), """
{"000000000000/app1": {"Id": "app1", "Name": "stored-app", "Description": null, "tags": {}}}
""");
Files.writeString(dir.resolve("appconfig-environments.json"), """
{"000000000000/env1": {"Id": "env1", "ApplicationId": "app1", "Name": "stored-env",
"Description": null, "State": "READY"}}
""");
Files.writeString(dir.resolve("appconfig-profiles.json"), """
{"000000000000/prof1": {"Id": "prof1", "ApplicationId": "app1", "Name": "stored-profile",
"Description": null, "LocationUri": "hosted", "Type": null}}
""");
Files.writeString(dir.resolve("appconfig-deployments.json"), """
{"000000000000/app1::env1::1": {"ApplicationId": "app1", "EnvironmentId": "env1",
"ConfigurationProfileId": "prof1", "DeploymentNumber": 1, "ConfigurationName": null,
"ConfigurationVersion": "1", "DeploymentStrategyId": "AppConfig.AllAtOnce",
"State": "COMPLETE", "Description": null}}
""");
AppConfigService service = new AppConfigService(newFactory(), mock(EmulatorConfig.class));
Environment environment = service.getEnvironment("app1", "env1");
assertEquals("stored-env", environment.getName());
assertNull(environment.getMonitors());
assertFalse(MAPPER.valueToTree(environment).has("Monitors"));
Deployment deployment = service.getDeployment("app1", "env1", 1);
assertEquals("prof1", deployment.getConfigurationProfileId());
assertEquals("COMPLETE", deployment.getState());
assertNull(deployment.getConfigurationLocationUri());
assertFalse(MAPPER.valueToTree(deployment).has("ConfigurationLocationUri"));
assertNull(service.getConfigurationProfile("app1", "prof1").getType());
}
@Test
void monitorsAndDeploymentLocationsSurviveARestart() {
StorageFactory first = newFactory();
AppConfigService service = new AppConfigService(first, mock(EmulatorConfig.class));
String appId = service.createApplication(Map.of("Name", "restarted-app")).getId();
String monitoredId = service.createEnvironment(appId, Map.of("Name", "monitored", "Monitors", List.of(
Map.of("AlarmArn", ALARM_ARN, "AlarmRoleArn", ALARM_ROLE_ARN),
Map.of("AlarmArn", SECOND_ALARM_ARN)))).getId();
String unmonitoredId = service.createEnvironment(appId, Map.of("Name", "unmonitored")).getId();
String profileId = service.createConfigurationProfile(appId, Map.of(
"Name", "service-settings", "LocationUri", "ssm-parameter://service-settings")).getId();
int deploymentNumber = service.startDeployment(appId, monitoredId, Map.of(
"ConfigurationProfileId", profileId,
"ConfigurationVersion", "1",
"DeploymentStrategyId", "AppConfig.AllAtOnce")).getDeploymentNumber();
first.shutdownAll();
AppConfigService restarted = new AppConfigService(newFactory(), mock(EmulatorConfig.class));
assertEquals(List.of(new Monitor(ALARM_ARN, ALARM_ROLE_ARN), new Monitor(SECOND_ALARM_ARN, null)),
restarted.getEnvironment(appId, monitoredId).getMonitors());
assertNull(restarted.getEnvironment(appId, unmonitoredId).getMonitors());
Deployment deployment = restarted.getDeployment(appId, monitoredId, deploymentNumber);
assertEquals("service-settings", deployment.getConfigurationName());
assertEquals("ssm-parameter://service-settings", deployment.getConfigurationLocationUri());
}
private StorageFactory newFactory() {
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
when(config.defaultAccountId()).thenReturn("000000000000");
when(config.storage().persistentPath()).thenReturn(dir.toString());
ServiceConfigAccess serviceConfigAccess = mock(ServiceConfigAccess.class);
when(serviceConfigAccess.storageMode(anyString())).thenReturn("persistent");
return new StorageFactory(config, serviceConfigAccess);
}
}
@@ -10,9 +10,12 @@ import io.github.hectorvent.floci.services.appconfig.model.ConfigurationProfile;
import io.github.hectorvent.floci.services.appconfig.model.Deployment;
import io.github.hectorvent.floci.services.appconfig.model.DeploymentSummary;
import io.github.hectorvent.floci.services.appconfig.model.Environment;
import io.github.hectorvent.floci.services.appconfig.model.Monitor;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -27,15 +30,22 @@ import java.util.concurrent.atomic.AtomicReference;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.clearInvocations;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
class AppConfigServiceTest {
private static final String ALARM_ARN = "arn:aws:cloudwatch:us-east-1:000000000000:alarm:configuration-errors";
private static final String SECOND_ALARM_ARN = "arn:aws:cloudwatch:us-east-1:000000000000:alarm:configuration-latency";
private static final String ALARM_ROLE_ARN = "arn:aws:iam::000000000000:role/appconfig-alarm-monitor";
private AccountAwareStorageBackend<Application> applicationStore;
private AccountAwareStorageBackend<Environment> environmentStore;
private AccountAwareStorageBackend<Deployment> deploymentStore;
@@ -134,6 +144,91 @@ class AppConfigServiceTest {
assertThrows(RuntimeException.class, () -> service.listDeployments("app", "env", 1, "unknown"));
}
@Test
void environmentKeepsAnImmutableCopyOfItsMonitors() {
Map<String, Object> withRole = new HashMap<>(Map.of("AlarmArn", ALARM_ARN, "AlarmRoleArn", ALARM_ROLE_ARN));
List<Object> monitors = new ArrayList<>(List.of(withRole, Map.of("AlarmArn", SECOND_ALARM_ARN)));
Environment environment = service.createEnvironment("app", Map.of("Name", "monitored", "Monitors", monitors));
monitors.clear();
withRole.put("AlarmArn", "changed");
assertEquals(List.of(new Monitor(ALARM_ARN, ALARM_ROLE_ARN), new Monitor(SECOND_ALARM_ARN, null)),
environment.getMonitors());
assertThrows(UnsupportedOperationException.class, () -> environment.getMonitors().clear());
}
@Test
void environmentCreatedWithoutMonitorsHasNone() {
assertNull(service.createEnvironment("app", Map.of("Name", "unmonitored")).getMonitors());
}
@Test
void monitorsAtTheDocumentedLimitsAreAccepted() {
String rolePrefix = "arn:aws:iam::000000000000:role/";
List<Map<String, Object>> monitors = List.of(
Map.of("AlarmArn", "a".repeat(2048)),
Map.of("AlarmArn", "a", "AlarmRoleArn", rolePrefix + "r".repeat(2048 - rolePrefix.length())),
Map.of("AlarmArn", ALARM_ARN, "AlarmRoleArn", "arn:aws-cn:iam::000000000000:role/monitor"),
Map.of("AlarmArn", ALARM_ARN, "AlarmRoleArn", "arn:aws-us-gov:iam::000000000000:role/path/monitor"),
Map.of("AlarmArn", ALARM_ARN, "AlarmRoleArn", "arn:aws-eusc:iam::000000000000:role/monitor"));
assertEquals(5, service.createEnvironment("app", Map.of("Name", "limits", "Monitors", monitors))
.getMonitors().size());
assertEquals(List.of(), service.createEnvironment("app", Map.of("Name", "empty", "Monitors", List.of()))
.getMonitors());
}
@Test
void invalidMonitorsAreRefusedBeforeTheEnvironmentIsStored() {
clearInvocations(environmentStore);
Map<String, Object> monitor = Map.of("AlarmArn", ALARM_ARN);
List<Object> invalid = new ArrayList<>(List.of(
"not-a-list",
Collections.nCopies(6, monitor),
List.of("not-a-monitor"),
List.of(Map.of()),
List.of(Map.of("AlarmArn", "")),
List.of(Map.of("AlarmArn", 1)),
List.of(Map.of("AlarmArn", "a".repeat(2049))),
List.of(Map.of("AlarmArn", ALARM_ARN, "AlarmRoleArn", 1)),
List.of(Map.of("AlarmArn", ALARM_ARN, "AlarmRoleArn", "appconfig-alarm-monitor")),
List.of(Map.of("AlarmArn", ALARM_ARN, "AlarmRoleArn", "arn:aws:iam::000000000000:user/monitor")),
List.of(Map.of("AlarmArn", ALARM_ARN, "AlarmRoleArn", ALARM_ROLE_ARN + "x".repeat(2048)))));
invalid.add(Collections.singletonList(null));
for (Object monitors : invalid) {
AwsException refused = assertThrows(AwsException.class,
() -> service.createEnvironment("app", Map.of("Name", "refused", "Monitors", monitors)));
assertEquals("BadRequestException", refused.getErrorCode());
assertEquals(400, refused.getHttpStatus());
}
verify(environmentStore, never()).put(anyString(), any());
}
@Test
void invalidMonitorsAreRefusedBeforeTheApplicationIsLookedUp() {
AwsException refused = assertThrows(AwsException.class, () -> service.createEnvironment("missing",
Map.of("Name", "refused", "Monitors", Collections.nCopies(6, Map.of("AlarmArn", ALARM_ARN)))));
assertEquals("BadRequestException", refused.getErrorCode());
}
@Test
void startDeploymentRecordsTheDeployedProfileNameAndLocation() {
ConfigurationProfile profile = service.getConfigurationProfile("app", "profile");
profile.setName("service-settings");
profile.setLocationUri("ssm-parameter://service-settings");
Deployment deployment = service.startDeployment("app", "env", Map.of(
"ConfigurationProfileId", "profile",
"ConfigurationVersion", "1",
"DeploymentStrategyId", "AppConfig.AllAtOnce"));
assertEquals("service-settings", deployment.getConfigurationName());
assertEquals("ssm-parameter://service-settings", deployment.getConfigurationLocationUri());
}
@Test
void deleteEnvironmentRemovesRuntimeState() {
deploymentStore.put("app::env::1", deployment("app", "env", 1));