java.asciidoc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. [[java-clients]]
  2. === Java Client and security
  3. deprecated[7.0.0, The `TransportClient` is deprecated in favour of the {java-rest}/java-rest-high.html[Java High Level REST Client] and will be removed in Elasticsearch 8.0. The {java-rest}/java-rest-high-level-migration.html[migration guide] describes all the steps needed to migrate.]
  4. The {es} {security-features} support the Java http://www.elastic.co/guide/en/elasticsearch/client/java-api/current/transport-client.html[transport client] for Elasticsearch.
  5. The transport client uses the same transport protocol that the cluster nodes use
  6. for inter-node communication. It is very efficient as it does not have to marshall
  7. and unmarshall JSON requests like a typical REST client.
  8. NOTE: Using the Java Node Client with secured clusters is not recommended or
  9. supported.
  10. [float]
  11. [[transport-client]]
  12. ==== Configuring the Transport Client to work with a Secured Cluster
  13. To use the transport client with a secured cluster, you need to:
  14. [[java-transport-client-role]]
  15. . {ref}/setup-xpack-client.html[Configure the {xpack} transport client].
  16. . Configure a user with the privileges required to start the transport client.
  17. A default `transport_client` role is built-in to the {es} {security-features},
  18. which grants the
  19. appropriate cluster permissions for the transport client to work with the secured
  20. cluster. The transport client uses the _Nodes Info API_ to fetch information about
  21. the nodes in the cluster.
  22. . Set up the transport client. At a minimum, you must configure `xpack.security.user` to
  23. include the name and password of your transport client user in your requests. The
  24. following snippet configures the user credentials globally--every request
  25. submitted with this client includes the `transport_client_user` credentials in
  26. its headers.
  27. +
  28. --
  29. [source,java]
  30. -------------------------------------------------------------------------------------------------
  31. import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;
  32. ...
  33. TransportClient client = new PreBuiltXPackTransportClient(Settings.builder()
  34. .put("cluster.name", "myClusterName")
  35. .put("xpack.security.user", "transport_client_user:x-pack-test-password")
  36. ...
  37. .build())
  38. .addTransportAddress(new TransportAddress("localhost", 9300))
  39. .addTransportAddress(new TransportAddress("localhost", 9301));
  40. -------------------------------------------------------------------------------------------------
  41. WARNING: If you configure a transport client without SSL, passwords are sent in
  42. clear text.
  43. You can also add an `Authorization` header to each request. If you've configured
  44. global authorization credentials, the `Authorization` header overrides the global
  45. authentication credentials. This is useful when an application has multiple users
  46. who access Elasticsearch using the same client. You can set the global token to
  47. a user that only has the `transport_client` role, and add the `transport_client`
  48. role to the individual users.
  49. For example, the following snippet adds the `Authorization` header to a search
  50. request:
  51. [source,java]
  52. --------------------------------------------------------------------------------------------------
  53. import org.elasticsearch.common.settings.SecureString;
  54. import org.elasticsearch.common.settings.Settings;
  55. import org.elasticsearch.common.transport.TransportAddress;
  56. import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;
  57. import static UsernamePasswordToken.basicAuthHeaderValue;
  58. ...
  59. TransportClient client = new PreBuiltXPackTransportClient(Settings.builder()
  60. .put("cluster.name", "myClusterName")
  61. .put("xpack.security.user", "transport_client_user:x-pack-test-password")
  62. ...
  63. .build())
  64. .build()
  65. .addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300))
  66. .addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9301))
  67. String token = basicAuthHeaderValue("test_user", new SecureString("x-pack-test-password".toCharArray()));
  68. client.filterWithHeader(Collections.singletonMap("Authorization", token))
  69. .prepareSearch().get();
  70. --------------------------------------------------------------------------------------------------
  71. --
  72. . Enable SSL to authenticate clients and encrypt communications. To enable SSL,
  73. you need to:
  74. .. Configure the paths to the client's key and certificate in addition to the certificate authorities.
  75. Client authentication requires every client to have a certification signed by a trusted CA.
  76. +
  77. --
  78. NOTE: Client authentication is enabled by default. For information about
  79. disabling client authentication, see <<disabling-client-auth, Disabling Client Authentication>>.
  80. [source,java]
  81. --------------------------------------------------------------------------------------------------
  82. import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;
  83. ...
  84. TransportClient client = new PreBuiltXPackTransportClient(Settings.builder()
  85. .put("cluster.name", "myClusterName")
  86. .put("xpack.security.user", "transport_client_user:x-pack-test-password")
  87. .put("xpack.ssl.key", "/path/to/client.key")
  88. .put("xpack.ssl.certificate", "/path/to/client.crt")
  89. .put("xpack.ssl.certificate_authorities", "/path/to/ca.crt")
  90. ...
  91. .build());
  92. --------------------------------------------------------------------------------------------------
  93. --
  94. .. Enable the SSL transport by setting `xpack.security.transport.ssl.enabled` to `true` in the
  95. client configuration.
  96. +
  97. --
  98. [source,java]
  99. --------------------------------------------------------------------------------------------------
  100. import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;
  101. ...
  102. TransportClient client = new PreBuiltXPackTransportClient(Settings.builder()
  103. .put("cluster.name", "myClusterName")
  104. .put("xpack.security.user", "transport_client_user:x-pack-test-password")
  105. .put("xpack.ssl.key", "/path/to/client.key")
  106. .put("xpack.ssl.certificate", "/path/to/client.crt")
  107. .put("xpack.ssl.certificate_authorities", "/path/to/ca.crt")
  108. .put("xpack.security.transport.ssl.enabled", "true")
  109. ...
  110. .build())
  111. .addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300))
  112. .addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9301))
  113. --------------------------------------------------------------------------------------------------
  114. --
  115. [float]
  116. [[disabling-client-auth]]
  117. ===== Disabling client authentication
  118. If you want to disable client authentication, you can use a client-specific
  119. transport protocol. For more information see <<separating-node-client-traffic, Separating Node to Node and Client Traffic>>.
  120. If you are not using client authentication and sign the Elasticsearch node
  121. certificates with your own CA, you need to provide the path to the CA
  122. certificate in your client configuration.
  123. [source,java]
  124. ------------------------------------------------------------------------------------------------------
  125. import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;
  126. ...
  127. TransportClient client = new PreBuiltXPackTransportClient(Settings.builder()
  128. .put("cluster.name", "myClusterName")
  129. .put("xpack.security.user", "test_user:x-pack-test-password")
  130. .put("xpack.ssl.certificate_authorities", "/path/to/ca.crt")
  131. .put("xpack.security.transport.ssl.enabled", "true")
  132. ...
  133. .build())
  134. .addTransportAddress(new TransportAddress("localhost", 9300))
  135. .addTransportAddress(new TransportAddress("localhost", 9301));
  136. ------------------------------------------------------------------------------------------------------
  137. NOTE: If you are using a public CA that is already trusted by the Java runtime,
  138. you do not need to set the `xpack.ssl.certificate_authorities`.
  139. [float]
  140. [[connecting-anonymously]]
  141. ===== Connecting anonymously
  142. To enable the transport client to connect anonymously, you must assign the
  143. anonymous user the privileges defined in the <<java-transport-client-role,transport_client>>
  144. role. Anonymous access must also be enabled, of course. For more information,
  145. see <<anonymous-access,Enabling Anonymous Access>>.
  146. [float]
  147. [[security-client]]
  148. ==== Security client
  149. The {stack} {security-features} expose an API through the `SecurityClient` class.
  150. To get a hold of a `SecurityClient` you first need to create the `XPackClient`,
  151. which is a wrapper around the existing {es} clients (any client class implementing
  152. `org.elasticsearch.client.Client`).
  153. The following example shows how you can clear the realm caches using
  154. the `SecurityClient`:
  155. [source,java]
  156. ------------------------------------------------------------------------------------------------------
  157. Client client = ... // create the transport client
  158. XPackClient xpackClient = new XPackClient(client);
  159. SecurityClient securityClient = xpackClient.security();
  160. ClearRealmCacheResponse response = securityClient.authc().prepareClearRealmCache()
  161. .realms("ldap1", "ad1") <1>
  162. .usernames("rdeniro")
  163. .get();
  164. ------------------------------------------------------------------------------------------------------
  165. <1> Clears the `ldap1` and `ad1` realm caches for the `rdeniro` user.