mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -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
@@ -40,6 +40,16 @@
|
||||
| `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) |
|
||||
| `GetAccountSetting` / `PutAccountSetting` | Read and store account settings for the registry (stored, not acted on) |
|
||||
|
||||
`GetAccountSetting` and `PutAccountSetting` keep values per account and Region.
|
||||
Supported names are `BASIC_SCAN_TYPE_VERSION` (`AWS_NATIVE`), `REGISTRY_POLICY_SCOPE`
|
||||
(`V2`), and `BLOB_MOUNTING` (`ENABLED` or `DISABLED`). Floci defaults to `AWS_NATIVE`,
|
||||
`V2`, and `DISABLED`, respectively. Unsupported setting names and values return
|
||||
`ValidationException`. Requests are checked against the vendored ECR model: missing
|
||||
or non-string members and names outside 1-64 characters return `InvalidParameterException`.
|
||||
These settings are metadata only: they do not start scanning, enforce registry policies,
|
||||
or change the registry's blob upload behavior.
|
||||
|
||||
`ListImages` accepts `maxResults` from 1 to 1000 (default 100) and returns `nextToken`
|
||||
when another page remains. Tokens are scoped to the region, registry, and repository;
|
||||
|
||||
@@ -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; }
|
||||
}
|
||||
@@ -4,6 +4,8 @@
|
||||
"botocore": "botocore 1.43.113 (installed package)"
|
||||
},
|
||||
"operations": {
|
||||
"GetAccountSetting": "GetAccountSettingRequest",
|
||||
"PutAccountSetting": "PutAccountSettingRequest",
|
||||
"PutRegistryPolicy": "PutRegistryPolicyRequest",
|
||||
"GetRegistryPolicy": "GetRegistryPolicyRequest",
|
||||
"DeleteRegistryPolicy": "DeleteRegistryPolicyRequest",
|
||||
@@ -18,6 +20,14 @@
|
||||
"DeleteRepositoryCreationTemplate": "DeleteRepositoryCreationTemplateRequest"
|
||||
},
|
||||
"shapes": {
|
||||
"AccountSettingName": {
|
||||
"type": "string",
|
||||
"min": 1,
|
||||
"max": 64
|
||||
},
|
||||
"AccountSettingValue": {
|
||||
"type": "string"
|
||||
},
|
||||
"BatchGetRepositoryScanningConfigurationRequest": {
|
||||
"type": "structure",
|
||||
"required": [
|
||||
@@ -93,6 +103,15 @@
|
||||
"KMS_DSSE"
|
||||
]
|
||||
},
|
||||
"GetAccountSettingRequest": {
|
||||
"type": "structure",
|
||||
"required": [
|
||||
"name"
|
||||
],
|
||||
"members": {
|
||||
"name": "AccountSettingName"
|
||||
}
|
||||
},
|
||||
"GetRegistryPolicyRequest": {
|
||||
"type": "structure",
|
||||
"members": {}
|
||||
@@ -168,6 +187,17 @@
|
||||
"type": "list",
|
||||
"member": "Prefix"
|
||||
},
|
||||
"PutAccountSettingRequest": {
|
||||
"type": "structure",
|
||||
"required": [
|
||||
"name",
|
||||
"value"
|
||||
],
|
||||
"members": {
|
||||
"name": "AccountSettingName",
|
||||
"value": "AccountSettingValue"
|
||||
}
|
||||
},
|
||||
"PutRegistryPolicyRequest": {
|
||||
"type": "structure",
|
||||
"required": [
|
||||
|
||||
+133
@@ -0,0 +1,133 @@
|
||||
package io.github.hectorvent.floci.services.ecr;
|
||||
|
||||
import io.github.hectorvent.floci.testing.AwsTestClients;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.NullSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.ecr.EcrClient;
|
||||
import software.amazon.awssdk.services.ecr.model.GetAccountSettingResponse;
|
||||
import software.amazon.awssdk.services.ecr.model.InvalidParameterException;
|
||||
import software.amazon.awssdk.services.ecr.model.PutAccountSettingResponse;
|
||||
import software.amazon.awssdk.services.ecr.model.ValidationException;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
@QuarkusTest
|
||||
class EcrAccountSettingsIntegrationTest {
|
||||
private static final AtomicLong ACCOUNTS = new AtomicLong(741682900000L);
|
||||
private static final String REGION = "eu-west-2";
|
||||
private static final Map<String, String> DEFAULTS = Map.of(
|
||||
"BASIC_SCAN_TYPE_VERSION", "AWS_NATIVE", "REGISTRY_POLICY_SCOPE", "V2", "BLOB_MOUNTING", "DISABLED");
|
||||
private String account;
|
||||
private EcrClient client;
|
||||
|
||||
@BeforeAll
|
||||
static void configureAwsJson() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void openClient() {
|
||||
account = String.valueOf(ACCOUNTS.incrementAndGet());
|
||||
client = AwsTestClients.create(EcrClient.builder(), account, Region.of(REGION));
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void closeClient() {
|
||||
client.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultsAreReturnedByName() {
|
||||
DEFAULTS.forEach((name, value) -> {
|
||||
GetAccountSettingResponse response = client.getAccountSetting(request -> request.name(name));
|
||||
assertEquals(name, response.name());
|
||||
assertEquals(value, response.value());
|
||||
});
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"BASIC_SCAN_TYPE_VERSION,AWS_NATIVE", "REGISTRY_POLICY_SCOPE,V2",
|
||||
"BLOB_MOUNTING,ENABLED", "BLOB_MOUNTING,DISABLED"})
|
||||
void validValuesRoundTrip(String name, String value) {
|
||||
PutAccountSettingResponse response = client.putAccountSetting(request -> request.name(name).value(value));
|
||||
assertEquals(name, response.name());
|
||||
assertEquals(value, response.value());
|
||||
assertEquals(value, client.getAccountSetting(request -> request.name(name)).value());
|
||||
}
|
||||
|
||||
@Test
|
||||
void accountAndRegionAreIsolated() {
|
||||
client.putAccountSetting(request -> request.name("BLOB_MOUNTING").value("ENABLED"));
|
||||
try (EcrClient other = AwsTestClients.create(EcrClient.builder(), "741682999999", Region.of(REGION));
|
||||
EcrClient region = AwsTestClients.create(EcrClient.builder(), account, Region.US_EAST_1)) {
|
||||
assertEquals("DISABLED", other.getAccountSetting(request -> request.name("BLOB_MOUNTING")).value());
|
||||
assertEquals("DISABLED", region.getAccountSetting(request -> request.name("BLOB_MOUNTING")).value());
|
||||
}
|
||||
assertEquals("ENABLED", client.getAccountSetting(request -> request.name("BLOB_MOUNTING")).value());
|
||||
assertEquals("AWS_NATIVE", client.getAccountSetting(request -> request.name("BASIC_SCAN_TYPE_VERSION")).value());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"UNKNOWN", "blob_mounting"})
|
||||
void unsupportedNamesAreValidationErrors(String name) {
|
||||
assertThrows(ValidationException.class, () -> client.getAccountSetting(request -> request.name(name)));
|
||||
assertThrows(ValidationException.class, () -> client.putAccountSetting(request -> request.name(name).value("ENABLED")));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@NullSource
|
||||
@ValueSource(strings = {""})
|
||||
void missingAndEmptyNamesUseTheRequestModelError(String name) {
|
||||
assertThrows(InvalidParameterException.class, () -> client.getAccountSetting(request -> request.name(name)));
|
||||
assertThrows(InvalidParameterException.class, () -> client.putAccountSetting(request -> request.name(name).value("ENABLED")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void namesLongerThanTheModelLimitAreRejectedBeforeSettingLookup() {
|
||||
String name = "x".repeat(65);
|
||||
assertThrows(InvalidParameterException.class, () -> client.getAccountSetting(request -> request.name(name)));
|
||||
assertThrows(InvalidParameterException.class, () -> client.putAccountSetting(request -> request.name(name).value("ENABLED")));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"{}", "{\"name\":null}", "{\"name\":1}", "{\"name\":[]}", "{\"name\":{}}"})
|
||||
void getSettingUsesModelValidation(String body) {
|
||||
given().contentType("application/x-amz-json-1.1")
|
||||
.header("X-Amz-Target", "AmazonEC2ContainerRegistry_V20150921.GetAccountSetting")
|
||||
.body(body).post("/").then().statusCode(400).body("__type", equalTo("InvalidParameterException"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"BASIC_SCAN_TYPE_VERSION,CLAIR", "REGISTRY_POLICY_SCOPE,V1", "BLOB_MOUNTING,enabled",
|
||||
"BLOB_MOUNTING,AWS_NATIVE", "REGISTRY_POLICY_SCOPE,ENABLED"})
|
||||
void invalidValuesLeaveStoredSettingsUnchanged(String name, String value) {
|
||||
client.putAccountSetting(request -> request.name("BLOB_MOUNTING").value("ENABLED"));
|
||||
assertThrows(ValidationException.class, () -> client.putAccountSetting(request -> request.name(name).value(value)));
|
||||
assertEquals("ENABLED", client.getAccountSetting(request -> request.name("BLOB_MOUNTING")).value());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"{}", "{\"name\":1}", "{\"name\":[]}", "{\"name\":{}}",
|
||||
"{\"name\":\"BLOB_MOUNTING\"}", "{\"name\":\"BLOB_MOUNTING\",\"value\":null}",
|
||||
"{\"name\":\"BLOB_MOUNTING\",\"value\":true}", "{\"name\":\"BLOB_MOUNTING\",\"value\":{}}"})
|
||||
void missingOrNonStringInputsAreRejected(String body) {
|
||||
given().contentType("application/x-amz-json-1.1")
|
||||
.header("X-Amz-Target", "AmazonEC2ContainerRegistry_V20150921.PutAccountSetting")
|
||||
.body(body).post("/").then().statusCode(400).body("__type", equalTo("InvalidParameterException"));
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package io.github.hectorvent.floci.services.ecr;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.services.ecr.model.RegistrySettings;
|
||||
import io.github.hectorvent.floci.services.ecr.registry.EcrRegistryManager;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
class EcrAccountSettingsPersistenceTest {
|
||||
private static final String REGION = "eu-west-2";
|
||||
private static final String ACCOUNT = "741682900099";
|
||||
private static final String KEY = REGION + "::" + ACCOUNT;
|
||||
|
||||
@Test
|
||||
void legacySettingsLoadAndAccountValuesSurviveSerializationAndOtherUpdates() throws Exception {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
InMemoryStorage<String, RegistrySettings> store = new InMemoryStorage<>();
|
||||
store.put(KEY, mapper.readValue("{\"scanType\":\"ENHANCED\",\"policyText\":\"{}\"}", RegistrySettings.class));
|
||||
EcrService service = service(store);
|
||||
assertEquals("DISABLED", service.getAccountSetting("BLOB_MOUNTING", REGION));
|
||||
service.putAccountSetting("BLOB_MOUNTING", "ENABLED", REGION);
|
||||
assertEquals("ENHANCED", service.registryScanningConfiguration(REGION).getScanType());
|
||||
assertEquals("{}", service.getRegistryPolicy(REGION));
|
||||
|
||||
String saved = mapper.writeValueAsString(store.get(KEY).orElseThrow());
|
||||
InMemoryStorage<String, RegistrySettings> reloadedStore = new InMemoryStorage<>();
|
||||
reloadedStore.put(KEY, mapper.readValue(saved, RegistrySettings.class));
|
||||
EcrService reloaded = service(reloadedStore);
|
||||
assertEquals("ENABLED", reloaded.getAccountSetting("BLOB_MOUNTING", REGION));
|
||||
reloaded.putRegistryScanningConfiguration("BASIC", List.of(), REGION);
|
||||
reloaded.deleteRegistryPolicy(REGION);
|
||||
assertEquals("ENABLED", reloaded.getAccountSetting("BLOB_MOUNTING", REGION));
|
||||
}
|
||||
|
||||
private EcrService service(InMemoryStorage<String, RegistrySettings> settings) {
|
||||
return new EcrService(new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
settings, new InMemoryStorage<>(), mock(EcrRegistryManager.class), mock(EmulatorConfig.class),
|
||||
new RegionResolver(REGION, ACCOUNT));
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ 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 = (
|
||||
"GetAccountSetting", "PutAccountSetting",
|
||||
"PutRegistryPolicy", "GetRegistryPolicy", "DeleteRegistryPolicy",
|
||||
"PutReplicationConfiguration", "DescribeRegistry",
|
||||
"PutRegistryScanningConfiguration", "GetRegistryScanningConfiguration",
|
||||
|
||||
Reference in new issue
Block a user