From f92cadf6340d8600162580eeb5256e3c717a229a Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Tue, 17 Dec 2024 00:51:49 +0100 Subject: [PATCH] Logs: clean user info, not just a file descriptor. --- src/service.cr | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/src/service.cr b/src/service.cr index 36f1e33..3a69a46 100644 --- a/src/service.cr +++ b/src/service.cr @@ -61,8 +61,11 @@ class DNSManager::Service < IPC self.service_init @configuration.service_name end - def get_logged_user(event : IPC::Event) - @logged_users[event.fd]? + def get_logged_user(fd : Int32) : AuthD::User::Public? + @logged_users[fd]? + end + def get_logged_user(event : IPC::Event) : AuthD::User::Public? + get_logged_user event.fd end def decode_token(token : String) @@ -92,6 +95,17 @@ class DNSManager::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 handle_request(event : IPC::Event) request_start = Time.utc @@ -105,38 +119,39 @@ class DNSManager::Service < IPC end reqname = request.class.name.sub /^DNSManager::Request::/, "" + connection_info_str = log_user_info event.fd response = begin request.handle self, event rescue e : AuthorizationException - Baguette::Log.error "(fd #{"%4d" % event.fd}) #{request} authorization error" + Baguette::Log.error "(#{connection_info_str}) #{request} authorization error" Response::Error.new "authorization error" rescue e : DomainNotFoundException - Baguette::Log.error "(fd #{"%4d" % event.fd}) #{request} domain not found" + Baguette::Log.error "(#{connection_info_str}) #{request} domain not found" Response::DomainNotFound.new rescue e : CannotCheckPermissionsException - Baguette::Log.error "(fd #{"%4d" % event.fd}) #{request} cannot check permissions of user '#{e.uid}' on resource '#{e.resource}'" + Baguette::Log.error "(#{connection_info_str}) #{request} cannot check permissions of user '#{e.uid}' on resource '#{e.resource}'" Response::InsufficientRights.new rescue e : UnknownUserException - Baguette::Log.error "(fd #{"%4d" % event.fd}) #{request} unknown user" + Baguette::Log.error "(#{connection_info_str}) #{request} unknown user" Response::UnknownUser.new rescue e : NoOwnershipException - Baguette::Log.error "(fd #{"%4d" % event.fd}) #{request} no ownership error" + Baguette::Log.error "(#{connection_info_str}) #{request} no ownership error" Response::NoOwnership.new rescue e : NotLoggedException - Baguette::Log.error "(fd #{"%4d" % event.fd}) #{request} user not logged" + Baguette::Log.error "(#{connection_info_str}) #{request} user not logged" Response::Error.new "user not logged" rescue e : RRNotFoundException - Baguette::Log.error "(fd #{"%4d" % event.fd}) #{request} RR not found" + Baguette::Log.error "(#{connection_info_str}) #{request} RR not found" Response::RRNotFound.new rescue e : TokenNotFoundException - Baguette::Log.error "(fd #{"%4d" % event.fd}) #{request} Token not found" + Baguette::Log.error "(#{connection_info_str}) #{request} Token not found" Response::Error.new "token not found" rescue e : RRReadOnlyException - Baguette::Log.error "(fd #{"%4d" % event.fd}) #{request} RR is read only" + Baguette::Log.error "(#{connection_info_str}) #{request} RR is read only" Response::RRReadOnly.new e.domain, e.rr rescue e # Generic case - Baguette::Log.error "(fd #{"%4d" % event.fd}) #{request} generic error #{e}" + Baguette::Log.error "(#{connection_info_str}) #{request} generic error #{e}" Response::Error.new "generic error" end @@ -149,10 +164,10 @@ class DNSManager::Service < IPC duration = Time.utc - request_start if response.is_a? DNSManager::Response::Error - Baguette::Log.warning "fd #{"%4d" % event.fd} (#{duration}) #{request} >> #{response}" + Baguette::Log.warning "(#{connection_info_str}) (#{duration}) #{request} >> #{response}" else if reqname != "KeepAlive" || should_display? DNSMESSAGE::KEEPALIVE - Baguette::Log.debug "fd #{"%4d" % event.fd} (#{duration}) #{request} >> #{response}" + Baguette::Log.debug "(#{connection_info_str}) (#{duration}) #{request} >> #{response}" end end end