emqx_authz_api_settings.erl 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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_authz_api_settings).
  17. -behaviour(minirest_api).
  18. -include_lib("hocon/include/hoconsc.hrl").
  19. -import(hoconsc, [mk/1, ref/2]).
  20. -export([
  21. api_spec/0,
  22. paths/0,
  23. schema/1
  24. ]).
  25. -export([settings/2]).
  26. -define(BAD_REQUEST, 'BAD_REQUEST').
  27. api_spec() ->
  28. emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true}).
  29. paths() ->
  30. ["/authorization/settings"].
  31. %%--------------------------------------------------------------------
  32. %% Schema for each URI
  33. %%--------------------------------------------------------------------
  34. schema("/authorization/settings") ->
  35. #{
  36. 'operationId' => settings,
  37. get =>
  38. #{
  39. description => ?DESC(authorization_settings_get),
  40. responses =>
  41. #{200 => ref_authz_schema()}
  42. },
  43. put =>
  44. #{
  45. description => ?DESC(authorization_settings_put),
  46. 'requestBody' => ref_authz_schema(),
  47. responses =>
  48. #{
  49. 200 => ref_authz_schema(),
  50. 400 => emqx_dashboard_swagger:error_codes([?BAD_REQUEST], <<"Bad Request">>)
  51. }
  52. }
  53. }.
  54. ref_authz_schema() ->
  55. emqx_schema:authz_fields().
  56. settings(get, _Params) ->
  57. {200, authorization_settings()};
  58. settings(put, #{
  59. body := #{
  60. <<"no_match">> := NoMatch,
  61. <<"deny_action">> := DenyAction,
  62. <<"cache">> := Cache
  63. }
  64. }) ->
  65. {ok, _} = emqx_authz_utils:update_config([authorization, no_match], NoMatch),
  66. {ok, _} = emqx_authz_utils:update_config(
  67. [authorization, deny_action], DenyAction
  68. ),
  69. {ok, _} = emqx_authz_utils:update_config([authorization, cache], Cache),
  70. {200, authorization_settings()}.
  71. authorization_settings() ->
  72. C = maps:remove(<<"sources">>, emqx:get_raw_config([authorization], #{})),
  73. Schema = emqx_hocon:make_schema(emqx_schema:authz_fields()),
  74. hocon_tconf:make_serializable(Schema, C, #{}).