emqx_authn_pgsql_tls_SUITE.erl 4.7 KB

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