chore(style): improve the codes style
This commit is contained in:
parent
6a83cf3f35
commit
f0993c6b0f
|
@ -164,22 +164,32 @@ t_check_auth(_) ->
|
||||||
BcryptFoo = #{clientid => <<"bcrypt_foo">>, username => <<"bcrypt_foo">>, zone => external},
|
BcryptFoo = #{clientid => <<"bcrypt_foo">>, username => <<"bcrypt_foo">>, zone => external},
|
||||||
User1 = #{clientid => <<"bcrypt_foo">>, username => <<"user">>, zone => external},
|
User1 = #{clientid => <<"bcrypt_foo">>, username => <<"user">>, zone => external},
|
||||||
Bcrypt = #{clientid => <<"bcrypt">>, username => <<"bcrypt">>, zone => external},
|
Bcrypt = #{clientid => <<"bcrypt">>, username => <<"bcrypt">>, zone => external},
|
||||||
%
|
BcryptWrong = #{clientid => <<"bcrypt_wrong">>, username => <<"bcrypt_wrong">>, zone => external},
|
||||||
reload([{password_hash, plain}]),
|
reload([{password_hash, plain}]),
|
||||||
{ok, #{is_superuser := true}} = emqx_access_control:authenticate(Plain#{password => <<"plain">>}),
|
{ok,#{is_superuser := true}} =
|
||||||
|
emqx_access_control:authenticate(Plain#{password => <<"plain">>}),
|
||||||
reload([{password_hash, md5}]),
|
reload([{password_hash, md5}]),
|
||||||
{ok, #{is_superuser := false}} = emqx_access_control:authenticate(Md5#{password => <<"md5">>}),
|
{ok,#{is_superuser := false}} =
|
||||||
|
emqx_access_control:authenticate(Md5#{password => <<"md5">>}),
|
||||||
reload([{password_hash, sha}]),
|
reload([{password_hash, sha}]),
|
||||||
{ok, #{is_superuser := false}} = emqx_access_control:authenticate(Sha#{password => <<"sha">>}),
|
{ok,#{is_superuser := false}} =
|
||||||
|
emqx_access_control:authenticate(Sha#{password => <<"sha">>}),
|
||||||
reload([{password_hash, sha256}]),
|
reload([{password_hash, sha256}]),
|
||||||
{ok, #{is_superuser := false}} = emqx_access_control:authenticate(Sha256#{password => <<"sha256">>}),
|
{ok,#{is_superuser := false}} =
|
||||||
|
emqx_access_control:authenticate(Sha256#{password => <<"sha256">>}),
|
||||||
reload([{password_hash, bcrypt}]),
|
reload([{password_hash, bcrypt}]),
|
||||||
{ok, #{is_superuser := false}} = emqx_access_control:authenticate(Bcrypt#{password => <<"password">>}),
|
{ok,#{is_superuser := false}} =
|
||||||
|
emqx_access_control:authenticate(Bcrypt#{password => <<"password">>}),
|
||||||
reload([{password_hash, {pbkdf2, sha, 1, 16}}, {auth_query, "select password, salt from mqtt_user where username = '%u' limit 1"}]),
|
{error, not_authorized} =
|
||||||
{ok, #{is_superuser := false}} = emqx_access_control:authenticate(Pbkdf2#{password => <<"password">>}),
|
emqx_access_control:authenticate(BcryptWrong#{password => <<"password">>}),
|
||||||
|
%%pbkdf2 sha
|
||||||
|
reload([{password_hash, {pbkdf2, sha, 1, 16}},
|
||||||
|
{auth_query, "select password, salt from mqtt_user where username = '%u' limit 1"}]),
|
||||||
|
{ok,#{is_superuser := false}} =
|
||||||
|
emqx_access_control:authenticate(Pbkdf2#{password => <<"password">>}),
|
||||||
reload([{password_hash, {salt, bcrypt}}]),
|
reload([{password_hash, {salt, bcrypt}}]),
|
||||||
{ok, #{is_superuser := false}} = emqx_access_control:authenticate(BcryptFoo#{password => <<"foo">>}),
|
{ok,#{is_superuser := false}} =
|
||||||
|
emqx_access_control:authenticate(BcryptFoo#{password => <<"foo">>}),
|
||||||
{error, _} = emqx_access_control:authenticate(User1#{password => <<"foo">>}),
|
{error, _} = emqx_access_control:authenticate(User1#{password => <<"foo">>}),
|
||||||
{error, not_authorized} = emqx_access_control:authenticate(Bcrypt#{password => <<"password">>}).
|
{error, not_authorized} = emqx_access_control:authenticate(Bcrypt#{password => <<"password">>}).
|
||||||
|
|
||||||
|
|
|
@ -121,6 +121,7 @@ t_placeholders(_) ->
|
||||||
emqx_access_control:authenticate(ClientA#{password => <<"plain">>}),
|
emqx_access_control:authenticate(ClientA#{password => <<"plain">>}),
|
||||||
{ok, _} =
|
{ok, _} =
|
||||||
emqx_access_control:authenticate(ClientA#{password => <<"plain">>, peerhost => {192,168,1,5}}).
|
emqx_access_control:authenticate(ClientA#{password => <<"plain">>, peerhost => {192,168,1,5}}).
|
||||||
|
|
||||||
t_check_auth(_) ->
|
t_check_auth(_) ->
|
||||||
Plain = #{clientid => <<"client1">>, username => <<"plain">>, zone => external},
|
Plain = #{clientid => <<"client1">>, username => <<"plain">>, zone => external},
|
||||||
Md5 = #{clientid => <<"md5">>, username => <<"md5">>, zone => external},
|
Md5 = #{clientid => <<"md5">>, username => <<"md5">>, zone => external},
|
||||||
|
|
|
@ -113,7 +113,7 @@ on_resource_create(_Name, Conf) ->
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
%% Action 'inspect'
|
%% Action 'inspect'
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
-spec on_action_create_inspect(action_instance_id(), Params :: map()) -> {bindings(), NewParams :: map()}.
|
-spec on_action_create_inspect(Id :: action_instance_id(), Params :: map()) -> {bindings(), NewParams :: map()}.
|
||||||
on_action_create_inspect(Id, Params) ->
|
on_action_create_inspect(Id, Params) ->
|
||||||
Params.
|
Params.
|
||||||
|
|
||||||
|
|
|
@ -43,7 +43,6 @@
|
||||||
]).
|
]).
|
||||||
|
|
||||||
-export([ init_resource/4
|
-export([ init_resource/4
|
||||||
, init_resource/5
|
|
||||||
, init_action/4
|
, init_action/4
|
||||||
, clear_resource/3
|
, clear_resource/3
|
||||||
, clear_rule/1
|
, clear_rule/1
|
||||||
|
@ -239,7 +238,7 @@ create_resource(#{type := Type, config := Config0} = Params) ->
|
||||||
ok = emqx_rule_registry:add_resource(Resource),
|
ok = emqx_rule_registry:add_resource(Resource),
|
||||||
%% Note that we will return OK in case of resource creation failure,
|
%% Note that we will return OK in case of resource creation failure,
|
||||||
%% A timer is started to re-start the resource later.
|
%% A timer is started to re-start the resource later.
|
||||||
catch cluster_call(init_resource, [M, F, ResId, Config, true]),
|
catch cluster_call(init_resource, [M, F, ResId, Config]),
|
||||||
{ok, Resource};
|
{ok, Resource};
|
||||||
not_found ->
|
not_found ->
|
||||||
{error, {resource_type_not_found, Type}}
|
{error, {resource_type_not_found, Type}}
|
||||||
|
@ -382,24 +381,23 @@ delete_resource(ResId) ->
|
||||||
|
|
||||||
-spec(refresh_resources() -> ok).
|
-spec(refresh_resources() -> ok).
|
||||||
refresh_resources() ->
|
refresh_resources() ->
|
||||||
lists:foreach(fun(#resource{id = ResId} = Res) ->
|
lists:foreach(fun refresh_resource/1,
|
||||||
try refresh_resource(Res)
|
emqx_rule_registry:get_resources()).
|
||||||
|
|
||||||
|
refresh_resource(Type) when is_atom(Type) ->
|
||||||
|
lists:foreach(fun refresh_resource/1,
|
||||||
|
emqx_rule_registry:get_resources_by_type(Type));
|
||||||
|
|
||||||
|
refresh_resource(#resource{id = ResId, config = Config, type = Type}) ->
|
||||||
|
{ok, #resource_type{on_create = {M, F}}} = emqx_rule_registry:find_resource_type(Type),
|
||||||
|
try cluster_call(init_resource, [M, F, ResId, Config])
|
||||||
catch Error:Reason:ST ->
|
catch Error:Reason:ST ->
|
||||||
logger:critical(
|
logger:critical(
|
||||||
"Can not re-stablish resource ~p: ~0p. The resource is disconnected."
|
"Can not re-stablish resource ~p: ~0p. The resource is disconnected."
|
||||||
"Fix the issue and establish it manually.\n"
|
"Fix the issue and establish it manually.\n"
|
||||||
"Stacktrace: ~0p",
|
"Stacktrace: ~0p",
|
||||||
[ResId, {Error, Reason}, ST])
|
[ResId, {Error, Reason}, ST])
|
||||||
end
|
end.
|
||||||
end, emqx_rule_registry:get_resources()).
|
|
||||||
|
|
||||||
refresh_resource(Type) when is_atom(Type) ->
|
|
||||||
lists:foreach(fun(Resource) ->
|
|
||||||
refresh_resource(Resource)
|
|
||||||
end, emqx_rule_registry:get_resources_by_type(Type));
|
|
||||||
refresh_resource(#resource{id = ResId, config = Config, type = Type}) ->
|
|
||||||
{ok, #resource_type{on_create = {M, F}}} = emqx_rule_registry:find_resource_type(Type),
|
|
||||||
cluster_call(init_resource, [M, F, ResId, Config]).
|
|
||||||
|
|
||||||
-spec(refresh_rules() -> ok).
|
-spec(refresh_rules() -> ok).
|
||||||
refresh_rules() ->
|
refresh_rules() ->
|
||||||
|
@ -531,14 +529,11 @@ cluster_call(Func, Args) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
init_resource(Module, OnCreate, ResId, Config) ->
|
init_resource(Module, OnCreate, ResId, Config) ->
|
||||||
init_resource(Module, OnCreate, ResId, Config, false).
|
|
||||||
|
|
||||||
init_resource(Module, OnCreate, ResId, Config, Restart) ->
|
|
||||||
Params = ?RAISE(
|
Params = ?RAISE(
|
||||||
Module:OnCreate(ResId, Config),
|
begin
|
||||||
Restart andalso
|
emqx_rule_registry:find_resource(ResId) /= not_found
|
||||||
timer:apply_after(timer:seconds(60), ?MODULE, init_resource,
|
andalso Module:OnCreate(ResId, Config)
|
||||||
[Module, OnCreate, ResId, Config, Restart]),
|
end,
|
||||||
{{Module, OnCreate}, {_EXCLASS_, _EXCPTION_, _ST_}}),
|
{{Module, OnCreate}, {_EXCLASS_, _EXCPTION_, _ST_}}),
|
||||||
ResParams = #resource_params{id = ResId,
|
ResParams = #resource_params{id = ResId,
|
||||||
params = Params,
|
params = Params,
|
||||||
|
|
Loading…
Reference in New Issue