Skip to content

Commit d36104f

Browse files
authored
fix: user/pass in redis URI support (#305)
This changes the code to only override/set username and pass if they are explicitly set, otherwise leaving the `opts` unmodified. I also updated the log line to ensure that credentials are not logged in plaintext during the info log statement. Fixes #304 Signed-off-by: Micah Nagel <micah.nagel@defenseunicorns.com>
1 parent f5cb100 commit d36104f

3 files changed

Lines changed: 31 additions & 6 deletions

File tree

internal/oidc/redis.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,12 @@ func NewRedisClient(config *oidc.RedisConfig) (redis.Cmdable, error) {
7272
if err != nil {
7373
return nil, fmt.Errorf("parsing redis URL: %w", err)
7474
}
75-
opts.Username = config.GetUsername()
76-
opts.Password = config.GetPassword()
75+
if username := config.GetUsername(); username != "" {
76+
opts.Username = username
77+
}
78+
if password := config.GetPassword(); password != "" {
79+
opts.Password = password
80+
}
7781

7882
log.Info("connecting to redis",
7983
"addr", opts.Addr,

internal/oidc/redis_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ func TestRedisAuth(t *testing.T) {
6969
_, err = NewRedisStore(&Clock{}, client, 0, 1*time.Minute)
7070
require.NoError(t, err)
7171
})
72+
73+
t.Run("credentials-in-url", func(t *testing.T) {
74+
// Test credentials in URL (the fix for issue #304)
75+
client, err := NewRedisClient(&oidc.RedisConfig{
76+
ServerUri: "redis://redis-user:redis-pass@" + mr.Addr(),
77+
})
78+
require.NoError(t, err)
79+
80+
_, err = NewRedisStore(&Clock{}, client, 0, 1*time.Minute)
81+
require.NoError(t, err)
82+
})
7283
}
7384

7485
func TestRedisTLS(t *testing.T) {

internal/oidc/session.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"sync"
2727
"time"
2828

29+
"github.com/redis/go-redis/v9"
2930
"github.com/tetratelabs/run"
3031
"github.com/tetratelabs/telemetry"
3132
"golang.org/x/oauth2"
@@ -129,10 +130,19 @@ func (s *sessionStoreFactory) PreRun() error {
129130
// loadRedisConfig loads the Redis configuration from the OIDCConfig and initializes or updates
130131
// the Redis session store.
131132
func (s *sessionStoreFactory) loadRedisConfig(cfg *oidcv1.OIDCConfig) error {
132-
s.log.Info("configuring redis session store",
133-
"redis-url", cfg.GetRedisSessionStoreConfig().GetServerUri(),
134-
"client-id", cfg.GetClientId(),
135-
)
133+
// Parse the Redis URL to extract host and port for logging (without credentials)
134+
parseOpts, parseErr := redis.ParseURL(cfg.GetRedisSessionStoreConfig().GetServerUri())
135+
if parseErr == nil {
136+
s.log.Info("configuring redis session store",
137+
"redis-url", parseOpts.Addr,
138+
"client-id", cfg.GetClientId(),
139+
)
140+
} else {
141+
// If URL parsing fails, log without the URL
142+
s.log.Info("configuring redis session store",
143+
"client-id", cfg.GetClientId(),
144+
)
145+
}
136146
client, err := NewRedisClient(cfg.GetRedisSessionStoreConfig())
137147
if err != nil {
138148
return err

0 commit comments

Comments
 (0)