Skip to content

Commit 732abe2

Browse files
authored
Passes allowHttp to RegistryAuthenticator. (GoogleContainerTools#572)
1 parent 23b898a commit 732abe2

7 files changed

Lines changed: 99 additions & 72 deletions

File tree

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,25 @@
1717
package com.google.cloud.tools.jib.registry;
1818

1919
import com.google.cloud.tools.jib.http.Authorization;
20+
import com.google.cloud.tools.jib.image.ImageReference;
21+
import com.google.cloud.tools.jib.image.InvalidImageReferenceException;
22+
import java.io.IOException;
2023
import org.junit.Assert;
2124
import org.junit.Test;
2225

2326
/** Integration tests for {@link RegistryAuthenticator}. */
2427
public class RegistryAuthenticatorIntegrationTest {
2528

2629
@Test
27-
public void testAuthenticate() throws RegistryAuthenticationFailedException {
30+
public void testAuthenticate()
31+
throws RegistryAuthenticationFailedException, InvalidImageReferenceException, IOException,
32+
RegistryException {
33+
ImageReference dockerHubImageReference = ImageReference.parse("library/busybox");
2834
RegistryAuthenticator registryAuthenticator =
29-
RegistryAuthenticators.forDockerHub("library/busybox");
35+
RegistryAuthenticator.initializer(
36+
dockerHubImageReference.getRegistry(), dockerHubImageReference.getRepository())
37+
.initialize();
38+
Assert.assertNotNull(registryAuthenticator);
3039
Authorization authorization = registryAuthenticator.authenticatePull();
3140

3241
// Checks that some token was received.

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import com.google.cloud.tools.jib.http.Authorization;
2424
import com.google.cloud.tools.jib.registry.RegistryAuthenticationFailedException;
2525
import com.google.cloud.tools.jib.registry.RegistryAuthenticator;
26-
import com.google.cloud.tools.jib.registry.RegistryAuthenticators;
2726
import com.google.cloud.tools.jib.registry.RegistryException;
2827
import com.google.common.util.concurrent.Futures;
2928
import com.google.common.util.concurrent.ListenableFuture;
@@ -79,9 +78,11 @@ public Authorization call()
7978
NonBlockingSteps.get(retrieveTargetRegistryCredentialsStep);
8079

8180
RegistryAuthenticator registryAuthenticator =
82-
RegistryAuthenticators.forOther(
83-
buildConfiguration.getTargetImageRegistry(),
84-
buildConfiguration.getTargetImageRepository());
81+
RegistryAuthenticator.initializer(
82+
buildConfiguration.getTargetImageRegistry(),
83+
buildConfiguration.getTargetImageRepository())
84+
.setAllowHttp(buildConfiguration.getAllowHttp())
85+
.initialize();
8586
if (registryAuthenticator == null) {
8687
return registryCredentials;
8788
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import com.google.cloud.tools.jib.json.JsonTemplateMapper;
3838
import com.google.cloud.tools.jib.registry.RegistryAuthenticationFailedException;
3939
import com.google.cloud.tools.jib.registry.RegistryAuthenticator;
40-
import com.google.cloud.tools.jib.registry.RegistryAuthenticators;
4140
import com.google.cloud.tools.jib.registry.RegistryClient;
4241
import com.google.cloud.tools.jib.registry.RegistryException;
4342
import com.google.cloud.tools.jib.registry.RegistryUnauthorizedException;
@@ -128,9 +127,11 @@ public BaseImageWithAuthorization call()
128127
// The registry requires us to authenticate using the Docker Token Authentication.
129128
// See https://docs.docker.com/registry/spec/auth/token
130129
RegistryAuthenticator registryAuthenticator =
131-
RegistryAuthenticators.forOther(
132-
buildConfiguration.getBaseImageRegistry(),
133-
buildConfiguration.getBaseImageRepository());
130+
RegistryAuthenticator.initializer(
131+
buildConfiguration.getBaseImageRegistry(),
132+
buildConfiguration.getBaseImageRepository())
133+
.setAllowHttp(buildConfiguration.getAllowHttp())
134+
.initialize();
134135
if (registryAuthenticator == null) {
135136
buildConfiguration
136137
.getBuildLogger()

‎jib-core/src/main/java/com/google/cloud/tools/jib/registry/RegistryAuthenticator.java‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,68 @@
4141
*/
4242
public class RegistryAuthenticator {
4343

44+
/** Initializer for {@link RegistryAuthenticator}. */
45+
public static class Initializer {
46+
47+
private final String serverUrl;
48+
private final String repository;
49+
private boolean allowHttp = false;
50+
51+
/**
52+
* Instantiates a new initializer for {@link RegistryAuthenticator}.
53+
*
54+
* @param serverUrl the server URL for the registry (for example, {@code gcr.io})
55+
* @param repository the image/repository name (also known as, namespace)
56+
*/
57+
private Initializer(String serverUrl, String repository) {
58+
this.serverUrl = serverUrl;
59+
this.repository = repository;
60+
}
61+
62+
public Initializer setAllowHttp(boolean allowHttp) {
63+
this.allowHttp = allowHttp;
64+
return this;
65+
}
66+
67+
/**
68+
* Gets a {@link RegistryAuthenticator} for a custom registry server and repository.
69+
*
70+
* @return the {@link RegistryAuthenticator} to authenticate pulls/pushes with the registry, or
71+
* {@code null} if no token authentication is necessary
72+
* @throws RegistryAuthenticationFailedException if failed to create the registry authenticator
73+
* @throws IOException if communicating with the endpoint fails
74+
* @throws RegistryException if communicating with the endpoint fails
75+
*/
76+
@Nullable
77+
public RegistryAuthenticator initialize()
78+
throws RegistryAuthenticationFailedException, IOException, RegistryException {
79+
try {
80+
return RegistryClient.factory(serverUrl, repository)
81+
.setAllowHttp(allowHttp)
82+
.newRegistryClient()
83+
.getRegistryAuthenticator();
84+
85+
} catch (MalformedURLException ex) {
86+
throw new RegistryAuthenticationFailedException(ex);
87+
88+
} catch (InsecureRegistryException ex) {
89+
// HTTP is not allowed, so just return null.
90+
return null;
91+
}
92+
}
93+
}
94+
95+
/**
96+
* Gets a new initializer for {@link RegistryAuthenticator}.
97+
*
98+
* @param serverUrl the server URL for the registry (for example, {@code gcr.io})
99+
* @param repository the image/repository name (also known as, namespace)
100+
* @return the new {@link Initializer}
101+
*/
102+
public static Initializer initializer(String serverUrl, String repository) {
103+
return new Initializer(serverUrl, repository);
104+
}
105+
44106
// TODO: Replace with a WWW-Authenticate header parser.
45107
/**
46108
* Instantiates from parsing a {@code WWW-Authenticate} header.

‎jib-core/src/main/java/com/google/cloud/tools/jib/registry/RegistryAuthenticators.java‎

Lines changed: 0 additions & 58 deletions
This file was deleted.

‎jib-gradle-plugin/CHANGELOG.md‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@ All notable changes to this project will be documented in this file.
55

66
### Added
77

8-
- Incubating feature to build `src/main/jib` as extra layer in image ([#562](https://github.com/GoogleContainerTools/jib/pull/562))
9-
108
### Changed
119

1210
### Fixed
1311

12+
- Using a private registry that does token authentication with `allowInsecureRegistries` set to `true` ([#572](https://github.com/GoogleContainerTools/jib/pull/572))
13+
14+
## 0.9.5
15+
16+
### Added
17+
18+
- Incubating feature to build `src/main/jib` as extra layer in image ([#562](https://github.com/GoogleContainerTools/jib/pull/562))
19+
1420
## 0.9.4
1521

1622
### Fixed

‎jib-maven-plugin/CHANGELOG.md‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@ All notable changes to this project will be documented in this file.
55

66
### Added
77

8-
- Incubating feature to build `src/main/jib` as extra layer in image ([#565](https://github.com/GoogleContainerTools/jib/pull/565))
9-
108
### Changed
119

1210
### Fixed
1311

12+
- Using a private registry that does token authentication with `allowInsecureRegistries` set to `true` ([#572](https://github.com/GoogleContainerTools/jib/pull/572))
13+
14+
## 0.9.5
15+
16+
### Added
17+
18+
- Incubating feature to build `src/main/jib` as extra layer in image ([#565](https://github.com/GoogleContainerTools/jib/pull/565))
19+
1420
## 0.9.4
1521

1622
### Fixed

0 commit comments

Comments
 (0)