1

I'm looking to securely replace the use of a local .env file in my PHP script with HashiCorp Vault Secrets. Here’s how my current code loads environment variables from .env:

$envFile = __DIR__ . '/../.env';
if (file_exists($envFile)) {
    $envVariables = parse_ini_file($envFile);
    foreach ($envVariables as $key => $value) {
        putenv("$key=$value");
    }
}

$servername = getenv('DB_HOST');
$username = getenv('DB_USERNAME');
$password = getenv('DB_PASSWORD');
$dbname = getenv('DB_NAME');

I installed the HCP CLI on my Ubuntu 24.04 machine following this HashiCorp tutorial. I successfully authenticated and retrieved secrets from HashiCorp Vault Secrets using the command hcp vault-secrets secrets list. I'm running nginx as the web server.

I want to securely retrieve these secrets from HashiCorp Secrets for use in my PHP script, without exposing or leaking them.

The PHP script is part of a live website, so I need a solution that can either inject secrets into the environment when nginx starts or have the PHP script dynamically retrieve the secrets in a secure and efficient way on each request I guess. I want to avoid exposing the secrets as environment variables in phpinfo.

What can I do?

3
  • There appears to exist at least one PHP client for vault; have you looked at that?
    – larsks
    Commented Sep 7, 2024 at 16:22
  • @larsks I was hoping there could be a solution that wouldn't require external libraries Commented Sep 8, 2024 at 13:41
  • Vault has a well documented REST API, so you could just write your own client.
    – larsks
    Commented Sep 8, 2024 at 13:52

0

You must log in to answer this question.

Browse other questions tagged .