From 848d49e70f2aa2755a7e57a3f110320e591af76a Mon Sep 17 00:00:00 2001 From: Matheus Macabu Date: Wed, 12 Mar 2025 08:26:41 +0100 Subject: [PATCH] Chore: Add username option for redis remote cache (#101787) * Chore: Add username option for redis remote cache (cherry picked from commit 25e28dc85e646e8cb7ab9ab582de8fb247b58b07) * Chore: Update docs and config with sample Redis conn with user+pass --------- Co-authored-by: Thomas Fournier --- conf/defaults.ini | 2 +- conf/sample.ini | 2 +- docs/sources/setup-grafana/configure-grafana/_index.md | 4 +++- pkg/infra/remotecache/redis_storage.go | 2 ++ pkg/infra/remotecache/redis_storage_test.go | 3 ++- 5 files changed, 9 insertions(+), 4 deletions(-) diff --git a/conf/defaults.ini b/conf/defaults.ini index 8fc8296fead..ba04540e320 100644 --- a/conf/defaults.ini +++ b/conf/defaults.ini @@ -198,7 +198,7 @@ type = database # cache connectionstring options # database: will use Grafana primary database. -# redis: config like redis server e.g. `addr=127.0.0.1:6379,pool_size=100,db=0,ssl=false`. Only addr is required. ssl may be 'true', 'false', or 'insecure'. +# redis: config like redis server e.g. `addr=127.0.0.1:6379,pool_size=100,db=0,username=grafana,password=grafanaRocks,ssl=false`. Only addr is required. ssl may be 'true', 'false', or 'insecure'. # memcache: 127.0.0.1:11211 connstr = diff --git a/conf/sample.ini b/conf/sample.ini index 152fbf6fb96..15f94510ba6 100644 --- a/conf/sample.ini +++ b/conf/sample.ini @@ -197,7 +197,7 @@ # cache connectionstring options # database: will use Grafana primary database. -# redis: config like redis server e.g. `addr=127.0.0.1:6379,pool_size=100,db=0,ssl=false`. Only addr is required. ssl may be 'true', 'false', or 'insecure'. +# redis: config like redis server e.g. `addr=127.0.0.1:6379,pool_size=100,db=0,username=grafana,password=grafanaRocks,ssl=false`. Only addr is required. ssl may be 'true', 'false', or 'insecure'. # memcache: 127.0.0.1:11211 ;connstr = diff --git a/docs/sources/setup-grafana/configure-grafana/_index.md b/docs/sources/setup-grafana/configure-grafana/_index.md index 9f16b072e63..c77d5cab8d7 100644 --- a/docs/sources/setup-grafana/configure-grafana/_index.md +++ b/docs/sources/setup-grafana/configure-grafana/_index.md @@ -491,11 +491,13 @@ Leave empty when using `database` and Grafana uses the primary database. ##### `redis` -Example connection string: `addr=127.0.0.1:6379,pool_size=100,db=0,ssl=false` +Example connection string: `addr=127.0.0.1:6379,pool_size=100,db=0,username=grafana,password=grafanaRocks,ssl=false` - `addr` is the host `:` port of the Redis server. - `pool_size` (optional) is the number of underlying connections that can be made to Redis. - `db` (optional) is the number identifier of the Redis database you want to use. +- `username` (optional) is the connection identifier to authenticate the current connection. +- `password` (optional) is the connection secret to authenticate the current connection. - `ssl` (optional) is if SSL should be used to connect to Redis server. The value may be `true`, `false`, or `insecure`. Setting the value to `insecure` skips verification of the certificate chain and hostname when making the connection. ##### `memcache` diff --git a/pkg/infra/remotecache/redis_storage.go b/pkg/infra/remotecache/redis_storage.go index 84c9a081f81..de10aa88eb3 100644 --- a/pkg/infra/remotecache/redis_storage.go +++ b/pkg/infra/remotecache/redis_storage.go @@ -39,6 +39,8 @@ func parseRedisConnStr(connStr string) (*redis.Options, error) { switch connKey { case "addr": options.Addr = connVal + case "username": + options.Username = connVal case "password": options.Password = connVal case "db": diff --git a/pkg/infra/remotecache/redis_storage_test.go b/pkg/infra/remotecache/redis_storage_test.go index 32138431f21..6f796bd5ab6 100644 --- a/pkg/infra/remotecache/redis_storage_test.go +++ b/pkg/infra/remotecache/redis_storage_test.go @@ -16,11 +16,12 @@ func Test_parseRedisConnStr(t *testing.T) { ShouldErr bool }{ "all redis options should parse": { - "addr=127.0.0.1:6379,pool_size=100,db=1,password=grafanaRocks,ssl=false", + "addr=127.0.0.1:6379,pool_size=100,db=1,username=grafana,password=grafanaRocks,ssl=false", &redis.Options{ Addr: "127.0.0.1:6379", PoolSize: 100, DB: 1, + Username: "grafana", Password: "grafanaRocks", Network: "tcp", TLSConfig: nil,