Search in sources :

Example 1 with SSLConfig

use of com.github.dockerjava.core.SSLConfig in project camel by apache.

the class DockerClientFactory method getDockerClient.

/**
     * Produces a {@link DockerClient} to communicate with Docker
     */
@SuppressWarnings("resource")
public static DockerClient getDockerClient(DockerComponent dockerComponent, DockerConfiguration dockerConfiguration, Message message) throws DockerException {
    ObjectHelper.notNull(dockerConfiguration, "dockerConfiguration");
    Integer port = DockerHelper.getProperty(DockerConstants.DOCKER_PORT, dockerConfiguration, message, Integer.class, dockerConfiguration.getPort());
    String host = DockerHelper.getProperty(DockerConstants.DOCKER_HOST, dockerConfiguration, message, String.class, dockerConfiguration.getHost());
    Integer maxTotalConnections = DockerHelper.getProperty(DockerConstants.DOCKER_MAX_TOTAL_CONNECTIONS, dockerConfiguration, message, Integer.class, dockerConfiguration.getMaxTotalConnections());
    Integer maxPerRouteConnections = DockerHelper.getProperty(DockerConstants.DOCKER_MAX_PER_ROUTE_CONNECTIONS, dockerConfiguration, message, Integer.class, dockerConfiguration.getMaxPerRouteConnections());
    String username = DockerHelper.getProperty(DockerConstants.DOCKER_USERNAME, dockerConfiguration, message, String.class, dockerConfiguration.getUsername());
    String password = DockerHelper.getProperty(DockerConstants.DOCKER_PASSWORD, dockerConfiguration, message, String.class, dockerConfiguration.getPassword());
    String email = DockerHelper.getProperty(DockerConstants.DOCKER_EMAIL, dockerConfiguration, message, String.class, dockerConfiguration.getEmail());
    Integer requestTimeout = DockerHelper.getProperty(DockerConstants.DOCKER_API_REQUEST_TIMEOUT, dockerConfiguration, message, Integer.class, dockerConfiguration.getRequestTimeout());
    String serverAddress = DockerHelper.getProperty(DockerConstants.DOCKER_SERVER_ADDRESS, dockerConfiguration, message, String.class, dockerConfiguration.getServerAddress());
    String certPath = DockerHelper.getProperty(DockerConstants.DOCKER_CERT_PATH, dockerConfiguration, message, String.class, dockerConfiguration.getCertPath());
    Boolean secure = DockerHelper.getProperty(DockerConstants.DOCKER_SECURE, dockerConfiguration, message, Boolean.class, dockerConfiguration.isSecure());
    Boolean tlsVerify = DockerHelper.getProperty(DockerConstants.DOCKER_TLSVERIFY, dockerConfiguration, message, Boolean.class, dockerConfiguration.isTlsVerify());
    Boolean socket = DockerHelper.getProperty(DockerConstants.DOCKER_SOCKET_ENABLED, dockerConfiguration, message, Boolean.class, dockerConfiguration.isSocket());
    String cmdExecFactory = DockerHelper.getProperty(DockerConstants.DOCKER_CMD_EXEC_FACTORY, dockerConfiguration, message, String.class, dockerConfiguration.getCmdExecFactory());
    DockerClientProfile clientProfile = new DockerClientProfile();
    clientProfile.setHost(host);
    clientProfile.setPort(port);
    clientProfile.setEmail(email);
    clientProfile.setUsername(username);
    clientProfile.setPassword(password);
    clientProfile.setRequestTimeout(requestTimeout);
    clientProfile.setServerAddress(serverAddress);
    clientProfile.setCertPath(certPath);
    clientProfile.setMaxTotalConnections(maxTotalConnections);
    clientProfile.setMaxPerRouteConnections(maxPerRouteConnections);
    clientProfile.setSecure(secure);
    clientProfile.setTlsVerify(tlsVerify);
    clientProfile.setSocket(socket);
    clientProfile.setCmdExecFactory(cmdExecFactory);
    DockerClient dockerClient = dockerComponent.getClient(clientProfile);
    if (dockerClient != null) {
        return dockerClient;
    }
    SSLConfig sslConfig;
    if (clientProfile.isSecure() != null && clientProfile.isSecure()) {
        ObjectHelper.notNull(clientProfile.getCertPath(), "certPath must be specified in secure mode");
        sslConfig = new LocalDirectorySSLConfig(clientProfile.getCertPath());
    } else {
        // docker-java requires an implementation of SslConfig interface
        // to be available for DockerCmdExecFactoryImpl
        sslConfig = new NoImplSslConfig();
    }
    DefaultDockerClientConfig.Builder configBuilder = DefaultDockerClientConfig.createDefaultConfigBuilder().withDockerHost(clientProfile.toUrl()).withDockerTlsVerify(clientProfile.isTlsVerify()).withRegistryUsername(clientProfile.getUsername()).withRegistryPassword(clientProfile.getPassword()).withRegistryEmail(clientProfile.getEmail()).withRegistryUrl(clientProfile.getServerAddress()).withCustomSslConfig(sslConfig);
    if (clientProfile.getCertPath() != null) {
        configBuilder.withDockerCertPath(clientProfile.getCertPath());
    }
    CamelContext camelContext = dockerComponent.getCamelContext();
    try {
        DockerCmdExecFactory factory = null;
        if (cmdExecFactory.equals(JerseyDockerCmdExecFactory.class.getName())) {
            factory = new JerseyDockerCmdExecFactory();
            ((JerseyDockerCmdExecFactory) factory).withReadTimeout(clientProfile.getRequestTimeout()).withConnectTimeout(clientProfile.getRequestTimeout()).withMaxTotalConnections(clientProfile.getMaxTotalConnections()).withMaxPerRouteConnections(clientProfile.getMaxPerRouteConnections());
        } else if (cmdExecFactory.equals(NettyDockerCmdExecFactory.class.getName())) {
            factory = new NettyDockerCmdExecFactory();
            ((NettyDockerCmdExecFactory) factory).withConnectTimeout(clientProfile.getRequestTimeout());
        } else {
            Class<DockerCmdExecFactory> clazz = camelContext.getClassResolver().resolveMandatoryClass(cmdExecFactory, DockerCmdExecFactory.class);
            factory = ObjectHelper.newInstance(clazz);
        }
        dockerClient = DockerClientBuilder.getInstance(configBuilder).withDockerCmdExecFactory(factory).build();
        dockerComponent.setClient(clientProfile, dockerClient);
        return dockerClient;
    } catch (ClassNotFoundException e) {
        throw new IllegalStateException("Unable to resolve DockerCmdExecFactory class: " + cmdExecFactory, e);
    }
}
Also used : CamelContext(org.apache.camel.CamelContext) LocalDirectorySSLConfig(com.github.dockerjava.core.LocalDirectorySSLConfig) SSLConfig(com.github.dockerjava.core.SSLConfig) DockerClient(com.github.dockerjava.api.DockerClient) LocalDirectorySSLConfig(com.github.dockerjava.core.LocalDirectorySSLConfig) NoImplSslConfig(org.apache.camel.component.docker.ssl.NoImplSslConfig) DockerCmdExecFactory(com.github.dockerjava.api.command.DockerCmdExecFactory) NettyDockerCmdExecFactory(com.github.dockerjava.netty.NettyDockerCmdExecFactory) JerseyDockerCmdExecFactory(com.github.dockerjava.jaxrs.JerseyDockerCmdExecFactory) JerseyDockerCmdExecFactory(com.github.dockerjava.jaxrs.JerseyDockerCmdExecFactory) DefaultDockerClientConfig(com.github.dockerjava.core.DefaultDockerClientConfig) NettyDockerCmdExecFactory(com.github.dockerjava.netty.NettyDockerCmdExecFactory)

Aggregations

DockerClient (com.github.dockerjava.api.DockerClient)1 DockerCmdExecFactory (com.github.dockerjava.api.command.DockerCmdExecFactory)1 DefaultDockerClientConfig (com.github.dockerjava.core.DefaultDockerClientConfig)1 LocalDirectorySSLConfig (com.github.dockerjava.core.LocalDirectorySSLConfig)1 SSLConfig (com.github.dockerjava.core.SSLConfig)1 JerseyDockerCmdExecFactory (com.github.dockerjava.jaxrs.JerseyDockerCmdExecFactory)1 NettyDockerCmdExecFactory (com.github.dockerjava.netty.NettyDockerCmdExecFactory)1 CamelContext (org.apache.camel.CamelContext)1 NoImplSslConfig (org.apache.camel.component.docker.ssl.NoImplSslConfig)1