chore(unit_test,userspace): allow env var to get expanded in yaml even when part of a string#2918
chore(unit_test,userspace): allow env var to get expanded in yaml even when part of a string#2918
Conversation
…n when part of a string. Moreover, support env variable embedding another env variable. Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
|
Expands #2562. |
| " - ' ${ENV_VAR}'\n" | ||
| " - $UNSED_XX_X_X_VAR\n"; | ||
| conf.load_from_string(sample_yaml); | ||
| " - '${ENV_VAR} '\n" |
There was a problem hiding this comment.
Tests were expanded:
- new case with whitespace AFTER the expanded string. Moreover, since we now support env variables embedded inside bigger strings, both
${ENV_VAR}and${ENV_VAR}are now expanded - we do now tests that env variables found in the middle of strings are properly expanded (
paths[0,1,2,3]cases) paths[4]checks an even weirder case, ie: when the env variable gets expanded to another env variable. It must get resolved twice.
There was a problem hiding this comment.
We check that plugins config (that is getted as a sequence) is still correctly expanded.
userspace/falco/yaml_helper.h
Outdated
|
|
||
| // Helper function to convert string to the desired type T | ||
| auto convert_str_to_t = [&default_value](const std::string& str) -> T { | ||
| if (str.empty()) |
There was a problem hiding this comment.
Short-circuit, when empty value is passed, just return default.
There was a problem hiding this comment.
Do we have a test for it? I may have overlooked it.
There was a problem hiding this comment.
Will double check and eventually add one!
userspace/falco/yaml_helper.h
Outdated
|
|
||
| if constexpr (std::is_same_v<T, std::string>) | ||
| { | ||
| return str; |
There was a problem hiding this comment.
For strings, just return it.
This workarounds the fact that stringstream splits values by whitespaces, and this is something we don't want to be done when converting string->string.
userspace/falco/yaml_helper.h
Outdated
| std::stringstream ss(str); | ||
| T result; | ||
| if (ss >> result) return result; | ||
| if (ss >> std::boolalpha >> result) return result; |
There was a problem hiding this comment.
Enforce std::boolalpha so that true and false are correctly parsed to booleans.
Moreover, added some more tests around env vars. Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
4add0ee to
a7f2693
Compare
|
/cc @jasondellaluce I am not sure whether we want this for 0.37.0, leave it to other maintainers to decide ;) |
| auto integer = conf.get_scalar<int32_t>("num_test", -1); | ||
| ASSERT_EQ(integer, 12); | ||
|
|
||
| /* Clear the set environment variables after testing */ |
There was a problem hiding this comment.
On windows we should map unsetenv to _putenv(string,"") anyway, so i decided to avoid adding a new macro and just use what i got :)
userspace/falco/yaml_helper.h
Outdated
|
|
||
| // Helper function to convert string to the desired type T | ||
| auto convert_str_to_t = [&default_value](const std::string& str) -> T { | ||
| if (str.empty()) |
There was a problem hiding this comment.
Do we have a test for it? I may have overlooked it.
… default. Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
Added in latest commit, thank you! |
|
Trying to put this in the 0.37.0 milestone; let's see if we can make this happen :D |
|
/hold |
8cb1a26 to
cf3bb89
Compare
|
|
||
| class yaml_helper; | ||
|
|
||
| class yaml_visitor { |
There was a problem hiding this comment.
yaml_visitor is a small visitor API for yaml files, to call a callback function on each scalar value.
It is private to yaml_helper since it receives non-constant YAML::Nodes (thus it can modify the parsed yaml structure).
|
|
||
| // If it's not an environment variable reference, return the value as is | ||
| return node.as<T>(); | ||
| return node.as<T>(default_value); |
There was a problem hiding this comment.
No more custom logic needed.
There was a problem hiding this comment.
BREAKING CHANGE: in #2562 env var that resolved to empty string (both non-set env vars and env vars set to "") returned default value.
This was in constrast with everything else in our yaml impl (ie: we only returned default value if node is not defined).
I reset to the same behavior of all other variables.
There was a problem hiding this comment.
BREAKING CHANGE: node.as<T>(default_value); will return default_value when it is not able to parse the node to T.
It makes sense IMHO.
There was a problem hiding this comment.
… scalar values of yaml file. Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
cf3bb89 to
76274a3
Compare
|
/unhold Now we support env vars expansion in all getters, since we pre-process the yaml root to expand any scalar value as soon as the document is loaded. |
|
/assign I'll take a look as soon as I can |
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: FedeDP, incertum, leogr The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
What type of PR is this?
/kind feature
Any specific area of the project related to this PR?
/area engine
What this PR does / why we need it:
Moreover, support env variable embedding another env variable.
Basically, this PR would allow us to use eg:
in #2413.
Which issue(s) this PR fixes:
Fixes #
Special notes for your reviewer:
Does this PR introduce a user-facing change?: