Search code examples
cakephpconfigurationpassword-encryption

CakePHP: Encrypt password in config file


I don't want to store my password for my database configuration in plain text in the database.php for revision control reasons.

I want something like this:

public $default = array(
    'datasource' => 'Database/Mysql',
    'persistent' => false,
    'host' => 'localhost',
    'login' => 'root',
    'password' => simpleEncryptFunction('v3RyH4rD3NcRyPtEdPaS$wOrD'),
    'database' => 'sample',
    'prefix' => '',
    //'encoding' => 'utf8',
);

private function simpleEncryptFunction($hardPassword == null) {
   // Some pretty easy decrypt code. Not safe at all but I don't care.
   // It just shouldn't be that easy for people who don't understand code. 
}

Solution

  • ndm's comment is right, your approach is flawed. Instead do this:

    • Create a database.default.php and put the default values in it without passwords and commit it.
    • Put database.php in your .gitignore, this file should never going to be committed.
    • Change your deploy script to automatically copy database.default.php to default.php and replace the password value with whatever is needed
    • For development your developers will have to do it manually (or by a script)

    The same concept can be applied to bootstrap.php as well. And check this plugin out, we're using it for a project to have multiple environments configured and loaded conditionally.