Skip to content

Commit 4d7d8c4

Browse files
authored
Add more convenience methods on registry (#289)
2 parents 17adf18 + 5fdff24 commit 4d7d8c4

2 files changed

Lines changed: 70 additions & 17 deletions

File tree

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,15 @@ public Builder defaults() {
694694
return this;
695695
}
696696

697+
/**
698+
* Return a new builder with default authentication using existing host auth and registry url
699+
* @param registry The registry URL (ex: localhost:5000)
700+
* @return The builder
701+
*/
702+
public Builder defaults(String registry) {
703+
return defaults().withRegistry(registry);
704+
}
705+
697706
/**
698707
* Set username and password authentication
699708
* @param username The username
@@ -705,6 +714,17 @@ public Builder defaults(String username, String password) {
705714
return this;
706715
}
707716

717+
/**
718+
* Set username and password authentication
719+
* @param registry The registry URL (ex: localhost:5000)
720+
* @param username The username
721+
* @param password The password
722+
* @return The builder
723+
*/
724+
public Builder defaults(String registry, String username, String password) {
725+
return defaults(username, password).withRegistry(registry);
726+
}
727+
708728
/**
709729
* Set insecure communication and no authentification
710730
* @return The builder
@@ -716,6 +736,26 @@ public Builder insecure() {
716736
return this;
717737
}
718738

739+
/**
740+
* Set insecure communication and no authentification
741+
* @param registry The registry (ex: localhost:5000)
742+
* @return The builder
743+
*/
744+
public Builder insecure(String registry) {
745+
return insecure().withRegistry(registry);
746+
}
747+
748+
/**
749+
* Return a new insecure builder with username and password authentication
750+
* @param registry The registry (ex: localhost:5000)
751+
* @param username The username
752+
* @param password The password
753+
* @return The builder
754+
*/
755+
public Builder insecure(String registry, String username, String password) {
756+
return insecure().defaults(registry, username, password);
757+
}
758+
719759
/**
720760
* Set the registry URL
721761
* @param registry The registry URL

src/test/java/land/oras/RegistryTest.java

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,8 @@ void before() {
7070
void shouldListRepositories() {
7171

7272
// Setup
73-
Registry registry = Registry.Builder.builder()
74-
.defaults("myuser", "mypass")
75-
.withRegistry(this.registry.getRegistry())
76-
.withInsecure(true)
73+
Registry registry = Registry.builder()
74+
.insecure(this.registry.getRegistry(), "myuser", "mypass")
7775
.build();
7876

7977
// Test
@@ -83,13 +81,11 @@ void shouldListRepositories() {
8381

8482
@Test
8583
void shouldFailToPushBlobForInvalidDigest() {
86-
Registry registry = Registry.Builder.builder()
87-
.defaults("myuser", "mypass")
88-
.withInsecure(true)
84+
Registry registry = Registry.builder()
85+
.insecure(this.registry.getRegistry(), "myuser", "mypass")
8986
.build();
9087
ContainerRef containerRef1 = ContainerRef.parse(
91-
"%s/library/artifact-text@sha256:2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
92-
.formatted(this.registry.getRegistry()));
88+
"library/artifact-text@sha256:2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824");
9389
// Ensure the blob is deleted
9490
assertThrows(OrasException.class, () -> {
9591
registry.pushBlob(containerRef1, "invalid".getBytes());
@@ -100,11 +96,11 @@ void shouldFailToPushBlobForInvalidDigest() {
10096
void shouldPushAndGetBlobThenDeleteWithSha256() {
10197
Registry registry = Registry.Builder.builder()
10298
.defaults("myuser", "mypass")
99+
.withRegistry(this.registry.getRegistry())
103100
.withInsecure(true)
104101
.build();
105102
ContainerRef containerRef = ContainerRef.parse(
106-
"%s/library/artifact-text@sha256:2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
107-
.formatted(this.registry.getRegistry()));
103+
"library/artifact-text@sha256:2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824");
108104
Layer layer = registry.pushBlob(containerRef, "hello".getBytes());
109105
assertEquals("sha256:2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824", layer.getDigest());
110106
byte[] blob = registry.getBlob(
@@ -156,12 +152,29 @@ void shouldFailWithoutAuthentication() {
156152
Registry registry = Registry.Builder.builder().insecure().build();
157153
ContainerRef containerRef =
158154
ContainerRef.parse("%s/library/artifact-text".formatted(this.registry.getRegistry()));
159-
assertThrows(
160-
OrasException.class,
161-
() -> {
162-
registry.pushBlob(containerRef, "hello".getBytes());
163-
},
164-
"Response code: 401");
155+
assertThrows(OrasException.class, () -> {
156+
registry.pushBlob(containerRef, "hello".getBytes());
157+
});
158+
}
159+
160+
@Test
161+
void shouldFailWithoutAuthenticationAndRegistry() {
162+
Registry registry =
163+
Registry.Builder.builder().insecure(this.registry.getRegistry()).build();
164+
ContainerRef containerRef = ContainerRef.parse("library/artifact-text");
165+
assertThrows(OrasException.class, () -> {
166+
registry.pushBlob(containerRef, "hello".getBytes());
167+
});
168+
}
169+
170+
@Test
171+
void shouldFailWithSecureOnInsecure() {
172+
Registry registry =
173+
Registry.Builder.builder().defaults(this.registry.getRegistry()).build();
174+
ContainerRef containerRef = ContainerRef.parse("library/artifact-text");
175+
assertThrows(OrasException.class, () -> {
176+
registry.pushBlob(containerRef, "hello".getBytes());
177+
});
165178
}
166179

167180
@Test

0 commit comments

Comments
 (0)