chore(authz api): fix dialyzer error

This commit is contained in:
Rory Z 2021-09-02 11:13:41 +08:00 committed by Rory Z
parent b8ee977d9d
commit 5669ea4034
1 changed files with 20 additions and 14 deletions

View File

@ -331,8 +331,9 @@ sources(get, _) ->
end, [], emqx_authz:lookup()), end, [], emqx_authz:lookup()),
{200, #{sources => Sources}}; {200, #{sources => Sources}};
sources(post, #{body := #{<<"type">> := <<"file">>, <<"rules">> := Rules, <<"enable">> := Enable}}) when is_list(Rules) -> sources(post, #{body := #{<<"type">> := <<"file">>, <<"rules">> := Rules, <<"enable">> := Enable}}) when is_list(Rules) ->
Filename = filename:join([emqx:get_config([node, data_dir]), "authorization_rules.conf"]), {ok, Filename} = write_file(filename:join([emqx:get_config([node, data_dir]), "authorization_rules.conf"]),
write_file(Filename, erlang:list_to_bitstring([<<Rule/binary, "\n">> || Rule <- Rules])), erlang:list_to_bitstring([<<Rule/binary, "\n">> || Rule <- Rules])
),
case emqx_authz:update(head, [#{type => file, enable => Enable, path => Filename}]) of case emqx_authz:update(head, [#{type => file, enable => Enable, path => Filename}]) of
{ok, _} -> {204}; {ok, _} -> {204};
{error, Reason} -> {error, Reason} ->
@ -350,8 +351,9 @@ sources(put, #{body := Body}) when is_list(Body) ->
NBody = [ begin NBody = [ begin
case Source of case Source of
#{<<"type">> := <<"file">>, <<"rules">> := Rules, <<"enable">> := Enable} -> #{<<"type">> := <<"file">>, <<"rules">> := Rules, <<"enable">> := Enable} ->
Filename = filename:join([emqx:get_config([node, data_dir]), "authorization_rules.conf"]), {ok, Filename} = write_file(filename:join([emqx:get_config([node, data_dir]), "authorization_rules.conf"]),
write_file(Filename, erlang:list_to_bitstring([<<Rule/binary, "\n">> || Rule <- Rules])), erlang:list_to_bitstring([<<Rule/binary, "\n">> || Rule <- Rules])
),
#{type => file, enable => Enable, path => Filename}; #{type => file, enable => Enable, path => Filename};
_ -> write_cert(Source) _ -> write_cert(Source)
end end
@ -395,9 +397,10 @@ source(get, #{bindings := #{type := Type}}) ->
{200, read_cert(NSource2)} {200, read_cert(NSource2)}
end; end;
source(put, #{bindings := #{type := file}, body := #{<<"type">> := <<"file">>, <<"rules">> := Rules, <<"enable">> := Enable}}) -> source(put, #{bindings := #{type := file}, body := #{<<"type">> := <<"file">>, <<"rules">> := Rules, <<"enable">> := Enable}}) ->
#{path := Path} = emqx_authz:lookup(file), {ok, Filename} = write_file(maps:get(path, emqx_authz:lookup(file), ""),
write_file(Path, erlang:list_to_bitstring([<<Rule/binary, "\n">> || Rule <- Rules])), erlang:list_to_bitstring([<<Rule/binary, "\n">> || Rule <- Rules])
case emqx_authz:update({replace_once, file}, #{type => file, enable => Enable, path => Path}) of ),
case emqx_authz:update({replace_once, file}, #{type => file, enable => Enable, path => Filename}) of
{ok, _} -> {204}; {ok, _} -> {204};
{error, Reason} -> {error, Reason} ->
{400, #{code => <<"BAD_REQUEST">>, {400, #{code => <<"BAD_REQUEST">>,
@ -455,20 +458,23 @@ write_cert(#{<<"config">> := #{<<"ssl">> := #{<<"enable">> := true} = SSL} = Con
CertPath = filename:join([emqx:get_config([node, data_dir]), "certs"]), CertPath = filename:join([emqx:get_config([node, data_dir]), "certs"]),
CaCert = case maps:is_key(<<"cacertfile">>, SSL) of CaCert = case maps:is_key(<<"cacertfile">>, SSL) of
true -> true ->
write_file(filename:join([CertPath, "cacert-" ++ emqx_rule_id:gen() ++".pem"]), {ok, CaCertFile} = write_file(filename:join([CertPath, "cacert-" ++ emqx_rule_id:gen() ++".pem"]),
maps:get(<<"cacertfile">>, SSL)); maps:get(<<"cacertfile">>, SSL)),
CaCertFile;
false -> "" false -> ""
end, end,
Cert = case maps:is_key(<<"certfile">>, SSL) of Cert = case maps:is_key(<<"certfile">>, SSL) of
true -> true ->
write_file(filename:join([CertPath, "cert-" ++ emqx_rule_id:gen() ++".pem"]), {ok, CertFile} = write_file(filename:join([CertPath, "cert-" ++ emqx_rule_id:gen() ++".pem"]),
maps:get(<<"certfile">>, SSL)); maps:get(<<"certfile">>, SSL)),
CertFile;
false -> "" false -> ""
end, end,
Key = case maps:is_key(<<"keyfile">>, SSL) of Key = case maps:is_key(<<"keyfile">>, SSL) of
true -> true ->
write_file(filename:join([CertPath, "key-" ++ emqx_rule_id:gen() ++".pem"]), {ok, KeyFile} = write_file(filename:join([CertPath, "key-" ++ emqx_rule_id:gen() ++".pem"]),
maps:get(<<"keyfile">>, SSL)); maps:get(<<"keyfile">>, SSL)),
KeyFile;
false -> "" false -> ""
end, end,
Source#{<<"config">> := Config#{<<"ssl">> => SSL#{<<"cacertfile">> => CaCert, Source#{<<"config">> := Config#{<<"ssl">> => SSL#{<<"cacertfile">> => CaCert,
@ -481,7 +487,7 @@ write_cert(Source) -> Source.
write_file(Filename, Bytes) -> write_file(Filename, Bytes) ->
ok = filelib:ensure_dir(Filename), ok = filelib:ensure_dir(Filename),
case file:write_file(Filename, Bytes) of case file:write_file(Filename, Bytes) of
ok -> Filename; ok -> {ok, Filename};
{error, Reason} -> {error, Reason} ->
?LOG(error, "Write File ~p Error: ~p", [Filename, Reason]), ?LOG(error, "Write File ~p Error: ~p", [Filename, Reason]),
error(Reason) error(Reason)