diff --git a/compatibility-tests/sdk-test-python/tests/test_config.py b/compatibility-tests/sdk-test-python/tests/test_config.py index b3dfe67fe1..d57e666b6b 100644 --- a/compatibility-tests/sdk-test-python/tests/test_config.py +++ b/compatibility-tests/sdk-test-python/tests/test_config.py @@ -226,6 +226,16 @@ class TestRecorderChannelLifecycle: ) try: + with pytest.raises(ClientError) as exc: + config_client.start_configuration_recorder(ConfigurationRecorderName="default") + assert exc.value.response["Error"]["Code"] == "NoAvailableDeliveryChannelException" + assert exc.value.response["ResponseMetadata"]["HTTPStatusCode"] == 400 + status = config_client.describe_configuration_recorder_status()[ + "ConfigurationRecordersStatus" + ][0] + assert status["recording"] is False + assert status.get("lastStartTime") is None + config_client.put_delivery_channel( DeliveryChannel={"name": "default", "s3BucketName": "config-bucket"} ) @@ -238,6 +248,16 @@ class TestRecorderChannelLifecycle: config_client.stop_configuration_recorder(ConfigurationRecorderName="default") config_client.delete_delivery_channel(DeliveryChannelName="default") + stopped = config_client.describe_configuration_recorder_status()[ + "ConfigurationRecordersStatus" + ][0] + with pytest.raises(ClientError) as exc: + config_client.start_configuration_recorder(ConfigurationRecorderName="default") + assert exc.value.response["Error"]["Code"] == "NoAvailableDeliveryChannelException" + assert config_client.describe_configuration_recorder_status()[ + "ConfigurationRecordersStatus" + ][0] == stopped + with pytest.raises(ClientError) as exc: config_client.delete_delivery_channel(DeliveryChannelName="default") assert exc.value.response["Error"]["Code"] == "NoSuchDeliveryChannelException" diff --git a/docs/services/config.md b/docs/services/config.md index c178132ef7..7d0efbda41 100644 --- a/docs/services/config.md +++ b/docs/services/config.md @@ -36,7 +36,7 @@ | `PutConfigurationRecorder` | Create or update a configuration recorder | | `DescribeConfigurationRecorders` | List configuration recorders | | `DeleteConfigurationRecorder` | Delete a configuration recorder | -| `StartConfigurationRecorder` | Start recording configuration changes | +| `StartConfigurationRecorder` | Start recording configuration changes using a delivery channel in the same account and Region | | `StopConfigurationRecorder` | Stop recording configuration changes | | `DescribeConfigurationRecorderStatus` | Get the status of configuration recorders. Whether a recorder is recording, and when it last started and stopped, persist with the recorder, so a started recorder is still recording after a restart | @@ -164,7 +164,13 @@ aws configservice put-configuration-recorder --configuration-recorder '{ } }' -# Start recording +# Create a delivery channel before starting the recorder +aws configservice put-delivery-channel --delivery-channel '{ + "name": "default", + "s3BucketName": "my-config-bucket" +}' + +# Start recording (without a delivery channel, returns NoAvailableDeliveryChannelException) aws configservice start-configuration-recorder --configuration-recorder-name default # Check recorder status 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 da411cce21..3a7eb8d660 100644 --- a/src/main/java/io/floci/aws/services/configservice/AwsConfigService.java +++ b/src/main/java/io/floci/aws/services/configservice/AwsConfigService.java @@ -679,13 +679,19 @@ public class AwsConfigService { } public void startConfigurationRecorder(String region, String name) { - ConfigurationRecorder recorder = configurationRecorders.get(region); - if (recorder == null || !recorder.name().equals(name)) { - throw new AwsException("NoSuchConfigurationRecorderException", - "Cannot find configuration recorder with the specified name.", 400); + synchronized (lockFor(recorderLockKey(region))) { + ConfigurationRecorder recorder = configurationRecorders.get(region); + if (recorder == null || !recorder.name().equals(name)) { + throw new AwsException("NoSuchConfigurationRecorderException", + "Cannot find configuration recorder with the specified name.", 400); + } + if (!deliveryChannels.containsKey(region)) { + throw new AwsException("NoAvailableDeliveryChannelException", + "Delivery channel is not available to start configuration recorder.", 400); + } + recorderRunning.put(runtimeKey(region), true); + recorderLastStartTime.put(runtimeKey(region), now()); } - recorderRunning.put(runtimeKey(region), true); - recorderLastStartTime.put(runtimeKey(region), now()); } public void stopConfigurationRecorder(String region, String name) { @@ -762,17 +768,19 @@ public class AwsConfigService { } public void deleteDeliveryChannel(String region, String name) { - DeliveryChannel channel = deliveryChannels.get(region); - if (channel == null || !channel.name().equals(name)) { - throw new AwsException("NoSuchDeliveryChannelException", - "Cannot find delivery channel with the specified name.", 400); + synchronized (lockFor(recorderLockKey(region))) { + DeliveryChannel channel = deliveryChannels.get(region); + if (channel == null || !channel.name().equals(name)) { + throw new AwsException("NoSuchDeliveryChannelException", + "Cannot find delivery channel with the specified name.", 400); + } + if (recorderRunning.getOrDefault(runtimeKey(region), false)) { + throw new AwsException("LastDeliveryChannelDeleteFailedException", + "You cannot delete the delivery channel you specified because the customer managed " + + "configuration recorder is running.", 400); + } + deliveryChannels.remove(region); } - if (recorderRunning.getOrDefault(runtimeKey(region), false)) { - throw new AwsException("LastDeliveryChannelDeleteFailedException", - "You cannot delete the delivery channel you specified because the customer managed " - + "configuration recorder is running.", 400); - } - deliveryChannels.remove(region); } // --- Retention Configuration --- 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 f385feadee..ea032bfd2a 100644 --- a/src/test/java/io/floci/aws/services/configservice/AwsConfigAccountIsolationTest.java +++ b/src/test/java/io/floci/aws/services/configservice/AwsConfigAccountIsolationTest.java @@ -1,5 +1,6 @@ package io.floci.aws.services.configservice; +import io.floci.aws.core.common.AwsException; import io.floci.aws.core.common.RegionResolver; import io.floci.aws.core.common.RequestContext; import io.floci.aws.core.storage.AccountAwareStorageBackend; @@ -56,6 +57,7 @@ class AwsConfigAccountIsolationTest { assertFalse(service.describeConfigurationRecorderStatus(REGION, null, null).getFirst().recording()); assertNull(service.describeConfigurationRecorderStatus(REGION, null, null).getFirst().lastStartTime()); service.deleteDeliveryChannel(REGION, "default"); + service.putDeliveryChannel(REGION, new DeliveryChannel("replacement", "bucket", null, null, null, null)); service.startConfigurationRecorder(REGION, "default"); service.stopConfigurationRecorder(REGION, "default"); context.setAccountId(FIRST); @@ -68,6 +70,86 @@ class AwsConfigAccountIsolationTest { assertNotNull(service.describeConfigurationRecorderStatus(REGION, null, null).getFirst().lastStartTime()); } + @Test + void recorderWithoutDeliveryChannelCannotStartOrAcquireStartTime() { + service.putConfigurationRecorder(REGION, new ConfigurationRecorder(null, "default", "role", null, null), null); + + AwsException error = assertThrows(AwsException.class, + () -> service.startConfigurationRecorder(REGION, "default")); + + assertEquals("NoAvailableDeliveryChannelException", error.getErrorCode()); + assertEquals(400, error.getHttpStatus()); + assertFalse(service.describeConfigurationRecorderStatus(REGION, null, null).getFirst().recording()); + assertNull(service.describeConfigurationRecorderStatus(REGION, null, null).getFirst().lastStartTime()); + service.putDeliveryChannel(REGION, new DeliveryChannel("delivery", "bucket", null, null, null, null)); + service.startConfigurationRecorder(REGION, "default"); + assertTrue(service.describeConfigurationRecorderStatus(REGION, null, null).getFirst().recording()); + assertNotNull(service.describeConfigurationRecorderStatus(REGION, null, null).getFirst().lastStartTime()); + } + + @Test + void deliveryChannelInAnotherAccountDoesNotAllowRecorderStart() { + service.putConfigurationRecorder(REGION, new ConfigurationRecorder(null, "default", "role", null, null), null); + service.putDeliveryChannel(REGION, new DeliveryChannel("default", "bucket", null, null, null, null)); + context.setAccountId(SECOND); + service.putConfigurationRecorder(REGION, new ConfigurationRecorder(null, "default", "role", null, null), null); + + AwsException error = assertThrows(AwsException.class, + () -> service.startConfigurationRecorder(REGION, "default")); + + assertEquals("NoAvailableDeliveryChannelException", error.getErrorCode()); + assertFalse(service.describeConfigurationRecorderStatus(REGION, null, null).getFirst().recording()); + assertNull(service.describeConfigurationRecorderStatus(REGION, null, null).getFirst().lastStartTime()); + context.setAccountId(FIRST); + service.startConfigurationRecorder(REGION, "default"); + assertTrue(service.describeConfigurationRecorderStatus(REGION, null, null).getFirst().recording()); + } + + @Test + void deliveryChannelInAnotherRegionDoesNotAllowRecorderStart() { + service.putConfigurationRecorder(REGION, new ConfigurationRecorder(null, "default", "role", null, null), null); + service.putDeliveryChannel(REGION, new DeliveryChannel("default", "bucket", null, null, null, null)); + String otherRegion = "eu-west-1"; + service.putConfigurationRecorder(otherRegion, new ConfigurationRecorder(null, "default", "role", null, null), null); + + AwsException error = assertThrows(AwsException.class, + () -> service.startConfigurationRecorder(otherRegion, "default")); + + assertEquals("NoAvailableDeliveryChannelException", error.getErrorCode()); + assertFalse(service.describeConfigurationRecorderStatus(otherRegion, null, null).getFirst().recording()); + assertNull(service.describeConfigurationRecorderStatus(otherRegion, null, null).getFirst().lastStartTime()); + service.startConfigurationRecorder(REGION, "default"); + assertTrue(service.describeConfigurationRecorderStatus(REGION, null, null).getFirst().recording()); + } + + @Test + void failedRestartAfterChannelDeletionPreservesPreviousRecorderTimes() { + service.putConfigurationRecorder(REGION, new ConfigurationRecorder(null, "default", "role", null, null), null); + service.putDeliveryChannel(REGION, new DeliveryChannel("default", "bucket", null, null, null, null)); + service.startConfigurationRecorder(REGION, "default"); + service.stopConfigurationRecorder(REGION, "default"); + Long lastStart = service.describeConfigurationRecorderStatus(REGION, null, null).getFirst().lastStartTime(); + Long lastStop = service.describeConfigurationRecorderStatus(REGION, null, null).getFirst().lastStopTime(); + service.deleteDeliveryChannel(REGION, "default"); + + AwsException error = assertThrows(AwsException.class, + () -> service.startConfigurationRecorder(REGION, "default")); + + assertEquals("NoAvailableDeliveryChannelException", error.getErrorCode()); + assertFalse(service.describeConfigurationRecorderStatus(REGION, null, null).getFirst().recording()); + assertEquals(lastStart, service.describeConfigurationRecorderStatus(REGION, null, null).getFirst().lastStartTime()); + assertEquals(lastStop, service.describeConfigurationRecorderStatus(REGION, null, null).getFirst().lastStopTime()); + } + + @Test + void missingRecorderKeepsItsOwnErrorWhenDeliveryChannelIsAlsoMissing() { + AwsException error = assertThrows(AwsException.class, + () -> service.startConfigurationRecorder(REGION, "missing")); + + assertEquals("NoSuchConfigurationRecorderException", error.getErrorCode()); + assertTrue(service.describeConfigurationRecorders(REGION, null, null).isEmpty()); + } + @Test void ruleInvocationAndDeletionDoNotChangeAnotherAccountsTiming() { for (String account : List.of(FIRST, SECOND)) { diff --git a/src/test/java/io/floci/aws/services/configservice/ConfigAccountIsolationIntegrationTest.java b/src/test/java/io/floci/aws/services/configservice/ConfigAccountIsolationIntegrationTest.java index 147f9e29c9..425d48711c 100644 --- a/src/test/java/io/floci/aws/services/configservice/ConfigAccountIsolationIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/configservice/ConfigAccountIsolationIntegrationTest.java @@ -39,6 +39,9 @@ class ConfigAccountIsolationIntegrationTest { "SourceIdentifier":"arn:aws:lambda:us-east-1:%s:function:check"}}} """.formatted(account)); } + call(FIRST, "PutDeliveryChannel", """ + {"DeliveryChannel":{"name":"shared","s3BucketName":"config-isolation"}} + """); call(FIRST, "StartConfigurationRecorder", "{\"ConfigurationRecorderName\":\"shared\"}"); call(FIRST, "StartConfigRulesEvaluation", "{\"ConfigRuleNames\":[\"shared\"]}"); call(SECOND, "DescribeConfigurationRecorderStatus", "{}") @@ -55,5 +58,6 @@ class ConfigAccountIsolationIntegrationTest { .body("ConfigRulesEvaluationStatus[0].FirstActivatedTime", notNullValue()); call(FIRST, "DeleteConfigRule", "{\"ConfigRuleName\":\"shared\"}"); call(FIRST, "DeleteConfigurationRecorder", "{\"ConfigurationRecorderName\":\"shared\"}"); + call(FIRST, "DeleteDeliveryChannel", "{\"DeliveryChannelName\":\"shared\"}"); } } 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 c938106dab..82f4557518 100644 --- a/src/test/java/io/floci/aws/services/configservice/ConfigurationRecorderIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/configservice/ConfigurationRecorderIntegrationTest.java @@ -82,6 +82,32 @@ class ConfigurationRecorderIntegrationTest { @Test @Order(3) + void startWithoutDeliveryChannelFailsWithoutChangingStatus() { + given() + .header("X-Amz-Target", TARGET_PREFIX + "StartConfigurationRecorder") + .contentType(CONTENT_TYPE) + .body("{\"ConfigurationRecorderName\": \"default\"}") + .when() + .post("/") + .then() + .statusCode(400) + .body("__type", equalTo("NoAvailableDeliveryChannelException")) + .body("message", equalTo("Delivery channel is not available to start configuration recorder.")); + + given() + .header("X-Amz-Target", TARGET_PREFIX + "DescribeConfigurationRecorderStatus") + .contentType(CONTENT_TYPE) + .body("{\"ConfigurationRecorderNames\": [\"default\"]}") + .when() + .post("/") + .then() + .statusCode(200) + .body("ConfigurationRecordersStatus[0].recording", equalTo(false)) + .body("ConfigurationRecordersStatus[0].lastStartTime", nullValue()); + } + + @Test + @Order(5) void startConfigurationRecorder() { given() .header("X-Amz-Target", TARGET_PREFIX + "StartConfigurationRecorder") @@ -96,7 +122,7 @@ class ConfigurationRecorderIntegrationTest { } @Test - @Order(4) + @Order(6) void describeConfigurationRecorderStatusAfterStart() { given() .header("X-Amz-Target", TARGET_PREFIX + "DescribeConfigurationRecorderStatus") @@ -116,7 +142,7 @@ class ConfigurationRecorderIntegrationTest { } @Test - @Order(5) + @Order(7) void stopConfigurationRecorder() { given() .header("X-Amz-Target", TARGET_PREFIX + "StopConfigurationRecorder") @@ -131,7 +157,7 @@ class ConfigurationRecorderIntegrationTest { } @Test - @Order(6) + @Order(8) void describeConfigurationRecorderStatusAfterStop() { given() .header("X-Amz-Target", TARGET_PREFIX + "DescribeConfigurationRecorderStatus") @@ -148,7 +174,7 @@ class ConfigurationRecorderIntegrationTest { } @Test - @Order(7) + @Order(9) void startNonexistentConfigurationRecorder() { given() .header("X-Amz-Target", TARGET_PREFIX + "StartConfigurationRecorder") @@ -166,7 +192,7 @@ class ConfigurationRecorderIntegrationTest { // --- Delivery Channel --- @Test - @Order(8) + @Order(4) void putDeliveryChannel() { given() .header("X-Amz-Target", TARGET_PREFIX + "PutDeliveryChannel") @@ -190,7 +216,7 @@ class ConfigurationRecorderIntegrationTest { } @Test - @Order(9) + @Order(10) void describeDeliveryChannels() { given() .header("X-Amz-Target", TARGET_PREFIX + "DescribeDeliveryChannels") @@ -222,7 +248,7 @@ class ConfigurationRecorderIntegrationTest { // --- Deletes --- @Test - @Order(10) + @Order(11) void deleteDeliveryChannelWhileRecorderRunning() { given() .header("X-Amz-Target", TARGET_PREFIX + "StartConfigurationRecorder") @@ -249,7 +275,7 @@ class ConfigurationRecorderIntegrationTest { } @Test - @Order(11) + @Order(12) void deleteDeliveryChannelAfterRecorderStopped() { given() .header("X-Amz-Target", TARGET_PREFIX + "StopConfigurationRecorder") @@ -275,7 +301,7 @@ class ConfigurationRecorderIntegrationTest { } @Test - @Order(12) + @Order(13) void deleteDeliveryChannelAgain() { given() .header("X-Amz-Target", TARGET_PREFIX + "DeleteDeliveryChannel") @@ -291,7 +317,7 @@ class ConfigurationRecorderIntegrationTest { } @Test - @Order(13) + @Order(14) void deleteConfigurationRecorder() { given() .header("X-Amz-Target", TARGET_PREFIX + "DeleteConfigurationRecorder") @@ -306,7 +332,7 @@ class ConfigurationRecorderIntegrationTest { } @Test - @Order(14) + @Order(15) void describeConfigurationRecordersAfterDelete() { given() .header("X-Amz-Target", TARGET_PREFIX + "DescribeConfigurationRecorders") @@ -320,7 +346,7 @@ class ConfigurationRecorderIntegrationTest { } @Test - @Order(15) + @Order(16) void deleteConfigurationRecorderAgain() { given() .header("X-Amz-Target", TARGET_PREFIX + "DeleteConfigurationRecorder") @@ -336,7 +362,7 @@ class ConfigurationRecorderIntegrationTest { } @Test - @Order(16) + @Order(17) void recordingStrategyRoundTrips() { given() .header("X-Amz-Target", TARGET_PREFIX + "PutConfigurationRecorder") @@ -365,7 +391,7 @@ class ConfigurationRecorderIntegrationTest { } @Test - @Order(17) + @Order(18) void exclusionsAndRecordingModeRoundTrip() { given() .header("X-Amz-Target", TARGET_PREFIX + "PutConfigurationRecorder") @@ -407,7 +433,7 @@ class ConfigurationRecorderIntegrationTest { } @Test - @Order(18) + @Order(19) void absentFieldsAreNotSynthesized() { given() .header("X-Amz-Target", TARGET_PREFIX + "PutConfigurationRecorder") @@ -436,7 +462,7 @@ class ConfigurationRecorderIntegrationTest { /** PutConfigurationRecorder's Tags is a TagsList, and the operation models ValidationException. */ @Test - @Order(19) + @Order(20) void putConfigurationRecorderRejectsAnInvalidTagWithoutStoringIt() { given() .header("X-Amz-Target", TARGET_PREFIX + "DeleteConfigurationRecorder") @@ -474,7 +500,7 @@ class ConfigurationRecorderIntegrationTest { } @Test - @Order(20) + @Order(21) void recorderTagsAreListedUnderItsArnUntilItIsDeleted() { given() .header("X-Amz-Target", TARGET_PREFIX + "PutConfigurationRecorder") @@ -539,7 +565,7 @@ class ConfigurationRecorderIntegrationTest { } @Test - @Order(21) + @Order(22) void bothDescribeCallsReturnAndFilterByTheRecorderArn() { RestAssuredJsonUtils.awsAction("StarlingDoveService", "PutConfigurationRecorder", """ {"ConfigurationRecorder": {"name": "default", "roleARN": "arn:aws:iam::123456789012:role/r"}} @@ -565,7 +591,7 @@ class ConfigurationRecorderIntegrationTest { } @Test - @Order(22) + @Order(23) void anArnOrNameThatMissesTheRecorderIsNoSuchConfigurationRecorder() { String arn = RestAssuredJsonUtils.awsAction("StarlingDoveService", "DescribeConfigurationRecorders", "{}") .then().statusCode(200) @@ -596,7 +622,7 @@ class ConfigurationRecorderIntegrationTest { /** Arn is an AmazonResourceName, 1-1000 characters, on both Describe calls. */ @Test - @Order(23) + @Order(24) void anArnOutsideItsModeledLengthIsAValidationException() { for (String action : List.of("DescribeConfigurationRecorders", "DescribeConfigurationRecorderStatus")) { RestAssuredJsonUtils.awsAction("StarlingDoveService", action, "{\"Arn\": \"\"}")