Skip to content

Commit

Permalink
Remove legacy db config (#16385)
Browse files Browse the repository at this point in the history
* Remove legacy db config
Should be configured via the environment or .env.

* Lint fix

* Remove call to removed method
  • Loading branch information
murrant authored Sep 13, 2024
1 parent 2501b7a commit 13da0ae
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 25 deletions.
15 changes: 0 additions & 15 deletions LibreNMS/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,9 +478,6 @@ private static function processConfig()
}

self::populateTime();

// populate legacy DB credentials, just in case something external uses them. Maybe remove this later
self::populateLegacyDbCredentials();
}

/**
Expand Down Expand Up @@ -561,18 +558,6 @@ private static function populateTime()
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
*
Expand Down
24 changes: 17 additions & 7 deletions LibreNMS/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import os


class DBConfig:
"""
Bare minimal config class for LibreNMS.DB class usage
Expand All @@ -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)
)
3 changes: 0 additions & 3 deletions config_to_json.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,5 @@
require __DIR__ . '/includes/init.php';

if (App::runningInConsole()) {
// fill in db variables for legacy external scripts
Config::populateLegacyDbCredentials();

echo Config::toJson();
}

0 comments on commit 13da0ae

Please sign in to comment.