com.spotify.docker.client.DockerCertificates Java Examples

The following examples show how to use com.spotify.docker.client.DockerCertificates. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DockerClientFactory.java    From docker-swarm-elastic-agent-plugin with Apache License 2.0 6 votes vote down vote up
private static void setupCerts(PluginSettings pluginSettings, DefaultDockerClient.Builder builder) throws IOException, DockerCertificateException {
    if (isBlank(pluginSettings.getDockerCACert()) || isBlank(pluginSettings.getDockerClientCert()) || isBlank(pluginSettings.getDockerClientKey())) {
        LOG.warn("Missing docker certificates, will attempt to connect without certificates");
        return;
    }

    Path certificateDir = Files.createTempDirectory(UUID.randomUUID().toString());
    File tempDirectory = certificateDir.toFile();

    try {
        FileUtils.writeStringToFile(new File(tempDirectory, DockerCertificates.DEFAULT_CA_CERT_NAME), pluginSettings.getDockerCACert(), StandardCharsets.UTF_8);
        FileUtils.writeStringToFile(new File(tempDirectory, DockerCertificates.DEFAULT_CLIENT_CERT_NAME), pluginSettings.getDockerClientCert(), StandardCharsets.UTF_8);
        FileUtils.writeStringToFile(new File(tempDirectory, DockerCertificates.DEFAULT_CLIENT_KEY_NAME), pluginSettings.getDockerClientKey(), StandardCharsets.UTF_8);
        builder.dockerCertificates(new DockerCertificates(certificateDir));
    } finally {
        FileUtils.deleteDirectory(tempDirectory);
    }
}
 
Example #2
Source File: DockerClientFactory.java    From docker-elastic-agents-plugin with Apache License 2.0 6 votes vote down vote up
private static void setupCerts(PluginSettings pluginSettings, DefaultDockerClient.Builder builder) throws IOException, DockerCertificateException {
    if (isBlank(pluginSettings.getDockerCACert()) || isBlank(pluginSettings.getDockerClientCert()) || isBlank(pluginSettings.getDockerClientKey())) {
        LOG.warn("Missing docker certificates, will attempt to connect without certificates");
        return;
    }

    Path certificateDir = Files.createTempDirectory(UUID.randomUUID().toString());
    File tempDirectory = certificateDir.toFile();

    try {
        FileUtils.writeStringToFile(new File(tempDirectory, DockerCertificates.DEFAULT_CA_CERT_NAME), pluginSettings.getDockerCACert(), StandardCharsets.UTF_8);
        FileUtils.writeStringToFile(new File(tempDirectory, DockerCertificates.DEFAULT_CLIENT_CERT_NAME), pluginSettings.getDockerClientCert(), StandardCharsets.UTF_8);
        FileUtils.writeStringToFile(new File(tempDirectory, DockerCertificates.DEFAULT_CLIENT_KEY_NAME), pluginSettings.getDockerClientKey(), StandardCharsets.UTF_8);
        builder.dockerCertificates(new DockerCertificates(certificateDir));
    } finally {
        FileUtils.deleteDirectory(tempDirectory);
    }
}
 
Example #3
Source File: DockerClientSpotify.java    From doclipser with Eclipse Public License 1.0 6 votes vote down vote up
public DockerClientSpotify() {
    messageConsole = new DockerClientMessageConsole(Constants.CONSOLE_NAME);
    dockerConfig = new DockerConfig();

    final String endpoint = dockerConfig.getUri();
    final String dockerCertPath = dockerConfig.getDockerCertPath();

    final DefaultDockerClient.Builder builder = new DefaultDockerClient.Builder();
    builder.uri(endpoint);
    builder.readTimeoutMillis(DefaultDockerClient.NO_TIMEOUT);

    if (dockerCertPath != null && !dockerCertPath.isEmpty()) {
        try {
            builder.dockerCertificates(new DockerCertificates(Paths
                    .get(dockerCertPath)));
        } catch (DockerCertificateException e) {
            messageConsole.getDockerConsoleOut().println(
                    "[ERROR] " + e.toString());
        }
    }

    dockerClient = builder.build();
}
 
