feat(authz): add id for every rule
This commit is contained in:
parent
4dea41f8a2
commit
8ba0890ce5
|
@ -83,19 +83,22 @@ find_action_in_hooks() ->
|
||||||
[Action] = [Action || {callback,{?MODULE, authorize, _} = Action, _, _} <- Callbacks ],
|
[Action] = [Action || {callback,{?MODULE, authorize, _} = Action, _, _} <- Callbacks ],
|
||||||
Action.
|
Action.
|
||||||
|
|
||||||
|
gen_id(Type) ->
|
||||||
|
iolist_to_binary([io_lib:format("~s_~s",[?APP, Type]), "_", integer_to_list(erlang:system_time())]).
|
||||||
|
|
||||||
create_resource(#{type := DB,
|
create_resource(#{type := DB,
|
||||||
config := Config
|
config := Config
|
||||||
} = Rule) ->
|
} = Rule) ->
|
||||||
ResourceID = iolist_to_binary([io_lib:format("~s_~s",[?APP, DB]), "_", integer_to_list(erlang:system_time())]),
|
ResourceID = gen_id(DB),
|
||||||
case emqx_resource:create(
|
case emqx_resource:create(
|
||||||
ResourceID,
|
ResourceID,
|
||||||
list_to_existing_atom(io_lib:format("~s_~s",[emqx_connector, DB])),
|
list_to_existing_atom(io_lib:format("~s_~s",[emqx_connector, DB])),
|
||||||
Config)
|
Config)
|
||||||
of
|
of
|
||||||
{ok, _} ->
|
{ok, _} ->
|
||||||
Rule#{resource_id => ResourceID};
|
Rule#{id => ResourceID};
|
||||||
{error, already_created} ->
|
{error, already_created} ->
|
||||||
Rule#{resource_id => ResourceID};
|
Rule#{id => ResourceID};
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
error({load_config_error, Reason})
|
error({load_config_error, Reason})
|
||||||
end.
|
end.
|
||||||
|
@ -108,7 +111,8 @@ init_rule(#{topics := Topics,
|
||||||
} = Rule) when ?ALLOW_DENY(Permission), ?PUBSUB(Action), is_list(Topics) ->
|
} = Rule) when ?ALLOW_DENY(Permission), ?PUBSUB(Action), is_list(Topics) ->
|
||||||
NTopics = [compile_topic(Topic) || Topic <- Topics],
|
NTopics = [compile_topic(Topic) || Topic <- Topics],
|
||||||
Rule#{principal => compile_principal(Principal),
|
Rule#{principal => compile_principal(Principal),
|
||||||
topics => NTopics
|
topics => NTopics,
|
||||||
|
id => gen_id(simple)
|
||||||
};
|
};
|
||||||
|
|
||||||
init_rule(#{principal := Principal,
|
init_rule(#{principal := Principal,
|
||||||
|
@ -199,7 +203,8 @@ authorize(#{username := Username,
|
||||||
|
|
||||||
do_authorize(Client, PubSub, Topic,
|
do_authorize(Client, PubSub, Topic,
|
||||||
[Connector = #{principal := Principal,
|
[Connector = #{principal := Principal,
|
||||||
type := DB} | Tail] ) ->
|
type := DB,
|
||||||
|
enable := true} | Tail] ) ->
|
||||||
case match_principal(Client, Principal) of
|
case match_principal(Client, Principal) of
|
||||||
true ->
|
true ->
|
||||||
Mod = list_to_existing_atom(io_lib:format("~s_~s",[emqx_authz, DB])),
|
Mod = list_to_existing_atom(io_lib:format("~s_~s",[emqx_authz, DB])),
|
||||||
|
|
|
@ -34,7 +34,7 @@ description() ->
|
||||||
"AuthZ with http".
|
"AuthZ with http".
|
||||||
|
|
||||||
authorize(Client, PubSub, Topic,
|
authorize(Client, PubSub, Topic,
|
||||||
#{resource_id := ResourceID,
|
#{id := ResourceID,
|
||||||
type := http,
|
type := http,
|
||||||
config := #{url := #{path := Path} = Url,
|
config := #{url := #{path := Path} = Url,
|
||||||
headers := Headers,
|
headers := Headers,
|
||||||
|
|
|
@ -34,7 +34,7 @@ description() ->
|
||||||
"AuthZ with Mongo".
|
"AuthZ with Mongo".
|
||||||
|
|
||||||
authorize(Client, PubSub, Topic,
|
authorize(Client, PubSub, Topic,
|
||||||
#{resource_id := ResourceID,
|
#{id := ResourceID,
|
||||||
collection := Collection,
|
collection := Collection,
|
||||||
find := Find
|
find := Find
|
||||||
}) ->
|
}) ->
|
||||||
|
|
|
@ -46,7 +46,7 @@ parse_query(Sql) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
authorize(Client, PubSub, Topic,
|
authorize(Client, PubSub, Topic,
|
||||||
#{resource_id := ResourceID,
|
#{id := ResourceID,
|
||||||
sql := {SQL, Params}
|
sql := {SQL, Params}
|
||||||
}) ->
|
}) ->
|
||||||
case emqx_resource:query(ResourceID, {sql, SQL, replvar(Params, Client)}) of
|
case emqx_resource:query(ResourceID, {sql, SQL, replvar(Params, Client)}) of
|
||||||
|
|
|
@ -50,7 +50,7 @@ parse_query(Sql) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
authorize(Client, PubSub, Topic,
|
authorize(Client, PubSub, Topic,
|
||||||
#{resource_id := ResourceID,
|
#{id := ResourceID,
|
||||||
sql := {SQL, Params}
|
sql := {SQL, Params}
|
||||||
}) ->
|
}) ->
|
||||||
case emqx_resource:query(ResourceID, {sql, SQL, replvar(Params, Client)}) of
|
case emqx_resource:query(ResourceID, {sql, SQL, replvar(Params, Client)}) of
|
||||||
|
|
|
@ -34,7 +34,7 @@ description() ->
|
||||||
"AuthZ with redis".
|
"AuthZ with redis".
|
||||||
|
|
||||||
authorize(Client, PubSub, Topic,
|
authorize(Client, PubSub, Topic,
|
||||||
#{resource_id := ResourceID,
|
#{id := ResourceID,
|
||||||
cmd := CMD
|
cmd := CMD
|
||||||
}) ->
|
}) ->
|
||||||
NCMD = string:tokens(replvar(CMD, Client), " "),
|
NCMD = string:tokens(replvar(CMD, Client), " "),
|
||||||
|
|
|
@ -74,17 +74,19 @@ end_per_suite(_Config) ->
|
||||||
%% Testcases
|
%% Testcases
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
t_init_rule(_) ->
|
t_init_rule(_) ->
|
||||||
?assertEqual(#{permission => deny,
|
?assertMatch(#{permission := deny,
|
||||||
action => all,
|
action := all,
|
||||||
principal => all,
|
principal := all,
|
||||||
topics => [['#']]
|
topics := [['#']],
|
||||||
|
id := _ID
|
||||||
}, emqx_authz:init_rule(?RULE1)),
|
}, emqx_authz:init_rule(?RULE1)),
|
||||||
?assertEqual(#{permission => allow,
|
?assertMatch(#{permission := allow,
|
||||||
action => all,
|
action := all,
|
||||||
principal =>
|
principal :=
|
||||||
#{ipaddress => {{127,0,0,1},{127,0,0,1},32}},
|
#{ipaddress := {{127,0,0,1},{127,0,0,1},32}},
|
||||||
topics => [#{eq => ['#']},
|
topics := [#{eq := ['#']},
|
||||||
#{eq => ['+']}]
|
#{eq := ['+']}],
|
||||||
|
id := _ID
|
||||||
}, emqx_authz:init_rule(?RULE2)),
|
}, emqx_authz:init_rule(?RULE2)),
|
||||||
?assertMatch(
|
?assertMatch(
|
||||||
#{permission := allow,
|
#{permission := allow,
|
||||||
|
@ -94,7 +96,8 @@ t_init_rule(_) ->
|
||||||
#{clientid := {re_pattern, _, _, _, _}}
|
#{clientid := {re_pattern, _, _, _, _}}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
topics := [[<<"test">>]]
|
topics := [[<<"test">>]],
|
||||||
|
id := _ID
|
||||||
}, emqx_authz:init_rule(?RULE3)),
|
}, emqx_authz:init_rule(?RULE3)),
|
||||||
?assertMatch(
|
?assertMatch(
|
||||||
#{permission := deny,
|
#{permission := deny,
|
||||||
|
@ -106,7 +109,8 @@ t_init_rule(_) ->
|
||||||
},
|
},
|
||||||
topics := [#{pattern := [<<"%u">>]},
|
topics := [#{pattern := [<<"%u">>]},
|
||||||
#{pattern := [<<"%c">>]}
|
#{pattern := [<<"%c">>]}
|
||||||
]
|
],
|
||||||
|
id := _ID
|
||||||
}, emqx_authz:init_rule(?RULE4)),
|
}, emqx_authz:init_rule(?RULE4)),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue