Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes 3360: Add support for newer Elasticsearch search api #426

Closed
wants to merge 7 commits into from

Conversation

vga91
Copy link
Owner

@vga91 vga91 commented Feb 14, 2024

Since we cannot use certificates, see below, I created tests using basic authentication with the xpack.security.http.ssl.enabled: false configuration

  • Create factory ElasticSearchHandler
  • Split test to test both Elastic 7.9.2 (default tag) and Elastic 8.12.1, where the ElasticSearchTest.java is now the Base Test
  • Tried several Elastic 8 APIs
  • Some refactoring / code cleaning



  • Waiting for https://github.com/neo4j-contrib/neo4j-apoc-procedures/issues/2136.
  • Waiting for apoc.es.delete to change testPostCreateDocumentWithAuthHeader
  • Write trello card to create certificate

Certificate

To authenticate with elastic 8, there are basically 3 ways:

  • TESTED IN THIS PR: configuring xpack.security.http.ssl.enabled: false, but we have to pass the header (which will be implemented inhttps://github.com/neo4j-contrib/neo4j-apoc-procedures/issues/2136),

  • configuring xpack.security.enabled=false, which is of course a turnoff, since we can authenticate without user and password

  • without any config, but in this case a certificate is required.
    However to handle it, it should be put in the apoc core code something like this in Util.openUrlConnection:

    private static URLConnection getUrlConnection(URL src, ConnectionConfig config) throws IOException {
        if (!src.getProtocol().equalsIgnoreCase("https") || Optional.ofNullable(config).map(ConnectionConfig::getKeyStoreUrl).isEmpty()) {
            return src.openConnection();
        }

        try {
            KeyStore keyStore;
            keyStore = KeyStore.getInstance(config.getKeyStoreType());
            keyStore.load(new FileInputStream (config.getKeyStoreUrl()), config.getKeyStorePassword().toCharArray());
            TrustManagerFactory tmFactory = TrustManagerFactory.getInstance(config.getTrustManagerAlgorithm());
            tmFactory.init(keyStore);

            SSLContext sslContext = SSLContext.getInstance(config.getSecureProtocol());
            sslContext.init (null, tmFactory.getTrustManagers(), null);
            SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

            HttpsURLConnection secureConnection = (HttpsURLConnection) src.openConnection();
            secureConnection.setSSLSocketFactory(sslSocketFactory);
            return secureConnection;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static URLConnection openUrlConnection(URL src, Map<String, Object> headers) throws IOException {
        URLConnection con = getUrlConnection(src);
        con.setRequestProperty("User-Agent", "APOC Procedures for Neo4j");
        if (con instanceof HttpURLConnection) {
            HttpURLConnection http = (HttpURLConnection) con;
            http.setInstanceFollowRedirects(false);
            if (headers != null) {
                Object method = headers.get("method");
                if (method != null) {
                    http.setRequestMethod(method.toString());
                    http.setChunkedStreamingMode(1024 * 1024);
                }
                headers.forEach((k, v) -> con.setRequestProperty(k, v == null ? "" : v.toString()));
            }
        }
  }

since we cannot make changes on Core at the moment, I think it is worth creating a trello card to implement it.

I don't think there are any feasible alternatives without touching the apoc core code, unless you copy all the methods into extended of course.

@vga91 vga91 force-pushed the issue-3360 branch 2 times, most recently from cd647d0 to 591ea58 Compare March 1, 2024 08:53
@RobertoSannino
Copy link
Collaborator

LGTM

@vga91 vga91 closed this Apr 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants