mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(ecr): support account settings (#6855)
* feat(ecr): support account settings * fix(ecr): validate account settings against request model
This commit is contained in:
1 parent
03da5da997
commit
e24bc36f64
8 files changed
+273
No files matched your search
@@ -47,6 +47,7 @@ public class EcrJsonHandler {
|
||||
|
||||
/** The actions whose requests are checked against the vendored model constraints first. */
|
||||
private static final Set<String> MODEL_CHECKED = Set.of(
|
||||
"GetAccountSetting", "PutAccountSetting",
|
||||
"PutRegistryPolicy", "GetRegistryPolicy", "DeleteRegistryPolicy",
|
||||
"PutReplicationConfiguration", "DescribeRegistry",
|
||||
"PutRegistryScanningConfiguration", "GetRegistryScanningConfiguration",
|
||||
@@ -59,6 +60,8 @@ public class EcrJsonHandler {
|
||||
EcrRequestShapes.check(action, request);
|
||||
}
|
||||
return switch (action) {
|
||||
case "GetAccountSetting" -> handleAccountSetting(request, region, false);
|
||||
case "PutAccountSetting" -> handleAccountSetting(request, region, true);
|
||||
case "CreateRepository" -> handleCreateRepository(request, region);
|
||||
case "CreatePullThroughCacheRule" -> handleCreatePullThroughCacheRule(request, region);
|
||||
case "DescribePullThroughCacheRules" -> handleDescribePullThroughCacheRules(request, region);
|
||||
@@ -110,6 +113,16 @@ public class EcrJsonHandler {
|
||||
};
|
||||
}
|
||||
|
||||
private Response handleAccountSetting(JsonNode request, String region, boolean put) {
|
||||
String name = request.path("name").asText(null);
|
||||
String value = put ? service.putAccountSetting(name, request.path("value").asText(null), region)
|
||||
: service.getAccountSetting(name, region);
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.put("name", name);
|
||||
response.put("value", value);
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handleCreateRepository(JsonNode request, String region) {
|
||||
String repositoryName = request.path("repositoryName").asText(null);
|
||||
String registryId = request.path("registryId").asText(null);
|
||||
|
||||
@@ -43,6 +43,7 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -86,6 +87,8 @@ public class EcrService implements ResourceProvider {
|
||||
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;
|
||||
private static final Map<String, String> ACCOUNT_SETTING_DEFAULTS = Map.of(
|
||||
"BASIC_SCAN_TYPE_VERSION", "AWS_NATIVE", "REGISTRY_POLICY_SCOPE", "V2", "BLOB_MOUNTING", "DISABLED");
|
||||
|
||||
private final StorageBackend<String, Repository> repoStore;
|
||||
private final StorageBackend<String, ImageMetadata> imageMetaStore;
|
||||
@@ -1491,6 +1494,37 @@ public class EcrService implements ResourceProvider {
|
||||
return regionResolver.getAccountId();
|
||||
}
|
||||
|
||||
public String getAccountSetting(String name, String region) {
|
||||
String defaultValue = accountSettingDefault(name);
|
||||
synchronized (registryLock(region)) {
|
||||
Map<String, String> values = registrySettings(region).getAccountSettings();
|
||||
return values == null ? defaultValue : values.getOrDefault(name, defaultValue);
|
||||
}
|
||||
}
|
||||
|
||||
public String putAccountSetting(String name, String value, String region) {
|
||||
String defaultValue = accountSettingDefault(name);
|
||||
boolean valid = "BLOB_MOUNTING".equals(name)
|
||||
? "ENABLED".equals(value) || "DISABLED".equals(value) : defaultValue.equals(value);
|
||||
if (!valid) {
|
||||
throw new AwsException("ValidationException", "Invalid value for account setting " + name + ".", 400);
|
||||
}
|
||||
return updateRegistrySettings(region, settings -> {
|
||||
Map<String, String> values = settings.getAccountSettings() == null
|
||||
? new HashMap<>() : new HashMap<>(settings.getAccountSettings());
|
||||
values.put(name, value);
|
||||
settings.setAccountSettings(Map.copyOf(values));
|
||||
return value;
|
||||
});
|
||||
}
|
||||
|
||||
private static String accountSettingDefault(String name) {
|
||||
if (name == null || !ACCOUNT_SETTING_DEFAULTS.containsKey(name)) {
|
||||
throw new AwsException("ValidationException", "Unsupported account setting name.", 400);
|
||||
}
|
||||
return ACCOUNT_SETTING_DEFAULTS.get(name);
|
||||
}
|
||||
|
||||
private RegistrySettings registrySettings(String region) {
|
||||
return registrySettingsStore.get(registrySettingsKey(region)).orElseGet(RegistrySettings::new);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.ecr.model;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* A registry's own settings, one record per account and Region: the registry policy, the
|
||||
@@ -32,6 +33,7 @@ public class RegistrySettings {
|
||||
private List<ReplicationRule> replicationRules;
|
||||
private String scanType;
|
||||
private List<ScanningRule> scanningRules;
|
||||
private Map<String, String> accountSettings;
|
||||
|
||||
public RegistrySettings() {}
|
||||
|
||||
@@ -46,4 +48,7 @@ public class RegistrySettings {
|
||||
|
||||
public List<ScanningRule> getScanningRules() { return scanningRules; }
|
||||
public void setScanningRules(List<ScanningRule> scanningRules) { this.scanningRules = scanningRules; }
|
||||
|
||||
public Map<String, String> getAccountSettings() { return accountSettings; }
|
||||
public void setAccountSettings(Map<String, String> accountSettings) { this.accountSettings = accountSettings; }
|
||||
}
|
||||
Reference in new issue
Block a user