fix(config): scope recorder and rule timing state by account (#5719)

This commit is contained in:
Daniël van Ziel authored and GitHub committed 2026-10-15 10:19:49 -04:00
1 parent 3ad8c25a4f
commit fdfc74146e
3 files changed
+175 -19

No files matched your search

@@ -96,17 +96,17 @@ public class AwsConfigService {
private Map<String, DeliveryChannel> deliveryChannels = new ConcurrentHashMap<>();
private Map<String, RetentionConfiguration> retentionConfigurations = new ConcurrentHashMap<>();
// recorder run-state is transient runtime state (not persisted)
// 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<>();
// per-rule evaluation timing is transient runtime state, keyed region + "|" + ruleName
// per-rule evaluation timing is transient runtime state, keyed account + region + ruleName
private final ConcurrentHashMap<String, Long> ruleFirstActivatedTime = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, Long> ruleLastInvokedTime = new ConcurrentHashMap<>();
// guards the check-then-act between a rule's existence and its evaluations, keyed
// region + "|" + ruleName, so DeleteConfigRule can't race PutEvaluations/PutExternalEvaluation
// account + region + ruleName, so DeleteConfigRule can't race PutEvaluations/PutExternalEvaluation
// into recreating a deleted rule's evaluation bucket for a later same-named rule to inherit
private final ConcurrentHashMap<String, Object> ruleLocks = new ConcurrentHashMap<>();
@@ -683,9 +683,9 @@ public class AwsConfigService {
"Cannot find configuration recorder with the specified name.", 400);
}
configurationRecorders.remove(region);
recorderRunning.remove(region);
recorderLastStartTime.remove(region);
recorderLastStopTime.remove(region);
recorderRunning.remove(runtimeKey(region));
recorderLastStartTime.remove(runtimeKey(region));
recorderLastStopTime.remove(runtimeKey(region));
}
public void startConfigurationRecorder(String region, String name) {
@@ -694,8 +694,8 @@ public class AwsConfigService {
throw new AwsException("NoSuchConfigurationRecorderException",
"Cannot find configuration recorder with the specified name.", 400);
}
recorderRunning.put(region, true);
recorderLastStartTime.put(region, now());
recorderRunning.put(runtimeKey(region), true);
recorderLastStartTime.put(runtimeKey(region), now());
}
public void stopConfigurationRecorder(String region, String name) {
@@ -704,8 +704,8 @@ public class AwsConfigService {
throw new AwsException("NoSuchConfigurationRecorderException",
"Cannot find configuration recorder with the specified name.", 400);
}
recorderRunning.put(region, false);
recorderLastStopTime.put(region, now());
recorderRunning.put(runtimeKey(region), false);
recorderLastStopTime.put(runtimeKey(region), now());
}
public List<ConfigurationRecorderStatus> describeConfigurationRecorderStatus(String region, List<String> names) {
@@ -727,10 +727,10 @@ public class AwsConfigService {
}
ConfigurationRecorderStatus status = new ConfigurationRecorderStatus(
recorder.name(),
recorderRunning.getOrDefault(region, false),
recorderLastStartTime.containsKey(region) ? "SUCCESS" : "Pending",
recorderLastStartTime.get(region),
recorderLastStopTime.get(region));
recorderRunning.getOrDefault(runtimeKey(region), false),
recorderLastStartTime.containsKey(runtimeKey(region)) ? "SUCCESS" : "Pending",
recorderLastStartTime.get(runtimeKey(region)),
recorderLastStopTime.get(runtimeKey(region)));
return List.of(status);
}
@@ -773,7 +773,7 @@ public class AwsConfigService {
throw new AwsException("NoSuchDeliveryChannelException",
"Cannot find delivery channel with the specified name.", 400);
}
if (recorderRunning.getOrDefault(region, false)) {
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);
@@ -1049,9 +1049,9 @@ public class AwsConfigService {
/** Aggregation authorizations share {@link #ruleLocks} with the config rules, so their keys
* are namespaced apart. A config rule name cannot contain '|', but making the two key spaces
* disjoint by construction costs nothing and does not depend on that staying true. */
private static String authorizationLockKey(String region, String authorizedAccountId,
private String authorizationLockKey(String region, String authorizedAccountId,
String authorizedAwsRegion) {
return "aggregation-authorization|" + region + "|"
return "aggregation-authorization|" + runtimeKey(region) + "|"
+ authorizationKey(authorizedAccountId, authorizedAwsRegion);
}
@@ -1063,8 +1063,12 @@ public class AwsConfigService {
return resourceType + "|" + resourceId;
}
private static String ruleKey(String region, String ruleName) {
return region + "|" + ruleName;
private String runtimeKey(String region) {
return regionResolver.getAccountId() + "|" + region;
}
private String ruleKey(String region, String ruleName) {
return runtimeKey(region) + "|" + ruleName;
}
private Object lockFor(String key) {
@@ -0,0 +1,93 @@
package io.github.hectorvent.floci.services.configservice;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.RequestContext;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
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.DeliveryChannel;
import jakarta.enterprise.inject.Instance;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
class AwsConfigAccountIsolationTest {
private static final String REGION = "us-east-1";
private static final String FIRST = "111111111111";
private static final String SECOND = "222222222222";
private final RequestContext context = new RequestContext();
private AwsConfigService service;
@BeforeEach
@SuppressWarnings("unchecked")
void setUp() {
Instance<RequestContext> contexts = mock(Instance.class);
when(contexts.get()).thenReturn(context);
RegionResolver resolver = mock(RegionResolver.class);
when(resolver.getAccountId()).thenAnswer(ignored -> context.getAccountId());
StorageFactory factory = mock(StorageFactory.class);
when(factory.create(eq("config"), anyString(), any())).thenAnswer(ignored ->
new AccountAwareStorageBackend<>(new InMemoryStorage<>(), contexts, FIRST));
context.setAccountId(FIRST);
service = new AwsConfigService(resolver, factory);
service.initializeStorage();
}
@Test
void recorderStartStopAndDeletionStayInTheirAccount() {
for (String account : List.of(FIRST, SECOND)) {
context.setAccountId(account);
service.putConfigurationRecorder(REGION, new ConfigurationRecorder("default", "role", null));
service.putDeliveryChannel(REGION, new DeliveryChannel("default", "bucket", null, null, null, null));
}
context.setAccountId(FIRST);
service.startConfigurationRecorder(REGION, "default");
context.setAccountId(SECOND);
assertFalse(service.describeConfigurationRecorderStatus(REGION, null).getFirst().recording());
assertNull(service.describeConfigurationRecorderStatus(REGION, 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());
context.setAccountId(SECOND);
service.deleteConfigurationRecorder(REGION, "default");
context.setAccountId(FIRST);
assertTrue(service.describeConfigurationRecorderStatus(REGION, null).getFirst().recording());
assertNotNull(service.describeConfigurationRecorderStatus(REGION, null).getFirst().lastStartTime());
}
@Test
void ruleInvocationAndDeletionDoNotChangeAnotherAccountsTiming() {
for (String account : List.of(FIRST, SECOND)) {
context.setAccountId(account);
service.putConfigRule(REGION, new ConfigRule("shared", null, null, null, null,
new ConfigRuleSource("CUSTOM_LAMBDA", "check", null, null),
null, null, null, null, null));
}
context.setAccountId(FIRST);
service.startConfigRulesEvaluation(REGION, List.of("shared"));
context.setAccountId(SECOND);
assertFalse(service.describeConfigRuleEvaluationStatus(REGION, null, null, null)
.items().getFirst().firstEvaluationStarted());
service.putEvaluations(REGION, "shared", List.of(new ConfigEvaluation("AWS::S3::Bucket", "bucket",
"COMPLIANT", null, 1700000000.0, null, null)), false);
service.deleteConfigRule(REGION, "shared");
context.setAccountId(FIRST);
assertNotNull(service.describeConfigRuleEvaluationStatus(REGION, null, null, null)
.items().getFirst().firstActivatedTime());
assertTrue(service.describeConfigRuleEvaluationStatus(REGION, null, null, null)
.items().getFirst().firstEvaluationStarted());
assertEquals("INSUFFICIENT_DATA", service.complianceForRule(REGION, "shared").complianceType());
}
}
@@ -0,0 +1,59 @@
package io.github.hectorvent.floci.services.configservice;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.response.ValidatableResponse;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.List;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.*;
@QuarkusTest
class ConfigAccountIsolationIntegrationTest {
private static final String FIRST = "833333333333";
private static final String SECOND = "844444444444";
@BeforeAll
static void setUpJson() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
private static ValidatableResponse call(String account, String action, String body) {
return given().header("Authorization", "AWS4-HMAC-SHA256 Credential=" + account
+ "/20261011/us-east-1/config/aws4_request")
.header("X-Amz-Target", "StarlingDoveService." + action)
.contentType("application/x-amz-json-1.1").body(body).post("/").then().statusCode(200);
}
@Test
void sameNamedRecorderAndRuleRuntimeStateStaysInEachAccount() {
for (String account : List.of(FIRST, SECOND)) {
call(account, "PutConfigurationRecorder", """
{"ConfigurationRecorder":{"name":"shared","roleARN":"arn:aws:iam::%s:role/config"}}
""".formatted(account));
call(account, "PutConfigRule", """
{"ConfigRule":{"ConfigRuleName":"shared","Source":{"Owner":"CUSTOM_LAMBDA",
"SourceIdentifier":"arn:aws:lambda:us-east-1:%s:function:check"}}}
""".formatted(account));
}
call(FIRST, "StartConfigurationRecorder", "{\"ConfigurationRecorderName\":\"shared\"}");
call(FIRST, "StartConfigRulesEvaluation", "{\"ConfigRuleNames\":[\"shared\"]}");
call(SECOND, "DescribeConfigurationRecorderStatus", "{}")
.body("ConfigurationRecordersStatus[0].recording", equalTo(false))
.body("ConfigurationRecordersStatus[0].lastStartTime", nullValue());
call(SECOND, "DescribeConfigRuleEvaluationStatus", "{}")
.body("ConfigRulesEvaluationStatus[0].FirstEvaluationStarted", equalTo(false));
call(SECOND, "DeleteConfigRule", "{\"ConfigRuleName\":\"shared\"}");
call(SECOND, "DeleteConfigurationRecorder", "{\"ConfigurationRecorderName\":\"shared\"}");
call(FIRST, "DescribeConfigurationRecorderStatus", "{}")
.body("ConfigurationRecordersStatus[0].recording", equalTo(true));
call(FIRST, "DescribeConfigRuleEvaluationStatus", "{}")
.body("ConfigRulesEvaluationStatus[0].FirstEvaluationStarted", equalTo(true))
.body("ConfigRulesEvaluationStatus[0].FirstActivatedTime", notNullValue());
call(FIRST, "DeleteConfigRule", "{\"ConfigRuleName\":\"shared\"}");
call(FIRST, "DeleteConfigurationRecorder", "{\"ConfigurationRecorderName\":\"shared\"}");
}
}