mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -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
@@ -140,6 +140,34 @@ class EcrTest {
|
||||
assertThat(disabledReadback.scanningConfigurations().get(0).scanOnPush()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
@DisplayName("Batch scanning configuration returns valid results alongside missing repositories")
|
||||
void scanningBatchReportsMissingRepositories() {
|
||||
String missing = "floci-it/missing-scan-config";
|
||||
BatchGetRepositoryScanningConfigurationResponse mixed =
|
||||
ecr.batchGetRepositoryScanningConfiguration(b -> b.repositoryNames(missing, REPO_NAME));
|
||||
|
||||
assertThat(mixed.scanningConfigurations()).hasSize(1);
|
||||
RepositoryScanningConfiguration config = mixed.scanningConfigurations().get(0);
|
||||
assertThat(config.repositoryName()).isEqualTo(REPO_NAME);
|
||||
assertThat(config.scanOnPush()).isFalse();
|
||||
assertThat(config.scanFrequency()).isEqualTo(ScanFrequency.MANUAL);
|
||||
assertThat(config.appliedScanFilters()).isEmpty();
|
||||
assertThat(mixed.failures()).hasSize(1);
|
||||
RepositoryScanningConfigurationFailure failure = mixed.failures().get(0);
|
||||
assertThat(failure.repositoryName()).isEqualTo(missing);
|
||||
assertThat(failure.failureCode()).isEqualTo(ScanningConfigurationFailureCode.REPOSITORY_NOT_FOUND);
|
||||
assertThat(failure.failureReason()).contains(missing);
|
||||
|
||||
BatchGetRepositoryScanningConfigurationResponse absent =
|
||||
ecr.batchGetRepositoryScanningConfiguration(b -> b.repositoryNames(missing));
|
||||
assertThat(absent.scanningConfigurations()).isEmpty();
|
||||
assertThat(absent.failures()).hasSize(1);
|
||||
assertThat(absent.failures().get(0).failureCode())
|
||||
.isEqualTo(ScanningConfigurationFailureCode.REPOSITORY_NOT_FOUND);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
@DisplayName("PutLifecyclePolicy round-trips the policy text")
|
||||
|
||||
@@ -64,7 +64,7 @@
|
||||
- Concurrent writes to the policy, replication and scanning configuration, as Terraform makes, each keep their setting.
|
||||
- `GetRegistryPolicy` and `DeleteRegistryPolicy` answer `RegistryPolicyNotFoundException` when no policy is set. `PutRegistryPolicy` refuses text that is not a JSON object.
|
||||
- An empty `rules` list in `PutReplicationConfiguration` clears the configuration. `DescribeRegistry` returns `{"rules": []}` until rules are set.
|
||||
- The scanning configuration is `BASIC` with no rules until one is put. `CONTINUOUS_SCAN` is refused with `BASIC`, since AWS offers it only with enhanced scanning. `BatchGetRepositoryScanningConfiguration` still reports each repository's own `scanOnPush`.
|
||||
- The scanning configuration is `BASIC` with no rules until one is put. `CONTINUOUS_SCAN` is refused with `BASIC`, since AWS offers it only with enhanced scanning. `BatchGetRepositoryScanningConfiguration` still reports each repository's own `scanOnPush`. Missing repository names appear in `failures` with `REPOSITORY_NOT_FOUND`, alongside configurations for the names that exist.
|
||||
- **Manifest format negotiation.** `BatchGetImage` forwards the caller's `acceptedMediaTypes` as the upstream `Accept` header. Modern OCI manifests (`application/vnd.oci.image.manifest.v1+json`) and Docker v2 schema 2 are both supported.
|
||||
- **Cross-account / cross-region isolation.** Internally the registry namespaces repositories as `<account>/<region>/<repoName>`, so the same repository name in different accounts or regions cannot collide.
|
||||
- **Reconcile on first start.** When the registry container starts, Floci queries `GET /v2/_catalog` and recreates `Repository` metadata entries for any namespaces present in the registry but missing from local storage. This means image bytes are never orphaned across restarts.
|
||||
|
||||
@@ -226,20 +226,13 @@ public class EcrJsonHandler {
|
||||
}
|
||||
|
||||
private Response handleBatchGetRepositoryScanningConfiguration(JsonNode request, String region) {
|
||||
// Scanning configuration is not modeled. Resolve names via describeRepositories
|
||||
// and synthesize a wire-accurate RepositoryScanningConfiguration per repo:
|
||||
// scanFrequency derived from scanOnPush (AWS enum: SCAN_ON_PUSH|CONTINUOUS_SCAN|
|
||||
// MANUAL), with empty appliedScanFilters. Note: real AWS reports unknown repos
|
||||
// in the `failures` array; here describeRepositories throws
|
||||
// RepositoryNotFoundException, failing the whole batch instead.
|
||||
// AWS's request has no registryId, so the repositories are the caller's.
|
||||
List<String> names = parseStringList(request.path("repositoryNames"));
|
||||
|
||||
List<Repository> repos = service.describeRepositories(names, null, region);
|
||||
EcrService.BatchScanningConfigurationResult result =
|
||||
service.batchGetRepositoryScanningConfiguration(names, null, region);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode configs = objectMapper.createArrayNode();
|
||||
for (Repository r : repos) {
|
||||
for (Repository r : result.repositories()) {
|
||||
ObjectNode cfg = objectMapper.createObjectNode();
|
||||
cfg.put("repositoryArn", r.getRepositoryArn());
|
||||
cfg.put("repositoryName", r.getRepositoryName());
|
||||
@@ -249,7 +242,15 @@ public class EcrJsonHandler {
|
||||
configs.add(cfg);
|
||||
}
|
||||
response.set("scanningConfigurations", configs);
|
||||
response.set("failures", objectMapper.createArrayNode());
|
||||
ArrayNode failures = objectMapper.createArrayNode();
|
||||
for (EcrService.ScanningConfigurationFailure failure : result.failures()) {
|
||||
ObjectNode entry = objectMapper.createObjectNode();
|
||||
entry.put("repositoryName", failure.repositoryName());
|
||||
entry.put("failureCode", failure.failureCode());
|
||||
entry.put("failureReason", failure.failureReason());
|
||||
failures.add(entry);
|
||||
}
|
||||
response.set("failures", failures);
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
|
||||
@@ -400,6 +400,33 @@ public class EcrService implements ResourceProvider {
|
||||
return out;
|
||||
}
|
||||
|
||||
public record ScanningConfigurationFailure(String repositoryName, String failureCode, String failureReason) {}
|
||||
|
||||
public record BatchScanningConfigurationResult(List<Repository> repositories,
|
||||
List<ScanningConfigurationFailure> failures) {}
|
||||
|
||||
public BatchScanningConfigurationResult batchGetRepositoryScanningConfiguration(
|
||||
List<String> repositoryNames, String registryId, String region) {
|
||||
if (repositoryNames == null || repositoryNames.isEmpty()) {
|
||||
return new BatchScanningConfigurationResult(describeRepositories(repositoryNames, registryId, region),
|
||||
List.of());
|
||||
}
|
||||
|
||||
String account = effectiveAccount(registryId);
|
||||
List<Repository> repositories = new ArrayList<>();
|
||||
List<ScanningConfigurationFailure> failures = new ArrayList<>();
|
||||
for (String name : repositoryNames) {
|
||||
Optional<Repository> repository = repoStore.get(key(region, account, name));
|
||||
if (repository.isPresent()) {
|
||||
repositories.add(refreshRepositoryUri(repository.get(), region));
|
||||
} else {
|
||||
failures.add(new ScanningConfigurationFailure(name, "REPOSITORY_NOT_FOUND",
|
||||
notFound(name, account).getMessage()));
|
||||
}
|
||||
}
|
||||
return new BatchScanningConfigurationResult(repositories, failures);
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// DeleteRepository
|
||||
// ============================================================
|
||||
|
||||
@@ -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