emqx_authz_pgsql_SUITE.erl 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2021 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %% http://www.apache.org/licenses/LICENSE-2.0
  8. %%
  9. %% Unless required by applicable law or agreed to in writing, software
  10. %% distributed under the License is distributed on an "AS IS" BASIS,
  11. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. %% See the License for the specific language governing permissions and
  13. %% limitations under the License.
  14. %%--------------------------------------------------------------------
  15. -module(emqx_authz_pgsql_SUITE).
  16. -compile(nowarn_export_all).
  17. -compile(export_all).
  18. -include("emqx_authz.hrl").
  19. -include_lib("eunit/include/eunit.hrl").
  20. -include_lib("common_test/include/ct.hrl").
  21. all() ->
  22. emqx_ct:all(?MODULE).
  23. groups() ->
  24. [].
  25. init_per_suite(Config) ->
  26. meck:new(emqx_resource, [non_strict, passthrough, no_history, no_link]),
  27. meck:expect(emqx_resource, check_and_create, fun(_, _, _) -> {ok, meck_data} end ),
  28. ok = emqx_ct_helpers:start_apps([emqx_authz], fun set_special_configs/1),
  29. Config.
  30. end_per_suite(_Config) ->
  31. file:delete(filename:join(emqx:get_env(plugins_etc_dir), 'authz.conf')),
  32. emqx_ct_helpers:stop_apps([emqx_authz, emqx_resource]),
  33. meck:unload(emqx_resource).
  34. set_special_configs(emqx) ->
  35. application:set_env(emqx, allow_anonymous, false),
  36. application:set_env(emqx, enable_acl_cache, false),
  37. application:set_env(emqx, acl_nomatch, deny),
  38. application:set_env(emqx, plugins_loaded_file,
  39. emqx_ct_helpers:deps_path(emqx, "test/loaded_plguins")),
  40. ok;
  41. set_special_configs(emqx_authz) ->
  42. Rules = [#{config =>#{<<"meck">> => <<"fake">>},
  43. principal => all,
  44. sql => <<"fake sql">>,
  45. type => pgsql}
  46. ],
  47. emqx_config:put([emqx_authz], #{rules => Rules}),
  48. ok;
  49. set_special_configs(_App) ->
  50. ok.
  51. -define(COLUMNS, [ {column, <<"ipaddress">>, meck, meck, meck, meck, meck, meck, meck}
  52. , {column, <<"username">>, meck, meck, meck, meck, meck, meck, meck}
  53. , {column, <<"clientid">>, meck, meck, meck, meck, meck, meck, meck}
  54. , {column, <<"action">>, meck, meck, meck, meck, meck, meck, meck}
  55. , {column, <<"permission">>, meck, meck, meck, meck, meck, meck, meck}
  56. , {column, <<"topic">>, meck, meck, meck, meck, meck, meck, meck}
  57. ]).
  58. -define(RULE1, [{<<"127.0.0.1">>, <<>>, <<>>, <<"all">>, <<"deny">>, <<"#">>}]).
  59. -define(RULE2, [{<<"127.0.0.1">>, <<>>, <<>>, <<"all">>, <<"allow">>, <<"eq #">>}]).
  60. -define(RULE3, [{<<>>, <<"^test">>, <<"^test">> ,<<"subscribe">>, <<"allow">>, <<"test/%c">>}]).
  61. -define(RULE4, [{<<>>, <<"^test">>, <<"^test">> ,<<"publish">>, <<"allow">>, <<"test/%u">>}]).
  62. %%------------------------------------------------------------------------------
  63. %% Testcases
  64. %%------------------------------------------------------------------------------
  65. t_authz(_) ->
  66. ClientInfo1 = #{clientid => <<"test">>,
  67. username => <<"test">>,
  68. peerhost => {127,0,0,1},
  69. zone => zone
  70. },
  71. ClientInfo2 = #{clientid => <<"test_clientid">>,
  72. username => <<"test_username">>,
  73. peerhost => {192,168,0,10},
  74. zone => zone
  75. },
  76. ClientInfo3 = #{clientid => <<"test_clientid">>,
  77. username => <<"fake_username">>,
  78. peerhost => {127,0,0,1},
  79. zone => zone
  80. },
  81. meck:expect(emqx_resource, query, fun(_, _) -> {ok, ?COLUMNS, []} end),
  82. ?assertEqual(deny, emqx_access_control:authorize(ClientInfo1, subscribe, <<"#">>)), % nomatch
  83. ?assertEqual(deny, emqx_access_control:authorize(ClientInfo1, publish, <<"#">>)), % nomatch
  84. meck:expect(emqx_resource, query, fun(_, _) -> {ok, ?COLUMNS, ?RULE1 ++ ?RULE2} end),
  85. ?assertEqual(deny, emqx_access_control:authorize(ClientInfo1, subscribe, <<"+">>)),
  86. ?assertEqual(deny, emqx_access_control:authorize(ClientInfo1, publish, <<"+">>)),
  87. meck:expect(emqx_resource, query, fun(_, _) -> {ok, ?COLUMNS, ?RULE2 ++ ?RULE1} end),
  88. ?assertEqual(allow, emqx_access_control:authorize(ClientInfo1, subscribe, <<"#">>)),
  89. ?assertEqual(deny, emqx_access_control:authorize(ClientInfo2, subscribe, <<"+">>)),
  90. meck:expect(emqx_resource, query, fun(_, _) -> {ok, ?COLUMNS, ?RULE3 ++ ?RULE4} end),
  91. ?assertEqual(allow, emqx_access_control:authorize(ClientInfo2, subscribe, <<"test/test_clientid">>)),
  92. ?assertEqual(deny, emqx_access_control:authorize(ClientInfo2, publish, <<"test/test_clientid">>)),
  93. ?assertEqual(deny, emqx_access_control:authorize(ClientInfo2, subscribe, <<"test/test_username">>)),
  94. ?assertEqual(allow, emqx_access_control:authorize(ClientInfo2, publish, <<"test/test_username">>)),
  95. ?assertEqual(deny, emqx_access_control:authorize(ClientInfo3, subscribe, <<"test">>)), % nomatch
  96. ?assertEqual(deny, emqx_access_control:authorize(ClientInfo3, publish, <<"test">>)), % nomatch
  97. ok.