Skip to content

Commit c4c2f08

Browse files
authored
Allow overriding the Redis password via REDIS_PASSWORD (#521)
- The Redis producer reads its password from the REDIS_PASSWORD environment variable when set, falling back to the config-file value.
1 parent 9ac0b6f commit c4c2f08

3 files changed

Lines changed: 31 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ For ease of installation and operation, run Fleet Telemetry on Kubernetes or a s
8484
"redis": { // Redis pub/sub config
8585
"addrs": ["redis:6379"], // one or more addresses (cluster/sentinel supported)
8686
"username": string - optional,
87-
"password": string - optional,
87+
"password": string - optional; overridden by the REDIS_PASSWORD environment variable when set,
8888
"db": int - optional,
8989
"subscriber_set_prefix": string - optional; names the per-VIN subscriber sorted set. When empty, the sorted set is not consulted,
9090
"publish_vin_topics": bool - additionally publish each record to the VIN channel namespace_topic_{vin},

config/config.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,15 @@ func (r *Redis) options() (*goredis.UniversalOptions, error) {
191191
if err != nil {
192192
return nil, err
193193
}
194+
password := r.Password
195+
if envPassword := os.Getenv("REDIS_PASSWORD"); envPassword != "" {
196+
password = envPassword
197+
}
198+
194199
options := &goredis.UniversalOptions{
195200
Addrs: r.Addrs,
196201
Username: r.Username,
197-
Password: r.Password,
202+
Password: password,
198203
DB: r.DB,
199204
TLSConfig: tlsConfig,
200205
}

config/config_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,30 @@ var _ = Describe("Test full application config", func() {
223223
})
224224
})
225225

226+
Context("configure redis", func() {
227+
AfterEach(func() {
228+
_ = os.Unsetenv("REDIS_PASSWORD")
229+
})
230+
231+
It("uses the password from config when REDIS_PASSWORD is unset", func() {
232+
redis := &Redis{Addrs: []string{"redis:6379"}, Password: "config-password"}
233+
234+
options, err := redis.options()
235+
Expect(err).NotTo(HaveOccurred())
236+
Expect(options.Password).To(Equal("config-password"))
237+
})
238+
239+
It("overrides the password with the REDIS_PASSWORD env variable when set", func() {
240+
err := os.Setenv("REDIS_PASSWORD", "env-password")
241+
Expect(err).NotTo(HaveOccurred())
242+
redis := &Redis{Addrs: []string{"redis:6379"}, Password: "config-password"}
243+
244+
options, err := redis.options()
245+
Expect(err).NotTo(HaveOccurred())
246+
Expect(options.Password).To(Equal("env-password"))
247+
})
248+
})
249+
226250
Context("VinsToTrack", func() {
227251

228252
AfterEach(func() {

0 commit comments

Comments
 (0)