Logs: clean user info, not just a file descriptor.

This commit is contained in:
Philippe Pittoli 2024-12-17 00:51:13 +01:00
parent 43673222df
commit ecdc3bdc68

View File

@ -111,7 +111,7 @@ class AuthD::Service < IPC
File.write @last_uid_file, uid.to_s
end
def get_logged_user?(fd : Int32)
def get_logged_user?(fd : Int32) : AuthD::User::Public?
@logged_users[fd]?
end
@ -123,6 +123,17 @@ class AuthD::Service < IPC
end
end
# `log_user_info` provides a string composed from either the user
# id in case the user was authenticated or the file descriptor of
# the connection.
def log_user_info(fd : Int32) : String
if user = get_logged_user? fd
"userid #{user.uid}"
else
"fd #{"%4d" % fd}"
end
end
def user?(uid_or_login : UserID)
if uid_or_login.is_a? UInt32
@users_per_uid.get? uid_or_login.to_s
@ -144,20 +155,21 @@ class AuthD::Service < IPC
end
request_name = request.class.name.sub /^AuthD::Request::/, ""
connection_info_str = log_user_info event.fd
response = begin
request.handle self, event.fd
rescue e : UserNotFound
Baguette::Log.error "(fd #{ "%4d" % event.fd}) #{request} user not found"
Baguette::Log.error "(#{connection_info_str}) #{request} user not found"
AuthD::Response::Error.new "authorization error"
rescue e : AuthenticationInfoLacking
Baguette::Log.error "(fd #{ "%4d" % event.fd}) #{request} lacking authentication info"
Baguette::Log.error "(#{connection_info_str}) #{request} lacking authentication info"
AuthD::Response::Error.new "authorization error"
rescue e : AdminAuthorizationException
Baguette::Log.error "(fd #{ "%4d" % event.fd}) #{request} admin authentication failed"
Baguette::Log.error "(#{connection_info_str}) #{request} admin authentication failed"
AuthD::Response::Error.new "authorization error"
rescue e
Baguette::Log.error "(fd #{ "%4d" % event.fd}) #{request} generic error #{e}"
Baguette::Log.error "(#{connection_info_str}) #{request} generic error #{e}"
AuthD::Response::Error.new "unknown error"
end
@ -170,10 +182,10 @@ class AuthD::Service < IPC
duration = Time.utc - request_start
if response.is_a? AuthD::Response::Error
Baguette::Log.warning "fd #{ "%4d" % event.fd} (#{duration}) #{request} >> #{response}"
Baguette::Log.warning "(#{connection_info_str}) (#{duration}) #{request} >> #{response}"
else
if request_name != "KeepAlive" || should_display? AUTHMESSAGE::KEEPALIVE
Baguette::Log.debug "fd #{ "%4d" % event.fd} (#{duration}) #{request} >> #{response}"
Baguette::Log.debug "(#{connection_info_str}) (#{duration}) #{request} >> #{response}"
end
end
end