Skip to content

Commit 0d21b43

Browse files
committed
Fix rewrite of unqualified reference
Signed-off-by: Valentin Delaye <jonesbusy@users.noreply.github.com>
1 parent 88b096f commit 0d21b43

2 files changed

Lines changed: 43 additions & 11 deletions

File tree

src/main/java/land/oras/auth/RegistriesConf.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,33 @@ public ContainerRef rewrite(ContainerRef ref) {
295295
}
296296

297297
rewrittenRefString = location + remainder;
298-
}
298+
} else {
299+
300+
// For unqualified references, we need to construct the rewritten reference properly
301+
// by replacing the registry component and preserving the namespace/repository/tag structure
302+
if (ref.isUnqualified()) {
303+
// For unqualified references, build the new reference with the location as the new registry
304+
String namespace = ref.getNamespace();
305+
String repository = ref.getRepository();
306+
String tag = ref.getTag();
307+
String digest = ref.getDigest();
308+
309+
StringBuilder rewritten = new StringBuilder(location);
310+
if (namespace != null && !namespace.isEmpty()) {
311+
rewritten.append("/").append(namespace);
312+
}
313+
rewritten.append("/").append(repository);
314+
rewritten.append(":").append(tag);
315+
if (digest != null && !digest.isEmpty()) {
316+
rewritten.append("@").append(digest);
317+
}
318+
rewrittenRefString = rewritten.toString();
319+
}
299320

300-
// Just replace the prefix with the location (e.g., docker.io/library → my-registry.com/library)
301-
else {
302-
rewrittenRefString = location + currentRefString.substring(prefix.length());
321+
// For qualified references, use the original string manipulation approach
322+
else {
323+
rewrittenRefString = location + currentRefString.substring(prefix.length());
324+
}
303325
}
304326

305327
LOG.debug(

src/test/java/land/oras/auth/RegistryConfTest.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,23 @@ void shouldRewriteContainerRef() {
186186
originalRef = ContainerRef.parse("example.com/foo/library/test:latest");
187187
rewrittenRef = conf.rewrite(originalRef);
188188
assertEquals("internal-registry-for-example.com/bar/library/test:latest", rewrittenRef.toString());
189+
}
190+
191+
@Test
192+
void shouldRewriteUnqualifiedContainerRef() {
189193

190-
// With tag only
191-
// registry = new RegistriesConf.RegistryConfig("example.com/foo:latest", "example.com/foo:othertag", null,
192-
// null);
193-
// conf = new RegistriesConf(new RegistriesConf.Config(List.of(registry)));
194-
// originalRef = ContainerRef.parse("example.com/foo:latest");
195-
// rewrittenRef = conf.rewrite(originalRef);
196-
// assertEquals("example.com/foo:othertag", rewrittenRef.toString());
194+
RegistriesConf.RegistryConfig registry =
195+
new RegistriesConf.RegistryConfig("docker.io", "internal-registry-for-example.com", null, null);
196+
RegistriesConf conf = new RegistriesConf(new RegistriesConf.Config(List.of(registry)));
197+
198+
// Without tag
199+
ContainerRef originalRef = ContainerRef.parse("alpine");
200+
ContainerRef rewrittenRef = conf.rewrite(originalRef);
201+
assertEquals("internal-registry-for-example.com/library/alpine:latest", rewrittenRef.toString());
202+
203+
// Ensure to keep tag
204+
originalRef = ContainerRef.parse("alpine:1.0.0");
205+
rewrittenRef = conf.rewrite(originalRef);
206+
assertEquals("internal-registry-for-example.com/library/alpine:1.0.0", rewrittenRef.toString());
197207
}
198208
}

0 commit comments

Comments
 (0)