Skip to content

Commit c37fefd

Browse files
authored
feat: Add support for setting the redis username when using redis persistence. (#142)
**Requirements** - [ ] I have added test coverage for new or changed functionality - [x] I have followed the repository's [pull request submission guidelines](../blob/main/CONTRIBUTING.md#submitting-pull-requests) - [x] I have validated my changes against all supported platform versions **Describe the solution you've provided** `eredis` supports taking in a username for connections in addition to the password. The default is `undefined`, which will remain unchanged unless `redis_password` is explicitly configured.
1 parent 3cf3b94 commit c37fefd

File tree

4 files changed

+8
-3
lines changed

4 files changed

+8
-3
lines changed

src/ldclient_config.erl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
redis_host => string(),
6969
redis_port => pos_integer(),
7070
redis_database => integer(),
71+
redis_username => string() | undefined,
7172
redis_password => string(),
7273
redis_prefix => string(),
7374
redis_tls => [ssl:tls_option()] | undefined,
@@ -112,6 +113,7 @@
112113
-define(DEFAULT_REDIS_HOST, "127.0.0.1").
113114
-define(DEFAULT_REDIS_PORT, 6379).
114115
-define(DEFAULT_REDIS_DATABASE, 0).
116+
-define(DEFAULT_REDIS_USERNAME, undefined).
115117
-define(DEFAULT_REDIS_PASSWORD, "").
116118
-define(DEFAULT_REDIS_PREFIX, "launchdarkly").
117119
-define(DEFAULT_REDIS_TLS, undefined).
@@ -185,6 +187,7 @@ parse_options(SdkKey, Options) when is_list(SdkKey), is_map(Options) ->
185187
RedisHost = maps:get(redis_host, Options, ?DEFAULT_REDIS_HOST),
186188
RedisPort = maps:get(redis_port, Options, ?DEFAULT_REDIS_PORT),
187189
RedisDatabase = maps:get(redis_database, Options, ?DEFAULT_REDIS_DATABASE),
190+
RedisUsername = maps:get(redis_username, Options, ?DEFAULT_REDIS_USERNAME),
188191
RedisPassword = maps:get(redis_password, Options, ?DEFAULT_REDIS_PASSWORD),
189192
RedisPrefix = maps:get(redis_prefix, Options, ?DEFAULT_REDIS_PREFIX),
190193
CacheTtl = maps:get(cache_ttl, Options, ?DEFAULT_CACHE_TTL),
@@ -218,6 +221,7 @@ parse_options(SdkKey, Options) when is_list(SdkKey), is_map(Options) ->
218221
redis_host => RedisHost,
219222
redis_port => RedisPort,
220223
redis_database => RedisDatabase,
224+
redis_username => RedisUsername,
221225
redis_password => RedisPassword,
222226
redis_prefix => RedisPrefix,
223227
redis_tls => RedisTls,

src/ldclient_instance.erl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
redis_host => string(),
3131
redis_port => pos_integer(),
3232
redis_database => integer(),
33+
redis_username => string() | undefined,
3334
redis_password => string(),
3435
redis_prefix => string(),
3536
redis_tls => [ssl:tls_option()] | undefined,

src/ldclient_storage_redis_server.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,12 @@ init([Tag]) ->
5757
Host = ldclient_config:get_value(Tag, redis_host),
5858
Port = ldclient_config:get_value(Tag, redis_port),
5959
Database = ldclient_config:get_value(Tag, redis_database),
60+
Username = ldclient_config:get_value(Tag, redis_username),
6061
Password = ldclient_config:get_value(Tag, redis_password),
6162
Prefix = ldclient_config:get_value(Tag, redis_prefix),
6263
TlsOpts = ldclient_config:get_value(Tag, redis_tls),
6364
BasicOpts = [
64-
{host, Host}, {port, Port}, {database, Database}, {password, Password}
65+
{host, Host}, {port, Port}, {database, Database}, {username, Username}, {password, Password}
6566
],
6667
EredisOpts = set_tls_options(BasicOpts, TlsOpts),
6768
{ok, Client} = eredis:start_link(EredisOpts),
@@ -341,4 +342,3 @@ get_init(Client, Prefix) ->
341342
undefined -> false;
342343
_ -> true
343344
end.
344-

test-usage/ldclient_usage.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ init_with_all_options() ->
3232
redis_host => "redis_host",
3333
redis_port => 9900,
3434
redis_database => 0,
35+
redis_username => "username",
3536
redis_password => "password",
3637
redis_prefix => "prefix",
3738
redis_tls => [
@@ -69,4 +70,3 @@ use_variation_with_all_types_context() ->
6970
<<"object">> => #{<<"a">> => <<"b">>}
7071
},
7172
ldclient_context:new(<<"org-key">>, <<"org">>)))))))), false).
72-

0 commit comments

Comments
 (0)