Skip to content

Commit afd0c87

Browse files
authored
chore: use tls.ConnectionOptions instead of tls.SecureContextOptions for TLS options (#1312)
Allows sentinel connections to use all available TLS options ConnectionOptions is used by tls.connect() ConnectionOptions extends SecureContextOptions, CommonConnectionOptions
1 parent 41ca587 commit afd0c87

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

lib/connectors/SentinelConnector/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
sample,
77
Debug,
88
} from "../../utils";
9-
import { connect as createTLSConnection, SecureContextOptions } from "tls";
9+
import { connect as createTLSConnection, ConnectionOptions } from "tls";
1010
import {
1111
ITcpConnectionOptions,
1212
isIIpcConnectionOptions,
@@ -42,7 +42,7 @@ export interface ISentinelConnectionOptions extends ITcpConnectionOptions {
4242
preferredSlaves?: PreferredSlaves;
4343
connectTimeout?: number;
4444
enableTLSForSentinelMode?: boolean;
45-
sentinelTLS?: SecureContextOptions;
45+
sentinelTLS?: ConnectionOptions;
4646
natMap?: INatMap;
4747
updateSentinels?: boolean;
4848
}

lib/connectors/StandaloneConnector.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createConnection, TcpNetConnectOpts, IpcNetConnectOpts } from "net";
2-
import { connect as createTLSConnection, SecureContextOptions } from "tls";
2+
import { connect as createTLSConnection, ConnectionOptions } from "tls";
33
import { CONNECTION_CLOSED_ERROR_MSG } from "../utils";
44
import AbstractConnector, { ErrorEmitter } from "./AbstractConnector";
55
import { NetStream } from "../types";
@@ -11,11 +11,11 @@ export function isIIpcConnectionOptions(
1111
}
1212

1313
export interface ITcpConnectionOptions extends TcpNetConnectOpts {
14-
tls?: SecureContextOptions;
14+
tls?: ConnectionOptions;
1515
}
1616

1717
export interface IIpcConnectionOptions extends IpcNetConnectOpts {
18-
tls?: SecureContextOptions;
18+
tls?: ConnectionOptions;
1919
}
2020

2121
export default class StandaloneConnector extends AbstractConnector {

test/functional/tls.ts

+21-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ describe("tls option", () => {
1515
// @ts-ignore
1616
expect(op.ca).to.eql("123");
1717
// @ts-ignore
18+
expect(op.servername).to.eql("localhost");
19+
// @ts-ignore
20+
expect(op.rejectUnauthorized).to.eql(false);
21+
// @ts-ignore
1822
expect(op.port).to.eql(6379);
1923
const stream = net.createConnection(op);
2024
stream.on("connect", (data) => {
@@ -23,7 +27,9 @@ describe("tls option", () => {
2327
return stream;
2428
});
2529

26-
redis = new Redis({ tls: { ca: "123" } });
30+
redis = new Redis({
31+
tls: { ca: "123", servername: "localhost", rejectUnauthorized: false },
32+
});
2733
redis.on("ready", () => {
2834
redis.disconnect();
2935
stub.restore();
@@ -68,6 +74,10 @@ describe("tls option", () => {
6874
const stub = sinon.stub(tls, "connect").callsFake((op) => {
6975
// @ts-ignore
7076
expect(op.ca).to.eql("123");
77+
// @ts-ignore
78+
expect(op.servername).to.eql("localhost");
79+
// @ts-ignore
80+
expect(op.rejectUnauthorized).to.eql(false);
7181
redis.disconnect();
7282
stub.restore();
7383
process.nextTick(done);
@@ -77,7 +87,7 @@ describe("tls option", () => {
7787
redis = new Redis({
7888
sentinels: [{ port: 27379 }],
7989
name: "my",
80-
tls: { ca: "123" },
90+
tls: { ca: "123", servername: "localhost", rejectUnauthorized: false },
8191
enableTLSForSentinelMode: true,
8292
});
8393
});
@@ -96,6 +106,10 @@ describe("tls option", () => {
96106
// @ts-ignore
97107
expect(op.ca).to.eql("123");
98108
// @ts-ignore
109+
expect(op.servername).to.eql("localhost");
110+
// @ts-ignore
111+
expect(op.rejectUnauthorized).to.eql(false);
112+
// @ts-ignore
99113
expect(op.port).to.eql(27379);
100114
const stream = net.createConnection(op);
101115
stream.on("connect", (data) => {
@@ -107,7 +121,11 @@ describe("tls option", () => {
107121
redis = new Redis({
108122
sentinels: [{ port: 27379 }],
109123
name: "my",
110-
sentinelTLS: { ca: "123" },
124+
sentinelTLS: {
125+
ca: "123",
126+
servername: "localhost",
127+
rejectUnauthorized: false,
128+
},
111129
});
112130
redis.on("ready", () => {
113131
redis.disconnect();

test/unit/connectors/connector.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,16 @@ describe("StandaloneConnector", () => {
3232
const spy = sinon.spy(tls, "connect");
3333
const connector = new StandaloneConnector({
3434
port: 6379,
35-
tls: { ca: "on" },
35+
tls: { ca: "on", servername: "localhost", rejectUnauthorized: false },
3636
});
3737
await connector.connect(() => {});
3838
expect(spy.calledOnce).to.eql(true);
39-
expect(spy.firstCall.args[0]).to.eql({ port: 6379, ca: "on" });
39+
expect(spy.firstCall.args[0]).to.eql({
40+
port: 6379,
41+
ca: "on",
42+
servername: "localhost",
43+
rejectUnauthorized: false,
44+
});
4045
connector.disconnect();
4146
});
4247
});

0 commit comments

Comments
 (0)