diff --git a/workhorse/_support/lint_last_known_acceptable_go1.21.txt b/workhorse/_support/lint_last_known_acceptable_go1.21.txt index 0911364d46f6d612c164c70b803a0e6db54ef615..89c077e70f338df5074afc5493064dd98715a2b7 100644 --- a/workhorse/_support/lint_last_known_acceptable_go1.21.txt +++ b/workhorse/_support/lint_last_known_acceptable_go1.21.txt @@ -181,10 +181,6 @@ internal/redis/redis.go:18:2: blank-imports: a blank import should be only in a internal/redis/redis.go:23:24: var-declaration: should omit type error from declaration of var errSentinelMasterAddr; it will be inferred from the right-hand side (revive) internal/redis/redis.go:25:2: exported: exported var TotalConnections should have comment or be unexported (revive) internal/redis/redis.go:110:10: elseif: can replace 'else {if cond {}}' with 'else if cond {}' (gocritic) -internal/redis/redis_test.go:24:2: error-nil: use require.NoError (testifylint) -internal/redis/redis_test.go:29:3: error-nil: use require.NoError (testifylint) -internal/redis/redis_test.go:79:15: `initialise` is a misspelling of `initialize` (misspell) -internal/redis/redis_test.go:119:15: `initialise` is a misspelling of `initialize` (misspell) internal/senddata/contentprocessor/contentprocessor.go:136:35: response body must be closed (bodyclose) internal/sendfile/sendfile_test.go:180:34: response body must be closed (bodyclose) internal/sendurl/sendurl.go:100: Function 'Inject' has too many statements (51 > 40) (funlen) diff --git a/workhorse/_support/lint_last_known_acceptable_go1.22.txt b/workhorse/_support/lint_last_known_acceptable_go1.22.txt index 98734858876c6222b8c0045dda441c646f4a0134..b623020d97b36bcc72c57e02ee57891601cde7ca 100644 --- a/workhorse/_support/lint_last_known_acceptable_go1.22.txt +++ b/workhorse/_support/lint_last_known_acceptable_go1.22.txt @@ -181,10 +181,6 @@ internal/redis/redis.go:18:2: blank-imports: a blank import should be only in a internal/redis/redis.go:23:24: var-declaration: should omit type error from declaration of var errSentinelMasterAddr; it will be inferred from the right-hand side (revive) internal/redis/redis.go:25:2: exported: exported var TotalConnections should have comment or be unexported (revive) internal/redis/redis.go:110:10: elseif: can replace 'else {if cond {}}' with 'else if cond {}' (gocritic) -internal/redis/redis_test.go:24:2: error-nil: use require.NoError (testifylint) -internal/redis/redis_test.go:29:3: error-nil: use require.NoError (testifylint) -internal/redis/redis_test.go:79:15: `initialise` is a misspelling of `initialize` (misspell) -internal/redis/redis_test.go:119:15: `initialise` is a misspelling of `initialize` (misspell) internal/senddata/contentprocessor/contentprocessor.go:136:35: response body must be closed (bodyclose) internal/sendfile/sendfile_test.go:180:34: response body must be closed (bodyclose) internal/sendurl/sendurl.go:100: Function 'Inject' has too many statements (51 > 40) (funlen) diff --git a/workhorse/internal/redis/redis.go b/workhorse/internal/redis/redis.go index b4262f3a4593f9bcca608d69557d7b30c3b30394..68fa8829d82b11a6a1b960695b8eed8cfbf356fb 100644 --- a/workhorse/internal/redis/redis.go +++ b/workhorse/internal/redis/redis.go @@ -176,7 +176,9 @@ func configureRedis(cfg *config.RedisConfig) (*redis.Client, error) { } opt.DB = getOrDefault(cfg.DB, 0) - opt.Password = cfg.Password + if cfg.Password != "" { + opt.Password = cfg.Password + } opt.PoolSize = getOrDefault(cfg.MaxActive, defaultMaxActive) opt.MaxIdleConns = getOrDefault(cfg.MaxIdle, defaultMaxIdle) diff --git a/workhorse/internal/redis/redis_test.go b/workhorse/internal/redis/redis_test.go index ab258c524b820a66de85824538254f79858c5fa7..88dc73aed6623d06f28e7674e8ff3e0a622d7f68 100644 --- a/workhorse/internal/redis/redis_test.go +++ b/workhorse/internal/redis/redis_test.go @@ -2,10 +2,12 @@ package redis import ( "context" + "fmt" "net" "sync/atomic" "testing" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitlab/workhorse/internal/config" @@ -21,12 +23,12 @@ const ( func mockRedisServer(t *testing.T, connectReceived *atomic.Value) string { ln, err := net.Listen("tcp", "127.0.0.1:0") - require.Nil(t, err) + require.NoError(t, err) go func() { defer ln.Close() conn, err := ln.Accept() - require.Nil(t, err) + assert.NoError(t, err) connectReceived.Store(true) conn.Write([]byte("OK\n")) }() @@ -48,7 +50,11 @@ func TestConfigureConfigWithoutRedis(t *testing.T) { func TestConfigureValidConfigX(t *testing.T) { testCases := []struct { - scheme string + scheme string + username string + urlPassword string + redisPassword string + expectedPassword string }{ { scheme: "redis", @@ -59,6 +65,24 @@ func TestConfigureValidConfigX(t *testing.T) { { scheme: "tcp", }, + { + scheme: "redis", + username: "redis-user", + urlPassword: "redis-password", + expectedPassword: "redis-password", + }, + { + scheme: "redis", + redisPassword: "override-password", + expectedPassword: "override-password", + }, + { + scheme: "redis", + username: "redis-user", + urlPassword: "redis-password", + redisPassword: "override-password", + expectedPassword: "override-password", + }, } for _, tc := range testCases { @@ -66,8 +90,18 @@ func TestConfigureValidConfigX(t *testing.T) { connectReceived := atomic.Value{} a := mockRedisServer(t, &connectReceived) - parsedURL := helper.URLMustParse(tc.scheme + "://" + a) - redisCfg := &config.RedisConfig{URL: config.TomlURL{URL: *parsedURL}} + var u string + if tc.username != "" || tc.urlPassword != "" { + u = fmt.Sprintf("%s://%s:%s@%s", tc.scheme, tc.username, tc.urlPassword, a) + } else { + u = fmt.Sprintf("%s://%s", tc.scheme, a) + } + + parsedURL := helper.URLMustParse(u) + redisCfg := &config.RedisConfig{ + URL: config.TomlURL{URL: *parsedURL}, + Password: tc.redisPassword, + } cfg := &config.Config{Redis: redisCfg} rdb, err := Configure(cfg) @@ -75,8 +109,11 @@ func TestConfigureValidConfigX(t *testing.T) { defer rdb.Close() require.NotNil(t, rdb.Conn(), "Pool should not be nil") + opt := rdb.Options() + require.Equal(t, tc.username, opt.Username) + require.Equal(t, tc.expectedPassword, opt.Password) - // goredis initialise connections lazily + // goredis initialize connections lazily rdb.Ping(context.Background()) require.True(t, connectReceived.Load().(bool)) }) @@ -116,7 +153,7 @@ func TestConnectToSentinel(t *testing.T) { require.NotNil(t, rdb.Conn(), "Pool should not be nil") - // goredis initialise connections lazily + // goredis initialize connections lazily rdb.Ping(context.Background()) require.True(t, connectReceived.Load().(bool)) })