I use a small script that fetches the credentials necessary for applications to run (MySQL databases, API credentials for 3rd-party apps, etc.):
function get_credentials($id) {
$env = $_SERVER['DOCUMENT_ROOT'];
$host = '/path/to/host/www';
$local = '/path/to/local/www';
if (strpos($env, $host) !== false) $source = '/path/to/host/.wwwcred';
elseif (strpos($env, $local) !== false) $source = '/path/to/local/.wwwcred';
$index = file_exists($source) ? json_decode(file_get_contents($source), true) : false;
return ($index ? true : false) ? (array_key_exists($id, $index) ? $index[$id] : false) : false;
}
The source (.wwwcred) is a flat, plain-text JSON file stored outside the web root with the minimum required permissions. Any application that needs to "login" to anything uses the above function to retrieve credentials; usernames and passwords are never stored inside any script/application.
My Question: Is there a more secure way to complete this task? Is there a procedure for encrypting the passwords stored in .wwwcred (and decrypt before passing)? Or is there a better approach to storing this kind of data altogether?