Unable to use environment variables in Lua code - nginx

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.

Related

Setting environment variables in jupyter hub

I followed approach in this thread. I can easily set env variable in jupyter hub using the %env VAR = 5. However, when I try to print out this variable in the terminal I get only a blank line, as if the variable did not exist at all. Is it somehow possible to be able to print in terminal the env var defined in the notebook?
Setting environment variables from the notebook results in these variables being available only from that notebook.
%env VAR=TEST
import os
print(os.environ["VAR"])
...
>>> TEST
If you want to persist the variable, you need to put it either in the kernel.json file, or in systemd service file for jupyterhub, or in something like ~/.bashrc.
JupyterHub has a jupyterhub_config.py file (e.g. located in /etc/jupyterhub/jupyterhub_config.py) which can be used to control various aspects of the hub and the notebook environment.
In order to make a specific environment variable available to all notebooks on the hub, add the following lines to it:
c.Spawner.environment = {
'VAR': 'Test'
}
and restart the JupyterHub service (something like sudo service jupyterhub restart).
P.S. If you would just like to forward an environment variable from the user's environment, there is also
c.Spawner.env_keep = ['VAR']
Following from #leopold.talirz's answer, for those wanting to modify an environment variable without overwriting it (i.e. append a path to the PATH variable), I found you can do something like the following,
import os
original_path = os.environ['PATH']
c.Spawner.environment = {
'PATH': '/path/to/foo:{}'.format(original_path)
}
NOTE: In my case, I'm working with The Littlest JupyterHub, so I put the above in /opt/tljh/config/jupyterhub_config.d/environment.py.

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

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)%'

How to get the environment variable name in nginx config

I have nginx server up running as docker with base image openresty/openresty:1.15.8.3-buster.
In the docker file I have an environment variable that I am passing as below
ENV UPLOAD_FOLDER /sharedvolume
How do I access this variable with in nginx.config?
I tried using set_by_lua as below
set_by_lua $store_path 'return os.getenv("UPLOAD_FOLDER")';
But the store_path value was giving nil
From the documentation:
System Environment Variable Support
If you want to access the system environment variable, say, foo, in Lua via the standard Lua API os.getenv, then you should also list this environment variable name in your nginx.conf file via the env directive. For example,
env foo;
So add env UPLOAD_FOLDER; and it will work.

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.

How to reference OS Environment Variables in nginx.conf

In nginx.conf.
After set a variable by set $name value,
i can reference it like $name,
But when I export an OS Environment Variable
by env name_from_env,
like https://nginx.org/en/docs/ngx_core_module.html#env said,
and i am sure the name_from_env is valid which
defined form nginx's parent process.
But, my friends, how to reference it ?
$name_from_env or ${name_from_env} or
%name_from_env% didn't work what I've tried before.
nginx doesn't have the built-in ability to reference its environment variables in the configuration at present. The simplest solution however is the perl_set directive from ngx_http_perl_module, an extra module for nginx. The official nginx packaging builds the Perl module dynamically so it's a case of ensuring you install the extra nginx-module-perl package (or configure your custom build of nginx, if that's what you're doing).
Configuration looks like this:
# Make environment variable available
env NAME_FROM_ENV;
# Load dynamic module (if built with Perl as dynamic module; omit if static)
load_module modules/ngx_http_perl_module.so;
http {
server {
location / {
# Use Lua to get get and set the variable
perl_set $name_from_env 'sub { return $ENV{"NAME_FROM_ENV"}; }';
...
}
}
}
See also https://docs.apitools.com/blog/2014/07/02/using-environment-variables-in-nginx-conf.html for how to use Lua to achieve the same thing. Lua support requires a third party module and isn't shipped with nginx's default packages.
It should be $name_from_env, just like any other Nginx variable.
Note that env can only be used in the main part of your config, not http, server or location blocks.
I'm guessing that env isn't really what you need in any case. If you are trying to pass variables down to your application, you should use proxy_param or fastcgi_param (depending on how you are talking to your upstream):
fastcgi_param MYVAR foo;

Resources