Njs: Add ability to use Environment Variables as it's possible with Lua and Perl

Created on 10 Jan 2019  路  4Comments  路  Source: nginx/njs

As of Using Environment Variables In Nginx.conf

Lua supports os.getenv("API_KEY")

Perl supports $ENV{"API_KEY"};

would be very usefull and appreciated to have such an ability in njs too, especially for cases like handling secrets for Creating HS JWT

thank you

feature

Most helpful comment

@appelgriebsch

You may try the following patch: https://gist.github.com/xeioex/4cde7653d301694700373ca96fcefa69

Please note that you have to declare all the needed variables using env directive. Because by default nginx removes all environment variables inherited from its parent process except the TZ variable.

All 4 comments

Just looked into that same scenario myself and a potential workaround would be to put the JWT key into a local file first and have it read out from Nginx via the filesystem API. But to be honest I would also prefer to have os.getenv or something similar in the JS core module.

Is there some roadmap on the upcoming changes / extensions and when they are ready?

Hi @appelgriebsch

There is no official roadmap as of now. But you can find most of the planned features to add by 'feature' tag on the issues page. Upcoming changes can be found by the activity (available patches) in the specific feature tasks.
Release notes are available here, they are published through nginx-announce.

Access to environment variables will be added in 0.3.3.

@appelgriebsch

You may try the following patch: https://gist.github.com/xeioex/4cde7653d301694700373ca96fcefa69

Please note that you have to declare all the needed variables using env directive. Because by default nginx removes all environment variables inherited from its parent process except the TZ variable.

@xeioex

why not?

diff -r 2fb43ddbce84 njs/njs_builtin.c
--- a/njs/njs_builtin.c Mon Jun 10 22:23:56 2019 -0400
+++ b/njs/njs_builtin.c Tue Jun 18 22:40:48 2019 +0300
@@ -1335,24 +1335,28 @@
         .type = NJS_PROPERTY_HANDLER,
         .name = njs_string("argv"),
         .value = njs_prop_handler(njs_process_object_argv),
+        .enumerable = 1,
     },

     {
         .type = NJS_PROPERTY_HANDLER,
         .name = njs_string("env"),
         .value = njs_prop_handler(njs_process_object_env),
+        .enumerable = 1,
     },

     {
         .type = NJS_PROPERTY_HANDLER,
         .name = njs_string("pid"),
         .value = njs_prop_handler(njs_process_object_pid),
+        .enumerable = 1,
     },

     {
         .type = NJS_PROPERTY_HANDLER,
         .name = njs_string("ppid"),
         .value = njs_prop_handler(njs_process_object_ppid),
+        .enumerable = 1,
     },

 };

but njs.dump and JSON.stringify should be fixed also to invoke property handlers.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

porunov picture porunov  路  3Comments

axipo picture axipo  路  3Comments

drsm picture drsm  路  4Comments

drsm picture drsm  路  4Comments

xeioex picture xeioex  路  3Comments