forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultDockerClientConfig.java
More file actions
445 lines (364 loc) · 16 KB
/
Copy pathDefaultDockerClientConfig.java
File metadata and controls
445 lines (364 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package com.github.dockerjava.core;
import com.github.dockerjava.api.exception.DockerClientException;
import com.github.dockerjava.api.model.AuthConfig;
import com.github.dockerjava.api.model.AuthConfigurations;
import com.github.dockerjava.core.NameParser.HostnameReposName;
import com.github.dockerjava.core.NameParser.ReposTag;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.net.URI;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.apache.commons.lang.BooleanUtils.isTrue;
/**
* Respects some of the docker CLI options. See https://docs.docker.com/engine/reference/commandline/cli/#environment-variables
*/
public class DefaultDockerClientConfig implements Serializable, DockerClientConfig {
private static final long serialVersionUID = 1L;
public static final String DOCKER_HOST = "DOCKER_HOST";
public static final String DOCKER_TLS_VERIFY = "DOCKER_TLS_VERIFY";
public static final String DOCKER_CONFIG = "DOCKER_CONFIG";
public static final String DOCKER_CERT_PATH = "DOCKER_CERT_PATH";
public static final String API_VERSION = "api.version";
public static final String REGISTRY_USERNAME = "registry.username";
public static final String REGISTRY_PASSWORD = "registry.password";
public static final String REGISTRY_EMAIL = "registry.email";
public static final String REGISTRY_URL = "registry.url";
private static final String DOCKER_JAVA_PROPERTIES = "docker-java.properties";
private static final Set<String> CONFIG_KEYS = new HashSet<>();
static {
CONFIG_KEYS.add(DOCKER_HOST);
CONFIG_KEYS.add(DOCKER_TLS_VERIFY);
CONFIG_KEYS.add(DOCKER_CONFIG);
CONFIG_KEYS.add(DOCKER_CERT_PATH);
CONFIG_KEYS.add(API_VERSION);
CONFIG_KEYS.add(REGISTRY_USERNAME);
CONFIG_KEYS.add(REGISTRY_PASSWORD);
CONFIG_KEYS.add(REGISTRY_EMAIL);
CONFIG_KEYS.add(REGISTRY_URL);
}
private final URI dockerHost;
private final String registryUsername, registryPassword, registryEmail, registryUrl, dockerConfigPath;
private final SSLConfig sslConfig;
private final RemoteApiVersion apiVersion;
private DockerConfigFile dockerConfig = null;
DefaultDockerClientConfig(URI dockerHost, String dockerConfigPath, String apiVersion, String registryUrl,
String registryUsername, String registryPassword, String registryEmail, SSLConfig sslConfig) {
this.dockerHost = checkDockerHostScheme(dockerHost);
this.dockerConfigPath = dockerConfigPath;
this.apiVersion = RemoteApiVersion.parseConfigWithDefault(apiVersion);
this.sslConfig = sslConfig;
this.registryUsername = registryUsername;
this.registryPassword = registryPassword;
this.registryEmail = registryEmail;
this.registryUrl = registryUrl;
}
private URI checkDockerHostScheme(URI dockerHost) {
if ("tcp".equals(dockerHost.getScheme()) || "unix".equals(dockerHost.getScheme())) {
return dockerHost;
} else {
throw new DockerClientException("Unsupported protocol scheme found: '" + dockerHost
+ "'. Only 'tcp://' or 'unix://' supported.");
}
}
private static Properties loadIncludedDockerProperties(Properties systemProperties) {
try (InputStream is = DefaultDockerClientConfig.class.getResourceAsStream("/" + DOCKER_JAVA_PROPERTIES)) {
Properties p = new Properties();
p.load(is);
replaceProperties(p, systemProperties);
return p;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static void replaceProperties(Properties properties, Properties replacements) {
for (Object objectKey : properties.keySet()) {
String key = objectKey.toString();
properties.setProperty(key, replaceProperties(properties.getProperty(key), replacements));
}
}
private static String replaceProperties(String s, Properties replacements) {
for (Map.Entry<Object, Object> entry : replacements.entrySet()) {
String key = "${" + entry.getKey() + "}";
while (s.contains(key)) {
s = s.replace(key, String.valueOf(entry.getValue()));
}
}
return s;
}
/**
* Creates a new Properties object containing values overridden from ${user.home}/.docker.io.properties
*
* @param p
* The original set of properties to override
* @return A copy of the original Properties with overridden values
*/
private static Properties overrideDockerPropertiesWithSettingsFromUserHome(Properties p, Properties systemProperties) {
Properties overriddenProperties = new Properties();
overriddenProperties.putAll(p);
final File usersDockerPropertiesFile = new File(systemProperties.getProperty("user.home"),
"." + DOCKER_JAVA_PROPERTIES);
if (usersDockerPropertiesFile.isFile()) {
try (FileInputStream in = new FileInputStream(usersDockerPropertiesFile)) {
overriddenProperties.load(in);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return overriddenProperties;
}
private static Properties overrideDockerPropertiesWithEnv(Properties properties, Map<String, String> env) {
Properties overriddenProperties = new Properties();
overriddenProperties.putAll(properties);
// special case which is a sensible default
if (env.containsKey(DOCKER_HOST)) {
overriddenProperties.setProperty(DOCKER_HOST, env.get(DOCKER_HOST));
}
for (Map.Entry<String, String> envEntry : env.entrySet()) {
String envKey = envEntry.getKey();
if (CONFIG_KEYS.contains(envKey)) {
overriddenProperties.setProperty(envKey, envEntry.getValue());
}
}
return overriddenProperties;
}
/**
* Creates a new Properties object containing values overridden from the System properties
*
* @param p
* The original set of properties to override
* @return A copy of the original Properties with overridden values
*/
private static Properties overrideDockerPropertiesWithSystemProperties(Properties p, Properties systemProperties) {
Properties overriddenProperties = new Properties();
overriddenProperties.putAll(p);
for (String key : CONFIG_KEYS) {
if (systemProperties.containsKey(key)) {
overriddenProperties.setProperty(key, systemProperties.getProperty(key));
}
}
return overriddenProperties;
}
public static Builder createDefaultConfigBuilder() {
return createDefaultConfigBuilder(System.getenv(), (Properties) System.getProperties().clone());
}
/**
* Allows you to build the config without system environment interfering for more robust testing
*/
static Builder createDefaultConfigBuilder(Map<String, String> env, Properties systemProperties) {
Properties properties = loadIncludedDockerProperties(systemProperties);
properties = overrideDockerPropertiesWithSettingsFromUserHome(properties, systemProperties);
properties = overrideDockerPropertiesWithEnv(properties, env);
properties = overrideDockerPropertiesWithSystemProperties(properties, systemProperties);
return new Builder().withProperties(properties);
}
@Override
public URI getDockerHost() {
return dockerHost;
}
@Override
public RemoteApiVersion getApiVersion() {
return apiVersion;
}
@Override
public String getRegistryUsername() {
return registryUsername;
}
@Override
public String getRegistryPassword() {
return registryPassword;
}
@Override
public String getRegistryEmail() {
return registryEmail;
}
@Override
public String getRegistryUrl() {
return registryUrl;
}
@CheckForNull
public String getDockerConfigPath() {
return dockerConfigPath;
}
@Nonnull
public DockerConfigFile getDockerConfig() {
if (dockerConfig == null) {
try {
dockerConfig = DockerConfigFile.loadConfig(getObjectMapper(), getDockerConfigPath());
} catch (IOException e) {
throw new DockerClientException("Failed to parse docker configuration file", e);
}
}
return dockerConfig;
}
private AuthConfig getAuthConfig() {
AuthConfig authConfig = null;
if (getRegistryUsername() != null && getRegistryPassword() != null && getRegistryUrl() != null) {
authConfig = new AuthConfig()
.withUsername(getRegistryUsername())
.withPassword(getRegistryPassword())
.withEmail(getRegistryEmail())
.withRegistryAddress(getRegistryUrl());
}
return authConfig;
}
@Override
public AuthConfig effectiveAuthConfig(String imageName) {
AuthConfig authConfig = getAuthConfig();
if (authConfig != null) {
return authConfig;
}
DockerConfigFile dockerCfg = getDockerConfig();
ReposTag reposTag = NameParser.parseRepositoryTag(imageName);
HostnameReposName hostnameReposName = NameParser.resolveRepositoryName(reposTag.repos);
return dockerCfg.resolveAuthConfig(hostnameReposName.hostname);
}
@Override
public AuthConfigurations getAuthConfigurations() {
return getDockerConfig().getAuthConfigurations();
}
@Override
public SSLConfig getSSLConfig() {
return sslConfig;
}
@Override
public boolean equals(Object o) {
return EqualsBuilder.reflectionEquals(this, o);
}
@Override
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
public static class Builder {
private URI dockerHost;
private String apiVersion, registryUsername, registryPassword, registryEmail, registryUrl, dockerConfig,
dockerCertPath;
private Boolean dockerTlsVerify;
private SSLConfig customSslConfig = null;
/**
* This will set all fields in the builder to those contained in the Properties object. The Properties object should contain the
* following docker-java configuration keys: DOCKER_HOST, DOCKER_TLS_VERIFY, api.version, registry.username, registry.password,
* registry.email, DOCKER_CERT_PATH, and DOCKER_CONFIG.
*/
public Builder withProperties(Properties p) {
return withDockerHost(p.getProperty(DOCKER_HOST))
.withDockerTlsVerify(p.getProperty(DOCKER_TLS_VERIFY))
.withDockerConfig(p.getProperty(DOCKER_CONFIG))
.withDockerCertPath(p.getProperty(DOCKER_CERT_PATH))
.withApiVersion(p.getProperty(API_VERSION))
.withRegistryUsername(p.getProperty(REGISTRY_USERNAME))
.withRegistryPassword(p.getProperty(REGISTRY_PASSWORD))
.withRegistryEmail(p.getProperty(REGISTRY_EMAIL))
.withRegistryUrl(p.getProperty(REGISTRY_URL));
}
/**
* configure DOCKER_HOST
*/
public final Builder withDockerHost(String dockerHost) {
checkNotNull(dockerHost, "uri was not specified");
this.dockerHost = URI.create(dockerHost);
return this;
}
public final Builder withApiVersion(RemoteApiVersion apiVersion) {
this.apiVersion = apiVersion.getVersion();
return this;
}
public final Builder withApiVersion(String apiVersion) {
this.apiVersion = apiVersion;
return this;
}
public final Builder withRegistryUsername(String registryUsername) {
this.registryUsername = registryUsername;
return this;
}
public final Builder withRegistryPassword(String registryPassword) {
this.registryPassword = registryPassword;
return this;
}
public final Builder withRegistryEmail(String registryEmail) {
this.registryEmail = registryEmail;
return this;
}
public Builder withRegistryUrl(String registryUrl) {
this.registryUrl = registryUrl;
return this;
}
public final Builder withDockerCertPath(String dockerCertPath) {
this.dockerCertPath = dockerCertPath;
return this;
}
public final Builder withDockerConfig(String dockerConfig) {
this.dockerConfig = dockerConfig;
return this;
}
public final Builder withDockerTlsVerify(String dockerTlsVerify) {
if (dockerTlsVerify != null) {
String trimmed = dockerTlsVerify.trim();
this.dockerTlsVerify = "true".equalsIgnoreCase(trimmed) || "1".equals(trimmed);
} else {
this.dockerTlsVerify = false;
}
return this;
}
public final Builder withDockerTlsVerify(Boolean dockerTlsVerify) {
this.dockerTlsVerify = dockerTlsVerify;
return this;
}
/**
* Overrides the default {@link SSLConfig} that is used when calling {@link Builder#withDockerTlsVerify(java.lang.Boolean)} and
* {@link Builder#withDockerCertPath(String)}. This way it is possible to pass a custom {@link SSLConfig} to the resulting
* {@link DockerClientConfig} that may be created by other means than the local file system.
*/
public final Builder withCustomSslConfig(SSLConfig customSslConfig) {
this.customSslConfig = customSslConfig;
return this;
}
public DefaultDockerClientConfig build() {
SSLConfig sslConfig = null;
if (customSslConfig == null) {
if (isTrue(dockerTlsVerify)) {
dockerCertPath = checkDockerCertPath(dockerCertPath);
sslConfig = new LocalDirectorySSLConfig(dockerCertPath);
}
} else {
sslConfig = customSslConfig;
}
return new DefaultDockerClientConfig(dockerHost, dockerConfig, apiVersion, registryUrl, registryUsername,
registryPassword, registryEmail, sslConfig);
}
private String checkDockerCertPath(String dockerCertPath) {
if (StringUtils.isEmpty(dockerCertPath)) {
throw new DockerClientException(
"Enabled TLS verification (DOCKER_TLS_VERIFY=1) but certifate path (DOCKER_CERT_PATH) is not defined.");
}
File certPath = new File(dockerCertPath);
if (!certPath.exists()) {
throw new DockerClientException(
"Enabled TLS verification (DOCKER_TLS_VERIFY=1) but certificate path (DOCKER_CERT_PATH) '"
+ dockerCertPath + "' doesn't exist.");
} else if (!certPath.isDirectory()) {
throw new DockerClientException(
"Enabled TLS verification (DOCKER_TLS_VERIFY=1) but certificate path (DOCKER_CERT_PATH) '"
+ dockerCertPath + "' doesn't point to a directory.");
}
return dockerCertPath;
}
}
}