From 1bf86250dd80763532e9a67f55e1058465ce0f7e Mon Sep 17 00:00:00 2001 From: Serge Tupchii Date: Wed, 30 Aug 2023 20:56:28 +0300 Subject: [PATCH] fix(emqx_bridge_cassandra): allow cassandra bridge without username/password Cassandra can be used without credentials, if it is configured with AllowAllAuthenticator (default). --- .../src/emqx_bridge_cassandra.app.src | 2 +- .../src/emqx_bridge_cassandra_connector.erl | 23 +++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/apps/emqx_bridge_cassandra/src/emqx_bridge_cassandra.app.src b/apps/emqx_bridge_cassandra/src/emqx_bridge_cassandra.app.src index de790ab46..26028e8ab 100644 --- a/apps/emqx_bridge_cassandra/src/emqx_bridge_cassandra.app.src +++ b/apps/emqx_bridge_cassandra/src/emqx_bridge_cassandra.app.src @@ -1,6 +1,6 @@ {application, emqx_bridge_cassandra, [ {description, "EMQX Enterprise Cassandra Bridge"}, - {vsn, "0.1.3"}, + {vsn, "0.1.4"}, {registered, []}, {applications, [ kernel, diff --git a/apps/emqx_bridge_cassandra/src/emqx_bridge_cassandra_connector.erl b/apps/emqx_bridge_cassandra/src/emqx_bridge_cassandra_connector.erl index 2cbf0d6fe..0610ee743 100644 --- a/apps/emqx_bridge_cassandra/src/emqx_bridge_cassandra_connector.erl +++ b/apps/emqx_bridge_cassandra/src/emqx_bridge_cassandra_connector.erl @@ -94,7 +94,6 @@ on_start( #{ servers := Servers0, keyspace := Keyspace, - username := Username, pool_size := PoolSize, ssl := SSL } = Config @@ -114,12 +113,12 @@ on_start( Options = [ {nodes, Servers}, - {username, Username}, - {password, emqx_secret:wrap(maps:get(password, Config, ""))}, {keyspace, Keyspace}, {auto_reconnect, ?AUTO_RECONNECT_INTERVAL}, {pool_size, PoolSize} ], + Options1 = maybe_add_opt(username, Config, Options), + Options2 = maybe_add_opt(password, Config, Options1, _IsSensitive = true), SslOpts = case maps:get(enable, SSL) of @@ -132,7 +131,7 @@ on_start( [] end, State = parse_prepare_cql(Config), - case emqx_resource_pool:start(InstId, ?MODULE, Options ++ SslOpts) of + case emqx_resource_pool:start(InstId, ?MODULE, Options2 ++ SslOpts) of ok -> {ok, init_prepare(State#{pool_name => InstId, prepare_statement => #{}})}; {error, Reason} -> @@ -513,3 +512,19 @@ maybe_assign_type(V) when is_integer(V) -> maybe_assign_type(V) when is_float(V) -> {double, V}; maybe_assign_type(V) -> V. + +maybe_add_opt(Key, Conf, Opts) -> + maybe_add_opt(Key, Conf, Opts, _IsSensitive = false). + +maybe_add_opt(Key, Conf, Opts, IsSensitive) -> + case Conf of + #{Key := Val} -> + [{Key, maybe_wrap(IsSensitive, Val)} | Opts]; + _ -> + Opts + end. + +maybe_wrap(false = _IsSensitive, Val) -> + Val; +maybe_wrap(true, Val) -> + emqx_secret:wrap(Val).