1

The problem:

It is not obvious how to correctly authenticate with:

  • sentinels
  • redis instances themselves

when using the ServiceStack.Redis solution.

According to the docs a proper way to provide a password for redis/sentinel is to set it via URLs in the following manner:

var sentinel = new RedisSentinel(new[] {"password@localhost:26381"})

This works fine, but how do I provide a password for the redis connections themselves? The password I've specified in the URL will not be copied over to redis connections.

I've found a workaround, that seems to be working, but I'm not sure if it will actually cover all the possible cases:

//Get the original factory
var clientFactory = RedisConfig.ClientFactory;
//Decorate it with the logic that sets a password. 
RedisConfig.ClientFactory = c =>
{
    c.Password = "password";
    return clientFactory.Invoke(c);
};

//Continue to initialize sentinels
var sentinel = new RedisSentinel(new[] {"password@localhost:26379"});

It seems like this will actually fix the problem and will provide a password to the connections to redis instances (not sentinels).

The question is: is this the recommended way of doing it? Or is there a better approach?

Dmitry Kotov
  • 375
  • 3
  • 9

1 Answers1

1

You can use a RedisSentinel HostFilter to customize the connection string for the individual hosts the sentinel connects to, e.g:

sentinel.HostFilter = host => $"password@{host}";
mythz
  • 141,670
  • 29
  • 246
  • 390
  • Hi mythz. Will it apply to both sentinels and redis servers or only to sentinels? – Dmitry Kotov Apr 20 '21 at 15:16
  • If I understood correctly - this is how it is supposed to look like var sentinel = new RedisSentinel(new[] {"sentinelpassword@localhost:26379"}) { HostFilter = host => $"redispassword@{host}", }; Is that correct? – Dmitry Kotov Apr 20 '21 at 15:26
  • @DmitryKotov No it's just a filter for redis instance hosts, yes you'd need to configure both. – mythz Apr 20 '21 at 15:28
  • As a side question in the same topic - will the same sentinel password apply for sentinels discovered by `ScanForOtherSentinels` feature? – Dmitry Kotov Apr 20 '21 at 15:32
  • @DmitryKotov It doesn't so I've just added a `SentinelHostFilter` in [this commit](https://github.com/ServiceStack/ServiceStack.Redis/commit/771870d7c65880c9bb461242cffd5b52ac57a170) for the next version (should be out tomorrow). – mythz Apr 20 '21 at 15:37