feat(inspector2): support disabling scans (#7181)

* feat(inspector2): support disabling scans

* fix(inspector2): enforce disable permission

* fix(inspector2): accept optional resource types

---------

Co-authored-by: Ovidiu (Ovi) <462561+ovidiubute@users.noreply.github.com>
This commit is contained in:
Ovidiu (Ovi)andOvidiu authored and GitHub committed 2026-11-01 12:22:49 -04:00
1 parent 488a3197f1
commit fd4abd365b
11 files changed
+275 -33

No files matched your search

@@ -89,6 +89,16 @@ class IamActionRegistryTest {
new MultivaluedHashMap<>(), MediaType.APPLICATION_JSON_TYPE, "{}")));
}
@Test
void inspector2DisableRouteResolvesItsAction() {
ContainerRequestContext ctx = mockCtx("POST", "/disable",
new MultivaluedHashMap<>(), MediaType.APPLICATION_JSON_TYPE, "{}");
assertEquals("inspector2:Disable", registry.resolveRoute("inspector2", ctx));
assertNull(registry.resolveRoute("inspector2", mockCtx("GET", "/disable",
new MultivaluedHashMap<>(), null, "")));
}
@Test
void cloudWatchGetUsesQueryActionAndOperationPrecedence() {
MultivaluedMap<String, String> query = new MultivaluedHashMap<>();
@@ -0,0 +1,57 @@
package io.github.hectorvent.floci.services.inspector2;
import io.github.hectorvent.floci.testing.EnforcementFixtures;
import io.github.hectorvent.floci.testing.IamEnforcementProfile;
import io.github.hectorvent.floci.testing.PartitionCleanup;
import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import java.net.URI;
import java.util.UUID;
import static io.github.hectorvent.floci.testing.EnforcementFixtures.auth;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
@QuarkusTest
@TestProfile(IamEnforcementProfile.class)
class Inspector2IamEnforcementIntegrationTest {
private static final String ACCOUNT = "111111111111";
@TestHTTPResource("/")
static URI baseUri;
@RegisterExtension
final PartitionCleanup cleanup = new PartitionCleanup();
@Test
void explicitDenyPreventsDisablingScans() {
String user = "inspector2-iam-" + UUID.randomUUID().toString().substring(0, 8);
String accessKeyId = EnforcementFixtures.createUser(cleanup, baseUri.getPort(),
auth(ACCOUNT, "iam"), user).accessKeyId();
String policy = """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"*","Resource":"*"},
{"Effect":"Deny","Action":"inspector2:Disable","Resource":"*"}
]}""";
given().formParam("Action", "PutUserPolicy").formParam("UserName", user).formParam("PolicyName", "p")
.formParam("PolicyDocument", policy)
.header("Authorization", auth(ACCOUNT, "iam"))
.post("/").then().statusCode(200);
given().contentType("application/json").header("Authorization", auth(ACCOUNT, "inspector2"))
.body("{\"resourceTypes\":[\"EC2\"]}")
.post("/enable").then().statusCode(200);
given().contentType("application/json").header("Authorization", auth(accessKeyId, "inspector2"))
.body("{\"resourceTypes\":[\"EC2\"]}")
.post("/disable").then().statusCode(403)
.body("__type", equalTo("AccessDeniedException"))
.body("message", containsString("inspector2:Disable"));
}
}
@@ -107,6 +107,45 @@ class Inspector2IntegrationTest {
.body("accounts[0].accountId", equalTo(member));
}
@Test
void disableSelectedScanTypePreservesOthers() {
String management = "940000000071";
String member = "940000000072";
createOrganization(management, member);
given().contentType("application/json").header("Authorization", auth(member))
.body("{\"resourceTypes\":[\"EC2\",\"ECR\"]}")
.post("/enable").then().statusCode(200);
given().contentType("application/json").header("Authorization", auth(member)).body("{}")
.post("/status/batch/get").then().statusCode(200);
given().contentType("application/json").header("Authorization", auth(member)).body("{}")
.post("/status/batch/get").then().statusCode(200);
given().contentType("application/json").header("Authorization", auth(member))
.body("{\"resourceTypes\":[\"EC2\"]}")
.post("/disable").then().statusCode(200)
.body("accounts[0].status", equalTo("DISABLING"))
.body("accounts[0].resourceStatus.ec2", equalTo("DISABLING"))
.body("accounts[0].resourceStatus.ecr", equalTo("ENABLED"));
given().contentType("application/json").header("Authorization", auth(member)).body("{}")
.post("/status/batch/get").then().statusCode(200)
.body("accounts[0].state.status", equalTo("DISABLING"));
given().contentType("application/json").header("Authorization", auth(member)).body("{}")
.post("/status/batch/get").then().statusCode(200)
.body("accounts[0].state.status", equalTo("ENABLED"))
.body("accounts[0].resourceState.ec2.status", equalTo("DISABLED"))
.body("accounts[0].resourceState.ecr.status", equalTo("ENABLED"));
given().contentType("application/json").header("Authorization", auth(member)).body("{}")
.post("/disable").then().statusCode(200)
.body("accounts[0].status", equalTo("ENABLED"));
given().contentType("application/json").header("Authorization", auth(member))
.body("{\"resourceTypes\":[]}")
.post("/disable").then().statusCode(200)
.body("accounts[0].status", equalTo("ENABLED"));
}
@Test
void failedOrganizationUpdateDoesNotPartiallyMutateState() {
String management = "940000000041";
@@ -112,6 +112,38 @@ class Inspector2ServiceTest {
assertEquals("AccessDeniedException", denied.getErrorCode());
}
@Test
void disableOnlyRequestedResourceTypesAndConverges() throws Exception {
service.enable(REGION, MEMBER_ACCOUNT, objectMapper.readTree(
"{\"resourceTypes\":[\"EC2\",\"ECR\"]}"));
service.accountStatus(REGION, MEMBER_ACCOUNT, MEMBER_ACCOUNT);
service.accountStatus(REGION, MEMBER_ACCOUNT, MEMBER_ACCOUNT);
InspectorState disabling = service.disable(REGION, MEMBER_ACCOUNT,
objectMapper.readTree("{\"resourceTypes\":[\"EC2\"]}")).get(MEMBER_ACCOUNT);
assertEquals("DISABLING", disabling.getStatus());
assertEquals("DISABLING", disabling.getEc2Status());
assertEquals("ENABLED", disabling.getEcrStatus());
InspectorState first = service.accountStatus(REGION, MEMBER_ACCOUNT, MEMBER_ACCOUNT);
assertEquals("DISABLING", first.getStatus());
InspectorState converged = service.accountStatus(REGION, MEMBER_ACCOUNT, MEMBER_ACCOUNT);
assertEquals("ENABLED", converged.getStatus());
assertEquals("DISABLED", converged.getEc2Status());
assertEquals("ENABLED", converged.getEcrStatus());
}
@Test
void disableAcceptsOmittedOrEmptyResourceTypes() throws Exception {
InspectorState omitted = service.disable(REGION, MEMBER_ACCOUNT,
objectMapper.readTree("{}")).get(MEMBER_ACCOUNT);
InspectorState empty = service.disable(REGION, MEMBER_ACCOUNT,
objectMapper.readTree("{\"resourceTypes\":[]}")).get(MEMBER_ACCOUNT);
assertEquals("DISABLED", omitted.getStatus());
assertEquals("DISABLED", empty.getStatus());
}
@Test
void managementAccountCannotUpdateOrganizationConfiguration() throws Exception {
service.enableDelegatedAdmin(REGION, MANAGEMENT_ACCOUNT, ADMIN_ACCOUNT);