From a5254bdc0cff502d2c25bf2aa10f7d6116d635fa Mon Sep 17 00:00:00 2001 From: Allen Sanborn Date: Thu, 29 Oct 2026 18:32:06 -0500 Subject: [PATCH] fix(config): a started configuration recorder keeps recording across a restart (#6938) * fix(config): a started configuration recorder keeps recording across a restart Whether a recorder was recording, and when it last started and stopped, lived in memory while the recorder itself was persisted. After a restart with persistent storage every recorder reported recording=false, so Terraform's aws_config_configuration_recorder_status planned to start it again on every refresh, and a deployment that relies on Config looked off. The three are now persisted alongside the recorders. docs/services/config.md notes that the recording state persists. * test(config): assert recorder start and stop times survive a restart The restart test only checked recording(), so reverting either timestamp store to memory left it green. It now captures lastStartTime after the start and lastStopTime after the stop, and asserts each keeps its exact value through a reload. * test(config): restart AWS Config persistence tests through files on disk The restart tests shared one set of in-memory stores between service instances, so nothing was written or read back and a broken save or load of the recorder state stayed green. Each simulated restart now builds a fresh factory whose backends are new PersistentStorage files in a temporary directory, loaded from disk, including the restart after the recorder is deleted. * test(config): drop blank lines left at the top of the persistence tests * test(config): restart persistence tests through StorageFactories.open --- docs/services/config.md | 2 +- .../configservice/AwsConfigService.java | 17 ++- .../AwsConfigServicePersistenceTest.java | 121 +++++++++++------- 3 files changed, 90 insertions(+), 50 deletions(-) diff --git a/docs/services/config.md b/docs/services/config.md index 6815e9ceb9..adcd167c13 100644 --- a/docs/services/config.md +++ b/docs/services/config.md @@ -38,7 +38,7 @@ | `DeleteConfigurationRecorder` | Delete a configuration recorder | | `StartConfigurationRecorder` | Start recording configuration changes | | `StopConfigurationRecorder` | Stop recording configuration changes | -| `DescribeConfigurationRecorderStatus` | Get the status of configuration recorders | +| `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 | ### Delivery Channel diff --git a/src/main/java/io/github/hectorvent/floci/services/configservice/AwsConfigService.java b/src/main/java/io/github/hectorvent/floci/services/configservice/AwsConfigService.java index 8d1c0ff710..c97f60ae98 100644 --- a/src/main/java/io/github/hectorvent/floci/services/configservice/AwsConfigService.java +++ b/src/main/java/io/github/hectorvent/floci/services/configservice/AwsConfigService.java @@ -98,10 +98,13 @@ public class AwsConfigService { private Map deliveryChannels = new ConcurrentHashMap<>(); private Map retentionConfigurations = new ConcurrentHashMap<>(); - // Transient runtime state is scoped by account and region, like the durable stores. - private final ConcurrentHashMap recorderRunning = new ConcurrentHashMap<>(); - private final ConcurrentHashMap recorderLastStartTime = new ConcurrentHashMap<>(); - private final ConcurrentHashMap recorderLastStopTime = new ConcurrentHashMap<>(); + // Recorder runtime state is scoped by account and region, like the other stores. + // Whether each recorder is recording, and when it last started and stopped: persisted with the + // recorders themselves, so a restart does not silently stop recording (which Terraform's + // aws_config_configuration_recorder_status then plans to restart on every refresh). + private Map recorderRunning = new ConcurrentHashMap<>(); + private Map recorderLastStartTime = new ConcurrentHashMap<>(); + private Map recorderLastStopTime = new ConcurrentHashMap<>(); // per-rule evaluation timing is transient runtime state, keyed account + region + ruleName private final ConcurrentHashMap ruleFirstActivatedTime = new ConcurrentHashMap<>(); @@ -136,6 +139,12 @@ public class AwsConfigService { new TypeReference>>>() {}); this.configurationRecorders = storageBacked("config-recorders.json", new TypeReference>() {}); + this.recorderRunning = storageBacked("config-recorder-running.json", + new TypeReference>() {}); + this.recorderLastStartTime = storageBacked("config-recorder-last-start.json", + new TypeReference>() {}); + this.recorderLastStopTime = storageBacked("config-recorder-last-stop.json", + new TypeReference>() {}); this.deliveryChannels = storageBacked("config-delivery-channels.json", new TypeReference>() {}); this.retentionConfigurations = storageBacked("config-retention.json", diff --git a/src/test/java/io/github/hectorvent/floci/services/configservice/AwsConfigServicePersistenceTest.java b/src/test/java/io/github/hectorvent/floci/services/configservice/AwsConfigServicePersistenceTest.java index 9e95bb387f..c2cb661bd9 100644 --- a/src/test/java/io/github/hectorvent/floci/services/configservice/AwsConfigServicePersistenceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/configservice/AwsConfigServicePersistenceTest.java @@ -1,40 +1,44 @@ package io.github.hectorvent.floci.services.configservice; -import com.fasterxml.jackson.core.type.TypeReference; import io.github.hectorvent.floci.core.common.RegionResolver; -import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; -import io.github.hectorvent.floci.core.storage.InMemoryStorage; -import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.core.storage.StorageFactory; import io.github.hectorvent.floci.services.configservice.model.ConfigEvaluation; import io.github.hectorvent.floci.services.configservice.model.ConfigRule; import io.github.hectorvent.floci.services.configservice.model.ConfigRuleSource; import io.github.hectorvent.floci.services.configservice.model.ConfigurationRecorder; +import io.github.hectorvent.floci.services.configservice.model.ConfigurationRecorderStatus; import io.github.hectorvent.floci.services.configservice.model.DeliveryChannel; import io.github.hectorvent.floci.services.configservice.model.RecordingGroup; +import io.github.hectorvent.floci.testing.StorageFactories; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; -import java.util.HashMap; +import java.nio.file.Path; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; /** - * Verifies AWS Config durable resources survive a restart. Two service instances share the same - * {@link StorageFactory} backends; the second simulates a process restart reloading from disk. + * Verifies AWS Config durable resources survive a restart. Each service instance gets a fresh + * {@link StorageFactory} in {@code persistent} mode over one temporary directory, so a later + * instance sees only what the earlier one wrote to disk and it reads back. */ class AwsConfigServicePersistenceTest { private static final String REGION = "us-east-1"; + @TempDir + Path storageDir; + @Test void durableResourcesAndTagsSurviveRestart() { - SharedStorageFactory storage = new SharedStorageFactory(); - - AwsConfigService first = serviceWithStorage(storage); + AwsConfigService first = restartedService(); 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); @@ -44,7 +48,7 @@ class AwsConfigServicePersistenceTest { new DeliveryChannel("default", "config-bucket", null, null, null, null)); first.tagResource(REGION, rule.configRuleArn(), List.of(Map.of("Key", "env", "Value", "prod"))); - AwsConfigService reloaded = serviceWithStorage(storage); + AwsConfigService reloaded = restartedService(); assertEquals(List.of("s3-public-read"), reloaded.describeConfigRules(REGION, null).stream().map(ConfigRule::configRuleName).toList()); @@ -60,9 +64,7 @@ class AwsConfigServicePersistenceTest { @Test void deleteAndUntagArePersistedAfterRestart() { - SharedStorageFactory storage = new SharedStorageFactory(); - - AwsConfigService first = serviceWithStorage(storage); + AwsConfigService first = restartedService(); ConfigRule rule = first.putConfigRule(REGION, rule("keep", "AWS", "REQUIRED_TAGS")); first.putConfigRule(REGION, rule("drop", "AWS", "REQUIRED_TAGS")); first.deleteConfigRule(REGION, "drop"); @@ -70,7 +72,7 @@ class AwsConfigServicePersistenceTest { List.of(Map.of("Key", "env", "Value", "prod"), Map.of("Key", "team", "Value", "sec"))); first.untagResource(REGION, rule.configRuleArn(), List.of("team")); - AwsConfigService reloaded = serviceWithStorage(storage); + AwsConfigService reloaded = restartedService(); assertEquals(List.of("keep"), reloaded.describeConfigRules(REGION, null).stream().map(ConfigRule::configRuleName).toList()); @@ -82,15 +84,13 @@ class AwsConfigServicePersistenceTest { @Test void evaluationsSurviveRestart() { - SharedStorageFactory storage = new SharedStorageFactory(); - - AwsConfigService first = serviceWithStorage(storage); + AwsConfigService first = restartedService(); first.putConfigRule(REGION, rule("bucket-policy", "CUSTOM_LAMBDA", "arn:aws:lambda:us-east-1:000000000000:function:check")); first.putEvaluations(REGION, "bucket-policy", List.of(evaluation("AWS::S3::Bucket", "bucket-1", "NON_COMPLIANT")), false); - AwsConfigService reloaded = serviceWithStorage(storage); + AwsConfigService reloaded = restartedService(); assertEquals("NON_COMPLIANT", reloaded.complianceForRule(REGION, "bucket-policy").complianceType()); assertEquals(1, reloaded.getComplianceDetailsByConfigRule(REGION, "bucket-policy", @@ -99,9 +99,7 @@ class AwsConfigServicePersistenceTest { @Test void deleteConfigRuleCascadesEvaluationsAcrossRestart() { - SharedStorageFactory storage = new SharedStorageFactory(); - - AwsConfigService first = serviceWithStorage(storage); + AwsConfigService first = restartedService(); first.putConfigRule(REGION, rule("kept-rule", "CUSTOM_LAMBDA", "arn:aws:lambda:us-east-1:000000000000:function:check")); first.putConfigRule(REGION, rule("dropped-rule", "CUSTOM_LAMBDA", @@ -112,7 +110,7 @@ class AwsConfigServicePersistenceTest { List.of(evaluation("AWS::S3::Bucket", "bucket-1", "NON_COMPLIANT")), false); first.deleteConfigRule(REGION, "dropped-rule"); - AwsConfigService reloaded = serviceWithStorage(storage); + AwsConfigService reloaded = restartedService(); assertEquals("COMPLIANT", reloaded.complianceForRule(REGION, "kept-rule").complianceType()); assertEquals("INSUFFICIENT_DATA", reloaded.complianceForRule(REGION, "dropped-rule").complianceType()); @@ -120,12 +118,10 @@ class AwsConfigServicePersistenceTest { @Test void retentionConfigurationSurvivesRestart() { - SharedStorageFactory storage = new SharedStorageFactory(); - - AwsConfigService first = serviceWithStorage(storage); + AwsConfigService first = restartedService(); first.putRetentionConfiguration(REGION, 90); - AwsConfigService reloaded = serviceWithStorage(storage); + AwsConfigService reloaded = restartedService(); assertEquals(90, reloaded.describeRetentionConfigurations(REGION, null) .getFirst().retentionPeriodInDays()); @@ -140,25 +136,60 @@ class AwsConfigServicePersistenceTest { return new ConfigEvaluation(resourceType, resourceId, complianceType, null, 1700000000.0, null, null); } - private static AwsConfigService serviceWithStorage(StorageFactory storage) { - AwsConfigService service = new AwsConfigService(new RegionResolver(REGION, "000000000000"), storage); + @Test + void aStartedRecorderIsStillRecordingAfterRestart() { + AwsConfigService first = restartedService(); + first.putConfigurationRecorder(REGION, new ConfigurationRecorder("default", + "arn:aws:iam::000000000000:role/config", new RecordingGroup(true, false, null))); + first.putDeliveryChannel(REGION, + new DeliveryChannel("default", "config-bucket", null, null, null, null)); + first.startConfigurationRecorder(REGION, "default"); + Long startedAt = first.describeConfigurationRecorderStatus(REGION, null).getFirst().lastStartTime(); + assertNotNull(startedAt); + + AwsConfigService reloaded = restartedService(); + + ConfigurationRecorderStatus afterStart = + reloaded.describeConfigurationRecorderStatus(REGION, null).getFirst(); + assertTrue(afterStart.recording()); + assertEquals(startedAt, afterStart.lastStartTime()); + + reloaded.stopConfigurationRecorder(REGION, "default"); + Long stoppedAt = reloaded.describeConfigurationRecorderStatus(REGION, null).getFirst().lastStopTime(); + assertNotNull(stoppedAt); + + ConfigurationRecorderStatus afterStop = + restartedService().describeConfigurationRecorderStatus(REGION, null).getFirst(); + assertFalse(afterStop.recording()); + assertEquals(startedAt, afterStop.lastStartTime()); + assertEquals(stoppedAt, afterStop.lastStopTime()); + } + + @Test + void aDeletedRecorderDoesNotComeBackRecordingAfterRestart() { + AwsConfigService first = restartedService(); + ConfigurationRecorder recorder = new ConfigurationRecorder("default", + "arn:aws:iam::000000000000:role/config", new RecordingGroup(true, false, null)); + first.putConfigurationRecorder(REGION, recorder); + 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); + + ConfigurationRecorderStatus status = + reloaded.describeConfigurationRecorderStatus(REGION, null).getFirst(); + assertFalse(status.recording()); + assertNull(status.lastStartTime()); + } + + /** A service over a fresh factory and fresh backends that load whatever is on disk. */ + private AwsConfigService restartedService() { + AwsConfigService service = new AwsConfigService(new RegionResolver(REGION, "000000000000"), + StorageFactories.open(storageDir, "persistent", "000000000000")); service.initializeStorage(); return service; } - - private static final class SharedStorageFactory extends StorageFactory { - private final Map> stores = new HashMap<>(); - - private SharedStorageFactory() { - super(null, null); - } - - @Override - @SuppressWarnings("unchecked") - public AccountAwareStorageBackend create(String serviceName, - String fileName, - TypeReference> typeReference) { - return (AccountAwareStorageBackend) stores.computeIfAbsent(fileName, ignored -> AccountAwareStorageBackend.inMemory("000000000000")); - } - } }