Pass Environment variables to Kong Plugins
- Kalidass Mookkaiah
- Jan 25, 2024
- 1 min read
Updated: Feb 19, 2024
Define Environment variable
Define the environment variables as below
export HTTP_SSO_ENDPOINT=http://example.com
export PROXY_SSO_ENDPOINT=http://example.com
How to inject it into Kong using NGINX environment variable
Kong by default will pick the environment variable KONG_NGINX_MAIN_ENV and inject into Kong.
For injecting the above environment variables into Kong add the following environment variable
export "KONG_NGINX_MAIN_ENV=HTTP_SSO_ENDPOINT;env PROXY_SSO_ENDPOINT"
This will inject the following into kong
env HTTP_SSO_ENDPOINT;env PROXY_SSO_ENDPOINT;
How to use these environment variables
Can use theses environment variables in multiple ways
1. Using vault
With in any plugin use the kong pdf vault as below to access the variable
kong.vault.get("{vault://env/HTTP_SSO_ENDPOINT}")
kong.vault.get("{vault://env/PROXY_SSO_ENDPOINT}")
Using vault PDK is sandbox safe
2. Using os
Another option is to get the variable using lua os
os.getenv("HTTP_SSO_ENDPOINT")
os.getenv("PROXY_SSO_ENDPOINT")
How to use the environment variable in plugin
Here is a example plugin using pre-function plugin.
Add the following in function config to print the environment in logs using pre-function.
local httpsso=kong.vault.get("{vault://env/HTTP_SSO_ENDPOINT}")kong.log(">>>>>>>>>>123456789<<<<<<<<<<<<<<<") kong.log(">>>>",httpsso,"<<<<<<<<<<")
Comments