6

I know Magento stores the database connection details within the local.xml file, however our firm is trying to avoid passwords and other sensitive data being stored within our git repo's for security purposes.

I know you can create Environment Variables easily via an .htaccess file, but I'm hoping to find a workable solution that will enable me to set this database information dynamically from a environment variable.

As the local.xml is an XML file and as this is a non dynamic/server-side filetype we cannot use it to read environment variables.

Would there be a way to somehow add in some hook/custom behaviour to Magento in which I could replace the local.xml with a PHP file that will allow me to pull in these environment variables?

So in a sense, the local.XML would become a local.PHP file with the ability to read my own custom environment variables such DB_HOST, DB_USERNAME, DB_PASSWORD rather than having them already set in the xml file as localhost, root, password123 etc.

Any ideas on how best to achieve this, or are there any existing Magento add-ons/extensions/mods that will allow me to do this?

Zabs
  • 13,852
  • 45
  • 173
  • 297

4 Answers4

5

I would suggest git ignore your local.xml and dynamically create it with your deploy script. your deploy script should have your sensitive data variables.

lucidlogic
  • 1,814
  • 13
  • 15
3

I found an alternative solution to the problem. I extended Mage_Core_Model_Config_Element and overrode the 'xmlentities' function to check if the configuration value it is returning starts with a dollar sign, and if so substitute it with the equivalent environment variable.

If it helps anyone else, here it is...

https://github.com/rossigee/magento-config-envvars

Ross
  • 31
  • 1
  • 2
1

Please try this solution:

copy app/code/core/Mage/Core/Model/App.php to app/code/local/Mage/Core/Model/App.php and replace the _initBaseConfig() method with the following one:

protected function _initBaseConfig()
{
    Varien_Profiler::start('mage::app::init::system_config');
    $this->_config->loadBase();

    /* Read DB connection config from environment variables */
    $connection = $this->_config->getNode('global/resources/default_setup/connection');
    $connection->setNode('host', $_ENV['DB_HOST']);
    $connection->setNode('username', $_ENV['DB_USERNAME']);
    $connection->setNode('password', $_ENV['DB_PASSWORD']);

    Varien_Profiler::stop('mage::app::init::system_config');
    return $this;
}

This must help.

* EDIT

protected function _initBaseConfig()
{
    Varien_Profiler::start('mage::app::init::system_config');
    $this->_config->loadBase();

    /* Read DB connection config from environment variables */
    $this->_config->getNode('global/resources/default_setup/connection')
        ->setNode('host', $_ENV['DB_HOST'])
        ->setNode('username', $_ENV['DB_USERNAME'])
        ->setNode('password', $_ENV['DB_PASSWORD']);

    Varien_Profiler::stop('mage::app::init::system_config');
    return $this;
}
Mageworx
  • 932
  • 4
  • 7
  • This 'kinda' works - I've used the getenv() rather than $_ENV - the htaccess variables are now being pssed into this _initBaseConfig() function. Do you now how I would make the $connection variable to default database connection. In this instance it would need to overwrite the existing database setup at the start with the Magento default setup. – Zabs Apr 22 '14 at 12:13
  • 1
    The $connection is just a temporary variable that is used for the convenience of changing Magento configuration. In other words, you don't need to use the $connection variable at all. See the example above (I have edited the answer) – Mageworx Apr 23 '14 at 13:48
1

Have you considered simply adding local.xml to .gitignore and creating/updating it as part of your deployment process? Note that local.xml typically stores more than just database credentials. For example, it might also store the configuration for the caching backend(s) as and session storage. These are usually also server specific, and will make things very messy if you try to avoid using local.xml.

Agop
  • 1,907
  • 17
  • 23