Merge pull request #9120 from HJianBo/improving-the-retry-mechnism-plugin
Refactor auth-http plugins to support getting retry_times params
This commit is contained in:
commit
3d61211ded
|
@ -1 +1,20 @@
|
|||
%%--------------------------------------------------------------------
|
||||
%% Copyright (c) 2020-2022 EMQ Technologies Co., Ltd. All Rights Reserved.
|
||||
%%
|
||||
%% Licensed under the Apache License, Version 2.0 (the "License");
|
||||
%% you may not use this file except in compliance with the License.
|
||||
%% You may obtain a copy of the License at
|
||||
%%
|
||||
%% http://www.apache.org/licenses/LICENSE-2.0
|
||||
%%
|
||||
%% Unless required by applicable law or agreed to in writing, software
|
||||
%% distributed under the License is distributed on an "AS IS" BASIS,
|
||||
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
%% See the License for the specific language governing permissions and
|
||||
%% limitations under the License.
|
||||
%%--------------------------------------------------------------------
|
||||
|
||||
-define(APP, emqx_auth_http).
|
||||
|
||||
%% equals to the default value of ehttpc
|
||||
-define(DEFAULT_RETRY_TIMES, 2).
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
-logger_header("[ACL http]").
|
||||
|
||||
-import(emqx_auth_http_cli,
|
||||
[ request/6
|
||||
[ request/7
|
||||
, feedvar/2
|
||||
]).
|
||||
|
||||
|
@ -56,13 +56,15 @@ description() -> "ACL with HTTP API".
|
|||
%% Internal functions
|
||||
%%--------------------------------------------------------------------
|
||||
|
||||
check_acl_request(#{pool_name := PoolName,
|
||||
check_acl_request(ACLParams =
|
||||
#{pool_name := PoolName,
|
||||
path := Path,
|
||||
method := Method,
|
||||
headers := Headers,
|
||||
params := Params,
|
||||
timeout := Timeout}, ClientInfo) ->
|
||||
request(PoolName, Method, Path, Headers, feedvar(Params, ClientInfo), Timeout).
|
||||
Retry = maps:get(retry_times, ACLParams, ?DEFAULT_RETRY_TIMES),
|
||||
request(PoolName, Method, Path, Headers, feedvar(Params, ClientInfo), Timeout, Retry).
|
||||
|
||||
access(subscribe) -> 1;
|
||||
access(publish) -> 2.
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{application, emqx_auth_http,
|
||||
[{description, "EMQ X Authentication/ACL with HTTP API"},
|
||||
{vsn, "4.3.7"}, % strict semver, bump manually!
|
||||
{vsn, "4.3.8"}, % strict semver, bump manually!
|
||||
{modules, []},
|
||||
{registered, [emqx_auth_http_sup]},
|
||||
{applications, [kernel,stdlib,ehttpc]},
|
||||
|
|
|
@ -1,17 +1,27 @@
|
|||
%% -*- mode: erlang -*-
|
||||
%% Unless you know what you are doing, DO NOT edit manually!!
|
||||
{VSN,
|
||||
[{"4.3.6",
|
||||
[ %% There are only changes to the schema file, so we don't need any
|
||||
%% commands here
|
||||
]},
|
||||
[{"4.3.7",
|
||||
[{load_module,emqx_auth_http_cli,brutal_purge,soft_purge,[]},
|
||||
{load_module,emqx_auth_http,brutal_purge,soft_purge,[]},
|
||||
{load_module,emqx_acl_http,brutal_purge,soft_purge,[]}]},
|
||||
{"4.3.6",
|
||||
[{load_module,emqx_auth_http_cli,brutal_purge,soft_purge,[]},
|
||||
{load_module,emqx_auth_http,brutal_purge,soft_purge,[]},
|
||||
{load_module,emqx_acl_http,brutal_purge,soft_purge,[]}]},
|
||||
{"4.3.5",
|
||||
[{load_module,emqx_auth_http_app,brutal_purge,soft_purge,[]},
|
||||
[{load_module,emqx_auth_http_cli,brutal_purge,soft_purge,[]},
|
||||
{load_module,emqx_acl_http,brutal_purge,soft_purge,[]},
|
||||
{load_module,emqx_auth_http_app,brutal_purge,soft_purge,[]},
|
||||
{load_module,emqx_auth_http,brutal_purge,soft_purge,[]}]},
|
||||
{"4.3.4",
|
||||
[{load_module,emqx_auth_http_app,brutal_purge,soft_purge,[]},
|
||||
[{load_module,emqx_auth_http_cli,brutal_purge,soft_purge,[]},
|
||||
{load_module,emqx_acl_http,brutal_purge,soft_purge,[]},
|
||||
{load_module,emqx_auth_http_app,brutal_purge,soft_purge,[]},
|
||||
{load_module,emqx_auth_http,brutal_purge,soft_purge,[]}]},
|
||||
{"4.3.3",
|
||||
[{load_module,emqx_auth_http_app,brutal_purge,soft_purge,[]},
|
||||
[{load_module,emqx_auth_http_cli,brutal_purge,soft_purge,[]},
|
||||
{load_module,emqx_auth_http_app,brutal_purge,soft_purge,[]},
|
||||
{load_module,emqx_auth_http,brutal_purge,soft_purge,[]},
|
||||
{load_module,emqx_acl_http,brutal_purge,soft_purge,[]}]},
|
||||
{"4.3.2",
|
||||
|
@ -20,21 +30,29 @@
|
|||
{load_module,emqx_auth_http,brutal_purge,soft_purge,[]},
|
||||
{load_module,emqx_acl_http,brutal_purge,soft_purge,[]},
|
||||
{load_module,emqx_auth_http_cli,brutal_purge,soft_purge,[]}]},
|
||||
{<<"4.3.[0-1]">>,
|
||||
[{restart_application,emqx_auth_http}]},
|
||||
{<<"4.3.[0-1]">>,[{restart_application,emqx_auth_http}]},
|
||||
{<<".*">>,[]}],
|
||||
[{"4.3.6",
|
||||
[ %% There are only changes to the schema file, so we don't need any
|
||||
%% commands here
|
||||
]},
|
||||
[{"4.3.7",
|
||||
[{load_module,emqx_auth_http_cli,brutal_purge,soft_purge,[]},
|
||||
{load_module,emqx_auth_http,brutal_purge,soft_purge,[]},
|
||||
{load_module,emqx_acl_http,brutal_purge,soft_purge,[]}]},
|
||||
{"4.3.6",
|
||||
[{load_module,emqx_auth_http_cli,brutal_purge,soft_purge,[]},
|
||||
{load_module,emqx_auth_http,brutal_purge,soft_purge,[]},
|
||||
{load_module,emqx_acl_http,brutal_purge,soft_purge,[]}]},
|
||||
{"4.3.5",
|
||||
[{load_module,emqx_auth_http_app,brutal_purge,soft_purge,[]},
|
||||
[{load_module,emqx_auth_http_cli,brutal_purge,soft_purge,[]},
|
||||
{load_module,emqx_acl_http,brutal_purge,soft_purge,[]},
|
||||
{load_module,emqx_auth_http_app,brutal_purge,soft_purge,[]},
|
||||
{load_module,emqx_auth_http,brutal_purge,soft_purge,[]}]},
|
||||
{"4.3.4",
|
||||
[{load_module,emqx_auth_http_app,brutal_purge,soft_purge,[]},
|
||||
[{load_module,emqx_auth_http_cli,brutal_purge,soft_purge,[]},
|
||||
{load_module,emqx_acl_http,brutal_purge,soft_purge,[]},
|
||||
{load_module,emqx_auth_http_app,brutal_purge,soft_purge,[]},
|
||||
{load_module,emqx_auth_http,brutal_purge,soft_purge,[]}]},
|
||||
{"4.3.3",
|
||||
[{load_module,emqx_auth_http_app,brutal_purge,soft_purge,[]},
|
||||
[{load_module,emqx_auth_http_cli,brutal_purge,soft_purge,[]},
|
||||
{load_module,emqx_auth_http_app,brutal_purge,soft_purge,[]},
|
||||
{load_module,emqx_auth_http,brutal_purge,soft_purge,[]},
|
||||
{load_module,emqx_acl_http,brutal_purge,soft_purge,[]}]},
|
||||
{"4.3.2",
|
||||
|
@ -43,6 +61,5 @@
|
|||
{load_module,emqx_auth_http,brutal_purge,soft_purge,[]},
|
||||
{load_module,emqx_acl_http,brutal_purge,soft_purge,[]},
|
||||
{load_module,emqx_auth_http_cli,brutal_purge,soft_purge,[]}]},
|
||||
{<<"4.3.[0-1]">>,
|
||||
[{restart_application,emqx_auth_http}]},
|
||||
{<<"4.3.[0-1]">>,[{restart_application,emqx_auth_http}]},
|
||||
{<<".*">>,[]}]}.
|
||||
|
|
|
@ -25,7 +25,7 @@
|
|||
-logger_header("[Auth http]").
|
||||
|
||||
-import(emqx_auth_http_cli,
|
||||
[ request/6
|
||||
[ request/7
|
||||
, feedvar/2
|
||||
]).
|
||||
|
||||
|
@ -63,24 +63,28 @@ description() -> "Authentication by HTTP API".
|
|||
%% Requests
|
||||
%%--------------------------------------------------------------------
|
||||
|
||||
authenticate(#{pool_name := PoolName,
|
||||
authenticate(AuthParams =
|
||||
#{pool_name := PoolName,
|
||||
path := Path,
|
||||
method := Method,
|
||||
headers := Headers,
|
||||
params := Params,
|
||||
timeout := Timeout}, ClientInfo) ->
|
||||
request(PoolName, Method, Path, Headers, feedvar(Params, ClientInfo), Timeout).
|
||||
Retry = maps:get(retry_times, AuthParams, ?DEFAULT_RETRY_TIMES),
|
||||
request(PoolName, Method, Path, Headers, feedvar(Params, ClientInfo), Timeout, Retry).
|
||||
|
||||
-spec(is_superuser(maybe(map()), emqx_types:client()) -> boolean()).
|
||||
is_superuser(undefined, _ClientInfo) ->
|
||||
false;
|
||||
is_superuser(#{pool_name := PoolName,
|
||||
is_superuser(SuperParams =
|
||||
#{pool_name := PoolName,
|
||||
path := Path,
|
||||
method := Method,
|
||||
headers := Headers,
|
||||
params := Params,
|
||||
timeout := Timeout}, ClientInfo) ->
|
||||
case request(PoolName, Method, Path, Headers, feedvar(Params, ClientInfo), Timeout) of
|
||||
Retry = maps:get(retry_times, SuperParams, ?DEFAULT_RETRY_TIMES),
|
||||
case request(PoolName, Method, Path, Headers, feedvar(Params, ClientInfo), Timeout, Retry) of
|
||||
{ok, 200, _Body} -> true;
|
||||
{ok, _Code, _Body} -> false;
|
||||
{error, Error} -> ?LOG(error, "Request superuser path ~s, error: ~p", [Path, Error]),
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
-include("emqx_auth_http.hrl").
|
||||
|
||||
-export([ request/6
|
||||
, request/7
|
||||
, feedvar/2
|
||||
, feedvar/3
|
||||
]).
|
||||
|
@ -27,18 +28,21 @@
|
|||
%% HTTP Request
|
||||
%%--------------------------------------------------------------------
|
||||
|
||||
request(PoolName, get, Path, Headers, Params, Timeout) ->
|
||||
NewPath = Path ++ "?" ++ binary_to_list(cow_qs:qs(bin_kw(Params))),
|
||||
reply(ehttpc:request(PoolName, get, {NewPath, Headers}, Timeout));
|
||||
request(PoolName, Method, Path, Headers, Params, Timeout) ->
|
||||
request(PoolName, Method, Path, Headers, Params, Timeout, ?DEFAULT_RETRY_TIMES).
|
||||
|
||||
request(PoolName, post, Path, Headers, Params, Timeout) ->
|
||||
request(PoolName, get, Path, Headers, Params, Timeout, Retry) ->
|
||||
NewPath = Path ++ "?" ++ binary_to_list(cow_qs:qs(bin_kw(Params))),
|
||||
reply(ehttpc:request(PoolName, get, {NewPath, Headers}, Timeout, Retry));
|
||||
|
||||
request(PoolName, post, Path, Headers, Params, Timeout, Retry) ->
|
||||
Body = case proplists:get_value(<<"content-type">>, Headers) of
|
||||
"application/x-www-form-urlencoded" ->
|
||||
cow_qs:qs(bin_kw(Params));
|
||||
"application/json" ->
|
||||
emqx_json:encode(bin_kw(Params))
|
||||
end,
|
||||
reply(ehttpc:request(PoolName, post, {Path, Headers, Body}, Timeout)).
|
||||
reply(ehttpc:request(PoolName, post, {Path, Headers, Body}, Timeout, Retry)).
|
||||
|
||||
reply({ok, StatusCode, _Headers}) ->
|
||||
{ok, StatusCode, <<>>};
|
||||
|
|
|
@ -408,7 +408,7 @@ t_password_hash(_) ->
|
|||
ok = application:start(emqx_auth_mnesia).
|
||||
|
||||
t_will_message_connection_denied(Config) when is_list(Config) ->
|
||||
ClientId = Username = <<"subscriber">>,
|
||||
ClientId = <<"subscriber">>,
|
||||
Password = <<"p">>,
|
||||
application:stop(emqx_auth_mnesia),
|
||||
ok = emqx_ct_helpers:start_apps([emqx_auth_mnesia]),
|
||||
|
|
Loading…
Reference in New Issue