mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
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
This commit is contained in:
1 parent
de244061cf
commit
a5254bdc0c
3 files changed
+90
-50
No files matched your search
@@ -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
|
||||
|
||||
|
||||
+13
-4
@@ -98,10 +98,13 @@ public class AwsConfigService {
|
||||
private Map<String, DeliveryChannel> deliveryChannels = new ConcurrentHashMap<>();
|
||||
private Map<String, RetentionConfiguration> retentionConfigurations = new ConcurrentHashMap<>();
|
||||
|
||||
// Transient runtime state is scoped by account and region, like the durable stores.
|
||||
private final ConcurrentHashMap<String, Boolean> recorderRunning = new ConcurrentHashMap<>();
|
||||
private final ConcurrentHashMap<String, Long> recorderLastStartTime = new ConcurrentHashMap<>();
|
||||
private final ConcurrentHashMap<String, Long> 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<String, Boolean> recorderRunning = new ConcurrentHashMap<>();
|
||||
private Map<String, Long> recorderLastStartTime = new ConcurrentHashMap<>();
|
||||
private Map<String, Long> recorderLastStopTime = new ConcurrentHashMap<>();
|
||||
|
||||
// per-rule evaluation timing is transient runtime state, keyed account + region + ruleName
|
||||
private final ConcurrentHashMap<String, Long> ruleFirstActivatedTime = new ConcurrentHashMap<>();
|
||||
@@ -136,6 +139,12 @@ public class AwsConfigService {
|
||||
new TypeReference<Map<String, Map<String, Map<String, ConfigEvaluation>>>>() {});
|
||||
this.configurationRecorders = storageBacked("config-recorders.json",
|
||||
new TypeReference<Map<String, ConfigurationRecorder>>() {});
|
||||
this.recorderRunning = storageBacked("config-recorder-running.json",
|
||||
new TypeReference<Map<String, Boolean>>() {});
|
||||
this.recorderLastStartTime = storageBacked("config-recorder-last-start.json",
|
||||
new TypeReference<Map<String, Long>>() {});
|
||||
this.recorderLastStopTime = storageBacked("config-recorder-last-stop.json",
|
||||
new TypeReference<Map<String, Long>>() {});
|
||||
this.deliveryChannels = storageBacked("config-delivery-channels.json",
|
||||
new TypeReference<Map<String, DeliveryChannel>>() {});
|
||||
this.retentionConfigurations = storageBacked("config-retention.json",
|
||||
|
||||
+76
-45
@@ -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<String, StorageBackend<String, ?>> stores = new HashMap<>();
|
||||
|
||||
private SharedStorageFactory() {
|
||||
super(null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <V> AccountAwareStorageBackend<V> create(String serviceName,
|
||||
String fileName,
|
||||
TypeReference<Map<String, V>> typeReference) {
|
||||
return (AccountAwareStorageBackend<V>) stores.computeIfAbsent(fileName, ignored -> AccountAwareStorageBackend.inMemory("000000000000"));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user