From 7e8dde7e26741f4fb2b42d99c43422bb47f8c652 Mon Sep 17 00:00:00 2001 From: Rory Z Date: Tue, 24 Aug 2021 15:56:17 +0800 Subject: [PATCH] chore: fix dialyzer error Signed-off-by: zhanghongtong --- apps/emqx_authz/src/emqx_authz.erl | 5 ++- apps/emqx_authz/src/emqx_authz_rule.erl | 7 +--- apps/emqx_authz/test/emqx_authz_SUITE.erl | 9 ++--- apps/emqx_authz/test/emqx_authz_api_SUITE.erl | 35 ++----------------- .../emqx_authz/test/emqx_authz_http_SUITE.erl | 2 -- .../test/emqx_authz_redis_SUITE.erl | 2 -- .../src/emqx_connector_mongo.erl | 4 ++- 7 files changed, 11 insertions(+), 53 deletions(-) diff --git a/apps/emqx_authz/src/emqx_authz.erl b/apps/emqx_authz/src/emqx_authz.erl index 3c8a56629..f158322e1 100644 --- a/apps/emqx_authz/src/emqx_authz.erl +++ b/apps/emqx_authz/src/emqx_authz.erl @@ -233,7 +233,6 @@ create_resource(#{type := DB, {error, Reason} -> {error, Reason} end. --spec(init_provider(rule()) -> rule()). init_provider(#{enable := true, type := file, path := Path @@ -301,7 +300,7 @@ init_provider(#{enable := false} = Rule) ->Rule. -> {stop, allow} | {ok, deny}). authorize(#{username := Username, peerhost := IpAddress - } = Client, PubSub, Topic, _DefaultResult, Rules) -> + } = Client, PubSub, Topic, DefaultResult, Rules) -> case do_authorize(Client, PubSub, Topic, Rules) of {matched, allow} -> ?LOG(info, "Client succeeded authorization: Username: ~p, IP: ~p, Topic: ~p, Permission: allow", [Username, IpAddress, Topic]), @@ -313,7 +312,7 @@ authorize(#{username := Username, {stop, deny}; nomatch -> ?LOG(info, "Client failed authorization: Username: ~p, IP: ~p, Topic: ~p, Reasion: ~p", [Username, IpAddress, Topic, "no-match rule"]), - {stop, deny} + {stop, DefaultResult} end. do_authorize(Client, PubSub, Topic, diff --git a/apps/emqx_authz/src/emqx_authz_rule.erl b/apps/emqx_authz/src/emqx_authz_rule.erl index 4786bf39d..deb8968c6 100644 --- a/apps/emqx_authz/src/emqx_authz_rule.erl +++ b/apps/emqx_authz/src/emqx_authz_rule.erl @@ -70,11 +70,6 @@ atom(B) when is_binary(B) -> catch _ -> binary_to_atom(B) end; -atom(L) when is_list(L) -> - try list_to_existing_atom(L) - catch - _ -> list_to_atom(L) - end; atom(A) when is_atom(A) -> A. bin(L) when is_list(L) -> @@ -84,7 +79,7 @@ bin(B) when is_binary(B) -> -spec(matches(emqx_types:clientinfo(), emqx_types:pubsub(), emqx_types:topic(), [rule()]) -> {matched, allow} | {matched, deny} | nomatch). -matches(Client, PubSub, Topic, []) -> nomatch; +matches(_Client, _PubSub, _Topic, []) -> nomatch; matches(Client, PubSub, Topic, [{Permission, Who, Action, TopicFilters} | Tail]) -> case match(Client, PubSub, Topic, {Permission, Who, Action, TopicFilters}) of nomatch -> matches(Client, PubSub, Topic, Tail); diff --git a/apps/emqx_authz/test/emqx_authz_SUITE.erl b/apps/emqx_authz/test/emqx_authz_SUITE.erl index 30848f3d1..a766ecd82 100644 --- a/apps/emqx_authz/test/emqx_authz_SUITE.erl +++ b/apps/emqx_authz/test/emqx_authz_SUITE.erl @@ -33,6 +33,7 @@ groups() -> init_per_suite(Config) -> meck:new(emqx_resource, [non_strict, passthrough, no_history, no_link]), meck:expect(emqx_resource, create, fun(_, _, _) -> {ok, meck_data} end), + meck:expect(emqx_resource, update, fun(_, _, _, _) -> {ok, meck_data} end), meck:expect(emqx_resource, remove, fun(_) -> ok end ), ok = emqx_config:init_load(emqx_authz_schema, ?CONF_DEFAULT), @@ -110,10 +111,7 @@ t_update_rule(_) -> {ok, _} = emqx_authz:update(head, [?RULE1]), {ok, _} = emqx_authz:update(tail, [?RULE3]), - dbg:tracer(),dbg:p(all,c), - dbg:tpl(hocon_schema, check, cx), - Lists1 = emqx_authz:check_rules([?RULE1, ?RULE2, ?RULE3]), - ?assertMatch(Lists1, emqx:get_config([authorization_rules, rules], [])), + ?assertMatch([#{type := http}, #{type := mongo}, #{type := mysql}], emqx:get_config([authorization_rules, rules], [])), [#{annotations := #{id := Id1}, type := http}, #{annotations := #{id := Id2}, type := mongo}, @@ -122,8 +120,7 @@ t_update_rule(_) -> {ok, _} = emqx_authz:update({replace_once, Id1}, ?RULE5), {ok, _} = emqx_authz:update({replace_once, Id3}, ?RULE4), - Lists2 = emqx_authz:check_rules([?RULE1, ?RULE2, ?RULE4]), - ?assertMatch(Lists2, emqx:get_config([authorization_rules, rules], [])), + ?assertMatch([#{type := redis}, #{type := mongo}, #{type := pgsql}], emqx:get_config([authorization_rules, rules], [])), [#{annotations := #{id := Id1}, type := redis}, #{annotations := #{id := Id2}, type := mongo}, diff --git a/apps/emqx_authz/test/emqx_authz_api_SUITE.erl b/apps/emqx_authz/test/emqx_authz_api_SUITE.erl index 77d620ccb..56d0170c1 100644 --- a/apps/emqx_authz/test/emqx_authz_api_SUITE.erl +++ b/apps/emqx_authz/test/emqx_authz_api_SUITE.erl @@ -37,37 +37,6 @@ -define(API_VERSION, "v5"). -define(BASE_PATH, "api"). -% -define(RULE1, #{<<"principal">> => <<"all">>, -% <<"topics">> => [<<"#">>], -% <<"action">> => <<"all">>, -% <<"permission">> => <<"deny">>} -% ). -% -define(RULE2, #{<<"principal">> => -% #{<<"ipaddress">> => <<"127.0.0.1">>}, -% <<"topics">> => -% [#{<<"eq">> => <<"#">>}, -% #{<<"eq">> => <<"+">>} -% ] , -% <<"action">> => <<"all">>, -% <<"permission">> => <<"allow">>} -% ). -% -define(RULE3,#{<<"principal">> => -% #{<<"and">> => [#{<<"username">> => <<"^test?">>}, -% #{<<"clientid">> => <<"^test?">>} -% ]}, -% <<"topics">> => [<<"test">>], -% <<"action">> => <<"publish">>, -% <<"permission">> => <<"allow">>} -% ). -% -define(RULE4,#{<<"principal">> => -% #{<<"or">> => [#{<<"username">> => <<"^test">>}, -% #{<<"clientid">> => <<"test?">>} -% ]}, -% <<"topics">> => [<<"%u">>,<<"%c">>], -% <<"action">> => <<"publish">>, -% <<"permission">> => <<"deny">>} -% ). - -define(RULE1, #{<<"type">> => <<"http">>, <<"config">> => #{ <<"url">> => <<"https://fake.com:443/">>, @@ -119,8 +88,7 @@ }). all() -> - % emqx_ct:all(?MODULE). - []. + emqx_ct:all(?MODULE). groups() -> []. @@ -129,6 +97,7 @@ init_per_suite(Config) -> meck:new(emqx_resource, [non_strict, passthrough, no_history, no_link]), meck:expect(emqx_resource, create, fun(_, _, _) -> {ok, meck_data} end), meck:expect(emqx_resource, update, fun(_, _, _, _) -> {ok, meck_data} end), + meck:expect(emqx_resource, health_check, fun(_) -> ok end), meck:expect(emqx_resource, remove, fun(_) -> ok end ), ekka_mnesia:start(), diff --git a/apps/emqx_authz/test/emqx_authz_http_SUITE.erl b/apps/emqx_authz/test/emqx_authz_http_SUITE.erl index c7a3fc449..bbd4232dd 100644 --- a/apps/emqx_authz/test/emqx_authz_http_SUITE.erl +++ b/apps/emqx_authz/test/emqx_authz_http_SUITE.erl @@ -21,8 +21,6 @@ -include("emqx_authz.hrl"). -include_lib("eunit/include/eunit.hrl"). -include_lib("common_test/include/ct.hrl"). --define(CONF_DEFAULT, <<"authorization: {rules: []}">>). - -define(CONF_DEFAULT, <<"authorization_rules: {rules: []}">>). all() -> diff --git a/apps/emqx_authz/test/emqx_authz_redis_SUITE.erl b/apps/emqx_authz/test/emqx_authz_redis_SUITE.erl index 073c339ae..0da931cc7 100644 --- a/apps/emqx_authz/test/emqx_authz_redis_SUITE.erl +++ b/apps/emqx_authz/test/emqx_authz_redis_SUITE.erl @@ -21,8 +21,6 @@ -include("emqx_authz.hrl"). -include_lib("eunit/include/eunit.hrl"). -include_lib("common_test/include/ct.hrl"). --define(CONF_DEFAULT, <<"authorization: {rules: []}">>). - -define(CONF_DEFAULT, <<"authorization_rules: {rules: []}">>). all() -> diff --git a/apps/emqx_connector/src/emqx_connector_mongo.erl b/apps/emqx_connector/src/emqx_connector_mongo.erl index 6f69fafc4..c4953c3fb 100644 --- a/apps/emqx_connector/src/emqx_connector_mongo.erl +++ b/apps/emqx_connector/src/emqx_connector_mongo.erl @@ -84,7 +84,9 @@ mongo_fields() -> nullable => true}} , {database, fun emqx_connector_schema_lib:database/1} , {topology, #{type => hoconsc:ref(?MODULE, topology), - nullable => true}} + default => #{}}} + %% TODO: Does the ref type support nullable=ture ? + % nullable => true}} ] ++ emqx_connector_schema_lib:ssl_fields().