mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ecr): return per-repository scanning batch failures (#6569)
This commit is contained in:
1 parent
e1726e8394
commit
671e4bfb7c
7 files changed
+238
-14
No files matched your search
@@ -22,12 +22,13 @@ class EcrJsonHandlerTest {
|
||||
@Test
|
||||
void theScanningConfigurationIsOfTheCallersRepositories() throws Exception {
|
||||
EcrService service = mock(EcrService.class);
|
||||
when(service.describeRepositories(List.of("web"), null, "us-east-1")).thenReturn(List.of());
|
||||
when(service.batchGetRepositoryScanningConfiguration(List.of("web"), null, "us-east-1"))
|
||||
.thenReturn(new EcrService.BatchScanningConfigurationResult(List.of(), List.of()));
|
||||
EcrJsonHandler handler = new EcrJsonHandler(service, MAPPER);
|
||||
|
||||
assertEquals(200, handler.handle("BatchGetRepositoryScanningConfiguration",
|
||||
MAPPER.readTree("{\"registryId\":\"111122223333\",\"repositoryNames\":[\"web\"]}"), "us-east-1")
|
||||
.getStatus());
|
||||
verify(service).describeRepositories(List.of("web"), null, "us-east-1");
|
||||
verify(service).batchGetRepositoryScanningConfiguration(List.of("web"), null, "us-east-1");
|
||||
}
|
||||
}
|
||||
+35
@@ -1,6 +1,8 @@
|
||||
package io.github.hectorvent.floci.services.ecr;
|
||||
|
||||
import io.github.hectorvent.floci.services.ecr.registry.EcrRegistryManager;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.InjectMock;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
@@ -9,6 +11,7 @@ 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.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* The registry's own settings over the JSON protocol: the registry policy, the replication
|
||||
@@ -18,6 +21,9 @@ import static org.hamcrest.Matchers.hasSize;
|
||||
@QuarkusTest
|
||||
class EcrRegistrySettingsIntegrationTest {
|
||||
|
||||
@InjectMock
|
||||
EcrRegistryManager registryManager;
|
||||
|
||||
private static final String CT = "application/x-amz-json-1.1";
|
||||
private static final String PREFIX = "AmazonEC2ContainerRegistry_V20150921.";
|
||||
private static final String POLICY = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"Replicate\","
|
||||
@@ -99,6 +105,35 @@ class EcrRegistrySettingsIntegrationTest {
|
||||
.body("__type", equalTo("InvalidParameterException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void repositoryScanningBatchKeepsValidResultsWhenAnotherNameIsMissing() {
|
||||
String region = "eu-west-3";
|
||||
when(registryManager.getRepositoryUri("000000000000", region, "batch-scan-present"))
|
||||
.thenReturn("localhost:4566/batch-scan-present");
|
||||
call("CreateRepository", """
|
||||
{"repositoryName":"batch-scan-present","imageScanningConfiguration":{"scanOnPush":true}}
|
||||
""", region).statusCode(200);
|
||||
|
||||
call("BatchGetRepositoryScanningConfiguration", """
|
||||
{"repositoryNames":["batch-scan-missing","batch-scan-present"]}
|
||||
""", region).statusCode(200)
|
||||
.body("scanningConfigurations", hasSize(1))
|
||||
.body("scanningConfigurations[0].repositoryName", equalTo("batch-scan-present"))
|
||||
.body("scanningConfigurations[0].scanOnPush", equalTo(true))
|
||||
.body("scanningConfigurations[0].scanFrequency", equalTo("SCAN_ON_PUSH"))
|
||||
.body("scanningConfigurations[0].appliedScanFilters", hasSize(0))
|
||||
.body("failures", hasSize(1))
|
||||
.body("failures[0].repositoryName", equalTo("batch-scan-missing"))
|
||||
.body("failures[0].failureCode", equalTo("REPOSITORY_NOT_FOUND"));
|
||||
|
||||
call("BatchGetRepositoryScanningConfiguration", """
|
||||
{"repositoryNames":["batch-scan-missing"]}
|
||||
""", region).statusCode(200)
|
||||
.body("scanningConfigurations", hasSize(0))
|
||||
.body("failures", hasSize(1))
|
||||
.body("failures[0].failureCode", equalTo("REPOSITORY_NOT_FOUND"));
|
||||
}
|
||||
|
||||
private static String quote(String text) {
|
||||
return "\"" + text.replace("\\", "\\\\").replace("\"", "\\\"") + "\"";
|
||||
}
|
||||
|
||||
+132
@@ -0,0 +1,132 @@
|
||||
package io.github.hectorvent.floci.services.ecr;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.services.ecr.registry.EcrRegistryManager;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class EcrScanningConfigurationTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String ACCOUNT = "000000000000";
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
private EcrService service;
|
||||
private EcrRegistryManager registry;
|
||||
private EcrJsonHandler handler;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
registry = mock(EcrRegistryManager.class);
|
||||
when(registry.getRepositoryUri(anyString(), anyString(), anyString()))
|
||||
.thenReturn("localhost:4566/scanning-test");
|
||||
service = new EcrService(new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), registry, mock(EmulatorConfig.class),
|
||||
new RegionResolver(REGION, ACCOUNT));
|
||||
handler = new EcrJsonHandler(service, mapper);
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingRepositoryDoesNotDiscardConfigurationsBeforeOrAfterIt() {
|
||||
create("scan-on", true, null, REGION);
|
||||
create("scan-off", false, null, REGION);
|
||||
|
||||
JsonNode response = batch(List.of("scan-on", "missing", "scan-off"), REGION);
|
||||
|
||||
JsonNode configs = response.path("scanningConfigurations");
|
||||
assertEquals(2, configs.size());
|
||||
assertEquals("scan-on", configs.get(0).path("repositoryName").asText());
|
||||
assertTrue(configs.get(0).path("scanOnPush").asBoolean());
|
||||
assertEquals("SCAN_ON_PUSH", configs.get(0).path("scanFrequency").asText());
|
||||
assertEquals("scan-off", configs.get(1).path("repositoryName").asText());
|
||||
assertFalse(configs.get(1).path("scanOnPush").asBoolean());
|
||||
assertEquals("MANUAL", configs.get(1).path("scanFrequency").asText());
|
||||
assertTrue(configs.get(0).path("appliedScanFilters").isEmpty());
|
||||
JsonNode failures = response.path("failures");
|
||||
assertEquals(1, failures.size());
|
||||
assertFailure(failures.get(0), "missing");
|
||||
}
|
||||
|
||||
@Test
|
||||
void allMissingRepositoriesReturnIndividualFailures() {
|
||||
JsonNode response = batch(List.of("missing-first", "missing-second"), REGION);
|
||||
|
||||
assertTrue(response.path("scanningConfigurations").isEmpty());
|
||||
assertEquals(2, response.path("failures").size());
|
||||
assertFailure(response.path("failures").get(0), "missing-first");
|
||||
assertFailure(response.path("failures").get(1), "missing-second");
|
||||
}
|
||||
|
||||
@Test
|
||||
void allExistingRepositoriesReturnNoFailures() {
|
||||
create("present", false, null, REGION);
|
||||
|
||||
JsonNode response = batch(List.of("present"), REGION);
|
||||
|
||||
assertEquals(1, response.path("scanningConfigurations").size());
|
||||
assertTrue(response.path("failures").isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void repositoryInAnotherAccountOrRegionIsReportedMissing() {
|
||||
create("other-region", true, null, "us-west-2");
|
||||
create("other-account", true, "111111111111", REGION);
|
||||
|
||||
JsonNode response = batch(List.of("other-region", "other-account"), REGION);
|
||||
|
||||
assertTrue(response.path("scanningConfigurations").isEmpty());
|
||||
assertEquals(2, response.path("failures").size());
|
||||
assertFailure(response.path("failures").get(0), "other-region");
|
||||
assertFailure(response.path("failures").get(1), "other-account");
|
||||
}
|
||||
|
||||
@Test
|
||||
void unrelatedErrorsAreNotReportedAsMissingRepositories() {
|
||||
create("present", false, null, REGION);
|
||||
AwsException failure = new AwsException("ServerException", "lookup failed", 500);
|
||||
when(registry.getRepositoryUri(anyString(), anyString(), anyString())).thenThrow(failure);
|
||||
|
||||
assertSame(failure, assertThrows(AwsException.class,
|
||||
() -> batch(List.of("missing", "present"), REGION)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeRepositoriesStillFailsTheWholeRequest() {
|
||||
create("present", false, null, REGION);
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class,
|
||||
() -> service.describeRepositories(List.of("present", "missing"), null, REGION));
|
||||
|
||||
assertEquals("RepositoryNotFoundException", failure.getErrorCode());
|
||||
}
|
||||
|
||||
private void create(String name, boolean scanOnPush, String account, String region) {
|
||||
service.createRepository(name, account, null, scanOnPush, null, null, Map.of(), region);
|
||||
}
|
||||
|
||||
private JsonNode batch(List<String> names, String region) {
|
||||
try (Response response = handler.handle("BatchGetRepositoryScanningConfiguration",
|
||||
mapper.valueToTree(Map.of("repositoryNames", names)), region)) {
|
||||
assertEquals(200, response.getStatus());
|
||||
return (JsonNode) response.getEntity();
|
||||
}
|
||||
}
|
||||
|
||||
private static void assertFailure(JsonNode failure, String name) {
|
||||
assertEquals(name, failure.path("repositoryName").asText());
|
||||
assertEquals("REPOSITORY_NOT_FOUND", failure.path("failureCode").asText());
|
||||
assertTrue(failure.path("failureReason").asText().contains(name));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user