-
Notifications
You must be signed in to change notification settings - Fork 43
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: add support for yaml.unmarshal builtin #100
Conversation
This uses the "yaml" npm package to provide support for unmarshalling YAML strings within rego via the yaml.unmarshal() function. Test functionality has been included to verify parsing and handling of syntax, metadata, reference and warnings to match the current behavior of the opa tool. Signed-off-by: Aron Carroll <aron.carroll@snyk.io>
04e1e5b
to
21ee8f4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is pretty cool, thanks for contributing. 👏 🎉
I'll pick up the bits around using OPA's test-cases to assert conformance (next week, unless someone beats me to it).
@srenatus thanks!
Are there any docs on how to do this? I'd be happy to take a look as this functionality is something we're keen to start using. |
Here's the gist: You'd add the |
#### What does this PR do? This PR bumps @open-policy-agent/opa-wasm to [1.6.0](https://github.com/open-policy-agent/npm-opa-wasm/releases/tag/1.6.0) to include support for the `yaml.unmarshal()` function in Rego and support for Node 10. This allows the `snyk iac` command to support additional policies that unmarshall YAML content within Terraform configuration. #### Where should the reviewer start? package.json is the primary change here but the implementation of `yaml.unmarshal()` is here: open-policy-agent/npm-opa-wasm#100 This also removes the use of the `mock-fs` package in the file-scanner.spec.ts test as the opa-wasm package will use `console.error` to write out a warning to stderr when loading the wasm fixture and this somehow causes `mock-fs` to throw the following error bringing the test suite down: ``` ENOENT: no such file or directory, lstat '/Users/Aron/Code/snyk/node_modules/@jest/console/node_modules' ``` Rather than debug this weird behavior we've decided to just mock out the function that loads the fixtures directly in the test suite. #### How should this be manually tested? [rules.zip](https://github.com/snyk/snyk/files/7719060/rules.zip) Download the above zip file containing a custom rules bundle and unzip: With the `snyk-iac-rules` tool build a custom bundle: ``` % snyk-iac-rules build ``` Then test the fixture with the latest snyk cli: ``` % snyk-dev iac test --rules=bundle.tar.gz rules/HELLO/fixtures/sg.tf ``` You should see 15 issues found vs. 14 when run with current snyk. #### What are the relevant tickets? [CFG-1271]
This uses the "yaml" npm package to provide support for unmarshalling YAML strings within rego via the
yaml.unmarshal()
function. The implementation itself is straightforward. I've added the src/builtins/yaml.js file as per the strings.js example and exposed theyaml.parse()
function under the"yaml.unmarshal"
key.There's a bit of awkwardness to silence the default warnings emitted by the "yaml" library which required setting a
YAML_SILENCE_WARNINGS
global and then resetting it to avoid conflicts.Choices for YAML parser in the JS ecosystem seem to fall between yaml and js-yaml. Both are actively maintained. I went with
yaml
because it has zero dependencies and in our testing at Snyk on a wide variety of YAML fixtures it has the closest alignment with the ghodss/yaml YAML library used by OPA.Test functionality has been included in the test/opa-yaml-support.test.js file. This verifies parsing of a generic YAML structure. Handling of syntax, metadata, reference and warnings has also been exercised using fixtures from the yaml test suite, these are all ignored.
I've run the
fmt
andlint
commands on the source as well asopa fmt
on the rego fixtures.