fix(codeartifact): resolve upstreams in AWS's order, through their external connections (#8356)

Three ways a read through upstream repositories differed from the User
Guide:

- Upstreams were searched breadth-first. "If an upstream repository has
  its own upstream repositories, the same logic is used to find a package
  version before moving to the next upstream repository": the guide's
  own example, my_repo with upstreams A and B, A with C and D and D with
  E, is searched my_repo, A, C, D, E, B. The walk is depth-first in each
  repository's listed order now.
- A version an upstream had itself retained named that upstream as its
  origin, and one the upstream got from public:npmjs came back INTERNAL.
  A version's DomainEntryPoint is where it "originally entered" the
  domain, so a retained version now carries its upstream's own entry
  point: the external connection, EXTERNAL, or the repository it was
  published to, however many upstreams it came through. It is read
  again when the version's claim is confirmed, so a copy made while the
  upstream's own fetch is still finishing gets that fetch's connection.
- An upstream's external connection was never consulted, so a version no
  upstream had fetched yet was a 404. AWS fetches it through the
  upstream's connection and retains it there and in the repository the
  request was made to. A miss at every upstream now tries the external
  connections of the upstreams in the same order, for npm, Maven, PyPI
  and NuGet, and a packument, Simple index or maven-metadata.xml read
  through such an upstream offers what its connection has. That fetch
  skips a version Archived or Disposed at the upstream, or here in the
  Simple index, reads this repository's fences before it starts, so a
  deletion during it is not undone, and passes on an npm client's ask
  for the full packument. The four formats share it in
  UpstreamExternalConnectionFetch.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-14 15:42:29 -04:00
1 parent 045ecc4eeb
commit e286aef015
23 files changed
+984 -149

No files matched your search

