emqx_authz_test_lib.erl 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2021-2022 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. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_authz_test_lib).
  17. -include("emqx_authz.hrl").
  18. -include_lib("eunit/include/eunit.hrl").
  19. -compile(nowarn_export_all).
  20. -compile(export_all).
  21. -define(DEFAULT_CHECK_AVAIL_TIMEOUT, 1000).
  22. reset_authorizers() ->
  23. reset_authorizers(deny, false).
  24. restore_authorizers() ->
  25. reset_authorizers(allow, true).
  26. reset_authorizers(Nomatch, ChacheEnabled) ->
  27. {ok, _} = emqx:update_config(
  28. [authorization],
  29. #{<<"no_match">> => atom_to_binary(Nomatch),
  30. <<"cache">> => #{<<"enable">> => atom_to_binary(ChacheEnabled)},
  31. <<"sources">> => []}),
  32. ok.
  33. setup_config(BaseConfig, SpecialParams) ->
  34. Config = maps:merge(BaseConfig, SpecialParams),
  35. case emqx_authz:update(?CMD_REPLACE, [Config]) of
  36. {ok, _} -> ok;
  37. {error, Reason} -> {error, Reason}
  38. end.
  39. is_tcp_server_available(Host, Port) ->
  40. case gen_tcp:connect(Host, Port, [], ?DEFAULT_CHECK_AVAIL_TIMEOUT) of
  41. {ok, Socket} ->
  42. gen_tcp:close(Socket),
  43. true;
  44. {error, _} ->
  45. false
  46. end.
  47. test_samples(ClientInfo, Samples) ->
  48. lists:foreach(
  49. fun({Expected, Action, Topic}) ->
  50. ct:pal(
  51. "client_info: ~p, action: ~p, topic: ~p, expected: ~p",
  52. [ClientInfo, Action, Topic, Expected]),
  53. ?assertEqual(
  54. Expected,
  55. emqx_access_control:authorize(
  56. ClientInfo,
  57. Action,
  58. Topic))
  59. end,
  60. Samples).
  61. test_no_topic_rules(ClientInfo, SetupSamples) ->
  62. %% No rules
  63. ok = reset_authorizers(deny, false),
  64. ok = SetupSamples(ClientInfo, []),
  65. ok = test_samples(
  66. ClientInfo,
  67. [{deny, subscribe, <<"#">>},
  68. {deny, subscribe, <<"subs">>},
  69. {deny, publish, <<"pub">>}]).
  70. test_allow_topic_rules(ClientInfo, SetupSamples) ->
  71. Samples = [#{
  72. topics => [<<"eq testpub1/${username}">>,
  73. <<"testpub2/${clientid}">>,
  74. <<"testpub3/#">>],
  75. permission => <<"allow">>,
  76. action => <<"publish">>
  77. },
  78. #{
  79. topics => [<<"eq testsub1/${username}">>,
  80. <<"testsub2/${clientid}">>,
  81. <<"testsub3/#">>],
  82. permission => <<"allow">>,
  83. action => <<"subscribe">>
  84. },
  85. #{
  86. topics => [<<"eq testall1/${username}">>,
  87. <<"testall2/${clientid}">>,
  88. <<"testall3/#">>],
  89. permission => <<"allow">>,
  90. action => <<"all">>
  91. }
  92. ],
  93. ok = reset_authorizers(deny, false),
  94. ok = SetupSamples(ClientInfo, Samples),
  95. ok = test_samples(
  96. ClientInfo,
  97. [
  98. %% Publish rules
  99. {deny, publish, <<"testpub1/username">>},
  100. {allow, publish, <<"testpub1/${username}">>},
  101. {allow, publish, <<"testpub2/clientid">>},
  102. {allow, publish, <<"testpub3/foobar">>},
  103. {deny, publish, <<"testpub2/username">>},
  104. {deny, publish, <<"testpub1/clientid">>},
  105. {deny, subscribe, <<"testpub1/username">>},
  106. {deny, subscribe, <<"testpub2/clientid">>},
  107. {deny, subscribe, <<"testpub3/foobar">>},
  108. %% Subscribe rules
  109. {deny, subscribe, <<"testsub1/username">>},
  110. {allow, subscribe, <<"testsub1/${username}">>},
  111. {allow, subscribe, <<"testsub2/clientid">>},
  112. {allow, subscribe, <<"testsub3/foobar">>},
  113. {allow, subscribe, <<"testsub3/+/foobar">>},
  114. {allow, subscribe, <<"testsub3/#">>},
  115. {deny, subscribe, <<"testsub2/username">>},
  116. {deny, subscribe, <<"testsub1/clientid">>},
  117. {deny, subscribe, <<"testsub4/foobar">>},
  118. {deny, publish, <<"testsub1/username">>},
  119. {deny, publish, <<"testsub2/clientid">>},
  120. {deny, publish, <<"testsub3/foobar">>},
  121. %% All rules
  122. {deny, subscribe, <<"testall1/username">>},
  123. {allow, subscribe, <<"testall1/${username}">>},
  124. {allow, subscribe, <<"testall2/clientid">>},
  125. {allow, subscribe, <<"testall3/foobar">>},
  126. {allow, subscribe, <<"testall3/+/foobar">>},
  127. {allow, subscribe, <<"testall3/#">>},
  128. {deny, publish, <<"testall1/username">>},
  129. {allow, publish, <<"testall1/${username}">>},
  130. {allow, publish, <<"testall2/clientid">>},
  131. {allow, publish, <<"testall3/foobar">>},
  132. {deny, subscribe, <<"testall2/username">>},
  133. {deny, subscribe, <<"testall1/clientid">>},
  134. {deny, subscribe, <<"testall4/foobar">>},
  135. {deny, publish, <<"testall2/username">>},
  136. {deny, publish, <<"testall1/clientid">>},
  137. {deny, publish, <<"testall4/foobar">>}
  138. ]).
  139. test_deny_topic_rules(ClientInfo, SetupSamples) ->
  140. Samples = [
  141. #{
  142. topics => [<<"eq testpub1/${username}">>,
  143. <<"testpub2/${clientid}">>,
  144. <<"testpub3/#">>],
  145. permission => <<"deny">>,
  146. action => <<"publish">>
  147. },
  148. #{
  149. topics => [<<"eq testsub1/${username}">>,
  150. <<"testsub2/${clientid}">>,
  151. <<"testsub3/#">>],
  152. permission => <<"deny">>,
  153. action => <<"subscribe">>
  154. },
  155. #{
  156. topics => [<<"eq testall1/${username}">>,
  157. <<"testall2/${clientid}">>,
  158. <<"testall3/#">>],
  159. permission => <<"deny">>,
  160. action => <<"all">>
  161. }
  162. ],
  163. ok = reset_authorizers(allow, false),
  164. ok = SetupSamples(ClientInfo, Samples),
  165. ok = test_samples(
  166. ClientInfo,
  167. [
  168. %% Publish rules
  169. {allow, publish, <<"testpub1/username">>},
  170. {deny, publish, <<"testpub1/${username}">>},
  171. {deny, publish, <<"testpub2/clientid">>},
  172. {deny, publish, <<"testpub3/foobar">>},
  173. {allow, publish, <<"testpub2/username">>},
  174. {allow, publish, <<"testpub1/clientid">>},
  175. {allow, subscribe, <<"testpub1/username">>},
  176. {allow, subscribe, <<"testpub2/clientid">>},
  177. {allow, subscribe, <<"testpub3/foobar">>},
  178. %% Subscribe rules
  179. {allow, subscribe, <<"testsub1/username">>},
  180. {deny, subscribe, <<"testsub1/${username}">>},
  181. {deny, subscribe, <<"testsub2/clientid">>},
  182. {deny, subscribe, <<"testsub3/foobar">>},
  183. {deny, subscribe, <<"testsub3/+/foobar">>},
  184. {deny, subscribe, <<"testsub3/#">>},
  185. {allow, subscribe, <<"testsub2/username">>},
  186. {allow, subscribe, <<"testsub1/clientid">>},
  187. {allow, subscribe, <<"testsub4/foobar">>},
  188. {allow, publish, <<"testsub1/username">>},
  189. {allow, publish, <<"testsub2/clientid">>},
  190. {allow, publish, <<"testsub3/foobar">>},
  191. %% All rules
  192. {allow, subscribe, <<"testall1/username">>},
  193. {deny, subscribe, <<"testall1/${username}">>},
  194. {deny, subscribe, <<"testall2/clientid">>},
  195. {deny, subscribe, <<"testall3/foobar">>},
  196. {deny, subscribe, <<"testall3/+/foobar">>},
  197. {deny, subscribe, <<"testall3/#">>},
  198. {allow, publish, <<"testall1/username">>},
  199. {deny, publish, <<"testall1/${username}">>},
  200. {deny, publish, <<"testall2/clientid">>},
  201. {deny, publish, <<"testall3/foobar">>},
  202. {allow, subscribe, <<"testall2/username">>},
  203. {allow, subscribe, <<"testall1/clientid">>},
  204. {allow, subscribe, <<"testall4/foobar">>},
  205. {allow, publish, <<"testall2/username">>},
  206. {allow, publish, <<"testall1/clientid">>},
  207. {allow, publish, <<"testall4/foobar">>}
  208. ]).