mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(ecr): registry policy, replication and scanning configuration (#5817)
* feat(ecr): registry policy, replication and scanning configuration PutRegistryPolicy, GetRegistryPolicy, DeleteRegistryPolicy, PutReplicationConfiguration, DescribeRegistry, PutRegistryScanningConfiguration and GetRegistryScanningConfiguration store the registry's own settings per account and Region and return them as written. Floci does not replicate or scan images or evaluate the policy, as with repository policies. - The policy must be a JSON object; a missing one is RegistryPolicyNotFoundException. - Replication destinations, filters and rule counts are checked against the service model, and an empty rules list clears the configuration. - Scanning defaults to BASIC with no rules, and CONTINUOUS_SCAN is refused with BASIC. Closes #5803 * fix(ecr): put the registry setting writes under one lock PutRegistryPolicy, DeleteRegistryPolicy, PutReplicationConfiguration and PutRegistryScanningConfiguration read the registry's one settings record, change a field and write it back. Terraform applies the three resources in parallel, so two writers could start from the same record and the later write dropped the other's setting. The read, change and write now happen under a lock per account and Region. * refactor(ecr): check registry setting requests against the vendored model The Resource Groups request walker moves to core.common as RequestConstraintCheck, so a service can check a JSON request against botocore's constraints: member types, required members, lengths and counts, number ranges, patterns and enums, each answered in the service's own words. Resource Groups keeps its BadRequestException messages. - tools/aws/request_shapes.py gains build_constraints, which regen_resource_groups_shapes.py now calls; its vendored file is unchanged. - tools/aws/regen_ecr_shapes.py vendors ecr-request-shapes.json for the seven registry setting actions, and aws-data-sync and aws-data-check run it. - EcrJsonHandler checks those requests first, answering InvalidParameterException in ECR's form. The hand-written checks the model states are gone; a policy that is not a JSON object and CONTINUOUS_SCAN with BASIC stay, as the model cannot say them.
This commit is contained in:
1 parent
c4a2c0a4f6
commit
1f96bf42f1
20 files changed
+1493
-116
No files matched your search
@@ -0,0 +1,110 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/** Every constraint kind the walker reads, with the arguments it hands a service's messages. */
|
||||
class RequestConstraintCheckTest {
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
private static final String MODEL = """
|
||||
{"operations": {"Put": "PutInput"},
|
||||
"shapes": {
|
||||
"PutInput": {"type": "structure", "required": ["name"],
|
||||
"members": {"name": "Name", "items": "Items", "count": "Count", "labels": "Labels",
|
||||
"kind": "Kind"}},
|
||||
"Name": {"type": "string", "min": 2, "max": 5, "pattern": "[a-z]+"},
|
||||
"Items": {"type": "list", "max": 2, "member": "Item"},
|
||||
"Item": {"type": "structure", "required": ["id"], "members": {"id": "Name"}},
|
||||
"Count": {"type": "integer", "min": 1, "max": 10},
|
||||
"Labels": {"type": "map", "key": "Name", "value": "Name"},
|
||||
"Kind": {"type": "string", "enum": ["A", "B"]}
|
||||
}}
|
||||
""";
|
||||
|
||||
/** Records each violation as one line naming its kind and arguments. */
|
||||
private static final RequestConstraintCheck CHECK = RequestConstraintCheck.of(json(MODEL),
|
||||
new RequestConstraintCheck.Messages() {
|
||||
@Override
|
||||
public AwsException type(String path, String type) {
|
||||
return fail("type " + path + " " + type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AwsException bounds(String path, String type, Long min, Long max, long size) {
|
||||
return fail("bounds " + path + " " + type + " " + min + ".." + max + " " + size);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AwsException pattern(String path, String pattern) {
|
||||
return fail("pattern " + path + " " + pattern);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AwsException enumValue(String path, List<String> values) {
|
||||
return fail("enum " + path + " " + values);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AwsException required(String path) {
|
||||
return fail("required " + path);
|
||||
}
|
||||
|
||||
private AwsException fail(String message) {
|
||||
return new AwsException("Test", message, 400);
|
||||
}
|
||||
});
|
||||
|
||||
private static JsonNode json(String text) {
|
||||
try {
|
||||
return MAPPER.readTree(text);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException(text, e);
|
||||
}
|
||||
}
|
||||
|
||||
private static String violation(String body) {
|
||||
return assertThrows(AwsException.class, () -> CHECK.check("Put", json(body)), body).getMessage();
|
||||
}
|
||||
|
||||
@Test
|
||||
void eachConstraintKindIsReportedWithItsPath() {
|
||||
assertEquals("required Put.name", violation("{}"));
|
||||
assertEquals("type Put.name string", violation("{\"name\":7}"));
|
||||
assertEquals("bounds Put.name string 2..5 1", violation("{\"name\":\"a\"}"));
|
||||
assertEquals("bounds Put.name string 2..5 6", violation("{\"name\":\"abcdef\"}"));
|
||||
assertEquals("pattern Put.name [a-z]+", violation("{\"name\":\"AB\"}"));
|
||||
assertEquals("bounds Put.count integer 1..10 11", violation("{\"name\":\"ab\",\"count\":11}"));
|
||||
assertEquals("type Put.count integer", violation("{\"name\":\"ab\",\"count\":1.5}"));
|
||||
assertEquals("enum Put.kind [A, B]", violation("{\"name\":\"ab\",\"kind\":\"C\"}"));
|
||||
assertEquals("bounds Put.items list null..2 3",
|
||||
violation("{\"name\":\"ab\",\"items\":[{\"id\":\"ab\"},{\"id\":\"ab\"},{\"id\":\"ab\"}]}"));
|
||||
assertEquals("required Put.items[].id", violation("{\"name\":\"ab\",\"items\":[{}]}"));
|
||||
assertEquals("pattern Put.items[].id [a-z]+", violation("{\"name\":\"ab\",\"items\":[{\"id\":\"A1\"}]}"));
|
||||
assertEquals("pattern Put.labels.key [a-z]+", violation("{\"name\":\"ab\",\"labels\":{\"K1\":\"ab\"}}"));
|
||||
assertEquals("bounds Put.labels.ab string 2..5 1", violation("{\"name\":\"ab\",\"labels\":{\"ab\":\"x\"}}"));
|
||||
assertEquals("type Put structure", violation("[]"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRequestWithinTheConstraintsPasses() {
|
||||
assertDoesNotThrow(() -> CHECK.check("Put", json("{\"name\":\"abc\",\"items\":[{\"id\":\"ab\"}],"
|
||||
+ "\"count\":10,\"labels\":{\"ab\":\"cd\"},\"kind\":\"B\",\"unmodeled\":true,\"items2\":null}")));
|
||||
// A null member is treated as absent, as the walker found it before it moved here.
|
||||
assertDoesNotThrow(() -> CHECK.check("Put", json("{\"name\":\"abc\",\"count\":null}")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aStringIsMeasuredInCodePoints() {
|
||||
// One emoji is two UTF-16 units but one code point, so it is under the minimum of 2.
|
||||
assertEquals("bounds Put.name string 2..5 1", violation("{\"name\":\"\\uD83D\\uDE00\"}"));
|
||||
}
|
||||
}
|
||||
+117
@@ -0,0 +1,117 @@
|
||||
package io.github.hectorvent.floci.services.ecr;
|
||||
|
||||
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 static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
|
||||
/**
|
||||
* The registry's own settings over the JSON protocol: the registry policy, the replication
|
||||
* configuration and the scanning configuration. Each test uses its own Region so they do not
|
||||
* share state.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class EcrRegistrySettingsIntegrationTest {
|
||||
|
||||
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\","
|
||||
+ "\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"arn:aws:iam::111111111111:root\"},"
|
||||
+ "\"Action\":[\"ecr:CreateRepository\",\"ecr:ReplicateImage\"],"
|
||||
+ "\"Resource\":\"arn:aws:ecr:us-east-1:000000000000:repository/*\"}]}";
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@Test
|
||||
void registryPolicyRoundTrips() {
|
||||
String region = "eu-north-1";
|
||||
call("GetRegistryPolicy", "{}", region).statusCode(400)
|
||||
.body("__type", equalTo("RegistryPolicyNotFoundException"));
|
||||
call("PutRegistryPolicy", "{\"policyText\":" + quote(POLICY) + "}", region).statusCode(200)
|
||||
.body("registryId", equalTo("000000000000"))
|
||||
.body("policyText", equalTo(POLICY));
|
||||
call("GetRegistryPolicy", "{}", region).statusCode(200).body("policyText", equalTo(POLICY));
|
||||
call("DeleteRegistryPolicy", "{}", region).statusCode(200).body("policyText", equalTo(POLICY));
|
||||
call("DeleteRegistryPolicy", "{}", region).statusCode(400)
|
||||
.body("__type", equalTo("RegistryPolicyNotFoundException"));
|
||||
call("PutRegistryPolicy", "{\"policyText\":\"not json\"}", region).statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void replicationIsDescribedAndCleared() {
|
||||
String region = "eu-south-1";
|
||||
call("DescribeRegistry", "{}", region).statusCode(200)
|
||||
.body("registryId", equalTo("000000000000"))
|
||||
.body("replicationConfiguration.rules", hasSize(0));
|
||||
|
||||
String rules = "{\"replicationConfiguration\":{\"rules\":[{\"destinations\":[{\"region\":\"us-west-2\","
|
||||
+ "\"registryId\":\"000000000000\"}],\"repositoryFilters\":[{\"filter\":\"prod\","
|
||||
+ "\"filterType\":\"PREFIX_MATCH\"}]},{\"destinations\":[{\"region\":\"eu-west-1\","
|
||||
+ "\"registryId\":\"111111111111\"}]}]}}";
|
||||
call("PutReplicationConfiguration", rules, region).statusCode(200)
|
||||
.body("replicationConfiguration.rules", hasSize(2))
|
||||
.body("replicationConfiguration.rules[0].repositoryFilters[0].filter", equalTo("prod"));
|
||||
call("DescribeRegistry", "{}", region).statusCode(200)
|
||||
.body("replicationConfiguration.rules[0].destinations[0].region", equalTo("us-west-2"))
|
||||
.body("replicationConfiguration.rules[1].destinations[0].registryId", equalTo("111111111111"))
|
||||
.body("replicationConfiguration.rules[1].repositoryFilters", equalTo(null));
|
||||
|
||||
call("PutReplicationConfiguration", "{\"replicationConfiguration\":{\"rules\":[{\"destinations\":"
|
||||
+ "[{\"region\":\"us-west-2\",\"registryId\":\"123\"}]}]}}", region).statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterException"));
|
||||
call("PutReplicationConfiguration", "{}", region).statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterException"));
|
||||
|
||||
call("PutReplicationConfiguration", "{\"replicationConfiguration\":{\"rules\":[]}}", region).statusCode(200)
|
||||
.body("replicationConfiguration.rules", hasSize(0));
|
||||
call("DescribeRegistry", "{}", region).statusCode(200).body("replicationConfiguration.rules", hasSize(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void scanningConfigurationRoundTrips() {
|
||||
String region = "ap-south-2";
|
||||
call("GetRegistryScanningConfiguration", "{}", region).statusCode(200)
|
||||
.body("registryId", equalTo("000000000000"))
|
||||
.body("scanningConfiguration.scanType", equalTo("BASIC"))
|
||||
.body("scanningConfiguration.rules", hasSize(0));
|
||||
|
||||
call("PutRegistryScanningConfiguration", "{\"scanType\":\"ENHANCED\",\"rules\":[{\"scanFrequency\":"
|
||||
+ "\"CONTINUOUS_SCAN\",\"repositoryFilters\":[{\"filter\":\"prod-*\",\"filterType\":\"WILDCARD\"}]}]}",
|
||||
region).statusCode(200)
|
||||
.body("registryScanningConfiguration.scanType", equalTo("ENHANCED"))
|
||||
.body("registryScanningConfiguration.rules[0].scanFrequency", equalTo("CONTINUOUS_SCAN"));
|
||||
call("GetRegistryScanningConfiguration", "{}", region).statusCode(200)
|
||||
.body("scanningConfiguration.scanType", equalTo("ENHANCED"))
|
||||
.body("scanningConfiguration.rules[0].repositoryFilters[0].filter", equalTo("prod-*"));
|
||||
|
||||
call("PutRegistryScanningConfiguration", "{\"scanType\":\"BASIC\",\"rules\":[{\"scanFrequency\":"
|
||||
+ "\"CONTINUOUS_SCAN\",\"repositoryFilters\":[{\"filter\":\"*\",\"filterType\":\"WILDCARD\"}]}]}",
|
||||
region).statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterException"));
|
||||
}
|
||||
|
||||
private static String quote(String text) {
|
||||
return "\"" + text.replace("\\", "\\\\").replace("\"", "\\\"") + "\"";
|
||||
}
|
||||
|
||||
private static ValidatableResponse call(String action, String body, String region) {
|
||||
return given()
|
||||
.header("X-Amz-Target", PREFIX + action)
|
||||
.header("Authorization", "AWS4-HMAC-SHA256 Credential=000000000000/20261016/" + region
|
||||
+ "/ecr/aws4_request, SignedHeaders=host, Signature=abc")
|
||||
.contentType(CT)
|
||||
.body(body)
|
||||
.when()
|
||||
.post("/")
|
||||
.then();
|
||||
}
|
||||
}
|
||||
@@ -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.core.common.AwsException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/** The registry setting actions checked against the vendored ECR model constraints. */
|
||||
class EcrRequestShapesTest {
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
private static final String DESTINATION = "{\"region\":\"us-west-2\",\"registryId\":\"123456789012\"}";
|
||||
|
||||
private static JsonNode json(String body) {
|
||||
try {
|
||||
return MAPPER.readTree(body);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException(body, e);
|
||||
}
|
||||
}
|
||||
|
||||
private static String refusal(String operation, String body) {
|
||||
AwsException e = assertThrows(AwsException.class, () -> EcrRequestShapes.check(operation, json(body)), body);
|
||||
assertEquals("InvalidParameterException", e.getErrorCode());
|
||||
assertEquals(400, e.getHttpStatus());
|
||||
return e.getMessage();
|
||||
}
|
||||
|
||||
private static String replication(String rules) {
|
||||
return "{\"replicationConfiguration\":{\"rules\":[" + rules + "]}}";
|
||||
}
|
||||
|
||||
private static String rule(String destinations, String filters) {
|
||||
return "{\"destinations\":[" + destinations + "]" + (filters == null ? "" : ",\"repositoryFilters\":[" + filters + "]")
|
||||
+ "}";
|
||||
}
|
||||
|
||||
@Test
|
||||
void registryPolicyLengthAndPresence() {
|
||||
assertEquals("Invalid parameter at 'policyText' failed to satisfy constraint: "
|
||||
+ "'Member must have length less than or equal to 10240'",
|
||||
refusal("PutRegistryPolicy", "{\"policyText\":\"" + "x".repeat(10241) + "\"}"));
|
||||
assertEquals("Invalid parameter at 'policyText' failed to satisfy constraint: 'Member must not be null'",
|
||||
refusal("PutRegistryPolicy", "{}"));
|
||||
assertDoesNotThrow(() -> EcrRequestShapes.check("PutRegistryPolicy",
|
||||
json("{\"policyText\":\"" + "x".repeat(10240) + "\"}")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void replicationRulesDestinationsAndFilters() {
|
||||
assertEquals("Invalid parameter at 'replicationConfiguration.rules.destinations.region' failed to satisfy "
|
||||
+ "constraint: 'must satisfy regular expression '[0-9a-z-]{2,25}''",
|
||||
refusal("PutReplicationConfiguration", replication(rule(
|
||||
"{\"region\":\"US_WEST\",\"registryId\":\"123456789012\"}", null))));
|
||||
assertEquals("Invalid parameter at 'replicationConfiguration.rules.destinations.registryId' failed to satisfy "
|
||||
+ "constraint: 'must satisfy regular expression '[0-9]{12}''",
|
||||
refusal("PutReplicationConfiguration", replication(rule(
|
||||
"{\"region\":\"us-west-2\",\"registryId\":\"12345\"}", null))));
|
||||
assertEquals("Invalid parameter at 'replicationConfiguration.rules.repositoryFilters.filterType' failed to "
|
||||
+ "satisfy constraint: 'Member must satisfy enum value set: [PREFIX_MATCH]'",
|
||||
refusal("PutReplicationConfiguration", replication(rule(DESTINATION,
|
||||
"{\"filter\":\"prod\",\"filterType\":\"WILDCARD\"}"))));
|
||||
assertEquals("Invalid parameter at 'replicationConfiguration.rules.repositoryFilters' failed to satisfy "
|
||||
+ "constraint: 'Member must have length greater than or equal to 1'",
|
||||
refusal("PutReplicationConfiguration", replication(rule(DESTINATION, ""))));
|
||||
assertEquals("Invalid parameter at 'replicationConfiguration.rules' failed to satisfy constraint: "
|
||||
+ "'Member must have length less than or equal to 25'",
|
||||
refusal("PutReplicationConfiguration",
|
||||
replication(String.join(",", Collections.nCopies(26, rule(DESTINATION, null))))));
|
||||
assertEquals("Invalid parameter at 'replicationConfiguration.rules.destinations' failed to satisfy "
|
||||
+ "constraint: 'Member must not be null'",
|
||||
refusal("PutReplicationConfiguration", replication("{}")));
|
||||
assertEquals("Invalid parameter at 'replicationConfiguration' failed to satisfy constraint: "
|
||||
+ "'Member must not be null'",
|
||||
refusal("PutReplicationConfiguration", "{}"));
|
||||
refusal("PutReplicationConfiguration", replication(rule(DESTINATION,
|
||||
"{\"filter\":\"Prod\",\"filterType\":\"PREFIX_MATCH\"}")));
|
||||
refusal("PutReplicationConfiguration", replication(rule(DESTINATION,
|
||||
"{\"filter\":\"p\",\"filterType\":\"PREFIX_MATCH\"}")));
|
||||
refusal("PutReplicationConfiguration", replication(rule(
|
||||
String.join(",", Collections.nCopies(101, DESTINATION)), null)));
|
||||
|
||||
assertDoesNotThrow(() -> EcrRequestShapes.check("PutReplicationConfiguration",
|
||||
json(replication(String.join(",", Collections.nCopies(25, rule(DESTINATION,
|
||||
"{\"filter\":\"prod/api\",\"filterType\":\"PREFIX_MATCH\"}")))))));
|
||||
// An empty list is how a configuration is cleared.
|
||||
assertDoesNotThrow(() -> EcrRequestShapes.check("PutReplicationConfiguration", json(replication(""))));
|
||||
}
|
||||
|
||||
@Test
|
||||
void scanningTypeFrequencyAndFilters() {
|
||||
assertEquals("Invalid parameter at 'scanType' failed to satisfy constraint: "
|
||||
+ "'Member must satisfy enum value set: [BASIC, ENHANCED]'",
|
||||
refusal("PutRegistryScanningConfiguration", "{\"scanType\":\"FULL\"}"));
|
||||
assertEquals("Invalid parameter at 'rules.scanFrequency' failed to satisfy constraint: "
|
||||
+ "'Member must satisfy enum value set: [SCAN_ON_PUSH, CONTINUOUS_SCAN, MANUAL]'",
|
||||
refusal("PutRegistryScanningConfiguration", "{\"scanType\":\"ENHANCED\",\"rules\":[{\"scanFrequency\":"
|
||||
+ "\"HOURLY\",\"repositoryFilters\":[{\"filter\":\"*\",\"filterType\":\"WILDCARD\"}]}]}"));
|
||||
assertEquals("Invalid parameter at 'rules.repositoryFilters.filterType' failed to satisfy constraint: "
|
||||
+ "'Member must satisfy enum value set: [WILDCARD]'",
|
||||
refusal("PutRegistryScanningConfiguration", "{\"rules\":[{\"scanFrequency\":\"SCAN_ON_PUSH\","
|
||||
+ "\"repositoryFilters\":[{\"filter\":\"*\",\"filterType\":\"PREFIX_MATCH\"}]}]}"));
|
||||
refusal("PutRegistryScanningConfiguration", "{\"rules\":[{\"scanFrequency\":\"SCAN_ON_PUSH\","
|
||||
+ "\"repositoryFilters\":[{\"filter\":\"Bad Filter\",\"filterType\":\"WILDCARD\"}]}]}");
|
||||
refusal("PutRegistryScanningConfiguration", "{\"rules\":[{\"scanFrequency\":\"SCAN_ON_PUSH\"}]}");
|
||||
String scanRule = "{\"scanFrequency\":\"SCAN_ON_PUSH\",\"repositoryFilters\":[{\"filter\":\"*\","
|
||||
+ "\"filterType\":\"WILDCARD\"}]}";
|
||||
assertEquals("Invalid parameter at 'rules' failed to satisfy constraint: "
|
||||
+ "'Member must have length less than or equal to 2'",
|
||||
refusal("PutRegistryScanningConfiguration",
|
||||
"{\"rules\":[" + String.join(",", Collections.nCopies(3, scanRule)) + "]}"));
|
||||
assertEquals("Invalid parameter at 'scanType' failed to satisfy constraint: 'Member must be a string'",
|
||||
refusal("PutRegistryScanningConfiguration", "{\"scanType\":1}"));
|
||||
|
||||
assertDoesNotThrow(() -> EcrRequestShapes.check("PutRegistryScanningConfiguration",
|
||||
json("{\"scanType\":\"ENHANCED\",\"rules\":[" + scanRule + "," + scanRule + "]}")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void readsTakeAnEmptyBody() {
|
||||
for (String operation : new String[] {"GetRegistryPolicy", "DeleteRegistryPolicy", "DescribeRegistry",
|
||||
"GetRegistryScanningConfiguration"}) {
|
||||
assertDoesNotThrow(() -> EcrRequestShapes.check(operation, json("{}")), operation);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import io.github.hectorvent.floci.services.ecr.model.ImageDetail;
|
||||
import io.github.hectorvent.floci.services.ecr.model.ImageIdentifier;
|
||||
import io.github.hectorvent.floci.services.ecr.model.ImageMetadata;
|
||||
import io.github.hectorvent.floci.services.ecr.model.PullThroughCacheRule;
|
||||
import io.github.hectorvent.floci.services.ecr.model.RegistrySettings;
|
||||
import io.github.hectorvent.floci.services.ecr.model.Repository;
|
||||
import io.github.hectorvent.floci.services.ecr.registry.EcrRegistryManager;
|
||||
import io.github.hectorvent.floci.services.ecr.registry.RegistryHttpClient;
|
||||
@@ -28,6 +29,11 @@ import java.util.Base64;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CyclicBarrier;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@@ -1328,4 +1334,135 @@ class EcrServiceTest {
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------
|
||||
// Registry settings
|
||||
// ------------------------------------------------------------
|
||||
|
||||
private static final String REGISTRY_POLICY = "{\"Version\":\"2012-10-17\",\"Statement\":[]}";
|
||||
|
||||
private static RegistrySettings.ReplicationRule replicateTo(String region, String registryId, String prefix) {
|
||||
return new RegistrySettings.ReplicationRule(
|
||||
List.of(new RegistrySettings.ReplicationDestination(region, registryId)),
|
||||
prefix == null ? null : List.of(new RegistrySettings.RepositoryFilter(prefix, "PREFIX_MATCH")));
|
||||
}
|
||||
|
||||
private static RegistrySettings.ScanningRule scan(String frequency, String filter) {
|
||||
return new RegistrySettings.ScanningRule(frequency,
|
||||
List.of(new RegistrySettings.RepositoryFilter(filter, "WILDCARD")));
|
||||
}
|
||||
|
||||
private static void assertInvalid(Runnable call) {
|
||||
assertEquals("InvalidParameterException", assertThrows(AwsException.class, call::run).getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void registryPolicy_roundTripsAndIsNotFoundOnceDeleted() {
|
||||
AwsException missing = assertThrows(AwsException.class, () -> service.getRegistryPolicy(REGION));
|
||||
assertEquals("RegistryPolicyNotFoundException", missing.getErrorCode());
|
||||
|
||||
service.putRegistryPolicy(REGISTRY_POLICY, REGION);
|
||||
assertEquals(REGISTRY_POLICY, service.getRegistryPolicy(REGION));
|
||||
assertEquals(REGISTRY_POLICY, service.deleteRegistryPolicy(REGION));
|
||||
|
||||
assertEquals("RegistryPolicyNotFoundException",
|
||||
assertThrows(AwsException.class, () -> service.getRegistryPolicy(REGION)).getErrorCode());
|
||||
assertEquals("RegistryPolicyNotFoundException",
|
||||
assertThrows(AwsException.class, () -> service.deleteRegistryPolicy(REGION)).getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void registryPolicy_mustBeAJsonObject() {
|
||||
assertInvalid(() -> service.putRegistryPolicy("not json", REGION));
|
||||
assertInvalid(() -> service.putRegistryPolicy("[]", REGION));
|
||||
assertInvalid(() -> service.putRegistryPolicy("", REGION));
|
||||
}
|
||||
|
||||
@Test
|
||||
void replication_roundTripsAndAnEmptyListClearsIt() {
|
||||
assertTrue(service.replicationRules(REGION).isEmpty());
|
||||
|
||||
List<RegistrySettings.ReplicationRule> rules = List.of(
|
||||
replicateTo("us-west-2", ACCOUNT, "prod"),
|
||||
replicateTo("eu-west-1", "111111111111", null));
|
||||
assertEquals(rules, service.putReplicationConfiguration(rules, REGION));
|
||||
assertEquals(rules, service.replicationRules(REGION));
|
||||
|
||||
assertTrue(service.putReplicationConfiguration(List.of(), REGION).isEmpty());
|
||||
assertTrue(service.replicationRules(REGION).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void scanning_defaultsToBasicAndRoundTrips() {
|
||||
RegistrySettings initial = service.registryScanningConfiguration(REGION);
|
||||
assertEquals("BASIC", initial.getScanType());
|
||||
assertTrue(initial.getScanningRules().isEmpty());
|
||||
|
||||
List<RegistrySettings.ScanningRule> rules = List.of(scan("CONTINUOUS_SCAN", "prod-*"), scan("SCAN_ON_PUSH", "*"));
|
||||
RegistrySettings saved = service.putRegistryScanningConfiguration("ENHANCED", rules, REGION);
|
||||
assertEquals("ENHANCED", saved.getScanType());
|
||||
assertEquals(rules, service.registryScanningConfiguration(REGION).getScanningRules());
|
||||
}
|
||||
|
||||
@Test
|
||||
void scanning_refusesContinuousScanWithBasic() {
|
||||
assertInvalid(() -> service.putRegistryScanningConfiguration(
|
||||
"BASIC", List.of(scan("CONTINUOUS_SCAN", "*")), REGION));
|
||||
assertInvalid(() -> service.putRegistryScanningConfiguration(
|
||||
null, List.of(scan("CONTINUOUS_SCAN", "*")), REGION));
|
||||
assertDoesNotThrow(() -> service.putRegistryScanningConfiguration(
|
||||
"ENHANCED", List.of(scan("CONTINUOUS_SCAN", "*")), REGION));
|
||||
}
|
||||
|
||||
@Test
|
||||
void registrySettings_areKeptPerAccountAndRegion() {
|
||||
InMemoryStorage<String, RegistrySettings> store = new InMemoryStorage<>();
|
||||
EcrService first = new EcrService(new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
store, registryManager, Mockito.mock(EmulatorConfig.class), new RegionResolver(REGION, ACCOUNT));
|
||||
EcrService other = new EcrService(new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
store, registryManager, Mockito.mock(EmulatorConfig.class),
|
||||
new RegionResolver(REGION, "111111111111"));
|
||||
|
||||
first.putRegistryPolicy(REGISTRY_POLICY, REGION);
|
||||
assertEquals(REGISTRY_POLICY, first.getRegistryPolicy(REGION));
|
||||
assertThrows(AwsException.class, () -> first.getRegistryPolicy("us-west-2"));
|
||||
assertThrows(AwsException.class, () -> other.getRegistryPolicy(REGION));
|
||||
assertEquals("111111111111", other.registryId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Terraform puts the policy, replication and scanning configuration in parallel. Each round
|
||||
* starts the three puts together on a registry with no settings yet, and all three must stay.
|
||||
*/
|
||||
@Test
|
||||
void registrySettings_parallelPutsAllSurvive() throws Exception {
|
||||
EcrService parallel = new EcrService(new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), registryManager, Mockito.mock(EmulatorConfig.class),
|
||||
new RegionResolver(REGION, ACCOUNT));
|
||||
List<RegistrySettings.ReplicationRule> rules = List.of(replicateTo("eu-west-1", ACCOUNT, null));
|
||||
ExecutorService pool = Executors.newFixedThreadPool(3);
|
||||
try {
|
||||
for (int round = 0; round < 300; round++) {
|
||||
String region = "round-" + round;
|
||||
CyclicBarrier start = new CyclicBarrier(3);
|
||||
List<Runnable> puts = List.of(
|
||||
() -> parallel.putRegistryPolicy(REGISTRY_POLICY, region),
|
||||
() -> parallel.putReplicationConfiguration(rules, region),
|
||||
() -> parallel.putRegistryScanningConfiguration("ENHANCED", List.of(), region));
|
||||
CompletableFuture.allOf(puts.stream().map(put -> CompletableFuture.runAsync(() -> {
|
||||
try {
|
||||
start.await(5, TimeUnit.SECONDS);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
put.run();
|
||||
}, pool)).toArray(CompletableFuture[]::new)).get(10, TimeUnit.SECONDS);
|
||||
|
||||
assertEquals(REGISTRY_POLICY, parallel.getRegistryPolicy(region), region);
|
||||
assertEquals(rules, parallel.replicationRules(region), region);
|
||||
assertEquals("ENHANCED", parallel.registryScanningConfiguration(region).getScanType(), region);
|
||||
}
|
||||
} finally {
|
||||
pool.shutdownNow();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user