@@ -523,4 +523,14 @@ class CodeArtifactMavenExternalConnectionImporterTest {
"com/example/widget/1.0-SNAPSHOT/widget-1.0-20231001.123456-1.jar"));
verify(externalConnectionClient, never()).exists(anyString(), anyString());
}
/** An upstream deleted since a search order named it has no connection to fetch through. */
@Test
void aDeletedRepositoryHasNoExternalConnection() {
when(service.describeRepository(REGION, DOMAIN, OWNER, "deleted"))
.thenThrow(new AwsException("ResourceNotFoundException", "Repository 'deleted' not found.", 404));
assertFalse(importer.hasExternalConnection(REGION, DOMAIN, OWNER, "deleted"));
assertTrue(importer.hasExternalConnection(REGION, DOMAIN, OWNER, REPOSITORY));
}
}
@@ -4,6 +4,7 @@ import io.floci.aws.core.common.AwsException;
import io.floci.aws.services.codeartifact.model.CodeArtifactRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InOrder;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
@@ -22,8 +23,10 @@ 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;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -52,13 +55,15 @@ class CodeArtifactMavenUpstreamRepositoryImporterTest {
private CodeArtifactService service;
private ReposiliteSidecarClient reposilite;
private CodeArtifactMavenExternalConnectionImporter externalConnectionImporter;
private CodeArtifactMavenUpstreamRepositoryImporter importer;
@BeforeEach
void setUp() {
service = mock(CodeArtifactService.class);
reposilite = mock(ReposiliteSidecarClient.class);
importer = new CodeArtifactMavenUpstreamRepositoryImporter(service, reposilite);
externalConnectionImporter = mock(CodeArtifactMavenExternalConnectionImporter.class);
importer = new CodeArtifactMavenUpstreamRepositoryImporter(service, reposilite, externalConnectionImporter);
CodeArtifactRepository repository = new CodeArtifactRepository();
repository.setUpstreams(List.of(UPSTREAM));
@@ -298,6 +303,98 @@ class CodeArtifactMavenUpstreamRepositoryImporterTest {
PACKAGE_NAME, VERSION, EPISODE_ID, false);
}
/**
* An artifact no upstream holds is fetched through an upstream's own external connection, retained
* there first, then retained here from that upstream.
*/
@Test
void anArtifactNoUpstreamHoldsIsFetchedThroughAnUpstreamsExternalConnection() {
when(reposilite.artifactExists(UPSTREAM_REPO_ID, GAV)).thenReturn(false);
when(externalConnectionImporter.hasExternalConnection(REGION, DOMAIN, OWNER, UPSTREAM)).thenReturn(true);
when(externalConnectionImporter.tryImport(REGION, DOMAIN, OWNER, UPSTREAM, UPSTREAM_REPO_ID, GAV))
.thenReturn(true);
stubFetchArtifact(UPSTREAM_REPO_ID, GAV, "jar-bytes".getBytes());
assertTrue(importer.tryImport(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, GAV));
// The fences here are read before the fetch through the upstream's connection, so a deletion
// here during that fetch moves them away from the snapshot.
InOrder order = inOrder(service, externalConnectionImporter);
order.verify(service).currentContainerBackedPackageFence(REGION, DOMAIN, OWNER, REPOSITORY, "maven",
NAMESPACE, PACKAGE_NAME);
order.verify(service).currentContainerBackedVersionFence(REGION, DOMAIN, OWNER, REPOSITORY, "maven",
NAMESPACE, PACKAGE_NAME, VERSION);
order.verify(externalConnectionImporter).tryImport(REGION, DOMAIN, OWNER, UPSTREAM, UPSTREAM_REPO_ID, GAV);
order.verify(service).claimMavenRetainedVersionOrigin(REGION, DOMAIN, OWNER, REPOSITORY, NAMESPACE,
PACKAGE_NAME, VERSION, UPSTREAM, RetentionOriginKind.UPSTREAM_REPOSITORY, 42L);
// Leased once by the walk and once for the fetch through its connection, each released.
verify(service, times(2)).resolveContainerIdAndBeginUse("maven", REGION, DOMAIN, OWNER, UPSTREAM, reposilite);
verify(reposilite, times(2)).endUse(UPSTREAM_REPO_ID);
}
/**
* The upstream's connection answers for a version the upstream already holds, Archived: the walk
* over what the upstreams hold skipped it, and the fetch through the connection skips it too.
*/
@Test
void aVersionArchivedAtTheUpstreamIsNotCopiedThroughItsExternalConnection() {
doThrow(new AwsException("ResourceNotFoundException", "archived", 404)).when(service)
.requireContainerBackedVersionDownloadable(OWNER, REGION, DOMAIN, UPSTREAM, "maven", NAMESPACE,
PACKAGE_NAME, VERSION, UPSTREAM_REPO_ID);
when(externalConnectionImporter.hasExternalConnection(REGION, DOMAIN, OWNER, UPSTREAM)).thenReturn(true);
when(externalConnectionImporter.tryImport(REGION, DOMAIN, OWNER, UPSTREAM, UPSTREAM_REPO_ID, GAV))
.thenReturn(true);
assertFalse(importer.tryImport(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, GAV));
verify(service, never()).claimMavenRetainedVersionOrigin(anyString(), anyString(), anyString(), anyString(),
anyString(), anyString(), anyString(), anyString(), any(), anyLong());
verify(reposilite, times(2)).endUse(UPSTREAM_REPO_ID);
}
/** A merge through the connection that fails leaves the upstream's own metadata to read. */
@Test
void aFailedMergeThroughAnUpstreamsExternalConnectionStillReadsItsOwnMetadata() {
String metadataGav = "com/example/widget/maven-metadata.xml";
when(externalConnectionImporter.hasExternalConnection(REGION, DOMAIN, OWNER, UPSTREAM)).thenReturn(true);
when(externalConnectionImporter.tryImport(REGION, DOMAIN, OWNER, UPSTREAM, UPSTREAM_REPO_ID, metadataGav))
.thenThrow(new IllegalStateException("sidecar write failed"));
byte[] metadataBytes = METADATA_WITH_ONE_VERSION;
when(reposilite.fetchArtifactStreamed(UPSTREAM_REPO_ID, metadataGav)).thenAnswer(invocation ->
Optional.of(new ReposiliteSidecarClient.StreamedArtifact(
new ByteArrayInputStream(metadataBytes), metadataBytes.length, "text/xml")));
when(service.mergeMavenMetadataUnderPackageFence(eq(REGION), eq(DOMAIN), eq(OWNER), eq(REPOSITORY),
eq(NAMESPACE), eq(PACKAGE_NAME), eq(42L), eq(reposilite), eq(REPO_ID), eq(metadataBytes), any(),
eq(true))).thenReturn(true);
assertTrue(importer.tryImport(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, metadataGav));
verify(reposilite).endUse(UPSTREAM_REPO_ID);
}
@Test
void anUpstreamWithoutAnExternalConnectionIsNotAskedToFetch() {
when(reposilite.artifactExists(UPSTREAM_REPO_ID, GAV)).thenReturn(false);
assertFalse(importer.tryImport(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, GAV));
verify(externalConnectionImporter, never()).tryImport(anyString(), anyString(), anyString(), anyString(),
anyString(), anyString());
}
@Test
void anUpstreamsExternalConnectionMergesItsReleasesIntoTheMetadataReadThroughIt() {
String metadataGav = "com/example/widget/maven-metadata.xml";
when(externalConnectionImporter.hasExternalConnection(REGION, DOMAIN, OWNER, UPSTREAM)).thenReturn(true);
byte[] metadataBytes = METADATA_WITH_ONE_VERSION;
when(reposilite.fetchArtifactStreamed(UPSTREAM_REPO_ID, metadataGav)).thenAnswer(invocation ->
Optional.of(new ReposiliteSidecarClient.StreamedArtifact(
new ByteArrayInputStream(metadataBytes), metadataBytes.length, "text/xml")));
when(service.mergeMavenMetadataUnderPackageFence(eq(REGION), eq(DOMAIN), eq(OWNER), eq(REPOSITORY),
eq(NAMESPACE), eq(PACKAGE_NAME), eq(42L), eq(reposilite), eq(REPO_ID), eq(metadataBytes), any(),
eq(true))).thenReturn(true);
assertTrue(importer.tryImport(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, metadataGav));
verify(externalConnectionImporter).tryImport(REGION, DOMAIN, OWNER, UPSTREAM, UPSTREAM_REPO_ID,
metadataGav);
}
/** Names at least one real version, so the eligibility pre-check lets this candidate through. */
private static final byte[] METADATA_WITH_ONE_VERSION = ("<metadata><versioning><versions>"
+ "<version>1.0</version></versions></versioning></metadata>").getBytes();
@@ -651,4 +651,14 @@ class CodeArtifactNpmExternalConnectionImporterTest {
return Optional.of(new NpmExternalConnectionClient.FetchedFile(content.length));
});
}
/** An upstream deleted since a search order named it has no connection to fetch through. */
@Test
void aDeletedRepositoryHasNoExternalConnection() {
when(service.describeRepository(REGION, DOMAIN, OWNER, "deleted"))
.thenThrow(new AwsException("ResourceNotFoundException", "Repository 'deleted' not found.", 404));
assertFalse(importer.hasExternalConnection(REGION, DOMAIN, OWNER, "deleted"));
assertTrue(importer.hasExternalConnection(REGION, DOMAIN, OWNER, REPOSITORY));
}
}
@@ -7,6 +7,7 @@ import io.floci.aws.core.common.AwsException;
import io.floci.aws.services.codeartifact.model.CodeArtifactRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InOrder;
import java.io.ByteArrayInputStream;
import java.nio.file.Files;
@@ -23,8 +24,10 @@ 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;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -53,13 +56,16 @@ class CodeArtifactNpmUpstreamRepositoryImporterTest {
private CodeArtifactService service;
private VerdaccioSidecarClient verdaccio;
private CodeArtifactNpmExternalConnectionImporter externalConnectionImporter;
private CodeArtifactNpmUpstreamRepositoryImporter importer;
@BeforeEach
void setUp() {
service = mock(CodeArtifactService.class);
verdaccio = mock(VerdaccioSidecarClient.class);
importer = new CodeArtifactNpmUpstreamRepositoryImporter(service, verdaccio, new ObjectMapper());
externalConnectionImporter = mock(CodeArtifactNpmExternalConnectionImporter.class);
importer = new CodeArtifactNpmUpstreamRepositoryImporter(service, verdaccio, new ObjectMapper(),
externalConnectionImporter);
CodeArtifactRepository repository = new CodeArtifactRepository();
repository.setUpstreams(List.of(UPSTREAM));
@@ -306,7 +312,7 @@ class CodeArtifactNpmUpstreamRepositoryImporterTest {
when(verdaccio.fetchPackageDocument(BASE_URL, null, PACKAGE_NAME)).thenReturn(Optional.empty());
Optional<ObjectNode> merged = importer.mergedPackument(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, null,
PACKAGE_NAME);
PACKAGE_NAME, false);
assertTrue(merged.isPresent());
JsonNode versionDoc = merged.get().path("versions").path(VERSION);
@@ -316,6 +322,93 @@ class CodeArtifactNpmUpstreamRepositoryImporterTest {
verify(verdaccio).endUse(UPSTREAM_REPO_ID);
}
/**
* A version no upstream holds is fetched through an upstream's own external connection, retained
* there first, then retained here from that upstream, as the User Guide's chain example has it.
*/
@Test
void aVersionNoUpstreamHoldsIsFetchedThroughAnUpstreamsExternalConnection() {
when(verdaccio.packageVersionExists(UPSTREAM_REPO_ID, DOMAIN, UPSTREAM, null, PACKAGE_NAME, VERSION))
.thenReturn(false);
when(externalConnectionImporter.hasExternalConnection(REGION, DOMAIN, OWNER, UPSTREAM)).thenReturn(true);
when(externalConnectionImporter.tryImportAsset(REGION, DOMAIN, OWNER, UPSTREAM, UPSTREAM_REPO_ID, null,
PACKAGE_NAME, VERSION, ASSET_NAME)).thenReturn(true);
when(verdaccio.fetchPackageDocument(UPSTREAM_BASE_URL, null, PACKAGE_NAME))
.thenReturn(Optional.of(upstreamDocument("")));
byte[] tarballBytes = "tarball-content".getBytes();
when(verdaccio.fetchPackageVersionAssetStreamed(UPSTREAM_REPO_ID, DOMAIN, UPSTREAM, null, PACKAGE_NAME,
VERSION, ASSET_NAME)).thenAnswer(invocation -> Optional.of(new RepositorySidecarManager.StreamedAsset(
new ByteArrayInputStream(tarballBytes), tarballBytes.length)));
boolean imported = importer.tryImportAsset(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, null, PACKAGE_NAME,
VERSION, ASSET_NAME);
assertTrue(imported);
// The fences here are read before the fetch through the upstream's connection, so a deletion
// here during that fetch moves them away from the snapshot.
InOrder order = inOrder(service, externalConnectionImporter);
order.verify(service).currentContainerBackedPackageFence(REGION, DOMAIN, OWNER, REPOSITORY, "npm", null,
PACKAGE_NAME);
order.verify(service).currentContainerBackedVersionFence(REGION, DOMAIN, OWNER, REPOSITORY, "npm", null,
PACKAGE_NAME, VERSION);
order.verify(externalConnectionImporter).tryImportAsset(REGION, DOMAIN, OWNER, UPSTREAM, UPSTREAM_REPO_ID,
null, PACKAGE_NAME, VERSION, ASSET_NAME);
order.verify(service).claimRetainedVersionOrigin(REGION, DOMAIN, OWNER, REPOSITORY, "npm", null,
PACKAGE_NAME, VERSION, UPSTREAM, RetentionOriginKind.UPSTREAM_REPOSITORY, 42L);
// Leased once by the walk and once for the fetch through its connection, each released.
verify(service, times(2)).resolveContainerIdAndBeginUse("npm", REGION, DOMAIN, OWNER, UPSTREAM, verdaccio);
verify(verdaccio, times(2)).endUse(UPSTREAM_REPO_ID);
}
/**
* The upstream's connection answers for a version the upstream already holds, Archived: the walk
* over what the upstreams hold skipped it, and the fetch through the connection skips it too.
*/
@Test
void aVersionArchivedAtTheUpstreamIsNotCopiedThroughItsExternalConnection() {
doThrow(new AwsException("ResourceNotFoundException", "archived", 404)).when(service)
.requireContainerBackedVersionDownloadable(OWNER, REGION, DOMAIN, UPSTREAM, "npm", null, PACKAGE_NAME,
VERSION, UPSTREAM_REPO_ID);
when(externalConnectionImporter.hasExternalConnection(REGION, DOMAIN, OWNER, UPSTREAM)).thenReturn(true);
when(externalConnectionImporter.tryImportAsset(REGION, DOMAIN, OWNER, UPSTREAM, UPSTREAM_REPO_ID, null,
PACKAGE_NAME, VERSION, ASSET_NAME)).thenReturn(true);
assertFalse(importer.tryImportAsset(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, null, PACKAGE_NAME,
VERSION, ASSET_NAME));
verify(service, never()).claimRetainedVersionOrigin(anyString(), anyString(), anyString(), anyString(),
anyString(), any(), anyString(), anyString(), anyString(), any(), anyLong());
verify(verdaccio, times(2)).endUse(UPSTREAM_REPO_ID);
}
@Test
void anUpstreamWithoutAnExternalConnectionIsNotAskedToFetch() {
when(verdaccio.packageVersionExists(UPSTREAM_REPO_ID, DOMAIN, UPSTREAM, null, PACKAGE_NAME, VERSION))
.thenReturn(false);
assertFalse(importer.tryImportAsset(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, null, PACKAGE_NAME,
VERSION, ASSET_NAME));
verify(externalConnectionImporter, never()).tryImportAsset(anyString(), anyString(), anyString(),
anyString(), anyString(), any(), anyString(), anyString(), anyString());
}
/** The client's ask for the full document goes on to the upstream's connection, readme and all. */
@Test
void mergedPackumentOffersWhatAnUpstreamsExternalConnectionHas() {
when(externalConnectionImporter.hasExternalConnection(REGION, DOMAIN, OWNER, UPSTREAM)).thenReturn(true);
when(externalConnectionImporter.mergedPackument(REGION, DOMAIN, OWNER, UPSTREAM, UPSTREAM_REPO_ID, null,
PACKAGE_NAME, true)).thenReturn(Optional.of((ObjectNode) upstreamDocument("the readme")));
when(verdaccio.fetchPackageDocument(BASE_URL, null, PACKAGE_NAME)).thenReturn(Optional.empty());
Optional<ObjectNode> merged = importer.mergedPackument(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, null,
PACKAGE_NAME, true);
assertTrue(merged.isPresent());
assertEquals(BASE_URL + PACKAGE_NAME + "/-/" + ASSET_NAME,
merged.get().path("versions").path(VERSION).path("dist").path("tarball").asText());
assertEquals("the readme", merged.get().path("readme").asText());
verify(verdaccio, never()).fetchPackageDocument(UPSTREAM_BASE_URL, null, PACKAGE_NAME);
}
@Test
void mergedPackumentReturnsEmptyWhenNoUpstreamsAreConfigured() {
CodeArtifactRepository repository = new CodeArtifactRepository();
@@ -323,7 +416,7 @@ class CodeArtifactNpmUpstreamRepositoryImporterTest {
when(service.describeRepository(REGION, DOMAIN, OWNER, REPOSITORY)).thenReturn(repository);
Optional<ObjectNode> merged = importer.mergedPackument(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, null,
PACKAGE_NAME);
PACKAGE_NAME, false);
assertFalse(merged.isPresent());
}
@@ -1,5 +1,6 @@
package io.floci.aws.services.codeartifact;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.services.codeartifact.model.CodeArtifactRepository;
import io.floci.aws.services.codeartifact.model.ExternalConnection;
import org.junit.jupiter.api.BeforeEach;
@@ -241,4 +242,14 @@ class CodeArtifactNuGetExternalConnectionImporterTest {
return Optional.of(new NuGetExternalConnectionClient.FetchedFile(content.length));
});
}
/** An upstream deleted since a search order named it has no connection to fetch through. */
@Test
void aDeletedRepositoryHasNoExternalConnection() {
when(service.describeRepository(REGION, DOMAIN, OWNER, "deleted"))
.thenThrow(new AwsException("ResourceNotFoundException", "Repository 'deleted' not found.", 404));
assertFalse(importer.hasExternalConnection(REGION, DOMAIN, OWNER, "deleted"));
assertTrue(importer.hasExternalConnection(REGION, DOMAIN, OWNER, REPOSITORY));
}
}
@@ -4,6 +4,7 @@ import io.floci.aws.core.common.AwsException;
import io.floci.aws.services.codeartifact.model.CodeArtifactRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InOrder;
import java.io.ByteArrayInputStream;
import java.nio.file.Files;
@@ -20,8 +21,10 @@ import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -50,13 +53,15 @@ class CodeArtifactNuGetUpstreamRepositoryImporterTest {
private CodeArtifactService service;
private BagetterSidecarClient bagetter;
private CodeArtifactNuGetExternalConnectionImporter externalConnectionImporter;
private CodeArtifactNuGetUpstreamRepositoryImporter importer;
@BeforeEach
void setUp() {
service = mock(CodeArtifactService.class);
bagetter = mock(BagetterSidecarClient.class);
importer = new CodeArtifactNuGetUpstreamRepositoryImporter(service, bagetter);
externalConnectionImporter = mock(CodeArtifactNuGetExternalConnectionImporter.class);
importer = new CodeArtifactNuGetUpstreamRepositoryImporter(service, bagetter, externalConnectionImporter);
CodeArtifactRepository repository = new CodeArtifactRepository();
repository.setUpstreams(List.of(UPSTREAM));
@@ -94,6 +99,64 @@ class CodeArtifactNuGetUpstreamRepositoryImporterTest {
any())).thenReturn(new CodeArtifactService.AssetRetentionAttemptResult(true, true));
}
/**
* A version no upstream holds is fetched through an upstream's own external connection, retained
* there first, then retained here from that upstream.
*/
@Test
void aVersionNoUpstreamHoldsIsFetchedThroughAnUpstreamsExternalConnection() {
when(bagetter.packageVersionExists(UPSTREAM_REPO_ID, DOMAIN, UPSTREAM, null, PACKAGE_ID, VERSION))
.thenReturn(false);
when(externalConnectionImporter.hasExternalConnection(REGION, DOMAIN, OWNER, UPSTREAM)).thenReturn(true);
when(externalConnectionImporter.tryImport(REGION, DOMAIN, OWNER, UPSTREAM, UPSTREAM_REPO_ID, PACKAGE_ID,
VERSION)).thenReturn(true);
assertTrue(importer.tryImport(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, PACKAGE_ID, VERSION));
// The fences here are read before the fetch through the upstream's connection, so a deletion
// here during that fetch moves them away from the snapshot.
InOrder order = inOrder(service, externalConnectionImporter);
order.verify(service).currentContainerBackedPackageFence(REGION, DOMAIN, OWNER, REPOSITORY, "nuget", null,
PACKAGE_ID);
order.verify(service).currentContainerBackedVersionFence(REGION, DOMAIN, OWNER, REPOSITORY, "nuget", null,
PACKAGE_ID, VERSION);
order.verify(externalConnectionImporter).tryImport(REGION, DOMAIN, OWNER, UPSTREAM, UPSTREAM_REPO_ID,
PACKAGE_ID, VERSION);
order.verify(service).claimRetainedVersionOrigin(REGION, DOMAIN, OWNER, REPOSITORY, "nuget", null,
PACKAGE_ID, VERSION, UPSTREAM, RetentionOriginKind.UPSTREAM_REPOSITORY, 42L);
// Leased once by the walk and once for the fetch through its connection, each released.
verify(service, times(2)).resolveContainerIdAndBeginUse("nuget", REGION, DOMAIN, OWNER, UPSTREAM, bagetter);
verify(bagetter, times(2)).endUse(UPSTREAM_REPO_ID);
}
/**
* The upstream's connection answers for a version the upstream already holds, Archived: the walk
* over what the upstreams hold skipped it, and the fetch through the connection skips it too.
*/
@Test
void aVersionArchivedAtTheUpstreamIsNotCopiedThroughItsExternalConnection() {
doThrow(new AwsException("ResourceNotFoundException", "archived", 404)).when(service)
.requireContainerBackedVersionDownloadable(OWNER, REGION, DOMAIN, UPSTREAM, "nuget", null, PACKAGE_ID,
VERSION, UPSTREAM_REPO_ID);
when(externalConnectionImporter.hasExternalConnection(REGION, DOMAIN, OWNER, UPSTREAM)).thenReturn(true);
when(externalConnectionImporter.tryImport(REGION, DOMAIN, OWNER, UPSTREAM, UPSTREAM_REPO_ID, PACKAGE_ID,
VERSION)).thenReturn(true);
assertFalse(importer.tryImport(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, PACKAGE_ID, VERSION));
verify(service, never()).claimRetainedVersionOrigin(anyString(), anyString(), anyString(), anyString(),
anyString(), any(), anyString(), anyString(), anyString(), any(), anyLong());
verify(bagetter, times(2)).endUse(UPSTREAM_REPO_ID);
}
@Test
void anUpstreamWithoutAnExternalConnectionIsNotAskedToFetch() {
when(bagetter.packageVersionExists(UPSTREAM_REPO_ID, DOMAIN, UPSTREAM, null, PACKAGE_ID, VERSION))
.thenReturn(false);
assertFalse(importer.tryImport(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, PACKAGE_ID, VERSION));
verify(externalConnectionImporter, never()).tryImport(anyString(), anyString(), anyString(), anyString(),
anyString(), anyString(), anyString());
}
@Test
void importsFromTheDirectUpstreamWhenNotFoundLocally() {
boolean imported = importer.tryImport(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, PACKAGE_ID, VERSION);
@@ -370,6 +370,16 @@ class CodeArtifactPypiExternalConnectionImporterTest {
assertEquals(List.of(), additional);
}
/** A downstream repository listing these files through its upstreams leaves out what it refuses. */
@Test
void additionalExternalFilenamesLeavesOutAVersionTheCallerRefuses() {
List<CodeArtifactPypiExternalConnectionImporter.ExternalIndexFile> additional =
importer.additionalExternalFilenames(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, PACKAGE_NAME,
Set.of(), version -> !VERSION.equals(version));
assertEquals(List.of(), additional);
}
@Test
void additionalExternalFilenamesReturnsEmptyWhenNoConnectionIsConfigured() {
CodeArtifactRepository repository = new CodeArtifactRepository();
@@ -617,4 +627,14 @@ class CodeArtifactPypiExternalConnectionImporterTest {
}
return toBytes(root);
}
/** An upstream deleted since a search order named it has no connection to fetch through. */
@Test
void aDeletedRepositoryHasNoExternalConnection() {
when(service.describeRepository(REGION, DOMAIN, OWNER, "deleted"))
.thenThrow(new AwsException("ResourceNotFoundException", "Repository 'deleted' not found.", 404));
assertFalse(importer.hasExternalConnection(REGION, DOMAIN, OWNER, "deleted"));
assertTrue(importer.hasExternalConnection(REGION, DOMAIN, OWNER, REPOSITORY));
}
}
@@ -4,6 +4,8 @@ import io.floci.aws.core.common.AwsException;
import io.floci.aws.services.codeartifact.model.CodeArtifactRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InOrder;
import java.io.ByteArrayInputStream;
import java.nio.file.Files;
@@ -12,7 +14,9 @@ import java.time.Instant;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
@@ -21,8 +25,10 @@ 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;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -50,13 +56,15 @@ class CodeArtifactPypiUpstreamRepositoryImporterTest {
private CodeArtifactService service;
private PypiserverSidecarClient pypiserver;
private CodeArtifactPypiExternalConnectionImporter externalConnectionImporter;
private CodeArtifactPypiUpstreamRepositoryImporter importer;
@BeforeEach
void setUp() {
service = mock(CodeArtifactService.class);
pypiserver = mock(PypiserverSidecarClient.class);
importer = new CodeArtifactPypiUpstreamRepositoryImporter(service, pypiserver);
externalConnectionImporter = mock(CodeArtifactPypiExternalConnectionImporter.class);
importer = new CodeArtifactPypiUpstreamRepositoryImporter(service, pypiserver, externalConnectionImporter);
CodeArtifactRepository repository = new CodeArtifactRepository();
repository.setUpstreams(List.of(UPSTREAM));
@@ -97,6 +105,100 @@ class CodeArtifactPypiUpstreamRepositoryImporterTest {
any())).thenReturn(new CodeArtifactService.AssetRetentionAttemptResult(true, true));
}
/**
* A file no upstream holds is fetched through an upstream's own external connection, retained
* there first, then retained here from that upstream.
*/
@Test
void aFileNoUpstreamHoldsIsFetchedThroughAnUpstreamsExternalConnection() {
// Nothing listed at the upstream until its external connection has fetched the version.
when(pypiserver.listPackageVersions(UPSTREAM_REPO_ID, DOMAIN, UPSTREAM, null, PACKAGE_NAME))
.thenReturn(List.of())
.thenReturn(List.of(new RepositorySidecarManager.PackageVersionTimestamp(VERSION,
Instant.ofEpochSecond(1000))));
when(externalConnectionImporter.hasExternalConnection(REGION, DOMAIN, OWNER, UPSTREAM)).thenReturn(true);
when(externalConnectionImporter.tryImportAsset(REGION, DOMAIN, OWNER, UPSTREAM, UPSTREAM_REPO_ID, FILENAME))
.thenReturn(true);
assertTrue(importer.tryImportAsset(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, FILENAME));
// The package fence here is read before the fetch through the upstream's connection, so a
// DeletePackage here during that fetch moves it away from the snapshot.
InOrder order = inOrder(service, externalConnectionImporter);
order.verify(service).currentContainerBackedPackageFence(REGION, DOMAIN, OWNER, REPOSITORY, "pypi", null,
PACKAGE_NAME);
order.verify(externalConnectionImporter).tryImportAsset(REGION, DOMAIN, OWNER, UPSTREAM, UPSTREAM_REPO_ID,
FILENAME);
order.verify(service).claimRetainedVersionOrigin(REGION, DOMAIN, OWNER, REPOSITORY, "pypi", null,
PACKAGE_NAME, VERSION, UPSTREAM, RetentionOriginKind.UPSTREAM_REPOSITORY, 42L);
// Leased once by the walk and once for the fetch through its connection, each released.
verify(service, times(2)).resolveContainerIdAndBeginUse("pypi", REGION, DOMAIN, OWNER, UPSTREAM, pypiserver);
verify(pypiserver, times(2)).endUse(UPSTREAM_REPO_ID);
}
/**
* The upstream's connection answers for a version the upstream already holds, Archived: the walk
* over what the upstreams hold skipped it, and the fetch through the connection skips it too.
*/
@Test
void aVersionArchivedAtTheUpstreamIsNotCopiedThroughItsExternalConnection() {
doThrow(new AwsException("ResourceNotFoundException", "archived", 404)).when(service)
.requireContainerBackedVersionDownloadable(OWNER, REGION, DOMAIN, UPSTREAM, "pypi", null, PACKAGE_NAME,
VERSION, UPSTREAM_REPO_ID);
when(externalConnectionImporter.hasExternalConnection(REGION, DOMAIN, OWNER, UPSTREAM)).thenReturn(true);
when(externalConnectionImporter.tryImportAsset(REGION, DOMAIN, OWNER, UPSTREAM, UPSTREAM_REPO_ID, FILENAME))
.thenReturn(true);
assertFalse(importer.tryImportAsset(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, FILENAME));
verify(service, never()).claimRetainedVersionOrigin(anyString(), anyString(), anyString(), anyString(),
anyString(), any(), anyString(), anyString(), anyString(), any(), anyLong());
verify(pypiserver, times(2)).endUse(UPSTREAM_REPO_ID);
}
@Test
void anUpstreamWithoutAnExternalConnectionIsNotAskedToFetch() {
when(pypiserver.listPackageVersions(UPSTREAM_REPO_ID, DOMAIN, UPSTREAM, null, PACKAGE_NAME))
.thenReturn(List.of());
assertFalse(importer.tryImportAsset(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, FILENAME));
verify(externalConnectionImporter, never()).tryImportAsset(anyString(), anyString(), anyString(),
anyString(), anyString(), anyString());
}
@Test
void additionalInternalFilenamesOffersWhatAnUpstreamsExternalConnectionHas() {
when(externalConnectionImporter.hasExternalConnection(REGION, DOMAIN, OWNER, UPSTREAM)).thenReturn(true);
String publicOnly = "widget-2.0.0.tar.gz";
when(externalConnectionImporter.additionalExternalFilenames(eq(REGION), eq(DOMAIN), eq(OWNER), eq(UPSTREAM),
eq(UPSTREAM_REPO_ID), eq(PACKAGE_NAME), eq(Set.of(FILENAME)), any())).thenReturn(List.of(
new CodeArtifactPypiExternalConnectionImporter.ExternalIndexFile(publicOnly, false, null)));
List<String> offered = importer.additionalInternalFilenames(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID,
PACKAGE_NAME, Set.of()).stream()
.map(CodeArtifactPypiExternalConnectionImporter.ExternalIndexFile::filename).toList();
assertEquals(List.of(FILENAME, publicOnly), offered);
}
/**
* A version this repository marked Archived or Disposed is left out of what an upstream's external
* connection offers here, as it is left out of what the upstream itself holds.
*/
@Test
void additionalInternalFilenamesLeavesOutAVersionArchivedHereFromAnUpstreamsExternalConnection() {
when(externalConnectionImporter.hasExternalConnection(REGION, DOMAIN, OWNER, UPSTREAM)).thenReturn(true);
doThrow(new AwsException("ResourceNotFoundException", "archived", 404)).when(service)
.requireContainerBackedVersionDownloadable(OWNER, REGION, DOMAIN, REPOSITORY, "pypi", null,
PACKAGE_NAME, "2.0.0", REPO_ID);
importer.additionalInternalFilenames(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, PACKAGE_NAME, Set.of());
ArgumentCaptor<Predicate<String>> listable = ArgumentCaptor.forClass(Predicate.class);
verify(externalConnectionImporter).additionalExternalFilenames(eq(REGION), eq(DOMAIN), eq(OWNER),
eq(UPSTREAM), eq(UPSTREAM_REPO_ID), eq(PACKAGE_NAME), any(), listable.capture());
assertFalse(listable.getValue().test("2.0.0"));
assertTrue(listable.getValue().test(VERSION));
}
@Test
void importsFromTheDirectUpstreamWhenNotFoundLocally() {
boolean imported = importer.tryImportAsset(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, FILENAME);
@@ -2639,6 +2639,117 @@ class CodeArtifactServiceTest {
"repo", "maven", "com.example", "widget", "1.0"));
}
/** Retains npm {@code widget@1.0} into {@code repository} from {@code origin}, a connection or a repository. */
private void retainNpmWidget(String repository, String origin, RetentionOriginKind kind) {
long fence = service.currentContainerBackedPackageFence(REGION, "dom", null, repository, "npm", null,
"widget");
CodeArtifactService.RetainedVersionOriginClaimOutcome claim = service.claimRetainedVersionOrigin(REGION,
"dom", null, repository, "npm", null, "widget", "1.0", origin, kind, fence);
service.finishRetainedVersionOriginAttempt(REGION, "dom", null, repository, "npm", null, "widget", "1.0",
claim.episodeId(), true);
}
private CodeArtifactService.PackageVersionSummary listedNpmWidget(String repository) {
when(verdaccioClient.listPackageVersions(anyString(), eq("dom"), eq(repository), isNull(), eq("widget")))
.thenReturn(List.of(new RepositorySidecarManager.PackageVersionTimestamp("1.0",
Instant.ofEpochSecond(1000))));
return service.listPackageVersions(REGION, "dom", null, repository, "npm", null, "widget", null, null, null,
null, null).versions().get(0);
}
/**
* A version's origin names where it "originally entered" the domain: one retained through an
* upstream that had itself retained it from a repository further up names that repository, not
* the upstream it was read through.
*/
@Test
void aVersionRetainedThroughAnIntermediateUpstreamNamesTheRepositoryItWasPublishedTo() {
service.createDomain(REGION, "dom", null, Map.of());
for (String repository : List.of("origin", "mid", "repo")) {
service.createRepository(REGION, "dom", null, repository, null, null, Map.of());
}
retainNpmWidget("mid", "origin", RetentionOriginKind.UPSTREAM_REPOSITORY);
retainNpmWidget("repo", "mid", RetentionOriginKind.UPSTREAM_REPOSITORY);
CodeArtifactService.PackageVersionSummary summary = listedNpmWidget("repo");
assertEquals("INTERNAL", summary.originType());
assertEquals("origin", summary.upstreamRepositoryName());
assertEquals(null, summary.externalConnectionName());
assertEquals("origin", listedNpmWidget("mid").upstreamRepositoryName());
}
/** One an upstream fetched through its external connection entered the domain there, so it is EXTERNAL. */
@Test
void aVersionRetainedThroughAnUpstreamThatFetchedItExternallyIsExternal() {
service.createDomain(REGION, "dom", null, Map.of());
for (String repository : List.of("up", "repo")) {
service.createRepository(REGION, "dom", null, repository, null, null, Map.of());
}
retainNpmWidget("up", "public:npmjs", RetentionOriginKind.EXTERNAL_CONNECTION);
retainNpmWidget("repo", "up", RetentionOriginKind.UPSTREAM_REPOSITORY);
CodeArtifactService.PackageVersionSummary summary = listedNpmWidget("repo");
assertEquals("EXTERNAL", summary.originType());
assertEquals("public:npmjs", summary.externalConnectionName());
assertEquals(null, summary.upstreamRepositoryName());
assertEquals(Optional.of("up"), service.retainedUpstreamRepositoryOrigin(REGION, "dom", null, "repo", "npm",
null, "widget", "1.0"));
}
/**
* The upstream has written what it fetched through its connection but not yet confirmed it when
* the copy here is claimed and confirmed: the copy is still the one that entered the domain
* through that connection, and stays so once the upstream confirms.
*/
@Test
void aVersionCopiedWhileTheUpstreamsExternalFetchIsUnconfirmedIsExternal() {
service.createDomain(REGION, "dom", null, Map.of());
for (String repository : List.of("up", "repo")) {
service.createRepository(REGION, "dom", null, repository, null, null, Map.of());
}
long upFence = service.currentContainerBackedPackageFence(REGION, "dom", null, "up", "npm", null, "widget");
CodeArtifactService.RetainedVersionOriginClaimOutcome upClaim = service.claimRetainedVersionOrigin(REGION,
"dom", null, "up", "npm", null, "widget", "1.0", "public:npmjs",
RetentionOriginKind.EXTERNAL_CONNECTION, upFence);
retainNpmWidget("repo", "up", RetentionOriginKind.UPSTREAM_REPOSITORY);
service.finishRetainedVersionOriginAttempt(REGION, "dom", null, "up", "npm", null, "widget", "1.0",
upClaim.episodeId(), true);
CodeArtifactService.PackageVersionSummary summary = listedNpmWidget("repo");
assertEquals("EXTERNAL", summary.originType());
assertEquals("public:npmjs", summary.externalConnectionName());
}
/**
* The upstream's attempt gives its claim up after the copy here was claimed, the upstream's own
* copy having been published there: confirmed, the copy here names the upstream it came from.
*/
@Test
void aCopyConfirmedAfterTheUpstreamGaveItsClaimUpNamesTheUpstream() {
service.createDomain(REGION, "dom", null, Map.of());
for (String repository : List.of("up", "repo")) {
service.createRepository(REGION, "dom", null, repository, null, null, Map.of());
}
long upFence = service.currentContainerBackedPackageFence(REGION, "dom", null, "up", "npm", null, "widget");
CodeArtifactService.RetainedVersionOriginClaimOutcome upClaim = service.claimRetainedVersionOrigin(REGION,
"dom", null, "up", "npm", null, "widget", "1.0", "public:npmjs",
RetentionOriginKind.EXTERNAL_CONNECTION, upFence);
long fence = service.currentContainerBackedPackageFence(REGION, "dom", null, "repo", "npm", null, "widget");
CodeArtifactService.RetainedVersionOriginClaimOutcome claim = service.claimRetainedVersionOrigin(REGION,
"dom", null, "repo", "npm", null, "widget", "1.0", "up", RetentionOriginKind.UPSTREAM_REPOSITORY,
fence);
service.finishRetainedVersionOriginAttempt(REGION, "dom", null, "up", "npm", null, "widget", "1.0",
upClaim.episodeId(), false);
service.finishRetainedVersionOriginAttempt(REGION, "dom", null, "repo", "npm", null, "widget", "1.0",
claim.episodeId(), true);
CodeArtifactService.PackageVersionSummary summary = listedNpmWidget("repo");
assertEquals("INTERNAL", summary.originType());
assertEquals("up", summary.upstreamRepositoryName());
}
/**
* {@code originType} stays {@code "INTERNAL"} for a same-domain upstream-repository retention
* (real AWS's own {@code PackageVersionOrigin.originType} has no third value for this), with
@@ -11,24 +11,26 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* {@code resolveSearchOrder}'s own javadoc works through the exact A/B/C/D counterexample this
* test locks in: a depth-first walk would wrongly search a 2-hop match before a 1-hop sibling.
* {@code resolveSearchOrder} walks the upstream graph the way the User Guide's "Upstream repository
* priority order" example does: depth-first, in each repository's listed order.
*/
class UpstreamRepositoryResolverTest {
@Test
void directUpstreamsAreSearchedBeforeAnyOfTheirOwnTransitiveUpstreams() {
// A -> [B, C], B -> [D]. A depth-first walk reaches D (via B) before C, a direct upstream
// of A; the correct, breadth-first order is B, C (both direct), then D.
void theUserGuidesExampleIsSearchedDepthFirstInListOrder() {
// "Upstream repository priority order": my_repo -> [A, B], A -> [C, D], D -> [E] is
// searched my_repo, A, C, D, E, B.
Map<String, List<String>> upstreams = Map.of(
"A", List.of("B", "C"),
"B", List.of("D"),
"my_repo", List.of("A", "B"),
"A", List.of("C", "D"),
"B", List.of(),
"C", List.of(),
"D", List.of());
"D", List.of("E"),
"E", List.of());
List<String> order = UpstreamRepositoryResolver.resolveSearchOrder("A", upstreams::get);
List<String> order = UpstreamRepositoryResolver.resolveSearchOrder("my_repo", upstreams::get);
assertEquals(List.of("B", "C", "D"), order);
assertEquals(List.of("A", "C", "D", "E", "B"), order);
}
@Test
@@ -57,7 +59,7 @@ class UpstreamRepositoryResolverTest {
@Test
void aRepositoryReachableThroughTwoPathsAppearsExactlyOnce() {
// A -> [B, C], B -> [D], C -> [D]: D is reachable from both B and C, but must only be
// searched once, at the position its first discovery (through B) places it.
// searched once, where it is first reached, through B.
Map<String, List<String>> upstreams = Map.of(
"A", List.of("B", "C"),
"B", List.of("D"),
@@ -66,7 +68,7 @@ class UpstreamRepositoryResolverTest {
List<String> order = UpstreamRepositoryResolver.resolveSearchOrder("A", upstreams::get);
assertEquals(List.of("B", "C", "D"), order);
assertEquals(List.of("B", "D", "C"), order);
}
@Test