Giter Site home page Giter Site logo

HTTPS Support ? about jest HOT 11 CLOSED

searchbox-io avatar searchbox-io commented on July 28, 2024
HTTPS Support ?

from jest.

Comments (11)

peterhawkins avatar peterhawkins commented on July 28, 2024

you can use https with it now:

load key stores from files and 

static SSLSocketFactory getSSLSocketFactory() {
SSLSocketFactory sf = new SSLSocketFactory("SSL", keyStore, "changeit", trustStore,
SecureRandom.getInstance("SHA1PRNG"), new AllowAllHostnameVerifier())
return sf
}

private initJestClient(Set servers) {
// Configuration
ClientConfig clientConfig = new ClientConfig()
clientConfig.getServerProperties().put(ClientConstants.SERVER_LIST, servers)
// Construct a new Jest client according to configuration via factory
JestClientFactory factory = new JestClientFactory()
factory.setClientConfig(clientConfig)
client = factory.getObject()
// ssl
SSLSocketFactory sf = securityUtils.getSSLSocketFactory()
client.httpClient.connectionManager.schemeRegistry.register(new Scheme("https", sf, 8443))
}

 hope this helps!


Peter Hawkins
Sr Software Engineer


From: Javier Antoniucci [email protected]
To: searchbox-io/Jest [email protected]
Sent: Monday, April 1, 2013 10:15 AM
Subject: [Jest] HTTPS Support ? (#34)

Are there plans to include HTTPS support ?

Reply to this email directly or view it on GitHub.

from jest.

jantoniucci avatar jantoniucci commented on July 28, 2024

Works! Thanks!

from jest.

ferhatsb avatar ferhatsb commented on July 28, 2024

Sorry for not being involved, was fighting with production issues.
@peterhawkins thanks for suggestion, really made me smile how you helped here. Spirit of open source.

We may add this internally and construct according to given end points.

from jest.

tomidp avatar tomidp commented on July 28, 2024

Hi All
I try with @peterhawkins way but not work. Do you have any more advice to make it work?
Thank you

from jest.

ferhatsb avatar ferhatsb commented on July 28, 2024

Any error messages which it can guide us?

from jest.

tomidp avatar tomidp commented on July 28, 2024

Hi Ferhat

Thanks for your reply.
I am trying to connect to elastic search with jetty plugin and enable ssl. I use the sample project on : https://github.com/searchbox-io/java-jest-sample.

No error messages, because the code already marked a error on line

client.httpClient.connectionManager.schemeRegistry.register(new Scheme("https", sf, 8443));

This is the full code

@configuration
public class SpringConfiguration {
@bean
public JestClient jestClient() throws Exception {

    String connectionUrl = "https://localhost:9443";

    // Configuration
    ClientConfig clientConfig = new ClientConfig.Builder(connectionUrl).multiThreaded(true).build();

    // Construct a new Jest client according to configuration via factory
    JestClientFactory factory = new JestClientFactory();
    factory.setClientConfig(clientConfig);

    //ssl 
    SSLSocketFactory sf = getSSLSocketFactory(); 
    JestClient client = factory.getObject();

// SSLSocketFactory sf = getSSLSocketFactory();
client.httpClient.connectionManager.schemeRegistry.register(new Scheme("https", sf, 8443));

    return client;
}

private SSLSocketFactory getSSLSocketFactory() throws KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException { 
    InputStream stream = this.getClass().getClassLoader().getResourceAsStream("jettykeystore");
    KeyStore keyStore  = KeyStore.getInstance("pkcs12");
    keyStore.load(stream, "tomi12".toCharArray());
    SSLSocketFactory sf = new SSLSocketFactory("SSL", keyStore, "changeit", keyStore, SecureRandom.getInstance("RSA"), new AllowAllHostnameVerifier()); 
    return sf; 
}

}

Thank you

from jest.

ferhatsb avatar ferhatsb commented on July 28, 2024

Try this;
client.getHttpClient().getConnectionManager().getSchemeRegistry().register(new Scheme("https", sf, 8443));

from jest.

tomidp avatar tomidp commented on July 28, 2024

Hi @ferhatsb

Thank you very much for your reply. Now i have no issue on the code. My code can compile without any error.
But still i can't connect to the elastic-search with jetty plugin, with my generated keystore.

This is my code now

@Bean
public JestClient jestClient() {

    String connectionUrl = "https://localhost:9443";

    // Configuration
    ClientConfig clientConfig = new ClientConfig.Builder(connectionUrl).multiThreaded(true).build();

    // Construct a new Jest client according to configuration via factory
    JestClientFactory factory = new JestClientFactory();
    factory.setClientConfig(clientConfig);

    //ssl 
    SSLSocketFactory sf = getSSLSocketFactory(); 
    JestHttpClient client = (JestHttpClient) factory.getObject();
    client.getHttpClient().getConnectionManager().getSchemeRegistry().register(new Scheme("https", 9443, sf));

    return client;
}

private SSLSocketFactory getSSLSocketFactory() { 
    InputStream stream = this.getClass().getClassLoader().getResourceAsStream("jettykeystore");
    KeyStore keyStore;
    try {
        keyStore = KeyStore.getInstance("JKS");
        keyStore.load(stream, "tomi12".toCharArray());
        SSLSocketFactory sf = new SSLSocketFactory(SSLSocketFactory.SSL, keyStore, "tomi12", keyStore, SecureRandom.getInstance("SHA1PRNG"), new AllowAllHostnameVerifier()); 
        return sf; 
    } catch (KeyStoreException e) {
        log.info("Keystore Exception : {} \n {}",e.getMessage(), e.getClass());
    } catch (NoSuchAlgorithmException e) {
        log.info("NSA Exception : {} \n {}",e.getMessage(), e.getClass());
    } catch (CertificateException e) {
        log.info("Certificate Exception : {} \n {}",e.getMessage(), e.getClass());
    } catch (IOException e) {
        log.info("IO Exception : {} \n {}",e.getMessage(), e.getClass());
    } catch (KeyManagementException e) {
        log.info("KeyManagement Exception : {} \n {}",e.getMessage(), e.getClass());
    } catch (UnrecoverableKeyException e) {
        log.info("Uncoverable Key Exception : {} \n {}",e.getMessage(), e.getClass());
    }
    return null;

}

Right now when i am trying to connect to elastic search, i will get authorization issue.

ERROR AbstractJestClient - An exception occurred while converting json string to map object
INFO SearchService - RESULT : 401 Unauthorized

I am still trying to find the way to make it work.
Thank you

from jest.

tomidp avatar tomidp commented on July 28, 2024

Hi @ferhatsb

Thanks for your help, now my code working as i expected. Beside register scheme, I need to add auth to my code.

    List<String> authpref = new ArrayList<String>();
    authpref.add(AuthPolicy.BASIC);
    client.getHttpClient().getParams().setParameter(AuthPNames.PROXY_AUTH_PREF, authpref);

    DefaultHttpClient httpClient = (DefaultHttpClient) client.getHttpClient();

    UsernamePasswordCredentials creds = new UsernamePasswordCredentials("user", "password");
    httpClient.getCredentialsProvider().setCredentials(new AuthScope("localhost", 9443), creds);

from jest.

ferhatsb avatar ferhatsb commented on July 28, 2024

Hi @tomidp

I was going to advise you to check credentials due to error 401.

Anyway it is sorted now. Also you should be able to;

String connectionUrl = "https://user:password@localhost:9443";

Instead of setting credentials explicitly.

from jest.

ravinaik1312 avatar ravinaik1312 commented on July 28, 2024

Looks like the apis have changed, the above implementations to connect ES via Jest does not work as a lot of http client functions are deprecated.

Update on how you would do it now:

SSLContext sslContext = SSLContexts.custom()
.loadTrustMaterial(keyStore, new TrustSelfSignedStrategy())
.useSSL()
.build();

SSLConnectionSocketFactory sf = new SSLConnectionSocketFactory(sslContext, new AllowAllHostnameVerifier());

client.setHttpClient(HttpClientBuilder.create().setSSLSocketFactory(sf).build());

from jest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.