mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(config): scope recorder and rule timing state by account (#5719)
This commit is contained in:
1 parent
3ad8c25a4f
commit
fdfc74146e
3 files changed
+175
-19
No files matched your search
+93
@@ -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());
|
||||
}
|
||||
}
|
||||
+59
@@ -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\"}");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user