Search in sources :

Example 36 with SSLContextParameters

use of org.apache.camel.util.jsse.SSLContextParameters in project camel by apache.

the class NettySSLContextParametersTest method createRegistry.

@Override
protected JndiRegistry createRegistry() throws Exception {
    KeyStoreParameters ksp = new KeyStoreParameters();
    ksp.setResource(this.getClass().getClassLoader().getResource("keystore.jks").toString());
    ksp.setPassword("changeit");
    KeyManagersParameters kmp = new KeyManagersParameters();
    kmp.setKeyPassword("changeit");
    kmp.setKeyStore(ksp);
    TrustManagersParameters tmp = new TrustManagersParameters();
    tmp.setKeyStore(ksp);
    // NOTE: Needed since the client uses a loose trust configuration when no ssl context
    // is provided.  We turn on WANT client-auth to prefer using authentication
    SSLContextServerParameters scsp = new SSLContextServerParameters();
    scsp.setClientAuthentication(ClientAuthentication.WANT.name());
    SSLContextParameters sslContextParameters = new SSLContextParameters();
    sslContextParameters.setKeyManagers(kmp);
    sslContextParameters.setTrustManagers(tmp);
    sslContextParameters.setServerParameters(scsp);
    JndiRegistry registry = super.createRegistry();
    registry.bind("sslContextParameters", sslContextParameters);
    return registry;
}
Also used : KeyManagersParameters(org.apache.camel.util.jsse.KeyManagersParameters) JndiRegistry(org.apache.camel.impl.JndiRegistry) TrustManagersParameters(org.apache.camel.util.jsse.TrustManagersParameters) KeyStoreParameters(org.apache.camel.util.jsse.KeyStoreParameters) SSLContextServerParameters(org.apache.camel.util.jsse.SSLContextServerParameters) SSLContextParameters(org.apache.camel.util.jsse.SSLContextParameters)

Example 37 with SSLContextParameters

use of org.apache.camel.util.jsse.SSLContextParameters in project camel by apache.

the class RestletComponent method addServerIfNecessary.

protected void addServerIfNecessary(RestletEndpoint endpoint) throws Exception {
    String key = buildKey(endpoint);
    Server server;
    synchronized (servers) {
        server = servers.get(key);
        if (server == null) {
            server = createServer(endpoint);
            component.getServers().add(server);
            // Add any Restlet server parameters that were included
            Series<Parameter> params = server.getContext().getParameters();
            if ("https".equals(endpoint.getProtocol())) {
                SSLContextParameters scp = endpoint.getSslContextParameters();
                if (endpoint.getSslContextParameters() == null) {
                    throw new InvalidParameterException("Need to specify the SSLContextParameters option here!");
                }
                setupServerWithSSLContext(params, scp);
            }
            if (getControllerDaemon() != null) {
                params.add("controllerDaemon", getControllerDaemon().toString());
            }
            if (getControllerSleepTimeMs() != null) {
                params.add("controllerSleepTimeMs", getControllerSleepTimeMs().toString());
            }
            if (getInboundBufferSize() != null) {
                params.add("inboundBufferSize", getInboundBufferSize().toString());
            }
            if (getMinThreads() != null) {
                params.add("minThreads", getMinThreads().toString());
            }
            if (getMaxThreads() != null) {
                params.add("maxThreads", getMaxThreads().toString());
            }
            if (getLowThreads() != null) {
                params.add("lowThreads", getLowThreads().toString());
            }
            if (getMaxQueued() != null) {
                params.add("maxQueued", getMaxQueued().toString());
            }
            if (getMaxConnectionsPerHost() != null) {
                params.add("maxConnectionsPerHost", getMaxConnectionsPerHost().toString());
            }
            if (getMaxTotalConnections() != null) {
                params.add("maxTotalConnections", getMaxTotalConnections().toString());
            }
            if (getOutboundBufferSize() != null) {
                params.add("outboundBufferSize", getOutboundBufferSize().toString());
            }
            if (getPersistingConnections() != null) {
                params.add("persistingConnections", getPersistingConnections().toString());
            }
            if (getPipeliningConnections() != null) {
                params.add("pipeliningConnections", getPipeliningConnections().toString());
            }
            if (getThreadMaxIdleTimeMs() != null) {
                params.add("threadMaxIdleTimeMs", getThreadMaxIdleTimeMs().toString());
            }
            if (getUseForwardedForHeader() != null) {
                params.add("useForwardedForHeader", getUseForwardedForHeader().toString());
            }
            if (getReuseAddress() != null) {
                params.add("reuseAddress", getReuseAddress().toString());
            }
            LOG.debug("Setting parameters: {} to server: {}", params, server);
            server.getContext().setParameters(params);
            servers.put(key, server);
            LOG.debug("Added server: {}", key);
            server.start();
        }
    }
}
Also used : InvalidParameterException(java.security.InvalidParameterException) Server(org.restlet.Server) Parameter(org.restlet.data.Parameter) SSLContextParameters(org.apache.camel.util.jsse.SSLContextParameters)

