configuration.asciidoc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. [[java-rest-low-config]]
  2. == Common configuration
  3. As explained in <<java-rest-low-usage-initialization>>, the `RestClientBuilder`
  4. supports providing both a `RequestConfigCallback` and an `HttpClientConfigCallback`
  5. which allow for any customization that the Apache Async Http Client exposes.
  6. Those callbacks make it possible to modify some specific behaviour of the client
  7. without overriding every other default configuration that the `RestClient`
  8. is initialized with. This section describes some common scenarios that require
  9. additional configuration for the low-level Java REST Client.
  10. === Timeouts
  11. Configuring requests timeouts can be done by providing an instance of
  12. `RequestConfigCallback` while building the `RestClient` through its builder.
  13. The interface has one method that receives an instance of
  14. https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/config/RequestConfig.Builder.html[`org.apache.http.client.config.RequestConfig.Builder`]
  15. as an argument and has the same return type. The request config builder can
  16. be modified and then returned. In the following example we increase the
  17. connect timeout (defaults to 1 second) and the socket timeout (defaults to 30
  18. seconds).
  19. ["source","java",subs="attributes,callouts,macros"]
  20. --------------------------------------------------
  21. include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-config-timeouts]
  22. --------------------------------------------------
  23. Timeouts also can be set per request with RequestOptions, which overrides RestClient customizeRequestConfig.
  24. ["source","java",subs="attributes,callouts,macros"]
  25. --------------------------------------------------
  26. include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-config-request-options-timeouts]
  27. --------------------------------------------------
  28. === Number of threads
  29. The Apache Http Async Client starts by default one dispatcher thread, and a
  30. number of worker threads used by the connection manager, as many as the number
  31. of locally detected processors (depending on what
  32. `Runtime.getRuntime().availableProcessors()` returns). The number of threads
  33. can be modified as follows:
  34. ["source","java",subs="attributes,callouts,macros"]
  35. --------------------------------------------------
  36. include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-config-threads]
  37. --------------------------------------------------
  38. === Basic authentication
  39. Configuring basic authentication can be done by providing an
  40. `HttpClientConfigCallback` while building the `RestClient` through its builder.
  41. The interface has one method that receives an instance of
  42. https://hc.apache.org/httpcomponents-asyncclient-dev/httpasyncclient/apidocs/org/apache/http/impl/nio/client/HttpAsyncClientBuilder.html[`org.apache.http.impl.nio.client.HttpAsyncClientBuilder`]
  43. as an argument and has the same return type. The http client builder can be
  44. modified and then returned. In the following example we set a default
  45. credentials provider that requires basic authentication.
  46. ["source","java",subs="attributes,callouts,macros"]
  47. --------------------------------------------------
  48. include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-config-basic-auth]
  49. --------------------------------------------------
  50. Preemptive Authentication can be disabled, which means that every request will be sent without
  51. authorization headers to see if it is accepted and, upon receiving an HTTP 401 response, it will
  52. resend the exact same request with the basic authentication header. If you wish to do this, then
  53. you can do so by disabling it via the `HttpAsyncClientBuilder`:
  54. ["source","java",subs="attributes,callouts,macros"]
  55. --------------------------------------------------
  56. include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-config-disable-preemptive-auth]
  57. --------------------------------------------------
  58. <1> Disable preemptive authentication
  59. === Other authentication methods
  60. ==== Elasticsearch Token Service tokens
  61. If you want the client to authenticate with an Elasticsearch access token, set the relevant HTTP request header.
  62. If the client makes requests on behalf of a single user only, you can set the necessary `Authorization` header as a default header as shown
  63. in the following example:
  64. ["source","java",subs="attributes,callouts,macros"]
  65. --------------------------------------------------
  66. include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-auth-bearer-token]
  67. --------------------------------------------------
  68. ==== Elasticsearch API keys
  69. If you want the client to authenticate with an Elasticsearch API key, set the relevant HTTP request header.
  70. If the client makes requests on behalf of a single user only, you can set the necessary `Authorization` header as a default header as shown
  71. in the following example:
  72. ["source","java",subs="attributes,callouts,macros"]
  73. --------------------------------------------------
  74. include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-auth-api-key]
  75. --------------------------------------------------
  76. === Encrypted communication
  77. Encrypted communication using TLS can also be configured through the
  78. `HttpClientConfigCallback`. The
  79. https://hc.apache.org/httpcomponents-asyncclient-dev/httpasyncclient/apidocs/org/apache/http/impl/nio/client/HttpAsyncClientBuilder.html[`org.apache.http.impl.nio.client.HttpAsyncClientBuilder`]
  80. received as an argument exposes multiple methods to configure encrypted
  81. communication: `setSSLContext`, `setSSLSessionStrategy` and
  82. `setConnectionManager`, in order of precedence from the least important.
  83. When accessing an Elasticsearch cluster that is setup for TLS on the HTTP layer, the client needs to trust the certificate that
  84. Elasticsearch is using.
  85. The following is an example of setting up the client to trust the CA that has signed the certificate that Elasticsearch is using, when
  86. that CA certificate is available in a PKCS#12 keystore:
  87. ["source","java",subs="attributes,callouts,macros"]
  88. --------------------------------------------------
  89. include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-config-encrypted-communication]
  90. --------------------------------------------------
  91. The following is an example of setting up the client to trust the CA that has signed the certificate that Elasticsearch is using, when
  92. that CA certificate is available as a PEM encoded file.
  93. ["source","java",subs="attributes,callouts,macros"]
  94. --------------------------------------------------
  95. include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-config-trust-ca-pem]
  96. --------------------------------------------------
  97. When Elasticsearch is configured to require client TLS authentication, for example when a PKI realm is configured, the client needs to provide
  98. a client certificate during the TLS handshake in order to authenticate. The following is an example of setting up the client for TLS
  99. authentication with a certificate and a private key that are stored in a PKCS#12 keystore.
  100. ["source","java",subs="attributes,callouts,macros"]
  101. --------------------------------------------------
  102. include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-config-mutual-tls-authentication]
  103. --------------------------------------------------
  104. If the client certificate and key are not available in a keystore but rather as PEM encoded files, you cannot use them
  105. directly to build an SSLContext. You must rely on external libraries to parse the PEM key into a PrivateKey instance. Alternatively, you
  106. can use external tools to build a keystore from your PEM files, as shown in the following example:
  107. ```
  108. openssl pkcs12 -export -in client.crt -inkey private_key.pem \
  109. -name "client" -out client.p12
  110. ```
  111. If no explicit configuration is provided, the https://docs.oracle.com/javase/7/docs/technotes/guides/security/jsse/JSSERefGuide.html#CustomizingStores[system default configuration]
  112. will be used.
  113. === Others
  114. For any other required configuration needed, the Apache HttpAsyncClient docs
  115. should be consulted: https://hc.apache.org/httpcomponents-asyncclient-4.1.x/ .
  116. NOTE: If your application runs under the security manager you might be subject
  117. to the JVM default policies of caching positive hostname resolutions
  118. indefinitely and negative hostname resolutions for ten seconds. If the resolved
  119. addresses of the hosts to which you are connecting the client to vary with time
  120. then you might want to modify the default JVM behavior. These can be modified by
  121. adding
  122. https://docs.oracle.com/javase/8/docs/technotes/guides/net/properties.html[`networkaddress.cache.ttl=<timeout>`]
  123. and
  124. https://docs.oracle.com/javase/8/docs/technotes/guides/net/properties.html[`networkaddress.cache.negative.ttl=<timeout>`]
  125. to your
  126. https://docs.oracle.com/javase/8/docs/technotes/guides/security/PolicyFiles.html[Java
  127. security policy].
  128. === Node selector
  129. The client sends each request to one of the configured nodes in round-robin
  130. fashion. Nodes can optionally be filtered through a node selector that needs
  131. to be provided when initializing the client. This is useful when sniffing is
  132. enabled, in case only dedicated master nodes should be hit by HTTP requests.
  133. For each request the client will run the eventually configured node selector
  134. to filter the node candidates, then select the next one in the list out of the
  135. remaining ones.
  136. ["source","java",subs="attributes,callouts,macros"]
  137. --------------------------------------------------
  138. include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-init-allocation-aware-selector]
  139. --------------------------------------------------
  140. <1> Set an allocation aware node selector that allows to pick a node in the
  141. local rack if any available, otherwise go to any other node in any rack. It
  142. acts as a preference rather than a strict requirement, given that it goes to
  143. another rack if none of the local nodes are available, rather than returning
  144. no nodes in such case which would make the client forcibly revive a local node
  145. whenever none of the nodes from the preferred rack is available.
  146. WARNING: Node selectors that do not consistently select the same set of nodes
  147. will make round-robin behaviour unpredictable and possibly unfair. The
  148. preference example above is fine as it reasons about availability of nodes
  149. which already affects the predictability of round-robin. Node selection should
  150. not depend on other external factors or round-robin will not work properly.