|
| 1 | +import "dotenv/config"; |
| 2 | +import { Client } from "@elastic/elasticsearch"; |
| 3 | + |
| 4 | +// Have a look at the "Getting started" guide of the Elasticsearch node.js client |
| 5 | +// to learn more about how to configure these environment variables: |
| 6 | +// https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/getting-started-js.html |
| 7 | + |
| 8 | +const { |
| 9 | + // ES_NODE can include the username and password in the URL, e.g.: |
| 10 | + // ES_NODE=https://<USERNAME>:<PASSWORD>@<HOST>:9200 |
| 11 | + ES_NODE, |
| 12 | + // As an alternative to ES_NODE when using Elastic Cloud, you can use ES_CLOUD_ID and |
| 13 | + // set it to the Cloud ID that you can find in the cloud console of the deployment (https://cloud.elastic.co/). |
| 14 | + ES_CLOUD_ID, |
| 15 | + // ES_API_KEY can be used instead of username and password. |
| 16 | + // The API key will take precedence if both are set. |
| 17 | + ES_API_KEY, |
| 18 | + ES_USERNAME, |
| 19 | + ES_PASSWORD, |
| 20 | + // the fingerprint (SHA256) of the CA certificate that is used to sign |
| 21 | + // the certificate that the Elasticsearch node presents for TLS. |
| 22 | + ES_CA_FINGERPRINT, |
| 23 | + // Warning: This option should be considered an insecure workaround for local development only. |
| 24 | + // You may wish to specify a self-signed certificate rather than disabling certificate verification. |
| 25 | + // ES_UNSAFE_TLS_REJECT_UNAUTHORIZED can be set to FALSE to disable certificate verification. |
| 26 | + // See https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/client-connecting.html#auth-tls for more. |
| 27 | + ES_UNSAFE_TLS_REJECT_UNAUTHORIZED, |
| 28 | +} = process.env; |
| 29 | + |
| 30 | +if ((!ES_NODE && !ES_CLOUD_ID) || (ES_NODE && ES_CLOUD_ID)) |
| 31 | + throw new Error( |
| 32 | + "Either ES_NODE or ES_CLOUD_ID need to be defined, but not both.", |
| 33 | + ); |
| 34 | + |
| 35 | +const esUrl = ES_NODE ? new URL(ES_NODE) : undefined; |
| 36 | +const isHTTPS = esUrl?.protocol === "https:"; |
| 37 | +const isLocalhost = esUrl?.hostname === "localhost"; |
| 38 | + |
| 39 | +export const esClient = new Client({ |
| 40 | + ...(ES_NODE ? { node: ES_NODE } : {}), |
| 41 | + ...(ES_CLOUD_ID ? { cloud: { id: ES_CLOUD_ID } } : {}), |
| 42 | + ...(ES_CA_FINGERPRINT ? { caFingerprint: ES_CA_FINGERPRINT } : {}), |
| 43 | + ...(ES_API_KEY |
| 44 | + ? { |
| 45 | + auth: { |
| 46 | + apiKey: ES_API_KEY, |
| 47 | + }, |
| 48 | + } |
| 49 | + : {}), |
| 50 | + ...(!ES_API_KEY && ES_USERNAME && ES_PASSWORD |
| 51 | + ? { |
| 52 | + auth: { |
| 53 | + username: ES_USERNAME, |
| 54 | + password: ES_PASSWORD, |
| 55 | + }, |
| 56 | + } |
| 57 | + : {}), |
| 58 | + ...(isHTTPS && |
| 59 | + isLocalhost && |
| 60 | + ES_UNSAFE_TLS_REJECT_UNAUTHORIZED?.toLowerCase() === "false" |
| 61 | + ? { |
| 62 | + tls: { |
| 63 | + rejectUnauthorized: false, |
| 64 | + }, |
| 65 | + } |
| 66 | + : {}), |
| 67 | +}); |
0 commit comments