refactor(configs): make acl related configs only in zone
This commit is contained in:
parent
e0e9b7ec5b
commit
af5470cb30
|
@ -34,17 +34,17 @@ authenticate(Credential) ->
|
||||||
%% @doc Check ACL
|
%% @doc Check ACL
|
||||||
-spec authorize(emqx_types:clientinfo(), emqx_types:pubsub(), emqx_types:topic())
|
-spec authorize(emqx_types:clientinfo(), emqx_types:pubsub(), emqx_types:topic())
|
||||||
-> allow | deny.
|
-> allow | deny.
|
||||||
authorize(ClientInfo = #{zone := Zone, listener := Listener}, PubSub, Topic) ->
|
authorize(ClientInfo = #{zone := Zone}, PubSub, Topic) ->
|
||||||
case emqx_acl_cache:is_enabled(Zone, Listener) of
|
case emqx_acl_cache:is_enabled(Zone) of
|
||||||
true -> check_authorization_cache(ClientInfo, PubSub, Topic);
|
true -> check_authorization_cache(ClientInfo, PubSub, Topic);
|
||||||
false -> do_authorize(ClientInfo, PubSub, Topic)
|
false -> do_authorize(ClientInfo, PubSub, Topic)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
check_authorization_cache(ClientInfo = #{zone := Zone, listener := Listener}, PubSub, Topic) ->
|
check_authorization_cache(ClientInfo = #{zone := Zone}, PubSub, Topic) ->
|
||||||
case emqx_acl_cache:get_acl_cache(Zone, Listener, PubSub, Topic) of
|
case emqx_acl_cache:get_acl_cache(Zone, PubSub, Topic) of
|
||||||
not_found ->
|
not_found ->
|
||||||
AclResult = do_authorize(ClientInfo, PubSub, Topic),
|
AclResult = do_authorize(ClientInfo, PubSub, Topic),
|
||||||
emqx_acl_cache:put_acl_cache(Zone, Listener, PubSub, Topic, AclResult),
|
emqx_acl_cache:put_acl_cache(Zone, PubSub, Topic, AclResult),
|
||||||
AclResult;
|
AclResult;
|
||||||
AclResult -> AclResult
|
AclResult -> AclResult
|
||||||
end.
|
end.
|
||||||
|
|
|
@ -18,15 +18,15 @@
|
||||||
|
|
||||||
-include("emqx.hrl").
|
-include("emqx.hrl").
|
||||||
|
|
||||||
-export([ list_acl_cache/2
|
-export([ list_acl_cache/1
|
||||||
, get_acl_cache/4
|
, get_acl_cache/3
|
||||||
, put_acl_cache/5
|
, put_acl_cache/4
|
||||||
, cleanup_acl_cache/2
|
, cleanup_acl_cache/1
|
||||||
, empty_acl_cache/0
|
, empty_acl_cache/0
|
||||||
, dump_acl_cache/0
|
, dump_acl_cache/0
|
||||||
, get_cache_max_size/2
|
, get_cache_max_size/1
|
||||||
, get_cache_ttl/2
|
, get_cache_ttl/1
|
||||||
, is_enabled/2
|
, is_enabled/1
|
||||||
, drain_cache/0
|
, drain_cache/0
|
||||||
]).
|
]).
|
||||||
|
|
||||||
|
@ -50,45 +50,45 @@ cache_k(PubSub, Topic)-> {PubSub, Topic}.
|
||||||
cache_v(AclResult)-> {AclResult, time_now()}.
|
cache_v(AclResult)-> {AclResult, time_now()}.
|
||||||
drain_k() -> {?MODULE, drain_timestamp}.
|
drain_k() -> {?MODULE, drain_timestamp}.
|
||||||
|
|
||||||
-spec(is_enabled(atom(), atom()) -> boolean()).
|
-spec(is_enabled(atom()) -> boolean()).
|
||||||
is_enabled(Zone, Listener) ->
|
is_enabled(Zone) ->
|
||||||
emqx_config:get_zone_conf(Zone, [acl, cache, enable]).
|
emqx_config:get_zone_conf(Zone, [acl, cache, enable]).
|
||||||
|
|
||||||
-spec(get_cache_max_size(atom(), atom()) -> integer()).
|
-spec(get_cache_max_size(atom()) -> integer()).
|
||||||
get_cache_max_size(Zone, Listener) ->
|
get_cache_max_size(Zone) ->
|
||||||
emqx_config:get_zone_conf(Zone, [acl, cache, max_size]).
|
emqx_config:get_zone_conf(Zone, [acl, cache, max_size]).
|
||||||
|
|
||||||
-spec(get_cache_ttl(atom(), atom()) -> integer()).
|
-spec(get_cache_ttl(atom()) -> integer()).
|
||||||
get_cache_ttl(Zone, Listener) ->
|
get_cache_ttl(Zone) ->
|
||||||
emqx_config:get_zone_conf(Zone, [acl, cache, ttl]).
|
emqx_config:get_zone_conf(Zone, [acl, cache, ttl]).
|
||||||
|
|
||||||
-spec(list_acl_cache(atom(), atom()) -> [acl_cache_entry()]).
|
-spec(list_acl_cache(atom()) -> [acl_cache_entry()]).
|
||||||
list_acl_cache(Zone, Listener) ->
|
list_acl_cache(Zone) ->
|
||||||
cleanup_acl_cache(Zone, Listener),
|
cleanup_acl_cache(Zone),
|
||||||
map_acl_cache(fun(Cache) -> Cache end).
|
map_acl_cache(fun(Cache) -> Cache end).
|
||||||
|
|
||||||
%% We'll cleanup the cache before replacing an expired acl.
|
%% We'll cleanup the cache before replacing an expired acl.
|
||||||
-spec get_acl_cache(atom(), atom(), emqx_types:pubsub(), emqx_topic:topic()) ->
|
-spec get_acl_cache(atom(), emqx_types:pubsub(), emqx_topic:topic()) ->
|
||||||
acl_result() | not_found.
|
acl_result() | not_found.
|
||||||
get_acl_cache(Zone, Listener, PubSub, Topic) ->
|
get_acl_cache(Zone, PubSub, Topic) ->
|
||||||
case erlang:get(cache_k(PubSub, Topic)) of
|
case erlang:get(cache_k(PubSub, Topic)) of
|
||||||
undefined -> not_found;
|
undefined -> not_found;
|
||||||
{AclResult, CachedAt} ->
|
{AclResult, CachedAt} ->
|
||||||
if_expired(get_cache_ttl(Zone, Listener), CachedAt,
|
if_expired(get_cache_ttl(Zone), CachedAt,
|
||||||
fun(false) ->
|
fun(false) ->
|
||||||
AclResult;
|
AclResult;
|
||||||
(true) ->
|
(true) ->
|
||||||
cleanup_acl_cache(Zone, Listener),
|
cleanup_acl_cache(Zone),
|
||||||
not_found
|
not_found
|
||||||
end)
|
end)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%% If the cache get full, and also the latest one
|
%% If the cache get full, and also the latest one
|
||||||
%% is expired, then delete all the cache entries
|
%% is expired, then delete all the cache entries
|
||||||
-spec put_acl_cache(atom(), atom(), emqx_types:pubsub(), emqx_topic:topic(), acl_result())
|
-spec put_acl_cache(atom(), emqx_types:pubsub(), emqx_topic:topic(), acl_result())
|
||||||
-> ok.
|
-> ok.
|
||||||
put_acl_cache(Zone, Listener, PubSub, Topic, AclResult) ->
|
put_acl_cache(Zone, PubSub, Topic, AclResult) ->
|
||||||
MaxSize = get_cache_max_size(Zone, Listener), true = (MaxSize =/= 0),
|
MaxSize = get_cache_max_size(Zone), true = (MaxSize =/= 0),
|
||||||
Size = get_cache_size(),
|
Size = get_cache_size(),
|
||||||
case Size < MaxSize of
|
case Size < MaxSize of
|
||||||
true ->
|
true ->
|
||||||
|
@ -96,7 +96,7 @@ put_acl_cache(Zone, Listener, PubSub, Topic, AclResult) ->
|
||||||
false ->
|
false ->
|
||||||
NewestK = get_newest_key(),
|
NewestK = get_newest_key(),
|
||||||
{_AclResult, CachedAt} = erlang:get(NewestK),
|
{_AclResult, CachedAt} = erlang:get(NewestK),
|
||||||
if_expired(get_cache_ttl(Zone, Listener), CachedAt,
|
if_expired(get_cache_ttl(Zone), CachedAt,
|
||||||
fun(true) ->
|
fun(true) ->
|
||||||
% all cache expired, cleanup first
|
% all cache expired, cleanup first
|
||||||
empty_acl_cache(),
|
empty_acl_cache(),
|
||||||
|
@ -123,10 +123,10 @@ evict_acl_cache() ->
|
||||||
decr_cache_size().
|
decr_cache_size().
|
||||||
|
|
||||||
%% cleanup all the expired cache entries
|
%% cleanup all the expired cache entries
|
||||||
-spec(cleanup_acl_cache(atom(), atom()) -> ok).
|
-spec(cleanup_acl_cache(atom()) -> ok).
|
||||||
cleanup_acl_cache(Zone, Listener) ->
|
cleanup_acl_cache(Zone) ->
|
||||||
keys_queue_set(
|
keys_queue_set(
|
||||||
cleanup_acl(get_cache_ttl(Zone, Listener), keys_queue_get())).
|
cleanup_acl(get_cache_ttl(Zone), keys_queue_get())).
|
||||||
|
|
||||||
get_oldest_key() ->
|
get_oldest_key() ->
|
||||||
keys_queue_pick(queue_front()).
|
keys_queue_pick(queue_front()).
|
||||||
|
|
|
@ -956,9 +956,9 @@ handle_call({takeover, 'end'}, Channel = #channel{session = Session,
|
||||||
AllPendings = lists:append(Delivers, Pendings),
|
AllPendings = lists:append(Delivers, Pendings),
|
||||||
disconnect_and_shutdown(takeovered, AllPendings, Channel);
|
disconnect_and_shutdown(takeovered, AllPendings, Channel);
|
||||||
|
|
||||||
handle_call(list_acl_cache, #channel{clientinfo = #{zone := Zone, listener := Listener}}
|
handle_call(list_acl_cache, #channel{clientinfo = #{zone := Zone}}
|
||||||
= Channel) ->
|
= Channel) ->
|
||||||
{reply, emqx_acl_cache:list_acl_cache(Zone, Listener), Channel};
|
{reply, emqx_acl_cache:list_acl_cache(Zone), Channel};
|
||||||
|
|
||||||
handle_call({quota, Policy}, Channel) ->
|
handle_call({quota, Policy}, Channel) ->
|
||||||
Zone = info(zone, Channel),
|
Zone = info(zone, Channel),
|
||||||
|
|
Loading…
Reference in New Issue