Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

feat(ecr): registry policy, replication and scanning configuration (#5817)

David1f96bf42f1fe
diff --git a/Makefile b/Makefile
index 30008e70ff..8076514a26 100644
--- a/Makefile
+++ b/Makefile
@@ -21,6 +21,8 @@
# by tools/aws/regen_sesv2_shapes.py.
# src/main/resources/aws/ecs-request-shapes.json is generated from botocore's ECS model
# by tools/aws/regen_ecs_shapes.py.
+# src/main/resources/aws/ecr-request-shapes.json, the ECR request constraints, is generated
+# from botocore's ECR model by tools/aws/regen_ecr_shapes.py.
# src/test/resources/aws/rest-routes.json, the REST routes IamRestRouteCoverageTest checks, is
# generated from botocore's models by tools/aws/regen_rest_routes.py.
@@ -216,6 +218,7 @@ aws-data-sync: ## Regenerate src/main/resources/aws/*.json and the IAM managed-p
$(PYTHON) tools/aws/regen_dynamodb_shapes.py
$(PYTHON) tools/aws/regen_redshift_serverless_shapes.py
$(PYTHON) tools/aws/regen_resource_groups_shapes.py
+ $(PYTHON) tools/aws/regen_ecr_shapes.py
$(PYTHON) tools/aws/regen_apigateway_types.py
$(PYTHON) tools/aws/regen_sesv2_shapes.py
$(PYTHON) tools/aws/regen_ecs_shapes.py
@@ -252,6 +255,12 @@ aws-data-check: ## CI gate: the vendored AWS data must match a fresh generation
echo "error: Resource Groups request shapes are out of date; run 'make aws-data-sync'."; \
exit 1; \
}
+ @$(PYTHON) tools/aws/regen_ecr_shapes.py --check || { \
+ echo ""; \
+ echo "error: src/main/resources/aws/ecr-request-shapes.json is out of date."; \
+ echo " Run 'make aws-data-sync' and commit the result."; \
+ exit 1; \
+ }
@$(PYTHON) tools/aws/regen_apigateway_types.py --check || { \
echo "error: API Gateway integration types are out of date; run 'make aws-data-sync'."; \
exit 1; \
diff --git a/compatibility-tests/sdk-test-awscli/test/ecr.bats b/compatibility-tests/sdk-test-awscli/test/ecr.bats
index 6422e98850..2ba5b09e76 100644
--- a/compatibility-tests/sdk-test-awscli/test/ecr.bats
+++ b/compatibility-tests/sdk-test-awscli/test/ecr.bats
@@ -140,3 +140,33 @@ teardown() {
assert_success
[ "$(json_get "$output" '.imageDetails[0].imageDigest')" = "$digest" ]
}
+
+@test "ECR: registry policy, replication and scanning configuration round-trip" {
+ policy='{"Version":"2012-10-17","Statement":[{"Sid":"Replicate","Effect":"Allow","Principal":{"AWS":"arn:aws:iam::111111111111:root"},"Action":["ecr:CreateRepository","ecr:ReplicateImage"],"Resource":"*"}]}'
+ run aws_cmd ecr put-registry-policy --policy-text "$policy"
+ assert_success
+ run aws_cmd ecr get-registry-policy
+ assert_success
+ [ "$(json_get "$output" '.policyText')" = "$policy" ]
+
+ run aws_cmd ecr put-replication-configuration --replication-configuration \
+ 'rules=[{destinations=[{region=us-west-2,registryId=111111111111}],repositoryFilters=[{filter=prod,filterType=PREFIX_MATCH}]}]'
+ assert_success
+ run aws_cmd ecr describe-registry
+ assert_success
+ [ "$(json_get "$output" '.replicationConfiguration.rules[0].destinations[0].region')" = "us-west-2" ]
+
+ run aws_cmd ecr put-registry-scanning-configuration --scan-type ENHANCED \
+ --rules '[{"scanFrequency":"SCAN_ON_PUSH","repositoryFilters":[{"filter":"*","filterType":"WILDCARD"}]}]'
+ assert_success
+ run aws_cmd ecr get-registry-scanning-configuration
+ assert_success
+ [ "$(json_get "$output" '.scanningConfiguration.scanType')" = "ENHANCED" ]
+
+ aws_cmd ecr delete-registry-policy >/dev/null
+ aws_cmd ecr put-replication-configuration --replication-configuration 'rules=[]' >/dev/null
+ aws_cmd ecr put-registry-scanning-configuration --scan-type BASIC --rules '[]' >/dev/null
+ run aws_cmd ecr get-registry-policy
+ assert_failure
+ [[ "$output" == *"RegistryPolicyNotFoundException"* ]]
+}
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/EcrTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/EcrTest.java
index 2f77cc676b..cc4f6ce2c4 100644
--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/EcrTest.java
+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/EcrTest.java
@@ -296,4 +296,54 @@ class EcrTest {
private static String sha256(byte[] bytes) throws Exception {
return "sha256:" + HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(bytes));
}
+
+ @Test
+ @Order(14)
+ @DisplayName("Registry policy, replication and scanning configuration round-trip")
+ void registrySettingsRoundTrip() {
+ String policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"Replicate\",\"Effect\":\"Allow\","
+ + "\"Principal\":{\"AWS\":\"arn:" + TestFixtures.partition() + ":iam::111111111111:root\"},"
+ + "\"Action\":[\"ecr:CreateRepository\",\"ecr:ReplicateImage\"],\"Resource\":\"*\"}]}";
+ try {
+ assertThatThrownBy(() -> ecr.getRegistryPolicy(b -> {}))
+ .isInstanceOf(RegistryPolicyNotFoundException.class);
+ ecr.putRegistryPolicy(b -> b.policyText(policy));
+ assertThat(ecr.getRegistryPolicy(b -> {}).policyText()).isEqualTo(policy);
+
+ ecr.putReplicationConfiguration(b -> b.replicationConfiguration(c -> c.rules(ReplicationRule.builder()
+ .destinations(ReplicationDestination.builder().region("us-west-2").registryId("111111111111").build())
+ .repositoryFilters(RepositoryFilter.builder().filter("prod").filterType("PREFIX_MATCH").build())
+ .build())));
+ DescribeRegistryResponse registry = ecr.describeRegistry(b -> {});
+ assertThat(registry.registryId()).matches("[0-9]{12}");
+ assertThat(registry.replicationConfiguration().rules()).hasSize(1);
+ assertThat(registry.replicationConfiguration().rules().get(0).destinations().get(0).region())
+ .isEqualTo("us-west-2");
+
+ ecr.putRegistryScanningConfiguration(b -> b.scanType(ScanType.ENHANCED).rules(RegistryScanningRule.builder()
+ .scanFrequency(ScanFrequency.CONTINUOUS_SCAN)
+ .repositoryFilters(ScanningRepositoryFilter.builder().filter("prod-*")
+ .filterType(ScanningRepositoryFilterType.WILDCARD).build())
+ .build()));
+ GetRegistryScanningConfigurationResponse scanning = ecr.getRegistryScanningConfiguration(b -> {});
+ assertThat(scanning.scanningConfiguration().scanType()).isEqualTo(ScanType.ENHANCED);
+ assertThat(scanning.scanningConfiguration().rules().get(0).scanFrequency())
+ .isEqualTo(ScanFrequency.CONTINUOUS_SCAN);
+ assertThatThrownBy(() -> ecr.putRegistryScanningConfiguration(b -> b.scanType(ScanType.BASIC)
+ .rules(RegistryScanningRule.builder().scanFrequency(ScanFrequency.CONTINUOUS_SCAN)
+ .repositoryFilters(ScanningRepositoryFilter.builder().filter("*")
+ .filterType(ScanningRepositoryFilterType.WILDCARD).build())
+ .build())))
+ .isInstanceOf(InvalidParameterException.class);
+ } finally {
+ // Registry settings are per account and Region, so put back what a fresh registry has.
+ try {
+ ecr.deleteRegistryPolicy(b -> {});
+ } catch (RegistryPolicyNotFoundException ignored) {
+ // Already absent.
+ }
+ ecr.putReplicationConfiguration(b -> b.replicationConfiguration(c -> c.rules(List.of())));
+ ecr.putRegistryScanningConfiguration(b -> b.scanType(ScanType.BASIC).rules(List.of()));
+ }
+ }
}
diff --git a/docs/services/ecr.md b/docs/services/ecr.md
index c8423059a9..85913cdf6f 100644
--- a/docs/services/ecr.md
+++ b/docs/services/ecr.md
@@ -33,6 +33,9 @@
| `TagResource` / `UntagResource` / `ListTagsForResource` | Resource tagging |
| `PutLifecyclePolicy` / `GetLifecyclePolicy` / `DeleteLifecyclePolicy` | Lifecycle policy round-trip (stored, not enforced) |
| `SetRepositoryPolicy` / `GetRepositoryPolicy` / `DeleteRepositoryPolicy` | Repository policy round-trip (stored, not enforced) |
+| `PutRegistryPolicy` / `GetRegistryPolicy` / `DeleteRegistryPolicy` | Registry policy round-trip (stored, not enforced) |
+| `PutReplicationConfiguration` / `DescribeRegistry` | Store the registry's replication rules and return them with the registry ID (stored, not acted on) |
+| `PutRegistryScanningConfiguration` / `GetRegistryScanningConfiguration` | Store the registry's scan type and scanning rules (stored, not acted on) |
### Admin Endpoints
@@ -56,6 +59,12 @@
- `BatchCheckLayerAvailability` lists held layers as `AVAILABLE`, and a layer the repository does not hold under `failures` as `MissingLayerDigest`.
- **Authorization.** `GetAuthorizationToken` returns `Base64("AWS:floci")` plus a proxy endpoint. The backing `registry:2` runs without auth, so any `aws ecr get-login-password | docker login` succeeds.
- **Pull through cache rule configuration.** Pull-through cache rule actions persist AWS-compatible rule metadata with account and Region isolation. `UpdatePullThroughCacheRule` preserves fields omitted by the request and refreshes `updatedAt`. `ValidatePullThroughCacheRule` checks that the rule exists and reports it as valid; Floci does not contact the upstream registry or validate credentials. Rules do not proxy or cache images from the configured upstream registry.
+- **Registry settings.** The registry policy, replication configuration and scanning configuration are kept per account and Region and returned as written. Floci does not replicate or scan images, or evaluate the registry policy.
+ - Each request is checked against the constraints of the ECR service model, vendored in `src/main/resources/aws/ecr-request-shapes.json` by `tools/aws/regen_ecr_shapes.py`: member types, required members, lengths and counts, patterns and enum values. A request outside them is `InvalidParameterException` in ECR's form, `Invalid parameter at '<member>' failed to satisfy constraint: '<rule>'`.
+ - 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`.
- **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.
@@ -251,7 +260,7 @@ new lambda.DockerImageFunction(this, 'MyFn', {
The following ECR features are **not** implemented. Stored values for policies and lifecycle rules round-trip via the API but are not enforced at runtime:
-- Replication
+- Replication and registry scanning (the configurations are stored, but images are not copied or scanned)
- Pull-through cache image proxying and population. Rule configuration APIs are supported, but pulls are not routed to upstream registries
- Image scanning (`StartImageScan`, `DescribeImageScanFindings`)
- Image signing and notary attachments
diff --git a/docs/services/index.md b/docs/services/index.md
index 173f0fe81c..a0cd03fbaa 100644
--- a/docs/services/index.md
+++ b/docs/services/index.md
@@ -86,7 +86,7 @@ Operation counts are exact. For dispatch-table services (Query and JSON 1.1) eac
| [EC2](ec2.md) | `POST /` with `Action=` param | EC2 Query | 198 |
| [Lightsail](lightsail.md) | `POST /` + `X-Amz-Target: Lightsail_20161128.*` | JSON 1.1 | 79 local responses; 161 recognized actions |
| [ACM](acm.md) | `POST /` + `X-Amz-Target: CertificateManager.*` | JSON 1.1 | 16 |
-| [ECR](ecr.md) | `POST /` + `X-Amz-Target: AmazonEC2ContainerRegistry_V20150921.*` (control plane) and `/v2/...` (data plane proxied to `registry:2`) | JSON 1.1 + OCI Distribution | 23 |
+| [ECR](ecr.md) | `POST /` + `X-Amz-Target: AmazonEC2ContainerRegistry_V20150921.*` (control plane) and `/v2/...` (data plane proxied to `registry:2`) | JSON 1.1 + OCI Distribution | 30 |
| [AWS Resource Groups](resource-groups.md) | `/groups`, `/get-group`, `/delete-group`, `/group-resources`, `/list-group-resources` | REST JSON | 5 |
| [Resource Groups Tagging API](resource-groups-tagging.md) | `POST /` + `X-Amz-Target: ResourceGroupsTaggingAPI_20170126.*` | JSON 1.1 | 5 |
| [Resource Explorer](resource-explorer.md) | `POST /{OperationName}`, rewritten to `/_re2/*` for the four paths S3 Vectors also claims | REST JSON | 32 |
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/RequestConstraintCheck.java b/src/main/java/io/github/hectorvent/floci/core/common/RequestConstraintCheck.java
new file mode 100644
index 0000000000..4471f98ad3
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/core/common/RequestConstraintCheck.java
@@ -0,0 +1,140 @@
+package io.github.hectorvent.floci.core.common;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.TextNode;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Pattern;
+
+/**
+ * Checks a JSON request against the constraints a {@code tools/aws/regen_*_shapes.py} generator
+ * vendors from botocore through {@code request_shapes.build_constraints}: each member's type, the
+ * members a structure requires, a string's length, a list's or map's size, a number's range, a
+ * pattern and an enum. A service supplies its own {@link Messages}, so each answers with its own
+ * error code and wording.
+ *
+ * <p>Paths start with the operation name: {@code PutReplicationConfiguration.replicationConfiguration.rules[]},
+ * with {@code []} for a list element and {@code .key} for a map key.
+ */
+public final class RequestConstraintCheck {
+
+ private static final ObjectMapper MAPPER = new ObjectMapper();
+
+ /** How a service words each kind of violation. */
+ public interface Messages {
+ /** {@code value} at {@code path} is not of the shape's JSON {@code type}. */
+ AwsException type(String path, String type);
+
+ /** {@code size} (a length, a count or a number's value) is outside {@code min}..{@code max}. */
+ AwsException bounds(String path, String type, Long min, Long max, long size);
+
+ /** A string does not match the shape's {@code pattern}. */
+ AwsException pattern(String path, String pattern);
+
+ /** A value is not one of the shape's {@code values}. */
+ AwsException enumValue(String path, List<String> values);
+
+ /** The structure member at {@code path} is required but missing or null. */
+ AwsException required(String path);
+ }
+
+ private final JsonNode model;
+ private final Map<String, Pattern> patterns = new HashMap<>();
+ private final Messages messages;
+
+ private RequestConstraintCheck(JsonNode model, Messages messages) {
+ this.model = model;
+ this.messages = messages;
+ for (Map.Entry<String, JsonNode> shape : model.path("shapes").properties()) {
+ if (shape.getValue().has("pattern")) {
+ patterns.put(shape.getKey(), Pattern.compile(shape.getValue().get("pattern").asText()));
+ }
+ }
+ }
+
+ /** The vendored resource {@code name}; {@code description} names it in a failure. */
+ public static RequestConstraintCheck load(String name, String description, Messages messages) {
+ return VendoredResources.read(name, description,
+ input -> new RequestConstraintCheck(MAPPER.readTree(input), messages));
+ }
+
+ public static RequestConstraintCheck of(JsonNode model, Messages messages) {
+ return new RequestConstraintCheck(model, messages);
+ }
+
+ /** Checks {@code request}, the body of {@code operation}, against the operation's input shape. */
+ public void check(String operation, JsonNode request) {
+ checkValue(model.path("operations").path(operation).asText(), request, operation);
+ }
+
+ private void checkValue(String name, JsonNode value, String path) {
+ JsonNode shape = model.path("shapes").path(name);
+ String type = shape.path("type").asText();
+ boolean validType = value != null && switch (type) {
+ case "structure", "map" -> value.isObject();
+ case "list" -> value.isArray();
+ case "string" -> value.isTextual();
+ case "integer", "long" -> value.isIntegralNumber() && value.canConvertToLong();
+ default -> false;
+ };
+ if (!validType) {
+ throw messages.type(path, type);
+ }
+ long size = switch (type) {
+ case "string" -> value.textValue().codePointCount(0, value.textValue().length());
+ case "integer", "long" -> value.longValue();
+ default -> value.size();
+ };
+ Long min = shape.has("min") ? shape.get("min").asLong() : null;
+ Long max = shape.has("max") ? shape.get("max").asLong() : null;
+ if (min != null && size < min || max != null && size > max) {
+ throw messages.bounds(path, type, min, max, size);
+ }
+ if (patterns.containsKey(name) && !patterns.get(name).matcher(value.asText()).matches()) {
+ throw messages.pattern(path, shape.get("pattern").asText());
+ }
+ if (shape.has("enum")) {
+ boolean known = false;
+ List<String> values = new ArrayList<>();
+ for (JsonNode choice : shape.get("enum")) {
+ known |= choice.equals(value);
+ values.add(choice.asText());
+ }
+ if (!known) {
+ throw messages.enumValue(path, values);
+ }
+ }
+ switch (type) {
+ case "structure" -> {
+ for (JsonNode required : shape.path("required")) {
+ if (!value.hasNonNull(required.asText())) {
+ throw messages.required(path + "." + required.asText());
+ }
+ }
+ for (Map.Entry<String, JsonNode> field : value.properties()) {
+ JsonNode member = shape.path("members").get(field.getKey());
+ if (member != null && !field.getValue().isNull()) {
+ checkValue(member.asText(), field.getValue(), path + "." + field.getKey());
+ }
+ }
+ }
+ case "list" -> {
+ for (JsonNode item : value) {
+ checkValue(shape.get("member").asText(), item, path + "[]");
+ }
+ }
+ case "map" -> {
+ for (Map.Entry<String, JsonNode> field : value.properties()) {
+ checkValue(shape.get("key").asText(), TextNode.valueOf(field.getKey()), path + ".key");
+ checkValue(shape.get("value").asText(), field.getValue(), path + "." + field.getKey());
+ }
+ }
+ default -> {
+ }
+ }
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/ecr/EcrJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/ecr/EcrJsonHandler.java
index 585d668f08..ba78163821 100644
--- a/src/main/java/io/github/hectorvent/floci/services/ecr/EcrJsonHandler.java
+++ b/src/main/java/io/github/hectorvent/floci/services/ecr/EcrJsonHandler.java
@@ -14,6 +14,7 @@ import io.github.hectorvent.floci.services.ecr.model.ImageDetail;
import io.github.hectorvent.floci.services.ecr.model.ImageFailure;
import io.github.hectorvent.floci.services.ecr.model.ImageIdentifier;
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 jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
@@ -25,6 +26,7 @@ import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.Set;
/**
* AWS JSON 1.1 dispatcher for the {@code AmazonEC2ContainerRegistry_V20150921}
@@ -42,7 +44,16 @@ public class EcrJsonHandler {
this.objectMapper = objectMapper;
}
+ /** The actions whose requests are checked against the vendored model constraints first. */
+ private static final Set<String> MODEL_CHECKED = Set.of(
+ "PutRegistryPolicy", "GetRegistryPolicy", "DeleteRegistryPolicy",
+ "PutReplicationConfiguration", "DescribeRegistry",
+ "PutRegistryScanningConfiguration", "GetRegistryScanningConfiguration");
+
public Response handle(String action, JsonNode request, String region) {
+ if (MODEL_CHECKED.contains(action)) {
+ EcrRequestShapes.check(action, request);
+ }
return switch (action) {
case "CreateRepository" -> handleCreateRepository(request, region);
case "CreatePullThroughCacheRule" -> handleCreatePullThroughCacheRule(request, region);
@@ -76,6 +87,14 @@ public class EcrJsonHandler {
case "SetRepositoryPolicy" -> handleSetRepositoryPolicy(request, region);
case "GetRepositoryPolicy" -> handleGetRepositoryPolicy(request, region);
case "DeleteRepositoryPolicy" -> handleDeleteRepositoryPolicy(request, region);
+ case "PutRegistryPolicy" -> registryPolicyResponse(
+ service.putRegistryPolicy(request.path("policyText").asText(null), region));
+ case "GetRegistryPolicy" -> registryPolicyResponse(service.getRegistryPolicy(region));
+ case "DeleteRegistryPolicy" -> registryPolicyResponse(service.deleteRegistryPolicy(region));
+ case "PutReplicationConfiguration" -> handlePutReplicationConfiguration(request, region);
+ case "DescribeRegistry" -> handleDescribeRegistry(region);
+ case "PutRegistryScanningConfiguration" -> handlePutRegistryScanningConfiguration(request, region);
+ case "GetRegistryScanningConfiguration" -> handleGetRegistryScanningConfiguration(region);
default -> Response.status(400)
.entity(new AwsErrorResponse("UnsupportedOperation",
"Operation " + action + " is not supported."))
@@ -609,6 +628,106 @@ public class EcrJsonHandler {
// Builders / parsers
// ============================================================
+ private Response registryPolicyResponse(String policyText) {
+ ObjectNode response = objectMapper.createObjectNode();
+ response.put("registryId", service.registryId());
+ response.put("policyText", policyText);
+ return Response.ok(response).build();
+ }
+
+ private Response handlePutReplicationConfiguration(JsonNode request, String region) {
+ JsonNode config = request.path("replicationConfiguration");
+ List<RegistrySettings.ReplicationRule> rules = new ArrayList<>();
+ for (JsonNode rule : config.path("rules")) {
+ List<RegistrySettings.ReplicationDestination> destinations = null;
+ if (rule.path("destinations").isArray()) {
+ destinations = new ArrayList<>();
+ for (JsonNode d : rule.path("destinations")) {
+ destinations.add(new RegistrySettings.ReplicationDestination(
+ d.path("region").asText(null), d.path("registryId").asText(null)));
+ }
+ }
+ rules.add(new RegistrySettings.ReplicationRule(destinations,
+ rule.has("repositoryFilters") ? parseRepositoryFilters(rule.path("repositoryFilters")) : null));
+ }
+ ObjectNode response = objectMapper.createObjectNode();
+ response.set("replicationConfiguration",
+ buildReplicationConfiguration(service.putReplicationConfiguration(rules, region)));
+ return Response.ok(response).build();
+ }
+
+ private Response handleDescribeRegistry(String region) {
+ ObjectNode response = objectMapper.createObjectNode();
+ response.put("registryId", service.registryId());
+ response.set("replicationConfiguration", buildReplicationConfiguration(service.replicationRules(region)));
+ return Response.ok(response).build();
+ }
+
+ private Response handlePutRegistryScanningConfiguration(JsonNode request, String region) {
+ List<RegistrySettings.ScanningRule> rules = new ArrayList<>();
+ for (JsonNode rule : request.path("rules")) {
+ rules.add(new RegistrySettings.ScanningRule(rule.path("scanFrequency").asText(null),
+ rule.has("repositoryFilters") ? parseRepositoryFilters(rule.path("repositoryFilters")) : null));
+ }
+ RegistrySettings saved = service.putRegistryScanningConfiguration(
+ request.path("scanType").asText(null), rules, region);
+ ObjectNode response = objectMapper.createObjectNode();
+ response.set("registryScanningConfiguration", buildScanningConfiguration(saved));
+ return Response.ok(response).build();
+ }
+
+ private Response handleGetRegistryScanningConfiguration(String region) {
+ ObjectNode response = objectMapper.createObjectNode();
+ response.put("registryId", service.registryId());
+ response.set("scanningConfiguration", buildScanningConfiguration(service.registryScanningConfiguration(region)));
+ return Response.ok(response).build();
+ }
+
+ private static List<RegistrySettings.RepositoryFilter> parseRepositoryFilters(JsonNode node) {
+ List<RegistrySettings.RepositoryFilter> filters = new ArrayList<>();
+ for (JsonNode f : node) {
+ filters.add(new RegistrySettings.RepositoryFilter(
+ f.path("filter").asText(null), f.path("filterType").asText(null)));
+ }
+ return filters;
+ }
+
+ private ObjectNode buildReplicationConfiguration(List<RegistrySettings.ReplicationRule> rules) {
+ ObjectNode config = objectMapper.createObjectNode();
+ ArrayNode out = config.putArray("rules");
+ for (RegistrySettings.ReplicationRule rule : rules) {
+ ObjectNode n = out.addObject();
+ ArrayNode destinations = n.putArray("destinations");
+ for (RegistrySettings.ReplicationDestination d : rule.destinations()) {
+ destinations.addObject().put("region", d.region()).put("registryId", d.registryId());
+ }
+ if (rule.repositoryFilters() != null) {
+ n.set("repositoryFilters", buildRepositoryFilters(rule.repositoryFilters()));
+ }
+ }
+ return config;
+ }
+
+ private ObjectNode buildScanningConfiguration(RegistrySettings settings) {
+ ObjectNode config = objectMapper.createObjectNode();
+ config.put("scanType", settings.getScanType());
+ ArrayNode out = config.putArray("rules");
+ for (RegistrySettings.ScanningRule rule : settings.getScanningRules()) {
+ ObjectNode n = out.addObject();
+ n.put("scanFrequency", rule.scanFrequency());
+ n.set("repositoryFilters", buildRepositoryFilters(rule.repositoryFilters()));
+ }
+ return config;
+ }
+
+ private ArrayNode buildRepositoryFilters(List<RegistrySettings.RepositoryFilter> filters) {
+ ArrayNode out = objectMapper.createArrayNode();
+ for (RegistrySettings.RepositoryFilter f : filters) {
+ out.addObject().put("filter", f.filter()).put("filterType", f.filterType());
+ }
+ return out;
+ }
+
private ObjectNode buildImageIdentifier(ImageIdentifier id) {
ObjectNode n = objectMapper.createObjectNode();
if (id.getImageTag() != null) n.put("imageTag", id.getImageTag());
diff --git a/src/main/java/io/github/hectorvent/floci/services/ecr/EcrRequestShapes.java b/src/main/java/io/github/hectorvent/floci/services/ecr/EcrRequestShapes.java
new file mode 100644
index 0000000000..a83fec313a
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/ecr/EcrRequestShapes.java
@@ -0,0 +1,63 @@
+package io.github.hectorvent.floci.services.ecr;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import io.github.hectorvent.floci.core.common.AwsException;
+import io.github.hectorvent.floci.core.common.RequestConstraintCheck;
+
+import java.util.List;
+
+/**
+ * Checks an ECR request against the constraints vendored in {@code aws/ecr-request-shapes.json} by
+ * {@code tools/aws/regen_ecr_shapes.py}. A violation is {@code InvalidParameterException} in ECR's
+ * form, {@code Invalid parameter at '<member>' failed to satisfy constraint: '<rule>'}, with the
+ * member's path from the request's top level.
+ */
+final class EcrRequestShapes {
+
+ private static final RequestConstraintCheck CHECK = RequestConstraintCheck.load(
+ "aws/ecr-request-shapes.json", "ECR request shapes",
+ new RequestConstraintCheck.Messages() {
+ @Override
+ public AwsException type(String path, String type) {
+ return invalid(path, "Member must be a " + type);
+ }
+
+ @Override
+ public AwsException bounds(String path, String type, Long min, Long max, long size) {
+ String measure = "integer".equals(type) || "long".equals(type) ? "value" : "length";
+ return min != null && size < min
+ ? invalid(path, "Member must have " + measure + " greater than or equal to " + min)
+ : invalid(path, "Member must have " + measure + " less than or equal to " + max);
+ }
+
+ @Override
+ public AwsException pattern(String path, String pattern) {
+ return invalid(path, "must satisfy regular expression '" + pattern + "'");
+ }
+
+ @Override
+ public AwsException enumValue(String path, List<String> values) {
+ return invalid(path, "Member must satisfy enum value set: " + values);
+ }
+
+ @Override
+ public AwsException required(String path) {
+ return invalid(path, "Member must not be null");
+ }
+ });
+
+ private EcrRequestShapes() {
+ }
+
+ static void check(String operation, JsonNode request) {
+ CHECK.check(operation, request);
+ }
+
+ /** The member's path below the operation, with list elements folded into their list. */
+ private static AwsException invalid(String path, String rule) {
+ int dot = path.indexOf('.');
+ String member = (dot < 0 ? "" : path.substring(dot + 1)).replace("[]", "");
+ return new AwsException("InvalidParameterException",
+ "Invalid parameter at '" + member + "' failed to satisfy constraint: '" + rule + "'", 400);
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/ecr/EcrService.java b/src/main/java/io/github/hectorvent/floci/services/ecr/EcrService.java
index 0e32bed461..790ff96816 100644
--- a/src/main/java/io/github/hectorvent/floci/services/ecr/EcrService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/ecr/EcrService.java
@@ -15,6 +15,7 @@ import io.github.hectorvent.floci.core.resource.ExplorerResource;
import io.github.hectorvent.floci.core.resource.ResourceProvider;
import io.github.hectorvent.floci.core.resource.SupportedResourceType;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
+import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.ecr.model.AuthorizationData;
@@ -24,6 +25,7 @@ import io.github.hectorvent.floci.services.ecr.model.ImageFailure;
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;
@@ -62,7 +64,6 @@ public class EcrService implements ResourceProvider {
private static final Pattern REGISTRY_ID = Pattern.compile("[0-9]{12}");
private static final Pattern LAYER_DIGEST = Pattern.compile("[a-zA-Z0-9-_+.]+:[a-fA-F0-9]+");
private static final ObjectMapper MAPPER = new ObjectMapper();
- /** The OCI distribution spec's tag grammar, which the registry's manifest URL takes. */
/** ECR's imageTag rule, which the service model bounds at 300 characters. */
private static final Pattern ECR_IMAGE_TAG = Pattern.compile("[a-zA-Z0-9_.-]+");
/** The OCI distribution spec's tag grammar, which the backing registry's manifest URL takes. */
@@ -80,6 +81,8 @@ public class EcrService implements ResourceProvider {
private final StorageBackend<String, Repository> repoStore;
private final StorageBackend<String, ImageMetadata> imageMetaStore;
private final StorageBackend<String, PullThroughCacheRule> pullThroughCacheRuleStore;
+ private final StorageBackend<String, RegistrySettings> registrySettingsStore;
+ private final Map<String, Object> registrySettingsLocks = new ConcurrentHashMap<>();
private final EcrRegistryManager registryManager;
private final EmulatorConfig config;
private final RegionResolver regionResolver;
@@ -97,6 +100,8 @@ public class EcrService implements ResourceProvider {
new TypeReference<Map<String, ImageMetadata>>() {}),
factory.create("ecr", "pull-through-cache-rules.json",
new TypeReference<Map<String, PullThroughCacheRule>>() {}),
+ factory.create("ecr", "registry-settings.json",
+ new TypeReference<Map<String, RegistrySettings>>() {}),
registryManager, config, regionResolver);
}
@@ -106,9 +111,21 @@ public class EcrService implements ResourceProvider {
EcrRegistryManager registryManager,
EmulatorConfig config,
RegionResolver regionResolver) {
+ this(repoStore, imageMetaStore, pullThroughCacheRuleStore, new InMemoryStorage<>(),
+ registryManager, config, regionResolver);
+ }
+
+ EcrService(StorageBackend<String, Repository> repoStore,
+ StorageBackend<String, ImageMetadata> imageMetaStore,
+ StorageBackend<String, PullThroughCacheRule> pullThroughCacheRuleStore,
+ StorageBackend<String, RegistrySettings> registrySettingsStore,
+ EcrRegistryManager registryManager,
+ EmulatorConfig config,
+ RegionResolver regionResolver) {
this.repoStore = repoStore;
this.imageMetaStore = imageMetaStore;
this.pullThroughCacheRuleStore = pullThroughCacheRuleStore;
+ this.registrySettingsStore = registrySettingsStore;
this.registryManager = registryManager;
this.config = config;
this.regionResolver = regionResolver;
@@ -975,6 +992,137 @@ public class EcrService implements ResourceProvider {
return new AwsException("ServerException", "The ECR backing registry failed: " + e.getMessage(), 500);
}
+ // ============================================================
+ // Registry settings (policy, replication, scanning)
+ // ============================================================
+
+ /**
+ * Stores the registry policy. Its length is checked against the model before this is reached;
+ * text that is not a JSON object is refused here, as the model cannot say so.
+ */
+ public String putRegistryPolicy(String policyText, String region) {
+ JsonNode parsed;
+ try {
+ parsed = policyText == null ? null : MAPPER.readTree(policyText);
+ } catch (Exception e) {
+ parsed = null;
+ }
+ if (parsed == null || !parsed.isObject()) {
+ throw new AwsException("InvalidParameterException", "Invalid parameter at 'PolicyText' failed to satisfy "
+ + "constraint: 'Invalid registry policy provided'", 400);
+ }
+ updateRegistrySettings(region, settings -> {
+ settings.setPolicyText(policyText);
+ return null;
+ });
+ return policyText;
+ }
+
+ public String getRegistryPolicy(String region) {
+ String policy = registrySettings(region).getPolicyText();
+ if (policy == null) {
+ throw registryPolicyNotFound();
+ }
+ return policy;
+ }
+
+ /** Removes the registry policy and returns what it was. */
+ public String deleteRegistryPolicy(String region) {
+ return updateRegistrySettings(region, settings -> {
+ String policy = settings.getPolicyText();
+ if (policy == null) {
+ throw registryPolicyNotFound();
+ }
+ settings.setPolicyText(null);
+ return policy;
+ });
+ }
+
+ /**
+ * Stores the replication rules; an empty list clears them. The rules are checked against the
+ * model before this is reached. Floci does not replicate images.
+ */
+ public List<RegistrySettings.ReplicationRule> putReplicationConfiguration(
+ List<RegistrySettings.ReplicationRule> rules, String region) {
+ updateRegistrySettings(region, settings -> {
+ settings.setReplicationRules(rules.isEmpty() ? null : List.copyOf(rules));
+ return null;
+ });
+ return replicationRules(region);
+ }
+
+ /** The stored replication rules, empty when none are set. */
+ public List<RegistrySettings.ReplicationRule> replicationRules(String region) {
+ List<RegistrySettings.ReplicationRule> rules = registrySettings(region).getReplicationRules();
+ return rules == null ? List.of() : rules;
+ }
+
+ /**
+ * Stores the scanning configuration. The model's constraints are checked before this is
+ * reached; {@code CONTINUOUS_SCAN} is refused here with {@code BASIC}, as the API reference
+ * offers it only with enhanced scanning and the model cannot say so. Floci does not scan images.
+ */
+ public RegistrySettings putRegistryScanningConfiguration(String scanType, List<RegistrySettings.ScanningRule> rules,
+ String region) {
+ String type = scanType == null ? "BASIC" : scanType;
+ List<RegistrySettings.ScanningRule> given = rules == null ? List.of() : rules;
+ for (RegistrySettings.ScanningRule rule : given) {
+ if ("BASIC".equals(type) && "CONTINUOUS_SCAN".equals(rule.scanFrequency())) {
+ throw new AwsException("InvalidParameterException",
+ "CONTINUOUS_SCAN is supported only with the ENHANCED scan type", 400);
+ }
+ }
+ updateRegistrySettings(region, settings -> {
+ settings.setScanType(type);
+ settings.setScanningRules(List.copyOf(given));
+ return null;
+ });
+ return registryScanningConfiguration(region);
+ }
+
+ /** The scanning configuration, {@code BASIC} with no rules until one is put. */
+ public RegistrySettings registryScanningConfiguration(String region) {
+ RegistrySettings stored = registrySettings(region);
+ RegistrySettings view = new RegistrySettings();
+ view.setScanType(stored.getScanType() == null ? "BASIC" : stored.getScanType());
+ view.setScanningRules(stored.getScanningRules() == null ? List.of() : stored.getScanningRules());
+ return view;
+ }
+
+ /** The registry ID of the caller's account. */
+ public String registryId() {
+ return regionResolver.getAccountId();
+ }
+
+ private RegistrySettings registrySettings(String region) {
+ return registrySettingsStore.get(registrySettingsKey(region)).orElseGet(RegistrySettings::new);
+ }
+
+ /**
+ * Reads the registry's settings, changes them and writes them back under the registry's lock.
+ * The settings are one record, and Terraform puts the policy, replication and scanning
+ * configuration in parallel: unlocked, two writers could start from the same record and the
+ * later write would drop the other's field.
+ */
+ private <T> T updateRegistrySettings(String region, Function<RegistrySettings, T> change) {
+ String key = registrySettingsKey(region);
+ synchronized (registrySettingsLocks.computeIfAbsent(key, k -> new Object())) {
+ RegistrySettings settings = registrySettings(region);
+ T result = change.apply(settings);
+ registrySettingsStore.put(key, settings);
+ return result;
+ }
+ }
+
+ private String registrySettingsKey(String region) {
+ return region + "::" + regionResolver.getAccountId();
+ }
+
+ private AwsException registryPolicyNotFound() {
+ return new AwsException("RegistryPolicyNotFoundException",
+ "Registry policy does not exist in the registry with id '" + regionResolver.getAccountId() + "'", 400);
+ }
+
// ============================================================
// Tag mutability + resource tags + policies (metadata round-trip)
// ============================================================
diff --git a/src/main/java/io/github/hectorvent/floci/services/ecr/model/RegistrySettings.java b/src/main/java/io/github/hectorvent/floci/services/ecr/model/RegistrySettings.java
new file mode 100644
index 0000000000..6f15a9cb55
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/ecr/model/RegistrySettings.java
@@ -0,0 +1,49 @@
+package io.github.hectorvent.floci.services.ecr.model;
+
+import io.quarkus.runtime.annotations.RegisterForReflection;
+
+import java.util.List;
+
+/**
+ * A registry's own settings, one record per account and Region: the registry policy, the
+ * replication configuration and the scanning configuration. Floci records them and returns them;
+ * it does not replicate or scan images.
+ */
+@RegisterForReflection
+public class RegistrySettings {
+
+ /** One repository filter: a prefix ({@code PREFIX_MATCH}) or a wildcard ({@code WILDCARD}). */
+ @RegisterForReflection
+ public record RepositoryFilter(String filter, String filterType) {}
+
+ /** One replication destination. */
+ @RegisterForReflection
+ public record ReplicationDestination(String region, String registryId) {}
+
+ /** One replication rule: where to copy, and which repositories. */
+ @RegisterForReflection
+ public record ReplicationRule(List<ReplicationDestination> destinations, List<RepositoryFilter> repositoryFilters) {}
+
+ /** One scanning rule: how often, and which repositories. */
+ @RegisterForReflection
+ public record ScanningRule(String scanFrequency, List<RepositoryFilter> repositoryFilters) {}
+
+ private String policyText;
+ private List<ReplicationRule> replicationRules;
+ private String scanType;
+ private List<ScanningRule> scanningRules;
+
+ public RegistrySettings() {}
+
+ public String getPolicyText() { return policyText; }
+ public void setPolicyText(String policyText) { this.policyText = policyText; }
+
+ public List<ReplicationRule> getReplicationRules() { return replicationRules; }
+ public void setReplicationRules(List<ReplicationRule> replicationRules) { this.replicationRules = replicationRules; }
+
+ public String getScanType() { return scanType; }
+ public void setScanType(String scanType) { this.scanType = scanType; }
+
+ public List<ScanningRule> getScanningRules() { return scanningRules; }
+ public void setScanningRules(List<ScanningRule> scanningRules) { this.scanningRules = scanningRules; }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/resourcegroups/ResourceGroupsRequestShapes.java b/src/main/java/io/github/hectorvent/floci/services/resourcegroups/ResourceGroupsRequestShapes.java
index 8a70ffb016..0a9d98c4e4 100644
--- a/src/main/java/io/github/hectorvent/floci/services/resourcegroups/ResourceGroupsRequestShapes.java
+++ b/src/main/java/io/github/hectorvent/floci/services/resourcegroups/ResourceGroupsRequestShapes.java
@@ -1,97 +1,52 @@
package io.github.hectorvent.floci.services.resourcegroups;
import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.node.TextNode;
import io.github.hectorvent.floci.core.common.AwsException;
-import io.github.hectorvent.floci.core.common.VendoredResources;
+import io.github.hectorvent.floci.core.common.RequestConstraintCheck;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.regex.Pattern;
+import java.util.List;
+/**
+ * Checks a Resource Groups request against the constraints vendored in
+ * {@code aws/resource-groups-request-shapes.json} by {@code tools/aws/regen_resource_groups_shapes.py},
+ * answering {@code BadRequestException}.
+ */
final class ResourceGroupsRequestShapes {
- private static final JsonNode MODEL = VendoredResources.read("aws/resource-groups-request-shapes.json",
- "Resource Groups request shapes", input -> new ObjectMapper().readTree(input));
- private static final Map<String, Pattern> PATTERNS = new HashMap<>();
- static {
- for (Map.Entry<String, JsonNode> shape : MODEL.path("shapes").properties()) {
- if (shape.getValue().has("pattern")) {
- PATTERNS.put(shape.getKey(), Pattern.compile(shape.getValue().get("pattern").asText()));
- }
- }
- }
-
- private ResourceGroupsRequestShapes() {
- }
-
- static void check(String operation, JsonNode request) {
- checkValue(MODEL.path("operations").path(operation).asText(), request, operation);
- }
+ private static final RequestConstraintCheck CHECK = RequestConstraintCheck.load(
+ "aws/resource-groups-request-shapes.json", "Resource Groups request shapes",
+ new RequestConstraintCheck.Messages() {
+ @Override
+ public AwsException type(String path, String type) {
+ return bad(path + " must be a " + type + ".");
+ }
- private static void checkValue(String name, JsonNode value, String path) {
- JsonNode shape = MODEL.path("shapes").path(name);
- String type = shape.path("type").asText();
- boolean validType = value != null && switch (type) {
- case "structure", "map" -> value.isObject();
- case "list" -> value.isArray();
- case "string" -> value.isTextual();
- case "integer", "long" -> value.isIntegralNumber() && value.canConvertToLong();
- default -> false;
- };
- if (!validType) {
- throw bad(path + " must be a " + type + ".");
- }
- long size = switch (type) {
- case "string" -> value.textValue().codePointCount(0, value.textValue().length());
- case "integer", "long" -> value.longValue();
- default -> value.size();
- };
- if (shape.has("min") && size < shape.get("min").asLong()
- || shape.has("max") && size > shape.get("max").asLong()) {
- throw bad(path + " is outside the modeled bounds.");
- }
- if (PATTERNS.containsKey(name) && !PATTERNS.get(name).matcher(value.asText()).matches()) {
- throw bad(path + " does not match the modeled pattern.");
- }
- if (shape.has("enum")) {
- boolean known = false;
- for (JsonNode choice : shape.get("enum")) {
- known |= choice.equals(value);
- }
- if (!known) {
- throw bad(path + " is not a modeled value.");
- }
- }
- switch (type) {
- case "structure" -> {
- for (JsonNode required : shape.path("required")) {
- if (!value.hasNonNull(required.asText())) {
- throw bad(path + "." + required.asText() + " is required.");
- }
+ @Override
+ public AwsException bounds(String path, String type, Long min, Long max, long size) {
+ return bad(path + " is outside the modeled bounds.");
}
- for (Map.Entry<String, JsonNode> field : value.properties()) {
- JsonNode member = shape.path("members").get(field.getKey());
- if (member != null && !field.getValue().isNull()) {
- checkValue(member.asText(), field.getValue(), path + "." + field.getKey());
- }
+
+ @Override
+ public AwsException pattern(String path, String pattern) {
+ return bad(path + " does not match the modeled pattern.");
}
- }
- case "list" -> {
- for (JsonNode item : value) {
- checkValue(shape.get("member").asText(), item, path + "[]");
+
+ @Override
+ public AwsException enumValue(String path, List<String> values) {
+ return bad(path + " is not a modeled value.");
}
- }
- case "map" -> {
- for (Map.Entry<String, JsonNode> field : value.properties()) {
- checkValue(shape.get("key").asText(), TextNode.valueOf(field.getKey()), path + ".key");
- checkValue(shape.get("value").asText(), field.getValue(), path + "." + field.getKey());
+
+ @Override
+ public AwsException required(String path) {
+ return bad(path + " is required.");
}
- }
- default -> {
- }
- }
+ });
+
+ private ResourceGroupsRequestShapes() {
+ }
+
+ static void check(String operation, JsonNode request) {
+ CHECK.check(operation, request);
}
private static AwsException bad(String message) {
diff --git a/src/main/resources/aws/ecr-request-shapes.json b/src/main/resources/aws/ecr-request-shapes.json
new file mode 100644
index 0000000000..fc9ce215fa
--- /dev/null
+++ b/src/main/resources/aws/ecr-request-shapes.json
@@ -0,0 +1,205 @@
+{
+ "_source": {
+ "generator": "tools/aws/regen_ecr_shapes.py",
+ "botocore": "botocore 1.43.113 (installed package)"
+ },
+ "operations": {
+ "PutRegistryPolicy": "PutRegistryPolicyRequest",
+ "GetRegistryPolicy": "GetRegistryPolicyRequest",
+ "DeleteRegistryPolicy": "DeleteRegistryPolicyRequest",
+ "PutReplicationConfiguration": "PutReplicationConfigurationRequest",
+ "DescribeRegistry": "DescribeRegistryRequest",
+ "PutRegistryScanningConfiguration": "PutRegistryScanningConfigurationRequest",
+ "GetRegistryScanningConfiguration": "GetRegistryScanningConfigurationRequest"
+ },
+ "shapes": {
+ "DeleteRegistryPolicyRequest": {
+ "type": "structure",
+ "members": {}
+ },
+ "DescribeRegistryRequest": {
+ "type": "structure",
+ "members": {}
+ },
+ "GetRegistryPolicyRequest": {
+ "type": "structure",
+ "members": {}
+ },
+ "GetRegistryScanningConfigurationRequest": {
+ "type": "structure",
+ "members": {}
+ },
+ "PutRegistryPolicyRequest": {
+ "type": "structure",
+ "required": [
+ "policyText"
+ ],
+ "members": {
+ "policyText": "RegistryPolicyText"
+ }
+ },
+ "PutRegistryScanningConfigurationRequest": {
+ "type": "structure",
+ "members": {
+ "scanType": "ScanType",
+ "rules": "RegistryScanningRuleList"
+ }
+ },
+ "PutReplicationConfigurationRequest": {
+ "type": "structure",
+ "required": [
+ "replicationConfiguration"
+ ],
+ "members": {
+ "replicationConfiguration": "ReplicationConfiguration"
+ }
+ },
+ "Region": {
+ "type": "string",
+ "min": 2,
+ "max": 25,
+ "pattern": "[0-9a-z-]{2,25}"
+ },
+ "RegistryId": {
+ "type": "string",
+ "pattern": "[0-9]{12}"
+ },
+ "RegistryPolicyText": {
+ "type": "string",
+ "min": 0,
+ "max": 10240
+ },
+ "RegistryScanningRule": {
+ "type": "structure",
+ "required": [
+ "scanFrequency",
+ "repositoryFilters"
+ ],
+ "members": {
+ "scanFrequency": "ScanFrequency",
+ "repositoryFilters": "ScanningRepositoryFilterList"
+ }
+ },
+ "RegistryScanningRuleList": {
+ "type": "list",
+ "min": 0,
+ "max": 2,
+ "member": "RegistryScanningRule"
+ },
+ "ReplicationConfiguration": {
+ "type": "structure",
+ "required": [
+ "rules"
+ ],
+ "members": {
+ "rules": "ReplicationRuleList"
+ }
+ },
+ "ReplicationDestination": {
+ "type": "structure",
+ "required": [
+ "region",
+ "registryId"
+ ],
+ "members": {
+ "region": "Region",
+ "registryId": "RegistryId"
+ }
+ },
+ "ReplicationDestinationList": {
+ "type": "list",
+ "min": 0,
+ "max": 100,
+ "member": "ReplicationDestination"
+ },
+ "ReplicationRule": {
+ "type": "structure",
+ "required": [
+ "destinations"
+ ],
+ "members": {
+ "destinations": "ReplicationDestinationList",
+ "repositoryFilters": "RepositoryFilterList"
+ }
+ },
+ "ReplicationRuleList": {
+ "type": "list",
+ "min": 0,
+ "max": 25,
+ "member": "ReplicationRule"
+ },
+ "RepositoryFilter": {
+ "type": "structure",
+ "required": [
+ "filter",
+ "filterType"
+ ],
+ "members": {
+ "filter": "RepositoryFilterValue",
+ "filterType": "RepositoryFilterType"
+ }
+ },
+ "RepositoryFilterList": {
+ "type": "list",
+ "min": 1,
+ "max": 100,
+ "member": "RepositoryFilter"
+ },
+ "RepositoryFilterType": {
+ "type": "string",
+ "enum": [
+ "PREFIX_MATCH"
+ ]
+ },
+ "RepositoryFilterValue": {
+ "type": "string",
+ "min": 2,
+ "max": 256,
+ "pattern": "^(?:[a-z0-9]+(?:[._-][a-z0-9]*)*/)*[a-z0-9]*(?:[._-][a-z0-9]*)*$"
+ },
+ "ScanFrequency": {
+ "type": "string",
+ "enum": [
+ "SCAN_ON_PUSH",
+ "CONTINUOUS_SCAN",
+ "MANUAL"
+ ]
+ },
+ "ScanType": {
+ "type": "string",
+ "enum": [
+ "BASIC",
+ "ENHANCED"
+ ]
+ },
+ "ScanningRepositoryFilter": {
+ "type": "structure",
+ "required": [
+ "filter",
+ "filterType"
+ ],
+ "members": {
+ "filter": "ScanningRepositoryFilterValue",
+ "filterType": "ScanningRepositoryFilterType"
+ }
+ },
+ "ScanningRepositoryFilterList": {
+ "type": "list",
+ "min": 0,
+ "max": 100,
+ "member": "ScanningRepositoryFilter"
+ },
+ "ScanningRepositoryFilterType": {
+ "type": "string",
+ "enum": [
+ "WILDCARD"
+ ]
+ },
+ "ScanningRepositoryFilterValue": {
+ "type": "string",
+ "min": 1,
+ "max": 255,
+ "pattern": "^[a-z0-9*](?:[._\\-/a-z0-9*]?[a-z0-9*]+)*$"
+ }
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/RequestConstraintCheckTest.java b/src/test/java/io/github/hectorvent/floci/core/common/RequestConstraintCheckTest.java
new file mode 100644
index 0000000000..4e2912d067
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/core/common/RequestConstraintCheckTest.java
@@ -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\"}"));
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/ecr/EcrRegistrySettingsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ecr/EcrRegistrySettingsIntegrationTest.java
new file mode 100644
index 0000000000..867d8f0b1f
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/ecr/EcrRegistrySettingsIntegrationTest.java
@@ -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();
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/ecr/EcrRequestShapesTest.java b/src/test/java/io/github/hectorvent/floci/services/ecr/EcrRequestShapesTest.java
new file mode 100644
index 0000000000..1ab736f29e
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/ecr/EcrRequestShapesTest.java
@@ -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);
+ }
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/ecr/EcrServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/ecr/EcrServiceTest.java
index 17ce9e379d..ca9b3a1622 100644
--- a/src/test/java/io/github/hectorvent/floci/services/ecr/EcrServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/ecr/EcrServiceTest.java
@@ -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();
+ }
+ }
}
diff --git a/tools/aws/regen_ecr_shapes.py b/tools/aws/regen_ecr_shapes.py
new file mode 100644
index 0000000000..dd4b8c4e37
--- /dev/null
+++ b/tools/aws/regen_ecr_shapes.py
@@ -0,0 +1,50 @@
+#!/usr/bin/env python3
+"""Vendor ECR request constraints from botocore's Apache-2.0 service model.
+
+`EcrRequestShapes` checks the registry setting actions against the vendored file through the
+shared `RequestConstraintCheck`: member types, required members, lengths and counts, patterns and
+enum values, as the model states them.
+
+Run from anywhere in the repo:
+ python3 tools/aws/regen_ecr_shapes.py # rewrite the vendored file in place
+ python3 tools/aws/regen_ecr_shapes.py --check # exit 1 when the vendored file is stale
+"""
+from __future__ import annotations
+
+import argparse
+import json
+import sys
+from pathlib import Path
+
+from regen_partitions import resolve_botocore_data, strip_source
+from request_shapes import REPO_ROOT, build_constraints, load_model
+
+OUTPUT = REPO_ROOT / "src/main/resources/aws/ecr-request-shapes.json"
+MODEL = Path("ecr/2015-09-21")
+OPERATIONS = (
+ "PutRegistryPolicy", "GetRegistryPolicy", "DeleteRegistryPolicy",
+ "PutReplicationConfiguration", "DescribeRegistry",
+ "PutRegistryScanningConfiguration", "GetRegistryScanningConfiguration",
+)
+
+
+def main(argv: list[str] | None = None) -> int:
+ parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
+ parser.add_argument("--check", action="store_true", help="exit 1 when the vendored file differs from a fresh generation")
+ parser.add_argument("--botocore-data", type=Path, help="botocore data directory")
+ args = parser.parse_args(argv)
+ data, provenance = resolve_botocore_data(args.botocore_data)
+ doc = build_constraints(load_model(data, MODEL), OPERATIONS, provenance, "tools/aws/regen_ecr_shapes.py")
+ fresh = json.dumps(doc, indent=2) + "\n"
+ if args.check:
+ if not OUTPUT.exists() or strip_source(OUTPUT.read_text(encoding="utf-8")) != strip_source(fresh):
+ print(f"{OUTPUT} is stale (generated from {provenance}).", file=sys.stderr)
+ return 1
+ return 0
+ OUTPUT.write_text(fresh, encoding="utf-8")
+ print(f"wrote {OUTPUT.relative_to(REPO_ROOT)} from {provenance}")
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/tools/aws/regen_resource_groups_shapes.py b/tools/aws/regen_resource_groups_shapes.py
index 71a3936b85..a5e2995fb1 100644
--- a/tools/aws/regen_resource_groups_shapes.py
+++ b/tools/aws/regen_resource_groups_shapes.py
@@ -7,47 +7,21 @@ import json
from pathlib import Path
from regen_partitions import resolve_botocore_data, strip_source
-from request_shapes import REPO_ROOT, load_model
+from request_shapes import REPO_ROOT, build_constraints, load_model
OUTPUT = REPO_ROOT / "src/main/resources/aws/resource-groups-request-shapes.json"
OPERATIONS = ("CreateGroup", "GetGroup", "DeleteGroup", "GroupResources", "ListGroupResources")
-def build(model: dict, provenance: str) -> dict:
- kept = {}
-
- def keep(name: str) -> None:
- if name in kept:
- return
- shape = model["shapes"][name]
- entry = {k: shape[k] for k in ("type", "required", "min", "max", "pattern", "enum") if k in shape}
- kept[name] = entry
- if shape["type"] == "structure":
- entry["members"] = {k: v["shape"] for k, v in shape.get("members", {}).items()}
- for child in entry["members"].values():
- keep(child)
- elif shape["type"] == "list":
- entry["member"] = shape["member"]["shape"]
- keep(entry["member"])
- elif shape["type"] == "map":
- for side in ("key", "value"):
- entry[side] = shape[side]["shape"]
- keep(entry[side])
-
- operations = {name: model["operations"][name]["input"]["shape"] for name in OPERATIONS}
- for name in operations.values():
- keep(name)
- return {"_source": {"generator": "tools/aws/regen_resource_groups_shapes.py", "botocore": provenance},
- "operations": operations, "shapes": dict(sorted(kept.items()))}
-
-
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--check", action="store_true")
parser.add_argument("--botocore-data", type=Path)
args = parser.parse_args()
data, provenance = resolve_botocore_data(args.botocore_data)
- fresh = json.dumps(build(load_model(data, Path("resource-groups/2017-11-27")), provenance), indent=2) + "\n"
+ doc = build_constraints(load_model(data, Path("resource-groups/2017-11-27")), OPERATIONS, provenance,
+ "tools/aws/regen_resource_groups_shapes.py")
+ fresh = json.dumps(doc, indent=2) + "\n"
if args.check:
return int(not OUTPUT.exists() or strip_source(OUTPUT.read_text()) != strip_source(fresh))
OUTPUT.write_text(fresh)
diff --git a/tools/aws/request_shapes.py b/tools/aws/request_shapes.py
index 7bc92ba6c0..ec0092061c 100644
--- a/tools/aws/request_shapes.py
+++ b/tools/aws/request_shapes.py
@@ -1,7 +1,8 @@
"""Shared generator for the vendored request shapes a service's request check reads.
`regen_dynamodb_shapes.py`, `regen_sesv2_shapes.py` and `regen_ecs_shapes.py` each name a botocore
-model and an output file and hand the rest to `main()`. The output keeps only what `RequestShapeModel` reads: the input
+model and an output file and hand the rest to `main()`. `regen_resource_groups_shapes.py` and
+`regen_ecr_shapes.py` vendor constraints as well, through `build_constraints()`. The output keeps only what `RequestShapeModel` reads: the input
shape of every operation, reduced to the members carried in the JSON body, and for every shape
reachable from one, its type and the shapes of its members, list elements or map values.
@@ -69,6 +70,39 @@ def build(model: dict, provenance: str, generator: str) -> dict:
}
+def build_constraints(model: dict, operations: tuple[str, ...], provenance: str, generator: str) -> dict:
+ """The input shapes of `operations` with the constraints `RequestConstraintCheck` reads: for every
+ shape reachable from one, its type, required members, min, max, pattern and enum, and the shapes
+ of its members, list elements or map keys and values. Unlike `build`, it keeps members bound to
+ the URI, query string or headers, so it suits the JSON protocols, whose members all travel in
+ the body."""
+ kept: dict[str, dict] = {}
+
+ def keep(name: str) -> None:
+ if name in kept:
+ return
+ shape = model["shapes"][name]
+ entry = {k: shape[k] for k in ("type", "required", "min", "max", "pattern", "enum") if k in shape}
+ kept[name] = entry
+ if shape["type"] == "structure":
+ entry["members"] = {k: v["shape"] for k, v in shape.get("members", {}).items()}
+ for child in entry["members"].values():
+ keep(child)
+ elif shape["type"] == "list":
+ entry["member"] = shape["member"]["shape"]
+ keep(entry["member"])
+ elif shape["type"] == "map":
+ for side in ("key", "value"):
+ entry[side] = shape[side]["shape"]
+ keep(entry[side])
+
+ inputs = {name: model["operations"][name]["input"]["shape"] for name in operations}
+ for name in inputs.values():
+ keep(name)
+ return {"_source": {"generator": generator, "botocore": provenance},
+ "operations": inputs, "shapes": dict(sorted(kept.items()))}
+
+
def render(doc: dict) -> str:
return json.dumps(doc, indent=2, sort_keys=False) + "\n"
diff --git a/tools/aws/test_request_shapes.py b/tools/aws/test_request_shapes.py
index d66d757f8f..57a7673f0b 100644
--- a/tools/aws/test_request_shapes.py
+++ b/tools/aws/test_request_shapes.py
@@ -1,4 +1,5 @@
-"""Tests for request_shapes, the generator behind regen_dynamodb_shapes, regen_sesv2_shapes and regen_ecs_shapes.
+"""Tests for request_shapes, the generator behind regen_dynamodb_shapes, regen_sesv2_shapes and regen_ecs_shapes,
+and of the constraints regen_resource_groups_shapes and regen_ecr_shapes vendor.
Run with: pytest tools/aws -q (or: make aws-data-test)
"""
@@ -11,6 +12,7 @@ from pathlib import Path
import pytest
import regen_dynamodb_shapes
+import regen_ecr_shapes
import regen_ecs_shapes
import regen_sesv2_shapes
import request_shapes as r
@@ -178,3 +180,47 @@ def test_each_script_runs_the_shared_main_with_its_model_and_output(script, mode
"output": r.REPO_ROOT / "src/main/resources/aws" / output,
"description": script.__doc__,
})]
+
+
+CONSTRAINED_MODEL = {
+ "operations": {
+ "Put": {"input": {"shape": "PutRequest"}, "output": {"shape": "PutResponse"}},
+ "Unused": {"input": {"shape": "UnusedRequest"}},
+ },
+ "shapes": {
+ "PutRequest": {"type": "structure", "required": ["name"], "members": {
+ "name": {"shape": "Name", "documentation": "dropped"},
+ "rules": {"shape": "RuleList"},
+ "tags": {"shape": "TagMap"},
+ "id": {"shape": "Name", "location": "uri", "locationName": "Id"},
+ }},
+ "PutResponse": {"type": "structure", "members": {}},
+ "UnusedRequest": {"type": "structure", "members": {"other": {"shape": "Other"}}},
+ "Name": {"type": "string", "min": 1, "max": 5, "pattern": "[a-z]+", "documentation": "dropped"},
+ "RuleList": {"type": "list", "min": 0, "max": 25, "member": {"shape": "Kind"}},
+ "Kind": {"type": "string", "enum": ["A", "B"]},
+ "TagMap": {"type": "map", "key": {"shape": "Name"}, "value": {"shape": "Kind"}},
+ "Other": {"type": "string"},
+ },
+}
+
+
+def test_build_constraints_keeps_the_named_operations_with_their_constraints():
+ doc = r.build_constraints(CONSTRAINED_MODEL, ("Put",), "botocore test", "tools/aws/regen_x_shapes.py")
+
+ assert doc["_source"] == {"generator": "tools/aws/regen_x_shapes.py", "botocore": "botocore test"}
+ assert doc["operations"] == {"Put": "PutRequest"}
+ assert set(doc["shapes"]) == {"PutRequest", "Name", "RuleList", "Kind", "TagMap"}
+ # Members bound elsewhere than the body stay: the JSON protocols send every member in the body.
+ assert doc["shapes"]["PutRequest"] == {"type": "structure", "required": ["name"], "members": {
+ "name": "Name", "rules": "RuleList", "tags": "TagMap", "id": "Name"}}
+ assert doc["shapes"]["Name"] == {"type": "string", "min": 1, "max": 5, "pattern": "[a-z]+"}
+ assert doc["shapes"]["RuleList"] == {"type": "list", "min": 0, "max": 25, "member": "Kind"}
+ assert doc["shapes"]["Kind"] == {"type": "string", "enum": ["A", "B"]}
+ assert doc["shapes"]["TagMap"] == {"type": "map", "key": "Name", "value": "Kind"}
+
+
+def test_ecr_shapes_name_the_ecr_model_and_its_registry_setting_actions():
+ assert regen_ecr_shapes.MODEL == Path("ecr/2015-09-21")
+ assert regen_ecr_shapes.OUTPUT.name == "ecr-request-shapes.json"
+ assert "PutReplicationConfiguration" in regen_ecr_shapes.OPERATIONS