Redis TLS configuration with Nodejs
Asked Answered
H

2

10

I'm using ioRedis node package for connecting my node js application to redis server which is secured by TLS. Redis version I'm using is Redis 6.0. My server is running fine with the certificates but while connecting from node application I get Error.

 Redis({
          host: "localhost",
          port: 6379,
          tls: {
            key: fs.readFileSync('./redis.key'),
            cert: fs.readFileSync('./redis.crt'),
            maxVersion: 'TLSv1.3',
            minVersion: 'TLSv1.3',
            ca: [fs.readFileSync('./redis.pem')]
          }
        })

Error on the nodejs application side is

This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason:
   Error: read ECONNRESET
            at TCP.onStreamRead (internal/stream_base_commons.js:205:27)

Error on server while trying to connect from nodejs application

17:29:44.295 # Error accepting a client connection: error:1408F10B:SSL routines:ssl3_get_record:wrong version number

Objective is just to have a redis connection with TLS security.

Highflier answered 12/3, 2020 at 0:31 Comment(1)
I am asking the same question in GitHub: github.com/luin/ioredis/issues/1076 Maybe you can comment there too.Mezoff
F
22

I was also doing the same thing and I try this approach (node-redis v2.8.0):

const redis = require('redis');
const fs = require('fs');

const client = redis.createClient({
    host: '<hostname>',
    port: <port>,
    tls: {}
});

Instead of passing the certificate key and everything try to pass the tls as an empty object. The guide for this approach is as follow. https://docs.upstash.com/docs/howto/connectwithtls

Fou answered 26/3, 2021 at 3:56 Comment(2)
Updated link to docs: upstash.com/docs/redis/howto/connectwithtlsMatronage
Original post specifically states "I'm using ioRedis" which is not the same package as your example.Miki
E
0

I think you need to specify which encoding the files need to be read in

const redis = require('ioredis');
const fs = require('fs');

const client = redis.createClient({
    host: 'hostName',
    port: 'port',
    tls: {
       key: fs.readFileSync('pathToFile', 'ascii')  /* this is usually the encoding */
       cert: fs.readFileSync('pathToFile', 'ascii')
       ca: fs.readFileSync('pathToFile', 'ascii')  /* this is usually the encoding */
    }

})

find more info here

Ettieettinger answered 7/1, 2022 at 0:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.