Example 38 with SSLContextParameters

use of org.apache.camel.util.jsse.SSLContextParameters in project camel by apache.

the class SalesforceComponent method doStart.

@Override
protected void doStart() throws Exception {
    if (loginConfig == null) {
        if (ObjectHelper.isNotEmpty(password)) {
            loginConfig = new SalesforceLoginConfig(loginUrl, clientId, clientSecret, userName, password, lazyLogin);
        } else if (ObjectHelper.isNotEmpty(refreshToken)) {
            loginConfig = new SalesforceLoginConfig(loginUrl, clientId, clientSecret, refreshToken, lazyLogin);
        } else if (ObjectHelper.isNotEmpty(keystore)) {
            loginConfig = new SalesforceLoginConfig(loginUrl, clientId, userName, keystore, lazyLogin);
        } else {
            throw new IllegalArgumentException("Cannot define a login configuration, the component configuration" + " does not contain `password`, `refreshToken` or `keystore` parameters. Specifying one of those" + " determines the type of authentication performed.");
        }
        LOG.debug("Created login configuration: {}", loginConfig);
    } else {
        LOG.debug("Using shared login configuration: {}", loginConfig);
    }
    // create a Jetty HttpClient if not already set
    if (null == httpClient) {
        if (config != null && config.getHttpClient() != null) {
            httpClient = config.getHttpClient();
        } else {
            // set ssl context parameters if set
            final SSLContextParameters contextParameters = sslContextParameters != null ? sslContextParameters : new SSLContextParameters();
            final SslContextFactory sslContextFactory = new SslContextFactory();
            sslContextFactory.setSslContext(contextParameters.createSSLContext(getCamelContext()));
            httpClient = new SalesforceHttpClient(sslContextFactory);
            // default settings, use httpClientProperties to set other properties
            httpClient.setConnectTimeout(CONNECTION_TIMEOUT);
        }
    }
    // set HTTP client parameters
    if (httpClientProperties != null && !httpClientProperties.isEmpty()) {
        IntrospectionSupport.setProperties(getCamelContext().getTypeConverter(), httpClient, new HashMap<String, Object>(httpClientProperties));
    }
    // set HTTP proxy settings
    if (this.httpProxyHost != null && httpProxyPort != null) {
        Origin.Address proxyAddress = new Origin.Address(this.httpProxyHost, this.httpProxyPort);
        ProxyConfiguration.Proxy proxy;
        if (isHttpProxySocks4) {
            proxy = new Socks4Proxy(proxyAddress, isHttpProxySecure);
        } else {
            proxy = new HttpProxy(proxyAddress, isHttpProxySecure);
        }
        if (httpProxyIncludedAddresses != null && !httpProxyIncludedAddresses.isEmpty()) {
            proxy.getIncludedAddresses().addAll(httpProxyIncludedAddresses);
        }
        if (httpProxyExcludedAddresses != null && !httpProxyExcludedAddresses.isEmpty()) {
            proxy.getExcludedAddresses().addAll(httpProxyExcludedAddresses);
        }
        httpClient.getProxyConfiguration().getProxies().add(proxy);
    }
    if (this.httpProxyUsername != null && httpProxyPassword != null) {
        ObjectHelper.notEmpty(httpProxyAuthUri, "httpProxyAuthUri");
        ObjectHelper.notEmpty(httpProxyRealm, "httpProxyRealm");
        final Authentication authentication;
        if (httpProxyUseDigestAuth) {
            authentication = new DigestAuthentication(new URI(httpProxyAuthUri), httpProxyRealm, httpProxyUsername, httpProxyPassword);
        } else {
            authentication = new BasicAuthentication(new URI(httpProxyAuthUri), httpProxyRealm, httpProxyUsername, httpProxyPassword);
        }
        httpClient.getAuthenticationStore().addAuthentication(authentication);
    }
    // support restarts
    if (this.session == null) {
        this.session = new SalesforceSession(getCamelContext(), httpClient, httpClient.getTimeout(), loginConfig);
    }
    // set session before calling start()
    httpClient.setSession(this.session);
    // start the Jetty client to initialize thread pool, etc.
    httpClient.start();
    // login at startup if lazyLogin is disabled
    if (!loginConfig.isLazyLogin()) {
        ServiceHelper.startService(session);
    }
    if (packages != null && packages.length > 0) {
        // parse the packages to create SObject name to class map
        classMap = parsePackages();
        LOG.info("Found {} generated classes in packages: {}", classMap.size(), Arrays.asList(packages));
    } else {
        // use an empty map to avoid NPEs later
        LOG.warn("Missing property packages, getSObject* operations will NOT work");
        classMap = new HashMap<String, Class<?>>(0);
    }
    if (subscriptionHelper != null) {
        ServiceHelper.startService(subscriptionHelper);
    }
}
Also used : Origin(org.eclipse.jetty.client.Origin) DigestAuthentication(org.eclipse.jetty.client.util.DigestAuthentication) URI(java.net.URI) SSLContextParameters(org.apache.camel.util.jsse.SSLContextParameters) HttpProxy(org.eclipse.jetty.client.HttpProxy) Socks4Proxy(org.eclipse.jetty.client.Socks4Proxy) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ProxyConfiguration(org.eclipse.jetty.client.ProxyConfiguration) Authentication(org.eclipse.jetty.client.api.Authentication) DigestAuthentication(org.eclipse.jetty.client.util.DigestAuthentication) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) SalesforceSession(org.apache.camel.component.salesforce.internal.SalesforceSession) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication)