Example #4
Source File: BaseTest.java    From docker-swarm-elastic-agent-plugin with Apache License 2.0 5 votes vote down vote up
protected ClusterProfileProperties createClusterProfiles() throws IOException {
    ClusterProfileProperties settings = new ClusterProfileProperties();
    settings.setMaxDockerContainers(1);
    settings.setDockerURI(builder.uri().toString());
    if (settings.getDockerURI().startsWith("https://")) {
        settings.setDockerCACert(FileUtils.readFileToString(Paths.get(getenv("DOCKER_CERT_PATH"), DockerCertificates.DEFAULT_CA_CERT_NAME).toFile(), StandardCharsets.UTF_8));
        settings.setDockerClientCert(FileUtils.readFileToString(Paths.get(getenv("DOCKER_CERT_PATH"), DockerCertificates.DEFAULT_CLIENT_CERT_NAME).toFile(), StandardCharsets.UTF_8));
        settings.setDockerClientKey(FileUtils.readFileToString(Paths.get(getenv("DOCKER_CERT_PATH"), DockerCertificates.DEFAULT_CLIENT_KEY_NAME).toFile(), StandardCharsets.UTF_8));
    }

    return settings;
}
 
Example #5
Source File: BaseTest.java    From docker-swarm-elastic-agent-plugin with Apache License 2.0 5 votes vote down vote up
protected ClusterProfileProperties createClusterProfileProperties() throws IOException {
    ClusterProfileProperties settings = new ClusterProfileProperties();

    settings.setMaxDockerContainers(1);
    settings.setDockerURI(builder.uri().toString());
    if (settings.getDockerURI().startsWith("https://")) {
        settings.setDockerCACert(FileUtils.readFileToString(Paths.get(getenv("DOCKER_CERT_PATH"), DockerCertificates.DEFAULT_CA_CERT_NAME).toFile(), StandardCharsets.UTF_8));
        settings.setDockerClientCert(FileUtils.readFileToString(Paths.get(getenv("DOCKER_CERT_PATH"), DockerCertificates.DEFAULT_CLIENT_CERT_NAME).toFile(), StandardCharsets.UTF_8));
        settings.setDockerClientKey(FileUtils.readFileToString(Paths.get(getenv("DOCKER_CERT_PATH"), DockerCertificates.DEFAULT_CLIENT_KEY_NAME).toFile(), StandardCharsets.UTF_8));
    }

    return settings;
}
 
Example #6
Source File: BaseTest.java    From docker-elastic-agents-plugin with Apache License 2.0 5 votes vote down vote up
protected ClusterProfileProperties createClusterProfiles() throws IOException {
    ClusterProfileProperties settings = new ClusterProfileProperties();

    settings.setMaxDockerContainers(1);
    settings.setDockerURI(builder.uri().toString());
    if (settings.getDockerURI().startsWith("https://")) {
        settings.setDockerCACert(FileUtils.readFileToString(Paths.get(getenv("DOCKER_CERT_PATH"), DockerCertificates.DEFAULT_CA_CERT_NAME).toFile(), StandardCharsets.UTF_8));
        settings.setDockerClientCert(FileUtils.readFileToString(Paths.get(getenv("DOCKER_CERT_PATH"), DockerCertificates.DEFAULT_CLIENT_CERT_NAME).toFile(), StandardCharsets.UTF_8));
        settings.setDockerClientKey(FileUtils.readFileToString(Paths.get(getenv("DOCKER_CERT_PATH"), DockerCertificates.DEFAULT_CLIENT_KEY_NAME).toFile(), StandardCharsets.UTF_8));
    }

    return settings;
}
 
Example #7
Source File: AbstractDockerMojo.java    From docker-maven-plugin with Apache License 2.0 5 votes vote down vote up
protected Optional<DockerCertificatesStore> dockerCertificates()
    throws DockerCertificateException {
  if (!isNullOrEmpty(dockerCertPath)) {
    return DockerCertificates.builder()
      .dockerCertPath(Paths.get(dockerCertPath)).build();
  } else {
    return Optional.absent();
  }
}
 
