diff --git a/apps/emqx_authz/etc/emqx_authz.conf b/apps/emqx_authz/etc/emqx_authz.conf index 9e65517ad..3469aad3a 100644 --- a/apps/emqx_authz/etc/emqx_authz.conf +++ b/apps/emqx_authz/etc/emqx_authz.conf @@ -57,7 +57,7 @@ authorization { # }, { type: built-in-database - } + }, { type: file # file is loaded into cache diff --git a/apps/emqx_authz/include/emqx_authz.hrl b/apps/emqx_authz/include/emqx_authz.hrl index bf8371add..d86f08888 100644 --- a/apps/emqx_authz/include/emqx_authz.hrl +++ b/apps/emqx_authz/include/emqx_authz.hrl @@ -55,6 +55,11 @@ -define(CMD_APPEND, append). -define(CMD_MOVE, move). +-define(CMD_MOVE_TOP, <<"top">>). +-define(CMD_MOVE_BOTTOM, <<"bottom">>). +-define(CMD_MOVE_BEFORE(Before), {<<"before">>, Before}). +-define(CMD_MOVE_AFTER(After), {<<"after">>, After}). + -define(METRICS(Type), tl(tuple_to_list(#Type{}))). -define(METRICS(Type, K), #Type{}#Type.K). diff --git a/apps/emqx_authz/src/emqx_authz.erl b/apps/emqx_authz/src/emqx_authz.erl index 7a417925e..22c48e718 100644 --- a/apps/emqx_authz/src/emqx_authz.erl +++ b/apps/emqx_authz/src/emqx_authz.erl @@ -64,9 +64,9 @@ move(Type, Cmd) -> move(Type, Cmd, #{}). move(Type, #{<<"before">> := Before}, Opts) -> - emqx:update_config(?CONF_KEY_PATH, {?CMD_MOVE, type(Type), #{<<"before">> => type(Before)}}, Opts); + emqx:update_config(?CONF_KEY_PATH, {?CMD_MOVE, type(Type), ?CMD_MOVE_BEFORE(type(Before))}, Opts); move(Type, #{<<"after">> := After}, Opts) -> - emqx:update_config(?CONF_KEY_PATH, {?CMD_MOVE, type(Type), #{<<"after">> => type(After)}}, Opts); + emqx:update_config(?CONF_KEY_PATH, {?CMD_MOVE, type(Type), ?CMD_MOVE_AFTER(type(After))}, Opts); move(Type, Position, Opts) -> emqx:update_config(?CONF_KEY_PATH, {?CMD_MOVE, type(Type), Position}, Opts). @@ -80,17 +80,17 @@ update({delete, Type}, Sources, Opts) -> update(Cmd, Sources, Opts) -> emqx:update_config(?CONF_KEY_PATH, {Cmd, Sources}, Opts). -do_update({?CMD_MOVE, Type, <<"top">>}, Conf) when is_list(Conf) -> +do_update({?CMD_MOVE, Type, ?CMD_MOVE_TOP}, Conf) when is_list(Conf) -> {Source, Front, Rear} = take(Type, Conf), [Source | Front] ++ Rear; -do_update({?CMD_MOVE, Type, <<"bottom">>}, Conf) when is_list(Conf) -> +do_update({?CMD_MOVE, Type, ?CMD_MOVE_BOTTOM}, Conf) when is_list(Conf) -> {Source, Front, Rear} = take(Type, Conf), Front ++ Rear ++ [Source]; -do_update({?CMD_MOVE, Type, #{<<"before">> := Before}}, Conf) when is_list(Conf) -> +do_update({?CMD_MOVE, Type, ?CMD_MOVE_BEFORE(Before)}, Conf) when is_list(Conf) -> {S1, Front1, Rear1} = take(Type, Conf), {S2, Front2, Rear2} = take(Before, Front1 ++ Rear1), Front2 ++ [S1, S2] ++ Rear2; -do_update({?CMD_MOVE, Type, #{<<"after">> := After}}, Conf) when is_list(Conf) -> +do_update({?CMD_MOVE, Type, ?CMD_MOVE_AFTER(After)}, Conf) when is_list(Conf) -> {S1, Front1, Rear1} = take(Type, Conf), {S2, Front2, Rear2} = take(After, Front1 ++ Rear1), Front2 ++ [S2, S1] ++ Rear2; diff --git a/apps/emqx_authz/src/emqx_authz_api_sources.erl b/apps/emqx_authz/src/emqx_authz_api_sources.erl index efe402fa7..d93c469f5 100644 --- a/apps/emqx_authz/src/emqx_authz_api_sources.erl +++ b/apps/emqx_authz/src/emqx_authz_api_sources.erl @@ -345,9 +345,9 @@ sources(get, _) -> {200, #{sources => Sources}}; sources(post, #{body := #{<<"type">> := <<"file">>, <<"rules">> := Rules}}) -> {ok, Filename} = write_file(filename:join([emqx:get_config([node, data_dir]), "acl.conf"]), Rules), - update_config(head, [#{<<"type">> => <<"file">>, <<"enable">> => true, <<"path">> => Filename}]); + update_config(?CMD_PREPEND, [#{<<"type">> => <<"file">>, <<"enable">> => true, <<"path">> => Filename}]); sources(post, #{body := Body}) when is_map(Body) -> - update_config(head, [write_cert(Body)]); + update_config(?CMD_PREPEND, [write_cert(Body)]); sources(put, #{body := Body}) when is_list(Body) -> NBody = [ begin case Source of