fix(codeartifact): treat identical-content asset republish as idempotent (#4619)

PublishPackageVersion silently overwrote a generic-format asset's stored
bytes whenever the same asset name was republished to an Unfinished
package version, with no comparison against what was already there. AWS's
own docs ("Overwriting package assets" in the CodeArtifact packages
overview) say a republish is only rejected when the content actually
differs from what's already stored; byte-identical content succeeds
idempotently. Compares the new upload against the existing asset's real
bytes (read the same way GetPackageVersionAsset does, not the in-memory
PackageAsset.content field, which is @JsonIgnore and unreliable after a
reload) and now returns ConflictException on a genuine mismatch instead of
silently replacing the content.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-28 22:58:39 -05:00
1 parent 7e3ff3c5ee
commit 0232d5d224
4 files changed
+234 -5

No files matched your search

@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.codeartifact;
import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.PersistentStorage;
@@ -17,12 +18,15 @@ 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.util.Base64;
import java.util.List;
import java.util.Map;
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.assertThrows;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -72,6 +76,144 @@ class CodeArtifactAssetPersistenceTest {
assertEquals(published.packageVersion().getRevision(), result.packageVersionRevision());
}
/**
* The in-memory {@code PackageAsset.content} field is {@code @JsonIgnore}, so a package
* version reloaded from disk (a fresh {@code CodeArtifactService} instance here, standing in
* for a Floci restart) never has it populated; this only passes if the idempotent-republish
* check in {@code publishPackageVersion} reads the real bytes from the asset store the same
* way {@code getPackageVersionAsset} does, not that field.
*/
@Test
void republishingAnUnfinishedAssetAfterARestartWithIdenticalContentSucceedsIdempotently(@TempDir Path dir) {
CodeArtifactService first = newService(dir);
first.createDomain(REGION, "dom", null, Map.of());
first.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
byte[] content = "hello world".getBytes(StandardCharsets.UTF_8);
first.publishPackageVersion(REGION, "dom", null, "repo", "generic", null, "my-pkg", "1.0.0", "a.txt",
sha256Hex(content), "true", content);
CodeArtifactService restarted = newService(dir);
PublishPackageVersionResult result = restarted.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());
}
@Test
void republishingAnUnfinishedAssetAfterARestartWithDifferentContentConflicts(@TempDir Path dir) {
CodeArtifactService first = newService(dir);
first.createDomain(REGION, "dom", null, Map.of());
first.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
byte[] content = "hello world".getBytes(StandardCharsets.UTF_8);
first.publishPackageVersion(REGION, "dom", null, "repo", "generic", null, "my-pkg", "1.0.0", "a.txt",
sha256Hex(content), "true", content);
CodeArtifactService restarted = newService(dir);
byte[] different = "different bytes".getBytes(StandardCharsets.UTF_8);
AwsException e = assertThrows(AwsException.class, () -> restarted.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"));
}
/**
* 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".
*/
@Test
void republishingAnAssetWhoseBackingFileIsMissingRepairsItInsteadOfFailing(@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<Path> paths = Files.walk(assetRoot)) {
for (Path p : paths.filter(Files::isRegularFile).toList()) {
Files.delete(p);
}
}
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);
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));
}
/**
* 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.
*/
@Test
void republishingALargeAssetSpanningMultipleBufferReadsWithIdenticalContentSucceeds(@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[] content = deterministicBytes(20_000);
service.publishPackageVersion(REGION, "dom", null, "repo", "generic", null, "my-pkg", "1.0.0", "a.bin",
sha256Hex(content), "true", content);
PublishPackageVersionResult result = service.publishPackageVersion(REGION, "dom", null, "repo", "generic",
null, "my-pkg", "1.0.0", "a.bin", sha256Hex(content), "true", content);
assertEquals("Unfinished", result.packageVersion().getStatus());
assertEquals(1, result.packageVersion().getAssets().size());
}
@Test
void republishingALargeAssetWithAMismatchInTheFinalChunkConflicts(@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[] 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);
}
return bytes;
}
@Test
void distinctAssetNamesNeverCollideOnDisk(@TempDir Path dir) {
CodeArtifactService service = newService(dir);
@@ -849,6 +849,37 @@ class CodeArtifactServiceTest {
assertTrue(r2.packageVersion().getAssets().containsKey("b.txt"));
}
@Test
void republishingAnUnfinishedAssetWithIdenticalContentSucceedsIdempotently() {
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 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());
}
@Test
void republishingAnUnfinishedAssetWithDifferentContentConflicts() {
service.createDomain(REGION, "dom", null, Map.of());
service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
byte[] first = "first-bytes".getBytes(StandardCharsets.UTF_8);
byte[] different = "different-bytes".getBytes(StandardCharsets.UTF_8);
service.publishPackageVersion(REGION, "dom", null, "repo", "generic", null, "my-pkg", "1.0.0", "a.txt",
sha256Hex(first), "true", first);
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"));
assertEquals("asset", e.getExtendedData().get("resourceType"));
}
@Test
void publishingToAnAlreadyPublishedVersionConflicts() {
service.createDomain(REGION, "dom", null, Map.of());