feat(accessanalyzer): add analyzer lifecycle (#3117)

* feat(accessanalyzer): add analyzer lifecycle

* fix(accessanalyzer): complete service contracts

* test(accessanalyzer): make SDK cleanup best effort

* test(accessanalyzer): use project logging for cleanup
This commit is contained in:
Franklin G. Mendoza authored and GitHub committed 2026-09-06 10:55:17 -04:00
1 parent 9b28388d89
commit aebed6e15c
17 files changed
+619 -1

No files matched your search

@@ -0,0 +1,72 @@
package io.github.hectorvent.floci.services.accessanalyzer;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.notNullValue;
@QuarkusTest
class AccessAnalyzerIntegrationTest {
private static final String AUTH = "AWS4-HMAC-SHA256 Credential=AKID/20260904/us-east-1/access-analyzer/aws4_request";
@BeforeAll
static void configureRestAssured() { RestAssuredJsonUtils.configureAwsContentTypes(); }
@Test
void analyzerLifecycle() {
given().contentType("application/json").header("Authorization", AUTH)
.body("{\"analyzerName\":\"org-analyzer\",\"type\":\"ORGANIZATION\"}")
.put("/analyzer").then().statusCode(200).body("arn", notNullValue());
given().header("Authorization", AUTH).get("/analyzer")
.then().statusCode(200).body("analyzers", hasSize(1)).body("analyzers[0].name", equalTo("org-analyzer"));
given().header("Authorization", AUTH).delete("/analyzer/org-analyzer").then().statusCode(200);
given().header("Authorization", AUTH).get("/analyzer")
.then().statusCode(200).body("analyzers", hasSize(0));
}
@Test
void analyzerQuotasAreIndependentByExactType() {
given().contentType("application/json").header("Authorization", AUTH)
.body("{\"analyzerName\":\"account-external\",\"type\":\"ACCOUNT\"}")
.put("/analyzer").then().statusCode(200);
given().contentType("application/json").header("Authorization", AUTH)
.body("{\"analyzerName\":\"account-unused\",\"type\":\"ACCOUNT_UNUSED_ACCESS\"}")
.put("/analyzer").then().statusCode(200);
given().contentType("application/json").header("Authorization", AUTH)
.body("{\"analyzerName\":\"account-external-2\",\"type\":\"ACCOUNT\"}")
.put("/analyzer").then().statusCode(402).body("__type", equalTo("ServiceQuotaExceededException"));
given().header("Authorization", AUTH).delete("/analyzer/account-unused").then().statusCode(200);
given().header("Authorization", AUTH).delete("/analyzer/account-external").then().statusCode(200);
}
@Test
void organizationInternalAccessAnalyzerLimitIsOne() {
given().contentType("application/json").header("Authorization", AUTH)
.body("{\"analyzerName\":\"org-internal-1\",\"type\":\"ORGANIZATION_INTERNAL_ACCESS\"}")
.put("/analyzer").then().statusCode(200);
given().contentType("application/json").header("Authorization", AUTH)
.body("{\"analyzerName\":\"org-internal-2\",\"type\":\"ORGANIZATION_INTERNAL_ACCESS\"}")
.put("/analyzer").then().statusCode(402).body("__type", equalTo("ServiceQuotaExceededException"));
given().header("Authorization", AUTH).delete("/analyzer/org-internal-1").then().statusCode(200);
}
@Test
void trailingJsonReturnsSerializationException() {
given().contentType("application/json").header("Authorization", AUTH)
.body("{\"analyzerName\":\"trailing-json\",\"type\":\"ACCOUNT\"} {}")
.put("/analyzer").then().statusCode(400).body("__type", equalTo("SerializationException"));
}
@Test
void invalidAnalyzerTypeReturnsValidationError() {
given().contentType("application/json").header("Authorization", AUTH)
.body("{\"analyzerName\":\"bad-analyzer\",\"type\":\"INVALID\"}")
.put("/analyzer").then().statusCode(400).body("__type", equalTo("ValidationException"));
}
}
@@ -0,0 +1,77 @@
package io.github.hectorvent.floci.services.accessanalyzer;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.accessanalyzer.model.Analyzer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class AccessAnalyzerServiceTest {
private static final String REGION = "us-east-1";
private static final String ACCOUNT_ID = "123456789012";
private final ObjectMapper mapper = new ObjectMapper();
private AccessAnalyzerService service;
@BeforeEach
@SuppressWarnings({"unchecked", "rawtypes"})
void setUp() {
StorageFactory storageFactory = mock(StorageFactory.class);
AccountAwareStorageBackend<Analyzer> store = AccountAwareStorageBackend.inMemory(ACCOUNT_ID);
when(storageFactory.create(eq("accessanalyzer"), eq("accessanalyzer-analyzers.json"), any(TypeReference.class)))
.thenReturn((AccountAwareStorageBackend) store);
RegionResolver regionResolver = mock(RegionResolver.class);
when(regionResolver.buildArn(eq("access-analyzer"), eq(REGION), any(String.class)))
.thenAnswer(invocation -> "arn:aws:access-analyzer:" + REGION + ":" + ACCOUNT_ID + ":"
+ invocation.getArgument(2, String.class));
service = new AccessAnalyzerService(storageFactory, regionResolver);
}
@Test
void quotasAreAppliedPerExactAnalyzerType() {
service.createAnalyzer(request("external", "ACCOUNT"), REGION);
service.createAnalyzer(request("unused", "ACCOUNT_UNUSED_ACCESS"), REGION);
AwsException duplicateType = assertThrows(AwsException.class,
() -> service.createAnalyzer(request("external-2", "ACCOUNT"), REGION));
assertEquals("ServiceQuotaExceededException", duplicateType.getErrorCode());
}
@Test
void organizationInternalAccessLimitIsOne() {
service.createAnalyzer(request("internal", "ORGANIZATION_INTERNAL_ACCESS"), REGION);
AwsException duplicateType = assertThrows(AwsException.class,
() -> service.createAnalyzer(request("internal-2", "ORGANIZATION_INTERNAL_ACCESS"), REGION));
assertEquals("ServiceQuotaExceededException", duplicateType.getErrorCode());
}
@Test
void clearRemovesPersistedState() {
service.createAnalyzer(request("reset-me", "ACCOUNT"), REGION);
service.clear();
assertTrue(service.listAnalyzers(REGION, null, null, null).items().isEmpty());
}
private ObjectNode request(String name, String type) {
ObjectNode request = mapper.createObjectNode();
request.put("analyzerName", name);
request.put("type", type);
return request;
}
}