emqx_authz_test_lib.erl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2021-2023 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. reset_authorizers() ->
  22. reset_authorizers(deny, false, []).
  23. restore_authorizers() ->
  24. reset_authorizers(allow, true, []).
  25. reset_authorizers(Nomatch, CacheEnabled, Source) ->
  26. {ok, _} = emqx:update_config(
  27. [authorization],
  28. #{
  29. <<"no_match">> => atom_to_binary(Nomatch),
  30. <<"cache">> => #{<<"enable">> => CacheEnabled},
  31. <<"sources">> => Source
  32. }
  33. ),
  34. ok.
  35. %% Don't reset sources
  36. reset_authorizers(Nomatch, CacheEnabled) ->
  37. {ok, _} = emqx:update_config([<<"authorization">>, <<"no_match">>], Nomatch),
  38. {ok, _} = emqx:update_config([<<"authorization">>, <<"cache">>, <<"enable">>], CacheEnabled),
  39. ok.
  40. setup_config(BaseConfig, SpecialParams) ->
  41. Config = maps:merge(BaseConfig, SpecialParams),
  42. case emqx_authz:update(?CMD_REPLACE, [Config]) of
  43. {ok, _} -> ok;
  44. {error, Reason} -> {error, Reason}
  45. end.
  46. %%--------------------------------------------------------------------
  47. %% Table-based test helpers
  48. %%--------------------------------------------------------------------
  49. all_with_table_case(Mod, TableCase, Cases) ->
  50. (emqx_common_test_helpers:all(Mod) -- [TableCase]) ++
  51. [{group, Name} || Name <- case_names(Cases)].
  52. table_groups(TableCase, Cases) ->
  53. [{Name, [], [TableCase]} || Name <- case_names(Cases)].
  54. case_names(Cases) ->
  55. lists:map(fun(Case) -> maps:get(name, Case) end, Cases).
  56. get_case(Name, Cases) ->
  57. [Case] = [C || C <- Cases, maps:get(name, C) =:= Name],
  58. Case.
  59. setup_default_permission(Case) ->
  60. DefaultPermission = maps:get(default_permission, Case, deny),
  61. emqx_authz_test_lib:reset_authorizers(DefaultPermission, false).
  62. base_client_info() ->
  63. #{
  64. clientid => <<"clientid">>,
  65. username => <<"username">>,
  66. peerhost => {127, 0, 0, 1},
  67. zone => default,
  68. listener => {tcp, default}
  69. }.
  70. client_info(Overrides) ->
  71. maps:merge(base_client_info(), Overrides).
  72. enable_features(Case) ->
  73. Features = maps:get(features, Case, []),
  74. lists:foreach(
  75. fun(Feature) ->
  76. Enable = lists:member(Feature, Features),
  77. emqx_authz:set_feature_available(Feature, Enable)
  78. end,
  79. ?AUTHZ_FEATURES
  80. ).
  81. run_checks(#{checks := Checks} = Case) ->
  82. _ = setup_default_permission(Case),
  83. _ = enable_features(Case),
  84. ClientInfoOverrides = maps:get(client_info, Case, #{}),
  85. ClientInfo = client_info(ClientInfoOverrides),
  86. lists:foreach(
  87. fun(Check) ->
  88. run_check(ClientInfo, Check)
  89. end,
  90. Checks
  91. ).
  92. run_check(ClientInfo, Fun) when is_function(Fun, 0) ->
  93. run_check(ClientInfo, Fun());
  94. run_check(ClientInfo, {ExpectedPermission, Action, Topic}) ->
  95. ?assertEqual(
  96. ExpectedPermission,
  97. emqx_access_control:authorize(
  98. ClientInfo,
  99. Action,
  100. Topic
  101. )
  102. ).