Merge pull request #11695 from HJianBo/audit-logs-from

This commit is contained in:
zhongwencool 2023-09-28 13:50:55 +08:00 committed by GitHub
commit 69207b7a63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 15 additions and 16 deletions

View File

@ -61,7 +61,7 @@
) )
end). end).
-define(AUDIT(_Level_, _Msg_, _Meta_), begin -define(AUDIT(_Level_, _From_, _Meta_), begin
case emqx_config:get([log, audit], #{enable => false}) of case emqx_config:get([log, audit], #{enable => false}) of
#{enable := false} -> #{enable := false} ->
ok; ok;
@ -71,8 +71,8 @@ end).
emqx_trace:log( emqx_trace:log(
_Level_, _Level_,
[{emqx_audit, fun(L, _) -> L end, undefined, undefined}], [{emqx_audit, fun(L, _) -> L end, undefined, undefined}],
{report, _Msg_}, _Msg = undefined,
_Meta_ _Meta_#{from => _From_}
); );
gt -> gt ->
ok ok

View File

@ -93,6 +93,8 @@ format(Msg, Meta, Config) ->
end, end,
emqx_utils_json:encode(json_obj_root(Data, Config)). emqx_utils_json:encode(json_obj_root(Data, Config)).
maybe_format_msg(undefined, _Meta, _Config) ->
#{};
maybe_format_msg({report, Report} = Msg, #{report_cb := Cb} = Meta, Config) -> maybe_format_msg({report, Report} = Msg, #{report_cb := Cb} = Meta, Config) ->
case is_map(Report) andalso Cb =:= ?DEFAULT_FORMATTER of case is_map(Report) andalso Cb =:= ?DEFAULT_FORMATTER of
true -> true ->

View File

@ -145,7 +145,7 @@ run_command(Cmd, Args) when is_atom(Cmd) ->
audit_log( audit_log(
audit_level(Result, Duration), audit_level(Result, Duration),
"from_cli", cli,
#{duration_ms => Duration, cmd => Cmd, args => Args, node => node()} #{duration_ms => Duration, cmd => Cmd, args => Args, node => node()}
), ),
Result. Result.

View File

@ -25,23 +25,19 @@ log(Meta0) ->
Duration = erlang:convert_time_unit(ReqEnd - ReqStart, native, millisecond), Duration = erlang:convert_time_unit(ReqEnd - ReqStart, native, millisecond),
Level = level(Method, Code, Duration), Level = level(Method, Code, Duration),
Username = maps:get(username, Meta0, <<"">>), Username = maps:get(username, Meta0, <<"">>),
From = from(maps:get(auth_type, Meta0, "")),
Meta1 = maps:without([req_start, req_end], Meta0), Meta1 = maps:without([req_start, req_end], Meta0),
Meta2 = Meta1#{time => logger:timestamp(), duration_ms => Duration}, Meta2 = Meta1#{time => logger:timestamp(), duration_ms => Duration},
Meta = emqx_utils:redact(Meta2), Meta = emqx_utils:redact(Meta2),
?AUDIT( ?AUDIT(
Level, Level,
"from_api", From,
Meta#{ Meta#{username => binary_to_list(Username), node => node()}
from => from(maps:get(auth_type, Meta0, "")),
username => binary_to_list(Username),
node => node()
}
), ),
ok. ok.
from(jwt_token) -> "dashboard"; from(jwt_token) -> "dashboard";
from(api_key) -> "aip_key"; from(_) -> "rest_api".
from(_) -> "unauthorized".
level(get, _Code, _) -> debug; level(get, _Code, _) -> debug;
level(_, Code, _) when Code >= 200 andalso Code < 300 -> info; level(_, Code, _) when Code >= 200 andalso Code < 300 -> info;

View File

@ -47,7 +47,7 @@ post_boot() ->
ok = ensure_apps_started(), ok = ensure_apps_started(),
ok = print_vsn(), ok = print_vsn(),
ok = start_autocluster(), ok = start_autocluster(),
?AUDIT(alert, "from_cli", #{time => logger:timestamp(), event => "emqx_start"}), ?AUDIT(alert, cli, #{time => logger:timestamp(), event => "emqx_start"}),
ignore. ignore.
-ifdef(TEST). -ifdef(TEST).

View File

@ -67,8 +67,9 @@ graceful() ->
%% @doc Shutdown the Erlang VM and wait indefinitely. %% @doc Shutdown the Erlang VM and wait indefinitely.
graceful_wait() -> graceful_wait() ->
?AUDIT(alert, "from_cli", #{ ?AUDIT(alert, cli, #{
time => logger:timestamp(), msg => "run_emqx_stop_to_grace_shutdown" time => logger:timestamp(),
event => emqx_gracefully_stop
}), }),
ok = graceful(), ok = graceful(),
exit_loop(). exit_loop().

View File

@ -112,7 +112,7 @@ max_heap_size_warning(MF, Args) ->
log(_, {?MODULE, prompt_func}, [[{history, _}]]) -> log(_, {?MODULE, prompt_func}, [[{history, _}]]) ->
ok; ok;
log(IsAllow, MF, Args) -> log(IsAllow, MF, Args) ->
?AUDIT(warning, "from_remote_console", #{ ?AUDIT(warning, erlang_console, #{
time => logger:timestamp(), time => logger:timestamp(),
function => MF, function => MF,
args => pp_args(Args), args => pp_args(Args),