Example #8
Source File: AbstractDockerMojoTest.java    From docker-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void testDockerHostSet() throws Exception {
  ReflectionTestUtils.setField(sut, "dockerHost", DOCKER_HOST);
  ReflectionTestUtils.setField(sut, "dockerCertPath", DOCKER_CERT_PATH);

  sut.execute();

  verify(builder).uri(DOCKER_HOST);
  verify(builder).dockerCertificates(any(DockerCertificates.class));

}
 
Example #9
Source File: PollingDockerClient.java    From helios with Apache License 2.0 4 votes vote down vote up
public PollingDockerClient(final URI uri, DockerCertificates dockerCertificates) {
  super(uri, dockerCertificates);
}
 
Example #10
Source File: DockerClientFactory.java    From ice with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * This method returns the DockerClient dependent on the current OS.
 * 
 * @return
 * @throws DockerCertificateException
 * @throws IOException
 * @throws InterruptedException
 */
public DockerClient getDockerClient() throws DockerCertificateException, IOException, InterruptedException {
	DockerClient client = null;

	// If this is not Linux, then we have to find DOCKER_HOST
	if (!Platform.getOS().equals(Platform.OS_LINUX)) {

		// See if we can get the DOCKER_HOST environment variaable
		String dockerHost = System.getenv("DOCKER_HOST");
		if (dockerHost == null) {

			// If not, run a script to see if we can get it
			File script = getDockerConnectionScript();
			String[] scriptExec = null;
			if (Platform.getOS().equals(Platform.OS_MACOSX)) {
				scriptExec = new String[] { script.getAbsolutePath() };
			} else if (Platform.getOS().equals(Platform.OS_WIN32)) {
				scriptExec = new String[] { "cmd.exe", "/C", script.getAbsolutePath() };
			}

			// Execute the script to get the DOCKER vars.
			Process process = new ProcessBuilder(scriptExec).start();
			process.waitFor();
			int exitValue = process.exitValue();
			if (exitValue == 0) {

				// Read them into a Properties object
				InputStream processInputStream = process.getInputStream();
				Properties dockerSettings = new Properties();
				// Properties.load screws up windows path separators
				// so if windows, just get the string from the stream
				if (Platform.getOS().equals(Platform.OS_WIN32)) {
					String result = streamToString(processInputStream).trim();
					String[] dockerEnvs = result.split(System.lineSeparator());
					for (String s : dockerEnvs) {
						String[] env = s.split("=");
						dockerSettings.put(env[0], env[1]);
					}
				} else {
					dockerSettings.load(processInputStream);
				}
				
				// Create the Builder object that wil build the DockerClient
				Builder builder = new Builder();

				// Get the DOCKER_HOST and CERT_PATH vars
				String endpoint = dockerSettings.getProperty("DOCKER_HOST");
				Path dockerCertPath = Paths.get(dockerSettings.getProperty("DOCKER_CERT_PATH"));

				System.out.println("DOCKERHOST: " + endpoint);
				System.out.println("DOCKER CERT PATH: " + dockerSettings.getProperty("DOCKER_CERT_PATH"));
				// Set up the certificates
				DockerCertificates certs = DockerCertificates.builder().dockerCertPath(dockerCertPath).build();

				// Set the data need for the builder.
				String stripped = endpoint.replaceAll(".*://", "");
				HostAndPort hostAndPort = HostAndPort.fromString(stripped);
				String hostText = hostAndPort.getHostText();
				String scheme = certs != null ? "https" : "http";
				int port = hostAndPort.getPortOrDefault(2375);
				String address = hostText;
				builder.uri(scheme + "://" + address + ":" + port);
				if (certs != null) {
					builder.dockerCertificates(certs);
				}

				// Build the Dockerclient!
				client = builder.build();

			} else {
				// log what happened if the process did not end as expected
				// an exit value of 1 should indicate no connection found
				InputStream processErrorStream = process.getErrorStream();
				String errorMessage = streamToString(processErrorStream);
				logger.error("Error in getting DOCKER variables: " + errorMessage);
			}
		} else {
			client = DefaultDockerClient.fromEnv().build();
		}
	} else {
		// It was equal to Linux, so just use the default stuff.
		client = DefaultDockerClient.fromEnv().build();
	}

	return client;
}