How do you configure a binary environment variable with Symfony YAML configuration? - symfony

Suppose I have FOO=dGVzdA== in my .env file and then I try to load this binary environment variable in my YAML configuration.
foo: !!binary '%env(FOO)%'
This errors out because it tries to decode %env(FOO)% verbatim as if it were base64-encoded. That is, it does not substitute the environment variable when prefixed with !!binary. So then, how does one actually use a binary environment variable?

It seems the correct way to express this is:
foo: '%env(base64:FOO)%'

Related

Some env vars in .env are not available in debug:conter --env-vars

My .env file has the following entries, but FOO is not listed when I run bin/console debug:container --env-vars. Note however that $_ENV['FOO'] exists when I dump the variable.
FOO=1
AUTH0_CLIENT_ID=clientid
AUTH0_CLIENT_SECRET=secret
AUTH0_DOMAIN=myapp.us.auth0.com
What determines if an env var defined in .env will be available in the container?
Not really sure if this is worthy of an answer but I suppose it might help.
The Symfony .env files are really just one possible sources of $_ENV variables. There are lots of other env variables floating around and of course in production, you might not use .env at all.
So rather than save access to all env variables, the Symfony configuration system only saves those that are actually used. So in this case:
# config/services.yaml
parameters:
foo: '%env(resolve:FOO)%'
Will result in:
bin/console debug:container --env-vars
APP_SECRET n/a "84dc6de50e6f2f7af3db3f78f886840f"
DATABASE_URL n/a "mysql://db_user:db_password#127.0.0.1:3306/db_name?serverVersion=5.7"
FOO n/a "1"
MAILER_DSN n/a n/a
VAR_DUMPER_SERVER "127.0.0.1:9912" n/a
For a fresh 5.1 project.
Off-topic but vaguely interesting to me at least, the above command also generates a warning
[WARNING] The following variables are missing:
* MAILER_DSN
MAILER_DSN is commented out in the default .env file. So I guess it is possible to use env values during configuration even if none are defined at compile time. Good way to check for spelling errors I guess.

Symfony4 - Casting env variable issue

apparently, Symfony cast env variable is not working on symfony 4.0.
I have this configuration:
cache:
session:
enabled: "%env(bool:SESSION_CACHE_ENABLED)%"
But I get this error:
Invalid type for path "cache.session.enabled". Expected boolean, but got string.
What is my problem? I'm using symfony version 4.0
Thanks
EDIT
Probably, it is a problem of the plugins. That's what I think: Symfony 4 is now based on .env config variable, that are STRING as default; to handle this, S4 is able to use "casting" env var
'%env(bool:myvar)%'
And it works; if you do a var_dump within a controller, you can see that the variable is a boolean.
Most of current plugins, also those who supports S4, are not able to use this syntax, so, they see that variable as STRING, and the validator return an error.
These plugins should be fixed or, actually, I can duplicate the .yml file on each package/{env}/ dir with separated configuration ( the situation that I would avoid with .env )
The problem is fixed in symfony 4.1 https://github.com/symfony/symfony/issues/22151

Unable to use environment variables in Lua code

I have some Lua code, which I use in my openresty nginx.conf file. This Lua code contains such lines:
...
local secret = os.getenv("PATH")
assert(secret ~= nil, "Environment variable PATH not set")
...
Just for testing reasons I tried to check if PATH variable is set and for some reason the assert statement does not pass. I see in the console:
Environment variable PATH not set
However, when I run this
$ echo $PATH
I see, that this variable indeed has some value. So, what is wrong with that and how can I fix it?
You need to tell nginx to make environment variables available. From the docs for the env directive: "By default, nginx removes all environment variables inherited from its parent process except the TZ variable. This directive allows preserving some of the inherited variables, changing their values, or creating new environment variables."
So, in your case you'd need to specify env PATH; in nginx.conf.

Symfony2 Composer and environment variables

I would like to set the configuration of my symfony2 project using environment variables.
In the server I have defined:
SYMFONY__DATABASE__USER
SYMFONY__DATABASE__PASSWORD
SYMFONY__DATABASE__NAME
SYMFONY__DATABASE__HOST
SYMFONY__DATABASE__DRIVER
My parameters.yml.dist looks like this:
#app/config/parameters.yml.dist
parameters:
database_host: "%database.host%"
database_port: ~
database_name: "%database.name%"
database_user: "%database.user%"
database_password: "%database.password%"
database_driver: "%database.driver%"
when I run composer I get an exception
composer install --dev --no-interaction --prefer-source
[Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException]
You have requested a non-existent parameter "database.driver". Did you mean one of these: "database_user", "database_driver"?
These variables are defined in the server so I can modify the parameters.yml.dist to define these values. But this does not seams the right way, because wat I really want to use are the environment variables.
Note: I want to read this environment variables in travis, heroku and my vagrant machine. I only want to have in the repository the vagrant machine variables.
Which is the proper way to do this?
How should look my parameters.yml.dist?
Looks you are doing everything okay.
Here is the complete documentation for Setting Environment Variables which I believe you already read.
What is important to note is this:
Also, in order for your console to work (which does not use Apache),
you must export these as shell variables. On a Unix system, you can
run the following:
$ export SYMFONY__DATABASE__USER=user
$ export SYMFONY__DATABASE__PASSWORD=secret
I remember once I have a similar issue, I was setting everything on APACHE, but when running commands it wasn't working because I forgot to EXPORT the variables on the system.
Be aware that using export is a temp solution, if you reset your server those values will be lost, you will need to setup in a permanent way according to your OS.
I think you solved this long time ago, but the problem is actually that you have 2 _ between DATABASE and USER and the parser for this have a string replace function that replaces every __ with a . .
For your example to work you should have written like this:
SYMFONY__DATABASE_USER -> database_user
SYMFONY__DATABASE__USER -> database.user
You can try this bundle if your system version is >= 2.6.2:
This bundle provides a way to read parameters from environment
variables at runtime. The value defined in the container parameter is
used as fallback when the environment variable is not available.

Pass an environment variable into SBT to use in a Specs2 test?

What is the correct way of passing in an environment variable into SBT so that it can be accessed using Specs2? (And then retrieving the value in Specs2.) The environment variable will contain an API key to use for testing.
It needs to be an environment variable to work with Travis CI's encrypted environment variable functionality[1]
My setup:
SBT 0.13.0
Specs2 2.3.4
Travis CI
Edit: bonus points if somebody can link to an open-source repo that does this. There must be a few!
[1] Using secret api keys on travis-ci
I guess that you can encrypt your key with the travis api and get:
xxxEncryptedxxx
Then you can use the CommandLineArguments trait to pass arguments from the command-line in SBT to your specification.
In .travis.yml
sbt ++$TRAVIS_SCALA_VERSION testOnly *MySpec* -- key xxxEncryptedxxx
In MySpec.scala
class MySpec extends mutable.Specification with CommandLineArguments {
"this is an API test" >> {
arguments.commandLine.value("key").map { k =>
callApi(k) must beOk
}.getOrElse(ko("you need to pass a key on the command line"))
}
}
From you questions, I presume you're looking to pass secure environment variables using Travis's built-in support for encryption?
If so the environment variable is set before SBT is run, so it should be available to all processes. I don't use Specs, but the standard JVM way to get environment variable is to use System.getenv(String). It's possible that sbt deletes the environment variables before running Specs; if that's true then fixing that has to be done in your build.sbt somehow, and isn't specific to Travis.

Resources