emqx_authn_pgsql_tls_SUITE.erl 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2023 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_authn_pgsql_tls_SUITE).
  17. -compile(nowarn_export_all).
  18. -compile(export_all).
  19. -include_lib("emqx_connector/include/emqx_connector.hrl").
  20. -include_lib("emqx_authn/include/emqx_authn.hrl").
  21. -include_lib("eunit/include/eunit.hrl").
  22. -include_lib("common_test/include/ct.hrl").
  23. -define(PGSQL_HOST, "pgsql-tls").
  24. -define(PATH, [authentication]).
  25. -define(ResourceID, <<"password_based:postgresql">>).
  26. all() ->
  27. emqx_common_test_helpers:all(?MODULE).
  28. groups() ->
  29. [].
  30. init_per_testcase(_, Config) ->
  31. {ok, _} = emqx_cluster_rpc:start_link(node(), emqx_cluster_rpc, 1000),
  32. emqx_authentication:initialize_authentication(?GLOBAL, []),
  33. emqx_authn_test_lib:delete_authenticators(
  34. [authentication],
  35. ?GLOBAL
  36. ),
  37. Config.
  38. init_per_suite(Config) ->
  39. _ = application:load(emqx_conf),
  40. case emqx_common_test_helpers:is_tcp_server_available(?PGSQL_HOST, ?PGSQL_DEFAULT_PORT) of
  41. true ->
  42. Apps = emqx_cth_suite:start([emqx, emqx_conf, emqx_authn], #{
  43. work_dir => ?config(priv_dir, Config)
  44. }),
  45. [{apps, Apps} | Config];
  46. false ->
  47. {skip, no_pgsql_tls}
  48. end.
  49. end_per_suite(Config) ->
  50. emqx_authn_test_lib:delete_authenticators(
  51. [authentication],
  52. ?GLOBAL
  53. ),
  54. ok = emqx_cth_suite:stop(?config(apps, Config)),
  55. ok.
  56. %%------------------------------------------------------------------------------
  57. %% Tests
  58. %%------------------------------------------------------------------------------
  59. t_create(_Config) ->
  60. %% openssl s_client -tls1_2 -cipher ECDHE-RSA-AES256-GCM-SHA384 \
  61. %% -starttls postgres -connect authn-server:5432 \
  62. %% -cert client.crt -key client.key -CAfile ca.crt
  63. ?assertMatch(
  64. {ok, _},
  65. create_pgsql_auth_with_ssl_opts(
  66. #{
  67. <<"server_name_indication">> => <<"authn-server">>,
  68. <<"verify">> => <<"verify_peer">>,
  69. <<"versions">> => [<<"tlsv1.2">>],
  70. <<"ciphers">> => [<<"ECDHE-RSA-AES256-GCM-SHA384">>]
  71. }
  72. )
  73. ).
  74. t_create_invalid(_Config) ->
  75. %% invalid server_name
  76. ?assertMatch(
  77. {ok, _},
  78. create_pgsql_auth_with_ssl_opts(
  79. #{
  80. <<"server_name_indication">> => <<"authn-server-unknown-host">>,
  81. <<"verify">> => <<"verify_peer">>
  82. }
  83. )
  84. ),
  85. emqx_authn_test_lib:delete_config(?ResourceID),
  86. %% incompatible versions
  87. ?assertMatch(
  88. {ok, _},
  89. create_pgsql_auth_with_ssl_opts(
  90. #{
  91. <<"server_name_indication">> => <<"authn-server">>,
  92. <<"verify">> => <<"verify_peer">>,
  93. <<"versions">> => [<<"tlsv1.1">>]
  94. }
  95. )
  96. ),
  97. emqx_authn_test_lib:delete_config(?ResourceID),
  98. %% incompatible ciphers
  99. ?assertMatch(
  100. {ok, _},
  101. create_pgsql_auth_with_ssl_opts(
  102. #{
  103. <<"server_name_indication">> => <<"authn-server">>,
  104. <<"verify">> => <<"verify_peer">>,
  105. <<"versions">> => [<<"tlsv1.2">>],
  106. <<"ciphers">> => [<<"ECDHE-ECDSA-AES128-GCM-SHA256">>]
  107. }
  108. )
  109. ).
  110. %%------------------------------------------------------------------------------
  111. %% Helpers
  112. %%------------------------------------------------------------------------------
  113. create_pgsql_auth_with_ssl_opts(SpecificSSLOpts) ->
  114. AuthConfig = raw_pgsql_auth_config(SpecificSSLOpts),
  115. emqx:update_config(?PATH, {create_authenticator, ?GLOBAL, AuthConfig}).
  116. raw_pgsql_auth_config(SpecificSSLOpts) ->
  117. SSLOpts = maps:merge(
  118. emqx_authn_test_lib:client_ssl_cert_opts(),
  119. #{<<"enable">> => <<"true">>}
  120. ),
  121. #{
  122. <<"mechanism">> => <<"password_based">>,
  123. <<"password_hash_algorithm">> => #{
  124. <<"name">> => <<"plain">>,
  125. <<"salt_position">> => <<"suffix">>
  126. },
  127. <<"enable">> => <<"true">>,
  128. <<"backend">> => <<"postgresql">>,
  129. <<"database">> => <<"mqtt">>,
  130. <<"username">> => <<"root">>,
  131. <<"password">> => <<"public">>,
  132. <<"query">> => <<"SELECT 1">>,
  133. <<"server">> => pgsql_server(),
  134. <<"ssl">> => maps:merge(SSLOpts, SpecificSSLOpts)
  135. }.
  136. pgsql_server() ->
  137. iolist_to_binary(io_lib:format("~s", [?PGSQL_HOST])).
  138. start_apps(Apps) ->
  139. lists:foreach(fun application:ensure_all_started/1, Apps).
  140. stop_apps(Apps) ->
  141. lists:foreach(fun application:stop/1, Apps).