diff --git a/docs/services/codeartifact.md b/docs/services/codeartifact.md index 211fde8dea..093a61febb 100644 --- a/docs/services/codeartifact.md +++ b/docs/services/codeartifact.md @@ -60,16 +60,26 @@ upstream repositories or an external connection, but not both, matching AWS; `Cr and `UpdateRepository` also cap direct upstreams at 10, AWS's own repository limit. `PublishPackageVersion` creates a package version in the `Unfinished` state when the `unfinished` -flag is set, and `Published` otherwise; once `Published`, a repeat publish to the same -domain/repository/package/version fails with `ConflictException`, matching AWS's real rule that a -published version cannot accept additional assets. While still `Unfinished`, publishing a new -asset name is always accepted, but republishing an asset name that already exists is only -accepted when the content is byte-identical to what's already stored (AWS's own "Overwriting -package assets" rule: idempotent on a matching retry, `ConflictException` on genuinely different -content); each asset's hashes (`MD5`, `SHA-1`, `SHA-256`, `SHA-512`) are computed from the bytes -Floci actually received, not echoed from the request. Every publish returns a fresh -`versionRevision`. Floci enforces AWS's own published quotas for this action: a 5 GB max asset -file size and a 350-asset cap per package version, both returning `ServiceQuotaExceededException`. +flag is set, and `Published` otherwise. While still `Unfinished`, publishing a new asset name is +always accepted; once `Published`, a new asset name always conflicts, since real CodeArtifact never +lets a `Published` generic version grow beyond the assets it already has. Republishing an asset name +that already exists is only accepted when the content is byte-identical to what's already stored +(AWS's own "Overwriting package assets" rule: idempotent on a matching retry, `ConflictException` on +genuinely different content), and that rule applies the same way regardless of the version's status, +since a client is just as likely to retry the terminal publish call (the one that leaves a version +`Published`) as any earlier one. The comparison itself is against each asset's persisted SHA-256, +not its raw bytes, so deciding whether content matches costs no disk read and needs no special case +for a backing file that happens to be missing (e.g. a partial restore); each asset's hashes (`MD5`, +`SHA-1`, `SHA-256`, `SHA-512`) are computed from the bytes Floci actually received, not echoed from +the request. An idempotent retry still separately verifies the backing file's actual bytes (streamed +in fixed-size chunks, not loaded whole) before deciding there's nothing to do: an intact file stays +a true no-op needing no write capacity at all, while a file that's missing or has been corrupted +independently of Floci gets rewritten, so that kind of gap doesn't survive indefinitely just because +its recorded checksum still matched. A publish that adds or changes something returns a fresh +`versionRevision`; an idempotent no-op retry returns the version's existing one unchanged. Floci +enforces AWS's own published quotas for this action: a 5 GB max asset file size and a 350-asset cap +per package version, both returning +`ServiceQuotaExceededException`. ## The Maven repository endpoint 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 0928aec1ff..e6b388c983 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 @@ -35,7 +35,6 @@ import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.time.Instant; import java.util.ArrayList; -import java.util.Arrays; import java.util.Base64; import java.util.Comparator; import java.util.LinkedHashMap; @@ -568,8 +567,30 @@ public class CodeArtifactService implements Resettable { 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.", version, "package-version"); + // AWS's idempotency rule ("Overwriting package assets" in CodeArtifact's packages + // overview) is not scoped to Unfinished versions, and the terminal publish call (the + // one that finishes a version, or a single-shot generic publish that's Published + // immediately) is the likeliest one a client retries after a dropped response. A brand + // new asset name is still rejected outright: once Published, only a byte-identical + // replay of an asset that's already there is idempotent, nothing new can be added. + PackageAsset existingAsset = pv.getAssets().get(assetName); + if (existingAsset == null) { + throw conflict("Package version '" + version + "' of package '" + packageName + "' is already " + + "Published; no additional assets can be uploaded to it.", version, "package-version"); + } + // Throws on a genuine mismatch; a Published version can never transition to anything + // else via republish, so the match/no-stored-hash distinction contentMatchesExisting + // returns doesn't matter here the way it does for the Unfinished path below. + contentMatchesExisting(existingAsset, assetName, version, packageName, actualSha256); + // Only rewrites when the actual disk bytes don't check out: an intact retry (the + // overwhelmingly common case) must stay a true no-op that needs no write capacity at + // all, not fail just because the asset store happens to be low on free space. Repairs + // (missing file, or bytes corrupted independently of Floci) still self-heal here, since + // existingFileMatches actually reads the file back rather than trusting it's fine. + if (!existingFileMatches(owner, key, assetName, actualSha256)) { + writeAssetContent(owner, key, assetName, assetContent); + } + return new PublishPackageVersionResult(pv, existingAsset); } if (pv == null) { pv = new CodeArtifactPackageVersion(); @@ -584,20 +605,23 @@ public class CodeArtifactService implements Resettable { } Map assets = new LinkedHashMap<>(pv.getAssets()); - // AWS's own docs ("Overwriting package assets" in CodeArtifact's packages overview): a - // republish of an asset name that already exists is only rejected when the new content - // actually differs from what's already there; byte-identical content succeeds because the - // operation is idempotent, e.g. a client retrying after a dropped response. The in-memory - // PackageAsset.content field is @JsonIgnore and not reliable after a reload from persisted - // storage, so the real existing bytes have to come from the asset store, the same place - // getPackageVersionAsset reads them from. A genuinely missing backing file (metadata says - // the asset exists but its bytes don't, e.g. a partial restore) reads the same as "nothing - // to conflict with" rather than a hard failure, so a republish can still repair it. - if (assets.containsKey(assetName) && !existingAssetMatchesOrIsMissing(owner, key, assetName, assetContent)) { - throw conflict("Asset '" + assetName + "' already exists for package version '" + version - + "' of package '" + packageName + "' with different content.", assetName, "asset"); + PackageAsset existingAsset = assets.get(assetName); + if (existingAsset != null) { + // Throws on a genuine mismatch. A confirmed exact match is a true no-op only when this + // call also isn't trying to finalize the version (unfinished=false): that's a real + // status transition to Published even when the one asset it happens to touch is + // unchanged, and must not be skipped just because that asset's bytes already matched. + boolean matches = contentMatchesExisting(existingAsset, assetName, version, packageName, actualSha256); + if (matches && unfinished) { + // Only rewrites when the actual disk bytes don't check out: see the identical + // reasoning on the Published branch above. + if (!existingFileMatches(owner, key, assetName, actualSha256)) { + writeAssetContent(owner, key, assetName, assetContent); + } + return new PublishPackageVersionResult(pv, existingAsset); + } } - if (!assets.containsKey(assetName) && assets.size() >= MAX_ASSETS_PER_PACKAGE_VERSION) { + if (existingAsset == null && 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, resourceFields(version, "package-version")); @@ -952,6 +976,45 @@ public class CodeArtifactService implements Resettable { // ----------------------------------------------------------- asset bytes + /** + * {@code true} when the existing backing bytes actually hash to {@code expectedSha256}, so an + * idempotent retry can safely skip rewriting them; {@code false} when the file is missing, + * unreadable, or its real content doesn't match, meaning a repair write is still needed. For + * disk-backed storage this streams the file in fixed-size chunks rather than loading it whole: + * the caller's own upload is already fully resident in memory, so verifying a large existing + * asset this way costs no second full copy the way loading it to compare would. Deliberately + * separate from {@link #contentMatchesExisting}, which only compares persisted metadata and + * never touches disk: that is enough to reject a genuinely different republish outright, but + * not enough to know the disk file backing a *matching* one hasn't been corrupted or gone + * missing independently of Floci, which only actually reading it back can confirm. + */ + private boolean existingFileMatches(String owner, String packageVersionKey, String assetName, + String expectedSha256) { + if (inMemory) { + byte[] existing = memoryAssetStore.get(assetStoreKey(owner, packageVersionKey, assetName)); + return existing != null && expectedSha256.equalsIgnoreCase(sha256Hex(existing)); + } + Path filePath = resolveAssetPath(owner, packageVersionKey, assetName); + try (InputStream in = Files.newInputStream(filePath)) { + MessageDigest digest = MessageDigest.getInstance("SHA-256"); + byte[] buffer = new byte[8192]; + int read; + while ((read = in.read(buffer)) != -1) { + digest.update(buffer, 0, read); + } + return expectedSha256.equalsIgnoreCase(SigV4RequestValidator.hexEncode(digest.digest())); + } catch (NoSuchFileException e) { + return false; + } catch (IOException e) { + // Any I/O error reading the existing file (not just a clean "not found") reads the + // same as "can't confirm it's intact": a repair write is the safe response, not a + // thrown exception that would turn an otherwise-successful idempotent retry into one. + return false; + } catch (NoSuchAlgorithmException e) { + throw new IllegalStateException("JVM does not support SHA-256", e); + } + } + private void writeAssetContent(String owner, String packageVersionKey, String assetName, byte[] content) { if (inMemory) { memoryAssetStore.put(assetStoreKey(owner, packageVersionKey, assetName), content); @@ -989,43 +1052,6 @@ public class CodeArtifactService implements Resettable { } } - /** - * {@code true} when the asset's backing bytes are genuinely missing (e.g. a partial restore - * left the metadata but not the file, so there is nothing to conflict with) or match - * {@code newContent}; {@code false} only on a real mismatch. Deliberately never materializes - * the existing file as a second full byte array: {@code newContent} is already fully resident - * (RESTEasy Reactive buffers the whole request body before this method ever runs), so loading a - * second full copy of a large existing asset just to compare it would double peak memory for a - * same-content retry. Also deliberately separate from {@link #readAssetContent}: a real - * download through {@code getPackageVersionAsset} needs a missing file to stay a hard failure, - * not silently read as "nothing published yet". - */ - private boolean existingAssetMatchesOrIsMissing(String owner, String packageVersionKey, String assetName, - byte[] newContent) { - if (inMemory) { - byte[] existing = memoryAssetStore.get(assetStoreKey(owner, packageVersionKey, assetName)); - return existing == null || Arrays.equals(existing, newContent); - } - Path filePath = resolveAssetPath(owner, packageVersionKey, assetName); - try (InputStream in = Files.newInputStream(filePath)) { - byte[] buffer = new byte[8192]; - int offset = 0; - int read; - while ((read = in.read(buffer)) != -1) { - if (offset + read > newContent.length || !Arrays.equals(buffer, 0, read, - newContent, offset, offset + read)) { - return false; - } - offset += read; - } - return offset == newContent.length; - } catch (NoSuchFileException e) { - return true; - } catch (IOException e) { - throw new UncheckedIOException("Failed to read CodeArtifact asset file: " + filePath, e); - } - } - private static String assetStoreKey(String owner, String packageVersionKey, String assetName) { return owner + "::" + packageVersionKey + "::" + assetName; } @@ -1069,6 +1095,31 @@ public class CodeArtifactService implements Resettable { throw validation("unfinished must be 'true' or 'false'."); } + /** + * Rejects with {@code ConflictException} when {@code existingAsset}'s stored SHA-256 differs + * from the new upload's. Compares against the asset's own persisted {@code hashes} map rather + * than its bytes: unlike {@code PackageAsset.content} (which is {@code @JsonIgnore} and empty + * after a reload from persisted storage), the hash survives normally, so this needs no disk + * read, no in-memory-store lookup, and no missing-backing-file special case at all, since a + * hash never "goes missing" the way a file can. + * + *

Returns {@code true} only on a confirmed exact match, letting a caller treat that case as + * a genuine no-op; {@code false} when there's no stored hash to compare against (defensive: + * every asset gets one computed at publish time, so not a path any real request reaches). + */ + private static boolean contentMatchesExisting(PackageAsset existingAsset, String assetName, String version, + String packageName, String actualSha256) { + String existingSha256 = existingAsset.getHashes().get("SHA-256"); + if (existingSha256 == null) { + return false; + } + if (!existingSha256.equalsIgnoreCase(actualSha256)) { + throw conflict("Asset '" + assetName + "' already exists for package version '" + version + + "' of package '" + packageName + "' with different content.", assetName, "asset"); + } + return true; + } + private static Map computeHashes(byte[] content) { Map hashes = new LinkedHashMap<>(); for (String algorithm : ASSET_HASH_ALGORITHMS) { diff --git a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactAssetPersistenceTest.java b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactAssetPersistenceTest.java index b6e9f54373..18149fdc64 100644 --- a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactAssetPersistenceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactAssetPersistenceTest.java @@ -18,7 +18,7 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Arrays; +import java.nio.file.attribute.BasicFileAttributes; import java.util.Base64; import java.util.List; import java.util.Map; @@ -26,6 +26,7 @@ import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; @@ -118,14 +119,14 @@ class CodeArtifactAssetPersistenceTest { } /** - * A package version whose metadata still lists an asset but whose backing file is gone (a - * partial restore, or anything else that touched the asset store without also touching the - * metadata) must not turn a republish into a hard failure: that would block the one thing that - * could actually repair it. Greptile caught this as a real regression risk in the overwrite - * check above; this is the case that check must not treat as "the asset exists and must match". + * The overwrite check compares against an asset's persisted SHA-256 (in {@code hashes}), not + * its bytes, so a backing file that's missing on disk (a partial restore, or anything else that + * touched the asset store without also touching the metadata) has no special case at all: a + * republish of the exact content that was already recorded still passes the hash comparison and + * naturally rewrites the file as a side effect of the normal write path below. */ @Test - void republishingAnAssetWhoseBackingFileIsMissingRepairsItInsteadOfFailing(@TempDir Path dir) throws IOException { + void republishingWithMatchingContentRewritesAMissingBackingFile(@TempDir Path dir) throws IOException { CodeArtifactService service = newService(dir); service.createDomain(REGION, "dom", null, Map.of()); service.createRepository(REGION, "dom", null, "repo", null, null, Map.of()); @@ -140,78 +141,159 @@ class CodeArtifactAssetPersistenceTest { } } - byte[] repaired = "repaired content".getBytes(StandardCharsets.UTF_8); PublishPackageVersionResult result = service.publishPackageVersion(REGION, "dom", null, "repo", "generic", - null, "my-pkg", "1.0.0", "a.txt", sha256Hex(repaired), "true", repaired); + null, "my-pkg", "1.0.0", "a.txt", sha256Hex(content), "true", content); assertEquals("Unfinished", result.packageVersion().getStatus()); PackageVersionAssetResult fetched = service.getPackageVersionAsset(REGION, "dom", null, "repo", "generic", null, "my-pkg", "1.0.0", "a.txt", null); - assertEquals("repaired content", new String(fetched.asset().getContent(), StandardCharsets.UTF_8)); + assertEquals("hello world", new String(fetched.asset().getContent(), StandardCharsets.UTF_8)); } /** - * The overwrite check streams the existing file in fixed-size chunks rather than loading it - * whole, specifically to avoid holding two full copies of a large asset in memory at once for a - * same-content retry. These three cases exercise the chunk boundary directly: content spanning - * several reads of the comparison buffer, a mismatch that only appears in the final chunk, and a - * length mismatch that only becomes apparent once the shorter side is exhausted. + * Same repair guarantee as the Unfinished case above, but for a version that's already + * Published: the idempotent-retry short-circuit there always rewrites the now-confirmed-correct + * bytes rather than trusting that the file, if present, already has the right content. */ @Test - void republishingALargeAssetSpanningMultipleBufferReadsWithIdenticalContentSucceeds(@TempDir Path dir) { + void republishingAnAlreadyPublishedAssetWithMatchingContentRewritesAMissingBackingFile(@TempDir Path dir) + throws IOException { CodeArtifactService service = newService(dir); service.createDomain(REGION, "dom", null, Map.of()); service.createRepository(REGION, "dom", null, "repo", null, null, Map.of()); - byte[] content = deterministicBytes(20_000); - service.publishPackageVersion(REGION, "dom", null, "repo", "generic", null, "my-pkg", "1.0.0", "a.bin", - sha256Hex(content), "true", content); + byte[] content = "hello world".getBytes(StandardCharsets.UTF_8); + service.publishPackageVersion(REGION, "dom", null, "repo", "generic", null, "my-pkg", "1.0.0", "a.txt", + sha256Hex(content), "false", content); + + Path assetRoot = dir.resolve("codeartifact-assets"); + try (Stream paths = Files.walk(assetRoot)) { + for (Path p : paths.filter(Files::isRegularFile).toList()) { + Files.delete(p); + } + } PublishPackageVersionResult result = service.publishPackageVersion(REGION, "dom", null, "repo", "generic", - null, "my-pkg", "1.0.0", "a.bin", sha256Hex(content), "true", content); + null, "my-pkg", "1.0.0", "a.txt", sha256Hex(content), "false", content); + + assertEquals("Published", result.packageVersion().getStatus()); + PackageVersionAssetResult fetched = service.getPackageVersionAsset(REGION, "dom", null, "repo", "generic", + null, "my-pkg", "1.0.0", "a.txt", null); + assertEquals("hello world", new String(fetched.asset().getContent(), StandardCharsets.UTF_8)); + } + + /** + * A backing file that exists but whose bytes no longer match its recorded hash (disk-level + * corruption, or anything else that touched the file directly without going through Floci) must + * not survive a matching-content retry: an existence-only check would see the file is "there" + * and skip repairing it, letting the corruption persist through every future retry forever. + * Greptile caught this as a real regression risk in the presence-check this test replaces. + */ + @Test + void republishingWithMatchingContentRepairsACorruptedBackingFile(@TempDir Path dir) throws IOException { + CodeArtifactService service = newService(dir); + service.createDomain(REGION, "dom", null, Map.of()); + service.createRepository(REGION, "dom", null, "repo", null, null, Map.of()); + byte[] content = "hello world".getBytes(StandardCharsets.UTF_8); + service.publishPackageVersion(REGION, "dom", null, "repo", "generic", null, "my-pkg", "1.0.0", "a.txt", + sha256Hex(content), "true", content); + + Path assetRoot = dir.resolve("codeartifact-assets"); + try (Stream paths = Files.walk(assetRoot)) { + for (Path p : paths.filter(Files::isRegularFile).toList()) { + Files.write(p, "corrupted on disk".getBytes(StandardCharsets.UTF_8)); + } + } + + PublishPackageVersionResult result = service.publishPackageVersion(REGION, "dom", null, "repo", "generic", + null, "my-pkg", "1.0.0", "a.txt", sha256Hex(content), "true", content); assertEquals("Unfinished", result.packageVersion().getStatus()); - assertEquals(1, result.packageVersion().getAssets().size()); + PackageVersionAssetResult fetched = service.getPackageVersionAsset(REGION, "dom", null, "repo", "generic", + null, "my-pkg", "1.0.0", "a.txt", null); + assertEquals("hello world", new String(fetched.asset().getContent(), StandardCharsets.UTF_8)); } + /** + * The overwhelmingly common case, an intact asset republished unchanged, must stay a true + * no-op that needs no write capacity at all: Greptile caught that always rewriting (an earlier + * version of the fix above) would make a retry fail on a store that has no free space left, + * even though nothing on disk actually needed to change. {@code writeAssetContent} always + * replaces the file through a temp-file-then-rename, so its identity (device/inode) changing + * would prove a write happened; checking permission bits instead would be unreliable under a + * root-run CI container, which ignores them. + */ @Test - void republishingALargeAssetWithAMismatchInTheFinalChunkConflicts(@TempDir Path dir) { + void republishingAnIntactUnfinishedAssetDoesNotRewriteTheBackingFile(@TempDir Path dir) throws IOException { CodeArtifactService service = newService(dir); service.createDomain(REGION, "dom", null, Map.of()); service.createRepository(REGION, "dom", null, "repo", null, null, Map.of()); - byte[] original = deterministicBytes(20_000); - service.publishPackageVersion(REGION, "dom", null, "repo", "generic", null, "my-pkg", "1.0.0", "a.bin", - sha256Hex(original), "true", original); - - byte[] almostSame = original.clone(); - almostSame[almostSame.length - 1] ^= 0xFF; - - AwsException e = assertThrows(AwsException.class, () -> service.publishPackageVersion(REGION, "dom", null, - "repo", "generic", null, "my-pkg", "1.0.0", "a.bin", sha256Hex(almostSame), "true", almostSame)); - assertEquals("ConflictException", e.getErrorCode()); - } - - @Test - void republishingALargeAssetWithDifferentLengthConflicts(@TempDir Path dir) { - CodeArtifactService service = newService(dir); - service.createDomain(REGION, "dom", null, Map.of()); - service.createRepository(REGION, "dom", null, "repo", null, null, Map.of()); - byte[] original = deterministicBytes(20_000); - service.publishPackageVersion(REGION, "dom", null, "repo", "generic", null, "my-pkg", "1.0.0", "a.bin", - sha256Hex(original), "true", original); - - byte[] shorter = Arrays.copyOf(original, original.length - 1); - - AwsException e = assertThrows(AwsException.class, () -> service.publishPackageVersion(REGION, "dom", null, - "repo", "generic", null, "my-pkg", "1.0.0", "a.bin", sha256Hex(shorter), "true", shorter)); - assertEquals("ConflictException", e.getErrorCode()); - } - - private static byte[] deterministicBytes(int size) { - byte[] bytes = new byte[size]; - for (int i = 0; i < size; i++) { - bytes[i] = (byte) (i * 31 + 7); + byte[] content = "hello world".getBytes(StandardCharsets.UTF_8); + service.publishPackageVersion(REGION, "dom", null, "repo", "generic", null, "my-pkg", "1.0.0", "a.txt", + sha256Hex(content), "true", content); + Path assetFile; + try (Stream paths = Files.walk(dir.resolve("codeartifact-assets"))) { + assetFile = paths.filter(Files::isRegularFile).findFirst().orElseThrow(); } - return bytes; + Object fileKeyBefore = Files.readAttributes(assetFile, BasicFileAttributes.class).fileKey(); + assertNotNull(fileKeyBefore, "test filesystem must support file keys for this identity check to be meaningful"); + + service.publishPackageVersion(REGION, "dom", null, "repo", "generic", null, "my-pkg", "1.0.0", "a.txt", + sha256Hex(content), "true", content); + + Object fileKeyAfter = Files.readAttributes(assetFile, BasicFileAttributes.class).fileKey(); + assertEquals(fileKeyBefore, fileKeyAfter, "an intact retry must not replace the backing file"); + } + + @Test + void republishingAnIntactPublishedAssetDoesNotRewriteTheBackingFile(@TempDir Path dir) throws IOException { + CodeArtifactService service = newService(dir); + service.createDomain(REGION, "dom", null, Map.of()); + service.createRepository(REGION, "dom", null, "repo", null, null, Map.of()); + byte[] content = "hello world".getBytes(StandardCharsets.UTF_8); + service.publishPackageVersion(REGION, "dom", null, "repo", "generic", null, "my-pkg", "1.0.0", "a.txt", + sha256Hex(content), "false", content); + Path assetFile; + try (Stream paths = Files.walk(dir.resolve("codeartifact-assets"))) { + assetFile = paths.filter(Files::isRegularFile).findFirst().orElseThrow(); + } + Object fileKeyBefore = Files.readAttributes(assetFile, BasicFileAttributes.class).fileKey(); + assertNotNull(fileKeyBefore, "test filesystem must support file keys for this identity check to be meaningful"); + + service.publishPackageVersion(REGION, "dom", null, "repo", "generic", null, "my-pkg", "1.0.0", "a.txt", + sha256Hex(content), "false", content); + + Object fileKeyAfter = Files.readAttributes(assetFile, BasicFileAttributes.class).fileKey(); + assertEquals(fileKeyBefore, fileKeyAfter, "an intact retry must not replace the backing file"); + } + + /** + * A missing backing file must never turn into a loophole that lets a genuinely different + * republish through as a silent "repair": the asset's recorded checksum still exists (it's + * normal persisted metadata, not the {@code @JsonIgnore}d content field), so this conflicts the + * same as it would if the file were still there. + */ + @Test + void republishingWithDifferentContentConflictsEvenWhenTheBackingFileIsMissing(@TempDir Path dir) + throws IOException { + CodeArtifactService service = newService(dir); + service.createDomain(REGION, "dom", null, Map.of()); + service.createRepository(REGION, "dom", null, "repo", null, null, Map.of()); + byte[] content = "hello world".getBytes(StandardCharsets.UTF_8); + service.publishPackageVersion(REGION, "dom", null, "repo", "generic", null, "my-pkg", "1.0.0", "a.txt", + sha256Hex(content), "true", content); + + Path assetRoot = dir.resolve("codeartifact-assets"); + try (Stream paths = Files.walk(assetRoot)) { + for (Path p : paths.filter(Files::isRegularFile).toList()) { + Files.delete(p); + } + } + + byte[] different = "different content".getBytes(StandardCharsets.UTF_8); + AwsException e = assertThrows(AwsException.class, () -> service.publishPackageVersion(REGION, "dom", null, + "repo", "generic", null, "my-pkg", "1.0.0", "a.txt", sha256Hex(different), "true", different)); + assertEquals("ConflictException", e.getErrorCode()); + assertEquals("a.txt", e.getExtendedData().get("resourceId")); } @Test 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 9962e29a0d..1270056b76 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 @@ -854,14 +854,42 @@ class CodeArtifactServiceTest { service.createDomain(REGION, "dom", null, Map.of()); service.createRepository(REGION, "dom", null, "repo", null, null, Map.of()); byte[] content = "same-bytes".getBytes(StandardCharsets.UTF_8); - service.publishPackageVersion(REGION, "dom", null, "repo", "generic", null, "my-pkg", "1.0.0", "a.txt", - sha256Hex(content), "true", content); + PublishPackageVersionResult first = service.publishPackageVersion(REGION, "dom", null, "repo", "generic", + null, "my-pkg", "1.0.0", "a.txt", sha256Hex(content), "true", content); + // pv is the same mutable object across calls in this storage backend, so the revision has + // to be captured now, before the retry below has a chance to change it in place. + String firstRevision = first.packageVersion().getRevision(); - PublishPackageVersionResult result = service.publishPackageVersion(REGION, "dom", null, "repo", "generic", + PublishPackageVersionResult retry = service.publishPackageVersion(REGION, "dom", null, "repo", "generic", null, "my-pkg", "1.0.0", "a.txt", sha256Hex(content), "true", content); - assertEquals("Unfinished", result.packageVersion().getStatus()); - assertEquals(1, result.packageVersion().getAssets().size()); + assertEquals("Unfinished", retry.packageVersion().getStatus()); + assertEquals(1, retry.packageVersion().getAssets().size()); + assertEquals(firstRevision, retry.packageVersion().getRevision()); + } + + /** + * A no-op-looking retry (same asset name, same content) must not be treated as a true no-op + * when it also flips {@code unfinished} to {@code false}: that is a real status transition to + * Published, and skipping it just because the one asset it touches happens to be unchanged + * would leave the version stuck Unfinished forever. + */ + @Test + void finalizingWithTheSameAssetContentStillTransitionsToPublished() { + service.createDomain(REGION, "dom", null, Map.of()); + service.createRepository(REGION, "dom", null, "repo", null, null, Map.of()); + byte[] content = "same-bytes".getBytes(StandardCharsets.UTF_8); + PublishPackageVersionResult unfinished = service.publishPackageVersion(REGION, "dom", null, "repo", "generic", + null, "my-pkg", "1.0.0", "a.txt", sha256Hex(content), "true", content); + // pv is the same mutable object across calls in this storage backend, so this must be + // checked before the second call mutates it, not read back afterward. + assertEquals("Unfinished", unfinished.packageVersion().getStatus()); + + PublishPackageVersionResult finalized = service.publishPackageVersion(REGION, "dom", null, "repo", "generic", + null, "my-pkg", "1.0.0", "a.txt", sha256Hex(content), "false", content); + + assertEquals("Published", finalized.packageVersion().getStatus()); + assertTrue(finalized.packageVersion().getPublishedTime() != null); } @Test @@ -895,6 +923,40 @@ class CodeArtifactServiceTest { assertEquals("package-version", e.getExtendedData().get("resourceType")); } + @Test + void republishingTheExactSameAssetToAnAlreadyPublishedVersionSucceedsIdempotently() { + service.createDomain(REGION, "dom", null, Map.of()); + service.createRepository(REGION, "dom", null, "repo", null, null, Map.of()); + byte[] content = "x".getBytes(StandardCharsets.UTF_8); + PublishPackageVersionResult first = service.publishPackageVersion(REGION, "dom", null, "repo", "generic", + null, "my-pkg", "1.0.0", "a.txt", sha256Hex(content), "false", content); + // pv is the same mutable object across calls in this storage backend, so the revision has + // to be captured now, before the retry below has a chance to change it in place. + String firstRevision = first.packageVersion().getRevision(); + + PublishPackageVersionResult retry = service.publishPackageVersion(REGION, "dom", null, "repo", "generic", + null, "my-pkg", "1.0.0", "a.txt", sha256Hex(content), "false", content); + + assertEquals("Published", retry.packageVersion().getStatus()); + assertEquals(firstRevision, retry.packageVersion().getRevision()); + } + + @Test + void republishingAnAlreadyPublishedAssetWithDifferentContentConflicts() { + service.createDomain(REGION, "dom", null, Map.of()); + service.createRepository(REGION, "dom", null, "repo", null, null, Map.of()); + byte[] content = "x".getBytes(StandardCharsets.UTF_8); + service.publishPackageVersion(REGION, "dom", null, "repo", "generic", null, "my-pkg", "1.0.0", "a.txt", + sha256Hex(content), "false", content); + + byte[] different = "y".getBytes(StandardCharsets.UTF_8); + AwsException e = assertThrows(AwsException.class, () -> service.publishPackageVersion(REGION, "dom", null, + "repo", "generic", null, "my-pkg", "1.0.0", "a.txt", sha256Hex(different), "false", different)); + assertEquals("ConflictException", e.getErrorCode()); + assertEquals("a.txt", e.getExtendedData().get("resourceId")); + assertEquals("asset", e.getExtendedData().get("resourceType")); + } + @Test void publishPackageVersionCapsAssetsPerVersionAtThreeHundredFifty() { service.createDomain(REGION, "dom", null, Map.of());