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

[Fleet] Escape YAML string values if necessary #91418

Merged
merged 5 commits into from
Feb 16, 2021

Conversation

skh
Copy link
Contributor

@skh skh commented Feb 15, 2021

Summary

Fixes #91401

How to test this

Have a local registry running with the apm-server package contained in elastic/apm-server#4690 .

Install the Elastic APM integration with the "Add Elastic APM" button and follow the workflow to add the integration to a policy. In that workflow, enable RUM, and install the package by clicking "Save Integration".

The policy should be created without error, and be correct.

Checklist

@skh skh self-assigned this Feb 15, 2021
@skh skh added Feature:Fleet Fleet team's agent central management project Team:Fleet Team label for Observability Data Collection Fleet team v7.12.0 v8.0.0 release_note:skip Skip the PR/issue when compiling release notes labels Feb 15, 2021
const maybeEscapeNumericString = (value: string) => {
return value.length && !isNaN(+value) ? `"${value}"` : value;
const maybeEscapeString = (value: string) => {
return safeDump(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.

@nchaulet This seems to do the right thing, but safeDump() is given only one string here, not a complete object. I wonder if this is how it is meant to be used -- I didn't find any documentation for that use case.

Copy link
Member

Choose a reason for hiding this comment

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

Just tested the safeDump and looks like it had a \n at the end not sure it will work for our usecase

Copy link
Contributor Author

@skh skh Feb 15, 2021

Choose a reason for hiding this comment

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

Have a look at the test case output in 0409c3e , it looks ok to me. I'm happy to change it to a simple "${value}" as well.

Copy link
Member

Choose a reason for hiding this comment

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

I did more test and it's breaking some of our templates where we have loop, yes I think we should implement the escape function ourself here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've changed this to quote all strings containing YAML special characters as listed in https://yaml.org/spec/1.2/spec.html#id2772075

@nchaulet can you share what you're testing with, and which templates are being broken?

@skh skh marked this pull request as ready for review February 15, 2021 16:46
@skh skh requested a review from a team as a code owner February 15, 2021 16:46
@elasticmachine
Copy link
Contributor

Pinging @elastic/fleet (Feature:Fleet)

@skh skh requested a review from nchaulet February 15, 2021 16:47
@skh skh force-pushed the 91401-escape-specialchars-in-yamlstrings branch from dafdf7b to 9d7de5f Compare February 16, 2021 10:21
const yamlSpecialCharsRegex = /[{}\[\],&*?|\-<>=!%@:]/;

// In addition, numeric strings need to be quoted to stay strings.
if ((value.length && !isNaN(+value)) || value.match(yamlSpecialCharsRegex)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could also do yamlSpecialCharsRegex.test(value) since we only want a boolean result; not the matched item. It's also faster. Both are likely Fast Enough ™️ but this seems like a good place to use the more efficient option.

Copy link
Member

@nchaulet nchaulet left a comment

Choose a reason for hiding this comment

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

🚀

@kibanamachine
Copy link
Contributor

💚 Build Succeeded

Metrics [docs]

✅ unchanged

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@skh skh merged commit 58849bc into elastic:master Feb 16, 2021
skh added a commit to skh/kibana that referenced this pull request Feb 16, 2021
* Use js-yaml.safeDump() to escape string values.

* Add unit test.

* Explicitly check for YAML special characters.

* Remove unnecessary imports.

* Use RegExp.prototype.test() for speed.
jen-huang pushed a commit that referenced this pull request Feb 16, 2021
* Use js-yaml.safeDump() to escape string values.

* Add unit test.

* Explicitly check for YAML special characters.

* Remove unnecessary imports.

* Use RegExp.prototype.test() for speed.

Co-authored-by: Kibana Machine <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature:Fleet Fleet team's agent central management project release_note:skip Skip the PR/issue when compiling release notes Team:Fleet Team label for Observability Data Collection Fleet team v7.12.0 v8.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Fleet] Adding integration: List of strings does not allow value ['*']
5 participants