emqx_authn_postgresql_tls_SUITE.erl 5.1 KB

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