forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstrDockerCmdExec.java
More file actions
99 lines (81 loc) · 3.76 KB
/
Copy pathAbstrDockerCmdExec.java
File metadata and controls
99 lines (81 loc) · 3.76 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
package com.github.dockerjava.core.exec;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.dockerjava.api.model.AuthConfig;
import com.github.dockerjava.api.model.AuthConfigurations;
import com.github.dockerjava.core.DockerClientConfig;
import com.github.dockerjava.core.InvocationBuilder;
import com.github.dockerjava.core.RemoteApiVersion;
import com.github.dockerjava.core.WebTarget;
import com.google.common.io.BaseEncoding;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.Objects;
import static com.github.dockerjava.core.RemoteApiVersion.UNKNOWN_VERSION;
import static com.github.dockerjava.core.RemoteApiVersion.VERSION_1_19;
public abstract class AbstrDockerCmdExec {
private final transient DockerClientConfig dockerClientConfig;
private final transient WebTarget baseResource;
public AbstrDockerCmdExec(WebTarget baseResource, DockerClientConfig dockerClientConfig) {
this.baseResource = Objects.requireNonNull(baseResource, "baseResource was not specified");
this.dockerClientConfig = Objects.requireNonNull(dockerClientConfig, "dockerClientConfig was not specified");
}
protected WebTarget getBaseResource() {
return baseResource;
}
@CheckForNull
protected AuthConfigurations getBuildAuthConfigs() {
return dockerClientConfig.getAuthConfigurations();
}
protected String registryAuth(@Nonnull AuthConfig authConfig) {
try {
return BaseEncoding.base64Url().encode(dockerClientConfig.getObjectMapper().writeValueAsString(authConfig).getBytes());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Nonnull
protected String registryConfigs(@Nonnull AuthConfigurations authConfigs) {
try {
final String json;
final RemoteApiVersion apiVersion = dockerClientConfig.getApiVersion();
ObjectMapper objectMapper = dockerClientConfig.getObjectMapper();
if (apiVersion.equals(UNKNOWN_VERSION)) {
ObjectNode rootNode = objectMapper.valueToTree(authConfigs.getConfigs()); // all registries
final ObjectNode authNodes = objectMapper.valueToTree(authConfigs); // wrapped in "configs":{}
rootNode.setAll(authNodes); // merge 2 variants
json = rootNode.toString();
} else if (apiVersion.isGreaterOrEqual(VERSION_1_19)) {
json = objectMapper.writeValueAsString(authConfigs.getConfigs());
} else {
json = objectMapper.writeValueAsString(authConfigs);
}
return BaseEncoding.base64Url().encode(json.getBytes());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Nonnull
protected InvocationBuilder resourceWithAuthConfig(@Nonnull AuthConfig authConfig,
@Nonnull InvocationBuilder request) {
return request.header("X-Registry-Auth", registryAuth(authConfig));
}
@Nonnull
protected InvocationBuilder resourceWithOptionalAuthConfig(@CheckForNull AuthConfig authConfig,
@Nonnull InvocationBuilder request) {
if (authConfig != null) {
request = resourceWithAuthConfig(authConfig, request);
}
return request;
}
protected boolean bool(Boolean bool) {
return bool != null && bool;
}
protected WebTarget booleanQueryParam(WebTarget webTarget, String name, Boolean value) {
if (bool(value)) {
webTarget = webTarget.queryParam(name, bool(value) + "");
}
return webTarget;
}
}