refactor: force getenv to access only OS env with prefix EMQXVAR_

This commit is contained in:
zmstone 2024-07-29 23:54:00 +02:00
parent 7f7d0741d2
commit a49cd78aae
3 changed files with 6 additions and 4 deletions

View File

@ -583,7 +583,7 @@ getenv(Bin) when is_binary(Bin) ->
EnvKey = ?ENV_CACHE(Bin),
case persistent_term:get(EnvKey, undefined) of
undefined ->
Name = erlang:binary_to_list(Bin),
Name = "EMQXVAR_" ++ erlang:binary_to_list(Bin),
Result =
case os:getenv(Name) of
false ->

View File

@ -77,5 +77,5 @@ system_test() ->
EnvName = erlang:atom_to_list(?MODULE),
EnvVal = erlang:atom_to_list(?FUNCTION_NAME),
EnvNameBin = erlang:list_to_binary(EnvName),
os:putenv(EnvName, EnvVal),
os:putenv("EMQXVAR_" ++ EnvName, EnvVal),
?assertEqual(erlang:list_to_binary(EnvVal), emqx_variform_bif:getenv(EnvNameBin)).

View File

@ -1,2 +1,4 @@
Added a new builtin function `getenv` in the rule engine and variform expression to access the environment variables.
Note this value is immutable once loaded from the environment.
Added a new builtin function `getenv` in the rule engine and variform expression to access the environment variables with below limitations.
- Prefix `EMQXVAR_` is added before reading from OS environment variables. i.e. `getenv('FOO_BAR')` is to read `EMQXVAR_FOO_BAR`.
- The values are immutable once loaded from the OS environment.