emqx_authz.hrl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. -define(RE_PLACEHOLDER, "\\$\\{[a-z0-9_]+\\}").
  35. %% has to be the same as the root field name defined in emqx_schema
  36. -define(CONF_NS, ?EMQX_AUTHORIZATION_CONFIG_ROOT_NAME).
  37. -define(CONF_NS_ATOM, ?EMQX_AUTHORIZATION_CONFIG_ROOT_NAME_ATOM).
  38. -define(CONF_NS_BINARY, ?EMQX_AUTHORIZATION_CONFIG_ROOT_NAME_BINARY).
  39. %% API examples
  40. -define(USERNAME_RULES_EXAMPLE, #{
  41. username => user1,
  42. rules => [
  43. #{
  44. topic => <<"test/topic/1">>,
  45. permission => <<"allow">>,
  46. action => <<"publish">>
  47. },
  48. #{
  49. topic => <<"test/topic/2">>,
  50. permission => <<"allow">>,
  51. action => <<"subscribe">>
  52. },
  53. #{
  54. topic => <<"eq test/#">>,
  55. permission => <<"deny">>,
  56. action => <<"all">>
  57. },
  58. #{
  59. topic => <<"test/topic/3">>,
  60. permission => <<"allow">>,
  61. action => <<"publish">>,
  62. qos => [<<"1">>],
  63. retain => <<"true">>
  64. },
  65. #{
  66. topic => <<"test/topic/4">>,
  67. permission => <<"allow">>,
  68. action => <<"publish">>,
  69. qos => [<<"0">>, <<"1">>, <<"2">>],
  70. retain => <<"all">>
  71. }
  72. ]
  73. }).
  74. -define(CLIENTID_RULES_EXAMPLE, #{
  75. clientid => client1,
  76. rules => [
  77. #{
  78. topic => <<"test/topic/1">>,
  79. permission => <<"allow">>,
  80. action => <<"publish">>
  81. },
  82. #{
  83. topic => <<"test/topic/2">>,
  84. permission => <<"allow">>,
  85. action => <<"subscribe">>
  86. },
  87. #{
  88. topic => <<"eq test/#">>,
  89. permission => <<"deny">>,
  90. action => <<"all">>
  91. },
  92. #{
  93. topic => <<"test/topic/3">>,
  94. permission => <<"allow">>,
  95. action => <<"publish">>,
  96. qos => [<<"1">>],
  97. retain => <<"true">>
  98. },
  99. #{
  100. topic => <<"test/topic/4">>,
  101. permission => <<"allow">>,
  102. action => <<"publish">>,
  103. qos => [<<"0">>, <<"1">>, <<"2">>],
  104. retain => <<"all">>
  105. }
  106. ]
  107. }).
  108. -define(ALL_RULES_EXAMPLE, #{
  109. rules => [
  110. #{
  111. topic => <<"test/topic/1">>,
  112. permission => <<"allow">>,
  113. action => <<"publish">>
  114. },
  115. #{
  116. topic => <<"test/topic/2">>,
  117. permission => <<"allow">>,
  118. action => <<"subscribe">>
  119. },
  120. #{
  121. topic => <<"eq test/#">>,
  122. permission => <<"deny">>,
  123. action => <<"all">>
  124. },
  125. #{
  126. topic => <<"test/topic/3">>,
  127. permission => <<"allow">>,
  128. action => <<"publish">>,
  129. qos => [<<"1">>],
  130. retain => <<"true">>
  131. },
  132. #{
  133. topic => <<"test/topic/4">>,
  134. permission => <<"allow">>,
  135. action => <<"publish">>,
  136. qos => [<<"0">>, <<"1">>, <<"2">>],
  137. retain => <<"all">>
  138. }
  139. ]
  140. }).
  141. -define(USERNAME_RULES_EXAMPLE_COUNT, length(maps:get(rules, ?USERNAME_RULES_EXAMPLE))).
  142. -define(CLIENTID_RULES_EXAMPLE_COUNT, length(maps:get(rules, ?CLIENTID_RULES_EXAMPLE))).
  143. -define(ALL_RULES_EXAMPLE_COUNT, length(maps:get(rules, ?ALL_RULES_EXAMPLE))).
  144. -define(META_EXAMPLE, #{
  145. page => 1,
  146. limit => 100,
  147. count => 1
  148. }).
  149. -define(AUTHZ_RESOURCE_GROUP, <<"emqx_authz">>).
  150. -define(AUTHZ_FEATURES, [rich_actions]).
  151. -define(DEFAULT_RULE_QOS, [0, 1, 2]).
  152. -define(DEFAULT_RULE_RETAIN, all).
  153. -define(BUILTIN_SOURCES, [
  154. {client_info, emqx_authz_client_info},
  155. {file, emqx_authz_file}
  156. ]).