fix(config): give the configuration recorder an ARN and keep its tags (#8117)

This commit is contained in:
soreavis authored and GitHub committed 2026-11-12 10:54:08 -04:00
1 parent bb689885a3
commit acdbde836a
8 files changed
+302 -45

No files matched your search

@@ -46,7 +46,8 @@ class AwsConfigAccountIsolationTest {
void recorderStartStopAndDeletionStayInTheirAccount() {
for (String account : List.of(FIRST, SECOND)) {
context.setAccountId(account);
service.putConfigurationRecorder(REGION, new ConfigurationRecorder("default", "role", null, null));
service.putConfigurationRecorder(REGION, new ConfigurationRecorder(null, "default", "role", null, null),
null);
service.putDeliveryChannel(REGION, new DeliveryChannel("default", "bucket", null, null, null, null));
}
context.setAccountId(FIRST);
@@ -42,11 +42,13 @@ class AwsConfigServicePersistenceTest {
ConfigRule rule = first.putConfigRule(REGION,
rule("s3-public-read", "AWS", "S3_BUCKET_PUBLIC_READ_PROHIBITED"));
first.putConformancePack(REGION, "ops-pack", "s3://bucket/template.yaml", null);
first.putConfigurationRecorder(REGION, new ConfigurationRecorder("default",
"arn:aws:iam::000000000000:role/config", new RecordingGroup(true, false, null, null, null), null));
first.putConfigurationRecorder(REGION, new ConfigurationRecorder(null, "default",
"arn:aws:iam::000000000000:role/config", new RecordingGroup(true, false, null, null, null), null),
null);
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();
AwsConfigService reloaded = restartedService();
@@ -57,6 +59,7 @@ class AwsConfigServicePersistenceTest {
.map(p -> p.conformancePackName()).toList());
assertEquals("default",
reloaded.describeConfigurationRecorders(REGION, null).getFirst().name());
assertEquals(recorderArn, reloaded.describeConfigurationRecorders(REGION, null).getFirst().arn());
assertEquals("config-bucket",
reloaded.describeDeliveryChannels(REGION, null).getFirst().s3BucketName());
assertEquals("prod", reloaded.listTagsForResource(REGION, rule.configRuleArn()).getFirst().get("Value"));
@@ -139,8 +142,9 @@ class AwsConfigServicePersistenceTest {
@Test
void aStartedRecorderIsStillRecordingAfterRestart() {
AwsConfigService first = restartedService();
first.putConfigurationRecorder(REGION, new ConfigurationRecorder("default",
"arn:aws:iam::000000000000:role/config", new RecordingGroup(true, false, null, null, null), null));
first.putConfigurationRecorder(REGION, new ConfigurationRecorder(null, "default",
"arn:aws:iam::000000000000:role/config", new RecordingGroup(true, false, null, null, null), null),
null);
first.putDeliveryChannel(REGION,
new DeliveryChannel("default", "config-bucket", null, null, null, null));
first.startConfigurationRecorder(REGION, "default");
@@ -168,16 +172,16 @@ class AwsConfigServicePersistenceTest {
@Test
void aDeletedRecorderDoesNotComeBackRecordingAfterRestart() {
AwsConfigService first = restartedService();
ConfigurationRecorder recorder = new ConfigurationRecorder("default",
ConfigurationRecorder recorder = new ConfigurationRecorder(null, "default",
"arn:aws:iam::000000000000:role/config", new RecordingGroup(true, false, null, null, null), null);
first.putConfigurationRecorder(REGION, recorder);
first.putConfigurationRecorder(REGION, recorder, null);
first.putDeliveryChannel(REGION,
new DeliveryChannel("default", "config-bucket", null, null, null, null));
first.startConfigurationRecorder(REGION, "default");
first.deleteConfigurationRecorder(REGION, "default");
AwsConfigService reloaded = restartedService();
reloaded.putConfigurationRecorder(REGION, recorder);
reloaded.putConfigurationRecorder(REGION, recorder, null);
ConfigurationRecorderStatus status =
reloaded.describeConfigurationRecorderStatus(REGION, null).getFirst();
@@ -2,10 +2,12 @@ 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.storage.AccountAwareStorageBackend;
import io.floci.aws.core.storage.InMemoryStorage;
import io.floci.aws.core.storage.StorageBackend;
import io.floci.aws.services.configservice.model.ConfigRule;
import io.floci.aws.services.configservice.model.ConfigRuleSource;
import io.floci.aws.services.configservice.model.ConfigurationRecorder;
import io.floci.aws.testing.InMemoryStorageFactory;
import io.floci.aws.testing.ParkingStore;
import org.junit.jupiter.api.Test;
@@ -24,6 +26,8 @@ import java.util.concurrent.atomic.AtomicReference;
import static io.floci.aws.testing.ThreadStates.awaitBlockedOrDone;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -37,6 +41,7 @@ class AwsConfigServiceTaggingTest {
private static final String REGION = "us-east-1";
private static final String ACCOUNT = "000000000000";
private static final List<Map<String, String>> ENV_PROD = List.of(Map.of("Key", "env", "Value", "prod"));
private static final List<Map<String, String>> TEAM_RED = List.of(Map.of("Key", "team", "Value", "red"));
private AwsConfigService service() {
return new AwsConfigService(new RegionResolver(REGION, ACCOUNT), null);
@@ -154,6 +159,74 @@ class AwsConfigServiceTaggingTest {
assertAllThreeNotFound(service, REGION, arn);
}
@Test
void putConfigurationRecorderMintsAnArnAndAppliesRequestTagsAtCreationOnly() {
AwsConfigService service = service();
service.putConfigurationRecorder(REGION, recorder("default"), ENV_PROD);
String arn = recorderArn(service);
assertTrue(arn.startsWith("arn:aws:config:us-east-1:000000000000:configuration-recorder/default/"), arn);
assertEquals(ENV_PROD, service.listTagsForResource(REGION, arn));
service.putConfigurationRecorder(REGION, recorder("default"), List.of(Map.of("Key", "env", "Value", "dev")));
assertEquals(arn, recorderArn(service));
assertEquals(ENV_PROD, service.listTagsForResource(REGION, arn));
service.tagResource(REGION, arn, TEAM_RED);
service.untagResource(REGION, arn, List.of("env"));
assertEquals(TEAM_RED, service.listTagsForResource(REGION, arn));
}
@Test
void deletingARecorderDropsItsTagsAndARecreatedRecorderGetsANewArn() {
AwsConfigService service = service();
service.putConfigurationRecorder(REGION, recorder("default"), ENV_PROD);
String oldArn = recorderArn(service);
service.deleteConfigurationRecorder(REGION, "default");
assertTrue(service.storedTags(oldArn).isEmpty(), "delete must drop the recorder's tags");
assertAllThreeNotFound(service, REGION, oldArn);
service.putConfigurationRecorder(REGION, recorder("default"), TEAM_RED);
assertNotEquals(oldArn, recorderArn(service));
assertEquals(TEAM_RED, service.listTagsForResource(REGION, recorderArn(service)));
}
@Test
void aRecorderPutUnderAnotherNameReplacesTheOldOneAndItsTags() {
AwsConfigService service = service();
service.putConfigurationRecorder(REGION, recorder("default"), ENV_PROD);
String oldArn = recorderArn(service);
service.putConfigurationRecorder(REGION, recorder("renamed"), TEAM_RED);
String newArn = recorderArn(service);
assertTrue(newArn.contains(":configuration-recorder/renamed/"), newArn);
assertEquals(TEAM_RED, service.listTagsForResource(REGION, newArn));
assertAllThreeNotFound(service, REGION, oldArn);
assertTrue(service.storedTags(oldArn).isEmpty(), "the replaced recorder's tags must go");
}
@Test
void aRecorderSavedWithoutAnArnGetsOneOnItsNextPut() {
AwsConfigService service = serviceWithARecorderSavedWithoutAnArn();
assertNull(recorderArn(service));
service.putConfigurationRecorder(REGION, recorder("default"), ENV_PROD);
String arn = recorderArn(service);
assertTrue(arn.startsWith("arn:aws:config:us-east-1:000000000000:configuration-recorder/default/"), arn);
assertTrue(service.listTagsForResource(REGION, arn).isEmpty(), "the recorder already existed");
}
@Test
void aRecorderSavedWithoutAnArnCanBeDeleted() {
AwsConfigService service = serviceWithARecorderSavedWithoutAnArn();
service.deleteConfigurationRecorder(REGION, "default");
assertTrue(service.describeConfigurationRecorders(REGION, null).isEmpty());
}
@Test
void anArnOfAKindFlociDoesNotModelIsPassedThrough() {
AwsConfigService service = service();
@@ -225,6 +298,26 @@ class AwsConfigServiceTaggingTest {
assertEquals(ENV_PROD, service.listTagsForResource(REGION, arn));
}
@Test
void aRecorderPutRetriedAfterAFailedCreationTagWriteEndsWithTheTags() {
AtomicBoolean failNextWrite = new AtomicBoolean(true);
AwsConfigService service = serviceWithTagStore(new InMemoryStorage<String, Map<String, String>>() {
@Override
public void put(String key, Map<String, String> value) {
if (failNextWrite.getAndSet(false)) {
throw new IllegalStateException("tag store unavailable");
}
super.put(key, value);
}
});
assertThrows(IllegalStateException.class,
() -> service.putConfigurationRecorder(REGION, recorder("default"), ENV_PROD));
service.putConfigurationRecorder(REGION, recorder("default"), ENV_PROD);
assertEquals(ENV_PROD, service.listTagsForResource(REGION, recorderArn(service)));
}
private static AwsConfigService serviceWithTagStore(StorageBackend<String, Map<String, String>> tagStore) {
AwsConfigService service = new AwsConfigService(new RegionResolver(REGION, ACCOUNT),
new InMemoryStorageFactory(ACCOUNT).withStore("config-tags.json", tagStore));
@@ -232,9 +325,26 @@ class AwsConfigServiceTaggingTest {
return service;
}
private static AwsConfigService serviceWithARecorderSavedWithoutAnArn() {
AccountAwareStorageBackend<ConfigurationRecorder> recorders = AccountAwareStorageBackend.inMemory(ACCOUNT);
recorders.put(REGION, recorder("default"));
AwsConfigService service = new AwsConfigService(new RegionResolver(REGION, ACCOUNT),
new InMemoryStorageFactory(ACCOUNT).withStore("config-recorders.json", recorders));
service.initializeStorage();
return service;
}
private static ConfigRule rule(String name) {
return new ConfigRule(name, null, null, null, null,
new ConfigRuleSource("AWS", "S3_BUCKET_VERSIONING_ENABLED", null, null),
null, null, null, null, null);
}
private static ConfigurationRecorder recorder(String name) {
return new ConfigurationRecorder(null, name, "arn:aws:iam::000000000000:role/config", null, null);
}
private static String recorderArn(AwsConfigService service) {
return service.describeConfigurationRecorders(REGION, null).getFirst().arn();
}
}
@@ -431,4 +431,109 @@ class ConfigurationRecorderIntegrationTest {
.body("ConfigurationRecorders[0].recordingGroup", not(hasKey("exclusionByResourceTypes")))
.body("ConfigurationRecorders[0]", not(hasKey("recordingMode")));
}
/** PutConfigurationRecorder's Tags is a TagsList, and the operation models ValidationException,
* not the InvalidParameterValueException PutConfigRule answers. */
@Test
@Order(19)
void putConfigurationRecorderRejectsAnInvalidTagWithoutStoringIt() {
given()
.header("X-Amz-Target", TARGET_PREFIX + "DeleteConfigurationRecorder")
.contentType(CONTENT_TYPE)
.body("""
{"ConfigurationRecorderName": "default"}
""")
.when()
.post("/")
.then()
.statusCode(200);
given()
.header("X-Amz-Target", TARGET_PREFIX + "PutConfigurationRecorder")
.contentType(CONTENT_TYPE)
.body("""
{"ConfigurationRecorder": {"name": "default", "roleARN": "arn:aws:iam::123456789012:role/r"},
"Tags": [{"Key": "", "Value": "red"}]}
""")
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("ValidationException"));
given()
.header("X-Amz-Target", TARGET_PREFIX + "DescribeConfigurationRecorders")
.contentType(CONTENT_TYPE)
.body("{}")
.when()
.post("/")
.then()
.statusCode(200)
.body("ConfigurationRecorders", hasSize(0));
}
@Test
@Order(20)
void recorderTagsAreListedUnderItsArnUntilItIsDeleted() {
given()
.header("X-Amz-Target", TARGET_PREFIX + "PutConfigurationRecorder")
.contentType(CONTENT_TYPE)
.body("""
{"ConfigurationRecorder": {"name": "default", "roleARN": "arn:aws:iam::123456789012:role/r"},
"Tags": [{"Key": "team", "Value": "red"}]}
""")
.when()
.post("/")
.then()
.statusCode(200);
String arn = given()
.header("X-Amz-Target", TARGET_PREFIX + "DescribeConfigurationRecorders")
.contentType(CONTENT_TYPE)
.body("{}")
.when()
.post("/")
.then()
.statusCode(200)
.body("ConfigurationRecorders[0].arn",
matchesPattern("arn:aws:config:us-east-1:\\d{12}:configuration-recorder/default/\\w+"))
.extract().path("ConfigurationRecorders[0].arn");
given()
.header("X-Amz-Target", TARGET_PREFIX + "ListTagsForResource")
.contentType(CONTENT_TYPE)
.body("""
{"ResourceArn": "%s"}
""".formatted(arn))
.when()
.post("/")
.then()
.statusCode(200)
.body("Tags", hasSize(1))
.body("Tags[0].Key", equalTo("team"))
.body("Tags[0].Value", equalTo("red"));
given()
.header("X-Amz-Target", TARGET_PREFIX + "DeleteConfigurationRecorder")
.contentType(CONTENT_TYPE)
.body("""
{"ConfigurationRecorderName": "default"}
""")
.when()
.post("/")
.then()
.statusCode(200);
given()
.header("X-Amz-Target", TARGET_PREFIX + "ListTagsForResource")
.contentType(CONTENT_TYPE)
.body("""
{"ResourceArn": "%s"}
""".formatted(arn))
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("ResourceNotFoundException"));
}
}