Skip to content

Commit d735f70

Browse files
authored
Fixes using base image from registry that requires token authentication. (GoogleContainerTools#549)
1 parent ac29205 commit d735f70

17 files changed

Lines changed: 187 additions & 88 deletions

‎jib-core/src/integration-test/java/com/google/cloud/tools/jib/builder/BuildStepsIntegrationTest.java‎

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.cloud.tools.jib.cache.Caches;
2424
import com.google.cloud.tools.jib.frontend.ExposedPortsParser;
2525
import com.google.cloud.tools.jib.image.ImageReference;
26+
import com.google.cloud.tools.jib.image.InvalidImageReferenceException;
2627
import com.google.cloud.tools.jib.registry.LocalRegistry;
2728
import java.io.IOException;
2829
import java.net.URISyntaxException;
@@ -32,6 +33,7 @@
3233
import java.util.concurrent.ExecutionException;
3334
import org.hamcrest.CoreMatchers;
3435
import org.junit.Assert;
36+
import org.junit.Before;
3537
import org.junit.ClassRule;
3638
import org.junit.Rule;
3739
import org.junit.Test;
@@ -46,28 +48,28 @@ public class BuildStepsIntegrationTest {
4648

4749
@Rule public TemporaryFolder temporaryCacheDirectory = new TemporaryFolder();
4850

51+
private SourceFilesConfiguration sourceFilesConfiguration;
52+
53+
@Before
54+
public void setUp() throws IOException, URISyntaxException {
55+
sourceFilesConfiguration = new TestSourceFilesConfiguration();
56+
}
57+
4958
@Test
5059
public void testSteps_forBuildToDockerRegistry()
51-
throws IOException, URISyntaxException, InterruptedException, CacheMetadataCorruptedException,
52-
ExecutionException, CacheDirectoryNotOwnedException, CacheDirectoryCreationException {
53-
SourceFilesConfiguration sourceFilesConfiguration = new TestSourceFilesConfiguration();
54-
BuildConfiguration buildConfiguration =
55-
BuildConfiguration.builder(logger)
56-
.setBaseImage(ImageReference.of("gcr.io", "distroless/java", "latest"))
57-
.setTargetImage(ImageReference.of("localhost:5000", "testimage", "testtag"))
58-
.setMainClass("HelloWorld")
59-
.setJavaArguments(Collections.singletonList("An argument."))
60-
.setExposedPorts(
61-
ExposedPortsParser.parse(Arrays.asList("1000", "2000-2002/tcp", "3000/udp")))
62-
.setAllowHttp(true)
63-
.build();
64-
65-
Path cacheDirectory = temporaryCacheDirectory.newFolder().toPath();
60+
throws IOException, InterruptedException, CacheMetadataCorruptedException, ExecutionException,
61+
CacheDirectoryNotOwnedException, CacheDirectoryCreationException {
6662
BuildSteps buildImageSteps =
67-
BuildSteps.forBuildToDockerRegistry(
68-
buildConfiguration,
69-
sourceFilesConfiguration,
70-
new Caches.Initializer(cacheDirectory, false, cacheDirectory, false));
63+
getBuildSteps(
64+
BuildConfiguration.builder(logger)
65+
.setBaseImage(ImageReference.of("gcr.io", "distroless/java", "latest"))
66+
.setTargetImage(ImageReference.of("localhost:5000", "testimage", "testtag"))
67+
.setMainClass("HelloWorld")
68+
.setJavaArguments(Collections.singletonList("An argument."))
69+
.setExposedPorts(
70+
ExposedPortsParser.parse(Arrays.asList("1000", "2000-2002/tcp", "3000/udp")))
71+
.setAllowHttp(true)
72+
.build());
7173

7274
long lastTime = System.nanoTime();
7375
buildImageSteps.run();
@@ -91,11 +93,31 @@ public void testSteps_forBuildToDockerRegistry()
9193
"Hello, world. An argument.\n", new Command("docker", "run", imageReference).run());
9294
}
9395

96+
@Test
97+
public void testSteps_forBuildToDockerRegistry_dockerHubBaseImage()
98+
throws InvalidImageReferenceException, IOException, InterruptedException, ExecutionException,
99+
CacheDirectoryCreationException, CacheMetadataCorruptedException,
100+
CacheDirectoryNotOwnedException {
101+
getBuildSteps(
102+
BuildConfiguration.builder(logger)
103+
.setBaseImage(ImageReference.parse("openjdk:8-jre-alpine"))
104+
.setTargetImage(ImageReference.of("localhost:5000", "testimage", "testtag"))
105+
.setMainClass("HelloWorld")
106+
.setJavaArguments(Collections.singletonList("An argument."))
107+
.setAllowHttp(true)
108+
.build())
109+
.run();
110+
111+
String imageReference = "localhost:5000/testimage:testtag";
112+
new Command("docker", "pull", imageReference).run();
113+
Assert.assertEquals(
114+
"Hello, world. An argument.\n", new Command("docker", "run", imageReference).run());
115+
}
116+
94117
@Test
95118
public void testSteps_forBuildToDockerDaemon()
96-
throws IOException, URISyntaxException, InterruptedException, CacheMetadataCorruptedException,
97-
ExecutionException, CacheDirectoryNotOwnedException, CacheDirectoryCreationException {
98-
SourceFilesConfiguration sourceFilesConfiguration = new TestSourceFilesConfiguration();
119+
throws IOException, InterruptedException, CacheMetadataCorruptedException, ExecutionException,
120+
CacheDirectoryNotOwnedException, CacheDirectoryCreationException {
99121
BuildConfiguration buildConfiguration =
100122
BuildConfiguration.builder(logger)
101123
.setBaseImage(ImageReference.of("gcr.io", "distroless/java", "latest"))
@@ -107,13 +129,12 @@ public void testSteps_forBuildToDockerDaemon()
107129
.build();
108130

109131
Path cacheDirectory = temporaryCacheDirectory.newFolder().toPath();
110-
BuildSteps buildDockerSteps =
111-
BuildSteps.forBuildToDockerDaemon(
132+
BuildSteps.forBuildToDockerDaemon(
112133
buildConfiguration,
113134
sourceFilesConfiguration,
114-
new Caches.Initializer(cacheDirectory, false, cacheDirectory, false));
135+
new Caches.Initializer(cacheDirectory, false, cacheDirectory, false))
136+
.run();
115137

116-
buildDockerSteps.run();
117138
Assert.assertThat(
118139
new Command("docker", "inspect", "testdocker").run(),
119140
CoreMatchers.containsString(
@@ -126,4 +147,12 @@ public void testSteps_forBuildToDockerDaemon()
126147
Assert.assertEquals(
127148
"Hello, world. An argument.\n", new Command("docker", "run", "testdocker").run());
128149
}
150+
151+
private BuildSteps getBuildSteps(BuildConfiguration buildConfiguration) throws IOException {
152+
Path cacheDirectory = temporaryCacheDirectory.newFolder().toPath();
153+
return BuildSteps.forBuildToDockerRegistry(
154+
buildConfiguration,
155+
sourceFilesConfiguration,
156+
new Caches.Initializer(cacheDirectory, false, cacheDirectory, false));
157+
}
129158
}

‎jib-core/src/integration-test/java/com/google/cloud/tools/jib/registry/AuthenticationMethodRetrieverIntegrationTest.java‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.cloud.tools.jib.http.Authorization;
2020
import java.io.IOException;
21+
import org.junit.Assert;
2122
import org.junit.Test;
2223

2324
/** Integration tests for {@link AuthenticationMethodRetriever}. */
@@ -27,14 +28,15 @@ public class AuthenticationMethodRetrieverIntegrationTest {
2728
public void testGetRegistryAuthenticator()
2829
throws RegistryAuthenticationFailedException, IOException, RegistryException {
2930
RegistryClient registryClient =
30-
RegistryClient.factory("registry.hub.docker.com", "library/busybox")
31-
.newWithAuthorization(null);
31+
RegistryClient.factory("registry.hub.docker.com", "library/busybox").newRegistryClient();
3232
RegistryAuthenticator registryAuthenticator = registryClient.getRegistryAuthenticator();
33+
Assert.assertNotNull(registryAuthenticator);
3334
Authorization authorization = registryAuthenticator.authenticatePull();
3435

3536
RegistryClient authorizedRegistryClient =
3637
RegistryClient.factory("registry.hub.docker.com", "library/busybox")
37-
.newWithAuthorization(authorization);
38+
.setAuthorization(authorization)
39+
.newRegistryClient();
3840
authorizedRegistryClient.pullManifest("latest");
3941
}
4042
}

‎jib-core/src/integration-test/java/com/google/cloud/tools/jib/registry/BlobCheckerIntegrationTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class BlobCheckerIntegrationTest {
3232
@Test
3333
public void testCheck_exists() throws IOException, RegistryException {
3434
RegistryClient registryClient =
35-
RegistryClient.factory("localhost:5000", "busybox").newAllowHttp();
35+
RegistryClient.factory("localhost:5000", "busybox").setAllowHttp(true).newRegistryClient();
3636
V22ManifestTemplate manifestTemplate =
3737
registryClient.pullManifest("latest", V22ManifestTemplate.class);
3838
DescriptorDigest blobDigest = manifestTemplate.getLayers().get(0).getDigest();
@@ -43,7 +43,7 @@ public void testCheck_exists() throws IOException, RegistryException {
4343
@Test
4444
public void testCheck_doesNotExist() throws IOException, RegistryException, DigestException {
4545
RegistryClient registryClient =
46-
RegistryClient.factory("localhost:5000", "busybox").newAllowHttp();
46+
RegistryClient.factory("localhost:5000", "busybox").setAllowHttp(true).newRegistryClient();
4747
DescriptorDigest fakeBlobDigest =
4848
DescriptorDigest.fromHash(
4949
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");

‎jib-core/src/integration-test/java/com/google/cloud/tools/jib/registry/BlobPullerIntegrationTest.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class BlobPullerIntegrationTest {
4242
public void testPull() throws IOException, RegistryException {
4343
// Pulls the busybox image.
4444
RegistryClient registryClient =
45-
RegistryClient.factory("localhost:5000", "busybox").newAllowHttp();
45+
RegistryClient.factory("localhost:5000", "busybox").setAllowHttp(true).newRegistryClient();
4646
V21ManifestTemplate manifestTemplate =
4747
registryClient.pullManifest("latest", V21ManifestTemplate.class);
4848

@@ -64,7 +64,9 @@ public void testPull_unknownBlob() throws RegistryException, IOException, Digest
6464

6565
try {
6666
RegistryClient registryClient =
67-
RegistryClient.factory("localhost:5000", "busybox").newAllowHttp();
67+
RegistryClient.factory("localhost:5000", "busybox")
68+
.setAllowHttp(true)
69+
.newRegistryClient();
6870
registryClient.pullBlob(nonexistentDigest, Mockito.mock(OutputStream.class));
6971
Assert.fail("Trying to pull nonexistent blob should have errored");
7072

‎jib-core/src/integration-test/java/com/google/cloud/tools/jib/registry/BlobPusherIntegrationTest.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ public void testPush() throws DigestException, IOException, RegistryException {
3939
"52a9e4d4ba4333ce593707f98564fee1e6d898db0d3602408c0b2a6a424d357c");
4040

4141
RegistryClient registryClient =
42-
RegistryClient.factory("localhost:5000", "testimage").newAllowHttp();
42+
RegistryClient.factory("localhost:5000", "testimage")
43+
.setAllowHttp(true)
44+
.newRegistryClient();
4345
Assert.assertFalse(registryClient.pushBlob(testBlobDigest, testBlob));
4446
}
4547
}

‎jib-core/src/integration-test/java/com/google/cloud/tools/jib/registry/ManifestPullerIntegrationTest.java‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class ManifestPullerIntegrationTest {
3333
@Test
3434
public void testPull_v21() throws IOException, RegistryException {
3535
RegistryClient registryClient =
36-
RegistryClient.factory("localhost:5000", "busybox").newAllowHttp();
36+
RegistryClient.factory("localhost:5000", "busybox").setAllowHttp(true).newRegistryClient();
3737
V21ManifestTemplate manifestTemplate =
3838
registryClient.pullManifest("latest", V21ManifestTemplate.class);
3939

@@ -44,7 +44,7 @@ public void testPull_v21() throws IOException, RegistryException {
4444
@Test
4545
public void testPull_v22() throws IOException, RegistryException {
4646
RegistryClient registryClient =
47-
RegistryClient.factory("gcr.io", "distroless/java").newWithAuthorization(null);
47+
RegistryClient.factory("gcr.io", "distroless/java").newRegistryClient();
4848
ManifestTemplate manifestTemplate = registryClient.pullManifest("latest");
4949

5050
Assert.assertEquals(2, manifestTemplate.getSchemaVersion());
@@ -56,7 +56,9 @@ public void testPull_v22() throws IOException, RegistryException {
5656
public void testPull_unknownManifest() throws RegistryException, IOException {
5757
try {
5858
RegistryClient registryClient =
59-
RegistryClient.factory("localhost:5000", "busybox").newAllowHttp();
59+
RegistryClient.factory("localhost:5000", "busybox")
60+
.setAllowHttp(true)
61+
.newRegistryClient();
6062
registryClient.pullManifest("nonexistent-tag");
6163
Assert.fail("Trying to pull nonexistent image should have errored");
6264

‎jib-core/src/integration-test/java/com/google/cloud/tools/jib/registry/ManifestPusherIntegrationTest.java‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ public class ManifestPusherIntegrationTest {
3737
@Test
3838
public void testPush_missingBlobs() throws IOException, RegistryException {
3939
RegistryClient registryClient =
40-
RegistryClient.factory("gcr.io", "distroless/java").newWithAuthorization(null);
40+
RegistryClient.factory("gcr.io", "distroless/java").newRegistryClient();
4141
ManifestTemplate manifestTemplate = registryClient.pullManifest("latest");
4242

43-
registryClient = RegistryClient.factory("localhost:5000", "busybox").newAllowHttp();
43+
registryClient =
44+
RegistryClient.factory("localhost:5000", "busybox").setAllowHttp(true).newRegistryClient();
4445
try {
4546
registryClient.pushManifest((V22ManifestTemplate) manifestTemplate, "latest");
4647
Assert.fail("Pushing manifest without its BLOBs should fail");
@@ -72,7 +73,9 @@ public void testPush() throws DigestException, IOException, RegistryException {
7273

7374
// Pushes the BLOBs.
7475
RegistryClient registryClient =
75-
RegistryClient.factory("localhost:5000", "testimage").newAllowHttp();
76+
RegistryClient.factory("localhost:5000", "testimage")
77+
.setAllowHttp(true)
78+
.newRegistryClient();
7679
Assert.assertFalse(registryClient.pushBlob(testLayerBlobDigest, testLayerBlob));
7780
Assert.assertFalse(
7881
registryClient.pushBlob(

‎jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/PullAndCacheBaseImageLayerStep.java‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,13 @@ public ListenableFuture<CachedLayer> getFuture() {
6969
public CachedLayer call() throws IOException, RegistryException {
7070
try (Timer ignored =
7171
new Timer(buildConfiguration.getBuildLogger(), String.format(DESCRIPTION, layerDigest))) {
72-
RegistryClient.Factory registryClientFactory =
73-
RegistryClient.factory(
74-
buildConfiguration.getBaseImageRegistry(),
75-
buildConfiguration.getBaseImageRepository());
7672
RegistryClient registryClient =
77-
buildConfiguration.getAllowHttp()
78-
? registryClientFactory.newAllowHttp()
79-
: registryClientFactory.newWithAuthorization(pullAuthorization);
73+
RegistryClient.factory(
74+
buildConfiguration.getBaseImageRegistry(),
75+
buildConfiguration.getBaseImageRepository())
76+
.setAllowHttp(buildConfiguration.getAllowHttp())
77+
.setAuthorization(pullAuthorization)
78+
.newRegistryClient();
8079

8180
// Checks if the layer already exists in the cache.
8281
CachedLayer cachedLayer = new CacheReader(cache).getLayer(layerDigest);

‎jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/PullBaseImageStep.java‎

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
import com.google.cloud.tools.jib.image.json.V21ManifestTemplate;
3535
import com.google.cloud.tools.jib.image.json.V22ManifestTemplate;
3636
import com.google.cloud.tools.jib.json.JsonTemplateMapper;
37+
import com.google.cloud.tools.jib.registry.RegistryAuthenticationFailedException;
38+
import com.google.cloud.tools.jib.registry.RegistryAuthenticator;
39+
import com.google.cloud.tools.jib.registry.RegistryAuthenticators;
3740
import com.google.cloud.tools.jib.registry.RegistryClient;
3841
import com.google.cloud.tools.jib.registry.RegistryException;
3942
import com.google.cloud.tools.jib.registry.RegistryUnauthorizedException;
@@ -94,7 +97,7 @@ public ListenableFuture<BaseImageWithAuthorization> getFuture() {
9497
@Override
9598
public BaseImageWithAuthorization call()
9699
throws IOException, RegistryException, LayerPropertyNotFoundException,
97-
LayerCountMismatchException, ExecutionException {
100+
LayerCountMismatchException, ExecutionException, RegistryAuthenticationFailedException {
98101
buildConfiguration
99102
.getBuildLogger()
100103
.lifecycle("Getting base image " + buildConfiguration.getBaseImageReference() + "...");
@@ -114,8 +117,31 @@ public BaseImageWithAuthorization call()
114117

115118
Authorization registryCredentials =
116119
NonBlockingSteps.get(retrieveBaseRegistryCredentialsStep);
117-
return new BaseImageWithAuthorization(
118-
pullBaseImage(registryCredentials), registryCredentials);
120+
121+
try {
122+
return new BaseImageWithAuthorization(
123+
pullBaseImage(registryCredentials), registryCredentials);
124+
125+
} catch (RegistryUnauthorizedException registryUnauthorizedException) {
126+
// The registry requires us to authenticate using the Docker Token Authentication.
127+
// See https://docs.docker.com/registry/spec/auth/token
128+
RegistryAuthenticator registryAuthenticator =
129+
RegistryAuthenticators.forOther(
130+
buildConfiguration.getBaseImageRegistry(),
131+
buildConfiguration.getBaseImageRepository());
132+
if (registryAuthenticator == null) {
133+
buildConfiguration
134+
.getBuildLogger()
135+
.error(
136+
"Failed to retrieve authentication challenge for registry that required token authentication");
137+
throw registryUnauthorizedException;
138+
}
139+
registryCredentials =
140+
registryAuthenticator.setAuthorization(registryCredentials).authenticatePull();
141+
142+
return new BaseImageWithAuthorization(
143+
pullBaseImage(registryCredentials), registryCredentials);
144+
}
119145
}
120146
}
121147
}
@@ -134,13 +160,13 @@ public BaseImageWithAuthorization call()
134160
private Image<Layer> pullBaseImage(@Nullable Authorization registryCredentials)
135161
throws IOException, RegistryException, LayerPropertyNotFoundException,
136162
LayerCountMismatchException {
137-
RegistryClient.Factory registryClientFactory =
138-
RegistryClient.factory(
139-
buildConfiguration.getBaseImageRegistry(), buildConfiguration.getBaseImageRepository());
140163
RegistryClient registryClient =
141-
buildConfiguration.getAllowHttp()
142-
? registryClientFactory.newAllowHttp()
143-
: registryClientFactory.newWithAuthorization(registryCredentials);
164+
RegistryClient.factory(
165+
buildConfiguration.getBaseImageRegistry(),
166+
buildConfiguration.getBaseImageRepository())
167+
.setAllowHttp(buildConfiguration.getAllowHttp())
168+
.setAuthorization(registryCredentials)
169+
.newRegistryClient();
144170

145171
ManifestTemplate manifestTemplate =
146172
registryClient.pullManifest(buildConfiguration.getBaseImageTag());

‎jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/PushBlobStep.java‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,13 @@ public ListenableFuture<BlobDescriptor> getFuture() {
6868
public BlobDescriptor call() throws IOException, RegistryException, ExecutionException {
6969
try (Timer timer =
7070
new Timer(buildConfiguration.getBuildLogger(), DESCRIPTION + blobDescriptor)) {
71-
RegistryClient.Factory registryClientFactory =
72-
RegistryClient.factory(
73-
buildConfiguration.getTargetImageRegistry(),
74-
buildConfiguration.getTargetImageRepository());
7571
RegistryClient registryClient =
76-
buildConfiguration.getAllowHttp()
77-
? registryClientFactory.newAllowHttp()
78-
: registryClientFactory.newWithAuthorization(
79-
NonBlockingSteps.get(authenticatePushStep));
72+
RegistryClient.factory(
73+
buildConfiguration.getTargetImageRegistry(),
74+
buildConfiguration.getTargetImageRepository())
75+
.setAllowHttp(buildConfiguration.getAllowHttp())
76+
.setAuthorization(NonBlockingSteps.get(authenticatePushStep))
77+
.newRegistryClient();
8078
registryClient.setTimer(timer);
8179

8280
if (registryClient.checkBlob(blobDescriptor.getDigest()) != null) {

0 commit comments

Comments
 (0)