Frozendawn
Frozendawn

Reputation: 123

node.js elasticsearch self signed certificate in certificate chain

I've been trying to learn elasticsearch and decided to try to connect it with node.js. I have a elasticsearch running + a index I created named test-idx. I'm following the documentation of elasticsearch to connect and create a document however when I run my code I get 'ConnectionError: self signed certificate in certificate chain' followed by a huge meta object.

const client = new elasticsearch.Client({
    node: 'https://localhost:9200',
    auth: {
      username: 'elastic',
      password: '123456'
    }
})

client.index({
    index: 'test-idx',
    document: {
        field: 'test123'
    }
  })

I tried adding when creating the instance of the Client but it didn't seem to help

tls: {
    rejectUnauthorized: false
  }

Upvotes: 1

Views: 3867

Answers (2)

Abbas Tolgay Yılmaz
Abbas Tolgay Yılmaz

Reputation: 93

instead of accepting unauthorized when auth provided, you can create a certificate for elasticsearch and include that in your nestjs app.

By default cert file reside wherever elastic setup is: eg. elasticsearch/config/certs/http_ca.crt

if you are using dockerized one, copy like this to outside of container:

docker cp <elastic-container-name>:/usr/share/elasticsearch/config/certs/http_ca.crt .

import * as fs from 'fs';
import * as path from 'path';

const certificatePath = path.resolve(process.cwd(), '<path-to-your-crt-file-from-app-root');

...
 auth: {
        username: 'elastic',
        password: 'password'
      },
      tls: {
        ca: fs.readFileSync(certificatePath)
      }
...

Upvotes: 0

Rahul Kumar
Rahul Kumar

Reputation: 76

I'm my case it's Working. I'm using node with nest js

tls: { rejectUnauthorized: false }

Upvotes: 4

Related Questions