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

chore(unit_test,userspace): allow env var to get expanded in yaml even when part of a string #2918

Merged
merged 4 commits into from
Dec 13, 2023

Conversation

FedeDP
Copy link
Contributor

@FedeDP FedeDP commented Nov 20, 2023

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:

engine:
  ebpf:
    probe: /${HOME}/.falco/falco-bpf.o

in #2413.

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

new(userspace): support env variable expansion in all yaml, even inside strings.

…n when part of a string.

Moreover, support env variable embedding another env variable.

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
@FedeDP
Copy link
Contributor Author

FedeDP commented Nov 20, 2023

Expands #2562.

@@ -133,52 +136,81 @@ TEST(Configuration, configuration_environment_variables)
" sample_list:\n"
" - ${ENV_VAR}\n"
" - ' ${ENV_VAR}'\n"
" - $UNSED_XX_X_X_VAR\n";
conf.load_from_string(sample_yaml);
" - '${ENV_VAR} '\n"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We check that plugins config (that is getted as a sequence) is still correctly expanded.


// 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())
Copy link
Contributor Author

@FedeDP FedeDP Nov 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Short-circuit, when empty value is passed, just return default.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a test for it? I may have overlooked it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will double check and eventually add one!


if constexpr (std::is_same_v<T, std::string>)
{
return str;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@FedeDP FedeDP changed the title chore(unit_test,userspace): allow env var to get expanded in yaml even when part of a string wip: chore(unit_test,userspace): allow env var to get expanded in yaml even when part of a string Nov 20, 2023
std::stringstream ss(str);
T result;
if (ss >> result) return result;
if (ss >> std::boolalpha >> result) return result;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@FedeDP FedeDP force-pushed the new/env_var_expansion branch from 4add0ee to a7f2693 Compare November 20, 2023 14:33
@FedeDP FedeDP changed the title wip: chore(unit_test,userspace): allow env var to get expanded in yaml even when part of a string chore(unit_test,userspace): allow env var to get expanded in yaml even when part of a string Nov 21, 2023
@FedeDP
Copy link
Contributor Author

FedeDP commented Nov 21, 2023

/cc @jasondellaluce

I am not sure whether we want this for 0.37.0, leave it to other maintainers to decide ;)
Also cc @therealdwright who first opened the original PR!

@leogr leogr added this to the 0.37.0 milestone Nov 24, 2023
incertum
incertum previously approved these changes Nov 27, 2023
Copy link
Contributor

@incertum incertum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/approve

auto integer = conf.get_scalar<int32_t>("num_test", -1);
ASSERT_EQ(integer, 12);

/* Clear the set environment variables after testing */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or just unsetenv?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 :)

unit_tests/falco/test_configuration.cpp Outdated Show resolved Hide resolved

// 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())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a test for it? I may have overlooked it.

… default.

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
@FedeDP
Copy link
Contributor Author

FedeDP commented Nov 27, 2023

Do we have a test for it? I may have overlooked it.

Added in latest commit, thank you!

incertum
incertum previously approved these changes Nov 27, 2023
Copy link
Contributor

@incertum incertum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/approve

@FedeDP
Copy link
Contributor Author

FedeDP commented Nov 28, 2023

Trying to put this in the 0.37.0 milestone; let's see if we can make this happen :D
/milestone 0.37.0

@FedeDP
Copy link
Contributor Author

FedeDP commented Dec 4, 2023

/hold


#include "config_falco.h"

#include "event_drops.h"
#include "falco_outputs.h"

class yaml_helper;

class yaml_visitor {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No more custom logic needed.

Copy link
Contributor Author

@FedeDP FedeDP Dec 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

@FedeDP FedeDP Dec 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

… scalar values of yaml file.

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
@FedeDP FedeDP force-pushed the new/env_var_expansion branch from cf3bb89 to 76274a3 Compare December 5, 2023 08:19
@FedeDP
Copy link
Contributor Author

FedeDP commented Dec 5, 2023

/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.

@leogr
Copy link
Member

leogr commented Dec 5, 2023

/assign

I'll take a look as soon as I can

Copy link
Member

@leogr leogr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed with @FedeDP this PR introduces a minor behavior change that needs to be signaled and documented.

Anyway, the feature is great and the PR LGTM.

@incertum PTAL again

Copy link
Contributor

@incertum incertum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/approve

@poiana
Copy link
Contributor

poiana commented Dec 13, 2023

[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

Needs approval from an approver in each of these files:
  • OWNERS [FedeDP,incertum,leogr]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@poiana poiana merged commit cbbcb61 into master Dec 13, 2023
20 checks passed
@poiana poiana deleted the new/env_var_expansion branch December 13, 2023 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants