Skip to content

Commit

Permalink
feat: support rediss:// URL
Browse files Browse the repository at this point in the history
  • Loading branch information
monkbroc authored and luin committed Jul 31, 2019
1 parent fd25e89 commit 371bb9c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ new Redis({
});
```

You can also specify connection options as a [`redis://` URL](http://www.iana.org/assignments/uri-schemes/prov/redis):
You can also specify connection options as a [`redis://` URL](http://www.iana.org/assignments/uri-schemes/prov/redis) or [`rediss://` URL](https://www.iana.org/assignments/uri-schemes/prov/rediss) when using [TLS encryption](#tls-options):

```javascript
// Connect to 127.0.0.1:6380, db 4, using password "authpassword":
Expand Down Expand Up @@ -703,6 +703,12 @@ var redis = new Redis({
});
```

Alternatively, specify the connection through a [`rediss://` URL](https://www.iana.org/assignments/uri-schemes/prov/rediss).

```javascript
var redis = new Redis("rediss://redis.my-service.com");
```

<hr>

## Sentinel
Expand Down
1 change: 1 addition & 0 deletions lib/redis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ var debug = Debug("redis");
* var unixSocketRedis2 = new Redis('/tmp/echo.sock');
* var urlRedis = new Redis('redis://user:password@redis-service.com:6379/');
* var urlRedis2 = new Redis('//localhost:6379');
* var urlRedisTls = new Redis('rediss://user:password@redis-service.com:6379/');
* var authedRedis = new Redis(6380, '192.168.100.1', { password: 'password' });
* ```
*/
Expand Down
5 changes: 4 additions & 1 deletion lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export function parseURL(url) {
result.password = parsed.auth.split(":")[1];
}
if (parsed.pathname) {
if (parsed.protocol === "redis:") {
if (parsed.protocol === "redis:" || parsed.protocol === "rediss:") {
if (parsed.pathname.length > 1) {
result.db = parsed.pathname.slice(1);
}
Expand All @@ -274,6 +274,9 @@ export function parseURL(url) {
if (parsed.port) {
result.port = parsed.port;
}
if (parsed.protocol === "rediss:") {
result.tls = true;
}
defaults(result, parsed.query);

return result;
Expand Down
10 changes: 10 additions & 0 deletions test/unit/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,16 @@ describe("utils", function() {
expect(utils.parseURL("redis://127.0.0.1/")).to.eql({
host: "127.0.0.1"
});
expect(
utils.parseURL("rediss://user:pass@127.0.0.1:6380/4?key=value")
).to.eql({
tls: true,
host: "127.0.0.1",
port: "6380",
db: "4",
password: "pass",
key: "value"
});
});
});

Expand Down

2 comments on commit 371bb9c

@ahimta
Copy link

@ahimta ahimta commented on 371bb9c Aug 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much:smiley:. This is very useful because someone using a redis URL and then changing it to a rediss URL won't need to make any changes or diagnose the problem and try to find out why they can't connect anymore:smiley:.

@giedrioks
Copy link

@giedrioks giedrioks commented on 371bb9c Aug 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did someone realised, that this change have broke previous functionality?

I did a small code snippet and tested with 4.13.0 and 4.14.0. Same code works with earlier version, but fails with latest.


const redis = new Redis('rediss://username:password@some_domain.databases.appdomain.cloud:30268', 
{ tls: { ca: Buffer.from('cert_base_64_string', 'base64') } });

redis.on('connect', () => { 
    console.log('[REDIS][READY] Redis is ready');
    redis.set('123', 'vaslue')
    .then(() => {
        redis.get('123').then(data => console.log('get: ', data));
    });
    
});
redis.on('reconnecting', (delay, attempt) => console.log('[REDIS][RECONNECT]', { delay, attempt }));
redis.on('end', () => console.log('[REDIS][DISCONNECTED]'));
redis.on('error', err => console.log('[REDIS][ERROR]', err));

Results:
with 4.13.0

[REDIS][READY] Redis is ready
get:  vaslue

with 4.14.0

[REDIS][ERROR] { Error: self signed certificate in certificate chain
    at TLSSocket.onConnectSecure (_tls_wrap.js:1051:34)
    at TLSSocket.emit (events.js:189:13)
    at TLSSocket._finishInit (_tls_wrap.js:633:8) code: 'SELF_SIGNED_CERT_IN_CHAIN' }

Please sign in to comment.