Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to read env in plugin #7083

Closed
Asilent opened this issue Apr 23, 2021 · 6 comments
Closed

How to read env in plugin #7083

Asilent opened this issue Apr 23, 2021 · 6 comments

Comments

@Asilent
Copy link

Asilent commented Apr 23, 2021

I use docker run -e "env_name=xxx" kong:2.4.0-ubuntu to export env into container. Then I hope to get it by using os.getenv("env_name") in lua plugin. But it doesn't work and responds with nil.

@gszr
Copy link
Member

gszr commented Apr 23, 2021

Hi @Asilent,

This is expected behavior; in order to be able to read an environment variable from Lua code, you need to specify it in the Nginx env directive. In Kong, that can be done with directive injection.

In your case, you can achieve that by setting the following Kong configuration as an environment variable: KONG_NGINX_MAIN_ENV=env_name.

Closing this, but feel free to reopen if needed.

Happy Konging! 🦍

@gszr gszr closed this as completed Apr 23, 2021
@Tieske
Copy link
Member

Tieske commented Apr 23, 2021

Alternatively you can read the variable when the plugin code loads, since that is executed in the master process where variables are still available

@Asilent
Copy link
Author

Asilent commented Apr 25, 2021

I use directive injection like -e KONG_NGINX_HTTP_CUSTOM_ENV=env_value to add custom env when start the docker.
d5b0c0f-36f2-4904-a487-2d5e12028b45

Afrer this action, kong container can't restart with error.
The Error is as follows:
1619364668029

What should I do?

@Asilent
Copy link
Author

Asilent commented Apr 25, 2021

Hi @Asilent,

This is expected behavior; in order to be able to read an environment variable from Lua code, you need to specify it in the Nginx env directive. In Kong, that can be done with directive injection.

In your case, you can achieve that by setting the following Kong configuration as an environment variable: KONG_NGINX_MAIN_ENV=env_name.

Closing this, but feel free to reopen if needed.

Happy Konging! 🦍

Can I reopen this issue?

@Tieske
Copy link
Member

Tieske commented Apr 26, 2021

@Asilent you did not specify them as inidcated by @gszr .

This is the format:

export KONG_NGINX_MAIN_ENV=env_name

The name consists of 2 parts; KONG_NGINX_MAIN and ENV, and the value is env_name. The first part tells Kong where to inject the directive; in the Nginx main config file. The second one is the name of the directive ENV which causes Kong to inject it as name (lower-case) + value;

env env_name;

So you need to specify 2 environment variables for each variable you need

  1. to tell Kong it should keep that variable around
  2. the actual variable with its value

So you need to specify:

export KONG_NGINX_MAIN_ENV=HTTP_SSO_ENDPOINT
export HTTP_SSO_ENDPOINT=http://sso.my
export KONG_NGINX_MAIN_ENV=PROXY_SSO_ENDPOINT
export PROXY_SSO_ENDPOINT=http://sso.my

Now there is a problem, because the second KONG_NGINX_MAIN_ENV will overwrite the first... To get around that use a little hack, specify it like so (must be quoted because of the ;):

export "KONG_NGINX_MAIN_ENV=HTTP_SSO_ENDPOINT;env PROXY_SSO_ENDPOINT"
export HTTP_SSO_ENDPOINT=http://sso.my
export PROXY_SSO_ENDPOINT=http://sso.my

If you check the generated config file, you'll see that Kong injects this line:

env HTTP_SSO_ENDPOINT;env PROXY_SSO_ENDPOINT;

which is a valid nginx config, but now contains both variables. You should now be able to access those variables from your plugin code.

@demonkit
Copy link

@Asilent you did not specify them as inidcated by @gszr .

This is the format:

export KONG_NGINX_MAIN_ENV=env_name

The name consists of 2 parts; KONG_NGINX_MAIN and ENV, and the value is env_name. The first part tells Kong where to inject the directive; in the Nginx main config file. The second one is the name of the directive ENV which causes Kong to inject it as name (lower-case) + value;

env env_name;

So you need to specify 2 environment variables for each variable you need

  1. to tell Kong it should keep that variable around
  2. the actual variable with its value

So you need to specify:

export KONG_NGINX_MAIN_ENV=HTTP_SSO_ENDPOINT
export HTTP_SSO_ENDPOINT=http://sso.my
export KONG_NGINX_MAIN_ENV=PROXY_SSO_ENDPOINT
export PROXY_SSO_ENDPOINT=http://sso.my

Now there is a problem, because the second KONG_NGINX_MAIN_ENV will overwrite the first... To get around that use a little hack, specify it like so (must be quoted because of the ;):

export "KONG_NGINX_MAIN_ENV=HTTP_SSO_ENDPOINT;env PROXY_SSO_ENDPOINT"
export HTTP_SSO_ENDPOINT=http://sso.my
export PROXY_SSO_ENDPOINT=http://sso.my

If you check the generated config file, you'll see that Kong injects this line:

env HTTP_SSO_ENDPOINT;env PROXY_SSO_ENDPOINT;

which is a valid nginx config, but now contains both variables. You should now be able to access those variables from your plugin code.

if I run on k8s, how to write this env KONG_NGINX_MAIN_ENV?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants