Skip to content

Commit f5fc5a6

Browse files
authored
Separates BuildConfiguration image credentials between base and target. (GoogleContainerTools#248)
1 parent 6cde7e7 commit f5fc5a6

11 files changed

Lines changed: 216 additions & 328 deletions

File tree

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

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ public static class Builder {
3636

3737
// All the parameters below are set to their default values.
3838
@Nullable private ImageReference baseImageReference;
39+
@Nullable private String baseImageCredentialHelperName;
40+
@Nullable private RegistryCredentials knownBaseRegistryCredentials;
3941
@Nullable private ImageReference targetImageReference;
40-
private List<String> credentialHelperNames = new ArrayList<>();
41-
private RegistryCredentials knownRegistryCredentials = RegistryCredentials.none();
42+
@Nullable private String targetImageCredentialHelperName;
43+
@Nullable private RegistryCredentials knownTargetRegistryCredentials;
4244
@Nullable private String mainClass;
4345
private List<String> jvmFlags = new ArrayList<>();
4446
private Map<String, String> environmentMap = new HashMap<>();
@@ -60,18 +62,25 @@ public Builder setTargetImage(@Nullable ImageReference imageReference) {
6062
return this;
6163
}
6264

63-
public Builder setCredentialHelperNames(@Nullable List<String> credentialHelperNames) {
64-
if (credentialHelperNames != null) {
65-
this.credentialHelperNames = credentialHelperNames;
66-
}
65+
public Builder setBaseImageCredentialHelperName(@Nullable String credentialHelperName) {
66+
baseImageCredentialHelperName = credentialHelperName;
6767
return this;
6868
}
6969

70-
public Builder setKnownRegistryCredentials(
71-
@Nullable RegistryCredentials knownRegistryCredentials) {
72-
if (knownRegistryCredentials != null) {
73-
this.knownRegistryCredentials = knownRegistryCredentials;
74-
}
70+
public Builder setTargetImageCredentialHelperName(@Nullable String credentialHelperName) {
71+
targetImageCredentialHelperName = credentialHelperName;
72+
return this;
73+
}
74+
75+
public Builder setKnownBaseRegistryCredentials(
76+
@Nullable RegistryCredentials knownRegistryCrendentials) {
77+
knownBaseRegistryCredentials = knownRegistryCrendentials;
78+
return this;
79+
}
80+
81+
public Builder setKnownTargetRegistryCredentials(
82+
@Nullable RegistryCredentials knownRegistryCrendentials) {
83+
knownTargetRegistryCredentials = knownRegistryCrendentials;
7584
return this;
7685
}
7786

@@ -121,9 +130,11 @@ public BuildConfiguration build() {
121130
return new BuildConfiguration(
122131
buildLogger,
123132
baseImageReference,
133+
baseImageCredentialHelperName,
134+
knownBaseRegistryCredentials,
124135
targetImageReference,
125-
credentialHelperNames,
126-
knownRegistryCredentials,
136+
targetImageCredentialHelperName,
137+
knownTargetRegistryCredentials,
127138
mainClass,
128139
jvmFlags,
129140
environmentMap,
@@ -167,9 +178,11 @@ public static boolean isValidJavaClass(String className) {
167178

168179
private final BuildLogger buildLogger;
169180
private ImageReference baseImageReference;
181+
@Nullable private String baseImageCredentialHelperName;
182+
@Nullable private RegistryCredentials knownBaseRegistryCredentials;
170183
private ImageReference targetImageReference;
171-
private List<String> credentialHelperNames;
172-
private RegistryCredentials knownRegistryCredentials;
184+
@Nullable private String targetImageCredentialHelperName;
185+
@Nullable private RegistryCredentials knownTargetRegistryCredentials;
173186
private String mainClass;
174187
private List<String> jvmFlags;
175188
private Map<String, String> environmentMap;
@@ -183,18 +196,22 @@ public static Builder builder(BuildLogger buildLogger) {
183196
private BuildConfiguration(
184197
BuildLogger buildLogger,
185198
ImageReference baseImageReference,
199+
@Nullable String baseImageCredentialHelperName,
200+
@Nullable RegistryCredentials knownBaseRegistryCredentials,
186201
ImageReference targetImageReference,
187-
List<String> credentialHelperNames,
188-
RegistryCredentials knownRegistryCredentials,
202+
@Nullable String targetImageCredentialHelperName,
203+
@Nullable RegistryCredentials knownTargetRegistryCredentials,
189204
String mainClass,
190205
List<String> jvmFlags,
191206
Map<String, String> environmentMap,
192207
Class<? extends BuildableManifestTemplate> targetFormat) {
193208
this.buildLogger = buildLogger;
194209
this.baseImageReference = baseImageReference;
210+
this.baseImageCredentialHelperName = baseImageCredentialHelperName;
211+
this.knownBaseRegistryCredentials = knownBaseRegistryCredentials;
195212
this.targetImageReference = targetImageReference;
196-
this.credentialHelperNames = Collections.unmodifiableList(credentialHelperNames);
197-
this.knownRegistryCredentials = knownRegistryCredentials;
213+
this.targetImageCredentialHelperName = targetImageCredentialHelperName;
214+
this.knownTargetRegistryCredentials = knownTargetRegistryCredentials;
198215
this.mainClass = mainClass;
199216
this.jvmFlags = Collections.unmodifiableList(jvmFlags);
200217
this.environmentMap = Collections.unmodifiableMap(environmentMap);
@@ -217,6 +234,16 @@ public String getBaseImageTag() {
217234
return baseImageReference.getTag();
218235
}
219236

237+
@Nullable
238+
public String getBaseImageCredentialHelperName() {
239+
return baseImageCredentialHelperName;
240+
}
241+
242+
@Nullable
243+
public RegistryCredentials getKnownBaseRegistryCredentials() {
244+
return knownBaseRegistryCredentials;
245+
}
246+
220247
public String getTargetRegistry() {
221248
return targetImageReference.getRegistry();
222249
}
@@ -229,12 +256,14 @@ public String getTargetTag() {
229256
return targetImageReference.getTag();
230257
}
231258

232-
public RegistryCredentials getKnownRegistryCredentials() {
233-
return knownRegistryCredentials;
259+
@Nullable
260+
public String getTargetImageCredentialHelperName() {
261+
return targetImageCredentialHelperName;
234262
}
235263

236-
public List<String> getCredentialHelperNames() {
237-
return credentialHelperNames;
264+
@Nullable
265+
public RegistryCredentials getKnownTargetRegistryCredentials() {
266+
return knownTargetRegistryCredentials;
238267
}
239268

240269
public String getMainClass() {

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,17 @@ public void run()
7979
ListenableFuture<Authorization> retrieveTargetRegistryCredentialsFuture =
8080
listeningExecutorService.submit(
8181
new RetrieveRegistryCredentialsStep(
82-
buildConfiguration, buildConfiguration.getTargetRegistry()));
82+
buildConfiguration,
83+
buildConfiguration.getTargetRegistry(),
84+
buildConfiguration.getTargetImageCredentialHelperName(),
85+
buildConfiguration.getKnownTargetRegistryCredentials()));
8386
ListenableFuture<Authorization> retrieveBaseImageRegistryCredentialsFuture =
8487
listeningExecutorService.submit(
8588
new RetrieveRegistryCredentialsStep(
86-
buildConfiguration, buildConfiguration.getBaseImageRegistry()));
89+
buildConfiguration,
90+
buildConfiguration.getBaseImageRegistry(),
91+
buildConfiguration.getBaseImageCredentialHelperName(),
92+
buildConfiguration.getKnownBaseRegistryCredentials()));
8793

8894
timer2.lap("Setting up image push authentication");
8995
// Authenticates push.

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.cloud.tools.jib.registry.credentials.DockerCredentialHelperFactory;
2323
import com.google.cloud.tools.jib.registry.credentials.NonexistentDockerCredentialHelperException;
2424
import com.google.cloud.tools.jib.registry.credentials.NonexistentServerUrlDockerCredentialHelperException;
25+
import com.google.cloud.tools.jib.registry.credentials.RegistryCredentials;
2526
import com.google.common.annotations.VisibleForTesting;
2627
import com.google.common.collect.ImmutableMap;
2728
import java.io.IOException;
@@ -42,13 +43,21 @@ class RetrieveRegistryCredentialsStep implements Callable<Authorization> {
4243

4344
private final BuildConfiguration buildConfiguration;
4445
private final String registry;
46+
@Nullable private final String credentialHelperSuffix;
47+
@Nullable private final RegistryCredentials knownRegistryCredentials;
4548
private final DockerCredentialHelperFactory dockerCredentialHelperFactory;
4649
private final DockerConfigCredentialRetriever dockerConfigCredentialRetriever;
4750

48-
RetrieveRegistryCredentialsStep(BuildConfiguration buildConfiguration, String registry) {
51+
RetrieveRegistryCredentialsStep(
52+
BuildConfiguration buildConfiguration,
53+
String registry,
54+
@Nullable String credentialHelperSuffix,
55+
@Nullable RegistryCredentials knownRegistryCredentials) {
4956
this(
5057
buildConfiguration,
5158
registry,
59+
credentialHelperSuffix,
60+
knownRegistryCredentials,
5261
new DockerCredentialHelperFactory(registry),
5362
new DockerConfigCredentialRetriever(registry));
5463
}
@@ -57,10 +66,14 @@ class RetrieveRegistryCredentialsStep implements Callable<Authorization> {
5766
RetrieveRegistryCredentialsStep(
5867
BuildConfiguration buildConfiguration,
5968
String registry,
69+
@Nullable String credentialHelperSuffix,
70+
@Nullable RegistryCredentials knownRegistryCredentials,
6071
DockerCredentialHelperFactory dockerCredentialHelperFactory,
6172
DockerConfigCredentialRetriever dockerConfigCredentialRetriever) {
6273
this.buildConfiguration = buildConfiguration;
6374
this.registry = registry;
75+
this.credentialHelperSuffix = credentialHelperSuffix;
76+
this.knownRegistryCredentials = knownRegistryCredentials;
6477
this.dockerCredentialHelperFactory = dockerCredentialHelperFactory;
6578
this.dockerConfigCredentialRetriever = dockerConfigCredentialRetriever;
6679
}
@@ -69,23 +82,19 @@ class RetrieveRegistryCredentialsStep implements Callable<Authorization> {
6982
@Nullable
7083
public Authorization call() throws IOException, NonexistentDockerCredentialHelperException {
7184
try (Timer ignored =
72-
new Timer(
73-
buildConfiguration.getBuildLogger(),
74-
String.format(DESCRIPTION, buildConfiguration.getTargetRegistry()))) {
85+
new Timer(buildConfiguration.getBuildLogger(), String.format(DESCRIPTION, registry))) {
7586
// Tries to get registry credentials from Docker credential helpers.
76-
for (String credentialHelperSuffix : buildConfiguration.getCredentialHelperNames()) {
87+
if (credentialHelperSuffix != null) {
7788
Authorization authorization = retrieveFromCredentialHelper(credentialHelperSuffix);
7889
if (authorization != null) {
7990
return authorization;
8091
}
8192
}
8293

8394
// Tries to get registry credentials from known registry credentials.
84-
String credentialSource =
85-
buildConfiguration.getKnownRegistryCredentials().getCredentialSource(registry);
86-
if (credentialSource != null) {
87-
logGotCredentialsFrom(credentialSource);
88-
return buildConfiguration.getKnownRegistryCredentials().getAuthorization(registry);
95+
if (knownRegistryCredentials != null) {
96+
logGotCredentialsFrom(knownRegistryCredentials.getCredentialSource());
97+
return knownRegistryCredentials.getAuthorization();
8998
}
9099

91100
// Tries to get registry credentials from the Docker config.

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

Lines changed: 12 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -17,136 +17,32 @@
1717
package com.google.cloud.tools.jib.registry.credentials;
1818

1919
import com.google.cloud.tools.jib.http.Authorization;
20-
import java.io.IOException;
21-
import java.util.HashMap;
22-
import java.util.List;
23-
import java.util.Map;
24-
import javax.annotation.Nullable;
2520

2621
/**
27-
* Stores retrieved registry credentials.
22+
* Stores retrieved registry credentials and their source.
2823
*
2924
* <p>The credentials are referred to by the registry they are used for.
3025
*/
3126
public class RegistryCredentials {
3227

33-
private static final RegistryCredentials EMPTY = new RegistryCredentials();
34-
35-
/** Instantiates with no credentials. */
36-
public static RegistryCredentials none() {
37-
return EMPTY;
38-
}
39-
40-
/** Instantiates with credentials for a single registry. */
41-
public static RegistryCredentials of(
42-
String registry, String credentialSource, Authorization authorization) {
43-
return new RegistryCredentials().store(registry, credentialSource, authorization);
44-
}
45-
46-
/**
47-
* Retrieves credentials for {@code registries} using the credential helpers referred to by {@code
48-
* credentialHelperSuffixes}.
49-
*
50-
* <p>This obtains the registry credentials, not the <a
51-
* href="https://docs.docker.com/registry/spec/auth/token/">Docker authentication token</a>.
52-
*/
53-
public static RegistryCredentials from(
54-
List<String> credentialHelperSuffixes, List<String> registries)
55-
throws IOException, NonexistentDockerCredentialHelperException {
56-
RegistryCredentials registryCredentials = new RegistryCredentials();
57-
58-
// TODO: These can be done in parallel.
59-
for (String registry : registries) {
60-
for (String credentialHelperSuffix : credentialHelperSuffixes) {
61-
// Attempts to retrieve authorization for the registry using
62-
// docker-credential-[credentialSource].
63-
try {
64-
registryCredentials.store(
65-
registry,
66-
"docker-credential-" + credentialHelperSuffix,
67-
new DockerCredentialHelper(registry, credentialHelperSuffix).retrieve());
68-
69-
} catch (NonexistentServerUrlDockerCredentialHelperException ex) {
70-
// No authorization is found, so continues on to the next credential helper.
71-
}
72-
}
73-
}
74-
return registryCredentials;
75-
}
28+
private final Authorization authorization;
7629

7730
/**
78-
* Instantiates from a credential source and a map of registry credentials.
79-
*
80-
* @param credentialSource the source of the credentials, useful for informing users where the
81-
* credentials came from
82-
* @param registryCredentialMap a map from registries to their respective credentials
31+
* A string representation of where the credentials were retrieved from. This is useful for
32+
* letting the user know which credentials were used.
8333
*/
84-
public static RegistryCredentials from(
85-
String credentialSource, Map<String, Authorization> registryCredentialMap) {
86-
RegistryCredentials registryCredentials = new RegistryCredentials();
87-
for (Map.Entry<String, Authorization> registryCredential : registryCredentialMap.entrySet()) {
88-
registryCredentials.store(
89-
registryCredential.getKey(), credentialSource, registryCredential.getValue());
90-
}
91-
return registryCredentials;
92-
}
93-
94-
/** Pair of (source of credentials, {@link Authorization}). */
95-
private static class AuthorizationSourcePair {
34+
private final String credentialSource;
9635

97-
/**
98-
* A string representation of where the credentials were retrieved from. This is useful for
99-
* letting the user know which credentials were used.
100-
*/
101-
private final String credentialSource;
102-
103-
private final Authorization authorization;
104-
105-
private AuthorizationSourcePair(String credentialSource, Authorization authorization) {
106-
this.credentialSource = credentialSource;
107-
this.authorization = authorization;
108-
}
109-
}
110-
111-
/** Maps from registry to the credentials for that registry. */
112-
private final Map<String, AuthorizationSourcePair> credentials = new HashMap<>();
113-
114-
/** Instantiate using {@link #from}. Immutable after instantiation. */
115-
private RegistryCredentials() {};
116-
117-
/** @return {@code true} if there are credentials for {@code registry}; {@code false} otherwise */
118-
public boolean has(String registry) {
119-
return credentials.containsKey(registry);
36+
public RegistryCredentials(String credentialSource, Authorization authorization) {
37+
this.authorization = authorization;
38+
this.credentialSource = credentialSource;
12039
}
12140

122-
/**
123-
* @return the {@code Authorization} retrieved for the {@code registry}, or {@code null} if none
124-
* exists
125-
*/
126-
@Nullable
127-
public Authorization getAuthorization(String registry) {
128-
if (credentials.get(registry) == null) {
129-
return null;
130-
}
131-
return credentials.get(registry).authorization;
132-
}
133-
134-
/**
135-
* @return the name of the credential helper used to retrieve authorization for the {@code
136-
* registry}, or {@code null} if none exists
137-
*/
138-
@Nullable
139-
public String getCredentialSource(String registry) {
140-
if (credentials.get(registry) == null) {
141-
return null;
142-
}
143-
return credentials.get(registry).credentialSource;
41+
public Authorization getAuthorization() {
42+
return authorization;
14443
}
14544

146-
/** Only to be called in static initializers. */
147-
private RegistryCredentials store(
148-
String registry, String credentialSource, Authorization authorization) {
149-
credentials.put(registry, new AuthorizationSourcePair(credentialSource, authorization));
150-
return this;
45+
public String getCredentialSource() {
46+
return credentialSource;
15147
}
15248
}

0 commit comments

Comments
 (0)