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: add support for yaml.unmarshal builtin #100

Merged
merged 1 commit into from
Dec 10, 2021

Conversation

aron
Copy link
Contributor

@aron aron commented Dec 9, 2021

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 the yaml.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 and lint commands on the source as well as opa fmt on the rego fixtures.

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>
@aron aron force-pushed the feat/yaml-builtin branch from 04e1e5b to 21ee8f4 Compare December 9, 2021 21:07
Copy link
Contributor

@srenatus srenatus left a 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 srenatus merged commit f7b5c2a into open-policy-agent:main Dec 10, 2021
@aron aron deleted the feat/yaml-builtin branch December 10, 2021 15:43
@aron
Copy link
Contributor Author

aron commented Dec 10, 2021

@srenatus thanks!

I'll pick up the bits around using OPA's test-cases to assert conformance (next week, unless someone beats me to it).

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.

@srenatus
Copy link
Contributor

Here's the gist: You'd add the yaml.unmarshal builtin to npm-opa-wasm’s capabilities.json (this block from OPA's capabilities.json), and see if any of the cases you’d end up running as a consequence of that (some are defined here) need to be disabled here.

aron added a commit to snyk/cli that referenced this pull request Jan 4, 2022
#### 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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants