From 13da0aef4ce9fabfc2d1426c380a7381a57c3b3a Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Fri, 13 Sep 2024 09:25:23 -0500 Subject: [PATCH] Remove legacy db config (#16385) * Remove legacy db config Should be configured via the environment or .env. * Lint fix * Remove call to removed method --- LibreNMS/Config.php | 15 --------------- LibreNMS/config.py | 24 +++++++++++++++++------- config_to_json.php | 3 --- 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/LibreNMS/Config.php b/LibreNMS/Config.php index 2a631fd306..190f771c3f 100644 --- a/LibreNMS/Config.php +++ b/LibreNMS/Config.php @@ -478,9 +478,6 @@ class Config } self::populateTime(); - - // populate legacy DB credentials, just in case something external uses them. Maybe remove this later - self::populateLegacyDbCredentials(); } /** @@ -561,18 +558,6 @@ class Config self::set('time.twoyear', $now - 63072000); // time() - (2 * 365 * 24 * 60 * 60); } - public static function populateLegacyDbCredentials() - { - $db = config('database.default'); - - self::set('db_host', config("database.connections.$db.host", 'localhost')); - self::set('db_name', config("database.connections.$db.database", 'librenms')); - self::set('db_user', config("database.connections.$db.username", 'librenms')); - self::set('db_pass', config("database.connections.$db.password")); - self::set('db_port', config("database.connections.$db.port", 3306)); - self::set('db_socket', config("database.connections.$db.unix_socket")); - } - /** * Check if the config has been loaded yet * diff --git a/LibreNMS/config.py b/LibreNMS/config.py index ed884cf6c6..50a85b4776 100644 --- a/LibreNMS/config.py +++ b/LibreNMS/config.py @@ -1,3 +1,6 @@ +import os + + class DBConfig: """ Bare minimal config class for LibreNMS.DB class usage @@ -14,10 +17,17 @@ class DBConfig: db_ssl_ca = "/etc/ssl/certs/ca-certificates.crt" def populate(self, _config): - for key, val in _config.items(): - if key == "db_port": - # Special case: port number - self.db_port = int(val) - elif key.startswith("db_"): - # Prevent prototype pollution by enforcing prefix - setattr(DBConfig, key, val) + self.db_host = os.getenv("DB_HOST", _config.get("db_host", self.db_host)) + self.db_name = os.getenv("DB_DATABASE", _config.get("db_name", self.db_name)) + self.db_pass = os.getenv("DB_PASSWORD", _config.get("db_pass", self.db_pass)) + self.db_port = int(os.getenv("DB_PORT", _config.get("db_port", self.db_port))) + self.db_socket = os.getenv( + "DB_SOCKET", _config.get("db_socket", self.db_socket) + ) + self.db_user = os.getenv("DB_USERNAME", _config.get("db_user", self.db_user)) + self.db_sslmode = os.getenv( + "DB_SSLMODE", _config.get("db_sslmode", self.db_sslmode) + ) + self.db_ssl_ca = os.getenv( + "MYSQL_ATTR_SSL_CA", _config.get("db_ssl_ca", self.db_ssl_ca) + ) diff --git a/config_to_json.php b/config_to_json.php index 21bebf385f..4e04dd6091 100755 --- a/config_to_json.php +++ b/config_to_json.php @@ -13,8 +13,5 @@ $init_modules = ['nodb']; require __DIR__ . '/includes/init.php'; if (App::runningInConsole()) { - // fill in db variables for legacy external scripts - Config::populateLegacyDbCredentials(); - echo Config::toJson(); }