emqx_authz_test_lib.erl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2021-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_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. register_fake_sources(SourceTypes) ->
  47. lists:foreach(
  48. fun(Type) ->
  49. emqx_authz_source_registry:register(Type, emqx_authz_fake_source)
  50. end,
  51. SourceTypes
  52. ).
  53. deregister_sources() ->
  54. {BuiltInTypes, _} = lists:unzip(?BUILTIN_SOURCES),
  55. SourceTypes = emqx_authz_source_registry:get(),
  56. lists:foreach(
  57. fun(Type) ->
  58. emqx_authz_source_registry:register(Type, emqx_authz_fake_source)
  59. end,
  60. SourceTypes -- BuiltInTypes
  61. ).
  62. %%--------------------------------------------------------------------
  63. %% Table-based test helpers
  64. %%--------------------------------------------------------------------
  65. all_with_table_case(Mod, TableCase, Cases) ->
  66. (emqx_common_test_helpers:all(Mod) -- [TableCase]) ++
  67. [{group, Name} || Name <- case_names(Cases)].
  68. table_groups(TableCase, Cases) ->
  69. [{Name, [], [TableCase]} || Name <- case_names(Cases)].
  70. case_names(Cases) ->
  71. lists:map(fun(Case) -> maps:get(name, Case) end, Cases).
  72. get_case(Name, Cases) ->
  73. [Case] = [C || C <- Cases, maps:get(name, C) =:= Name],
  74. Case.
  75. setup_default_permission(Case) ->
  76. DefaultPermission = maps:get(default_permission, Case, deny),
  77. emqx_authz_test_lib:reset_authorizers(DefaultPermission, false).
  78. base_client_info() ->
  79. #{
  80. clientid => <<"clientid">>,
  81. username => <<"username">>,
  82. peerhost => {127, 0, 0, 1},
  83. zone => default,
  84. listener => {tcp, default}
  85. }.
  86. client_info(Overrides) ->
  87. maps:merge(base_client_info(), Overrides).
  88. enable_features(Case) ->
  89. Features = maps:get(features, Case, []),
  90. lists:foreach(
  91. fun(Feature) ->
  92. Enable = lists:member(Feature, Features),
  93. emqx_authz:set_feature_available(Feature, Enable)
  94. end,
  95. ?AUTHZ_FEATURES
  96. ).
  97. run_checks(#{checks := Checks} = Case) ->
  98. _ = setup_default_permission(Case),
  99. _ = enable_features(Case),
  100. ClientInfoOverrides = maps:get(client_info, Case, #{}),
  101. ClientInfo = client_info(ClientInfoOverrides),
  102. lists:foreach(
  103. fun(Check) ->
  104. run_check(ClientInfo, Check)
  105. end,
  106. Checks
  107. ).
  108. run_check(ClientInfo, Fun) when is_function(Fun, 0) ->
  109. run_check(ClientInfo, Fun());
  110. run_check(ClientInfo, {ExpectedPermission, Action, Topic}) ->
  111. ?assertEqual(
  112. ExpectedPermission,
  113. emqx_access_control:authorize(
  114. ClientInfo,
  115. Action,
  116. Topic
  117. )
  118. ).