mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(ecr): repository creation templates (#5955)
* feat(ecr): repository creation templates CreateRepositoryCreationTemplate, DescribeRepositoryCreationTemplates, UpdateRepositoryCreationTemplate and DeleteRepositoryCreationTemplate store templates per account and Region, keyed by prefix, with each field checked against the service model. An update replaces the fields it sets, and an empty string or list clears one. Templates are recorded, not applied, as nothing in Floci creates repositories through replication, pull through cache or create-on-push. Closes #5804 * refactor(ecr): check repository creation templates against the vendored model The four template actions join the registry settings in ecr-request-shapes.json, so the handler checks their requests against the ECR model's types, lengths, patterns and enums and answers in ECR's "failed to satisfy constraint" form. The handwritten copies of those rules go. What the model can't state stays in the service: appliedFor naming at least one value, and policy text being a JSON object. The model gives imageTagMutabilityExclusionFilters at least one entry, so an update sending an empty list is now refused rather than clearing the filters. Terraform omits the member when the filters are removed and moves to a mode without exclusions, so an update that leaves the template in such a mode now drops the filters. Before, they stayed, and every plan showed them again. * fix(ecr): lock creation template writes, share one JSON object check and fold a trailing slash Create, update and delete each read a template and then wrote it without a lock, so two creates of one prefix could both succeed and an update racing a delete wrote the deleted template back. They now take the registry lock the registry settings use. The template policies and the registry policy share one JSON object check that also refuses text after the object, such as {}{}. ECR assumes a / after every prefix, so prod and prod/ are now one template. The test helper imports Executable. * fix(ecr): describe each template once when prefixes differ only by a slash Describe removed duplicate prefixes before folding the trailing slash, so prefixes prod and prod/ returned the template twice, and with maxResults 1 a token followed by an empty page. It now removes duplicates by template key. The lock tests use the shared testing ParkingStore instead of their own copy.
This commit is contained in:
1 parent
6ff4700a22
commit
d10db3ce9b
12 files changed
+1120
-18
No files matched your search
@@ -16,6 +16,7 @@ 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 io.github.hectorvent.floci.services.ecr.model.RepositoryCreationTemplate;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
@@ -49,7 +50,9 @@ public class EcrJsonHandler {
|
||||
"PutRegistryPolicy", "GetRegistryPolicy", "DeleteRegistryPolicy",
|
||||
"PutReplicationConfiguration", "DescribeRegistry",
|
||||
"PutRegistryScanningConfiguration", "GetRegistryScanningConfiguration",
|
||||
"BatchGetRepositoryScanningConfiguration");
|
||||
"BatchGetRepositoryScanningConfiguration",
|
||||
"CreateRepositoryCreationTemplate", "UpdateRepositoryCreationTemplate",
|
||||
"DescribeRepositoryCreationTemplates", "DeleteRepositoryCreationTemplate");
|
||||
|
||||
public Response handle(String action, JsonNode request, String region) {
|
||||
if (MODEL_CHECKED.contains(action)) {
|
||||
@@ -62,6 +65,10 @@ public class EcrJsonHandler {
|
||||
case "UpdatePullThroughCacheRule" -> handleUpdatePullThroughCacheRule(request, region);
|
||||
case "ValidatePullThroughCacheRule" -> handleValidatePullThroughCacheRule(request, region);
|
||||
case "DeletePullThroughCacheRule" -> handleDeletePullThroughCacheRule(request, region);
|
||||
case "CreateRepositoryCreationTemplate" -> handleCreateRepositoryCreationTemplate(request, region);
|
||||
case "DescribeRepositoryCreationTemplates" -> handleDescribeRepositoryCreationTemplates(request, region);
|
||||
case "UpdateRepositoryCreationTemplate" -> handleUpdateRepositoryCreationTemplate(request, region);
|
||||
case "DeleteRepositoryCreationTemplate" -> handleDeleteRepositoryCreationTemplate(request, region);
|
||||
case "DescribeRepositories" -> handleDescribeRepositories(request, region);
|
||||
case "PutImageScanningConfiguration" -> handlePutImageScanningConfiguration(request, region);
|
||||
case "BatchGetRepositoryScanningConfiguration" ->
|
||||
@@ -850,6 +857,121 @@ public class EcrJsonHandler {
|
||||
return n;
|
||||
}
|
||||
|
||||
// ──────────────────────────── Repository creation templates ────────────────────────────
|
||||
|
||||
private Response handleCreateRepositoryCreationTemplate(JsonNode request, String region) {
|
||||
return templateResponse(service.createRepositoryCreationTemplate(parseTemplate(request), region));
|
||||
}
|
||||
|
||||
private Response handleUpdateRepositoryCreationTemplate(JsonNode request, String region) {
|
||||
return templateResponse(service.updateRepositoryCreationTemplate(parseTemplate(request), region));
|
||||
}
|
||||
|
||||
private Response handleDeleteRepositoryCreationTemplate(JsonNode request, String region) {
|
||||
return templateResponse(service.deleteRepositoryCreationTemplate(
|
||||
request.path("prefix").asText(null), region));
|
||||
}
|
||||
|
||||
private Response handleDescribeRepositoryCreationTemplates(JsonNode request, String region) {
|
||||
JsonNode prefixesNode = request.path("prefixes");
|
||||
List<String> prefixes = prefixesNode.isMissingNode() || prefixesNode.isNull()
|
||||
? null
|
||||
: parseStringList(prefixesNode);
|
||||
PaginatedResult<RepositoryCreationTemplate> result = service.describeRepositoryCreationTemplates(
|
||||
prefixes, parseMaxResults(request.path("maxResults")), request.path("nextToken").asText(null), region);
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.put("registryId", service.templateRegistryId());
|
||||
ArrayNode templates = response.putArray("repositoryCreationTemplates");
|
||||
result.items().forEach(t -> templates.add(buildTemplate(t)));
|
||||
if (result.nextToken() != null) {
|
||||
response.put("nextToken", result.nextToken());
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response templateResponse(RepositoryCreationTemplate template) {
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.put("registryId", service.templateRegistryId());
|
||||
response.set("repositoryCreationTemplate", buildTemplate(template));
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
/** The template fields a request sets; each one it leaves out stays null. */
|
||||
private static RepositoryCreationTemplate parseTemplate(JsonNode request) {
|
||||
RepositoryCreationTemplate t = new RepositoryCreationTemplate();
|
||||
t.setPrefix(request.path("prefix").asText(null));
|
||||
t.setDescription(request.path("description").asText(null));
|
||||
JsonNode encryption = request.path("encryptionConfiguration");
|
||||
if (encryption.isObject()) {
|
||||
t.setEncryptionConfiguration(new RepositoryCreationTemplate.EncryptionConfiguration(
|
||||
encryption.path("encryptionType").asText(null), encryption.path("kmsKey").asText(null)));
|
||||
}
|
||||
if (request.path("resourceTags").isArray()) {
|
||||
List<RepositoryCreationTemplate.ResourceTag> tags = new ArrayList<>();
|
||||
request.path("resourceTags").forEach(tag -> tags.add(new RepositoryCreationTemplate.ResourceTag(
|
||||
tag.path("Key").asText(null), tag.path("Value").asText(null))));
|
||||
t.setResourceTags(tags);
|
||||
}
|
||||
t.setImageTagMutability(request.path("imageTagMutability").asText(null));
|
||||
if (request.path("imageTagMutabilityExclusionFilters").isArray()) {
|
||||
List<RepositoryCreationTemplate.ExclusionFilter> filters = new ArrayList<>();
|
||||
request.path("imageTagMutabilityExclusionFilters").forEach(f -> filters.add(
|
||||
new RepositoryCreationTemplate.ExclusionFilter(
|
||||
f.path("filterType").asText(null), f.path("filter").asText(null))));
|
||||
t.setImageTagMutabilityExclusionFilters(filters);
|
||||
}
|
||||
t.setRepositoryPolicy(request.path("repositoryPolicy").asText(null));
|
||||
t.setLifecyclePolicy(request.path("lifecyclePolicy").asText(null));
|
||||
if (request.path("appliedFor").isArray()) {
|
||||
t.setAppliedFor(parseStringList(request.path("appliedFor")));
|
||||
}
|
||||
t.setCustomRoleArn(request.path("customRoleArn").asText(null));
|
||||
return t;
|
||||
}
|
||||
|
||||
private ObjectNode buildTemplate(RepositoryCreationTemplate t) {
|
||||
ObjectNode n = objectMapper.createObjectNode();
|
||||
n.put("prefix", t.getPrefix());
|
||||
if (t.getDescription() != null) {
|
||||
n.put("description", t.getDescription());
|
||||
}
|
||||
if (t.getEncryptionConfiguration() != null) {
|
||||
ObjectNode enc = n.putObject("encryptionConfiguration");
|
||||
enc.put("encryptionType", t.getEncryptionConfiguration().encryptionType());
|
||||
if (t.getEncryptionConfiguration().kmsKey() != null) {
|
||||
enc.put("kmsKey", t.getEncryptionConfiguration().kmsKey());
|
||||
}
|
||||
}
|
||||
ArrayNode tags = n.putArray("resourceTags");
|
||||
if (t.getResourceTags() != null) {
|
||||
t.getResourceTags().forEach(tag -> tags.addObject().put("Key", tag.key()).put("Value", tag.value()));
|
||||
}
|
||||
if (t.getImageTagMutability() != null) {
|
||||
n.put("imageTagMutability", t.getImageTagMutability());
|
||||
}
|
||||
if (t.getImageTagMutabilityExclusionFilters() != null && !t.getImageTagMutabilityExclusionFilters().isEmpty()) {
|
||||
ArrayNode filters = n.putArray("imageTagMutabilityExclusionFilters");
|
||||
t.getImageTagMutabilityExclusionFilters().forEach(f -> filters.addObject()
|
||||
.put("filterType", f.filterType()).put("filter", f.filter()));
|
||||
}
|
||||
if (t.getRepositoryPolicy() != null) {
|
||||
n.put("repositoryPolicy", t.getRepositoryPolicy());
|
||||
}
|
||||
if (t.getLifecyclePolicy() != null) {
|
||||
n.put("lifecyclePolicy", t.getLifecyclePolicy());
|
||||
}
|
||||
ArrayNode appliedFor = n.putArray("appliedFor");
|
||||
if (t.getAppliedFor() != null) {
|
||||
t.getAppliedFor().forEach(appliedFor::add);
|
||||
}
|
||||
if (t.getCustomRoleArn() != null) {
|
||||
n.put("customRoleArn", t.getCustomRoleArn());
|
||||
}
|
||||
n.put("createdAt", t.getCreatedAt().getEpochSecond());
|
||||
n.put("updatedAt", t.getUpdatedAt().getEpochSecond());
|
||||
return n;
|
||||
}
|
||||
|
||||
private static List<String> parseStringList(JsonNode node) {
|
||||
if (node == null || node.isMissingNode() || node.isNull() || !node.isArray()) {
|
||||
return new ArrayList<>();
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package io.github.hectorvent.floci.services.ecr;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.ObjectReader;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
@@ -27,6 +30,7 @@ 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.model.RepositoryCreationTemplate;
|
||||
import io.github.hectorvent.floci.services.ecr.registry.EcrRegistryManager;
|
||||
import io.github.hectorvent.floci.services.ecr.registry.RegistryHttpClient;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
@@ -38,6 +42,7 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@@ -76,6 +81,8 @@ public class EcrService implements ResourceProvider {
|
||||
"ecr", "ecr-public", "quay", "k8s", "docker-hub",
|
||||
"github-container-registry", "azure-container-registry",
|
||||
"gitlab-container-registry", "chainguard");
|
||||
private static final ObjectReader SINGLE_JSON_VALUE =
|
||||
MAPPER.reader().with(DeserializationFeature.FAIL_ON_TRAILING_TOKENS);
|
||||
private static final int MAX_REPO_NAME_LENGTH = 256;
|
||||
private static final int MAX_PULL_THROUGH_CACHE_PREFIX_LENGTH = 30;
|
||||
|
||||
@@ -83,7 +90,8 @@ public class EcrService implements ResourceProvider {
|
||||
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 Map<String, Object> registryLocks = new ConcurrentHashMap<>();
|
||||
private final StorageBackend<String, RepositoryCreationTemplate> templateStore;
|
||||
private final EcrRegistryManager registryManager;
|
||||
private final EmulatorConfig config;
|
||||
private final RegionResolver regionResolver;
|
||||
@@ -103,6 +111,8 @@ public class EcrService implements ResourceProvider {
|
||||
new TypeReference<Map<String, PullThroughCacheRule>>() {}),
|
||||
factory.create("ecr", "registry-settings.json",
|
||||
new TypeReference<Map<String, RegistrySettings>>() {}),
|
||||
factory.create("ecr", "repository-creation-templates.json",
|
||||
new TypeReference<Map<String, RepositoryCreationTemplate>>() {}),
|
||||
registryManager, config, regionResolver);
|
||||
}
|
||||
|
||||
@@ -112,7 +122,7 @@ public class EcrService implements ResourceProvider {
|
||||
EcrRegistryManager registryManager,
|
||||
EmulatorConfig config,
|
||||
RegionResolver regionResolver) {
|
||||
this(repoStore, imageMetaStore, pullThroughCacheRuleStore, new InMemoryStorage<>(),
|
||||
this(repoStore, imageMetaStore, pullThroughCacheRuleStore, new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
registryManager, config, regionResolver);
|
||||
}
|
||||
|
||||
@@ -120,6 +130,7 @@ public class EcrService implements ResourceProvider {
|
||||
StorageBackend<String, ImageMetadata> imageMetaStore,
|
||||
StorageBackend<String, PullThroughCacheRule> pullThroughCacheRuleStore,
|
||||
StorageBackend<String, RegistrySettings> registrySettingsStore,
|
||||
StorageBackend<String, RepositoryCreationTemplate> templateStore,
|
||||
EcrRegistryManager registryManager,
|
||||
EmulatorConfig config,
|
||||
RegionResolver regionResolver) {
|
||||
@@ -127,6 +138,7 @@ public class EcrService implements ResourceProvider {
|
||||
this.imageMetaStore = imageMetaStore;
|
||||
this.pullThroughCacheRuleStore = pullThroughCacheRuleStore;
|
||||
this.registrySettingsStore = registrySettingsStore;
|
||||
this.templateStore = templateStore;
|
||||
this.registryManager = registryManager;
|
||||
this.config = config;
|
||||
this.regionResolver = regionResolver;
|
||||
@@ -324,6 +336,181 @@ public class EcrService implements ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// Repository creation templates
|
||||
// ============================================================
|
||||
|
||||
/**
|
||||
* Stores a template under its prefix. Fields left null take ECR's defaults: AES256 encryption
|
||||
* and mutable tags.
|
||||
*/
|
||||
public RepositoryCreationTemplate createRepositoryCreationTemplate(RepositoryCreationTemplate requested,
|
||||
String region) {
|
||||
validateTemplateFields(requested);
|
||||
String account = regionResolver.getAccountId();
|
||||
String key = templateKey(region, account, requested.getPrefix());
|
||||
synchronized (registryLock(region)) {
|
||||
if (templateStore.get(key).isPresent()) {
|
||||
throw new AwsException("TemplateAlreadyExistsException",
|
||||
"The repository creation template with prefix '" + requested.getPrefix()
|
||||
+ "' already exists in the registry with id '" + account + "'", 400);
|
||||
}
|
||||
RepositoryCreationTemplate template = new RepositoryCreationTemplate();
|
||||
template.setPrefix(requested.getPrefix());
|
||||
template.setEncryptionConfiguration(new RepositoryCreationTemplate.EncryptionConfiguration("AES256", null));
|
||||
template.setImageTagMutability("MUTABLE");
|
||||
applyTemplateFields(template, requested);
|
||||
Instant now = Instant.now();
|
||||
template.setCreatedAt(now);
|
||||
template.setUpdatedAt(now);
|
||||
templateStore.put(key, template);
|
||||
return template;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Every template in the account and Region, or those named in {@code prefixes}; a prefix with
|
||||
* no template is left out, as the model lists no not-found error for this action.
|
||||
*/
|
||||
public PaginatedResult<RepositoryCreationTemplate> describeRepositoryCreationTemplates(
|
||||
List<String> prefixes, Integer maxResults, String nextToken, String region) {
|
||||
String account = regionResolver.getAccountId();
|
||||
List<RepositoryCreationTemplate> templates;
|
||||
if (prefixes == null || prefixes.isEmpty()) {
|
||||
String keyPrefix = region + "::" + account + "::";
|
||||
templates = templateStore.scan(key -> key.startsWith(keyPrefix));
|
||||
} else {
|
||||
Set<String> keys = new LinkedHashSet<>();
|
||||
for (String prefix : prefixes) {
|
||||
keys.add(templateKey(region, account, prefix));
|
||||
}
|
||||
templates = new ArrayList<>();
|
||||
for (String key : keys) {
|
||||
templateStore.get(key).ifPresent(templates::add);
|
||||
}
|
||||
}
|
||||
return Pagination.paginate(templates, RepositoryCreationTemplate::getPrefix,
|
||||
maxResults, nextToken, 100, 1000, "InvalidParameterException");
|
||||
}
|
||||
|
||||
/** Replaces each field the request sets; an empty string or list clears it. */
|
||||
public RepositoryCreationTemplate updateRepositoryCreationTemplate(RepositoryCreationTemplate requested,
|
||||
String region) {
|
||||
validateTemplateFields(requested);
|
||||
String key = templateKey(region, regionResolver.getAccountId(), requested.getPrefix());
|
||||
synchronized (registryLock(region)) {
|
||||
RepositoryCreationTemplate template = templateStore.get(key)
|
||||
.orElseThrow(() -> templateNotFound(requested.getPrefix()));
|
||||
applyTemplateFields(template, requested);
|
||||
// Exclusion filters mean nothing outside a *_WITH_EXCLUSION mode. Terraform leaves the
|
||||
// member out when the filters are removed, so they go when the mode no longer takes them.
|
||||
if (!template.getImageTagMutability().endsWith("_WITH_EXCLUSION")) {
|
||||
template.setImageTagMutabilityExclusionFilters(null);
|
||||
}
|
||||
template.setUpdatedAt(Instant.now());
|
||||
templateStore.put(key, template);
|
||||
return template;
|
||||
}
|
||||
}
|
||||
|
||||
public RepositoryCreationTemplate deleteRepositoryCreationTemplate(String prefix, String region) {
|
||||
String key = templateKey(region, regionResolver.getAccountId(), prefix);
|
||||
synchronized (registryLock(region)) {
|
||||
RepositoryCreationTemplate template = templateStore.get(key).orElseThrow(() -> templateNotFound(prefix));
|
||||
templateStore.delete(key);
|
||||
return template;
|
||||
}
|
||||
}
|
||||
|
||||
/** The registry the templates belong to: the caller's account. */
|
||||
public String templateRegistryId() {
|
||||
return regionResolver.getAccountId();
|
||||
}
|
||||
|
||||
/**
|
||||
* The template rules the ECR model can't state; {@code EcrRequestShapes} has already checked
|
||||
* the request against the model's types, lengths, patterns and enums.
|
||||
*/
|
||||
private static void validateTemplateFields(RepositoryCreationTemplate t) {
|
||||
if (t.getAppliedFor() != null && t.getAppliedFor().isEmpty()) {
|
||||
throw invalidTemplate("appliedFor must name at least one of REPLICATION, PULL_THROUGH_CACHE "
|
||||
+ "or CREATE_ON_PUSH");
|
||||
}
|
||||
requireJsonObject(t.getRepositoryPolicy(), "repositoryPolicy");
|
||||
requireJsonObject(t.getLifecyclePolicy(), "lifecyclePolicy");
|
||||
}
|
||||
|
||||
private static void requireJsonObject(String text, String field) {
|
||||
if (text == null || text.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
if (!isJsonObject(text)) {
|
||||
throw invalidTemplate(field + " must be a JSON object");
|
||||
}
|
||||
}
|
||||
|
||||
/** Whether {@code text} is one JSON object with nothing after it, so {@code {} x} is not. */
|
||||
private static boolean isJsonObject(String text) {
|
||||
try {
|
||||
return SINGLE_JSON_VALUE.readTree(text).isObject();
|
||||
} catch (JsonProcessingException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Copies the fields the request sets; an empty string or list clears the stored value. */
|
||||
private static void applyTemplateFields(RepositoryCreationTemplate template, RepositoryCreationTemplate requested) {
|
||||
if (requested.getDescription() != null) {
|
||||
template.setDescription(emptyToNull(requested.getDescription()));
|
||||
}
|
||||
if (requested.getEncryptionConfiguration() != null) {
|
||||
RepositoryCreationTemplate.EncryptionConfiguration e = requested.getEncryptionConfiguration();
|
||||
template.setEncryptionConfiguration(new RepositoryCreationTemplate.EncryptionConfiguration(
|
||||
e.encryptionType(), emptyToNull(e.kmsKey())));
|
||||
}
|
||||
if (requested.getResourceTags() != null) {
|
||||
template.setResourceTags(List.copyOf(requested.getResourceTags()));
|
||||
}
|
||||
if (requested.getImageTagMutability() != null) {
|
||||
template.setImageTagMutability(requested.getImageTagMutability());
|
||||
}
|
||||
if (requested.getImageTagMutabilityExclusionFilters() != null) {
|
||||
template.setImageTagMutabilityExclusionFilters(List.copyOf(requested.getImageTagMutabilityExclusionFilters()));
|
||||
}
|
||||
if (requested.getRepositoryPolicy() != null) {
|
||||
template.setRepositoryPolicy(emptyToNull(requested.getRepositoryPolicy()));
|
||||
}
|
||||
if (requested.getLifecyclePolicy() != null) {
|
||||
template.setLifecyclePolicy(emptyToNull(requested.getLifecyclePolicy()));
|
||||
}
|
||||
if (requested.getAppliedFor() != null) {
|
||||
template.setAppliedFor(List.copyOf(new LinkedHashSet<>(requested.getAppliedFor())));
|
||||
}
|
||||
if (requested.getCustomRoleArn() != null) {
|
||||
template.setCustomRoleArn(emptyToNull(requested.getCustomRoleArn()));
|
||||
}
|
||||
}
|
||||
|
||||
private static String emptyToNull(String value) {
|
||||
return value == null || value.isEmpty() ? null : value;
|
||||
}
|
||||
|
||||
/** ECR assumes a {@code /} after every prefix, so {@code prod} and {@code prod/} are one template. */
|
||||
private static String templateKey(String region, String account, String prefix) {
|
||||
String namespace = prefix.endsWith("/") ? prefix.substring(0, prefix.length() - 1) : prefix;
|
||||
return region + "::" + account + "::" + namespace;
|
||||
}
|
||||
|
||||
private static AwsException invalidTemplate(String message) {
|
||||
return new AwsException("InvalidParameterException", message, 400);
|
||||
}
|
||||
|
||||
private AwsException templateNotFound(String prefix) {
|
||||
return new AwsException("TemplateNotFoundException",
|
||||
"The repository creation template with prefix '" + prefix
|
||||
+ "' does not exist in the registry with id '" + regionResolver.getAccountId() + "'", 400);
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// CreateRepository
|
||||
// ============================================================
|
||||
@@ -1040,13 +1227,7 @@ public class EcrService implements ResourceProvider {
|
||||
* 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()) {
|
||||
if (policyText == null || !isJsonObject(policyText)) {
|
||||
throw new AwsException("InvalidParameterException", "Invalid parameter at 'PolicyText' failed to satisfy "
|
||||
+ "constraint: 'Invalid registry policy provided'", 400);
|
||||
}
|
||||
@@ -1145,7 +1326,7 @@ public class EcrService implements ResourceProvider {
|
||||
*/
|
||||
private <T> T updateRegistrySettings(String region, Function<RegistrySettings, T> change) {
|
||||
String key = registrySettingsKey(region);
|
||||
synchronized (registrySettingsLocks.computeIfAbsent(key, k -> new Object())) {
|
||||
synchronized (registryLock(region)) {
|
||||
RegistrySettings settings = registrySettings(region);
|
||||
T result = change.apply(settings);
|
||||
registrySettingsStore.put(key, settings);
|
||||
@@ -1153,6 +1334,15 @@ public class EcrService implements ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* One lock per registry for every read and write back of its settings and creation templates:
|
||||
* unlocked, two creates of one template prefix could both succeed, and an update could write
|
||||
* back a template a delete had just removed.
|
||||
*/
|
||||
private Object registryLock(String region) {
|
||||
return registryLocks.computeIfAbsent(registrySettingsKey(region), k -> new Object());
|
||||
}
|
||||
|
||||
private String registrySettingsKey(String region) {
|
||||
return region + "::" + regionResolver.getAccountId();
|
||||
}
|
||||
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
package io.github.hectorvent.floci.services.ecr.model;
|
||||
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A repository creation template: the settings ECR gives a repository it creates for a
|
||||
* replication, pull through cache or create-on-push rule whose name starts with the template's
|
||||
* prefix. Floci records templates; nothing in Floci creates repositories that would use them.
|
||||
*/
|
||||
@RegisterForReflection
|
||||
public class RepositoryCreationTemplate {
|
||||
|
||||
/** The encryption a created repository gets. */
|
||||
@RegisterForReflection
|
||||
public record EncryptionConfiguration(String encryptionType, String kmsKey) {}
|
||||
|
||||
/** One tag a created repository gets. */
|
||||
@RegisterForReflection
|
||||
public record ResourceTag(String key, String value) {}
|
||||
|
||||
/** One tag filter exempt from the template's tag mutability setting. */
|
||||
@RegisterForReflection
|
||||
public record ExclusionFilter(String filterType, String filter) {}
|
||||
|
||||
private String prefix;
|
||||
private String description;
|
||||
private EncryptionConfiguration encryptionConfiguration;
|
||||
private List<ResourceTag> resourceTags;
|
||||
private String imageTagMutability;
|
||||
private List<ExclusionFilter> imageTagMutabilityExclusionFilters;
|
||||
private String repositoryPolicy;
|
||||
private String lifecyclePolicy;
|
||||
private List<String> appliedFor;
|
||||
private String customRoleArn;
|
||||
private Instant createdAt;
|
||||
private Instant updatedAt;
|
||||
|
||||
public RepositoryCreationTemplate() {}
|
||||
|
||||
public String getPrefix() { return prefix; }
|
||||
public void setPrefix(String prefix) { this.prefix = prefix; }
|
||||
|
||||
public String getDescription() { return description; }
|
||||
public void setDescription(String description) { this.description = description; }
|
||||
|
||||
public EncryptionConfiguration getEncryptionConfiguration() { return encryptionConfiguration; }
|
||||
public void setEncryptionConfiguration(EncryptionConfiguration encryptionConfiguration) {
|
||||
this.encryptionConfiguration = encryptionConfiguration;
|
||||
}
|
||||
|
||||
public List<ResourceTag> getResourceTags() { return resourceTags; }
|
||||
public void setResourceTags(List<ResourceTag> resourceTags) { this.resourceTags = resourceTags; }
|
||||
|
||||
public String getImageTagMutability() { return imageTagMutability; }
|
||||
public void setImageTagMutability(String imageTagMutability) { this.imageTagMutability = imageTagMutability; }
|
||||
|
||||
public List<ExclusionFilter> getImageTagMutabilityExclusionFilters() { return imageTagMutabilityExclusionFilters; }
|
||||
public void setImageTagMutabilityExclusionFilters(List<ExclusionFilter> imageTagMutabilityExclusionFilters) {
|
||||
this.imageTagMutabilityExclusionFilters = imageTagMutabilityExclusionFilters;
|
||||
}
|
||||
|
||||
public String getRepositoryPolicy() { return repositoryPolicy; }
|
||||
public void setRepositoryPolicy(String repositoryPolicy) { this.repositoryPolicy = repositoryPolicy; }
|
||||
|
||||
public String getLifecyclePolicy() { return lifecyclePolicy; }
|
||||
public void setLifecyclePolicy(String lifecyclePolicy) { this.lifecyclePolicy = lifecyclePolicy; }
|
||||
|
||||
public List<String> getAppliedFor() { return appliedFor; }
|
||||
public void setAppliedFor(List<String> appliedFor) { this.appliedFor = appliedFor; }
|
||||
|
||||
public String getCustomRoleArn() { return customRoleArn; }
|
||||
public void setCustomRoleArn(String customRoleArn) { this.customRoleArn = customRoleArn; }
|
||||
|
||||
public Instant getCreatedAt() { return createdAt; }
|
||||
public void setCreatedAt(Instant createdAt) { this.createdAt = createdAt; }
|
||||
|
||||
public Instant getUpdatedAt() { return updatedAt; }
|
||||
public void setUpdatedAt(Instant updatedAt) { this.updatedAt = updatedAt; }
|
||||
}
|
||||
Reference in new issue
Block a user