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

feat: support parsing of system environment variables in yaml #2562

Conversation

therealdwright
Copy link
Contributor

@therealdwright therealdwright commented May 22, 2023

What type of PR is this?

/kind feature

Any specific area of the project related to this PR?

/area engine
/area tests

What this PR does / why we need it:

In order to allow the user to supply environment variables in standard
ways performed in other applications the get_scalar function has been
extended to support defining an environment variable in the format
${FOO}. Environment variables can be escaped via defining as $${FOO}.
As this handles some additional complexity, a unit test has been added
to cover this new functionality

Which issue(s) this PR fixes:
Fixes #2561

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

feat: support parsing of system environment variables in yaml

@poiana
Copy link

poiana commented May 22, 2023

Welcome @therealdwright! It looks like this is your first PR to falcosecurity/falco 🎉

@jasondellaluce
Copy link
Contributor

This is interesting! Thanks a lot. I'm gonna set this to the current release milestone just to not lose track of it, however keep in mind that we're very close to a Falco release and getting this into it will require a decision among the Falco maintainers and the release managers.

/milestone 0.35.0

cc @falcosecurity/falco-maintainers

@poiana poiana added this to the 0.35.0 milestone May 22, 2023
@FedeDP
Copy link
Contributor

FedeDP commented May 22, 2023

I love this! 👍 from me for the 0.35 milestone.

FedeDP
FedeDP previously approved these changes May 22, 2023
Copy link
Contributor

@FedeDP FedeDP 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

poiana commented May 22, 2023

LGTM label has been added.

Git tree hash: db4fea09ce034163dfa7815ec098655c97a36b58

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.

I love this feature. However, I would like more time for testing and thinking about corner cases. For example:

  • How can I escape from it if I wanted the literal $SOMETHING as a string (instead of the env var value)? 🤔

  • How this feature interact with the program_output which is a shell code? 🤔

  • Are any other possible side effects? 🤔

For the reasons above and since we are a few days from releasing Falco 0.35, I propose pushing this to Falco 0.36. Let's discuss it!

@therealdwright
Copy link
Contributor Author

therealdwright commented May 23, 2023

  • How can I escape from it if I wanted the literal $SOMETHING as a string (instead of the env var value)? 🤔

In most other applications, you would quote the string e.g. "$SOMETHING" . However, This has not something I have come across in practice. This implementation looks for a { at position 1, and a } at position n - 1, so it only handles unquoted environment strings, consistent with other OSS implementations (e.g. filebeat).

  • How this feature interact with the program_output which is a shell code? 🤔

The above response covers this somewhat, but I also tested the original behaviour and ran regression tests while developing the change. Does the regression suite cover such cases?

In terms of timeline, I'll leave it to the maintainers, I selfishly want this sooner rather than later as not being able to define environment variables leads to one of two consequences for my organisation.

  1. We have to encrypt the entire configuration file with something like sops if we want to use the current implementation as is.
  2. We must write some custom tooling to extend the upstream Falco image to iterate through the YAML and eval all environment variables.

Neither of these are the direction we want to take, but if we have to, so be it.

Edit: please let me know if you want additional unit testing to cover such cases, I would be happy to do so.

@leogr
Copy link
Member

leogr commented May 23, 2023

  • How can I escape from it if I wanted the literal $SOMETHING as a string (instead of the env var value)? thinking

In most other applications, you would quote the string e.g. "$SOMETHING" . However, This has not something I have come across in practice. This implementation looks for a { at position 1, and a } at position n - 1, so it only handles unquoted environment strings, consistent with other OSS implementations (e.g. filebeat).

  • How this feature interact with the program_output which is a shell code? thinking

The above response covers this somewhat, but I also tested the original behaviour and ran regression tests while developing the change. Does the regression suite cover such cases?

AFAIK, no.

The filebeat example is interesting. I have never used it so, but their docs report:

If you need to use a special character in your configuration file, use $ to escape the expansion. For example, you can escape ${ or } with $${ or $}.

Anyway, I'm not saying this is necessarily an issue - I just don't know yet and I couldn't find enough time to iterate on that 😅

In terms of timeline, I'll leave it to the maintainers, I selfishly want this sooner rather than later as not being able to define environment variables leads to one of two consequences for my organisation.

👍 I'm open to any decision, just the timeline is unfortunate. Let's see other maintainers' opinions.

  1. We have to encrypt the entire configuration file with something like sops if we want to use the current implementation as is.
  2. We must write some custom tooling to extend the upstream Falco image to iterate through the YAML and eval all environment variables.

Neither of these are the direction we want to take, but if we have to, so be it.

Out of curiosity: have you ever tried using --option as a workaround? For example:

falco ... --option someopt=$SOMEVAR

N.B. I'm not saying this is a clean solution.

@therealdwright
Copy link
Contributor Author

Let me know if you would like this PR extended to handle the special case scenario similarly to filebeat. I'm happy to update the PR.

Out of curiosity: have you ever tried using --option as a workaround? For example:

I wasn't aware this was an option; I'll take a look tomorrow and give it a try.

