NETRAVE/bin/NETRAVE
VetheonGames 54d348c99f Enhancements to Command Execution and Logging Mechanisms
This commit introduces several significant enhancements to the way commands are executed and logged in the application. The changes are primarily focused on improving the robustness, reliability, and transparency of the command execution process, as well as enhancing the quality and usefulness of the log output.

   1. Command Execution Enhancements: The use_sudo method has been refactored to handle commands that do not return any output. Previously, the method was designed to capture and return the output of the command being executed. However, some commands (such as modprobe) do not return any output, which caused issues with the previous implementation. The method now checks the exit status of the command to determine whether it was successful or not, and returns a success or failure message accordingly. This change improves the robustness of the command execution process and ensures that it can handle a wider range of commands.

   2. Error Handling Improvements: The use_sudo method now includes more comprehensive error handling. If a command fails to execute within a specified timeout period, an error message is logged and the method returns a failure message. Additionally, if a command fails to execute for any other reason, the method logs the error and returns a failure message with the command's exit status. These changes make it easier to identify and troubleshoot issues with command execution.

   3.  Logging Enhancements: The logging mechanism has been enhanced to provide more detailed and useful information. The use_sudo method now logs the command being executed and its outcome (success or failure). If a command fails, the method logs the command's exit status. These changes improve the transparency of the command execution process and make it easier to identify and troubleshoot issues.

   4. Code Refactoring: Several methods have been refactored for improved readability and maintainability. The use_sudo method has been refactored to reduce its complexity and improve its readability. The first_run_setup method has been refactored to ensure that the main interface name and the dummy interface name are properly passed to the setup_traffic_mirroring method.

   5. Bug Fixes: A bug in the create_dummy_interface method that caused it to return an array of Alert objects instead of the dummy interface name has been fixed. The method now correctly returns the dummy interface name.

These changes represent a significant improvement to the command execution and logging mechanisms in the application, and lay the groundwork for further enhancements in the future.
2023-07-07 11:25:08 -06:00

103 lines
3.5 KiB
Ruby

#!/usr/bin/env ruby
# frozen_string_literal: true
require 'dotenv'
require 'debug'
require_relative '../lib/utils/system_information_gather'
require_relative '../lib/utils/database_manager'
require_relative '../lib/utils/first_run_init'
require_relative '../lib/utils/utilities'
require_relative '../lib/utils/logg_man'
require_relative '../lib/utils/alert_queue_manager'
require_relative '../lib/utils/alert_manager'
include Utilities # rubocop:disable Style/MixinUsage
# binding.b(do: 'irb')
@loggman = LoggMan.new
begin
# Create .env file if it doesn't exist
File.open('.env', 'w') {} unless File.exist?('.env')
# Load environment variables from .env file
Dotenv.load
# Initialize AlertQueueManager, RingBuffer, and AlertManager
@alert_queue_manager = AlertQueueManager.new(@loggman)
# Initialize DatabaseManager
db_manager = DatabaseManager.new(@loggman, @alert_queue_manager)
# Get database details from environment variables
db_details = {
username: ENV['DB_USERNAME'],
password: ENV['DB_PASSWORD'],
key: ENV['DB_SECRET_KEY'],
database: ENV['DB_DATABASE']
}
# Decrypt password
dec_pass = decrypt_string_chacha20(db_details[:password], db_details[:key])
# If any of the necessary details are missing, run the first run setup
if db_details.values.any?(&:nil?)
@loggman.log_warn('Missing or incomplete configuration. Running first run setup.')
first_run_init = FirstRunInit.new(@loggman, @alert_queue_manager, db_manager)
first_run_init.run
# Reload environment variables after first run setup
Dotenv.load
db_details = {
username: ENV['DB_USERNAME'],
password: ENV['DB_PASSWORD'],
key: ENV['DB_SECRET_KEY'],
database: ENV['DB_DATABASE']
}
# Decrypt password again after potentially updating config
dec_pass = decrypt_string_chacha20(db_details[:password], db_details[:key])
end
# Test connection
unless db_manager.test_db_connection(db_details[:username], dec_pass, db_details[:database])
@loggman.log_warn('Failed to connect to the database with existing configuration. Please re-enter your details.')
first_run_init = FirstRunInit.new(@loggman, @alert_queue_manager, db_manager)
first_run_init.run
# Reload environment variables after potentially updating config
Dotenv.load
db_details = {
username: ENV['DB_USERNAME'],
password: ENV['DB_PASSWORD'],
key: ENV['DB_SECRET_KEY'],
database: ENV['DB_DATABASE']
}
# Decrypt password again after potentially updating config
dec_pass = decrypt_string_chacha20(db_details[:password], db_details[:key])
end
# Test connection again after potentially updating config
if db_manager.test_db_connection(db_details[:username], dec_pass, db_details[:database])
@loggman.log_info('Successfully connected to the database.')
else
@loggman.log_error('Failed to connect to the database. Please check your configuration.')
exit 1
end
@loggman.log_warn('Program successfully ran with no errors')
# TODO: Add the rest of application logic here
# End of the program
# wait for the alert_queue_manager to block before we exit.
@alert_queue_manager.shutdown
# Shush Rubocop. I know I shouldn't rescue an exception. I am just using it to log exceptions so the
# program doesn't crash silently
rescue Exception => e # rubocop:disable Lint/RescueException
# Log the exception
@loggman.log_error("An unhandled exception has occurred: #{e.class}: #{e.message}")
@loggman.log_error(e.backtrace.join("\n"))
# Re-raise the original exception
raise
end