Example 39 with SSLContextParameters

use of org.apache.camel.util.jsse.SSLContextParameters in project camel by apache.

the class RestApiIntegrationTest method testRetry.

@Test
public void testRetry() throws Exception {
    final SalesforceComponent sf = context().getComponent("salesforce", SalesforceComponent.class);
    final String accessToken = sf.getSession().getAccessToken();
    final SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setSslContext(new SSLContextParameters().createSSLContext(context));
    final HttpClient httpClient = new HttpClient(sslContextFactory);
    httpClient.setConnectTimeout(60000);
    httpClient.start();
    final String uri = sf.getLoginConfig().getLoginUrl() + "/services/oauth2/revoke?token=" + accessToken;
    final Request logoutGet = httpClient.newRequest(uri).method(HttpMethod.GET).timeout(1, TimeUnit.MINUTES);
    final ContentResponse response = logoutGet.send();
    assertEquals(HttpStatus.OK_200, response.getStatus());
    testGetGlobalObjects();
}
Also used : SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) Request(org.eclipse.jetty.client.api.Request) SSLContextParameters(org.apache.camel.util.jsse.SSLContextParameters) Test(org.junit.Test)

Example 40 with SSLContextParameters

use of org.apache.camel.util.jsse.SSLContextParameters in project camel by apache.

the class RestApiIntegrationTest method testRetryFailure.

@Test
public void testRetryFailure() throws Exception {
    final SalesforceComponent sf = context().getComponent("salesforce", SalesforceComponent.class);
    final String accessToken = sf.getSession().getAccessToken();
    final SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setSslContext(new SSLContextParameters().createSSLContext(context));
    final HttpClient httpClient = new HttpClient(sslContextFactory);
    httpClient.setConnectTimeout(60000);
    httpClient.start();
    final String uri = sf.getLoginConfig().getLoginUrl() + "/services/oauth2/revoke?token=" + accessToken;
    final Request logoutGet = httpClient.newRequest(uri).method(HttpMethod.GET).timeout(1, TimeUnit.MINUTES);
    final ContentResponse response = logoutGet.send();
    assertEquals(HttpStatus.OK_200, response.getStatus());
    // set component config to bad password to cause relogin attempts to fail
    final String password = sf.getLoginConfig().getPassword();
    sf.getLoginConfig().setPassword("bad_password");
    try {
        testGetGlobalObjects();
        fail("Expected CamelExecutionException!");
    } catch (final CamelExecutionException e) {
        if (e.getCause() instanceof SalesforceException) {
            final SalesforceException cause = (SalesforceException) e.getCause();
            assertEquals("Expected 400 on authentication retry failure", HttpStatus.BAD_REQUEST_400, cause.getStatusCode());
        } else {
            fail("Expected SalesforceException!");
        }
    } finally {
        // reset password and retries to allow other tests to pass
        sf.getLoginConfig().setPassword(password);
    }
}
Also used : CamelExecutionException(org.apache.camel.CamelExecutionException) SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) Request(org.eclipse.jetty.client.api.Request) SSLContextParameters(org.apache.camel.util.jsse.SSLContextParameters) Test(org.junit.Test)

Aggregations

SSLContextParameters (org.apache.camel.util.jsse.SSLContextParameters)59 KeyStoreParameters (org.apache.camel.util.jsse.KeyStoreParameters)35 KeyManagersParameters (org.apache.camel.util.jsse.KeyManagersParameters)28 TrustManagersParameters (org.apache.camel.util.jsse.TrustManagersParameters)27 JndiRegistry (org.apache.camel.impl.JndiRegistry)19 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)8 IOException (java.io.IOException)7 GeneralSecurityException (java.security.GeneralSecurityException)7 SSLContextServerParameters (org.apache.camel.util.jsse.SSLContextServerParameters)7 RouteBuilder (org.apache.camel.builder.RouteBuilder)6 SSLContext (javax.net.ssl.SSLContext)5 Test (org.junit.Test)4 URI (java.net.URI)3 SalesforceException (org.apache.camel.component.salesforce.api.SalesforceException)3 SalesforceSession (org.apache.camel.component.salesforce.internal.SalesforceSession)3 HttpClient (org.eclipse.jetty.client.HttpClient)3 JdkSslContext (io.netty.handler.ssl.JdkSslContext)2 URISyntaxException (java.net.URISyntaxException)2 CertificateException (java.security.cert.CertificateException)2 X509Certificate (java.security.cert.X509Certificate)2