Skip to content

Commit 118be67

Browse files
committed
Merge pull request #296 from docker-java/issue-288
Fix for issue #288
2 parents 3cb87ac + 12818cc commit 118be67

20 files changed

Lines changed: 179 additions & 43 deletions

src/main/java/com/github/dockerjava/core/CertificateUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static KeyStore createKeyStore(final String dockerCertPath) throws NoSuch
4949
keyStore.load(null);
5050

5151
keyStore.setKeyEntry("docker", keyPair.getPrivate(), "docker".toCharArray(),
52-
privateCertificates.toArray(new Certificate[privateCertificates.size()]) );
52+
privateCertificates.toArray(new Certificate[privateCertificates.size()]));
5353
return keyStore;
5454
}
5555

@@ -82,7 +82,8 @@ public static KeyStore createTrustStore(final String dockerCertPath) throws IOEx
8282

8383
}
8484

85-
private static List<Certificate> loadCertificates(final String dockerCertPath) throws IOException, CertificateException {
85+
private static List<Certificate> loadCertificates(final String dockerCertPath) throws IOException,
86+
CertificateException {
8687
File certificate = new File(dockerCertPath, "cert.pem");
8788
BufferedReader reader = new BufferedReader(new FileReader(certificate));
8889
PEMParser pemParser = null;

src/main/java/com/github/dockerjava/core/CompressArchiveUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public static File archiveTARFiles(File base, Iterable<File> files, String archi
1818
throws IOException {
1919
File tarFile = new File(FileUtils.getTempDirectoryPath(), archiveNameWithOutExtension + ".tar");
2020
tarFile.deleteOnExit();
21-
try(TarArchiveOutputStream tos = new TarArchiveOutputStream(new GZIPOutputStream(
22-
new BufferedOutputStream(new FileOutputStream(tarFile))))) {
21+
try (TarArchiveOutputStream tos = new TarArchiveOutputStream(new GZIPOutputStream(new BufferedOutputStream(
22+
new FileOutputStream(tarFile))))) {
2323
tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
2424
for (File file : files) {
2525
TarArchiveEntry tarEntry = new TarArchiveEntry(file);

src/main/java/com/github/dockerjava/core/DockerClientConfig.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ private static Properties overrideDockerPropertiesWithSystemProperties(Propertie
177177

178178
for (String key : new String[] { DOCKER_IO_URL_PROPERTY, DOCKER_IO_VERSION_PROPERTY,
179179
DOCKER_IO_USERNAME_PROPERTY, DOCKER_IO_PASSWORD_PROPERTY, DOCKER_IO_EMAIL_PROPERTY,
180-
DOCKER_IO_SERVER_ADDRESS_PROPERTY,
181-
DOCKER_IO_DOCKER_CERT_PATH_PROPERTY, DOCKER_IO_DOCKER_CFG_PATH_PROPERTY, }) {
180+
DOCKER_IO_SERVER_ADDRESS_PROPERTY, DOCKER_IO_DOCKER_CERT_PATH_PROPERTY,
181+
DOCKER_IO_DOCKER_CFG_PATH_PROPERTY, }) {
182182
if (systemProperties.containsKey(key)) {
183183
overriddenProperties.setProperty(key, systemProperties.getProperty(key));
184184
}
@@ -337,8 +337,7 @@ public int hashCode() {
337337
public String toString() {
338338
return "DockerClientConfig{" + "uri=" + uri + ", version='" + version + '\'' + ", username='" + username + '\''
339339
+ ", password='" + password + '\'' + ", email='" + email + '\'' + ", serverAddress='" + serverAddress
340-
+ '\'' + ", dockerCfgPath='" + dockerCfgPath + '\'' + ", sslConfig='" + sslConfig + '\''
341-
+ '}';
340+
+ '\'' + ", dockerCfgPath='" + dockerCfgPath + '\'' + ", sslConfig='" + sslConfig + '\'' + '}';
342341
}
343342

344343
public static class DockerClientConfigBuilder {
@@ -415,7 +414,7 @@ public final DockerClientConfigBuilder withSSLConfig(SSLConfig config) {
415414

416415
public DockerClientConfig build() {
417416
return new DockerClientConfig(uri, version, username, password, email, serverAddress, dockerCfgPath,
418-
sslConfig);
417+
sslConfig);
419418
}
420419
}
421420

src/main/java/com/github/dockerjava/core/async/ResultCallbackTemplate.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public void onStart(Closeable stream) {
3838

3939
@Override
4040
public void onError(Throwable throwable) {
41-
if(closed) return;
41+
if (closed)
42+
return;
4243

4344
try {
4445
LOGGER.error("Error during callback", throwable);

src/main/java/com/github/dockerjava/jaxrs/AbstrDockerCmdExec.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ protected String registryAuth(AuthConfig authConfig) {
3535

3636
protected String registryConfigs(AuthConfigurations authConfigs) {
3737
try {
38-
return Base64.encodeBase64String(new ObjectMapper().writeValueAsString(authConfigs).getBytes());
38+
String json = new ObjectMapper().writeValueAsString(authConfigs.getConfigs());
39+
return Base64.encodeBase64String(json.getBytes());
3940
} catch (IOException e) {
4041
throw new RuntimeException(e);
4142
}

src/main/java/com/github/dockerjava/jaxrs/DockerCmdExecFactoryImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ public void init(DockerClientConfig dockerClientConfig) {
124124

125125
if (clientRequestFilters != null) {
126126
for (ClientRequestFilter clientRequestFilter : clientRequestFilters) {
127-
if(clientRequestFilter != null)
128-
clientConfig.register(clientRequestFilter);
127+
if (clientRequestFilter != null)
128+
clientConfig.register(clientRequestFilter);
129129
}
130130
}
131131

src/test/java/com/github/dockerjava/client/AbstractDockerClientTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ public static void assertContainerHasVolumes(InspectContainerResponse inspectCon
186186
}
187187

188188
protected String containerLog(String containerId) throws Exception {
189-
return dockerClient.logContainerCmd(containerId).withStdOut()
190-
.exec(new LogContainerTestCallback()).awaitCompletion().toString();
189+
return dockerClient.logContainerCmd(containerId).withStdOut().exec(new LogContainerTestCallback())
190+
.awaitCompletion().toString();
191191
}
192192

193193
public static class LogContainerTestCallback extends LogContainerResultCallback {

src/test/java/com/github/dockerjava/core/DockerClientImplTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class DockerClientImplTest {
1010
@Test
1111
public void configuredInstanceAuthConfig() throws Exception {
1212
// given a config with null serverAddress
13-
DockerClientConfig dockerClientConfig = new DockerClientConfig(null, null, "", "", "", null, null,null);
13+
DockerClientConfig dockerClientConfig = new DockerClientConfig(null, null, "", "", "", null, null, null);
1414
DockerClientImpl dockerClient = DockerClientImpl.getInstance(dockerClientConfig);
1515

1616
// when we get the auth config

src/test/java/com/github/dockerjava/core/command/AttachContainerCmdImplTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ public void onNext(Frame frame) {
9494
};
9595
};
9696

97-
dockerClient.attachContainerCmd(container.getId()).withStdErr().withStdOut().withFollowStream()
98-
.exec(callback).awaitCompletion(15, TimeUnit.SECONDS).close();
97+
dockerClient.attachContainerCmd(container.getId()).withStdErr().withStdOut().withFollowStream().exec(callback)
98+
.awaitCompletion(15, TimeUnit.SECONDS).close();
9999

100100
System.out.println("log: " + callback.toString());
101101

src/test/java/com/github/dockerjava/core/command/BuildImageCmdImplTest.java

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@
1010

1111
import java.io.File;
1212
import java.io.FileInputStream;
13-
import java.io.IOException;
1413
import java.io.InputStream;
1514
import java.lang.reflect.Method;
1615
import java.util.Collection;
1716
import java.util.UUID;
1817

1918
import org.apache.commons.io.FileUtils;
2019
import org.apache.commons.io.filefilter.TrueFileFilter;
21-
import org.apache.commons.lang.StringUtils;
2220
import org.testng.ITestResult;
2321
import org.testng.annotations.AfterMethod;
2422
import org.testng.annotations.AfterTest;
@@ -31,7 +29,11 @@
3129
import com.github.dockerjava.api.command.CreateContainerResponse;
3230
import com.github.dockerjava.api.command.InspectContainerResponse;
3331
import com.github.dockerjava.api.command.InspectImageResponse;
34-
import com.github.dockerjava.api.model.BuildResponseItem;
32+
import com.github.dockerjava.api.model.AuthConfig;
33+
import com.github.dockerjava.api.model.AuthConfigurations;
34+
import com.github.dockerjava.api.model.ExposedPort;
35+
import com.github.dockerjava.api.model.PortBinding;
36+
import com.github.dockerjava.api.model.Ports;
3537
import com.github.dockerjava.client.AbstractDockerClientTest;
3638
import com.github.dockerjava.core.CompressArchiveUtil;
3739

@@ -225,17 +227,59 @@ public void testAddAndCopySubstitution() throws Exception {
225227
String response = dockerfileBuild(baseDir);
226228
assertThat(response, containsString("testENVSubstitution successfully completed"));
227229
}
228-
230+
229231
@Test
230-
public void testBuilderPerformance() throws Exception {
231-
File baseDir = new File(Thread.currentThread().getContextClassLoader().getResource("nginx").getFile());
232+
public void testBuildFromPrivateRegistry() throws Exception {
233+
File baseDir = new File(Thread.currentThread().getContextClassLoader().getResource("privateRegistry").getFile());
232234

233235
String imageId = buildImage(baseDir);
234236

235237
InspectImageResponse inspectImageResponse = dockerClient.inspectImageCmd(imageId).exec();
236238
assertThat(inspectImageResponse, not(nullValue()));
237239
LOG.info("Image Inspect: {}", inspectImageResponse.toString());
238240

239-
assertThat(inspectImageResponse.getAuthor(), equalTo("Guillaume J. Charmes \"[email protected]\""));
241+
dockerClient.tagImageCmd(imageId, "testregistry", "2").withForce().exec();
242+
243+
// see https://github.com/docker/distribution/blob/master/docs/deploying.md#native-basic-auth
244+
CreateContainerResponse testregistry = dockerClient
245+
.createContainerCmd("testregistry:2")
246+
.withName("registry")
247+
.withPortBindings(new PortBinding(new Ports.Binding(5000), ExposedPort.tcp(5000)))
248+
.withEnv("REGISTRY_AUTH=htpasswd", "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm",
249+
"REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd", "REGISTRY_LOG_LEVEL=debug",
250+
"REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt", "REGISTRY_HTTP_TLS_KEY=/certs/domain.key")
251+
.exec();
252+
253+
dockerClient.startContainerCmd(testregistry.getId()).exec();
254+
255+
AuthConfig authConfig = new AuthConfig();
256+
257+
// credentials as configured in /auth/htpasswd
258+
authConfig.setUsername("testuser");
259+
authConfig.setPassword("testpassword");
260+
authConfig.setEmail("[email protected]");
261+
authConfig.setServerAddress("localhost:5000");
262+
263+
dockerClient.authCmd().withAuthConfig(authConfig).exec();
264+
dockerClient.tagImageCmd("busybox:latest", "localhost:5000/testuser/busybox", "latest").withForce().exec();
265+
266+
dockerClient.pushImageCmd("localhost:5000/testuser/busybox").withTag("latest").withAuthConfig(authConfig)
267+
.exec(new PushImageResultCallback()).awaitSuccess();
268+
269+
dockerClient.removeImageCmd("localhost:5000/testuser/busybox").withForce().exec();
270+
271+
baseDir = new File(Thread.currentThread().getContextClassLoader().getResource("testBuildFromPrivateRegistry")
272+
.getFile());
273+
274+
AuthConfigurations authConfigurations = new AuthConfigurations();
275+
authConfigurations.addConfig(authConfig);
276+
277+
imageId = dockerClient.buildImageCmd(baseDir).withNoCache().withBuildAuthConfigs(authConfigurations)
278+
.exec(new BuildImageResultCallback()).awaitImageId();
279+
280+
inspectImageResponse = dockerClient.inspectImageCmd(imageId).exec();
281+
assertThat(inspectImageResponse, not(nullValue()));
282+
LOG.info("Image Inspect: {}", inspectImageResponse.toString());
283+
240284
}
241285
}

0 commit comments

Comments
 (0)