adonisjs / env

Framework agnostic environment variables parser and validator
https://docs.adonisjs.com/guides/environment-variables
MIT License
35 stars 10 forks source link

feat(parser): allow to add identifiers #35

Closed RomainLanz closed 8 months ago

RomainLanz commented 10 months ago

Hey there! 👋🏻

This PR adds a way to set up an identifier for an environment variable, helping to interpolate the value of your env.

It can be useful in multiple scenarios.

  1. To manage your secrets, you may use a Secrets Vault like Docker Secret or HashiCorp Vault. Doing so it will generate a path for your secret and won't give you any value directly. You can write a custom logic to read the file content.

    DB_PASSWORD=file:/run/secrets/db_password
  2. You may encrypt your secrets. You will be able to decrypt them on the fly.

    APP_KEY=encrypted:ksadskjadsjkdasjkdsjdjaskjkdjkasdkj
Env.identifier('file', (value: string) => {
  // ...
})

Env.create(new URL('../', import.meta.url), {
  ....
})
thetutlage commented 10 months ago

I see the current implementation will fail with certain values. For example:

Given, I add the following file identifier.

EnvParser.identifier('file', (_value: string) => {
  return '3000'
})

Following is the actual and the expected output.

ENV_USER=file_romain:password

// Expected value
file_romain:password

// Actual value
3000

Also, there should be a way to escape identifiers in the case where they conflict with some other value. For example:

ENV_USER=file\:///root/app/user.js

// Expected value
file:///root/app/user.js

// Actual value
3000