Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added username in config for redis auth #397

Merged
merged 1 commit into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ To enable the redis-based config:

When launched in redis-config mode, Refinery needs a redis host to use for managing the list of peers in the Refinery cluster. This hostname and port can be specified in one of two ways:

- set the `REFINERY_REDIS_HOST` environment variable (and optionally the `REFINERY_REDIS_PASSWORD` environment variable)
- set the `RedisHost` field in the config file (and optionally the `RedisPassword` field in the config file)
- set the `REFINERY_REDIS_HOST` environment variable (and optionally the `REFINERY_REDIS_USERNAME` and `REFINERY_REDIS_PASSWORD` environment variables)
- set the `RedisHost` field in the config file (and optionally the `RedisUsername` and `RedisPassword` fields in the config file)

The Redis host should be a hostname and a port, for example `redis.mydomain.com:6379`. The example config file has `localhost:6379` which obviously will not work with more than one host. When TLS is required to connect to the Redis instance, set the `UseTLS` config to `true`.

Expand Down
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ type Config interface {
// management.
GetRedisHost() (string, error)

// GetRedisUsername returns the username of a Redis instance to use for peer
// management.
GetRedisUsername() (string, error)

// GetRedisPassword returns the password of a Redis instance to use for peer
// management.
GetRedisPassword() (string, error)
Expand Down
17 changes: 17 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ func TestRedisHostEnvVar(t *testing.T) {
}
}

func TestRedisUsernameEnvVar(t *testing.T) {
const username = "admin"
const envVarName = "REFINERY_REDIS_USERNAME"
os.Setenv(envVarName, username)
defer os.Unsetenv(envVarName)

c, err := NewConfig("../config.toml", "../rules.toml", func(err error) {})

if err != nil {
t.Error(err)
}

if d, _ := c.GetRedisUsername(); d != username {
t.Error("received", d, "expected", username)
}
}

func TestRedisPasswordEnvVar(t *testing.T) {
const password = "admin1234"
const envVarName = "REFINERY_REDIS_PASSWORD"
Expand Down
9 changes: 9 additions & 0 deletions config/file_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type PeerManagementConfig struct {
Type string `validate:"required,oneof= file redis"`
Peers []string `validate:"dive,url"`
RedisHost string
RedisUsername string
RedisPassword string
UseTLS bool
UseTLSInsecure bool
Expand All @@ -96,6 +97,7 @@ func NewConfig(config, rules string, errorCallback func(error)) (Config, error)

c.BindEnv("GRPCListenAddr", "REFINERY_GRPC_LISTEN_ADDRESS")
c.BindEnv("PeerManagement.RedisHost", "REFINERY_REDIS_HOST")
c.BindEnv("PeerManagement.RedisUsername", "REFINERY_REDIS_USERNAME")
c.BindEnv("PeerManagement.RedisPassword", "REFINERY_REDIS_PASSWORD")
c.BindEnv("HoneycombLogger.LoggerAPIKey", "REFINERY_HONEYCOMB_API_KEY")
c.BindEnv("HoneycombMetrics.MetricsAPIKey", "REFINERY_HONEYCOMB_API_KEY")
Expand Down Expand Up @@ -414,6 +416,13 @@ func (f *fileConfig) GetRedisHost() (string, error) {
return f.config.GetString("PeerManagement.RedisHost"), nil
}

func (f *fileConfig) GetRedisUsername() (string, error) {
f.mux.RLock()
defer f.mux.RUnlock()

return f.config.GetString("PeerManagement.RedisUsername"), nil
}

func (f *fileConfig) GetRedisPassword() (string, error) {
f.mux.RLock()
defer f.mux.RUnlock()
Expand Down
8 changes: 8 additions & 0 deletions config/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ type MockConfig struct {
GetPeersVal []string
GetRedisHostErr error
GetRedisHostVal string
GetRedisUsernameErr error
GetRedisUsernameVal string
GetRedisPasswordErr error
GetRedisPasswordVal string
GetUseTLSErr error
Expand Down Expand Up @@ -173,6 +175,12 @@ func (m *MockConfig) GetRedisHost() (string, error) {

return m.GetRedisHostVal, m.GetRedisHostErr
}
func (m *MockConfig) GetRedisUsername() (string, error) {
m.Mux.RLock()
defer m.Mux.RUnlock()

return m.GetRedisUsernameVal, m.GetRedisUsernameErr
}
func (m *MockConfig) GetRedisPassword() (string, error) {
m.Mux.RLock()
defer m.Mux.RUnlock()
Expand Down
6 changes: 6 additions & 0 deletions config_complete.toml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ Metrics = "honeycomb"
# Not eligible for live reload.
# RedisHost = "localhost:6379"

# RedisUsername is the username used to connect to redis for peer cluster membership management.
# If the environment variable 'REFINERY_REDIS_USERNAME' is set it takes
# precedence and this value is ignored.
# Not eligible for live reload.
# RedisUsername = ""

# RedisPassword is the password used to connect to redis for peer cluster membership management.
# If the environment variable 'REFINERY_REDIS_PASSWORD' is set it takes
# precedence and this value is ignored.
Expand Down
7 changes: 6 additions & 1 deletion internal/peer/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func newRedisPeers(c config.Config) (Peers, error) {
// a 1 second delay between attempts to allow the redis process to init
var (
conn redis.Conn
err error
err error
)
for timeout := time.After(10 * time.Second); ; {
select {
Expand Down Expand Up @@ -190,6 +190,11 @@ func buildOptions(c config.Config) []redis.DialOption {
redis.DialDatabase(0), // TODO enable multiple databases for multiple samproxies
}

username, _ := c.GetRedisUsername()
if username != "" {
options = append(options, redis.DialUsername(username))
}

password, _ := c.GetRedisPassword()
if password != "" {
options = append(options, redis.DialPassword(password))
Expand Down