@therealdwright
Copy link
Contributor Author

Unfortunately, moving forward with http_output configured as an option did the job of getting the config in but resulted in a new bug which has been raised as #2579

@FedeDP
Copy link
Contributor

FedeDP commented May 29, 2023

I'd move this to 0.36.0 since we have not reach an agreement for this to get merged in time for 0.35.
I am sorry! We will review it asap right after 0.36.0

/milestone 0.36.0

@poiana poiana modified the milestones: 0.35.0, 0.36.0 May 29, 2023
@FedeDP
Copy link
Contributor

FedeDP commented May 30, 2023

/hold

@therealdwright therealdwright force-pushed the falco-config-yaml-env-var-support branch 5 times, most recently from c62db2f to 13ab729 Compare September 5, 2023 11:57
@therealdwright
Copy link
Contributor Author

OK @Andreagit97 @leogr and @FedeDP

This has taken me a bit to get around as I have not thought about or touched this codebase for three months, and each piece of feedback in isolation was reasonable, but there were some conflicts to resolve to appease it all. I think I have struck a good balance here.

  1. Only one format of an environment variable is now supported, being ${ENV_VAR}
  2. YAML values following a $ENV_VAR format will be processed as a regular string
  3. YAML Values following a $${ENV_VAR} format will be processed as a regular string with the first $ stripped
  4. YAML values following a $$ENV_VAR format will be processed as a regular string

I think I have adequately covered the various edge cases in the unit test, as I hope you can appreciate there are a lot of edge cases to check for here. Please re-review and let me know what you think. I would appreciate it if the team could come back to me soon as it's very hard to step away from this project and C++ for so long and be productive.

If you would like me to write some user-facing docs, LMK, I'd be happy to do so.

@therealdwright therealdwright force-pushed the falco-config-yaml-env-var-support branch 2 times, most recently from 33e7c6f to 4f2babc Compare September 5, 2023 12:20
Copy link
Member

@Andreagit97 Andreagit97 left a comment

Choose a reason for hiding this comment

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

Amazing work thank you! left just a minor comment :)

userspace/falco/yaml_helper.h Outdated Show resolved Hide resolved
unit_tests/falco/test_configuration.cpp Outdated Show resolved Hide resolved
@Andreagit97
Copy link
Member

  1. Only one format of an environment variable is now supported, being ${ENV_VAR}
  2. YAML values following a $ENV_VAR format will be processed as a regular string
  3. YAML Values following a $${ENV_VAR} format will be processed as a regular string with the first $ stripped
  4. YAML values following a $$ENV_VAR format will be processed as a regular string

Agree with the design, thanks!

Copy link
Contributor

@FedeDP FedeDP left a comment

Choose a reason for hiding this comment

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

LGTM! Agree with @Andreagit97 comments, then we are good to go!
Thanks for the effort @therealdwright !

@leogr
Copy link
Member

leogr commented Sep 5, 2023

LGTM! Agree with @Andreagit97 comments, then we are good to go!
Thanks for the effort @therealdwright !

+1 for me too! Thank you!

@therealdwright therealdwright force-pushed the falco-config-yaml-env-var-support branch from 4f2babc to 396c53d Compare September 5, 2023 20:24
Copy link
Contributor

@FedeDP FedeDP 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 poiana added the lgtm label Sep 5, 2023
@poiana
Copy link

poiana commented Sep 5, 2023

LGTM label has been added.

Git tree hash: 304a49cfa14a173eb0e94edcbd448caf86039aae

@therealdwright therealdwright force-pushed the falco-config-yaml-env-var-support branch from 396c53d to 53d71a6 Compare September 5, 2023 20:30
In order to allow the user to supply environment variables in standard
ways performed in other applications the get_scalar function has been
extended to support defining an environment variable in the format
`${FOO}`. Environment variables can be escaped via defining as `$${FOO}`.
As this handles some additional complexity, a unit test has been  added
to cover this new functionality

Signed-off-by: Daniel Wright <danielwright@bitgo.com>
@therealdwright therealdwright force-pushed the falco-config-yaml-env-var-support branch from 53d71a6 to 62a664b Compare September 5, 2023 20:31
@therealdwright
Copy link
Contributor Author

For posterity, PR description and commit message body have been updated to reflect the new state of the change.

Copy link
Member

@Andreagit97 Andreagit97 left a comment

Choose a reason for hiding this comment

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

Thanks!

/approve

@poiana
Copy link

poiana commented Sep 6, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Andreagit97, FedeDP, therealdwright

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:

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

@FedeDP
Copy link
Contributor

FedeDP commented Sep 6, 2023

/unhold

@FedeDP
Copy link
Contributor

FedeDP commented Sep 6, 2023

Let's go with this one! Again, thank you very much for your effort 🚀

@poiana poiana merged commit 513f122 into falcosecurity:master Sep 6, 2023
17 checks passed
@therealdwright therealdwright deleted the falco-config-yaml-env-var-support branch September 6, 2023 10:29
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.

Support Environment Variable Values in Configuration File
6 participants