emqx_access_control_SUITE.erl 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2019-2024 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_access_control_SUITE).
  17. -compile(export_all).
  18. -compile(nowarn_export_all).
  19. -include_lib("emqx/include/emqx_hooks.hrl").
  20. -include_lib("emqx/include/emqx_access_control.hrl").
  21. -include_lib("eunit/include/eunit.hrl").
  22. all() -> emqx_common_test_helpers:all(?MODULE).
  23. init_per_suite(Config) ->
  24. Apps = emqx_cth_suite:start(
  25. [{emqx, #{override_env => [{boot_modules, [broker]}]}}],
  26. #{work_dir => emqx_cth_suite:work_dir(Config)}
  27. ),
  28. [{apps, Apps} | Config].
  29. end_per_suite(Config) ->
  30. emqx_cth_suite:stop(proplists:get_value(apps, Config)).
  31. init_per_testcase(_, Config) ->
  32. Config.
  33. end_per_testcase(_, _Config) ->
  34. ok = emqx_hooks:del('client.authorize', {?MODULE, authz_stub}),
  35. ok = emqx_hooks:del('client.authenticate', {?MODULE, quick_deny_anonymous_authn}).
  36. t_authenticate(_) ->
  37. ?assertMatch({ok, _}, emqx_access_control:authenticate(clientinfo())).
  38. t_authorize(_) ->
  39. ?assertEqual(allow, emqx_access_control:authorize(clientinfo(), ?AUTHZ_PUBLISH, <<"t">>)).
  40. t_delayed_authorize(_) ->
  41. RawTopic = <<"$delayed/1/foo/2">>,
  42. InvalidTopic = <<"$delayed/1/foo/3">>,
  43. Topic = <<"foo/2">>,
  44. ok = emqx_hooks:put('client.authorize', {?MODULE, authz_stub, [Topic]}, ?HP_AUTHZ),
  45. ?assertEqual(allow, emqx_access_control:authorize(clientinfo(), ?AUTHZ_PUBLISH, RawTopic)),
  46. ?assertEqual(
  47. deny, emqx_access_control:authorize(clientinfo(), ?AUTHZ_PUBLISH, InvalidTopic)
  48. ),
  49. ok.
  50. t_quick_deny_anonymous(_) ->
  51. ok = emqx_hooks:put(
  52. 'client.authenticate',
  53. {?MODULE, quick_deny_anonymous_authn, []},
  54. ?HP_AUTHN
  55. ),
  56. RawClient0 = clientinfo(),
  57. RawClient = RawClient0#{username => undefined},
  58. %% No name, No authn
  59. Client1 = RawClient#{enable_authn => false},
  60. ?assertMatch({ok, _}, emqx_access_control:authenticate(Client1)),
  61. %% No name, With quick_deny_anonymous
  62. Client2 = RawClient#{enable_authn => quick_deny_anonymous},
  63. ?assertMatch({error, _}, emqx_access_control:authenticate(Client2)),
  64. %% Bad name, With quick_deny_anonymous
  65. Client3 = RawClient#{enable_authn => quick_deny_anonymous, username => <<"badname">>},
  66. ?assertMatch({error, _}, emqx_access_control:authenticate(Client3)),
  67. %% Good name, With quick_deny_anonymous
  68. Client4 = RawClient#{enable_authn => quick_deny_anonymous, username => <<"goodname">>},
  69. ?assertMatch({ok, _}, emqx_access_control:authenticate(Client4)),
  70. %% Name, With authn
  71. Client5 = RawClient#{enable_authn => true, username => <<"badname">>},
  72. ?assertMatch({error, _}, emqx_access_control:authenticate(Client5)),
  73. ok.
  74. %%--------------------------------------------------------------------
  75. %% Helper functions
  76. %%--------------------------------------------------------------------
  77. authz_stub(_Client, _Action, ValidTopic, _DefaultResult, ValidTopic) -> {stop, #{result => allow}};
  78. authz_stub(_Client, _Action, _Topic, _DefaultResult, _ValidTopic) -> {stop, #{result => deny}}.
  79. quick_deny_anonymous_authn(#{username := <<"badname">>}, _AuthResult) ->
  80. {stop, {error, not_authorized}};
  81. quick_deny_anonymous_authn(_ClientInfo, _AuthResult) ->
  82. {stop, {ok, #{is_superuser => false}}}.
  83. clientinfo() -> clientinfo(#{}).
  84. clientinfo(InitProps) ->
  85. maps:merge(
  86. #{
  87. zone => default,
  88. listener => {tcp, default},
  89. protocol => mqtt,
  90. peerhost => {127, 0, 0, 1},
  91. clientid => <<"clientid">>,
  92. username => <<"username">>,
  93. password => <<"passwd">>,
  94. is_superuser => false,
  95. mountpoint => undefined
  96. },
  97. InitProps
  98. ).