ES 7.4.1 - 身份验证 [Rest API]

ES 7.4.1 - Authentication [Rest API]

我是 ES 的新手,我的新工作中有一项任务是从 6.4.2 升级到 7.4.1 – 从 TCP 客户端到 Rest High Level API。

之前我们是这样构建客户端的:

Settings settings = Settings.builder()
      .put("xpack.security.user", String.format("%s:%s",esJavaUser,esJavaPassword))
      .put("cluster.name", esClusterName)
      .put("xpack.security.transport.ssl.enabled", xpackSecurityTransportSslEnabled)
      .put("xpack.ssl.certificate_authorities", xpackSslCertificateAuthorities)
      .build();

 client = new PreBuiltXPackTransportClient(settings);

现在,在休息时 API,它已更改为:

final CredentialsProvider credentialsProvider =
        new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
        new UsernamePasswordCredentials(esJavaUser, esJavaPassword));

RestClientBuilder restClientBuilder = RestClient.builder(hosts)
        .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder
                .setDefaultCredentialsProvider(credentialsProvider));
restHighLevelClient = new RestHighLevelClient(restClientBuilder);

在这个版本中,我通过 CredentialsProvider 设置了 ES 用户和密码 但是 ssl.enabledcertificate_authorities” 呢?我该如何让他们休息 API?

我从 ES 论坛得到了答案(没想到要先问那里..)

因为作为开发者,我一直在这里寻找答案,在Whosebug中,我决定不删除这个问题并复制TimV答案:

您要找的文档在这里:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.4/_encrypted_communication.html

SSL 会根据您传递给构建器的 HttpHost 个对象中的方案(协议)自动启用(或不启用)。

RestClient.builder(hosts)

如果您正在使用 SSL,您希望在构造 HttpHost 对象 (hosts) 时将 "https" 作为方案(第三个参数)传递。

不幸的是,没有简单的方法可以将 certificate_authorities 传递给 Rest 客户端,您需要将这些证书转换为标准的 Java 信任库。 您或许可以在网上找到一些示例代码 ("convert PEM certificates to Java truststore"),但其要点是:

  1. 将证书颁发机构文件作为 InputStream
  2. 打开
  3. 创建 X.509 证书工厂:java.security.cert.CertificateFactory.getInstance("X.509")
  4. 在证书工厂上调用 generateCertificates 以将这些证书文件读入 java Certificate 个对象
  5. 构造一个空的 KeyStore 对象
  6. 将加载的证书添加为可信条目
  7. 传递给 SSLContextBuilder.loadTrustMaterial

Link: https://discuss.elastic.co/t/es-7-4-1-authentication-rest-api/211969