emqx_authz_postgresql_SUITE.erl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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_connector.hrl").
  19. -include("emqx_authz.hrl").
  20. -include_lib("eunit/include/eunit.hrl").
  21. -include_lib("common_test/include/ct.hrl").
  22. -define(PGSQL_HOST, "pgsql").
  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. ok = stop_apps([emqx_resource, emqx_connector]),
  30. case emqx_common_test_helpers:is_tcp_server_available(?PGSQL_HOST, ?PGSQL_DEFAULT_PORT) of
  31. true ->
  32. ok = emqx_common_test_helpers:start_apps(
  33. [emqx_conf, emqx_authz],
  34. fun set_special_configs/1
  35. ),
  36. ok = start_apps([emqx_resource, emqx_connector]),
  37. {ok, _} = emqx_resource:create_local(
  38. ?PGSQL_RESOURCE,
  39. ?RESOURCE_GROUP,
  40. emqx_connector_pgsql,
  41. pgsql_config(),
  42. #{}
  43. ),
  44. Config;
  45. false ->
  46. {skip, no_pgsql}
  47. end.
  48. end_per_suite(_Config) ->
  49. ok = emqx_authz_test_lib:restore_authorizers(),
  50. ok = emqx_resource:remove_local(?PGSQL_RESOURCE),
  51. ok = stop_apps([emqx_resource, emqx_connector]),
  52. ok = emqx_common_test_helpers:stop_apps([emqx_authz]).
  53. init_per_testcase(_TestCase, Config) ->
  54. ok = emqx_authz_test_lib:reset_authorizers(),
  55. Config.
  56. set_special_configs(emqx_authz) ->
  57. ok = emqx_authz_test_lib:reset_authorizers();
  58. set_special_configs(_) ->
  59. ok.
  60. %%------------------------------------------------------------------------------
  61. %% Testcases
  62. %%------------------------------------------------------------------------------
  63. t_topic_rules(_Config) ->
  64. ClientInfo = #{
  65. clientid => <<"clientid">>,
  66. username => <<"username">>,
  67. peerhost => {127, 0, 0, 1},
  68. zone => default,
  69. listener => {tcp, default}
  70. },
  71. ok = emqx_authz_test_lib:test_no_topic_rules(ClientInfo, fun setup_client_samples/2),
  72. ok = emqx_authz_test_lib:test_allow_topic_rules(ClientInfo, fun setup_client_samples/2),
  73. ok = emqx_authz_test_lib:test_deny_topic_rules(ClientInfo, fun setup_client_samples/2).
  74. t_lookups(_Config) ->
  75. ClientInfo = #{
  76. clientid => <<"clientid">>,
  77. cn => <<"cn">>,
  78. dn => <<"dn">>,
  79. username => <<"username">>,
  80. peerhost => {127, 0, 0, 1},
  81. zone => default,
  82. listener => {tcp, default}
  83. },
  84. %% by clientid
  85. ok = init_table(),
  86. ok = insert(
  87. <<
  88. "INSERT INTO acl(clientid, topic, permission, action)"
  89. "VALUES($1, $2, $3, $4)"
  90. >>,
  91. [<<"clientid">>, <<"a">>, <<"allow">>, <<"subscribe">>]
  92. ),
  93. ok = setup_config(
  94. #{
  95. <<"query">> => <<
  96. "SELECT permission, action, topic "
  97. "FROM acl WHERE clientid = ${clientid}"
  98. >>
  99. }
  100. ),
  101. ok = emqx_authz_test_lib:test_samples(
  102. ClientInfo,
  103. [
  104. {allow, subscribe, <<"a">>},
  105. {deny, subscribe, <<"b">>}
  106. ]
  107. ),
  108. %% by peerhost
  109. ok = init_table(),
  110. ok = insert(
  111. <<
  112. "INSERT INTO acl(peerhost, topic, permission, action)"
  113. "VALUES($1, $2, $3, $4)"
  114. >>,
  115. [<<"127.0.0.1">>, <<"a">>, <<"allow">>, <<"subscribe">>]
  116. ),
  117. ok = setup_config(
  118. #{
  119. <<"query">> => <<
  120. "SELECT permission, action, topic "
  121. "FROM acl WHERE peerhost = ${peerhost}"
  122. >>
  123. }
  124. ),
  125. ok = emqx_authz_test_lib:test_samples(
  126. ClientInfo,
  127. [
  128. {allow, subscribe, <<"a">>},
  129. {deny, subscribe, <<"b">>}
  130. ]
  131. ),
  132. %% by cn
  133. ok = init_table(),
  134. ok = insert(
  135. <<
  136. "INSERT INTO acl(cn, topic, permission, action)"
  137. "VALUES($1, $2, $3, $4)"
  138. >>,
  139. [<<"cn">>, <<"a">>, <<"allow">>, <<"subscribe">>]
  140. ),
  141. ok = setup_config(
  142. #{
  143. <<"query">> => <<
  144. "SELECT permission, action, topic "
  145. "FROM acl WHERE cn = ${cert_common_name}"
  146. >>
  147. }
  148. ),
  149. ok = emqx_authz_test_lib:test_samples(
  150. ClientInfo,
  151. [
  152. {allow, subscribe, <<"a">>},
  153. {deny, subscribe, <<"b">>}
  154. ]
  155. ),
  156. %% by dn
  157. ok = init_table(),
  158. ok = insert(
  159. <<
  160. "INSERT INTO acl(dn, topic, permission, action)"
  161. "VALUES($1, $2, $3, $4)"
  162. >>,
  163. [<<"dn">>, <<"a">>, <<"allow">>, <<"subscribe">>]
  164. ),
  165. ok = setup_config(
  166. #{
  167. <<"query">> => <<
  168. "SELECT permission, action, topic "
  169. "FROM acl WHERE dn = ${cert_subject}"
  170. >>
  171. }
  172. ),
  173. ok = emqx_authz_test_lib:test_samples(
  174. ClientInfo,
  175. [
  176. {allow, subscribe, <<"a">>},
  177. {deny, subscribe, <<"b">>}
  178. ]
  179. ).
  180. t_pgsql_error(_Config) ->
  181. ClientInfo = #{
  182. clientid => <<"clientid">>,
  183. username => <<"username">>,
  184. peerhost => {127, 0, 0, 1},
  185. zone => default,
  186. listener => {tcp, default}
  187. },
  188. ok = setup_config(
  189. #{
  190. <<"query">> => <<
  191. "SELECT permission, action, topic "
  192. "FROM acl WHERE clientid = ${username}"
  193. >>
  194. }
  195. ),
  196. ok = emqx_authz_test_lib:test_samples(
  197. ClientInfo,
  198. [{deny, subscribe, <<"a">>}]
  199. ).
  200. t_create_invalid(_Config) ->
  201. BadConfig = maps:merge(
  202. raw_pgsql_authz_config(),
  203. #{<<"server">> => <<"255.255.255.255:33333">>}
  204. ),
  205. {ok, _} = emqx_authz:update(?CMD_REPLACE, [BadConfig]),
  206. [_] = emqx_authz:lookup().
  207. t_nonbinary_values(_Config) ->
  208. ClientInfo = #{
  209. clientid => clientid,
  210. username => "username",
  211. peerhost => {127, 0, 0, 1},
  212. zone => default,
  213. listener => {tcp, default}
  214. },
  215. ok = init_table(),
  216. ok = insert(
  217. <<
  218. "INSERT INTO acl(clientid, username, topic, permission, action)"
  219. "VALUES($1, $2, $3, $4, $5)"
  220. >>,
  221. [<<"clientid">>, <<"username">>, <<"a">>, <<"allow">>, <<"subscribe">>]
  222. ),
  223. ok = setup_config(
  224. #{
  225. <<"query">> => <<
  226. "SELECT permission, action, topic "
  227. "FROM acl WHERE clientid = ${clientid} AND username = ${username}"
  228. >>
  229. }
  230. ),
  231. ok = emqx_authz_test_lib:test_samples(
  232. ClientInfo,
  233. [
  234. {allow, subscribe, <<"a">>},
  235. {deny, subscribe, <<"b">>}
  236. ]
  237. ).
  238. %%------------------------------------------------------------------------------
  239. %% Helpers
  240. %%------------------------------------------------------------------------------
  241. raw_pgsql_authz_config() ->
  242. #{
  243. <<"enable">> => <<"true">>,
  244. <<"type">> => <<"postgresql">>,
  245. <<"database">> => <<"mqtt">>,
  246. <<"username">> => <<"root">>,
  247. <<"password">> => <<"public">>,
  248. <<"query">> => <<
  249. "SELECT permission, action, topic "
  250. "FROM acl WHERE username = ${username}"
  251. >>,
  252. <<"server">> => pgsql_server()
  253. }.
  254. q(Sql) ->
  255. emqx_resource:query(
  256. ?PGSQL_RESOURCE,
  257. {query, Sql}
  258. ).
  259. insert(Sql, Params) ->
  260. {ok, _} = emqx_resource:query(
  261. ?PGSQL_RESOURCE,
  262. {query, Sql, Params}
  263. ),
  264. ok.
  265. init_table() ->
  266. ok = drop_table(),
  267. {ok, _, _} = q(
  268. "CREATE TABLE acl(\n"
  269. " username VARCHAR(255),\n"
  270. " clientid VARCHAR(255),\n"
  271. " peerhost VARCHAR(255),\n"
  272. " cn VARCHAR(255),\n"
  273. " dn VARCHAR(255),\n"
  274. " topic VARCHAR(255),\n"
  275. " permission VARCHAR(255),\n"
  276. " action VARCHAR(255))"
  277. ),
  278. ok.
  279. drop_table() ->
  280. {ok, _, _} = q("DROP TABLE IF EXISTS acl"),
  281. ok.
  282. setup_client_samples(ClientInfo, Samples) ->
  283. #{username := Username} = ClientInfo,
  284. ok = init_table(),
  285. ok = lists:foreach(
  286. fun(#{topics := Topics, permission := Permission, action := Action}) ->
  287. lists:foreach(
  288. fun(Topic) ->
  289. insert(
  290. <<
  291. "INSERT INTO acl(username, topic, permission, action)"
  292. "VALUES($1, $2, $3, $4)"
  293. >>,
  294. [Username, Topic, Permission, Action]
  295. )
  296. end,
  297. Topics
  298. )
  299. end,
  300. Samples
  301. ),
  302. setup_config(
  303. #{
  304. <<"query">> => <<
  305. "SELECT permission, action, topic "
  306. "FROM acl WHERE username = ${username}"
  307. >>
  308. }
  309. ).
  310. setup_config(SpecialParams) ->
  311. emqx_authz_test_lib:setup_config(
  312. raw_pgsql_authz_config(),
  313. SpecialParams
  314. ).
  315. pgsql_server() ->
  316. iolist_to_binary(io_lib:format("~s", [?PGSQL_HOST])).
  317. pgsql_config() ->
  318. #{
  319. auto_reconnect => true,
  320. database => <<"mqtt">>,
  321. username => <<"root">>,
  322. password => <<"public">>,
  323. pool_size => 8,
  324. server => {?PGSQL_HOST, ?PGSQL_DEFAULT_PORT},
  325. ssl => #{enable => false}
  326. }.
  327. start_apps(Apps) ->
  328. lists:foreach(fun application:ensure_all_started/1, Apps).
  329. stop_apps(Apps) ->
  330. lists:foreach(fun application:stop/1, Apps).