diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CodeArtifactTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CodeArtifactTest.java index c1b607bfca..26dd037761 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CodeArtifactTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CodeArtifactTest.java @@ -78,7 +78,10 @@ class CodeArtifactTest { @DisplayName("CreateDomain - duplicate returns ConflictException") void createDomainDuplicateFails() { assertThatThrownBy(() -> codeArtifact.createDomain(r -> r.domain(DOMAIN))) - .isInstanceOf(ConflictException.class); + .isInstanceOfSatisfying(ConflictException.class, e -> { + assertThat(e.resourceId()).isEqualTo(DOMAIN); + assertThat(e.resourceTypeAsString()).isEqualTo("domain"); + }); } @Test @@ -88,7 +91,10 @@ class CodeArtifactTest { assertThatThrownBy(() -> codeArtifact.createRepository(r -> r .domain("does-not-exist-domain") .repository(REPO))) - .isInstanceOf(ResourceNotFoundException.class); + .isInstanceOfSatisfying(ResourceNotFoundException.class, e -> { + assertThat(e.resourceId()).isEqualTo("does-not-exist-domain"); + assertThat(e.resourceTypeAsString()).isEqualTo("domain"); + }); } @Test diff --git a/docs/services/codeartifact.md b/docs/services/codeartifact.md index 8b8a9c8f33..b02ad6ee0b 100644 --- a/docs/services/codeartifact.md +++ b/docs/services/codeartifact.md @@ -68,9 +68,15 @@ returning `ServiceQuotaExceededException`. ## AWS-compatible failures Domain and repository names, tags, pagination, duplicate names, missing upstreams, policy-revision -mismatches, and non-empty-domain deletes are validated. Floci returns `ValidationException`, +mismatches, and non-empty-domain deletes are validated. Every action also validates its required +identifiers (`domain`, `repository`, `package`, `packageVersion`, `asset`) are present, returning +`ValidationException` rather than a misleading `ResourceNotFoundException` for one that's simply +missing from the request. Floci returns `ValidationException`, `ConflictException`, `ResourceNotFoundException`, and `ServiceQuotaExceededException` (tag limits) -for deterministic conditions represented by local state. +for deterministic conditions represented by local state. `ConflictException`, +`ResourceNotFoundException`, and `ServiceQuotaExceededException` all carry the `resourceId`/ +`resourceType` fields the wire model declares for them, matching the typed accessors the AWS SDK +exposes on those exceptions. AWS also models `AccessDeniedException`, `InternalServerException`, and `ThrottlingException`. Floci does not inject provider-side failures that cannot be derived from the request or emulator diff --git a/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactService.java b/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactService.java index 9d8434662c..37cfe3b7de 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 @@ -142,12 +142,12 @@ public class CodeArtifactService implements Resettable { String owner = regionResolver.getAccountId(); String key = domainKey(region, domain); if (domains.getForAccount(owner, key).isPresent()) { - throw conflict("Domain with name '" + domain + "' already exists."); + throw conflict("Domain with name '" + domain + "' already exists.", domain, "domain"); } 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")); + resourceFields(domain, "domain")); } CodeArtifactDomain d = new CodeArtifactDomain(); d.setName(domain); @@ -157,7 +157,7 @@ public class CodeArtifactService implements Resettable { d.setEncryptionKey(encryptionKey != null ? encryptionKey : regionResolver.buildArn("kms", region, "alias/aws/codeartifact")); d.setCreatedTime(Instant.now().getEpochSecond()); - d.setTags(validateTags(tags, Map.of())); + d.setTags(validateTags(tags, Map.of(), domain, "domain")); domains.putForAccount(owner, key, d); return new DomainView(d, 0); } @@ -165,11 +165,11 @@ public class CodeArtifactService implements Resettable { public synchronized DomainView deleteDomain(String region, String domain, String domainOwner) { String owner = effectiveOwner(domainOwner); String key = domainKey(region, domain); - CodeArtifactDomain d = requireDomain(owner, key); + CodeArtifactDomain d = requireDomain(owner, key, domain); int repoCount = repositoryCountForDomain(owner, region, domain); if (repoCount > 0) { throw conflict("Domain '" + domain + "' contains repositories and cannot be deleted " - + "until they are deleted."); + + "until they are deleted.", domain, "domain"); } domains.deleteForAccount(owner, key); return new DomainView(d, 0); @@ -177,7 +177,7 @@ public class CodeArtifactService implements Resettable { public DomainView describeDomain(String region, String domain, String domainOwner) { String owner = effectiveOwner(domainOwner); - CodeArtifactDomain d = requireDomain(owner, domainKey(region, domain)); + CodeArtifactDomain d = requireDomain(owner, domainKey(region, domain), domain); return new DomainView(d, repositoryCountForDomain(owner, region, domain)); } @@ -196,8 +196,8 @@ public class CodeArtifactService implements Resettable { String policyDocument, String policyRevision) { String owner = effectiveOwner(domainOwner); String key = domainKey(region, domain); - CodeArtifactDomain d = requireDomain(owner, key); - checkRevision(d.getPolicyRevision(), policyRevision); + CodeArtifactDomain d = requireDomain(owner, key, domain); + checkRevision(d.getPolicyRevision(), policyRevision, domain, "domain"); validatePolicyDocument(policyDocument); d.setPolicyDocument(policyDocument); d.setPolicyRevision(newRevision()); @@ -207,9 +207,9 @@ public class CodeArtifactService implements Resettable { public ResourcePolicy getDomainPermissionsPolicy(String region, String domain, String domainOwner) { String owner = effectiveOwner(domainOwner); - CodeArtifactDomain d = requireDomain(owner, domainKey(region, domain)); + CodeArtifactDomain d = requireDomain(owner, domainKey(region, domain), domain); if (d.getPolicyDocument() == null) { - throw notFound("No resource policy is associated with domain '" + domain + "'."); + throw notFound("No resource policy is associated with domain '" + domain + "'.", domain, "domain"); } return new ResourcePolicy(d.getArn(), d.getPolicyRevision(), d.getPolicyDocument()); } @@ -218,11 +218,11 @@ public class CodeArtifactService implements Resettable { String policyRevision) { String owner = effectiveOwner(domainOwner); String key = domainKey(region, domain); - CodeArtifactDomain d = requireDomain(owner, key); + CodeArtifactDomain d = requireDomain(owner, key, domain); if (d.getPolicyDocument() == null) { - throw notFound("No resource policy is associated with domain '" + domain + "'."); + throw notFound("No resource policy is associated with domain '" + domain + "'.", domain, "domain"); } - checkRevision(d.getPolicyRevision(), policyRevision); + checkRevision(d.getPolicyRevision(), policyRevision, domain, "domain"); ResourcePolicy removed = new ResourcePolicy(d.getArn(), d.getPolicyRevision(), d.getPolicyDocument()); d.setPolicyDocument(null); d.setPolicyRevision(null); @@ -237,15 +237,16 @@ public class CodeArtifactService implements Resettable { List upstreams, Map tags) { validateRepositoryName(repository); String owner = effectiveOwner(domainOwner); - requireDomain(owner, domainKey(region, domain)); + requireDomain(owner, domainKey(region, domain), domain); String key = repositoryKey(region, domain, repository); if (repositories.getForAccount(owner, key).isPresent()) { - throw conflict("Repository with name '" + repository + "' already exists in domain '" + domain + "'."); + throw conflict("Repository with name '" + repository + "' already exists in domain '" + domain + "'.", + repository, "repository"); } 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")); + resourceFields(repository, "repository")); } validateUpstreams(owner, region, domain, repository, upstreams); validateDescription(description); @@ -260,36 +261,40 @@ public class CodeArtifactService implements Resettable { r.setDescription(description); r.setUpstreams(upstreams != null ? new ArrayList<>(upstreams) : new ArrayList<>()); r.setCreatedTime(Instant.now().getEpochSecond()); - r.setTags(validateTags(tags, Map.of())); + r.setTags(validateTags(tags, Map.of(), repository, "repository")); repositories.putForAccount(owner, key, r); return r; } public synchronized CodeArtifactRepository deleteRepository(String region, String domain, String domainOwner, String repository) { + requireNonBlank(domain, "domain"); String owner = effectiveOwner(domainOwner); String key = repositoryKey(region, domain, repository); - CodeArtifactRepository r = requireRepository(owner, key); + CodeArtifactRepository r = requireRepository(owner, key, repository); repositories.deleteForAccount(owner, key); return r; } public CodeArtifactRepository describeRepository(String region, String domain, String domainOwner, String repository) { + requireNonBlank(domain, "domain"); String owner = effectiveOwner(domainOwner); - return requireRepository(owner, repositoryKey(region, domain, repository)); + return requireRepository(owner, repositoryKey(region, domain, repository), repository); } public synchronized CodeArtifactRepository updateRepository(String region, String domain, String domainOwner, String repository, String description, List upstreams) { + requireNonBlank(domain, "domain"); String owner = effectiveOwner(domainOwner); String key = repositoryKey(region, domain, repository); - CodeArtifactRepository r = requireRepository(owner, key); + CodeArtifactRepository r = requireRepository(owner, key, repository); if (upstreams != null) { if (!upstreams.isEmpty() && !r.getExternalConnections().isEmpty()) { throw conflict("Repository '" + repository + "' has an external connection; " - + "a repository cannot have both an external connection and upstream repositories."); + + "a repository cannot have both an external connection and upstream repositories.", + repository, "repository"); } validateUpstreams(owner, region, domain, repository, upstreams); r.setUpstreams(new ArrayList<>(upstreams)); @@ -319,7 +324,7 @@ public class CodeArtifactService implements Resettable { String repositoryPrefix, Integer maxResults, String nextToken) { String owner = effectiveOwner(domainOwner); - requireDomain(owner, domainKey(region, domain)); + requireDomain(owner, domainKey(region, domain), domain); List matching = repositories .scanForAccount(owner, k -> k.startsWith(region + "::" + domain + "::")) .stream() @@ -338,18 +343,20 @@ public class CodeArtifactService implements Resettable { if (endpointType != null && !ENDPOINT_TYPES.contains(endpointType)) { throw validation("endpointType must be one of " + ENDPOINT_TYPES + "."); } + requireNonBlank(domain, "domain"); String owner = effectiveOwner(domainOwner); - requireRepository(owner, repositoryKey(region, domain, repository)); + requireRepository(owner, repositoryKey(region, domain, repository), repository); return config.effectiveBaseUrl() + "/codeartifact/" + format + "/" + domain + "/" + repository + "/"; } public synchronized ResourcePolicy putRepositoryPermissionsPolicy(String region, String domain, String domainOwner, String repository, String policyDocument, String policyRevision) { + requireNonBlank(domain, "domain"); String owner = effectiveOwner(domainOwner); String key = repositoryKey(region, domain, repository); - CodeArtifactRepository r = requireRepository(owner, key); - checkRevision(r.getPolicyRevision(), policyRevision); + CodeArtifactRepository r = requireRepository(owner, key, repository); + checkRevision(r.getPolicyRevision(), policyRevision, repository, "repository"); validatePolicyDocument(policyDocument); r.setPolicyDocument(policyDocument); r.setPolicyRevision(newRevision()); @@ -359,10 +366,12 @@ public class CodeArtifactService implements Resettable { public ResourcePolicy getRepositoryPermissionsPolicy(String region, String domain, String domainOwner, String repository) { + requireNonBlank(domain, "domain"); String owner = effectiveOwner(domainOwner); - CodeArtifactRepository r = requireRepository(owner, repositoryKey(region, domain, repository)); + CodeArtifactRepository r = requireRepository(owner, repositoryKey(region, domain, repository), repository); if (r.getPolicyDocument() == null) { - throw notFound("No resource policy is associated with repository '" + repository + "'."); + throw notFound("No resource policy is associated with repository '" + repository + "'.", + repository, "repository"); } return new ResourcePolicy(r.getArn(), r.getPolicyRevision(), r.getPolicyDocument()); } @@ -370,13 +379,15 @@ public class CodeArtifactService implements Resettable { public synchronized ResourcePolicy deleteRepositoryPermissionsPolicy(String region, String domain, String domainOwner, String repository, String policyRevision) { + requireNonBlank(domain, "domain"); String owner = effectiveOwner(domainOwner); String key = repositoryKey(region, domain, repository); - CodeArtifactRepository r = requireRepository(owner, key); + CodeArtifactRepository r = requireRepository(owner, key, repository); if (r.getPolicyDocument() == null) { - throw notFound("No resource policy is associated with repository '" + repository + "'."); + throw notFound("No resource policy is associated with repository '" + repository + "'.", + repository, "repository"); } - checkRevision(r.getPolicyRevision(), policyRevision); + checkRevision(r.getPolicyRevision(), policyRevision, repository, "repository"); ResourcePolicy removed = new ResourcePolicy(r.getArn(), r.getPolicyRevision(), r.getPolicyDocument()); r.setPolicyDocument(null); r.setPolicyRevision(null); @@ -391,16 +402,18 @@ public class CodeArtifactService implements Resettable { if (format == null) { throw validation("externalConnection must be one of " + EXTERNAL_CONNECTIONS.keySet() + "."); } + requireNonBlank(domain, "domain"); String owner = effectiveOwner(domainOwner); String key = repositoryKey(region, domain, repository); - CodeArtifactRepository r = requireRepository(owner, key); + CodeArtifactRepository r = requireRepository(owner, key, repository); if (!r.getExternalConnections().isEmpty()) { throw conflict("Repository '" + repository + "' already has an external connection; " - + "a repository can only have one."); + + "a repository can only have one.", repository, "repository"); } if (!r.getUpstreams().isEmpty()) { throw conflict("Repository '" + repository + "' has upstream repositories; " - + "a repository cannot have both an external connection and upstream repositories."); + + "a repository cannot have both an external connection and upstream repositories.", + repository, "repository"); } List connections = new ArrayList<>(r.getExternalConnections()); connections.add(new ExternalConnection(externalConnection, format, "Available")); @@ -412,14 +425,15 @@ public class CodeArtifactService implements Resettable { public synchronized CodeArtifactRepository disassociateExternalConnection(String region, String domain, String domainOwner, String repository, String externalConnection) { + requireNonBlank(domain, "domain"); String owner = effectiveOwner(domainOwner); String key = repositoryKey(region, domain, repository); - CodeArtifactRepository r = requireRepository(owner, key); + CodeArtifactRepository r = requireRepository(owner, key, repository); List connections = new ArrayList<>(r.getExternalConnections()); boolean removed = connections.removeIf(ec -> ec.getExternalConnectionName().equals(externalConnection)); if (!removed) { throw notFound("Repository '" + repository + "' has no external connection named '" - + externalConnection + "'."); + + externalConnection + "'.", repository, "repository"); } r.setExternalConnections(connections); repositories.putForAccount(owner, key, r); @@ -448,21 +462,23 @@ public class CodeArtifactService implements Resettable { byte[] assetContent = content != null ? content : new byte[0]; if (assetContent.length > MAX_ASSET_FILE_SIZE_BYTES) { throw new AwsException("ServiceQuotaExceededException", - "The maximum asset file size is 5 Gigabytes.", 402); + "The maximum asset file size is 5 Gigabytes.", 402, + resourceFields(assetName, "asset")); } String actualSha256 = sha256Hex(assetContent); if (!assetSha256.equalsIgnoreCase(actualSha256)) { throw validation("assetSHA256 does not match the SHA-256 hash of the uploaded content."); } + requireNonBlank(domain, "domain"); String owner = effectiveOwner(domainOwner); - requireRepository(owner, repositoryKey(region, domain, repository)); + requireRepository(owner, repositoryKey(region, domain, repository), repository); String key = packageVersionKey(region, domain, repository, format, namespace, packageName, version); CodeArtifactPackageVersion pv = packageVersions.getForAccount(owner, key).orElse(null); if (pv != null && "Published".equals(pv.getStatus())) { throw conflict("Package version '" + version + "' of package '" + packageName + "' is already " - + "Published; no additional assets can be uploaded to it."); + + "Published; no additional assets can be uploaded to it.", version, "package-version"); } if (pv == null) { pv = new CodeArtifactPackageVersion(); @@ -479,7 +495,8 @@ public class CodeArtifactService implements Resettable { Map assets = new LinkedHashMap<>(pv.getAssets()); if (!assets.containsKey(assetName) && assets.size() >= MAX_ASSETS_PER_PACKAGE_VERSION) { throw new AwsException("ServiceQuotaExceededException", - "A package version can have a maximum of " + MAX_ASSETS_PER_PACKAGE_VERSION + " assets.", 402); + "A package version can have a maximum of " + MAX_ASSETS_PER_PACKAGE_VERSION + " assets.", 402, + resourceFields(version, "package-version")); } PackageAsset asset = new PackageAsset(); @@ -506,11 +523,15 @@ public class CodeArtifactService implements Resettable { if (format == null || !PACKAGE_FORMATS.contains(format)) { throw validation("format must be one of " + PACKAGE_FORMATS + "."); } + requireNonBlank(domain, "domain"); + requireNonBlank(repository, "repository"); + requireNonBlank(packageName, "package"); + requireNonBlank(version, "packageVersion"); String owner = effectiveOwner(domainOwner); String key = packageVersionKey(region, domain, repository, format, namespace, packageName, version); return packageVersions.getForAccount(owner, key) .orElseThrow(() -> notFound("Package version '" + version + "' of package '" + packageName - + "' was not found.")); + + "' was not found.", version, "package-version")); } public PackageVersionAssetResult getPackageVersionAsset(String region, String domain, String domainOwner, @@ -519,18 +540,24 @@ public class CodeArtifactService implements Resettable { if (format == null || !PACKAGE_FORMATS.contains(format)) { throw validation("format must be one of " + PACKAGE_FORMATS + "."); } + requireNonBlank(domain, "domain"); + requireNonBlank(repository, "repository"); + requireNonBlank(packageName, "package"); + requireNonBlank(version, "packageVersion"); + requireNonBlank(assetName, "asset"); String owner = effectiveOwner(domainOwner); String key = packageVersionKey(region, domain, repository, format, namespace, packageName, version); CodeArtifactPackageVersion pv = packageVersions.getForAccount(owner, key) .orElseThrow(() -> notFound("Package version '" + version + "' of package '" + packageName - + "' was not found.")); + + "' was not found.", version, "package-version")); if (packageVersionRevision != null && !packageVersionRevision.equals(pv.getRevision())) { throw notFound("Package version '" + version + "' was not found at revision '" - + packageVersionRevision + "'."); + + packageVersionRevision + "'.", version, "package-version"); } PackageAsset asset = pv.getAssets().get(assetName); if (asset == null) { - throw notFound("Asset '" + assetName + "' was not found on package version '" + version + "'."); + throw notFound("Asset '" + assetName + "' was not found on package version '" + version + "'.", + assetName, "asset"); } // The live object's content may already be populated (same JVM as the publish that set // it), but it isn't guaranteed to be: a reload from the persisted store never restores it, @@ -546,13 +573,13 @@ public class CodeArtifactService implements Resettable { ResourceRef ref = parseResourceArn(resourceArn); if (ref.repository() == null) { String key = domainKey(ref.region(), ref.domain()); - CodeArtifactDomain d = requireDomain(ref.owner(), key); - d.setTags(validateTags(newTags, d.getTags())); + CodeArtifactDomain d = requireDomain(ref.owner(), key, ref.domain()); + d.setTags(validateTags(newTags, d.getTags(), ref.domain(), "domain")); domains.putForAccount(ref.owner(), key, d); } else { String key = repositoryKey(ref.region(), ref.domain(), ref.repository()); - CodeArtifactRepository r = requireRepository(ref.owner(), key); - r.setTags(validateTags(newTags, r.getTags())); + CodeArtifactRepository r = requireRepository(ref.owner(), key, ref.repository()); + r.setTags(validateTags(newTags, r.getTags(), ref.repository(), "repository")); repositories.putForAccount(ref.owner(), key, r); } } @@ -561,14 +588,14 @@ public class CodeArtifactService implements Resettable { ResourceRef ref = parseResourceArn(resourceArn); if (ref.repository() == null) { String key = domainKey(ref.region(), ref.domain()); - CodeArtifactDomain d = requireDomain(ref.owner(), key); + CodeArtifactDomain d = requireDomain(ref.owner(), key, ref.domain()); Map tags = new LinkedHashMap<>(d.getTags()); tagKeys.forEach(tags::remove); d.setTags(tags); domains.putForAccount(ref.owner(), key, d); } else { String key = repositoryKey(ref.region(), ref.domain(), ref.repository()); - CodeArtifactRepository r = requireRepository(ref.owner(), key); + CodeArtifactRepository r = requireRepository(ref.owner(), key, ref.repository()); Map tags = new LinkedHashMap<>(r.getTags()); tagKeys.forEach(tags::remove); r.setTags(tags); @@ -579,9 +606,10 @@ public class CodeArtifactService implements Resettable { public Map listTagsForResource(String resourceArn) { ResourceRef ref = parseResourceArn(resourceArn); if (ref.repository() == null) { - return requireDomain(ref.owner(), domainKey(ref.region(), ref.domain())).getTags(); + return requireDomain(ref.owner(), domainKey(ref.region(), ref.domain()), ref.domain()).getTags(); } - return requireRepository(ref.owner(), repositoryKey(ref.region(), ref.domain(), ref.repository())).getTags(); + return requireRepository(ref.owner(), repositoryKey(ref.region(), ref.domain(), ref.repository()), + ref.repository()).getTags(); } @Override @@ -645,31 +673,41 @@ public class CodeArtifactService implements Resettable { } if (upstreams.size() > 10) { throw new AwsException("ServiceQuotaExceededException", - "A repository can have a maximum of 10 direct upstream repositories.", 402); + "A repository can have a maximum of 10 direct upstream repositories.", 402, + resourceFields(repository, "repository")); } for (String upstream : upstreams) { if (upstream.equals(repository)) { throw validation("A repository cannot be its own upstream."); } if (repositories.getForAccount(owner, repositoryKey(region, domain, upstream)).isEmpty()) { - throw notFound("Upstream repository '" + upstream + "' was not found in domain '" + domain + "'."); + throw notFound("Upstream repository '" + upstream + "' was not found in domain '" + domain + "'.", + upstream, "repository"); } } } - private CodeArtifactDomain requireDomain(String owner, String key) { + private CodeArtifactDomain requireDomain(String owner, String key, String domainName) { + if (domainName == null || domainName.isBlank()) { + throw validation("domain is required."); + } return domains.getForAccount(owner, key) - .orElseThrow(() -> notFound("Domain not found.")); + .orElseThrow(() -> notFound("Domain not found.", domainName, "domain")); } - private CodeArtifactRepository requireRepository(String owner, String key) { + private CodeArtifactRepository requireRepository(String owner, String key, String repositoryName) { + if (repositoryName == null || repositoryName.isBlank()) { + throw validation("repository is required."); + } return repositories.getForAccount(owner, key) - .orElseThrow(() -> notFound("Repository not found.")); + .orElseThrow(() -> notFound("Repository not found.", repositoryName, "repository")); } - private void checkRevision(String currentRevision, String requestedRevision) { + private void checkRevision(String currentRevision, String requestedRevision, String resourceId, + String resourceType) { if (requestedRevision != null && !requestedRevision.equals(currentRevision)) { - throw conflict("The policy revision does not match the current policy revision."); + throw conflict("The policy revision does not match the current policy revision.", resourceId, + resourceType); } } @@ -689,6 +727,19 @@ public class CodeArtifactService implements Resettable { } } + /** + * {@code domain} is required on every operation that takes it, but a repository-scoped + * lookup only guards its own {@code repository} argument via {@link #requireRepository}; a + * missing domain would otherwise get silently baked into the composite key as the literal + * string "null" and surface as a misleading "Repository not found" instead of a proper + * validation error. + */ + private static void requireNonBlank(String value, String fieldName) { + if (value == null || value.isBlank()) { + throw validation(fieldName + " is required."); + } + } + private static void validateDomainName(String domain) { if (domain == null || !DOMAIN_NAME.matcher(domain).matches()) { throw validation("domain must be 2-50 characters and match [a-z][a-z0-9-]*[a-z0-9]."); @@ -701,7 +752,8 @@ public class CodeArtifactService implements Resettable { } } - private static Map validateTags(Map newTags, Map existing) { + private static Map validateTags(Map newTags, Map existing, + String resourceId, String resourceType) { Map merged = new LinkedHashMap<>(existing); if (newTags == null) { return merged; @@ -717,7 +769,8 @@ public class CodeArtifactService implements Resettable { }); if (merged.size() > 200) { throw new AwsException("ServiceQuotaExceededException", - "The maximum number of tags (200) for this resource has been exceeded.", 402); + "The maximum number of tags (200) for this resource has been exceeded.", 402, + resourceFields(resourceId, resourceType)); } return merged; } @@ -859,11 +912,25 @@ public class CodeArtifactService implements Resettable { return new AwsException("ValidationException", message, 400); } - private static AwsException conflict(String message) { - return new AwsException("ConflictException", message, 409); + private static AwsException conflict(String message, String resourceId, String resourceType) { + return new AwsException("ConflictException", message, 409, resourceFields(resourceId, resourceType)); } - private static AwsException notFound(String message) { - return new AwsException("ResourceNotFoundException", message, 404); + private static AwsException notFound(String message, String resourceId, String resourceType) { + return new AwsException("ResourceNotFoundException", message, 404, resourceFields(resourceId, resourceType)); + } + + /** + * {@link Map#of} rejects null values outright, but a resourceId can legitimately be + * unknown at the point an error is raised; omit it rather than let that turn into an NPE + * that replaces a clean 4xx with a 500. + */ + private static Map resourceFields(String resourceId, String resourceType) { + Map fields = new LinkedHashMap<>(); + fields.put("resourceType", resourceType); + if (resourceId != null) { + fields.put("resourceId", resourceId); + } + return fields; } } diff --git a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactIntegrationTest.java index 5977cc2128..820477d2b6 100644 --- a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactIntegrationTest.java @@ -199,6 +199,19 @@ class CodeArtifactIntegrationTest { .then().statusCode(200).body("packageVersion.status", equalTo("Unfinished")); } + @Test + void missingRequiredQueryParamsReturnValidationExceptionNotServerError() { + given().header("Authorization", AUTH).get("/v1/domain") + .then().statusCode(400).body("__type", equalTo("ValidationException")); + + given().header("Authorization", AUTH).get("/v1/repository") + .then().statusCode(400).body("__type", equalTo("ValidationException")); + + given().header("Authorization", AUTH) + .get("/v1/repository/endpoint?repository=r&format=npm") + .then().statusCode(400).body("__type", equalTo("ValidationException")); + } + private static String sha256Hex(byte[] content) { try { return SigV4RequestValidator.sha256Hex(content); 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.java index 8c0d6dddc7..4e345fdd72 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 @@ -18,6 +18,7 @@ import org.junit.jupiter.api.Test; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.CountDownLatch; @@ -76,6 +77,8 @@ class CodeArtifactServiceTest { service.createDomain(REGION, "dup", null, Map.of()); AwsException e = assertThrows(AwsException.class, () -> service.createDomain(REGION, "dup", null, Map.of())); assertEquals("ConflictException", e.getErrorCode()); + assertEquals("dup", e.getExtendedData().get("resourceId")); + assertEquals("domain", e.getExtendedData().get("resourceType")); } @Test @@ -204,13 +207,47 @@ class CodeArtifactServiceTest { assertEquals(attempts - 1, conflicts.get()); } + @Test + void requiredDomainFieldRejectsNullInsteadOfCrashing() { + AwsException describe = assertThrows(AwsException.class, () -> service.describeDomain(REGION, null, null)); + assertEquals("ValidationException", describe.getErrorCode()); + + AwsException delete = assertThrows(AwsException.class, () -> service.deleteDomain(REGION, null, null)); + assertEquals("ValidationException", delete.getErrorCode()); + + AwsException getPolicy = assertThrows(AwsException.class, + () -> service.getDomainPermissionsPolicy(REGION, null, null)); + assertEquals("ValidationException", getPolicy.getErrorCode()); + } + // ---------------------------------------------------------- repositories + @Test + void requiredRepositoryFieldsRejectNullInsteadOfCrashing() { + service.createDomain(REGION, "dom", null, Map.of()); + + AwsException missingDomain = assertThrows(AwsException.class, + () -> service.describeRepository(REGION, null, null, "repo")); + assertEquals("ValidationException", missingDomain.getErrorCode()); + assertEquals("domain is required.", missingDomain.getMessage()); + + AwsException missingRepository = assertThrows(AwsException.class, + () -> service.describeRepository(REGION, "dom", null, null)); + assertEquals("ValidationException", missingRepository.getErrorCode()); + assertEquals("repository is required.", missingRepository.getMessage()); + + AwsException missingRepositoryEndpoint = assertThrows(AwsException.class, + () -> service.getRepositoryEndpoint(REGION, null, null, "repo", "npm", null)); + assertEquals("ValidationException", missingRepositoryEndpoint.getErrorCode()); + } + @Test void createRepositoryRequiresExistingDomain() { AwsException e = assertThrows(AwsException.class, () -> service.createRepository(REGION, "missing-domain", null, "repo", null, null, Map.of())); assertEquals("ResourceNotFoundException", e.getErrorCode()); + assertEquals("missing-domain", e.getExtendedData().get("resourceId")); + assertEquals("domain", e.getExtendedData().get("resourceType")); } @Test @@ -219,6 +256,8 @@ class CodeArtifactServiceTest { AwsException e = assertThrows(AwsException.class, () -> service.createRepository(REGION, "dom", null, "repo", null, List.of("ghost"), Map.of())); assertEquals("ResourceNotFoundException", e.getErrorCode()); + assertEquals("ghost", e.getExtendedData().get("resourceId")); + assertEquals("repository", e.getExtendedData().get("resourceType")); } @Test @@ -262,6 +301,8 @@ class CodeArtifactServiceTest { AwsException e = assertThrows(AwsException.class, () -> service.createRepository(REGION, "dom", null, "consumer", null, upstreams, Map.of())); assertEquals("ServiceQuotaExceededException", e.getErrorCode()); + assertEquals("consumer", e.getExtendedData().get("resourceId")); + assertEquals("repository", e.getExtendedData().get("resourceType")); } @Test @@ -373,6 +414,8 @@ class CodeArtifactServiceTest { AwsException e = assertThrows(AwsException.class, () -> service.associateExternalConnection(REGION, "dom", null, "repo", "public:pypi")); assertEquals("ConflictException", e.getErrorCode()); + assertEquals("repo", e.getExtendedData().get("resourceId")); + assertEquals("repository", e.getExtendedData().get("resourceType")); CodeArtifactRepository disassociated = service.disassociateExternalConnection(REGION, "dom", null, "repo", "public:npmjs"); @@ -426,6 +469,35 @@ class CodeArtifactServiceTest { () -> service.tagResource("arn:aws:codeartifact:" + REGION + ":" + ACCOUNT_ID + ":domain/ghost", Map.of("k", "v"))); assertEquals("ResourceNotFoundException", e.getErrorCode()); + assertEquals("ghost", e.getExtendedData().get("resourceId")); + assertEquals("domain", e.getExtendedData().get("resourceType")); + } + + @Test + void createDomainRejectsMoreThanTwoHundredTags() { + Map tags = new HashMap<>(); + for (int i = 0; i < 201; i++) { + tags.put("key-" + i, "value-" + i); + } + AwsException e = assertThrows(AwsException.class, + () -> service.createDomain(REGION, "dom", null, tags)); + assertEquals("ServiceQuotaExceededException", e.getErrorCode()); + assertEquals("dom", e.getExtendedData().get("resourceId")); + assertEquals("domain", e.getExtendedData().get("resourceType")); + } + + @Test + void tagResourceRejectsMoreThanTwoHundredTagsOnRepository() { + service.createDomain(REGION, "dom", null, Map.of()); + CodeArtifactRepository repo = service.createRepository(REGION, "dom", null, "repo", null, null, Map.of()); + Map tags = new HashMap<>(); + for (int i = 0; i < 201; i++) { + tags.put("key-" + i, "value-" + i); + } + AwsException e = assertThrows(AwsException.class, () -> service.tagResource(repo.getArn(), tags)); + assertEquals("ServiceQuotaExceededException", e.getErrorCode()); + assertEquals("repo", e.getExtendedData().get("resourceId")); + assertEquals("repository", e.getExtendedData().get("resourceType")); } @Test @@ -536,6 +608,30 @@ class CodeArtifactServiceTest { assertEquals("ResourceNotFoundException", e.getErrorCode()); } + @Test + void requiredPackageVersionFieldsRejectNullInsteadOfCrashing() { + service.createDomain(REGION, "dom", null, Map.of()); + service.createRepository(REGION, "dom", null, "repo", null, null, Map.of()); + byte[] content = "x".getBytes(StandardCharsets.UTF_8); + + AwsException publishMissingDomain = assertThrows(AwsException.class, () -> service.publishPackageVersion( + REGION, null, null, "repo", "generic", null, "my-pkg", "1.0.0", "asset.txt", sha256Hex(content), + "false", content)); + assertEquals("ValidationException", publishMissingDomain.getErrorCode()); + assertEquals("domain is required.", publishMissingDomain.getMessage()); + + AwsException describeMissingRepository = assertThrows(AwsException.class, + () -> service.describePackageVersion(REGION, "dom", null, null, "generic", null, "my-pkg", "1.0.0")); + assertEquals("ValidationException", describeMissingRepository.getErrorCode()); + assertEquals("repository is required.", describeMissingRepository.getMessage()); + + AwsException getAssetMissingVersion = assertThrows(AwsException.class, + () -> service.getPackageVersionAsset(REGION, "dom", null, "repo", "generic", null, "my-pkg", null, + "asset.txt", null)); + assertEquals("ValidationException", getAssetMissingVersion.getErrorCode()); + assertEquals("packageVersion is required.", getAssetMissingVersion.getMessage()); + } + @Test void unfinishedPublishAllowsMultipleAssetsThenFinalizes() { service.createDomain(REGION, "dom", null, Map.of()); @@ -566,6 +662,8 @@ class CodeArtifactServiceTest { AwsException e = assertThrows(AwsException.class, () -> service.publishPackageVersion(REGION, "dom", null, "repo", "generic", null, "my-pkg", "1.0.0", "b.txt", sha256Hex(content), "false", content)); assertEquals("ConflictException", e.getErrorCode()); + assertEquals("1.0.0", e.getExtendedData().get("resourceId")); + assertEquals("package-version", e.getExtendedData().get("resourceType")); } @Test @@ -581,6 +679,8 @@ class CodeArtifactServiceTest { AwsException e = assertThrows(AwsException.class, () -> service.publishPackageVersion(REGION, "dom", null, "repo", "generic", null, "my-pkg", "1.0.0", "asset-350.txt", sha256Hex(oneMore), "true", oneMore)); assertEquals("ServiceQuotaExceededException", e.getErrorCode()); + assertEquals("1.0.0", e.getExtendedData().get("resourceId")); + assertEquals("package-version", e.getExtendedData().get("resourceType")); // Re-publishing an asset name already on the version must not itself trip the cap. byte[] resend = "content-0".getBytes(StandardCharsets.UTF_8); @@ -623,10 +723,14 @@ class CodeArtifactServiceTest { AwsException wrongAsset = assertThrows(AwsException.class, () -> service.getPackageVersionAsset(REGION, "dom", null, "repo", "generic", null, "my-pkg", "1.0.0", "missing.txt", null)); assertEquals("ResourceNotFoundException", wrongAsset.getErrorCode()); + assertEquals("missing.txt", wrongAsset.getExtendedData().get("resourceId")); + assertEquals("asset", wrongAsset.getExtendedData().get("resourceType")); AwsException wrongRevision = assertThrows(AwsException.class, () -> service.getPackageVersionAsset(REGION, "dom", null, "repo", "generic", null, "my-pkg", "1.0.0", "a.txt", "not-the-current-revision")); assertEquals("ResourceNotFoundException", wrongRevision.getErrorCode()); + assertEquals("1.0.0", wrongRevision.getExtendedData().get("resourceId")); + assertEquals("package-version", wrongRevision.getExtendedData().get("resourceType")); } @Test