mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(codeartifact): resolve Maven version ranges and dynamic versions through an external connection (#6961)
A bare group/artifact-level maven-metadata.xml request used to be rejected outright, so a Maven version range or a Gradle dynamic version could never resolve against a connected repository that had not already retained some other fixed version of the same package some other way. This merges the public repository's own maven-metadata.xml release-version list into the local copy instead: only release versions (never snapshots or versions this repository already knows are Archived/Disposed) are merged, a version already listed locally is never duplicated, and an already-existing local latest/release is never overridden by the external source, since there is no reliable way to compare Maven version strings for recency. The actual asset bytes for whichever version a resolver settles on are still only fetched lazily through the existing per-asset import path, unchanged. A bare metadata request always retries the merge, even once a local copy already exists, so a public release published after the first import is still discoverable later; a fixed version's own asset keeps the old miss-only gate, since it never changes once retained. The merge writes matching .sha1/.md5 checksums alongside the metadata, repairing them even on an otherwise-no-op retry, and reads any existing local copy through the confirmed-404 check rather than treating a transient read error as if no local metadata existed. The merge itself is fenced at package granularity the same way a per-version write already is, so a DeletePackage or DeleteRepository that finishes while the real, public-repository fetch is still in flight can never have its removal undone by a write that lands afterward: the package fence now also bumps on an origin-config-only delete, not just one that removed real content, and the merge separately re-checks that the repository itself still exists under the same lock, since neither case bumps any individual package's fence on its own. Also fixes a package-existence check that this surfaced: writing the merged metadata into the group/artifact directory made the sidecar's own packageExists treat a metadata-only package as already directly published, permanently blocking every later retention attempt for it since no claim could ever get recorded to prove otherwise. packageExists now requires an actual version subdirectory, not just any file, to consider the package present; a new hasAnyContentToDelete still catches the metadata-only case for DeletePackage, so a package left in that state is still cleared. Verified against the real, live public:maven-central connection with a genuine mvn dependency:resolve run against a version range, not just the mocked test suite.
This commit is contained in:
1 parent
e2d65a6378
commit
00ab75b395
10 files changed
+1068
-44
No files matched your search
+98
@@ -0,0 +1,98 @@
|
||||
package io.github.hectorvent.floci.services.codeartifact;
|
||||
|
||||
import io.github.hectorvent.floci.services.codeartifact.CodeArtifactService.AuthorizationTokenScope;
|
||||
import io.github.hectorvent.floci.services.codeartifact.ReposiliteSidecarClient.FetchedArtifact;
|
||||
import jakarta.ws.rs.core.HttpHeaders;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* {@link CodeArtifactMavenController#fetch}'s own decision of when to ask {@link
|
||||
* CodeArtifactMavenExternalConnectionImporter} to attempt an external-connection import at all,
|
||||
* not the importer's or {@link ReposiliteSidecarClient}'s own logic, which each have their own
|
||||
* tests. Both collaborators are mocked, so no real sidecar or network call ever happens here.
|
||||
*/
|
||||
class CodeArtifactMavenControllerTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String OWNER = "123456789012";
|
||||
private static final String DOMAIN = "dom";
|
||||
private static final String REPOSITORY = "repo";
|
||||
private static final String REPO_ID = "repo-id";
|
||||
private static final String METADATA_GAV = "com/example/widget/maven-metadata.xml";
|
||||
|
||||
private CodeArtifactService service;
|
||||
private ReposiliteSidecarClient reposilite;
|
||||
private CodeArtifactMavenExternalConnectionImporter externalConnectionImporter;
|
||||
private CodeArtifactMavenController controller;
|
||||
private HttpHeaders headers;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
service = mock(CodeArtifactService.class);
|
||||
reposilite = mock(ReposiliteSidecarClient.class);
|
||||
externalConnectionImporter = mock(CodeArtifactMavenExternalConnectionImporter.class);
|
||||
controller = new CodeArtifactMavenController(service, reposilite, externalConnectionImporter);
|
||||
headers = mock(HttpHeaders.class);
|
||||
when(headers.getHeaderString(HttpHeaders.AUTHORIZATION)).thenReturn("Bearer some-token");
|
||||
when(service.resolveAuthorizationToken("some-token", DOMAIN))
|
||||
.thenReturn(Optional.of(new AuthorizationTokenScope(OWNER, REGION)));
|
||||
when(service.ensureFormatContainerId("maven", REGION, DOMAIN, OWNER, REPOSITORY)).thenReturn(REPO_ID);
|
||||
}
|
||||
|
||||
/**
|
||||
* Raised by Greptile's review: once a bare group/artifact-level {@code maven-metadata.xml}
|
||||
* import ever succeeds, a local copy exists from then on, so the old miss-only gate
|
||||
* ({@code artifact.isEmpty()}) would never let a later request try the importer again, and a
|
||||
* public release published after that first import would stay invisible to every version
|
||||
* range or dynamic version indefinitely. This exact path is now always retried, so a second
|
||||
* request still picks up whatever the importer merges in afterward.
|
||||
*/
|
||||
@Test
|
||||
void aSecondRequestForBareMetadataStillAttemptsAFreshImportEvenWhenALocalCopyAlreadyExists() {
|
||||
FetchedArtifact stale = new FetchedArtifact("<metadata/>".getBytes(StandardCharsets.UTF_8),
|
||||
"application/xml");
|
||||
FetchedArtifact refreshed = new FetchedArtifact("<metadata><refreshed/></metadata>"
|
||||
.getBytes(StandardCharsets.UTF_8), "application/xml");
|
||||
when(reposilite.fetchArtifact(REPO_ID, METADATA_GAV)).thenReturn(Optional.of(stale))
|
||||
.thenReturn(Optional.of(refreshed));
|
||||
when(externalConnectionImporter.tryImport(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, METADATA_GAV))
|
||||
.thenReturn(true);
|
||||
|
||||
Response response = controller.fetch(headers, DOMAIN, REPOSITORY, METADATA_GAV);
|
||||
|
||||
assertEquals(200, response.getStatus());
|
||||
assertEquals("<metadata><refreshed/></metadata>",
|
||||
new String((byte[]) response.getEntity(), StandardCharsets.UTF_8));
|
||||
verify(externalConnectionImporter).tryImport(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, METADATA_GAV);
|
||||
}
|
||||
|
||||
/**
|
||||
* The counterpart that keeps the always-retry behavior scoped to bare metadata: a fixed
|
||||
* version's own asset never changes once retained, so re-asking the importer for it on every
|
||||
* request a local copy already answers would just be wasted external-connection traffic.
|
||||
*/
|
||||
@Test
|
||||
void aFixedVersionAssetIsNotReimportedOnceALocalCopyAlreadyExists() {
|
||||
String versionGav = "com/example/widget/1.0/widget-1.0.jar";
|
||||
FetchedArtifact existing = new FetchedArtifact("real-bytes".getBytes(StandardCharsets.UTF_8),
|
||||
"application/java-archive");
|
||||
when(reposilite.fetchArtifact(REPO_ID, versionGav)).thenReturn(Optional.of(existing));
|
||||
|
||||
Response response = controller.fetch(headers, DOMAIN, REPOSITORY, versionGav);
|
||||
|
||||
assertEquals(200, response.getStatus());
|
||||
verify(externalConnectionImporter, never()).tryImport(any(), any(), any(), any(), any(), any());
|
||||
}
|
||||
}
|
||||
+43
-1
@@ -14,6 +14,7 @@ import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.greaterThan;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@@ -25,7 +26,9 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
* network dependency, unlike every other sidecar-backed test here, which only ever talks to a
|
||||
* container Floci itself started; see {@code CodeArtifactMavenExternalConnectionImporterTest}
|
||||
* for the mocked, deterministic coverage of the asset-selection and mixed-origin logic this test
|
||||
* does not re-exercise.
|
||||
* does not re-exercise, and {@code ReposiliteSidecarClientTest} for the same of the
|
||||
* {@code maven-metadata.xml} merge a Maven version range or Gradle dynamic version actually relies
|
||||
* on to resolve at all.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestProfile(SidecarContainersProfile.class)
|
||||
@@ -36,8 +39,10 @@ class CodeArtifactMavenExternalConnectionDockerIntegrationTest {
|
||||
private static final String DOMAIN = "maven-external-connection-domain";
|
||||
private static final String REPO = "maven-external-connection-repo";
|
||||
private static final String BLOCKED_REPO = "maven-external-connection-blocked-repo";
|
||||
private static final String DYNAMIC_REPO = "maven-external-connection-dynamic-repo";
|
||||
private static final String JUNIT_JAR = "junit/junit/4.13.2/junit-4.13.2.jar";
|
||||
private static final String JUNIT_POM = "junit/junit/4.13.2/junit-4.13.2.pom";
|
||||
private static final String JUNIT_METADATA = "junit/junit/maven-metadata.xml";
|
||||
|
||||
private static String bearerToken;
|
||||
|
||||
@@ -59,6 +64,9 @@ class CodeArtifactMavenExternalConnectionDockerIntegrationTest {
|
||||
given().contentType("application/json").header("Authorization", AUTH).body("{}")
|
||||
.post("/v1/repository?domain=" + DOMAIN + "&repository=" + BLOCKED_REPO)
|
||||
.then().statusCode(200);
|
||||
given().contentType("application/json").header("Authorization", AUTH).body("{}")
|
||||
.post("/v1/repository?domain=" + DOMAIN + "&repository=" + DYNAMIC_REPO)
|
||||
.then().statusCode(200);
|
||||
given().contentType("application/json").header("Authorization", AUTH).body("{}")
|
||||
.post("/v1/repository/external-connection?domain=" + DOMAIN + "&repository=" + REPO
|
||||
+ "&external-connection=public:maven-central")
|
||||
@@ -67,6 +75,10 @@ class CodeArtifactMavenExternalConnectionDockerIntegrationTest {
|
||||
.post("/v1/repository/external-connection?domain=" + DOMAIN + "&repository=" + BLOCKED_REPO
|
||||
+ "&external-connection=public:maven-central")
|
||||
.then().statusCode(200);
|
||||
given().contentType("application/json").header("Authorization", AUTH).body("{}")
|
||||
.post("/v1/repository/external-connection?domain=" + DOMAIN + "&repository=" + DYNAMIC_REPO
|
||||
+ "&external-connection=public:maven-central")
|
||||
.then().statusCode(200);
|
||||
|
||||
bearerToken = given().header("Authorization", AUTH)
|
||||
.post("/v1/authorization-token?domain=" + DOMAIN)
|
||||
@@ -151,4 +163,34 @@ class CodeArtifactMavenExternalConnectionDockerIntegrationTest {
|
||||
+ "/com/example/does-not-exist/1.0-SNAPSHOT/does-not-exist-1.0-SNAPSHOT.jar")
|
||||
.then().statusCode(404);
|
||||
}
|
||||
|
||||
/**
|
||||
* The sequence a real Maven version range or Gradle dynamic version actually produces: a bare
|
||||
* {@code maven-metadata.xml} request first, merging in the real release versions {@code
|
||||
* public:maven-central} names for junit, then a request for whichever exact version got picked
|
||||
* (4.13.2, the real latest). Runs against its own repository, never touched by an earlier
|
||||
* {@code @Order}, so the metadata request here is genuinely this package's first contact with
|
||||
* the sidecar: merging metadata before any version is ever retained once made {@code
|
||||
* packageExists} see the group/artifact directory as already present and permanently refuse to
|
||||
* ever retain a real version for it, since no claim could get recorded to prove the package's
|
||||
* origin was external rather than directly published. This is the regression test for that.
|
||||
*/
|
||||
@Test
|
||||
@Order(6)
|
||||
void aVersionRangeResolvesThroughAMetadataRequestThenRetainsTheChosenVersion() {
|
||||
byte[] metadata = given().header("Authorization", "Bearer " + bearerToken)
|
||||
.get("/codeartifact/maven/" + DOMAIN + "/" + DYNAMIC_REPO + "/" + JUNIT_METADATA)
|
||||
.then().statusCode(200)
|
||||
.extract().asByteArray();
|
||||
String metadataXml = new String(metadata, StandardCharsets.UTF_8);
|
||||
assertThat(metadataXml, containsString("<version>4.13.2</version>"));
|
||||
|
||||
byte[] jar = given().header("Authorization", "Bearer " + bearerToken)
|
||||
.get("/codeartifact/maven/" + DOMAIN + "/" + DYNAMIC_REPO + "/" + JUNIT_JAR)
|
||||
.then().statusCode(200)
|
||||
.extract().asByteArray();
|
||||
assertEquals('P', (char) jar[0]);
|
||||
assertEquals('K', (char) jar[1]);
|
||||
assertThat(jar.length, greaterThan(1000));
|
||||
}
|
||||
}
|
||||
+115
-1
@@ -1,13 +1,16 @@
|
||||
package io.github.hectorvent.floci.services.codeartifact;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.codeartifact.model.CodeArtifactRepository;
|
||||
import io.github.hectorvent.floci.services.codeartifact.model.ExternalConnection;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InOrder;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
@@ -15,6 +18,7 @@ import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
@@ -99,7 +103,117 @@ class CodeArtifactMavenExternalConnectionImporterTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void doesNothingForABareMavenMetadataPath() {
|
||||
void resolvesMetadataThroughTheExternalConnectionWhenNotFoundLocally() {
|
||||
byte[] metadataBytes = "<metadata/>".getBytes();
|
||||
when(externalConnectionClient.fetchAsset("public:maven-central", "com/example/widget/maven-metadata.xml"))
|
||||
.thenReturn(Optional.of(new MavenExternalConnectionClient.FetchedAsset(metadataBytes)));
|
||||
when(service.mergeMavenMetadataUnderPackageFence(eq(REGION), eq(DOMAIN), eq(OWNER), eq(REPOSITORY),
|
||||
eq("com.example"), eq("widget"), anyLong(), eq(reposilite), eq(REPO_ID), eq(metadataBytes), any()))
|
||||
.thenReturn(true);
|
||||
|
||||
boolean imported = importer.tryImport(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID,
|
||||
"com/example/widget/maven-metadata.xml");
|
||||
|
||||
assertTrue(imported);
|
||||
}
|
||||
|
||||
/**
|
||||
* Raised by Greptile's review: the real fetch from the public repository can take long enough
|
||||
* for a concurrent {@code DeletePackage} to finish while it is still in flight, so the fence
|
||||
* snapshot passed to {@link CodeArtifactService#mergeMavenMetadataUnderPackageFence} must be
|
||||
* taken before that fetch, not after, or a delete landing in between would never be caught by
|
||||
* the check that runs inside that same call.
|
||||
*/
|
||||
@Test
|
||||
void takesThePackageFenceSnapshotBeforeFetchingFromThePublicRepository() {
|
||||
byte[] metadataBytes = "<metadata/>".getBytes();
|
||||
when(service.currentContainerBackedPackageFence(REGION, DOMAIN, OWNER, REPOSITORY, "maven", "com.example",
|
||||
"widget")).thenReturn(42L);
|
||||
when(externalConnectionClient.fetchAsset("public:maven-central", "com/example/widget/maven-metadata.xml"))
|
||||
.thenReturn(Optional.of(new MavenExternalConnectionClient.FetchedAsset(metadataBytes)));
|
||||
when(service.mergeMavenMetadataUnderPackageFence(eq(REGION), eq(DOMAIN), eq(OWNER), eq(REPOSITORY),
|
||||
eq("com.example"), eq("widget"), eq(42L), eq(reposilite), eq(REPO_ID), eq(metadataBytes), any()))
|
||||
.thenReturn(true);
|
||||
|
||||
boolean imported = importer.tryImport(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID,
|
||||
"com/example/widget/maven-metadata.xml");
|
||||
|
||||
assertTrue(imported);
|
||||
verify(service).mergeMavenMetadataUnderPackageFence(eq(REGION), eq(DOMAIN), eq(OWNER), eq(REPOSITORY),
|
||||
eq("com.example"), eq("widget"), eq(42L), eq(reposilite), eq(REPO_ID), eq(metadataBytes), any());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link CodeArtifactMavenExternalConnectionImporter#tryImportMetadata} builds its own
|
||||
* {@code versionAllowed} predicate from {@link
|
||||
* CodeArtifactService#requireContainerBackedVersionDownloadable}, the exact same existence
|
||||
* check a per-version download itself already enforces: a version this repository knows is
|
||||
* {@code Archived}/{@code Disposed} throws from that check and must come back {@code false},
|
||||
* while a version with no override at all (the common case: nothing here has ever recorded
|
||||
* one) must come back {@code true}.
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void versionAllowedPredicateRejectsOnlyVersionsTheServiceReportsNotDownloadable() {
|
||||
byte[] metadataBytes = "<metadata/>".getBytes();
|
||||
when(externalConnectionClient.fetchAsset("public:maven-central", "com/example/widget/maven-metadata.xml"))
|
||||
.thenReturn(Optional.of(new MavenExternalConnectionClient.FetchedAsset(metadataBytes)));
|
||||
when(service.mergeMavenMetadataUnderPackageFence(eq(REGION), eq(DOMAIN), eq(OWNER), eq(REPOSITORY),
|
||||
eq("com.example"), eq("widget"), anyLong(), eq(reposilite), eq(REPO_ID), eq(metadataBytes), any()))
|
||||
.thenReturn(true);
|
||||
doThrow(new AwsException("ResourceNotFoundException", "gone", 404))
|
||||
.when(service).requireContainerBackedVersionDownloadable(OWNER, REGION, DOMAIN, REPOSITORY, "maven",
|
||||
"com.example", "widget", "1.0-disposed", REPO_ID);
|
||||
|
||||
importer.tryImport(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, "com/example/widget/maven-metadata.xml");
|
||||
|
||||
ArgumentCaptor<Predicate<String>> captor = ArgumentCaptor.forClass(Predicate.class);
|
||||
verify(service).mergeMavenMetadataUnderPackageFence(eq(REGION), eq(DOMAIN), eq(OWNER), eq(REPOSITORY),
|
||||
eq("com.example"), eq("widget"), anyLong(), eq(reposilite), eq(REPO_ID), eq(metadataBytes),
|
||||
captor.capture());
|
||||
Predicate<String> versionAllowed = captor.getValue();
|
||||
assertFalse(versionAllowed.test("1.0-disposed"));
|
||||
assertTrue(versionAllowed.test("1.0"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void doesNothingForABareMavenMetadataPathWhenTheExternalSourceHasNone() {
|
||||
when(externalConnectionClient.fetchAsset("public:maven-central", "com/example/widget/maven-metadata.xml"))
|
||||
.thenReturn(Optional.empty());
|
||||
|
||||
boolean imported = importer.tryImport(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID,
|
||||
"com/example/widget/maven-metadata.xml");
|
||||
|
||||
assertFalse(imported);
|
||||
verify(service, never()).mergeMavenMetadataUnderPackageFence(any(), any(), any(), any(), any(), any(),
|
||||
anyLong(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void doesNothingForAMavenMetadataChecksumSiblingPath() {
|
||||
boolean imported = importer.tryImport(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID,
|
||||
"com/example/widget/maven-metadata.xml.sha1");
|
||||
|
||||
assertFalse(imported);
|
||||
verify(externalConnectionClient, never()).fetchAsset(anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void doesNothingForMavenMetadataWhenTheRepositoryHasNoMavenExternalConnection() {
|
||||
when(service.describeRepository(REGION, DOMAIN, OWNER, REPOSITORY)).thenReturn(new CodeArtifactRepository());
|
||||
|
||||
boolean imported = importer.tryImport(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID,
|
||||
"com/example/widget/maven-metadata.xml");
|
||||
|
||||
assertFalse(imported);
|
||||
verify(externalConnectionClient, never()).fetchAsset(anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void doesNothingForMavenMetadataWhenUpstreamRetentionIsBlocked() {
|
||||
when(service.isUpstreamRetentionBlocked(REGION, DOMAIN, OWNER, REPOSITORY, "maven", "com.example",
|
||||
"widget")).thenReturn(true);
|
||||
|
||||
boolean imported = importer.tryImport(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID,
|
||||
"com/example/widget/maven-metadata.xml");
|
||||
|
||||
|
||||
+155
-21
@@ -398,6 +398,8 @@ class CodeArtifactServiceTest {
|
||||
service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
|
||||
when(reposiliteClient.packageExists(anyString(), eq("dom"), eq("repo"), eq("com.example"), eq("removed")))
|
||||
.thenReturn(true);
|
||||
when(reposiliteClient.hasAnyContentToDelete(anyString(), eq("dom"), eq("repo"), eq("com.example"),
|
||||
eq("removed"))).thenReturn(true);
|
||||
recordBytes("dom", "repo", "maven", "com.example", "kept", 40L);
|
||||
recordBytes("dom", "repo", "maven", "com.example", "removed", 60L);
|
||||
assertEquals(100L, service.describeDomain(REGION, "dom", null).assetSizeBytes());
|
||||
@@ -495,6 +497,8 @@ class CodeArtifactServiceTest {
|
||||
"late");
|
||||
when(reposiliteClient.packageExists(anyString(), eq("dom"), eq("repo"), eq("com.example"), eq("late")))
|
||||
.thenReturn(true);
|
||||
when(reposiliteClient.hasAnyContentToDelete(anyString(), eq("dom"), eq("repo"), eq("com.example"),
|
||||
eq("late"))).thenReturn(true);
|
||||
service.deletePackage(REGION, "dom", null, "repo", "maven", "com.example", "late");
|
||||
|
||||
service.recordContainerBackedAssetBytes(REGION, "dom", null, "repo", "maven", "com.example", "late", 999L,
|
||||
@@ -517,6 +521,8 @@ class CodeArtifactServiceTest {
|
||||
"late");
|
||||
when(reposiliteClient.packageExists(anyString(), eq("dom"), eq("repo"), eq("com.example"), eq("late")))
|
||||
.thenReturn(true);
|
||||
when(reposiliteClient.hasAnyContentToDelete(anyString(), eq("dom"), eq("repo"), eq("com.example"),
|
||||
eq("late"))).thenReturn(true);
|
||||
service.deletePackage(REGION, "dom", null, "repo", "maven", "com.example", "late");
|
||||
// The republish: a different publish, same coordinates, after the first one's snapshot.
|
||||
recordBytes("dom", "repo", "maven", "com.example", "late", 10L);
|
||||
@@ -562,6 +568,8 @@ class CodeArtifactServiceTest {
|
||||
assertEquals(42L, service.describeDomain(REGION, "dom", null).assetSizeBytes());
|
||||
when(pypiserverClient.packageExists(anyString(), eq("dom"), eq("repo"), isNull(), eq("my_package")))
|
||||
.thenReturn(true);
|
||||
when(pypiserverClient.hasAnyContentToDelete(anyString(), eq("dom"), eq("repo"), isNull(), eq("my_package")))
|
||||
.thenReturn(true);
|
||||
|
||||
service.deletePackage(REGION, "dom", null, "repo", "pypi", null, "my_package");
|
||||
|
||||
@@ -582,6 +590,8 @@ class CodeArtifactServiceTest {
|
||||
assertEquals(42L, service.describeDomain(REGION, "dom", null).assetSizeBytes());
|
||||
when(bagetterClient.packageExists(anyString(), eq("dom"), eq("repo"), isNull(), eq("my.package")))
|
||||
.thenReturn(true);
|
||||
when(bagetterClient.hasAnyContentToDelete(anyString(), eq("dom"), eq("repo"), isNull(), eq("my.package")))
|
||||
.thenReturn(true);
|
||||
|
||||
service.deletePackage(REGION, "dom", null, "repo", "nuget", null, "my.package");
|
||||
|
||||
@@ -2107,6 +2117,8 @@ class CodeArtifactServiceTest {
|
||||
// something real to delete and, as part of its own ordinary cleanup, wipes the origin map
|
||||
// entry outright, the same way it already does today, regardless of any in-flight count.
|
||||
when(reposiliteClient.packageExists(repoId, "dom", "repo", "com.example", "widget")).thenReturn(true);
|
||||
when(reposiliteClient.hasAnyContentToDelete(repoId, "dom", "repo", "com.example", "widget"))
|
||||
.thenReturn(true);
|
||||
service.deletePackage(REGION, "dom", null, "repo", "maven", "com.example", "widget");
|
||||
|
||||
// A brand new episode is claimed fresh under the exact same connection name.
|
||||
@@ -2290,38 +2302,38 @@ class CodeArtifactServiceTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* DeletePackage on a package with no real content yet (nothing a stalled import has written
|
||||
* back) removes the origin claim outright but, having deleted nothing, never bumps the package
|
||||
* fence. A sibling attempt still mid-fetch under that now-removed claim takes its
|
||||
* versionFenceSnapshot/packageFenceSnapshot before the delete, so neither fence alone would
|
||||
* ever catch the delete happening in between; a fresh claim made afterward under a different
|
||||
* connection must still be shielded from that sibling's own stale write.
|
||||
* A failed attempt (nothing imported) releases its own claim through {@link
|
||||
* CodeArtifactService#finishMavenRetainedVersionOriginAttempt} without touching either fence
|
||||
* at all, so a fresh claim made afterward under a different connection leaves both fences
|
||||
* exactly as they were. A very late, straggling write from the attempt that already reported
|
||||
* itself finished (its own real network write's retry or duplicate, arriving well after its
|
||||
* finish call already ran) would otherwise still see both fences match and write real bytes
|
||||
* under whatever connection claimed the version next, mixing assets from two different public
|
||||
* sources into one version; only the episode check catches this, since neither fence ever
|
||||
* moved. {@link #deployMavenAssetUnderVersionFenceRefusesAStaleEpisodeAfterAnEmptyPackageDeleteAndFreshClaim}'s
|
||||
* delete-based scenario now also gets caught by the package fence itself, since {@link
|
||||
* CodeArtifactService#deletePackage} bumps it on an origin-config-only delete too; this
|
||||
* scenario has no delete in it at all, so it keeps the episode check genuinely necessary.
|
||||
*/
|
||||
@Test
|
||||
void deployMavenAssetUnderVersionFenceRefusesAStaleEpisodeAfterAnEmptyPackageDeleteAndFreshClaim() {
|
||||
void deployMavenAssetUnderVersionFenceRefusesAStaleEpisodeAfterAFailedAttemptReleasesItAndAFreshClaimFollows() {
|
||||
service.createDomain(REGION, "dom", null, Map.of());
|
||||
CodeArtifactRepository created = service.createRepository(REGION, "dom", null, "repo", null, null,
|
||||
Map.of());
|
||||
String repoId = created.getSidecarContainerIds().get("maven");
|
||||
// An origin configuration (e.g. from an earlier PutPackageOriginConfiguration) is what
|
||||
// lets deletePackage find this package at all despite no real content ever landing for
|
||||
// it; without one, deletePackage would 404 outright rather than reaching the empty-content
|
||||
// path this test is exercising.
|
||||
service.putPackageOriginConfiguration(REGION, "dom", null, "repo", "maven", "com.example", "widget",
|
||||
"ALLOW", "ALLOW");
|
||||
long packageFence = service.currentContainerBackedPackageFence(REGION, "dom", null, "repo", "maven",
|
||||
"com.example", "widget");
|
||||
long versionFence = service.currentContainerBackedVersionFence(REGION, "dom", null, "repo", "maven",
|
||||
"com.example", "widget", "1.0");
|
||||
// The stalled attempt: claims under the first connection, then takes no further action
|
||||
// until after the delete and fresh claim below (simulating a fetch still in flight).
|
||||
// until after it finishes empty-handed and a fresh claim follows below (simulating a
|
||||
// fetch still in flight for a real network write that arrives only after that).
|
||||
long staleEpisodeId = service.claimMavenRetainedVersionOrigin(REGION, "dom", null, "repo", "com.example",
|
||||
"widget", "1.0", "public:maven-central", packageFence).episodeId();
|
||||
|
||||
// No real content exists for this package yet, so this delete finds nothing to remove and
|
||||
// never bumps the package fence, even though it does wipe the origin claim above.
|
||||
when(reposiliteClient.packageExists(repoId, "dom", "repo", "com.example", "widget")).thenReturn(false);
|
||||
service.deletePackage(REGION, "dom", null, "repo", "maven", "com.example", "widget");
|
||||
// Finds nothing from the public repository: releases its own claim, touching neither fence.
|
||||
service.finishMavenRetainedVersionOriginAttempt(REGION, "dom", null, "repo", "com.example", "widget", "1.0",
|
||||
staleEpisodeId, false);
|
||||
assertEquals(packageFence, service.currentContainerBackedPackageFence(REGION, "dom", null, "repo", "maven",
|
||||
"com.example", "widget"));
|
||||
|
||||
@@ -2329,9 +2341,9 @@ class CodeArtifactServiceTest {
|
||||
service.claimMavenRetainedVersionOrigin(REGION, "dom", null, "repo", "com.example", "widget", "1.0",
|
||||
"public:maven-clojars", packageFence);
|
||||
|
||||
// The stalled attempt's fetch finally returns and tries to write under its own stale
|
||||
// episode: both fences still match (neither was ever bumped), so only the episode check
|
||||
// can refuse this write.
|
||||
// The stalled attempt's own late write finally arrives and tries to write under its own
|
||||
// stale episode: both fences still match (neither was ever bumped), so only the episode
|
||||
// check can refuse this write.
|
||||
String gav = "com/example/widget/1.0/widget-1.0.jar";
|
||||
CodeArtifactService.MavenDeployAttemptResult result = service.deployMavenAssetUnderVersionFence(REGION,
|
||||
"dom", null, "repo", "com.example", "widget", "1.0", versionFence, packageFence, staleEpisodeId,
|
||||
@@ -2341,6 +2353,118 @@ class CodeArtifactServiceTest {
|
||||
verify(reposiliteClient, never()).deployArtifact(anyString(), anyString(), any());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link CodeArtifactService#mergeMavenMetadataUnderPackageFence}'s own version of the same
|
||||
* race {@link #deployMavenAssetUnderVersionFenceWritesNothingWhenTheVersionFenceHasMoved}
|
||||
* covers for a per-version write: a {@code DeletePackage} that finishes while the real,
|
||||
* public-repository metadata fetch is still in flight must bump the package fence behind the
|
||||
* snapshot the importer took before that fetch, so the merge that follows never lands.
|
||||
*/
|
||||
@Test
|
||||
void mergeMavenMetadataUnderPackageFenceWritesNothingWhenThePackageFenceHasMoved() {
|
||||
service.createDomain(REGION, "dom", null, Map.of());
|
||||
CodeArtifactRepository created = service.createRepository(REGION, "dom", null, "repo", null, null,
|
||||
Map.of());
|
||||
String repoId = created.getSidecarContainerIds().get("maven");
|
||||
long packageFenceSnapshot = service.currentContainerBackedPackageFence(REGION, "dom", null, "repo", "maven",
|
||||
"com.example", "widget");
|
||||
// The DeletePackage that lands while the importer's own real fetch is still in flight,
|
||||
// simulated here the same way as every other fence test in this class.
|
||||
when(reposiliteClient.hasAnyContentToDelete(repoId, "dom", "repo", "com.example", "widget"))
|
||||
.thenReturn(true);
|
||||
service.deletePackage(REGION, "dom", null, "repo", "maven", "com.example", "widget");
|
||||
|
||||
boolean merged = service.mergeMavenMetadataUnderPackageFence(REGION, "dom", null, "repo", "com.example",
|
||||
"widget", packageFenceSnapshot, reposiliteClient, repoId, "<metadata/>".getBytes(
|
||||
StandardCharsets.UTF_8), v -> true);
|
||||
|
||||
assertFalse(merged);
|
||||
verify(reposiliteClient, never()).mergeExternalConnectionReleaseVersions(any(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void mergeMavenMetadataUnderPackageFenceDelegatesToReposiliteWhenTheFenceStillMatches() {
|
||||
service.createDomain(REGION, "dom", null, Map.of());
|
||||
CodeArtifactRepository created = service.createRepository(REGION, "dom", null, "repo", null, null,
|
||||
Map.of());
|
||||
String repoId = created.getSidecarContainerIds().get("maven");
|
||||
long packageFenceSnapshot = service.currentContainerBackedPackageFence(REGION, "dom", null, "repo", "maven",
|
||||
"com.example", "widget");
|
||||
byte[] metadataBytes = "<metadata/>".getBytes(StandardCharsets.UTF_8);
|
||||
when(reposiliteClient.mergeExternalConnectionReleaseVersions(eq(repoId), eq("com.example"), eq("widget"),
|
||||
eq(metadataBytes), any())).thenReturn(true);
|
||||
|
||||
boolean merged = service.mergeMavenMetadataUnderPackageFence(REGION, "dom", null, "repo", "com.example",
|
||||
"widget", packageFenceSnapshot, reposiliteClient, repoId, metadataBytes, v -> true);
|
||||
|
||||
assertTrue(merged);
|
||||
verify(reposiliteClient).mergeExternalConnectionReleaseVersions(eq(repoId), eq("com.example"), eq("widget"),
|
||||
eq(metadataBytes), any());
|
||||
}
|
||||
|
||||
/**
|
||||
* Raised by Greptile's review: an origin-config-only package (a {@code
|
||||
* PutPackageOriginConfiguration} with no version ever actually published or retained) used to
|
||||
* leave the package fence untouched on delete, since {@link
|
||||
* CodeArtifactMavenExternalConnectionImporterTest} and {@link
|
||||
* #mergeMavenMetadataUnderPackageFenceWritesNothingWhenThePackageFenceHasMoved} only ever cover
|
||||
* a delete that found real content. A {@code DeletePackage} racing a metadata fetch still in
|
||||
* flight for exactly that kind of package would otherwise leave the fence matching, letting the
|
||||
* merge land afterward and resurrect a package {@code DeletePackage} had already told its
|
||||
* caller was gone. See {@link CodeArtifactService#deletePackage}'s own javadoc for the fix.
|
||||
*/
|
||||
@Test
|
||||
void mergeMavenMetadataUnderPackageFenceWritesNothingAfterAnOriginConfigOnlyPackageIsDeleted() {
|
||||
service.createDomain(REGION, "dom", null, Map.of());
|
||||
CodeArtifactRepository created = service.createRepository(REGION, "dom", null, "repo", null, null,
|
||||
Map.of());
|
||||
String repoId = created.getSidecarContainerIds().get("maven");
|
||||
service.putPackageOriginConfiguration(REGION, "dom", null, "repo", "maven", "com.example", "widget",
|
||||
"ALLOW", "ALLOW");
|
||||
long packageFenceSnapshot = service.currentContainerBackedPackageFence(REGION, "dom", null, "repo", "maven",
|
||||
"com.example", "widget");
|
||||
// No real content exists for this package, only the origin configuration above, the same
|
||||
// way the importer's own metadata fetch found nothing to retain yet either.
|
||||
when(reposiliteClient.hasAnyContentToDelete(repoId, "dom", "repo", "com.example", "widget"))
|
||||
.thenReturn(false);
|
||||
|
||||
service.deletePackage(REGION, "dom", null, "repo", "maven", "com.example", "widget");
|
||||
|
||||
boolean merged = service.mergeMavenMetadataUnderPackageFence(REGION, "dom", null, "repo", "com.example",
|
||||
"widget", packageFenceSnapshot, reposiliteClient, repoId, "<metadata/>".getBytes(
|
||||
StandardCharsets.UTF_8), v -> true);
|
||||
|
||||
assertFalse(merged);
|
||||
verify(reposiliteClient, never()).mergeExternalConnectionReleaseVersions(any(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
/**
|
||||
* Raised by Greptile's review: the package fence alone says nothing about {@link
|
||||
* CodeArtifactService#deleteRepository} removing the whole repository, not just one package,
|
||||
* while the same metadata fetch is in flight; {@code deleteRepository} never touches any
|
||||
* individual package's fence, so the snapshot still matches even though the repository itself
|
||||
* is gone. Without a separate check, the sidecar merge's own {@code ensureRepository} would
|
||||
* transparently recreate the repository {@code deleteRepository} had just torn down.
|
||||
*/
|
||||
@Test
|
||||
void mergeMavenMetadataUnderPackageFenceWritesNothingAfterTheRepositoryItselfIsDeleted() {
|
||||
service.createDomain(REGION, "dom", null, Map.of());
|
||||
CodeArtifactRepository created = service.createRepository(REGION, "dom", null, "repo", null, null,
|
||||
Map.of());
|
||||
String repoId = created.getSidecarContainerIds().get("maven");
|
||||
long packageFenceSnapshot = service.currentContainerBackedPackageFence(REGION, "dom", null, "repo", "maven",
|
||||
"com.example", "widget");
|
||||
|
||||
service.deleteRepository(REGION, "dom", null, "repo");
|
||||
|
||||
boolean merged = service.mergeMavenMetadataUnderPackageFence(REGION, "dom", null, "repo", "com.example",
|
||||
"widget", packageFenceSnapshot, reposiliteClient, repoId, "<metadata/>".getBytes(
|
||||
StandardCharsets.UTF_8), v -> true);
|
||||
|
||||
assertFalse(merged);
|
||||
verify(reposiliteClient, never()).mergeExternalConnectionReleaseVersions(any(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
/**
|
||||
* Same coordinate-boundary collision as above, but for published version content rather than
|
||||
* origin configuration: without escaping, the second publish's bytes would be stored under the
|
||||
@@ -2564,6 +2688,8 @@ class CodeArtifactServiceTest {
|
||||
String repoId = created.getSidecarContainerIds().get("maven");
|
||||
when(reposiliteClient.packageExists(eq(repoId), eq("dom"), eq("repo"), eq("com.example"),
|
||||
eq("my-artifact"))).thenReturn(true);
|
||||
when(reposiliteClient.hasAnyContentToDelete(eq(repoId), eq("dom"), eq("repo"), eq("com.example"),
|
||||
eq("my-artifact"))).thenReturn(true);
|
||||
|
||||
CodeArtifactService.PackageDescription deleted = service.deletePackage(REGION, "dom", null, "repo", "maven",
|
||||
"com.example", "my-artifact");
|
||||
@@ -2599,6 +2725,8 @@ class CodeArtifactServiceTest {
|
||||
service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
|
||||
when(pypiserverClient.packageExists(anyString(), eq("dom"), eq("repo"), isNull(), eq("my-pkg")))
|
||||
.thenReturn(true);
|
||||
when(pypiserverClient.hasAnyContentToDelete(anyString(), eq("dom"), eq("repo"), isNull(), eq("my-pkg")))
|
||||
.thenReturn(true);
|
||||
doThrow(new UnsupportedOperationException("pypi packages cannot be deleted through this sidecar"))
|
||||
.when(pypiserverClient).deletePackage(anyString(), eq("dom"), eq("repo"), isNull(), eq("my-pkg"));
|
||||
|
||||
@@ -6243,6 +6371,8 @@ class CodeArtifactServiceTest {
|
||||
recordReadme("dom", "repo", "pypi", null, "my-pkg", "1.0.0", "old text");
|
||||
when(pypiserverClient.packageExists(anyString(), eq("dom"), eq("repo"), isNull(), eq("my-pkg")))
|
||||
.thenReturn(true);
|
||||
when(pypiserverClient.hasAnyContentToDelete(anyString(), eq("dom"), eq("repo"), isNull(), eq("my-pkg")))
|
||||
.thenReturn(true);
|
||||
|
||||
service.deletePackage(REGION, "dom", null, "repo", "pypi", null, "my-pkg");
|
||||
|
||||
@@ -6307,6 +6437,8 @@ class CodeArtifactServiceTest {
|
||||
long fence = service.currentContainerBackedPackageFence(REGION, "dom", null, "repo", "npm", null, "late");
|
||||
when(verdaccioClient.packageExists(anyString(), eq("dom"), eq("repo"), isNull(), eq("late")))
|
||||
.thenReturn(true);
|
||||
when(verdaccioClient.hasAnyContentToDelete(anyString(), eq("dom"), eq("repo"), isNull(), eq("late")))
|
||||
.thenReturn(true);
|
||||
service.deletePackage(REGION, "dom", null, "repo", "npm", null, "late");
|
||||
|
||||
service.recordPublishedPackageVersionReadme(REGION, "dom", null, "repo", "npm", null, "late", "1.0.0",
|
||||
@@ -6345,6 +6477,8 @@ class CodeArtifactServiceTest {
|
||||
recordReadme("dom", "repo", "pypi", null, "My.Package", "1.0.0", "the readme");
|
||||
when(pypiserverClient.packageExists(anyString(), eq("dom"), eq("repo"), isNull(), eq("my-package")))
|
||||
.thenReturn(true);
|
||||
when(pypiserverClient.hasAnyContentToDelete(anyString(), eq("dom"), eq("repo"), isNull(), eq("my-package")))
|
||||
.thenReturn(true);
|
||||
|
||||
service.deletePackage(REGION, "dom", null, "repo", "pypi", null, "my-package");
|
||||
|
||||
|
||||
+284
-4
@@ -574,17 +574,30 @@ class ReposiliteSidecarClientTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void packageExistsReflectsAGetResponseOnTheGroupArtifactDirectory() {
|
||||
server.createContext("/dom--repo/com/example/present/", exchange -> respond(exchange, 200, ""));
|
||||
server.createContext("/dom--repo/com/example/absent/", exchange -> respond(exchange, 404, ""));
|
||||
void packageExistsReflectsWhetherTheGroupArtifactDirectoryHasAVersionSubdirectory() {
|
||||
server.createContext("/api/maven/details/dom--repo/com/example/present/",
|
||||
exchange -> respond(exchange, 200, "{\"files\":["
|
||||
+ "{\"name\":\"1.0\",\"type\":\"DIRECTORY\"}]}"));
|
||||
server.createContext("/api/maven/details/dom--repo/com/example/absent/", exchange -> respond(exchange, 404,
|
||||
"{\"status\":404,\"message\":\"File not found\"}"));
|
||||
|
||||
assertTrue(client.packageExists("dom--repo", "dom", "repo", "com.example", "present"));
|
||||
assertFalse(client.packageExists("dom--repo", "dom", "repo", "com.example", "absent"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void packageExistsIsFalseForAGroupArtifactDirectoryThatHasOnlyAMergedMetadataFileAndNoRealVersion() {
|
||||
server.createContext("/api/maven/details/dom--repo/com/example/metadata-only/",
|
||||
exchange -> respond(exchange, 200, "{\"files\":["
|
||||
+ "{\"name\":\"maven-metadata.xml\",\"type\":\"FILE\"}]}"));
|
||||
|
||||
assertFalse(client.packageExists("dom--repo", "dom", "repo", "com.example", "metadata-only"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void packageExistsThrowsRatherThanReportingAbsentOnASidecarServerError() {
|
||||
server.createContext("/dom--repo/com/example/broken/", exchange -> respond(exchange, 500, ""));
|
||||
server.createContext("/api/maven/details/dom--repo/com/example/broken/", exchange -> respond(exchange, 500,
|
||||
"{\"status\":500,\"message\":\"internal error\"}"));
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> client.packageExists("dom--repo", "dom", "repo",
|
||||
"com.example", "broken"));
|
||||
@@ -788,6 +801,273 @@ class ReposiliteSidecarClientTest {
|
||||
assertEquals("1.0", XmlParser.extractFirst(xml, "release", null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void mergeExternalConnectionReleaseVersionsCreatesMetadataFromTheExternalSourceWhenNoneExistsLocally() {
|
||||
settingsBody.set("{\"repositories\":[{\"id\":\"dom--repo\",\"visibility\":\"PUBLIC\",\"redeployment\":false}]}");
|
||||
server.createContext("/api/settings/domain/maven", exchange -> respond(exchange, 200, settingsBody.get()));
|
||||
String metadataPath = "/dom--repo/com/example/a/maven-metadata.xml";
|
||||
AtomicReference<byte[]> putBody = new AtomicReference<>();
|
||||
server.createContext(metadataPath, exchange -> {
|
||||
if ("GET".equals(exchange.getRequestMethod())) {
|
||||
respond(exchange, 404, "");
|
||||
} else if (metadataPath.equals(exchange.getRequestURI().getPath())) {
|
||||
putBody.set(exchange.getRequestBody().readAllBytes());
|
||||
respond(exchange, 200, "");
|
||||
} else {
|
||||
respond(exchange, 200, "");
|
||||
}
|
||||
});
|
||||
String externalXml = "<metadata><groupId>com.example</groupId><artifactId>a</artifactId><versioning>"
|
||||
+ "<latest>1.2</latest><release>1.2</release><versions><version>1.0</version>"
|
||||
+ "<version>1.1</version><version>1.2</version></versions></versioning></metadata>";
|
||||
|
||||
boolean merged = client.mergeExternalConnectionReleaseVersions("dom--repo", "com.example", "a",
|
||||
externalXml.getBytes(StandardCharsets.UTF_8), v -> true);
|
||||
|
||||
assertTrue(merged);
|
||||
String xml = new String(putBody.get(), StandardCharsets.UTF_8);
|
||||
assertEquals("com.example", XmlParser.extractFirst(xml, "groupId", null));
|
||||
assertEquals("a", XmlParser.extractFirst(xml, "artifactId", null));
|
||||
assertEquals("1.2", XmlParser.extractFirst(xml, "latest", null));
|
||||
assertEquals("1.2", XmlParser.extractFirst(xml, "release", null));
|
||||
assertEquals(List.of("1.0", "1.1", "1.2"), XmlParser.extractAll(xml, "version"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void mergeExternalConnectionReleaseVersionsWritesMatchingSha1AndMd5Checksums() {
|
||||
settingsBody.set("{\"repositories\":[{\"id\":\"dom--repo\",\"visibility\":\"PUBLIC\",\"redeployment\":false}]}");
|
||||
server.createContext("/api/settings/domain/maven", exchange -> respond(exchange, 200, settingsBody.get()));
|
||||
String metadataPath = "/dom--repo/com/example/a/maven-metadata.xml";
|
||||
AtomicReference<byte[]> putBody = new AtomicReference<>();
|
||||
AtomicReference<byte[]> sha1Body = new AtomicReference<>();
|
||||
AtomicReference<byte[]> md5Body = new AtomicReference<>();
|
||||
server.createContext(metadataPath, exchange -> {
|
||||
if ("GET".equals(exchange.getRequestMethod())) {
|
||||
respond(exchange, 404, "");
|
||||
} else if (metadataPath.equals(exchange.getRequestURI().getPath())) {
|
||||
putBody.set(exchange.getRequestBody().readAllBytes());
|
||||
respond(exchange, 200, "");
|
||||
} else {
|
||||
respond(exchange, 200, "");
|
||||
}
|
||||
});
|
||||
server.createContext(metadataPath + ".sha1", exchange -> {
|
||||
sha1Body.set(exchange.getRequestBody().readAllBytes());
|
||||
respond(exchange, 200, "");
|
||||
});
|
||||
server.createContext(metadataPath + ".md5", exchange -> {
|
||||
md5Body.set(exchange.getRequestBody().readAllBytes());
|
||||
respond(exchange, 200, "");
|
||||
});
|
||||
String externalXml = "<metadata><groupId>com.example</groupId><artifactId>a</artifactId><versioning>"
|
||||
+ "<latest>1.0</latest><release>1.0</release><versions><version>1.0</version></versions>"
|
||||
+ "</versioning></metadata>";
|
||||
|
||||
client.mergeExternalConnectionReleaseVersions("dom--repo", "com.example", "a",
|
||||
externalXml.getBytes(StandardCharsets.UTF_8), v -> true);
|
||||
|
||||
assertEquals(Digests.sha1Hex(putBody.get()), new String(sha1Body.get(), StandardCharsets.UTF_8));
|
||||
assertEquals(Digests.md5Hex(putBody.get()), new String(md5Body.get(), StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
void mergeExternalConnectionReleaseVersionsExcludesAVersionTheVersionAllowedPredicateRejects() {
|
||||
settingsBody.set("{\"repositories\":[{\"id\":\"dom--repo\",\"visibility\":\"PUBLIC\",\"redeployment\":false}]}");
|
||||
server.createContext("/api/settings/domain/maven", exchange -> respond(exchange, 200, settingsBody.get()));
|
||||
String metadataPath = "/dom--repo/com/example/a/maven-metadata.xml";
|
||||
AtomicReference<byte[]> putBody = new AtomicReference<>();
|
||||
server.createContext(metadataPath, exchange -> {
|
||||
if ("GET".equals(exchange.getRequestMethod())) {
|
||||
respond(exchange, 404, "");
|
||||
} else if (metadataPath.equals(exchange.getRequestURI().getPath())) {
|
||||
putBody.set(exchange.getRequestBody().readAllBytes());
|
||||
respond(exchange, 200, "");
|
||||
} else {
|
||||
respond(exchange, 200, "");
|
||||
}
|
||||
});
|
||||
String externalXml = "<metadata><groupId>com.example</groupId><artifactId>a</artifactId><versioning>"
|
||||
+ "<latest>1.1</latest><release>1.1</release><versions><version>1.0</version>"
|
||||
+ "<version>1.1</version></versions></versioning></metadata>";
|
||||
|
||||
// Simulates a version this repository already knows is Disposed: a public repository's
|
||||
// own metadata has no idea that happened, so a merge must never let a rejected version
|
||||
// back in just because the public source still lists it.
|
||||
boolean merged = client.mergeExternalConnectionReleaseVersions("dom--repo", "com.example", "a",
|
||||
externalXml.getBytes(StandardCharsets.UTF_8), v -> !v.equals("1.1"));
|
||||
|
||||
assertTrue(merged);
|
||||
String xml = new String(putBody.get(), StandardCharsets.UTF_8);
|
||||
assertEquals(List.of("1.0"), XmlParser.extractAll(xml, "version"));
|
||||
assertEquals("1.0", XmlParser.extractFirst(xml, "latest", null));
|
||||
assertEquals("1.0", XmlParser.extractFirst(xml, "release", null));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link io.github.hectorvent.floci.services.codeartifact.ReposiliteSidecarClient#fetchArtifact}
|
||||
* returns {@code empty()} for a transient server error too, not just a confirmed 404: a merge
|
||||
* that read the existing local metadata through that method could mistake the error for "no
|
||||
* local metadata yet" and silently replace a real, locally-published package's {@code latest}/
|
||||
* {@code release} with the external source's own guess at them. The merge must surface the
|
||||
* failure instead.
|
||||
*/
|
||||
@Test
|
||||
void mergeExternalConnectionReleaseVersionsThrowsOnATransientReadErrorInsteadOfTreatingItAsMissing() {
|
||||
settingsBody.set("{\"repositories\":[{\"id\":\"dom--repo\",\"visibility\":\"PUBLIC\",\"redeployment\":false}]}");
|
||||
server.createContext("/api/settings/domain/maven", exchange -> respond(exchange, 200, settingsBody.get()));
|
||||
server.createContext("/dom--repo/com/example/a/maven-metadata.xml", exchange -> respond(exchange, 500, ""));
|
||||
String externalXml = "<metadata><groupId>com.example</groupId><artifactId>a</artifactId><versioning>"
|
||||
+ "<latest>1.0</latest><release>1.0</release><versions><version>1.0</version></versions>"
|
||||
+ "</versioning></metadata>";
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> client.mergeExternalConnectionReleaseVersions("dom--repo",
|
||||
"com.example", "a", externalXml.getBytes(StandardCharsets.UTF_8), v -> true));
|
||||
}
|
||||
|
||||
/**
|
||||
* A locally-published version has no reliable way to be compared against whatever the public
|
||||
* repository's own metadata declares as its latest/release (Maven version strings don't sort
|
||||
* by any universal rule), so a merge must only ever add the versions the local copy doesn't
|
||||
* already have, never move {@code latest}/{@code release} away from what a real local publish
|
||||
* already recorded.
|
||||
*/
|
||||
@Test
|
||||
void mergeExternalConnectionReleaseVersionsAppendsMissingVersionsWithoutOverridingAnExistingLocalLatest() {
|
||||
settingsBody.set("{\"repositories\":[{\"id\":\"dom--repo\",\"visibility\":\"PUBLIC\",\"redeployment\":false}]}");
|
||||
server.createContext("/api/settings/domain/maven", exchange -> respond(exchange, 200, settingsBody.get()));
|
||||
String existing = "<metadata><groupId>com.example</groupId><artifactId>a</artifactId><versioning>"
|
||||
+ "<latest>2.0</latest><release>2.0</release><versions><version>2.0</version></versions>"
|
||||
+ "<lastUpdated>20260101000000</lastUpdated></versioning></metadata>";
|
||||
String metadataPath = "/dom--repo/com/example/a/maven-metadata.xml";
|
||||
AtomicReference<byte[]> putBody = new AtomicReference<>();
|
||||
server.createContext(metadataPath, exchange -> {
|
||||
if ("GET".equals(exchange.getRequestMethod())) {
|
||||
respond(exchange, 200, existing);
|
||||
} else if (metadataPath.equals(exchange.getRequestURI().getPath())) {
|
||||
putBody.set(exchange.getRequestBody().readAllBytes());
|
||||
respond(exchange, 200, "");
|
||||
} else {
|
||||
respond(exchange, 200, "");
|
||||
}
|
||||
});
|
||||
String externalXml = "<metadata><groupId>com.example</groupId><artifactId>a</artifactId><versioning>"
|
||||
+ "<latest>1.1</latest><release>1.1</release><versions><version>1.0</version>"
|
||||
+ "<version>1.1</version></versions></versioning></metadata>";
|
||||
|
||||
boolean merged = client.mergeExternalConnectionReleaseVersions("dom--repo", "com.example", "a",
|
||||
externalXml.getBytes(StandardCharsets.UTF_8), v -> true);
|
||||
|
||||
assertTrue(merged);
|
||||
String xml = new String(putBody.get(), StandardCharsets.UTF_8);
|
||||
assertEquals(List.of("2.0", "1.0", "1.1"), XmlParser.extractAll(xml, "version"));
|
||||
assertEquals("2.0", XmlParser.extractFirst(xml, "latest", null));
|
||||
assertEquals("2.0", XmlParser.extractFirst(xml, "release", null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void mergeExternalConnectionReleaseVersionsFiltersOutSnapshotVersionsFromTheExternalSource() {
|
||||
settingsBody.set("{\"repositories\":[{\"id\":\"dom--repo\",\"visibility\":\"PUBLIC\",\"redeployment\":false}]}");
|
||||
server.createContext("/api/settings/domain/maven", exchange -> respond(exchange, 200, settingsBody.get()));
|
||||
String metadataPath = "/dom--repo/com/example/a/maven-metadata.xml";
|
||||
AtomicReference<byte[]> putBody = new AtomicReference<>();
|
||||
server.createContext(metadataPath, exchange -> {
|
||||
if ("GET".equals(exchange.getRequestMethod())) {
|
||||
respond(exchange, 404, "");
|
||||
} else if (metadataPath.equals(exchange.getRequestURI().getPath())) {
|
||||
putBody.set(exchange.getRequestBody().readAllBytes());
|
||||
respond(exchange, 200, "");
|
||||
} else {
|
||||
respond(exchange, 200, "");
|
||||
}
|
||||
});
|
||||
String externalXml = "<metadata><groupId>com.example</groupId><artifactId>a</artifactId><versioning>"
|
||||
+ "<latest>1.1-SNAPSHOT</latest><versions><version>1.0</version>"
|
||||
+ "<version>1.1-SNAPSHOT</version></versions></versioning></metadata>";
|
||||
|
||||
boolean merged = client.mergeExternalConnectionReleaseVersions("dom--repo", "com.example", "a",
|
||||
externalXml.getBytes(StandardCharsets.UTF_8), v -> true);
|
||||
|
||||
assertTrue(merged);
|
||||
String xml = new String(putBody.get(), StandardCharsets.UTF_8);
|
||||
assertEquals(List.of("1.0"), XmlParser.extractAll(xml, "version"));
|
||||
assertEquals("1.0", XmlParser.extractFirst(xml, "latest", null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void mergeExternalConnectionReleaseVersionsIsANoOpWhenEveryExternalVersionIsAlreadyListedLocally() {
|
||||
settingsBody.set("{\"repositories\":[{\"id\":\"dom--repo\",\"visibility\":\"PUBLIC\",\"redeployment\":false}]}");
|
||||
server.createContext("/api/settings/domain/maven", exchange -> respond(exchange, 200, settingsBody.get()));
|
||||
String existing = "<metadata><groupId>com.example</groupId><artifactId>a</artifactId><versioning>"
|
||||
+ "<latest>1.0</latest><release>1.0</release><versions><version>1.0</version></versions>"
|
||||
+ "<lastUpdated>20260101000000</lastUpdated></versioning></metadata>";
|
||||
String metadataPath = "/dom--repo/com/example/a/maven-metadata.xml";
|
||||
AtomicReference<byte[]> putBody = new AtomicReference<>();
|
||||
server.createContext(metadataPath, exchange -> {
|
||||
if ("GET".equals(exchange.getRequestMethod())) {
|
||||
respond(exchange, 200, existing);
|
||||
} else if (metadataPath.equals(exchange.getRequestURI().getPath())) {
|
||||
putBody.set(exchange.getRequestBody().readAllBytes());
|
||||
respond(exchange, 200, "");
|
||||
} else {
|
||||
respond(exchange, 200, "");
|
||||
}
|
||||
});
|
||||
String externalXml = "<metadata><groupId>com.example</groupId><artifactId>a</artifactId><versioning>"
|
||||
+ "<latest>1.0</latest><release>1.0</release><versions><version>1.0</version></versions></"
|
||||
+ "versioning></metadata>";
|
||||
|
||||
boolean merged = client.mergeExternalConnectionReleaseVersions("dom--repo", "com.example", "a",
|
||||
externalXml.getBytes(StandardCharsets.UTF_8), v -> true);
|
||||
|
||||
assertFalse(merged);
|
||||
assertNull(putBody.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Raised by Greptile's review: a prior attempt whose metadata write succeeded but whose
|
||||
* checksum write then failed would otherwise leave a stale checksum in place forever, since
|
||||
* every later request for this exact package also finds nothing new to add and takes this
|
||||
* same no-op path. The repair must happen every time this path is taken, not just once.
|
||||
*/
|
||||
@Test
|
||||
void mergeExternalConnectionReleaseVersionsRepairsChecksumsEvenWhenItIsANoOp() {
|
||||
settingsBody.set("{\"repositories\":[{\"id\":\"dom--repo\",\"visibility\":\"PUBLIC\",\"redeployment\":false}]}");
|
||||
server.createContext("/api/settings/domain/maven", exchange -> respond(exchange, 200, settingsBody.get()));
|
||||
String existing = "<metadata><groupId>com.example</groupId><artifactId>a</artifactId><versioning>"
|
||||
+ "<latest>1.0</latest><release>1.0</release><versions><version>1.0</version></versions>"
|
||||
+ "<lastUpdated>20260101000000</lastUpdated></versioning></metadata>";
|
||||
String metadataPath = "/dom--repo/com/example/a/maven-metadata.xml";
|
||||
AtomicReference<byte[]> sha1Body = new AtomicReference<>();
|
||||
AtomicReference<byte[]> md5Body = new AtomicReference<>();
|
||||
server.createContext(metadataPath, exchange -> {
|
||||
if ("GET".equals(exchange.getRequestMethod())) {
|
||||
respond(exchange, 200, existing);
|
||||
} else {
|
||||
respond(exchange, 200, "");
|
||||
}
|
||||
});
|
||||
server.createContext(metadataPath + ".sha1", exchange -> {
|
||||
sha1Body.set(exchange.getRequestBody().readAllBytes());
|
||||
respond(exchange, 200, "");
|
||||
});
|
||||
server.createContext(metadataPath + ".md5", exchange -> {
|
||||
md5Body.set(exchange.getRequestBody().readAllBytes());
|
||||
respond(exchange, 200, "");
|
||||
});
|
||||
String externalXml = "<metadata><groupId>com.example</groupId><artifactId>a</artifactId><versioning>"
|
||||
+ "<latest>1.0</latest><release>1.0</release><versions><version>1.0</version></versions></"
|
||||
+ "versioning></metadata>";
|
||||
|
||||
boolean merged = client.mergeExternalConnectionReleaseVersions("dom--repo", "com.example", "a",
|
||||
externalXml.getBytes(StandardCharsets.UTF_8), v -> true);
|
||||
|
||||
assertFalse(merged);
|
||||
assertEquals(Digests.sha1Hex(existing.getBytes(StandardCharsets.UTF_8)),
|
||||
new String(sha1Body.get(), StandardCharsets.UTF_8));
|
||||
assertEquals(Digests.md5Hex(existing.getBytes(StandardCharsets.UTF_8)),
|
||||
new String(md5Body.get(), StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link io.github.hectorvent.floci.services.codeartifact.ReposiliteSidecarClient#recordVersionDisposed}'s
|
||||
* dispose-time counterpart to {@link #recordVersionAvailableAppendsToExistingVersionsAndAdvancesReleaseForANonSnapshot}:
|
||||
|
||||
Reference in new issue
Block a user