diff --git a/src/main/java/io/floci/aws/services/configservice/AwsConfigService.java b/src/main/java/io/floci/aws/services/configservice/AwsConfigService.java index 8edce4bcef..451a535d42 100644 --- a/src/main/java/io/floci/aws/services/configservice/AwsConfigService.java +++ b/src/main/java/io/floci/aws/services/configservice/AwsConfigService.java @@ -711,8 +711,9 @@ public class AwsConfigService { } } - public List describeConfigurationRecorders(String region, List names) { + public List describeConfigurationRecorders(String region, List names, String arn) { ConfigurationRecorder recorder = configurationRecorders.get(region); + requireRecorderArn(arn, recorder); if (recorder == null) { if (names != null && !names.isEmpty()) { throw new AwsException("NoSuchConfigurationRecorderException", @@ -731,6 +732,13 @@ public class AwsConfigService { return List.of(recorder); } + private static void requireRecorderArn(String arn, ConfigurationRecorder recorder) { + if (arn != null && (recorder == null || !arn.equals(recorder.arn()))) { + throw new AwsException("NoSuchConfigurationRecorderException", + "Cannot find configuration recorder with the specified ARN.", 400); + } + } + public void deleteConfigurationRecorder(String region, String name) { synchronized (lockFor(recorderLockKey(region))) { ConfigurationRecorder recorder = configurationRecorders.get(region); @@ -770,8 +778,10 @@ public class AwsConfigService { recorderLastStopTime.put(runtimeKey(region), now()); } - public List describeConfigurationRecorderStatus(String region, List names) { + public List describeConfigurationRecorderStatus(String region, List names, + String arn) { ConfigurationRecorder recorder = configurationRecorders.get(region); + requireRecorderArn(arn, recorder); if (recorder == null) { if (names != null && !names.isEmpty()) { throw new AwsException("NoSuchConfigurationRecorderException", @@ -788,6 +798,7 @@ public class AwsConfigService { } } ConfigurationRecorderStatus status = new ConfigurationRecorderStatus( + recorder.arn(), recorder.name(), recorderRunning.getOrDefault(runtimeKey(region), false), recorderLastStartTime.containsKey(runtimeKey(region)) ? "SUCCESS" : "Pending", diff --git a/src/main/java/io/floci/aws/services/configservice/ConfigServiceJsonHandler.java b/src/main/java/io/floci/aws/services/configservice/ConfigServiceJsonHandler.java index 617701afe1..0f41b922dc 100644 --- a/src/main/java/io/floci/aws/services/configservice/ConfigServiceJsonHandler.java +++ b/src/main/java/io/floci/aws/services/configservice/ConfigServiceJsonHandler.java @@ -202,7 +202,8 @@ public class ConfigServiceJsonHandler { private Response describeConfigurationRecorders(JsonNode req, String region) { List names = extractStringList(req, "ConfigurationRecorderNames"); - List recorders = service.describeConfigurationRecorders(region, names); + String arn = req.path("Arn").asText(null); + List recorders = service.describeConfigurationRecorders(region, names, arn); ObjectNode resp = mapper.createObjectNode(); resp.set("ConfigurationRecorders", mapper.valueToTree(recorders)); return Response.ok(resp).build(); @@ -227,7 +228,8 @@ public class ConfigServiceJsonHandler { private Response describeConfigurationRecorderStatus(JsonNode req, String region) { List names = extractStringList(req, "ConfigurationRecorderNames"); - List statuses = service.describeConfigurationRecorderStatus(region, names); + String arn = req.path("Arn").asText(null); + List statuses = service.describeConfigurationRecorderStatus(region, names, arn); ObjectNode resp = mapper.createObjectNode(); resp.set("ConfigurationRecordersStatus", mapper.valueToTree(statuses)); return Response.ok(resp).build(); diff --git a/src/main/java/io/floci/aws/services/configservice/model/ConfigurationRecorderStatus.java b/src/main/java/io/floci/aws/services/configservice/model/ConfigurationRecorderStatus.java index 228f07832a..6c34f76fbf 100644 --- a/src/main/java/io/floci/aws/services/configservice/model/ConfigurationRecorderStatus.java +++ b/src/main/java/io/floci/aws/services/configservice/model/ConfigurationRecorderStatus.java @@ -9,6 +9,7 @@ import io.quarkus.runtime.annotations.RegisterForReflection; @JsonInclude(JsonInclude.Include.NON_NULL) @JsonIgnoreProperties(ignoreUnknown = true) public record ConfigurationRecorderStatus( + @JsonProperty("arn") String arn, @JsonProperty("name") String name, @JsonProperty("recording") Boolean recording, @JsonProperty("lastStatus") String lastStatus, diff --git a/src/test/java/io/floci/aws/services/configservice/AwsConfigAccountIsolationTest.java b/src/test/java/io/floci/aws/services/configservice/AwsConfigAccountIsolationTest.java index f8281642c8..f385feadee 100644 --- a/src/test/java/io/floci/aws/services/configservice/AwsConfigAccountIsolationTest.java +++ b/src/test/java/io/floci/aws/services/configservice/AwsConfigAccountIsolationTest.java @@ -53,19 +53,19 @@ class AwsConfigAccountIsolationTest { context.setAccountId(FIRST); service.startConfigurationRecorder(REGION, "default"); context.setAccountId(SECOND); - assertFalse(service.describeConfigurationRecorderStatus(REGION, null).getFirst().recording()); - assertNull(service.describeConfigurationRecorderStatus(REGION, null).getFirst().lastStartTime()); + assertFalse(service.describeConfigurationRecorderStatus(REGION, null, null).getFirst().recording()); + assertNull(service.describeConfigurationRecorderStatus(REGION, null, null).getFirst().lastStartTime()); service.deleteDeliveryChannel(REGION, "default"); service.startConfigurationRecorder(REGION, "default"); service.stopConfigurationRecorder(REGION, "default"); context.setAccountId(FIRST); - assertTrue(service.describeConfigurationRecorderStatus(REGION, null).getFirst().recording()); - assertNull(service.describeConfigurationRecorderStatus(REGION, null).getFirst().lastStopTime()); + assertTrue(service.describeConfigurationRecorderStatus(REGION, null, null).getFirst().recording()); + assertNull(service.describeConfigurationRecorderStatus(REGION, null, null).getFirst().lastStopTime()); context.setAccountId(SECOND); service.deleteConfigurationRecorder(REGION, "default"); context.setAccountId(FIRST); - assertTrue(service.describeConfigurationRecorderStatus(REGION, null).getFirst().recording()); - assertNotNull(service.describeConfigurationRecorderStatus(REGION, null).getFirst().lastStartTime()); + assertTrue(service.describeConfigurationRecorderStatus(REGION, null, null).getFirst().recording()); + assertNotNull(service.describeConfigurationRecorderStatus(REGION, null, null).getFirst().lastStartTime()); } @Test diff --git a/src/test/java/io/floci/aws/services/configservice/AwsConfigServicePersistenceTest.java b/src/test/java/io/floci/aws/services/configservice/AwsConfigServicePersistenceTest.java index 05d1beca92..8669dc82a0 100644 --- a/src/test/java/io/floci/aws/services/configservice/AwsConfigServicePersistenceTest.java +++ b/src/test/java/io/floci/aws/services/configservice/AwsConfigServicePersistenceTest.java @@ -48,7 +48,7 @@ class AwsConfigServicePersistenceTest { first.putDeliveryChannel(REGION, new DeliveryChannel("default", "config-bucket", null, null, null, null)); first.tagResource(REGION, rule.configRuleArn(), List.of(Map.of("Key", "env", "Value", "prod"))); - String recorderArn = first.describeConfigurationRecorders(REGION, null).getFirst().arn(); + String recorderArn = first.describeConfigurationRecorders(REGION, null, null).getFirst().arn(); AwsConfigService reloaded = restartedService(); @@ -58,8 +58,8 @@ class AwsConfigServicePersistenceTest { reloaded.describeConformancePacks(REGION, null).stream() .map(p -> p.conformancePackName()).toList()); assertEquals("default", - reloaded.describeConfigurationRecorders(REGION, null).getFirst().name()); - assertEquals(recorderArn, reloaded.describeConfigurationRecorders(REGION, null).getFirst().arn()); + reloaded.describeConfigurationRecorders(REGION, null, null).getFirst().name()); + assertEquals(recorderArn, reloaded.describeConfigurationRecorders(REGION, null, null).getFirst().arn()); assertEquals("config-bucket", reloaded.describeDeliveryChannels(REGION, null).getFirst().s3BucketName()); assertEquals("prod", reloaded.listTagsForResource(REGION, rule.configRuleArn()).getFirst().get("Value")); @@ -148,22 +148,22 @@ class AwsConfigServicePersistenceTest { first.putDeliveryChannel(REGION, new DeliveryChannel("default", "config-bucket", null, null, null, null)); first.startConfigurationRecorder(REGION, "default"); - Long startedAt = first.describeConfigurationRecorderStatus(REGION, null).getFirst().lastStartTime(); + Long startedAt = first.describeConfigurationRecorderStatus(REGION, null, null).getFirst().lastStartTime(); assertNotNull(startedAt); AwsConfigService reloaded = restartedService(); ConfigurationRecorderStatus afterStart = - reloaded.describeConfigurationRecorderStatus(REGION, null).getFirst(); + reloaded.describeConfigurationRecorderStatus(REGION, null, null).getFirst(); assertTrue(afterStart.recording()); assertEquals(startedAt, afterStart.lastStartTime()); reloaded.stopConfigurationRecorder(REGION, "default"); - Long stoppedAt = reloaded.describeConfigurationRecorderStatus(REGION, null).getFirst().lastStopTime(); + Long stoppedAt = reloaded.describeConfigurationRecorderStatus(REGION, null, null).getFirst().lastStopTime(); assertNotNull(stoppedAt); ConfigurationRecorderStatus afterStop = - restartedService().describeConfigurationRecorderStatus(REGION, null).getFirst(); + restartedService().describeConfigurationRecorderStatus(REGION, null, null).getFirst(); assertFalse(afterStop.recording()); assertEquals(startedAt, afterStop.lastStartTime()); assertEquals(stoppedAt, afterStop.lastStopTime()); @@ -184,7 +184,7 @@ class AwsConfigServicePersistenceTest { reloaded.putConfigurationRecorder(REGION, recorder, null); ConfigurationRecorderStatus status = - reloaded.describeConfigurationRecorderStatus(REGION, null).getFirst(); + reloaded.describeConfigurationRecorderStatus(REGION, null, null).getFirst(); assertFalse(status.recording()); assertNull(status.lastStartTime()); } diff --git a/src/test/java/io/floci/aws/services/configservice/AwsConfigServiceTaggingTest.java b/src/test/java/io/floci/aws/services/configservice/AwsConfigServiceTaggingTest.java index e4a240f779..3645b75bfc 100644 --- a/src/test/java/io/floci/aws/services/configservice/AwsConfigServiceTaggingTest.java +++ b/src/test/java/io/floci/aws/services/configservice/AwsConfigServiceTaggingTest.java @@ -42,6 +42,8 @@ class AwsConfigServiceTaggingTest { private static final String ACCOUNT = "000000000000"; private static final List> ENV_PROD = List.of(Map.of("Key", "env", "Value", "prod")); private static final List> TEAM_RED = List.of(Map.of("Key", "team", "Value", "red")); + private static final String GHOST_RECORDER_ARN = + "arn:aws:config:us-east-1:000000000000:configuration-recorder/default/ghost"; private AwsConfigService service() { return new AwsConfigService(new RegionResolver(REGION, ACCOUNT), null); @@ -53,6 +55,12 @@ class AwsConfigServiceTaggingTest { assertEquals(400, ex.getHttpStatus()); } + private static void assertNoSuchRecorder(Executable call) { + AwsException ex = assertThrows(AwsException.class, call); + assertEquals("NoSuchConfigurationRecorderException", ex.getErrorCode()); + assertEquals(400, ex.getHttpStatus()); + } + private static void assertAllThreeNotFound(AwsConfigService service, String region, String arn) { assertNotFound(() -> service.tagResource(region, arn, ENV_PROD)); assertNotFound(() -> service.untagResource(region, arn, List.of("env"))); @@ -224,7 +232,45 @@ class AwsConfigServiceTaggingTest { service.deleteConfigurationRecorder(REGION, "default"); - assertTrue(service.describeConfigurationRecorders(REGION, null).isEmpty()); + assertTrue(service.describeConfigurationRecorders(REGION, null, null).isEmpty()); + } + + @Test + void bothDescribeCallsReturnAndFilterByTheRecorderArn() { + AwsConfigService service = service(); + service.putConfigurationRecorder(REGION, recorder("default"), null); + String arn = recorderArn(service); + + assertEquals(arn, service.describeConfigurationRecorderStatus(REGION, null, null).getFirst().arn()); + assertEquals(arn, service.describeConfigurationRecorders(REGION, null, arn).getFirst().arn()); + assertEquals(arn, service.describeConfigurationRecorderStatus(REGION, null, arn).getFirst().arn()); + assertEquals(arn, service.describeConfigurationRecorders(REGION, List.of("default"), arn).getFirst().arn()); + assertEquals(arn, + service.describeConfigurationRecorderStatus(REGION, List.of("default"), arn).getFirst().arn()); + } + + @Test + void anArnOrNameThatMissesTheRecorderIsNoSuchConfigurationRecorder() { + AwsConfigService service = service(); + assertNoSuchRecorder(() -> service.describeConfigurationRecorders(REGION, null, GHOST_RECORDER_ARN)); + assertNoSuchRecorder(() -> service.describeConfigurationRecorderStatus(REGION, null, GHOST_RECORDER_ARN)); + + service.putConfigurationRecorder(REGION, recorder("default"), null); + String arn = recorderArn(service); + + assertNoSuchRecorder(() -> service.describeConfigurationRecorders(REGION, null, GHOST_RECORDER_ARN)); + assertNoSuchRecorder(() -> service.describeConfigurationRecorderStatus(REGION, null, GHOST_RECORDER_ARN)); + assertNoSuchRecorder(() -> service.describeConfigurationRecorders(REGION, List.of("other"), arn)); + assertNoSuchRecorder(() -> service.describeConfigurationRecorderStatus(REGION, List.of("other"), arn)); + } + + @Test + void aRecorderSavedWithoutAnArnHasNoArnInItsStatusAndNoArnFindsIt() { + AwsConfigService service = serviceWithARecorderSavedWithoutAnArn(); + + assertNull(service.describeConfigurationRecorderStatus(REGION, List.of("default"), null).getFirst().arn()); + assertNoSuchRecorder(() -> service.describeConfigurationRecorders(REGION, null, GHOST_RECORDER_ARN)); + assertNoSuchRecorder(() -> service.describeConfigurationRecorderStatus(REGION, null, GHOST_RECORDER_ARN)); } @Test @@ -345,6 +391,6 @@ class AwsConfigServiceTaggingTest { } private static String recorderArn(AwsConfigService service) { - return service.describeConfigurationRecorders(REGION, null).getFirst().arn(); + return service.describeConfigurationRecorders(REGION, null, null).getFirst().arn(); } } diff --git a/src/test/java/io/floci/aws/services/configservice/ConfigurationRecorderIntegrationTest.java b/src/test/java/io/floci/aws/services/configservice/ConfigurationRecorderIntegrationTest.java index 95f3375f7e..efd15c7bf8 100644 --- a/src/test/java/io/floci/aws/services/configservice/ConfigurationRecorderIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/configservice/ConfigurationRecorderIntegrationTest.java @@ -536,4 +536,60 @@ class ConfigurationRecorderIntegrationTest { .statusCode(400) .body("__type", equalTo("ResourceNotFoundException")); } + + @Test + @Order(21) + void bothDescribeCallsReturnAndFilterByTheRecorderArn() { + RestAssuredJsonUtils.awsAction("StarlingDoveService", "PutConfigurationRecorder", """ + {"ConfigurationRecorder": {"name": "default", "roleARN": "arn:aws:iam::123456789012:role/r"}} + """) + .then().statusCode(200); + String arn = RestAssuredJsonUtils.awsAction("StarlingDoveService", "DescribeConfigurationRecorders", "{}") + .then().statusCode(200) + .extract().path("ConfigurationRecorders[0].arn"); + + RestAssuredJsonUtils.awsAction("StarlingDoveService", "DescribeConfigurationRecorderStatus", "{}") + .then().statusCode(200) + .body("ConfigurationRecordersStatus[0].arn", equalTo(arn)); + RestAssuredJsonUtils.awsAction("StarlingDoveService", "DescribeConfigurationRecorders", """ + {"Arn": "%s"} + """.formatted(arn)) + .then().statusCode(200) + .body("ConfigurationRecorders.arn", contains(arn)); + RestAssuredJsonUtils.awsAction("StarlingDoveService", "DescribeConfigurationRecorderStatus", """ + {"Arn": "%s", "ConfigurationRecorderNames": ["default"]} + """.formatted(arn)) + .then().statusCode(200) + .body("ConfigurationRecordersStatus.arn", contains(arn)); + } + + @Test + @Order(22) + void anArnOrNameThatMissesTheRecorderIsNoSuchConfigurationRecorder() { + String arn = RestAssuredJsonUtils.awsAction("StarlingDoveService", "DescribeConfigurationRecorders", "{}") + .then().statusCode(200) + .extract().path("ConfigurationRecorders[0].arn"); + String ghost = "arn:aws:config:us-east-1:000000000000:configuration-recorder/default/ghost"; + + RestAssuredJsonUtils.awsAction("StarlingDoveService", "DescribeConfigurationRecorders", """ + {"Arn": "%s"} + """.formatted(ghost)) + .then().statusCode(400) + .body("__type", equalTo("NoSuchConfigurationRecorderException")); + RestAssuredJsonUtils.awsAction("StarlingDoveService", "DescribeConfigurationRecorderStatus", """ + {"Arn": "%s"} + """.formatted(ghost)) + .then().statusCode(400) + .body("__type", equalTo("NoSuchConfigurationRecorderException")); + RestAssuredJsonUtils.awsAction("StarlingDoveService", "DescribeConfigurationRecorders", """ + {"Arn": "%s", "ConfigurationRecorderNames": ["other"]} + """.formatted(arn)) + .then().statusCode(400) + .body("__type", equalTo("NoSuchConfigurationRecorderException")); + RestAssuredJsonUtils.awsAction("StarlingDoveService", "DescribeConfigurationRecorderStatus", """ + {"Arn": "%s", "ConfigurationRecorderNames": ["other"]} + """.formatted(arn)) + .then().statusCode(400) + .body("__type", equalTo("NoSuchConfigurationRecorderException")); + } }