emqx_authz.hrl 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-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. -include_lib("emqx/include/emqx_access_control.hrl").
  17. -include("emqx_auth.hrl").
  18. %% authz_mnesia
  19. -define(ACL_TABLE, emqx_acl).
  20. %% authz_cmd
  21. -define(CMD_REPLACE, replace).
  22. -define(CMD_DELETE, delete).
  23. -define(CMD_PREPEND, prepend).
  24. -define(CMD_APPEND, append).
  25. -define(CMD_MOVE, move).
  26. -define(CMD_MERGE, merge).
  27. -define(CMD_REORDER, reorder).
  28. -define(CMD_MOVE_FRONT, front).
  29. -define(CMD_MOVE_REAR, rear).
  30. -define(CMD_MOVE_BEFORE(Before), {before, Before}).
  31. -define(CMD_MOVE_AFTER(After), {'after', After}).
  32. -define(ROOT_KEY, [authorization]).
  33. -define(CONF_KEY_PATH, [authorization, sources]).
  34. %% has to be the same as the root field name defined in emqx_schema
  35. -define(CONF_NS, ?EMQX_AUTHORIZATION_CONFIG_ROOT_NAME).
  36. -define(CONF_NS_ATOM, ?EMQX_AUTHORIZATION_CONFIG_ROOT_NAME_ATOM).
  37. -define(CONF_NS_BINARY, ?EMQX_AUTHORIZATION_CONFIG_ROOT_NAME_BINARY).
  38. %% API examples
  39. -define(USERNAME_RULES_EXAMPLE, #{
  40. username => user1,
  41. rules => [
  42. #{
  43. topic => <<"test/topic/1">>,
  44. permission => <<"allow">>,
  45. action => <<"publish">>
  46. },
  47. #{
  48. topic => <<"test/topic/2">>,
  49. permission => <<"allow">>,
  50. action => <<"subscribe">>
  51. },
  52. #{
  53. topic => <<"eq test/#">>,
  54. permission => <<"deny">>,
  55. action => <<"all">>
  56. },
  57. #{
  58. topic => <<"test/topic/3">>,
  59. permission => <<"allow">>,
  60. action => <<"publish">>,
  61. qos => [<<"1">>],
  62. retain => <<"true">>
  63. },
  64. #{
  65. topic => <<"test/topic/4">>,
  66. permission => <<"allow">>,
  67. action => <<"publish">>,
  68. qos => [<<"0">>, <<"1">>, <<"2">>],
  69. retain => <<"all">>
  70. }
  71. ]
  72. }).
  73. -define(CLIENTID_RULES_EXAMPLE, #{
  74. clientid => client1,
  75. rules => [
  76. #{
  77. topic => <<"test/topic/1">>,
  78. permission => <<"allow">>,
  79. action => <<"publish">>
  80. },
  81. #{
  82. topic => <<"test/topic/2">>,
  83. permission => <<"allow">>,
  84. action => <<"subscribe">>
  85. },
  86. #{
  87. topic => <<"eq test/#">>,
  88. permission => <<"deny">>,
  89. action => <<"all">>
  90. },
  91. #{
  92. topic => <<"test/topic/3">>,
  93. permission => <<"allow">>,
  94. action => <<"publish">>,
  95. qos => [<<"1">>],
  96. retain => <<"true">>
  97. },
  98. #{
  99. topic => <<"test/topic/4">>,
  100. permission => <<"allow">>,
  101. action => <<"publish">>,
  102. qos => [<<"0">>, <<"1">>, <<"2">>],
  103. retain => <<"all">>
  104. }
  105. ]
  106. }).
  107. -define(ALL_RULES_EXAMPLE, #{
  108. rules => [
  109. #{
  110. topic => <<"test/topic/1">>,
  111. permission => <<"allow">>,
  112. action => <<"publish">>
  113. },
  114. #{
  115. topic => <<"test/topic/2">>,
  116. permission => <<"allow">>,
  117. action => <<"subscribe">>
  118. },
  119. #{
  120. topic => <<"eq test/#">>,
  121. permission => <<"deny">>,
  122. action => <<"all">>
  123. },
  124. #{
  125. topic => <<"test/topic/3">>,
  126. permission => <<"allow">>,
  127. action => <<"publish">>,
  128. qos => [<<"1">>],
  129. retain => <<"true">>
  130. },
  131. #{
  132. topic => <<"test/topic/4">>,
  133. permission => <<"allow">>,
  134. action => <<"publish">>,
  135. qos => [<<"0">>, <<"1">>, <<"2">>],
  136. retain => <<"all">>
  137. }
  138. ]
  139. }).
  140. -define(USERNAME_RULES_EXAMPLE_COUNT, length(maps:get(rules, ?USERNAME_RULES_EXAMPLE))).
  141. -define(CLIENTID_RULES_EXAMPLE_COUNT, length(maps:get(rules, ?CLIENTID_RULES_EXAMPLE))).
  142. -define(ALL_RULES_EXAMPLE_COUNT, length(maps:get(rules, ?ALL_RULES_EXAMPLE))).
  143. -define(META_EXAMPLE, #{
  144. page => 1,
  145. limit => 100,
  146. count => 1
  147. }).
  148. -define(AUTHZ_RESOURCE_GROUP, <<"authz">>).
  149. -define(AUTHZ_FEATURES, [rich_actions]).
  150. -define(DEFAULT_RULE_QOS, [0, 1, 2]).
  151. -define(DEFAULT_RULE_RETAIN, all).
  152. -define(BUILTIN_SOURCES, [
  153. {client_info, emqx_authz_client_info},
  154. {file, emqx_authz_file}
  155. ]).