fix(utils): handle improper lists as well in `redact/1`

This commit is contained in:
Andrew Mayorov 2024-02-22 13:35:11 +01:00
parent 7bb9d5d8f6
commit ae8f59979d
No known key found for this signature in database
GPG Key ID: 2837C62ACFBFED5D
1 changed files with 21 additions and 14 deletions

View File

@ -730,8 +730,8 @@ redact(Term, Checker) ->
is_sensitive_key(V) orelse Checker(V) is_sensitive_key(V) orelse Checker(V)
end). end).
do_redact(L, Checker) when is_list(L) -> do_redact([E | Rest], Checker) ->
lists:map(fun(E) -> do_redact(E, Checker) end, L); [do_redact(E, Checker) | do_redact(Rest, Checker)];
do_redact(M, Checker) when is_map(M) -> do_redact(M, Checker) when is_map(M) ->
maps:map( maps:map(
fun(K, V) -> fun(K, V) ->
@ -838,14 +838,8 @@ ipv6_probe_test() ->
end. end.
redact_test_() -> redact_test_() ->
Case = fun(Type, KeyT) -> Case = fun(TypeF, KeyIn) ->
Key = Key = TypeF(KeyIn),
case Type of
atom -> KeyT;
string -> erlang:atom_to_list(KeyT);
binary -> erlang:atom_to_binary(KeyT)
end,
?assert(is_sensitive_key(Key)), ?assert(is_sensitive_key(Key)),
%% direct %% direct
@ -866,10 +860,16 @@ redact_test_() ->
%% 3 level nested %% 3 level nested
?assertEqual([#{opts => [{Key, ?REDACT_VAL}]}], redact([#{opts => [{Key, foo}]}])), ?assertEqual([#{opts => [{Key, ?REDACT_VAL}]}], redact([#{opts => [{Key, foo}]}])),
?assertEqual([{opts, [{Key, ?REDACT_VAL}]}], redact([{opts, [{Key, foo}]}])), ?assertEqual([{opts, [{Key, ?REDACT_VAL}]}], redact([{opts, [{Key, foo}]}])),
?assertEqual([{opts, [#{Key => ?REDACT_VAL}]}], redact([{opts, [#{Key => foo}]}])) ?assertEqual([{opts, [#{Key => ?REDACT_VAL}]}], redact([{opts, [#{Key => foo}]}])),
end,
Types = [atom, string, binary], %% improper lists
?assertEqual([{opts, [{Key, ?REDACT_VAL} | oops]}], redact([{opts, [{Key, foo} | oops]}]))
end,
Types = [
{atom, fun identity/1},
{string, fun emqx_utils_conv:str/1},
{binary, fun emqx_utils_conv:bin/1}
],
Keys = [ Keys = [
authorization, authorization,
aws_secret_access_key, aws_secret_access_key,
@ -882,7 +882,11 @@ redact_test_() ->
token, token,
bind_password bind_password
], ],
[{case_name(Type, Key), fun() -> Case(Type, Key) end} || Key <- Keys, Type <- Types]. [
{case_name(Type, Key), fun() -> Case(TypeF, Key) end}
|| Key <- Keys,
{Type, TypeF} <- Types
].
redact2_test_() -> redact2_test_() ->
Case = fun(Key, Checker) -> Case = fun(Key, Checker) ->
@ -936,6 +940,9 @@ redact_is_authorization_test_() ->
case_name(Type, Key) -> case_name(Type, Key) ->
lists:concat([Type, "-", Key]). lists:concat([Type, "-", Key]).
identity(X) ->
X.
-endif. -endif.
pub_props_to_packet(Properties) -> pub_props_to_packet(Properties) ->