Light, fluffy, and always free - The AWS Local Emulator alternative
fix(codeartifact): enforce domains-per-account and repositories-per-domain quotas (#3949)
Priamo Germosen15ed62f8cbf1
diff --git a/docs/services/codeartifact.md b/docs/services/codeartifact.mdindex 4ce56551b6..8b8a9c8f33 100644--- a/docs/services/codeartifact.md+++ b/docs/services/codeartifact.md@@ -15,14 +15,14 @@ proxy behind them yet.<!-- floci:actions:start -->| Action | Description || --- | --- |-| `CreateDomain` | Creates a domain, optionally with a KMS encryption key and initial tags. |+| `CreateDomain` | Creates a domain (max 10 per account per Region), optionally with a KMS encryption key and initial tags. || `DeleteDomain` | Deletes a domain; fails with `ConflictException` while it still contains repositories. || `DescribeDomain` | Returns a domain's full description, including its repository count. || `ListDomains` | Lists domain summaries for the account and Region, paginated. || `PutDomainPermissionsPolicy` | Attaches or replaces a domain's resource policy, versioned by `policyRevision`. || `GetDomainPermissionsPolicy` | Returns a domain's current resource policy and revision. || `DeleteDomainPermissionsPolicy` | Removes a domain's resource policy, optionally checked against `policyRevision`. |-| `CreateRepository` | Creates a repository with optional description, upstreams (max 10), and tags. |+| `CreateRepository` | Creates a repository (max 1,000 per domain) with optional description, upstreams (max 10), and tags. || `DeleteRepository` | Deletes a repository. || `DescribeRepository` | Returns a repository's full description, including upstreams and external connections. || `UpdateRepository` | Updates a repository's description and/or upstream list. |@@ -45,7 +45,10 @@ proxy behind them yet.Domains and repositories are account and Region scoped and persisted through `StorageFactory`.`DeleteDomain` fails with `ConflictException` while the domain still contains repositories, matchingAWS. `PutDomainPermissionsPolicy`/`PutRepositoryPermissionsPolicy` use the returned `policyRevision`-for optimistic locking on subsequent updates, also matching AWS.+for optimistic locking on subsequent updates, also matching AWS. Floci enforces AWS's own account+and domain quotas: `CreateDomain` caps a single account at 10 domains per Region, and+`CreateRepository` caps a single domain at 1,000 repositories, both returning+`ServiceQuotaExceededException` with the offending `resourceId`/`resourceType` once reached.`AssociateExternalConnection` accepts the same fixed set of AWS-hosted public upstreamsdocumented for real CodeArtifact (`public:npmjs`, `public:pypi`, `public:maven-central`, etc.) anddiff --git a/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactService.java b/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactService.javaindex 56c86b68de..9d8434662c 100644--- a/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactService.java+++ b/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactService.java@@ -76,6 +76,8 @@ public class CodeArtifactService implements Resettable {private static final List<String> ASSET_HASH_ALGORITHMS = List.of("MD5", "SHA-1", "SHA-256", "SHA-512");private static final long MAX_ASSET_FILE_SIZE_BYTES = 5L * 1024 * 1024 * 1024;private static final int MAX_ASSETS_PER_PACKAGE_VERSION = 350;+ private static final int MAX_DOMAINS_PER_ACCOUNT = 10;+ private static final int MAX_REPOSITORIES_PER_DOMAIN = 1000;private static final String ASSET_STORAGE_DIR = "codeartifact-assets";public record DomainView(CodeArtifactDomain domain, int repositoryCount) {}@@ -142,6 +144,11 @@ public class CodeArtifactService implements Resettable {if (domains.getForAccount(owner, key).isPresent()) {throw conflict("Domain with name '" + domain + "' already exists.");}+ if (domainCountForAccount(owner, region) >= MAX_DOMAINS_PER_ACCOUNT) {+ throw new AwsException("ServiceQuotaExceededException",+ "An AWS account can have a maximum of " + MAX_DOMAINS_PER_ACCOUNT + " domains.", 402,+ Map.of("resourceId", domain, "resourceType", "domain"));+ }CodeArtifactDomain d = new CodeArtifactDomain();d.setName(domain);d.setOwner(owner);@@ -235,6 +242,11 @@ public class CodeArtifactService implements Resettable {if (repositories.getForAccount(owner, key).isPresent()) {throw conflict("Repository with name '" + repository + "' already exists in domain '" + domain + "'.");}+ if (repositoryCountForDomain(owner, region, domain) >= MAX_REPOSITORIES_PER_DOMAIN) {+ throw new AwsException("ServiceQuotaExceededException",+ "A domain can have a maximum of " + MAX_REPOSITORIES_PER_DOMAIN + " repositories.", 402,+ Map.of("resourceId", repository, "resourceType", "repository"));+ }validateUpstreams(owner, region, domain, repository, upstreams);validateDescription(description);@@ -622,6 +634,10 @@ public class CodeArtifactService implements Resettable {return repositories.scanForAccount(owner, k -> k.startsWith(region + "::" + domain + "::")).size();}+ private int domainCountForAccount(String owner, String region) {+ return domains.scanForAccount(owner, k -> k.startsWith(region + "::")).size();+ }+private void validateUpstreams(String owner, String region, String domain, String repository,List<String> upstreams) {if (upstreams == null) {diff --git a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactServiceTest.javaindex 9c97414a22..8c0d6dddc7 100644--- a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactServiceTest.java@@ -39,6 +39,7 @@ class CodeArtifactServiceTest {private static final String ACCOUNT_ID = "123456789012";private CodeArtifactService service;+ private RegionResolver regionResolver;@BeforeEachvoid setUp() {@@ -47,7 +48,7 @@ class CodeArtifactServiceTest {AccountAwareStorageBackend<CodeArtifactPackageVersion> packageVersionStore =AccountAwareStorageBackend.inMemory(ACCOUNT_ID);- RegionResolver regionResolver = mock(RegionResolver.class);+ regionResolver = mock(RegionResolver.class);when(regionResolver.getAccountId()).thenReturn(ACCOUNT_ID);when(regionResolver.buildArn(anyString(), anyString(), anyString())).thenAnswer(invocation ->"arn:aws:" + invocation.getArgument(0, String.class) + ":" + invocation.getArgument(1, String.class)@@ -93,6 +94,59 @@ class CodeArtifactServiceTest {assertEquals(1, service.listDomains(OTHER_REGION, null, null).items().size());}+ @Test+ void createDomainRejectsMoreThanTenDomainsPerAccount() {+ for (int i = 0; i < 10; i++) {+ service.createDomain(REGION, "dom-" + i, null, Map.of());+ }+ AwsException e = assertThrows(AwsException.class,+ () -> service.createDomain(REGION, "dom-overflow", null, Map.of()));+ assertEquals("ServiceQuotaExceededException", e.getErrorCode());+ assertEquals(402, e.getHttpStatus());+ assertEquals("dom-overflow", e.getExtendedData().get("resourceId"));+ assertEquals("domain", e.getExtendedData().get("resourceType"));+ }++ @Test+ void createDomainAtCapStillReturnsConflictForDuplicateName() {+ for (int i = 0; i < 10; i++) {+ service.createDomain(REGION, "dom-" + i, null, Map.of());+ }+ AwsException e = assertThrows(AwsException.class,+ () -> service.createDomain(REGION, "dom-0", null, Map.of()));+ assertEquals("ConflictException", e.getErrorCode());+ }++ @Test+ void deletingDomainFreesRoomForANewOne() {+ for (int i = 0; i < 10; i++) {+ service.createDomain(REGION, "dom-" + i, null, Map.of());+ }+ service.deleteDomain(REGION, "dom-0", null);+ DomainView view = service.createDomain(REGION, "dom-new", null, Map.of());+ assertEquals("dom-new", view.domain().getName());+ }++ @Test+ void domainQuotaIsScopedPerRegion() {+ for (int i = 0; i < 10; i++) {+ service.createDomain(REGION, "dom-" + i, null, Map.of());+ }+ DomainView otherRegion = service.createDomain(OTHER_REGION, "dom-0", null, Map.of());+ assertEquals("dom-0", otherRegion.domain().getName());+ }++ @Test+ void domainQuotaIsScopedPerAccount() {+ for (int i = 0; i < 10; i++) {+ service.createDomain(REGION, "dom-" + i, null, Map.of());+ }+ String otherAccount = "999999999999";+ when(regionResolver.getAccountId()).thenReturn(otherAccount);+ DomainView view = service.createDomain(REGION, "dom-0", null, Map.of());+ assertEquals(otherAccount, view.domain().getOwner());+ }+@Testvoid deleteDomainFailsWhileRepositoriesExist() {service.createDomain(REGION, "with-repo", null, Map.of());@@ -210,6 +264,40 @@ class CodeArtifactServiceTest {assertEquals("ServiceQuotaExceededException", e.getErrorCode());}+ @Test+ void createRepositoryRejectsMoreThanOneThousandRepositoriesPerDomain() {+ service.createDomain(REGION, "dom", null, Map.of());+ for (int i = 0; i < 1000; i++) {+ service.createRepository(REGION, "dom", null, "repo-" + i, null, null, Map.of());+ }+ AwsException e = assertThrows(AwsException.class,+ () -> service.createRepository(REGION, "dom", null, "overflow", null, null, Map.of()));+ assertEquals("ServiceQuotaExceededException", e.getErrorCode());+ assertEquals(402, e.getHttpStatus());+ assertEquals("overflow", e.getExtendedData().get("resourceId"));+ assertEquals("repository", e.getExtendedData().get("resourceType"));++ AwsException duplicate = assertThrows(AwsException.class,+ () -> service.createRepository(REGION, "dom", null, "repo-0", null, null, Map.of()));+ assertEquals("ConflictException", duplicate.getErrorCode());++ service.deleteRepository(REGION, "dom", null, "repo-0");+ CodeArtifactRepository freed = service.createRepository(REGION, "dom", null, "repo-new", null, null,+ Map.of());+ assertEquals("repo-new", freed.getName());+ }++ @Test+ void repositoryQuotaIsScopedPerDomain() {+ service.createDomain(REGION, "dom", null, Map.of());+ for (int i = 0; i < 1000; i++) {+ service.createRepository(REGION, "dom", null, "repo-" + i, null, null, Map.of());+ }+ service.createDomain(REGION, "dom-2", null, Map.of());+ CodeArtifactRepository r = service.createRepository(REGION, "dom-2", null, "repo-0", null, null, Map.of());+ assertEquals("repo-0", r.getName());+ }+@Testvoid listRepositoriesInDomainFiltersByPrefixAndDomain() {service.createDomain(REGION, "dom-a", null, Map.of());