Skip to content

Commit 8545937

Browse files
[Backport 8.0] Update connecting documentation (#1667)
Co-authored-by: Tomas Della Vedova <delvedor@users.noreply.github.com>
1 parent e68dec8 commit 8545937

25 files changed

+315
-100
lines changed

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ We recommend that you write a lightweight proxy that uses this client instead, y
9595
'use strict'
9696

9797
const { Client } = require('@elastic/elasticsearch')
98-
const client = new Client({ node: 'http://localhost:9200' })
98+
const client = new Client({
99+
cloud: { id: '<cloud-id>' },
100+
auth: { apiKey: 'base64EncodedKey' }
101+
})
99102

100103
async function run () {
101104
// Let's start by indexing some data
@@ -165,8 +168,14 @@ You will require the packages from your code by using the alias you have defined
165168
const { Client: Client6 } = require('es6')
166169
const { Client: Client7 } = require('es7')
167170

168-
const client6 = new Client6({ node: 'http://localhost:9200' })
169-
const client7 = new Client7({ node: 'http://localhost:9201' })
171+
const client6 = new Client6({
172+
cloud: { id: '<cloud-id>' },
173+
auth: { apiKey: 'base64EncodedKey' }
174+
})
175+
const client7 = new Client7({
176+
cloud: { id: '<cloud-id>' },
177+
auth: { apiKey: 'base64EncodedKey' }
178+
})
170179

171180
client6.info().then(console.log, console.log)
172181
client7.info().then(console.log, console.log)

docs/advanced-config.asciidoc

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ class MyConnectionPool extends ConnectionPool {
3030
}
3131
3232
const client = new Client({
33-
ConnectionPool: MyConnectionPool
33+
ConnectionPool: MyConnectionPool,
34+
cloud: { id: '<cloud-id>' },
35+
auth: { apiKey: 'base64EncodedKey' }
3436
})
3537
----
3638

@@ -54,7 +56,9 @@ class MyConnection extends BaseConnection {
5456
}
5557
5658
const client = new Client({
57-
Connection: MyConnection
59+
Connection: MyConnection,
60+
cloud: { id: '<cloud-id>' },
61+
auth: { apiKey: 'base64EncodedKey' }
5862
})
5963
----
6064

@@ -81,7 +85,9 @@ class MySerializer extends Serializer {
8185
}
8286
8387
const client = new Client({
84-
Serializer: MySerializer
88+
Serializer: MySerializer,
89+
cloud: { id: '<cloud-id>' },
90+
auth: { apiKey: 'base64EncodedKey' }
8591
})
8692
----
8793

docs/basic-config.asciidoc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ offers.
1010
const { Client } = require('@elastic/elasticsearch')
1111
1212
const client = new Client({
13-
node: 'http://localhost:9200',
13+
cloud: { id: '<cloud-id>' },
14+
auth: { apiKey: 'base64EncodedKey' }
1415
maxRetries: 5,
1516
requestTimeout: 60000,
1617
sniffOnStart: true
@@ -241,7 +242,7 @@ _Cloud configuration example:_
241242
----
242243
const client = new Client({
243244
cloud: {
244-
id: 'name:bG9jYWxob3N0JGFiY2QkZWZnaA=='
245+
id: '<cloud-id>'
245246
},
246247
auth: {
247248
username: 'elastic',

docs/child.asciidoc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ will be closed.
2222
[source,js]
2323
----
2424
const { Client } = require('@elastic/elasticsearch')
25-
const client = new Client({ node: 'http://localhost:9200' })
25+
const client = new Client({
26+
cloud: { id: '<cloud-id>' },
27+
auth: { apiKey: 'base64EncodedKey' }
28+
})
2629
const child = client.child({
2730
headers: { 'x-foo': 'bar' },
2831
requestTimeout: 1000

docs/connecting.asciidoc

Lines changed: 159 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ to know more.
4646
const { Client } = require('@elastic/elasticsearch')
4747
const 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
----
214309
const { 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
217315
const 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
----
231329
const { 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
234335
const 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
----
267368
const AbortController = require('node-abort-controller')
268369
const { 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
271375
const abortController = new AbortController()
272376
setImmediate(() => abortController.abort())

docs/examples/asStream.asciidoc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ data.
99
'use strict'
1010
1111
const { Client } = require('@elastic/elasticsearch')
12-
const client = new Client({ node: 'http://localhost:9200' })
12+
const client = new Client({
13+
cloud: { id: '<cloud-id>' },
14+
auth: { apiKey: 'base64EncodedKey' }
15+
})
1316
1417
async function run () {
1518
const bulkResponse = await client.bulk({
@@ -83,7 +86,10 @@ send it directly to another source.
8386
'use strict'
8487
8588
const { Client } = require('@elastic/elasticsearch')
86-
const client = new Client({ node: 'http://localhost:9200' })
89+
const client = new Client({
90+
cloud: { id: '<cloud-id>' },
91+
auth: { apiKey: 'base64EncodedKey' }
92+
})
8793
const fastify = require('fastify')()
8894
8995
fastify.post('/search/:index', async (req, reply) => {

docs/examples/bulk.asciidoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ NOTE: Did you know that we provide an helper for sending bulk request? You can f
1313
require('array.prototype.flatmap').shim()
1414
const { Client } = require('@elastic/elasticsearch')
1515
const client = new Client({
16-
node: 'http://localhost:9200'
16+
cloud: { id: '<cloud-id>' },
17+
auth: { apiKey: 'base64EncodedKey' }
1718
})
1819
1920
async function run () {

docs/examples/exists.asciidoc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ NOTE: Since this API uses the `HEAD` method, the body value will be boolean.
1010
'use strict'
1111
1212
const { Client } = require('@elastic/elasticsearch')
13-
const client = new Client({ node: 'http://localhost:9200' })
13+
const client = new Client({
14+
cloud: { id: '<cloud-id>' },
15+
auth: { apiKey: 'base64EncodedKey' }
16+
})
1417
1518
async function run () {
1619
await client.index({

0 commit comments

Comments
 (0)