From 0ee755f4e9fb519ee2e67ba4624c88e91f0ba8dd Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Thu, 12 Dec 2024 20:27:33 +0100 Subject: [PATCH] Select messages to show (IPC and Auth). --- src/configuration.cr | 7 +++++++ src/server.cr | 4 ++++ src/service.cr | 39 +++++++++++++++++++++++++++++++++------ 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/configuration.cr b/src/configuration.cr index b5193d7..75c20f6 100644 --- a/src/configuration.cr +++ b/src/configuration.cr @@ -12,6 +12,13 @@ class Baguette::Configuration property mailer_exe : String = "/usr/local/bin/mailer" property read_only_profile_keys : Array(String) = Array(String).new + # Handle messages to display and to mask. + # Messages to mask, to focus on what is important. + property messages_to_mask : Array(AUTHMESSAGE) = [AUTHMESSAGE::KEEPALIVE] + + # Path to the log file. No log file = just print everything. + property log_file : String? = nil + property print_password_recovery_parameters : Bool = false end end diff --git a/src/server.cr b/src/server.cr index f9f217c..6bf90e9 100644 --- a/src/server.cr +++ b/src/server.cr @@ -82,6 +82,10 @@ begin exit 0 end + if path = configuration.log_file + Baguette::Log.log_path = path + end + AuthD::Service.new(configuration).run rescue e : OptionParser::Exception diff --git a/src/service.cr b/src/service.cr index 55c7abf..4f3bc70 100644 --- a/src/service.cr +++ b/src/service.cr @@ -5,6 +5,25 @@ extend AuthD require "./configuration" +class Array(T) + def contains?(value : T) + (self.select { |x| x == value }).size > 0 + end +end + +# WIP: select (dynamically) messages to mask + +module AuthD + enum MESSAGE + KEEPALIVE + LOGIN + # TODO + end +end + +alias IPCMESSAGE = Baguette::Configuration::IPC::MESSAGE +alias AUTHMESSAGE = AuthD::MESSAGE + # Provides a JWT-based authentication scheme for service-specific users. class AuthD::Service < IPC property configuration : Baguette::Configuration::Auth @@ -47,6 +66,14 @@ class AuthD::Service < IPC self.service_init @configuration.service_name end + def should_display?(value : AUTHMESSAGE) + (@configuration.messages_to_mask.select { |x| x == value }).size == 0 + end + + def should_display?(value : IPCMESSAGE) + @configuration.ipc_messages_to_show.contains? value + end + def obsolete_hash_password(password : String) : String digest = OpenSSL::Digest.new "sha256" digest << password @@ -147,7 +174,7 @@ class AuthD::Service < IPC if response.is_a? AuthD::Response::Error Baguette::Log.warning "fd #{ "%4d" % event.fd} (#{duration}) #{request_name} >> #{response_name} (#{response.reason})" else - if request_name != "KeepAlive" || @configuration.print_keepalive + if request_name != "KeepAlive" || should_display? AUTHMESSAGE::KEEPALIVE Baguette::Log.debug "fd #{ "%4d" % event.fd} (#{duration}) #{request_name} >> #{response_name}" end end @@ -174,10 +201,10 @@ class AuthD::Service < IPC self.loop do |event| case event.type when LibIPC::EventType::Timer - Baguette::Log.debug "Timer" if @configuration.print_ipc_timer + Baguette::Log.debug "Timer" if should_display? IPCMESSAGE::TIMER when LibIPC::EventType::MessageRx - Baguette::Log.debug "Received message from #{event.fd}" if @configuration.print_ipc_message_received + Baguette::Log.debug "Received message from #{event.fd}" if should_display? IPCMESSAGE::RX begin handle_request event rescue e @@ -186,12 +213,12 @@ class AuthD::Service < IPC end when LibIPC::EventType::MessageTx - Baguette::Log.debug "Message sent to #{event.fd}" if @configuration.print_ipc_message_sent + Baguette::Log.debug "Message sent to #{event.fd}" if should_display? IPCMESSAGE::TX when LibIPC::EventType::Connection - Baguette::Log.debug "Connection from #{event.fd}" if @configuration.print_ipc_connection + Baguette::Log.debug "Connection from #{event.fd}" if should_display? IPCMESSAGE::CONNECTION when LibIPC::EventType::Disconnection - Baguette::Log.debug "Disconnection from #{event.fd}" if @configuration.print_ipc_disconnection + Baguette::Log.debug "Disconnection from #{event.fd}" if should_display? IPCMESSAGE::DISCONNECTION @logged_users.delete event.fd else Baguette::Log.error "Not implemented behavior for event: #{event}"