Skip to content

Commit

Permalink
Fixed ssl configuration for java 1.6
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Linke committed Oct 24, 2014
1 parent d669f20 commit ef4e197
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 22 deletions.
23 changes: 11 additions & 12 deletions src/main/java/com/github/dockerjava/core/CertificateUtils.java
@@ -1,11 +1,10 @@
package com.github.dockerjava.core;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyStore;
Expand All @@ -30,8 +29,8 @@ public class CertificateUtils {
public static boolean verifyCertificatesExist(String dockerCertPath) {
String[] files = {"ca.pem", "cert.pem", "key.pem"};
for (String file : files) {
Path path = Paths.get(dockerCertPath, file);
boolean exists = Files.exists(path);
File path = new File(dockerCertPath, file);
boolean exists = path.exists();
if(!exists) {
return false;
}
Expand All @@ -52,8 +51,8 @@ public static KeyStore createKeyStore(final String dockerCertPath) throws NoSuch
}

public static KeyStore createTrustStore(final String dockerCertPath) throws IOException, CertificateException, KeyStoreException, NoSuchAlgorithmException {
Path caPath = Paths.get(dockerCertPath, "ca.pem");
BufferedReader reader = Files.newBufferedReader(caPath, Charset.defaultCharset());
File caPath = new File(dockerCertPath, "ca.pem");
BufferedReader reader = new BufferedReader(new FileReader(caPath));
PEMParser pemParser = null;

try {
Expand All @@ -80,8 +79,8 @@ public static KeyStore createTrustStore(final String dockerCertPath) throws IOEx
}

private static Certificate loadCertificate(final String dockerCertPath) throws IOException, CertificateException {
Path certificate = Paths.get(dockerCertPath, "cert.pem");
BufferedReader reader = Files.newBufferedReader(certificate, Charset.defaultCharset());
File certificate = new File(dockerCertPath, "cert.pem");
BufferedReader reader = new BufferedReader(new FileReader(certificate));
PEMParser pemParser = null;

try {
Expand All @@ -102,8 +101,8 @@ private static Certificate loadCertificate(final String dockerCertPath) throws I
}

private static KeyPair loadPrivateKey(final String dockerCertPath) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
Path certificate = Paths.get(dockerCertPath, "key.pem");
BufferedReader reader = Files.newBufferedReader(certificate, Charset.defaultCharset());
File certificate = new File(dockerCertPath, "key.pem");
BufferedReader reader = new BufferedReader(new FileReader(certificate));

PEMParser pemParser = null;

Expand Down
Expand Up @@ -112,17 +112,22 @@ public void init(DockerClientConfig dockerClientConfig) {

Security.addProvider(new BouncyCastleProvider());

SslConfigurator sslConfig = SslConfigurator.newInstance();

KeyStore keyStore = CertificateUtils.createKeyStore(dockerCertPath);
KeyStore trustStore = CertificateUtils.createTrustStore(dockerCertPath);


// properties acrobatics not needed for java > 1.6
String httpProtocols = System.getProperty("https.protocols");
System.setProperty("https.protocols", "TLSv1");
SslConfigurator sslConfig = SslConfigurator.newInstance(true);
if(httpProtocols != null ) System.setProperty("https.protocols", httpProtocols);

sslConfig.keyStore(keyStore);
sslConfig.keyStorePassword("docker");

sslConfig.trustStore(trustStore);

SSLContext sslContext = sslConfig.createSSLContext();


clientBuilder.sslContext(sslContext);

}
Expand Down
Expand Up @@ -23,7 +23,7 @@ public void fromString() {
}

@Test(expectedExceptions = IllegalArgumentException.class,
expectedExceptionsMessageRegExp = "No enum constant.*")
expectedExceptionsMessageRegExp = "No enum const.*")
public void fromIllegalString() {
AccessMode.valueOf("xx");
}
Expand Down
Expand Up @@ -67,6 +67,8 @@ public void testEventStreamTimeBound() throws InterruptedException, IOException
boolean zeroCount = countDownLatch.await(5, TimeUnit.SECONDS);

executorService.shutdown();


assertTrue(zeroCount, "Expected 4 events, [create, start, die, stop]");
}

Expand Down
Expand Up @@ -18,6 +18,7 @@

import com.github.dockerjava.api.DockerException;
import com.github.dockerjava.api.InternalServerErrorException;
import com.github.dockerjava.api.NotFoundException;
import com.github.dockerjava.api.command.InspectImageResponse;
import com.github.dockerjava.api.model.Info;
import com.github.dockerjava.client.AbstractDockerClientTest;
Expand Down Expand Up @@ -61,7 +62,13 @@ public void testPullImage() throws DockerException, IOException {
String testImage = "hackmann/empty";

LOG.info("Removing image: {}", testImage);
dockerClient.removeImageCmd(testImage).exec();

try {
dockerClient.removeImageCmd(testImage).exec();
} catch (NotFoundException e) {
// just ignore if not exist
}


info = dockerClient.infoCmd().exec();
LOG.info("Client info: {}", info.toString());
Expand Down
Expand Up @@ -17,6 +17,7 @@

import com.github.dockerjava.api.DockerException;
import com.github.dockerjava.api.InternalServerErrorException;
import com.github.dockerjava.api.NotFoundException;
import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.command.InspectContainerResponse;
import com.github.dockerjava.client.AbstractDockerClientTest;
Expand Down Expand Up @@ -70,12 +71,10 @@ public void testWaitContainer() throws DockerException {

@Test
public void testWaitNonExistingContainer() throws DockerException {
// docker returns InternalServerError instead of NotFound
// see https://github.com/docker/docker/issues/8107
try {
dockerClient.waitContainerCmd("non-existing").exec();
fail("expected InternalServerErrorException");
} catch (InternalServerErrorException e) {
fail("expected NotFoundException");
} catch (NotFoundException e) {
}
}
}

0 comments on commit ef4e197

Please sign in to comment.