Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/main/java/land/oras/Registry.java
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,16 @@ public void pullArtifact(ContainerRef containerRef, Path path, boolean overwrite
// Only collect layer that are files
String contentType = getContentType(ref);
List<Layer> layers = collectLayers(ref, contentType, false);
if (layers.isEmpty()) {
if (layers.isEmpty()
|| layers.stream().noneMatch(layer -> layer.getAnnotations().containsKey(Const.ANNOTATION_TITLE))) {
LOG.info("Skipped pulling layers without file name in '{}'", Const.ANNOTATION_TITLE);
return;
}
for (Layer layer : layers) {
if (!layer.getAnnotations().containsKey(Const.ANNOTATION_TITLE)) {
LOG.info("Skipped pulling layer without file name in '{}'", Const.ANNOTATION_TITLE);
continue;
}
try (InputStream is = fetchBlob(ref.withDigest(layer.getDigest()))) {
// Unpack or just copy blob
if (Boolean.parseBoolean(layer.getAnnotations().getOrDefault(Const.ANNOTATION_ORAS_UNPACK, "false"))) {
Expand All @@ -360,8 +365,7 @@ public void pullArtifact(ContainerRef containerRef, Path path, boolean overwrite
ArchiveUtils.untar(Files.newInputStream(tempArchive.getPath()), path);

} else {
Path targetPath = path.resolve(
layer.getAnnotations().getOrDefault(Const.ANNOTATION_TITLE, layer.getDigest()));
Path targetPath = path.resolve(layer.getAnnotations().get(Const.ANNOTATION_TITLE));
if (Files.exists(targetPath) && !overwrite) {
LOG.info("File already exists: {}", targetPath);
continue;
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/land/oras/HarborS3ITCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ void shouldPushJenkinsScriptArtifact() {
ContainerRef containerRef = ContainerRef.parse("demo.goharbor.io/oras/jenkins-cps:latest");
Config config = Config.empty();
Layer layer = Layer.fromData(containerRef, jenkinsfile.getBytes(StandardCharsets.UTF_8))
.withMediaType(contentMediaType);
.withMediaType(contentMediaType)
.withAnnotations(Map.of(Const.ANNOTATION_TITLE, "Jenkinsfile"));
Layer imageLayer = Layer.fromFile(image)
.withMediaType("image/png")
.withAnnotations(Map.of("io.goharbor.artifact.v1alpha1.icon", ""));
Expand Down
56 changes: 56 additions & 0 deletions src/test/java/land/oras/RegistryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,62 @@ void shouldPushManifestWithRegistryUrl() {
});
}

@Test
void pullArtifactWithoutLayer() {
Registry registry = Registry.Builder.builder()
.defaults("myuser", "mypass")
.withInsecure(true)
.build();
ContainerRef containerRef1 = ContainerRef.parse("%s/empty-layers".formatted(this.registry.getRegistry()));
Config emptyConfig = Config.empty();
Manifest manifest1 = Manifest.empty().withConfig(emptyConfig);
registry.pushConfig(containerRef1, emptyConfig);
registry.pushManifest(containerRef1, manifest1);
assertDoesNotThrow(() -> {
registry.pullArtifact(containerRef1, artifactDir, true);
});
}

@Test
void pullArtifactShouldPullLayerWithTitleOnly() {
Registry registry = Registry.Builder.builder()
.defaults("myuser", "mypass")
.withInsecure(true)
.build();
ContainerRef containerRef1 = ContainerRef.parse("%s/empty-layers-title".formatted(this.registry.getRegistry()));
Config emptyConfig = Config.empty();
Manifest manifest1 = Manifest.empty().withConfig(emptyConfig);
Layer layer = registry.pushBlob(containerRef1, "hello".getBytes(StandardCharsets.UTF_8))
.withAnnotations(Map.of(Const.ANNOTATION_TITLE, "file.txt"));
Layer layerWithoutTitle = registry.pushBlob(containerRef1, "hello".getBytes(StandardCharsets.UTF_8));
manifest1 = manifest1.withLayers(List.of(layer, layerWithoutTitle));
registry.pushConfig(containerRef1, emptyConfig);
registry.pushManifest(containerRef1, manifest1);
assertDoesNotThrow(() -> {
registry.pullArtifact(containerRef1, artifactDir, true);
});
}

@Test
void pullArtifactShouldPullLayerWithNoTitle() {
Registry registry = Registry.Builder.builder()
.defaults("myuser", "mypass")
.withInsecure(true)
.build();
ContainerRef containerRef1 = ContainerRef.parse("%s/no-layers-title".formatted(this.registry.getRegistry()));
Config emptyConfig = Config.empty();
Manifest manifest1 = Manifest.empty().withConfig(emptyConfig);
Layer layer = registry.pushBlob(containerRef1, "hello".getBytes(StandardCharsets.UTF_8))
.withAnnotations(Map.of());
Layer layerWithoutTitle = registry.pushBlob(containerRef1, "hello".getBytes(StandardCharsets.UTF_8));
manifest1 = manifest1.withLayers(List.of(layer, layerWithoutTitle));
registry.pushConfig(containerRef1, emptyConfig);
registry.pushManifest(containerRef1, manifest1);
assertDoesNotThrow(() -> {
registry.pullArtifact(containerRef1, artifactDir, true);
});
}

@Test
void shouldPushComplexArtifactWithConfigMediaType() throws IOException {
Registry registry = Registry.Builder.builder()
Expand Down