Repository navigation
Add support for loading env variables from secret files #46524
Replies: 5 comments 14 replies
|
Would this not do what you want? Or am I mistaken? |
|
I can clearly see one problem with this approach: What should be the root path for all secrets? We can't just hardcode "/run/secrets/" it's totally wrong. So we need a config or .env, to pass this path. Kinda loop dependency. |
|
Agreed there needs to be something. Been searching and found no great solutions yet. Was looking at doing something like this https://github.com/preciousaang/laravel-gcp-secret-injector and https://gist.github.com/koistya/5fb986553efd21796e330f67cd660b2e |
|
Following. This would instantly simplify our environment configuration. |
|
I believe you can extend this behavior by writing a service provider and inject your secret into the config tree in the On the other hand, I am thinking about connecting Laravel to some centralized secret facility (for example, Infisical) where it fetches secrets dynamically from a remote host. Putting the API logic into |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Preface
Currently, Laravel can be configured via environment variables, and to a lesser extent, via default values passed to the
env()utility directly in config files.This is an issue, though: Confidential values provided to applications via the process environment is openly readable by other processes, and thus not always secure. Additionally, the ecosystem conventions promote not modifying the configuration files themselves; Laravel Shift, for example, will downright revert any modifications to default values to the factory value. In our projects, we try to have non-confidential production values in the config defaults, and keep the
.envfiles as small as possible; they have a tendency to grow uncontrollably otherwise.In container orchestration platforms such as Kubernetes or Docker Swarm, we have a more robust alternative to environment variables: Secret files. That is, sensitive values will be stored securely by the orchestrator, and mounted as files on the application container's file system. Using secret files in Laravel is possible right now by using a replacement
env()function (albeit it must be named different due to PHP's function loading..). This, however, must be implemented independently, requires changes in all config files, and breaks e.g. Shift's assumptions.Suggested solution
By adding the capability to read secret files to Laravel, it would become better integrated with containerised environments and improve the security posture of Laravel apps at once. I believe this is possible in a minimal invasive way, and without adding much overhead to Laravel.
I can see three strategies here:
FOOisn't defined in the environment, butFOO_PATHis, attempt to read it from that path; fall back to the default if that isn't possible (this is what MySQL does, for example).secret()utility that can be passed as the default value toenv(), users could recreate the behaviour from 1. and 2. manually:'key' => env('APP_KEY', secret('/path/to/file.txt'))would perform ordinary env variable lookup, and fall back to reading a secret file.Option 1 and 2 could be implemented by creating a Laravel-specific env repository that incorporates secret file lookup.
In all cases, this wouldn't be a breaking change: At worst, with option one, an additional disk access will be made, once during startup/caching, to read a missing file. At best, seamless secret support is added to Laravel.
I'd be happy to contribute a PR for this.
For reference, here's the secret helper we use right now instead of
envin our config files:All reactions