-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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] Add helpers for escaping special characters in YAML variables #127268
Comments
Pinging @elastic/fleet (Team:Fleet) |
I agree that we need to improve this area. Based on the investigation around GCP credentials, I was thinking about base64 vars just to prevent dangerous escaping or trimming. It might be a stupid idea to disable all escaping and simply passed based input down to agent, which will unbase there. |
I guess the test: 123
{{custom}} or (this one is currently not supported)
the |
to_json
helper for YAML variables
Good feedback here, thanks @nchaulet. I agree, let's explore separate helpers for these. I think |
One interesting development I've noticed in reviewing all the context here is the difference in usage of quotes we have in various integrations today. Let's take for example two of the integrations linked in this issue mentioned as having issues with escaping in the past: vSphere and Cloudflare In vSphere's case, we reported an issue elastic/integrations#3429 around special characters in a So, in the vSphere integration, we're interpolating the
This results in special characters like @ - { :, etc coming through unescaped. This opens us up to various YAML parsing issues and cryptic errors as evidenced in the linked issue. In the Cloudflare integration, we've observed a similar issue elastic/integrations#3388 around interpolation of a password variable containing special characters: elastic/integrations#3388. This is working off of an The difference in Cloudflare's case, however, is that we wrap this value in double quotes. When we interpolate this variable, we have it wrapped in double quotes in an effort to escape special characters, e.g.
This results in a slightly different issue around escaping because double quotes within double quotes must be escaped in YAML. e.g.
See relevant section of the YAML specification here: https://yaml.org/spec/1.2.2/#731-double-quoted-style. Single quotes are probably the best option for the escaping behavior we want, because they
Single quotes do, however, require any nested single quotes to be duplicated as an escaping mechanism, e.g. - my_yaml_property: 'hello''world' So, I think what we can do in Fleet is register an # .yml.hbs file
- my_yaml_property: {{escape_string my_variable }}
# { my_variable: "hello'world" }
# elastic-agent.yml output file
- my_yaml_property: 'hello''world' Integration developers will just need to make certain they don't "double up" on quoting mechanisms when using the There's also been mention of a # .yml.hbs
- my_yaml_property: {{to_json my_variable }}
# { my_variable: { foo: ["bar"] } }
# elastic-agent.yml
- my_yaml_property: {"my_variable":{"foo":["bar"]}} I think the above should satisfy the need for an easier way to generate JSON blocks in YAML templates. @nchaulet do you know of any explicit examples where we need this behavior so I can get some context? |
We've had many issues around interpolating string variables that contain special YAML characters into Agent policy handlebar templates. See:
['*']
#91401@jen-huang suggested in #121934 (comment):
We should explore adding helpers to our template engine to ease package developers and users to avoid issues with special characters in strings. As noted by @nchaulet below, there are different use cases to consider and we should be clear about what we want to support and whether or not these should be solved by a single helper or separate helpers for each use cases.
Use cases
cc @mtojek @jsoriano for any feedback
Implementation
escape_string
Handlebars helper in Fleet's templating implementation that performs as follows'
''
to_json
Handlebars helper that runs the variable throughJSON.stringify
and returns itDocumentation
These helpers should be documented somewhere accessible to integration developers who rely on Fleet's templating engine to generate
elastic-agent.yml
files. We should specifically be sure to capture the caveat that usingescape_string
and wrapping the result in single or double quotes is not supported and will likely cause errors.@kpollich to determine the best location for these docs
The text was updated successfully, but these errors were encountered: