emqx_authz_utils.erl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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_authz_utils).
  17. -include_lib("emqx/include/emqx_placeholder.hrl").
  18. -include_lib("emqx_authz.hrl").
  19. -export([ cleanup_resources/0
  20. , make_resource_id/1
  21. , create_resource/2
  22. , update_config/2
  23. , parse_deep/2
  24. , parse_sql/3
  25. , render_deep/2
  26. , render_sql_params/2
  27. ]).
  28. %%------------------------------------------------------------------------------
  29. %% APIs
  30. %%------------------------------------------------------------------------------
  31. create_resource(Module, Config) ->
  32. ResourceID = make_resource_id(Module),
  33. case emqx_resource:create_local(ResourceID, ?RESOURCE_GROUP, Module, Config) of
  34. {ok, already_created} -> {ok, ResourceID};
  35. {ok, _} -> {ok, ResourceID};
  36. {error, Reason} -> {error, Reason}
  37. end.
  38. cleanup_resources() ->
  39. lists:foreach(
  40. fun emqx_resource:remove_local/1,
  41. emqx_resource:list_group_instances(?RESOURCE_GROUP)).
  42. make_resource_id(Name) ->
  43. NameBin = bin(Name),
  44. emqx_resource:generate_id(NameBin).
  45. update_config(Path, ConfigRequest) ->
  46. emqx_conf:update(Path, ConfigRequest, #{rawconf_with_defaults => true,
  47. override_to => cluster}).
  48. parse_deep(Template, PlaceHolders) ->
  49. emqx_placeholder:preproc_tmpl_deep(Template, #{placeholders => PlaceHolders}).
  50. parse_sql(Template, ReplaceWith, PlaceHolders) ->
  51. emqx_placeholder:preproc_sql(
  52. Template,
  53. #{replace_with => ReplaceWith,
  54. placeholders => PlaceHolders}).
  55. render_deep(Template, Values) ->
  56. emqx_placeholder:proc_tmpl_deep(
  57. Template,
  58. client_vars(Values),
  59. #{return => full_binary, var_trans => fun handle_var/2}).
  60. render_sql_params(ParamList, Values) ->
  61. emqx_placeholder:proc_tmpl(
  62. ParamList,
  63. client_vars(Values),
  64. #{return => rawlist, var_trans => fun handle_sql_var/2}).
  65. %%------------------------------------------------------------------------------
  66. %% Internal functions
  67. %%------------------------------------------------------------------------------
  68. client_vars(ClientInfo) ->
  69. maps:from_list(
  70. lists:map(
  71. fun convert_client_var/1,
  72. maps:to_list(ClientInfo))).
  73. convert_client_var({cn, CN}) -> {cert_common_name, CN};
  74. convert_client_var({dn, DN}) -> {cert_subject, DN};
  75. convert_client_var({protocol, Proto}) -> {proto_name, Proto};
  76. convert_client_var(Other) -> Other.
  77. handle_var({var, _Name}, undefined) ->
  78. "undefined";
  79. handle_var({var, <<"peerhost">>}, IpAddr) ->
  80. inet_parse:ntoa(IpAddr);
  81. handle_var(_Name, Value) ->
  82. emqx_placeholder:bin(Value).
  83. handle_sql_var({var, _Name}, undefined) ->
  84. "undefined";
  85. handle_sql_var({var, <<"peerhost">>}, IpAddr) ->
  86. inet_parse:ntoa(IpAddr);
  87. handle_sql_var(_Name, Value) ->
  88. emqx_placeholder:sql_data(Value).
  89. bin(A) when is_atom(A) -> atom_to_binary(A, utf8);
  90. bin(L) when is_list(L) -> list_to_binary(L);
  91. bin(X) -> X.