emqx_authz_postgresql_SUITE.erl 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2022 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. %% http://www.apache.org/licenses/LICENSE-2.0
  8. %%
  9. %% Unless required by applicable law or agreed to in writing, software
  10. %% distributed under the License is distributed on an "AS IS" BASIS,
  11. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. %% See the License for the specific language governing permissions and
  13. %% limitations under the License.
  14. %%--------------------------------------------------------------------
  15. -module(emqx_authz_postgresql_SUITE).
  16. -compile(nowarn_export_all).
  17. -compile(export_all).
  18. -include("emqx_authz.hrl").
  19. -include_lib("eunit/include/eunit.hrl").
  20. -include_lib("common_test/include/ct.hrl").
  21. -define(PGSQL_HOST, "pgsql").
  22. -define(PGSQL_PORT, 5432).
  23. -define(PGSQL_RESOURCE, <<"emqx_authz_pgsql_SUITE">>).
  24. all() ->
  25. emqx_common_test_helpers:all(?MODULE).
  26. groups() ->
  27. [].
  28. init_per_suite(Config) ->
  29. case emqx_authn_test_lib:is_tcp_server_available(?PGSQL_HOST, ?PGSQL_PORT) of
  30. true ->
  31. ok = emqx_common_test_helpers:start_apps(
  32. [emqx_conf, emqx_authz],
  33. fun set_special_configs/1
  34. ),
  35. ok = start_apps([emqx_resource, emqx_connector]),
  36. {ok, _} = emqx_resource:create_local(
  37. ?PGSQL_RESOURCE,
  38. emqx_connector_pgsql,
  39. pgsql_config()),
  40. Config;
  41. false ->
  42. {skip, no_pgsql}
  43. end.
  44. end_per_suite(_Config) ->
  45. ok = emqx_authz_test_lib:restore_authorizers(),
  46. ok = emqx_resource:remove_local(?PGSQL_RESOURCE),
  47. ok = stop_apps([emqx_resource, emqx_connector]),
  48. ok = emqx_common_test_helpers:stop_apps([emqx_authz]).
  49. init_per_testcase(_TestCase, Config) ->
  50. ok = emqx_authz_test_lib:reset_authorizers(),
  51. Config.
  52. set_special_configs(emqx_authz) ->
  53. ok = emqx_authz_test_lib:reset_authorizers();
  54. set_special_configs(_) ->
  55. ok.
  56. %%------------------------------------------------------------------------------
  57. %% Testcases
  58. %%------------------------------------------------------------------------------
  59. t_topic_rules(_Config) ->
  60. ClientInfo = #{clientid => <<"clientid">>,
  61. username => <<"username">>,
  62. peerhost => {127,0,0,1},
  63. zone => default,
  64. listener => {tcp, default}
  65. },
  66. ok = emqx_authz_test_lib:test_no_topic_rules(ClientInfo, fun setup_client_samples/2),
  67. ok = emqx_authz_test_lib:test_allow_topic_rules(ClientInfo, fun setup_client_samples/2),
  68. ok = emqx_authz_test_lib:test_deny_topic_rules(ClientInfo, fun setup_client_samples/2).
  69. t_lookups(_Config) ->
  70. ClientInfo = #{clientid => <<"clientid">>,
  71. cn => <<"cn">>,
  72. dn => <<"dn">>,
  73. username => <<"username">>,
  74. peerhost => {127,0,0,1},
  75. zone => default,
  76. listener => {tcp, default}
  77. },
  78. %% by clientid
  79. ok = init_table(),
  80. ok = insert(<<"INSERT INTO acl(clientid, topic, permission, action)"
  81. "VALUES($1, $2, $3, $4)">>,
  82. [<<"clientid">>, <<"a">>, <<"allow">>, <<"subscribe">>]),
  83. ok = setup_config(
  84. #{<<"query">> => <<"SELECT permission, action, topic "
  85. "FROM acl WHERE clientid = ${clientid}">>}),
  86. ok = emqx_authz_test_lib:test_samples(
  87. ClientInfo,
  88. [{allow, subscribe, <<"a">>},
  89. {deny, subscribe, <<"b">>}]),
  90. %% by peerhost
  91. ok = init_table(),
  92. ok = insert(<<"INSERT INTO acl(peerhost, topic, permission, action)"
  93. "VALUES($1, $2, $3, $4)">>,
  94. [<<"127.0.0.1">>, <<"a">>, <<"allow">>, <<"subscribe">>]),
  95. ok = setup_config(
  96. #{<<"query">> => <<"SELECT permission, action, topic "
  97. "FROM acl WHERE peerhost = ${peerhost}">>}),
  98. ok = emqx_authz_test_lib:test_samples(
  99. ClientInfo,
  100. [{allow, subscribe, <<"a">>},
  101. {deny, subscribe, <<"b">>}]),
  102. %% by cn
  103. ok = init_table(),
  104. ok = insert(<<"INSERT INTO acl(cn, topic, permission, action)"
  105. "VALUES($1, $2, $3, $4)">>,
  106. [<<"cn">>, <<"a">>, <<"allow">>, <<"subscribe">>]),
  107. ok = setup_config(
  108. #{<<"query">> => <<"SELECT permission, action, topic "
  109. "FROM acl WHERE cn = ${cert_common_name}">>}),
  110. ok = emqx_authz_test_lib:test_samples(
  111. ClientInfo,
  112. [{allow, subscribe, <<"a">>},
  113. {deny, subscribe, <<"b">>}]),
  114. %% by dn
  115. ok = init_table(),
  116. ok = insert(<<"INSERT INTO acl(dn, topic, permission, action)"
  117. "VALUES($1, $2, $3, $4)">>,
  118. [<<"dn">>, <<"a">>, <<"allow">>, <<"subscribe">>]),
  119. ok = setup_config(
  120. #{<<"query">> => <<"SELECT permission, action, topic "
  121. "FROM acl WHERE dn = ${cert_subject}">>}),
  122. ok = emqx_authz_test_lib:test_samples(
  123. ClientInfo,
  124. [{allow, subscribe, <<"a">>},
  125. {deny, subscribe, <<"b">>}]).
  126. t_pgsql_error(_Config) ->
  127. ClientInfo = #{clientid => <<"clientid">>,
  128. username => <<"username">>,
  129. peerhost => {127,0,0,1},
  130. zone => default,
  131. listener => {tcp, default}
  132. },
  133. ok = setup_config(
  134. #{<<"query">> => <<"SELECT permission, action, topic "
  135. "FROM acl WHERE clientid = ${username}">>}),
  136. ok = emqx_authz_test_lib:test_samples(
  137. ClientInfo,
  138. [{deny, subscribe, <<"a">>}]).
  139. t_create_invalid(_Config) ->
  140. BadConfig = maps:merge(
  141. raw_pgsql_authz_config(),
  142. #{<<"server">> => <<"255.255.255.255:33333">>}),
  143. {error, _} = emqx_authz:update(?CMD_REPLACE, [BadConfig]),
  144. [] = emqx_authz:lookup().
  145. t_nonbinary_values(_Config) ->
  146. ClientInfo = #{clientid => clientid,
  147. username => "username",
  148. peerhost => {127,0,0,1},
  149. zone => default,
  150. listener => {tcp, default}
  151. },
  152. ok = init_table(),
  153. ok = insert(<<"INSERT INTO acl(clientid, username, topic, permission, action)"
  154. "VALUES($1, $2, $3, $4, $5)">>,
  155. [<<"clientid">>, <<"username">>, <<"a">>, <<"allow">>, <<"subscribe">>]),
  156. ok = setup_config(
  157. #{<<"query">> => <<"SELECT permission, action, topic "
  158. "FROM acl WHERE clientid = ${clientid} AND username = ${username}">>}),
  159. ok = emqx_authz_test_lib:test_samples(
  160. ClientInfo,
  161. [{allow, subscribe, <<"a">>},
  162. {deny, subscribe, <<"b">>}]).
  163. %%------------------------------------------------------------------------------
  164. %% Helpers
  165. %%------------------------------------------------------------------------------
  166. raw_pgsql_authz_config() ->
  167. #{
  168. <<"enable">> => <<"true">>,
  169. <<"type">> => <<"postgresql">>,
  170. <<"database">> => <<"mqtt">>,
  171. <<"username">> => <<"root">>,
  172. <<"password">> => <<"public">>,
  173. <<"query">> => <<"SELECT permission, action, topic "
  174. "FROM acl WHERE username = ${username}">>,
  175. <<"server">> => pgsql_server()
  176. }.
  177. q(Sql) ->
  178. emqx_resource:query(
  179. ?PGSQL_RESOURCE,
  180. {query, Sql}).
  181. insert(Sql, Params) ->
  182. {ok, _} = emqx_resource:query(
  183. ?PGSQL_RESOURCE,
  184. {query, Sql, Params}),
  185. ok.
  186. init_table() ->
  187. ok = drop_table(),
  188. {ok, _, _} = q("CREATE TABLE acl(
  189. username VARCHAR(255),
  190. clientid VARCHAR(255),
  191. peerhost VARCHAR(255),
  192. cn VARCHAR(255),
  193. dn VARCHAR(255),
  194. topic VARCHAR(255),
  195. permission VARCHAR(255),
  196. action VARCHAR(255))"),
  197. ok.
  198. drop_table() ->
  199. {ok, _, _} = q("DROP TABLE IF EXISTS acl"),
  200. ok.
  201. setup_client_samples(ClientInfo, Samples) ->
  202. #{username := Username} = ClientInfo,
  203. ok = init_table(),
  204. ok = lists:foreach(
  205. fun(#{topics := Topics, permission := Permission, action := Action}) ->
  206. lists:foreach(
  207. fun(Topic) ->
  208. insert(<<"INSERT INTO acl(username, topic, permission, action)"
  209. "VALUES($1, $2, $3, $4)">>,
  210. [Username, Topic, Permission, Action])
  211. end,
  212. Topics)
  213. end,
  214. Samples),
  215. setup_config(
  216. #{<<"query">> => <<"SELECT permission, action, topic "
  217. "FROM acl WHERE username = ${username}">>}).
  218. setup_config(SpecialParams) ->
  219. emqx_authz_test_lib:setup_config(
  220. raw_pgsql_authz_config(),
  221. SpecialParams).
  222. pgsql_server() ->
  223. iolist_to_binary(
  224. io_lib:format(
  225. "~s:~b",
  226. [?PGSQL_HOST, ?PGSQL_PORT])).
  227. pgsql_config() ->
  228. #{auto_reconnect => true,
  229. database => <<"mqtt">>,
  230. username => <<"root">>,
  231. password => <<"public">>,
  232. pool_size => 8,
  233. server => {?PGSQL_HOST, ?PGSQL_PORT},
  234. ssl => #{enable => false}
  235. }.
  236. start_apps(Apps) ->
  237. lists:foreach(fun application:ensure_all_started/1, Apps).
  238. stop_apps(Apps) ->
  239. lists:foreach(fun application:stop/1, Apps).