chore(authz): api returns the original content of the file

Signed-off-by: zhanghongtong <rory-z@outlook.com>
This commit is contained in:
zhanghongtong 2021-09-14 18:09:47 +08:00 committed by Rory Z
parent ca4b1ca3b5
commit 129a171de2
2 changed files with 16 additions and 5 deletions

View File

@ -6,6 +6,7 @@
{applications, {applications,
[kernel, [kernel,
stdlib, stdlib,
crypto,
emqx_connector emqx_connector
]}, ]},
{env,[]}, {env,[]},

View File

@ -298,11 +298,11 @@ move_source_api() ->
sources(get, _) -> sources(get, _) ->
Sources = lists:foldl(fun (#{type := file, enable := Enable, path := Path}, AccIn) -> Sources = lists:foldl(fun (#{type := file, enable := Enable, path := Path}, AccIn) ->
case file:consult(Path) of case file:read_file(Path) of
{ok, Rules} -> {ok, Rules} ->
lists:append(AccIn, [#{type => file, lists:append(AccIn, [#{type => file,
enable => Enable, enable => Enable,
rules => iolist_to_binary([io_lib:format("~p.\n", [R]) || R <- Rules]), rules => Rules,
annotations => #{status => healthy} annotations => #{status => healthy}
}]); }]);
{error, _} -> {error, _} ->
@ -356,11 +356,11 @@ source(get, #{bindings := #{type := Type}}) ->
case emqx_authz:lookup(Type) of case emqx_authz:lookup(Type) of
{error, Reason} -> {404, #{message => atom_to_binary(Reason)}}; {error, Reason} -> {404, #{message => atom_to_binary(Reason)}};
#{type := file, enable := Enable, path := Path}-> #{type := file, enable := Enable, path := Path}->
case file:consult(Path) of case file:read_file(Path) of
{ok, Rules} -> {ok, Rules} ->
{200, #{type => file, {200, #{type => file,
enable => Enable, enable => Enable,
rules => iolist_to_binary([io_lib:format("~p.\n", [R]) || R <- Rules]), rules => Rules,
annotations => #{status => healthy} annotations => #{status => healthy}
} }
}; };
@ -476,8 +476,18 @@ write_cert(#{<<"ssl">> := #{<<"enable">> := true} = SSL} = Source) ->
}; };
write_cert(Source) -> Source. write_cert(Source) -> Source.
write_file(Filename, Bytes) -> write_file(Filename, Bytes0) ->
ok = filelib:ensure_dir(Filename), ok = filelib:ensure_dir(Filename),
case file:read_file(Filename) of
{ok, Bytes1} ->
case crypto:hash(md5, Bytes1) =:= crypto:hash(md5, Bytes0) of
true -> {ok,iolist_to_binary(Filename)};
false -> do_write_file(Filename, Bytes0)
end;
_ -> do_write_file(Filename, Bytes0)
end.
do_write_file(Filename, Bytes) ->
case file:write_file(Filename, Bytes) of case file:write_file(Filename, Bytes) of
ok -> {ok, iolist_to_binary(Filename)}; ok -> {ok, iolist_to_binary(Filename)};
{error, Reason} -> {error, Reason} ->