Skip to content

Commit aa9834d

Browse files
authored
Ensure to pass resolved target registries for pushArtifact (#566)
2 parents 13a0456 + 0d92f0c commit aa9834d

6 files changed

Lines changed: 46 additions & 9 deletions

File tree

src/main/java/land/oras/Config.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ public Config withMediaType(String mediaType) {
115115
annotations == null ? Annotations.empty() : Annotations.ofConfig(annotations));
116116
}
117117

118+
@Override
119+
protected Config withRegistry(String registry) {
120+
return (Config) super.withRegistry(registry);
121+
}
122+
118123
/**
119124
* Get the data as bytes
120125
* @return The data as bytes

src/main/java/land/oras/CopyUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void copy(
7777
LOG.debug("Manifest digest: {}", manifestDigest);
7878

7979
// Write all layer
80-
for (Layer layer : source.collectLayers(sourceRef, contentType, true)) {
80+
for (Layer layer : source.collectLayers(sourceRef.forTarget(resolveSourceRegistry), contentType, true)) {
8181
Objects.requireNonNull(layer.getDigest(), "Layer digest is required for streaming copy");
8282
Objects.requireNonNull(layer.getSize(), "Layer size is required for streaming copy");
8383
LOG.debug("Copying layer {}", layer.getDigest());

src/main/java/land/oras/Layer.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,26 @@ private Layer(
7373
this.blobPath = null;
7474
}
7575

76+
/**
77+
* Constructor that set the resolved registry
78+
* @param mediaType The media type
79+
* @param digest The digest
80+
* @param size The size
81+
* @param data The base 64 encoded data
82+
* @param registry The registry
83+
* @param annotations The annotations
84+
*/
85+
private Layer(
86+
String mediaType,
87+
String digest,
88+
@Nullable Long size,
89+
@Nullable String data,
90+
String registry,
91+
@Nullable Map<String, String> annotations) {
92+
this(mediaType, digest, size, data, annotations);
93+
this.registry = registry;
94+
}
95+
7696
/**
7797
* Constructor that set the data from a file
7898
* @param mediaType The media type
@@ -186,6 +206,7 @@ public static Layer fromData(ContainerRef containerRef, byte[] data) {
186206
containerRef.getAlgorithm().digest(data),
187207
(long) data.length,
188208
Base64.getEncoder().encodeToString(data),
209+
containerRef.getRegistry(),
189210
Map.of());
190211
}
191212

src/main/java/land/oras/Manifest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ public int getSchemaVersion() {
140140
return ArtifactType.unknown();
141141
}
142142

143+
@Override
144+
protected Manifest withRegistry(String registry) {
145+
return (Manifest) super.withRegistry(registry);
146+
}
147+
143148
/**
144149
* Get the artifact type as string for JSON serialization
145150
* @return The artifact type as string

src/main/java/land/oras/OCI.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public Layer pushBlob(T ref, Path blob) {
102102
}
103103

104104
/**
105-
* Push a blob from file
105+
* Push a blob stream. Creates a temporary file to store the blob and push the file. The temporary file will be deleted after pushing
106106
* @param ref The ref
107107
* @param input The input stream
108108
* @return The layer
@@ -234,7 +234,7 @@ protected boolean isManifestMediaType(String mediaType) {
234234
public final Config pushConfig(T ref, Config config) {
235235
Layer layer = pushBlob(ref, config.getDataBytes());
236236
LOG.debug("Config pushed: {}", layer.getDigest());
237-
return config;
237+
return config.withRegistry(layer.getRegistry());
238238
}
239239

240240
/**

src/main/java/land/oras/Registry.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.HashMap;
3232
import java.util.List;
3333
import java.util.Map;
34+
import java.util.Objects;
3435
import java.util.function.Supplier;
3536
import land.oras.auth.AuthProvider;
3637
import land.oras.auth.AuthStoreAuthenticationProvider;
@@ -392,20 +393,25 @@ public Manifest pushArtifact(
392393
manifest = manifest.withConfig(config);
393394
}
394395

395-
// Push layers
396-
List<Layer> layers = pushLayers(containerRef, false, paths);
397-
398396
// Push the config like any other blob
399397
Config pushedConfig = pushConfig(containerRef, config != null ? config : Config.empty());
398+
String resolvedRegistry = pushedConfig.getRegistry();
399+
Objects.requireNonNull(resolvedRegistry, "Pushed config must have a registry resolved");
400+
401+
// Build the resolved ref
402+
ContainerRef resolvedRef = containerRef.forRegistry(resolvedRegistry);
403+
404+
// Push layers
405+
List<Layer> layers = pushLayers(resolvedRef, false, paths);
400406

401407
// Add layer and config
402408
manifest = manifest.withLayers(layers).withConfig(pushedConfig);
403409

404410
// Push the manifest
405-
manifest = pushManifest(containerRef, manifest);
411+
manifest = pushManifest(resolvedRef, manifest);
406412
LOG.debug(
407413
"Manifest pushed to: {}",
408-
containerRef.withDigest(manifest.getDescriptor().getDigest()));
414+
resolvedRef.withDigest(manifest.getDescriptor().getDigest()));
409415
return manifest;
410416
}
411417

@@ -575,7 +581,7 @@ public Layer pushBlob(ContainerRef containerRef, byte[] data) {
575581
}
576582

577583
handleError(response);
578-
return Layer.fromData(containerRef, data);
584+
return Layer.fromData(ref, data);
579585
}
580586

581587
/**

0 commit comments

Comments
 (0)