@@ -46,7 +46,7 @@ to know more.
4646const { Client } = require('@elastic/elasticsearch')
4747const client = new Client({
4848 cloud: {
49- id: 'name:bG9jYWxob3N0JGFiY2QkZWZnaA==',
49+ id: '<cloud-id>'
5050 },
5151 auth: {
5252 username: 'elastic',
@@ -55,6 +55,152 @@ const client = new Client({
5555})
5656----
5757
58+ [discrete]
59+ [[connect-self-managed-new]]
60+ === Connecting to a self-managed cluster
61+
62+ By default {es} will start with security features like authentication and TLS
63+ enabled. To connect to the {es} cluster you'll need to configure the Node.js {es}
64+ client to use HTTPS with the generated CA certificate in order to make requests
65+ successfully.
66+
67+ If you're just getting started with {es} we recommend reading the documentation
68+ on https://www.elastic.co/guide/en/elasticsearch/reference/current/settings.html[configuring]
69+ and
70+ https://www.elastic.co/guide/en/elasticsearch/reference/current/starting-elasticsearch.html[starting {es}]
71+ to ensure your cluster is running as expected.
72+
73+ When you start {es} for the first time you'll see a distinct block like the one
74+ below in the output from {es} (you may have to scroll up if it's been a while):
75+
76+ [source,sh]
77+ ----
78+
79+ -> Elasticsearch security features have been automatically configured!
80+ -> Authentication is enabled and cluster connections are encrypted.
81+
82+ -> Password for the elastic user (reset with `bin/elasticsearch-reset-password -u elastic`):
83+ lhQpLELkjkrawaBoaz0Q
84+
85+ -> HTTP CA certificate SHA-256 fingerprint:
86+ a52dd93511e8c6045e21f16654b77c9ee0f34aea26d9f40320b531c474676228
87+ ...
88+
89+ ----
90+
91+ Depending on the circumstances there are two options for verifying the HTTPS
92+ connection, either verifying with the CA certificate itself or via the HTTP CA
93+ certificate fingerprint.
94+
95+ [discrete]
96+ [[auth-tls]]
97+ ==== TLS configuration
98+
99+ The generated root CA certificate can be found in the `certs` directory in your
100+ {es} config location (`$ES_CONF_PATH/certs/http_ca.crt`). If you're running {es}
101+ in Docker there is
102+ https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html[additional documentation for retrieving the CA certificate].
103+
104+ Without any additional configuration you can specify `https://` node urls, and
105+ the certificates used to sign these requests will be verified. To turn off
106+ certificate verification, you must specify an `tls` object in the top level
107+ config and set `rejectUnauthorized: false`. The default `tls` values are the
108+ same that Node.js's https://nodejs.org/api/tls.html#tls_tls_connect_options_callback[`tls.connect()`]
109+ uses.
110+
111+ [source,js]
112+ ----
113+ const { Client } = require('@elastic/elasticsearch')
114+ const client = new Client({
115+ node: 'https://localhost:9200',
116+ auth: {
117+ username: 'elastic',
118+ password: 'changeme'
119+ },
120+ tls: {
121+ ca: fs.readFileSync('./http_ca.crt'),
122+ rejectUnauthorized: false
123+ }
124+ })
125+ ----
126+
127+ [discrete]
128+ [[auth-ca-fingerprint]]
129+ ==== CA fingerprint
130+
131+ You can configure the client to only trust certificates that are signed by a specific CA certificate
132+ (CA certificate pinning) by providing a `caFingerprint` option.
133+ This will verify that the fingerprint of the CA certificate that has signed
134+ the certificate of the server matches the supplied value.
135+ You must configure a SHA256 digest.
136+
137+ [source,js]
138+ ----
139+ const { Client } = require('@elastic/elasticsearch')
140+ const client = new Client({
141+ node: 'https://example.com'
142+ auth: { ... },
143+ // the fingerprint (SHA256) of the CA certificate that is used to sign
144+ // the certificate that the Elasticsearch node presents for TLS.
145+ caFingerprint: '20:0D:CA:FA:76:...',
146+ tls: {
147+ // might be required if it's a self-signed certificate
148+ rejectUnauthorized: false
149+ }
150+ })
151+ ----
152+
153+ The certificate fingerprint can be calculated using `openssl x509` with the
154+ certificate file:
155+
156+ [source,sh]
157+ ----
158+ openssl x509 -fingerprint -sha256 -noout -in /path/to/http_ca.crt
159+ ----
160+
161+ If you don't have access to the generated CA file from {es} you can use the
162+ following script to output the root CA fingerprint of the {es} instance with
163+ `openssl s_client`:
164+
165+ [source,sh]
166+ ----
167+ # Replace the values of 'localhost' and '9200' to the
168+ # corresponding host and port values for the cluster.
169+ openssl s_client -connect localhost:9200 -servername localhost -showcerts </dev/null 2>/dev/null \
170+ | openssl x509 -fingerprint -sha256 -noout -in /dev/stdin
171+ ----
172+
173+ The output of `openssl x509` will look something like this:
174+
175+ [source,sh]
176+ ----
177+ SHA256 Fingerprint=A5:2D:D9:35:11:E8:C6:04:5E:21:F1:66:54:B7:7C:9E:E0:F3:4A:EA:26:D9:F4:03:20:B5:31:C4:74:67:62:28
178+ ----
179+
180+
181+ [discrete]
182+ [[connect-no-security]]
183+ === Connecting without security enabled
184+
185+ WARNING: Running {es} without security enabled is not recommended.
186+
187+ If your cluster is configured with
188+ https://www.elastic.co/guide/en/elasticsearch/reference/current/security-settings.html[security explicitly disabled]
189+ then you can connect via HTTP:
190+
191+ [source,js]
192+ ----
193+ const { Client } = require('@elastic/elasticsearch')
194+ const client = new Client({
195+ node: 'http://example.com'
196+ })
197+ ----
198+
199+ [discrete]
200+ [[auth-strategies]]
201+ === Authentication strategies
202+
203+ Following you can find all the supported authentication strategies.
58204
59205[discrete]
60206[[auth-apikey]]
@@ -150,57 +296,6 @@ const client = new Client({
150296----
151297
152298
153- [discrete]
154- [[auth-tls]]
155- ==== TLS configuration
156-
157- Without any additional configuration you can specify `https://` node urls, and
158- the certificates used to sign these requests will be verified. To turn off
159- certificate verification, you must specify an `tls` object in the top level
160- config and set `rejectUnauthorized: false`. The default `tls` values are the
161- same that Node.js's https://nodejs.org/api/tls.html#tls_tls_connect_options_callback[`tls.connect()`]
162- uses.
163-
164- [source,js]
165- ----
166- const { Client } = require('@elastic/elasticsearch')
167- const client = new Client({
168- node: 'https://localhost:9200',
169- auth: {
170- username: 'elastic',
171- password: 'changeme'
172- },
173- tls: {
174- ca: fs.readFileSync('./cacert.pem'),
175- rejectUnauthorized: false
176- }
177- })
178- ----
179-
180- [discrete]
181- [[auth-ca-fingerprint]]
182- ==== CA fingerprint
183-
184- You can configure the client to only trust certificates that are signed by a specific CA certificate ( CA certificate pinning ) by providing a `caFingerprint` option. This will verify that the fingerprint of the CA certificate that has signed the certificate of the server matches the supplied value.
185- a `caFingerprint` option, which will verify the supplied certificate authority fingerprint.
186- You must configure a SHA256 digest.
187-
188- [source,js]
189- ----
190- const { Client } = require('@elastic/elasticsearch')
191- const client = new Client({
192- node: 'https://example.com'
193- auth: { ... },
194- // the fingerprint (SHA256) of the CA certificate that is used to sign the certificate that the Elasticsearch node presents for TLS.
195- caFingerprint: '20:0D:CA:FA:76:...',
196- tls: {
197- // might be required if it's a self-signed certificate
198- rejectUnauthorized: false
199- }
200- })
201- ----
202-
203-
204299[discrete]
205300[[client-usage]]
206301=== Usage
@@ -212,7 +307,10 @@ and every method exposes the same signature.
212307[source,js]
213308----
214309const { Client } = require('@elastic/elasticsearch')
215- const client = new Client({ node: 'http://localhost:9200' })
310+ const client = new Client({
311+ cloud: { id: '<cloud-id>' },
312+ auth: { apiKey: 'base64EncodedKey' }
313+ })
216314
217315const result = await client.search({
218316 index: 'my-index',
@@ -229,7 +327,10 @@ you must specify `meta: true` in the request options:
229327[source,js]
230328----
231329const { Client } = require('@elastic/elasticsearch')
232- const client = new Client({ node: 'http://localhost:9200' })
330+ const client = new Client({
331+ cloud: { id: '<cloud-id>' },
332+ auth: { apiKey: 'base64EncodedKey' }
333+ })
233334
234335const result = await client.search({
235336 index: 'my-index',
@@ -266,7 +367,10 @@ CAUTION: If you abort a request, the request will fail with a
266367----
267368const AbortController = require('node-abort-controller')
268369const { Client } = require('@elastic/elasticsearch')
269- const client = new Client({ node: 'http://localhost:9200' })
370+ const client = new Client({
371+ cloud: { id: '<cloud-id>' },
372+ auth: { apiKey: 'base64EncodedKey' }
373+ })
270374
271375const abortController = new AbortController()
272376setImmediate(() => abortController.abort())
0 commit comments