Skip to content

Commit ad5b455

Browse files
authored
feat: add auth support for Redis 6 (#1130)
1 parent a345103 commit ad5b455

File tree

11 files changed

+260
-53
lines changed

11 files changed

+260
-53
lines changed

Diff for: examples/basic_operations.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const Redis = require("ioredis");
22
const redis = new Redis({
33
port: process.env.redisPort,
44
host: process.env.redisEndpoint,
5+
username: process.env.redisUsername,
56
password: process.env.redisPW,
67
});
78

Diff for: lib/cluster/ClusterSubscriber.ts

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export default class ClusterSubscriber {
7474
this.subscriber = new Redis({
7575
port: options.port,
7676
host: options.host,
77+
username: options.username,
7778
password: options.password,
7879
enableReadyCheck: true,
7980
connectionName: SUBSCRIBER_CONNECTION_NAME,

Diff for: lib/cluster/util.ts

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export type NodeRole = "master" | "slave" | "all";
77
export interface IRedisOptions {
88
port: number;
99
host: string;
10+
username?: string;
1011
password?: string;
1112
[key: string]: any;
1213
}

Diff for: lib/connectors/SentinelConnector/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export { ISentinelAddress, SentinelIterator };
3535
export interface ISentinelConnectionOptions extends ITcpConnectionOptions {
3636
role: "master" | "slave";
3737
name: string;
38+
sentinelUsername?: string;
3839
sentinelPassword?: string;
3940
sentinels: Array<Partial<ISentinelAddress>>;
4041
sentinelRetryStrategy?: (retryAttempts: number) => number | void | null;
@@ -278,6 +279,7 @@ export default class SentinelConnector extends AbstractConnector {
278279
const client = new Redis({
279280
port: endpoint.port || 26379,
280281
host: endpoint.host,
282+
username: this.options.sentinelUsername || null,
281283
password: this.options.sentinelPassword || null,
282284
family:
283285
endpoint.family ||

Diff for: lib/redis/RedisOptions.ts

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface IRedisOptions
1414
keepAlive?: number;
1515
noDelay?: boolean;
1616
connectionName?: string;
17+
username?: string;
1718
password?: string;
1819
db?: number;
1920
dropBufferSupport?: boolean;
@@ -50,6 +51,7 @@ export const DEFAULT_REDIS_OPTIONS: IRedisOptions = {
5051
enableTLSForSentinelMode: false,
5152
updateSentinels: true,
5253
// Status
54+
username: null,
5355
password: null,
5456
db: 0,
5557
// Others

Diff for: lib/redis/event_handler.ts

+20-4
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,29 @@ export function connectHandler(self) {
2525
return;
2626
}
2727
if (err) {
28-
if (err.message.indexOf("no password is set") === -1) {
29-
flushed = true;
30-
self.recoverFromFatalError(err, err);
31-
} else {
28+
if (err.message.indexOf("no password is set") !== -1) {
3229
console.warn(
3330
"[WARN] Redis server does not require a password, but a password was supplied."
3431
);
32+
} else if (
33+
err.message.indexOf(
34+
"without any password configured for the default user"
35+
) !== -1
36+
) {
37+
console.warn(
38+
"[WARN] This Redis server's `default` user does not require a password, but a password was supplied"
39+
);
40+
} else if (
41+
err.message.indexOf(
42+
"wrong number of arguments for 'auth' command"
43+
) !== -1
44+
) {
45+
console.warn(
46+
`[ERROR] The server returned "wrong number of arguments for 'auth' command". You are probably passing both username and password to Redis version 5 or below. You should only pass the 'password' option for Redis version 5 and under.`
47+
);
48+
} else {
49+
flushed = true;
50+
self.recoverFromFatalError(err, err);
3551
}
3652
}
3753
});

Diff for: lib/redis/index.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const debug = Debug("redis");
4242
* it to reduce the latency.
4343
* @param {string} [options.connectionName=null] - Connection name.
4444
* @param {number} [options.db=0] - Database index to use.
45+
* @param {string} [options.username=null] - If set, client will send AUTH command with this user and password when connected.
4546
* @param {string} [options.password=null] - If set, client will send AUTH command
4647
* with the value of this option when connected.
4748
* @param {boolean} [options.dropBufferSupport=false] - Drop the buffer support for better performance.
@@ -277,7 +278,10 @@ Redis.prototype.connect = function (callback) {
277278

278279
this.condition = {
279280
select: options.db,
280-
auth: options.password,
281+
auth:
282+
options.username
283+
? [options.username, options.password]
284+
: options.password,
281285
subscriber: false,
282286
};
283287

Diff for: lib/utils/index.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,11 @@ export function parseURL(url) {
257257

258258
const result: any = {};
259259
if (parsed.auth) {
260-
result.password = parsed.auth.split(":")[1];
260+
const parsedAuth = parsed.auth.split(":");
261+
if (parsedAuth[0]) {
262+
result.username = parsedAuth[0];
263+
}
264+
result.password = parsedAuth[1];
261265
}
262266
if (parsed.pathname) {
263267
if (parsed.protocol === "redis:" || parsed.protocol === "rediss:") {

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
],
99
"scripts": {
1010
"test": "TS_NODE_TRANSPILE_ONLY=true TS_NODE_LOG_ERROR=true NODE_ENV=test mocha \"test/**/*.ts\"",
11+
"test-single": "TS_NODE_TRANSPILE_ONLY=true TS_NODE_LOG_ERROR=true NODE_ENV=test mocha $1",
1112
"lint": "eslint --ext .js,.ts .",
1213
"format": "prettier --write \"{,!(node_modules)/**/}*.{js,ts}\"",
1314
"format-check": "prettier --check \"{,!(node_modules)/**/}*.{js,ts}\"",

0 commit comments

Comments
 (0)