fix(codeartifact): treat identical-content maven redeploy as idempotent (#4837)

Reposilite's own redeployment:false setting rejects a redeploy to an
existing GAV path unconditionally, even when the new content is
byte-identical to what is already there (confirmed against a real
instance). AWS's own CodeArtifact docs ("Overwriting package assets"
in the packages overview) say a republish of an asset whose content
matches what is already published succeeds because the operation is
idempotent; only a genuine content mismatch is a real conflict.
Reposilite has no content-aware mode of its own, so this fetches the
existing artifact first and short-circuits to a 200 on an exact match
rather than ever sending Reposilite a PUT it would reject regardless
of whether the retry was actually harmless.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-01 21:18:01 -05:00
1 parent c7bb03ace3
commit cd89be4a80
4 files changed
+198 -10

No files matched your search

+8
View File
@@ -108,6 +108,14 @@ shared settings list. Two config knobs,
image version or point at an already-running instance and skip container management, matching the
pattern used elsewhere in Floci for sidecars.
Each Reposilite repository is provisioned with `redeployment: false`, so redeploying an existing
GAV path with different content is rejected with a real `409`, matching AWS's documented asset
immutability. Reposilite's own setting has no content-aware mode, though, and would reject a
byte-identical redeploy too; Floci fetches the existing artifact first and short-circuits to `200`
on an exact match, matching AWS's own documented exception ("Overwriting package assets" in
CodeArtifact's packages overview) that a republish is idempotent when the content hasn't actually
changed.
## The npm repository endpoint
`GetRepositoryEndpoint` for `format=npm` returns `http://localhost:4566/codeartifact/npm/<domain>/<repository>/`.
@@ -9,6 +9,7 @@ import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
@@ -16,6 +17,7 @@ import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.time.Duration;
import java.util.Arrays;
import java.util.Optional;
/**
@@ -233,18 +235,82 @@ public class ReposiliteSidecarClient implements RepositorySidecarManager {
}
}
/** Deploys {@code content} to {@code repoId}'s {@code gav} path, returning the HTTP status. */
/**
* Deploys {@code content} to {@code repoId}'s {@code gav} path, returning the HTTP status.
*
* <p>Reposilite's own {@code redeployment: false} setting (set in {@link #ensureRepository})
* rejects a redeploy to an existing path unconditionally, even when the new content is
* byte-identical to what is already there (confirmed against a real instance). AWS's own
* CodeArtifact docs ("Overwriting package assets" in the packages-overview page) say a
* republish of an asset whose content matches what is already published succeeds because the
* operation is idempotent; only a genuine content mismatch is a real conflict. Reposilite has
* no content-aware mode of its own, so this checks the existing artifact first and
* short-circuits to a 200 on an exact match rather than ever sending Reposilite a PUT it would
* reject regardless of whether the retry was actually harmless. That check is best-effort: any
* failure reading the existing artifact falls through to the real PUT below rather than failing
* the deploy outright, since Reposilite's own redeployment check is still there to catch a
* genuine conflict either way.
*/
public int deployArtifact(String repoId, String gav, byte[] content) {
String baseUrl = manager.ensureReady();
if (existingArtifactMatches(baseUrl, repoId, gav, content)) {
return 200;
}
HttpRequest request = authenticated(baseUrl, repoId, gav)
.PUT(BodyPublishers.ofByteArray(content))
.build();
return send(request, BodyHandlers.discarding()).statusCode();
}
/**
* {@code true} when {@code repoId}'s {@code gav} path already holds content identical to
* {@code content}. Streams the existing artifact in fixed-size chunks rather than buffering it
* whole: {@code content} is already fully resident (RESTEasy Reactive buffers the whole request
* body before {@link CodeArtifactMavenController#deploy} ever runs), so comparing this way
* costs no second full copy for a large artifact the way downloading it to compare would.
*
* <p>Any failure reading the existing artifact, including one genuinely unexpected (a timeout,
* a dropped connection), reads the same as "can't confirm a match" rather than propagating: a
* client's upload must never fail just because this best-effort preflight did, when the real
* PUT below is always there as the safe fallback.
*/
private boolean existingArtifactMatches(String baseUrl, String repoId, String gav, byte[] content) {
HttpRequest request = authenticated(baseUrl, repoId, gav).GET().build();
try {
HttpResponse<InputStream> response = httpClient.send(request, BodyHandlers.ofInputStream());
if (response.statusCode() != 200) {
response.body().close();
return false;
}
try (InputStream in = response.body()) {
byte[] buffer = new byte[8192];
int offset = 0;
int read;
while ((read = in.read(buffer)) != -1) {
if (offset + read > content.length
|| !Arrays.equals(buffer, 0, read, content, offset, offset + read)) {
return false;
}
offset += read;
}
return offset == content.length;
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return false;
} catch (IOException e) {
LOG.warnv(e, "Could not check {0}/{1} for an existing match before deploying: {2}", repoId, gav,
e.getMessage());
return false;
}
}
/** Fetches {@code repoId}'s {@code gav} path, or {@link Optional#empty()} on a non-200 response. */
public Optional<FetchedArtifact> fetchArtifact(String repoId, String gav) {
String baseUrl = manager.ensureReady();
return fetchArtifact(manager.ensureReady(), repoId, gav);
}
private Optional<FetchedArtifact> fetchArtifact(String baseUrl, String repoId, String gav) {
HttpRequest request = authenticated(baseUrl, repoId, gav).GET().build();
HttpResponse<byte[]> response = send(request, BodyHandlers.ofByteArray());
if (response.statusCode() != 200) {
@@ -90,8 +90,54 @@ class CodeArtifactMavenDockerIntegrationTest {
.then().statusCode(200);
}
/**
* Reposilite's {@code redeployment: false} rejects a redeploy to an existing GAV path
* unconditionally, even when the new content is byte-identical to what is already there. AWS's
* own CodeArtifact docs ("Overwriting package assets" in packages-overview.html) say that case
* specifically succeeds because the operation is idempotent; only a genuine content mismatch,
* covered by {@link #redeployingDifferentBytesStillConflicts}, is a real conflict.
*/
@Test
@Order(2)
void redeployingTheExactSameBytesSucceedsIdempotently() {
byte[] content = "real-jar-bytes".getBytes(StandardCharsets.UTF_8);
given().header("Authorization", "Bearer " + bearerToken).body(content)
.put("/codeartifact/maven/" + DOMAIN + "/" + REPO + "/" + GAV)
.then().statusCode(200);
byte[] fetched = given().header("Authorization", "Bearer " + bearerToken)
.get("/codeartifact/maven/" + DOMAIN + "/" + REPO + "/" + GAV)
.then().statusCode(200)
.extract().asByteArray();
assertEquals(new String(content, StandardCharsets.UTF_8), new String(fetched, StandardCharsets.UTF_8));
}
/**
* The real content-mismatch counterpart to the idempotent-retry test above, against the live
* shared Reposilite container rather than a fake server: proves the fix only closes the
* byte-identical-retry gap and leaves Reposilite's own {@code redeployment: false} enforcement
* for a genuine conflict untouched, so a config change there that started allowing silent
* overwrites would still be caught here.
*/
@Test
@Order(3)
void redeployingDifferentBytesStillConflicts() {
byte[] different = "different-jar-bytes".getBytes(StandardCharsets.UTF_8);
given().header("Authorization", "Bearer " + bearerToken).body(different)
.put("/codeartifact/maven/" + DOMAIN + "/" + REPO + "/" + GAV)
.then().statusCode(409);
byte[] fetched = given().header("Authorization", "Bearer " + bearerToken)
.get("/codeartifact/maven/" + DOMAIN + "/" + REPO + "/" + GAV)
.then().statusCode(200)
.extract().asByteArray();
assertEquals("real-jar-bytes", new String(fetched, StandardCharsets.UTF_8));
}
@Test
@Order(4)
void missingArtifactAndMissingRepositoryAreNotFound() {
given().header("Authorization", "Bearer " + bearerToken)
.get("/codeartifact/maven/" + DOMAIN + "/" + REPO + "/does/not/exist.jar")
@@ -103,7 +149,7 @@ class CodeArtifactMavenDockerIntegrationTest {
}
@Test
@Order(3)
@Order(5)
void repositoriesAreIsolatedFromEachOther() {
given().contentType("application/json").header("Authorization", AUTH).body("{}")
.post("/v1/repository?domain=" + DOMAIN + "&repository=other-repo")
@@ -115,7 +161,7 @@ class CodeArtifactMavenDockerIntegrationTest {
}
@Test
@Order(4)
@Order(6)
void missingOrWrongDomainTokensAreUnauthorized() {
// Challenges as Basic, not Bearer: a real Maven wagon client only retries a 401 with its
// configured settings.xml credentials when the challenge scheme matches what it sent.
@@ -141,7 +187,7 @@ class CodeArtifactMavenDockerIntegrationTest {
}
@Test
@Order(5)
@Order(7)
void concurrentFirstUseOfANewRepositoryOnlyProvisionsItOnce() throws InterruptedException {
given().contentType("application/json").header("Authorization", AUTH).body("{}")
.post("/v1/repository?domain=" + DOMAIN + "&repository=concurrent-repo")
@@ -186,7 +232,7 @@ class CodeArtifactMavenDockerIntegrationTest {
}
@Test
@Order(6)
@Order(8)
void recreatingASameNamedRepositoryDoesNotInheritThePreviousOnesArtifacts() {
given().contentType("application/json").header("Authorization", AUTH).body("{}")
.post("/v1/repository?domain=" + DOMAIN + "&repository=reused-name")
@@ -208,7 +254,7 @@ class CodeArtifactMavenDockerIntegrationTest {
}
@Test
@Order(7)
@Order(9)
void aRealMavenClientsBasicAuthCredentialsAreAcceptedWithTheTokenAsThePassword() {
String basic = "Basic " + Base64.getEncoder().encodeToString(("aws:" + bearerToken).getBytes(StandardCharsets.UTF_8));
byte[] content = "basic-auth-bytes".getBytes(StandardCharsets.UTF_8);
@@ -225,7 +271,7 @@ class CodeArtifactMavenDockerIntegrationTest {
}
@Test
@Order(8)
@Order(10)
void aDomainCreatedInANonDefaultRegionIsServedThroughTheTokensOwnRegionNotTheDefault() {
// The default region here is us-east-1 (application.yml). Every other test in this class
// creates its domain under an AUTH header whose SigV4 scope is also us-east-1, so those
@@ -259,7 +305,7 @@ class CodeArtifactMavenDockerIntegrationTest {
}
@Test
@Order(9)
@Order(11)
void aDomainCreatedUnderANonDefaultAccountIsServedThroughTheTokensOwnAccountNotTheDefault() {
// Every other test in this class authenticates as the default account (000000000000), so
// none of them would notice if the owner half of the token-scope fix regressed and every
@@ -291,7 +337,7 @@ class CodeArtifactMavenDockerIntegrationTest {
}
@Test
@Order(9)
@Order(11)
void deletingARepositoryReleasesItsStorageFromTheSharedReposiliteInstance() throws Exception {
given().contentType("application/json").header("Authorization", AUTH).body("{}")
.post("/v1/repository?domain=" + DOMAIN + "&repository=released-repo")
@@ -266,6 +266,74 @@ class ReposiliteSidecarClientTest {
assertThat(authHeader.get(), equalTo("Basic dGVzdDp0ZXN0"));
}
@Test
void deployArtifactShortCircuitsToTwoHundredWithoutPuttingWhenContentIsByteIdentical() {
byte[] content = "hello".getBytes(StandardCharsets.UTF_8);
List<String> methodsSeen = new CopyOnWriteArrayList<>();
server.createContext("/dom--repo/com/example/a/1.0/a-1.0.jar", exchange -> {
methodsSeen.add(exchange.getRequestMethod());
respond(exchange, 200, "hello");
});
int status = client.deployArtifact("dom--repo", "com/example/a/1.0/a-1.0.jar", content);
assertThat(status, is(200));
assertThat(methodsSeen, equalTo(List.of("GET")));
}
@Test
void deployArtifactStillPutsWhenExistingContentDiffers() {
AtomicReference<byte[]> putBody = new AtomicReference<>();
List<String> methodsSeen = new CopyOnWriteArrayList<>();
server.createContext("/dom--repo/com/example/a/1.0/a-1.0.jar", exchange -> {
methodsSeen.add(exchange.getRequestMethod());
if ("PUT".equals(exchange.getRequestMethod())) {
putBody.set(exchange.getRequestBody().readAllBytes());
// Reposilite's real redeployment:false response for a genuine content mismatch;
// this must pass straight through unchanged.
respond(exchange, 409, "{\"status\":409,\"message\":\"Redeployment is not allowed\"}");
} else {
respond(exchange, 200, "goodbye");
}
});
int status = client.deployArtifact("dom--repo", "com/example/a/1.0/a-1.0.jar",
"hello".getBytes(StandardCharsets.UTF_8));
assertThat(status, is(409));
assertThat(methodsSeen, equalTo(List.of("GET", "PUT")));
assertArrayEquals("hello".getBytes(StandardCharsets.UTF_8), putBody.get());
}
@Test
void deployArtifactStillPutsWhenThePreflightCheckFails() {
AtomicReference<byte[]> putBody = new AtomicReference<>();
List<String> methodsSeen = new CopyOnWriteArrayList<>();
server.createContext("/dom--repo/com/example/a/1.0/a-1.0.jar", exchange -> {
methodsSeen.add(exchange.getRequestMethod());
if ("PUT".equals(exchange.getRequestMethod())) {
putBody.set(exchange.getRequestBody().readAllBytes());
respond(exchange, 200, "");
} else {
// Simulates a dropped connection on the preflight GET (a timeout, a transient
// network error): closing without ever sending a response is what the client sees
// as an IOException, not a clean 4xx/5xx. The PUT must still go through. The JDK's
// own HttpClient transparently retries an idempotent GET once on this kind of
// connection-level failure before giving up, so more than one GET landing here
// first is expected and not itself part of what this test is checking.
exchange.close();
}
});
int status = client.deployArtifact("dom--repo", "com/example/a/1.0/a-1.0.jar",
"hello".getBytes(StandardCharsets.UTF_8));
assertThat(status, is(200));
assertTrue(methodsSeen.stream().allMatch(m -> m.equals("GET") || m.equals("PUT")));
assertThat(methodsSeen.get(methodsSeen.size() - 1), equalTo("PUT"));
assertArrayEquals("hello".getBytes(StandardCharsets.UTF_8), putBody.get());
}
@Test
void fetchArtifactReturnsBytesAndContentTypeOnSuccess() {
server.createContext("/dom--repo/com/example/a/1.0/a-1.0.jar", exchange -> {