emqx_plugin_libs_ssl.erl 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2021-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_plugin_libs_ssl).
  17. -export([save_files_return_opts/2,
  18. save_files_return_opts/3,
  19. save_file/2
  20. ]).
  21. -type file_input_key() :: atom() | binary(). %% <<"file">> | <<"filename">>
  22. -type file_input() :: #{file_input_key() => binary()}.
  23. %% options are below paris
  24. %% <<"keyfile">> => file_input()
  25. %% <<"certfile">> => file_input()
  26. %% <<"cafile">> => file_input() %% backward compatible
  27. %% <<"cacertfile">> => file_input()
  28. %% <<"verify">> => boolean()
  29. %% <<"tls_versions">> => binary()
  30. %% <<"ciphers">> => binary()
  31. -type opts_key() :: binary() | atom().
  32. -type opts_input() :: #{opts_key() => term()}.
  33. -type opt_key() :: keyfile | certfile | cacertfile | verify | versions | ciphers.
  34. -type opt_value() :: term().
  35. -type opts() :: [{opt_key(), opt_value()}].
  36. %% @doc Parse ssl options input.
  37. %% If the input contains file content, save the files in the given dir.
  38. %% Returns ssl options for Erlang's ssl application.
  39. %%
  40. %% For SSL files in the input Option, it can either be a file path
  41. %% or a map like `#{filename := FileName, file := Content}`.
  42. %% In case it's a map, the file is saved in EMQX's `data_dir'
  43. %% (unless `SubDir' is an absolute path).
  44. %% NOTE: This function is now deprecated, use emqx_tls_lib:ensure_ssl_files/2 instead.
  45. -spec save_files_return_opts(opts_input(), atom() | string() | binary(),
  46. string() | binary()) -> opts().
  47. save_files_return_opts(Options, SubDir, ResId) ->
  48. Dir = filename:join([emqx:data_dir(), SubDir, ResId]),
  49. save_files_return_opts(Options, Dir).
  50. %% @doc Parse ssl options input.
  51. %% If the input contains file content, save the files in the given dir.
  52. %% Returns ssl options for Erlang's ssl application.
  53. %%
  54. %% For SSL files in the input Option, it can either be a file path
  55. %% or a map like `#{filename := FileName, file := Content}`.
  56. %% In case it's a map, the file is saved in EMQX's `data_dir'
  57. %% (unless `SubDir' is an absolute path).
  58. %% NOTE: This function is now deprecated, use emqx_tls_lib:ensure_ssl_files/2 instead.
  59. -spec save_files_return_opts(opts_input(), file:name_all()) -> opts().
  60. save_files_return_opts(Options, Dir) ->
  61. GetD = fun(Key, Default) -> fuzzy_map_get(Key, Options, Default) end,
  62. Get = fun(Key) -> GetD(Key, undefined) end,
  63. KeyFile = Get(keyfile),
  64. CertFile = Get(certfile),
  65. CAFile = Get(cacertfile),
  66. Key = do_save_file(KeyFile, Dir),
  67. Cert = do_save_file(CertFile, Dir),
  68. CA = do_save_file(CAFile, Dir),
  69. Verify = GetD(verify, verify_none),
  70. SNI = Get(server_name_indication),
  71. Versions = emqx_tls_lib:integral_versions(Get(versions)),
  72. Ciphers = emqx_tls_lib:integral_ciphers(Versions, Get(ciphers)),
  73. filter([{keyfile, Key}, {certfile, Cert}, {cacertfile, CA},
  74. {verify, Verify}, {server_name_indication, SNI}, {versions, Versions}, {ciphers, Ciphers}]).
  75. %% @doc Save a key or certificate file in data dir,
  76. %% and return path of the saved file.
  77. %% empty string is returned if the input is empty.
  78. -spec save_file(file_input(), atom() | string() | binary()) -> string().
  79. save_file(Param, SubDir) ->
  80. Dir = filename:join([emqx:data_dir(), SubDir]),
  81. do_save_file(Param, Dir).
  82. filter([]) -> [];
  83. filter([{_, undefined} | T]) -> filter(T);
  84. filter([{_, ""} | T]) -> filter(T);
  85. filter([H | T]) -> [H | filter(T)].
  86. do_save_file(#{filename := FileName, file := Content}, Dir)
  87. when FileName =/= undefined andalso Content =/= undefined ->
  88. do_save_file(ensure_str(FileName), iolist_to_binary(Content), Dir);
  89. do_save_file(FilePath, _) when is_list(FilePath) ->
  90. FilePath;
  91. do_save_file(FilePath, _) when is_binary(FilePath) ->
  92. ensure_str(FilePath);
  93. do_save_file(_, _) -> "".
  94. do_save_file("", _, _Dir) -> ""; %% ignore
  95. do_save_file(_, <<>>, _Dir) -> ""; %% ignore
  96. do_save_file(FileName, Content, Dir) ->
  97. FullFilename = filename:join([Dir, FileName]),
  98. ok = filelib:ensure_dir(FullFilename),
  99. case file:write_file(FullFilename, Content) of
  100. ok ->
  101. ensure_str(FullFilename);
  102. {error, Reason} ->
  103. logger:error("failed_to_save_ssl_file ~ts: ~0p", [FullFilename, Reason]),
  104. error({"failed_to_save_ssl_file", FullFilename, Reason})
  105. end.
  106. ensure_str(L) when is_list(L) -> L;
  107. ensure_str(B) when is_binary(B) -> unicode:characters_to_list(B, utf8).
  108. -spec fuzzy_map_get(atom() | binary(), map(), any()) -> any().
  109. fuzzy_map_get(Key, Options, Default) ->
  110. case maps:find(Key, Options) of
  111. {ok, Val} -> Val;
  112. error when is_atom(Key) ->
  113. fuzzy_map_get(atom_to_binary(Key, utf8), Options, Default);
  114. error -> Default
  115. end.