Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

Overriding Secrets Locally (Local Vault)

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $10.00

With a Subscription, click any sentence in the script to jump to that part of the video!

Login Subscribe

What if I need to override a secret value on my local machine? MAILER_DSN is a perfect example: in the dev secrets vault, it's set to use the null transport. What if I want to see what an email looks like... and so I need to override that value locally to send to Mailtrap?

Well, we could run over to the terminal and say:

php bin/console secrets:set MAILER_DSN

And then modify the vault value. But... ugh - then I have to be super careful not to commit that change... and eventually... I will on accident... and I'll look super uncool because I accidentally changed a development secret. Fortunately, for absent-minded committers like me, there's a built-in solution to help!

Setting a Secret into the "Local" Vault

Pretend like we're going to override the MAILER_DSN secret... but add an extra --local flag to the end:

php bin/console secrets:set MAILER_DSN --local

So far... this looks identical to before. I'll paste in my Mailtrap value... which the command hides for security reasons. And... fascinating! This didn't change our dev vault at all! Nope, it apparently added the secret to .env.dev.local.

Quick review about .env files: Symfony allows you to create a .env.local file as a way to override values in .env. And thanks to our .gitignore, .env.local is ignored from Git. And, though it's not as common, you can also create a .env.dev.local file. It works the same way: it overrides .env and isn't committed. The only difference - which is super minor - is that it's only loaded in the dev environment.

The point is: this "local" vault thing is nothing more than a fancy way of setting an environment variable to this "local" file.

Environment Variables Override Secrets

And... wait: that's kind of beautiful! I mentioned earlier that when you use the environment variable system - when you use that %env()% syntax - Symfony first looks for MAILER_DSN as an environment variable. If it finds it, it uses it. And only if it does not find it, does it then go and try to see if it is a secret.

So now, in the dev environment on my machine, it will find MAILER_DSN as an environment variable! Go refresh the page to prove it. There it is: my local override.

You can use this cool secrets:set --local thing if you want... but really all you need to understand is that if you want to override a secret value locally, just set it as an environment variable.

And, personally, I don't love having .env.local and .env.dev.local - it seems like overkill. So I would delete .env.dev.local and instead put my overridden MAILER_DSN directly into .env.local.

But... don't do that - delete the override entirely: it'll help me show you one more thing.

Now that we understand that environment variables override secrets, we can unlock three possibilities. The first is what we just saw: we can override a secret locally by creating an environment variable. The other two deal with a performance optimization on production and... our test environment... which is currently busted! That's next.

Leave a comment!

0
Login or Register to join the conversation
Cat in space

"Houston: no signs of life"
Start the conversation!

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": "^7.3.0",
        "ext-iconv": "*",
        "antishov/doctrine-extensions-bundle": "^1.4", // v1.4.2
        "aws/aws-sdk-php": "^3.87", // 3.110.11
        "composer/package-versions-deprecated": "^1.11", // 1.11.99
        "doctrine/doctrine-bundle": "^2.0", // 2.0.6
        "doctrine/doctrine-migrations-bundle": "^1.3|^2.0", // 2.1.2
        "doctrine/orm": "^2.5.11", // v2.7.2
        "doctrine/persistence": "^1.3.7", // 1.3.8
        "easycorp/easy-log-handler": "^1.0", // v1.0.9
        "http-interop/http-factory-guzzle": "^1.0", // 1.0.0
        "knplabs/knp-markdown-bundle": "^1.7", // 1.8.1
        "knplabs/knp-paginator-bundle": "^5.0", // v5.0.0
        "knplabs/knp-snappy-bundle": "^1.6", // v1.7.0
        "knplabs/knp-time-bundle": "^1.8", // v1.11.0
        "league/flysystem-aws-s3-v3": "^1.0", // 1.0.23
        "league/flysystem-cached-adapter": "^1.0", // 1.0.9
        "league/html-to-markdown": "^4.8", // 4.8.2
        "liip/imagine-bundle": "^2.1", // 2.3.0
        "nexylan/slack-bundle": "^2.1", // v2.2.1
        "oneup/flysystem-bundle": "^3.0", // 3.3.0
        "php-http/guzzle6-adapter": "^2.0", // v2.0.1
        "sensio/framework-extra-bundle": "^5.1", // v5.5.3
        "symfony/asset": "5.0.*", // v5.0.2
        "symfony/console": "5.0.*", // v5.0.2
        "symfony/dotenv": "5.0.*", // v5.0.2
        "symfony/flex": "^1.0", // v1.17.6
        "symfony/form": "5.0.*", // v5.0.2
        "symfony/framework-bundle": "5.0.*", // v5.0.2
        "symfony/mailer": "5.0.*", // v5.0.2
        "symfony/messenger": "5.0.*", // v5.0.2
        "symfony/monolog-bundle": "^3.5", // v3.5.0
        "symfony/security-bundle": "5.0.*", // v5.0.2
        "symfony/sendgrid-mailer": "5.0.*", // v5.0.2
        "symfony/serializer-pack": "^1.0", // v1.0.2
        "symfony/twig-bundle": "5.0.*", // v5.0.2
        "symfony/twig-pack": "^1.0", // v1.0.0
        "symfony/validator": "5.0.*", // v5.0.2
        "symfony/webpack-encore-bundle": "^1.4", // v1.7.2
        "symfony/yaml": "5.0.*", // v5.0.2
        "twig/cssinliner-extra": "^2.12", // v2.12.0
        "twig/extensions": "^1.5", // v1.5.4
        "twig/inky-extra": "^2.12" // v2.12.0
    },
    "require-dev": {
        "doctrine/doctrine-fixtures-bundle": "^3.0", // 3.3.0
        "fzaninotto/faker": "^1.7", // v1.8.0
        "symfony/browser-kit": "5.0.*", // v5.0.2
        "symfony/debug-bundle": "5.0.*", // v5.0.2
        "symfony/maker-bundle": "^1.0", // v1.14.3
        "symfony/phpunit-bridge": "5.0.*", // v5.0.2
        "symfony/profiler-pack": "^1.0", // v1.0.4
        "symfony/var-dumper": "5.0.*" // v5.0.2
    }
}
userVoice