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

chore: Added tests for IaC JSON test output #3492

Merged
merged 1 commit into from
Aug 1, 2022

Conversation

ofekatr
Copy link
Contributor

@ofekatr ofekatr commented Jul 26, 2022

What does this PR do?

  • Separates the different output tests to different files.
  • Adds acceptance tests for the JSON output format of the snyk iac test command.

Where should the reviewer start?

test/jest/acceptance/iac/output/json.spec.ts

Any background context you want to provide?

Notes for the reviewer

  • This is a sensitive PR, and we would like to 100% ensure the JSON output schema is accurate. Please take your time, make a cup of coffee, and go through it cautiously ☕

@ofekatr ofekatr force-pushed the chore/add-tests-for-test-json-output branch from 2fc9776 to 4e82244 Compare July 26, 2022 14:59
@ofekatr ofekatr requested a review from ipapast July 26, 2022 15:19
@ofekatr ofekatr marked this pull request as ready for review July 26, 2022 15:19
@ofekatr ofekatr requested review from a team as code owners July 26, 2022 15:19
@ofekatr ofekatr force-pushed the chore/add-tests-for-test-json-output branch 3 times, most recently from a26b8f5 to 758d996 Compare July 26, 2022 15:48
test/jest/acceptance/iac/output/json.spec.ts Outdated Show resolved Hide resolved
@ofekatr ofekatr force-pushed the chore/add-tests-for-test-json-output branch 12 times, most recently from 614a180 to 0d999b6 Compare July 27, 2022 08:07
Copy link
Contributor

@ipapast ipapast left a comment

Choose a reason for hiding this comment

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

Sorry it took me some time to get to this! I left some comments. I tried a few files and could not get some of the fields in any of my example runs. which files did you use? It's ok as the fields are not required but I'm just wondering which cases they cover.

Should we also cover the --experimental run with a similar test as we are already on it and we are building that too?

Comment on lines +37 to +38
"projectId": {
"type": "string"
Copy link
Contributor

Choose a reason for hiding this comment

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

I can't see this on any of my example runs :(

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This value should be populated when Share Results is used to create a project on the platform.
The value represents the public ID of the project that was created.

Comment on lines +43 to +44
"gitRemoteUrl": {
"type": "string"
Copy link
Contributor

Choose a reason for hiding this comment

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

can't find this either

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 was unable to produce results with this value, but the values is set here.
@YairZ101 should have more context on this.

Copy link
Contributor

Choose a reason for hiding this comment

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

this value is used in share results when we need to save the git remote url of the git repo that we are scanning.
the main use case is for contributing developers.

"title": {
"type": "string"
},
"description": {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't find this in any of my example runs. it's ok as it's not requred, I'd be interested to see in which policies it's getting generated.

Copy link
Contributor Author

@ofekatr ofekatr Jul 31, 2022

Choose a reason for hiding this comment

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

Scanning snyk-labs/infrastructure-as-code-goof, description was usually not set, but was sometimes set as an empty string.
Doing a search in snyk/cloud-config-opa-policies, you can see that some policies have the top-level description set as "", e.g., SNYK-CC-AWS-50.

It seems like this value is not being used since it's empty, but it exists in the output none the less.

Comment on lines +147 to +131
"type": {
"type": "string"
Copy link
Contributor

Choose a reason for hiding this comment

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

type does not exist in all policies. but I think it's fine here as you don't have it required 👍 just mentioning it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just ran the following command in snyk/cli/test/fixtures/iac/cloudformation:

snyk iac test ./test/fixtures/iac/cloudformation --json | jq '[.[].infrastructureAsCodeIssues[] | keys | select(contains(["type"]))] | length'

And got 2 as the output.

Upon closer examination, when scanning snyk/cli/test/fixtures/iac/cloudformation/aurora-valid.yml:

snyk iac test --json test/fixtures/iac/cloudformation/aurora-valid.yml | jq '[.infrastructureAsCodeIssues[] | .type]'

I got the following:

  null,
  null,
  "terraform",
  null,
  null,
  null,
  null,
  null,
  "terraform",
  null
]

Comment on lines +153 to +138
"policyEngineType": {
"type": "string"
},
Copy link
Contributor

Choose a reason for hiding this comment

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

this does not exist in all policies either. but like before, it's ok as it's not required. 👍

Copy link
Contributor Author

@ofekatr ofekatr Jul 31, 2022

Choose a reason for hiding this comment

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

Just ran the following command in snyk/cli/test/fixtures/iac/cloudformation:

snyk iac test ./test/fixtures/iac/cloudformation --json | jq '[.[].infrastructureAsCodeIssues[] | keys | select(contains(["policyEngineType"]))] | length'

And got 2 as the output.

Upon closer examination, when scanning snyk/cli/test/fixtures/iac/cloudformation/aurora-valid.yml:

snyk iac test --json test/fixtures/iac/cloudformation/aurora-valid.yml | jq '[.infrastructureAsCodeIssues[] | .policyEngineType]'

I got the following:

  null,
  null,
  "opa",
  null,
  null,
  null,
  null,
  null,
  "opa",
  null
]

Comment on lines 205 to 207
"name": {
"type": "string"
},
Copy link
Contributor

Choose a reason for hiding this comment

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

can't find this in any of my example runs either

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 88d276224

Comment on lines 208 to 213
"from": {
"type": "array",
"items": {
"type": "string"
}
},
Copy link
Contributor

Choose a reason for hiding this comment

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

can't find this in any of my example runs either

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 88d276224

Comment on lines 63 to 75
"ignoreSettings": {
"type": "object",
"properties": {
"adminOnly": {
"type": "boolean"
},
"reasonRequired": {
"type": "boolean"
},
"disregardFilesystemIgnores": {
"type": "boolean"
}
},
Copy link
Contributor

Choose a reason for hiding this comment

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

this ignoreSettings object is null on my local example run.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Having looked at the code where this value is set, it is hard-coded to always be null.

Fixed in 88d276224

Copy link
Contributor Author

Choose a reason for hiding this comment

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

On a separate note, it looks like we also hard-code policy to be an empty string, which is not changed by any of the flows we currently have, including Share Results. I added a maxLength: 0 requirement for it.

@@ -0,0 +1,200 @@
import * as fs from 'fs';
import * as pathLib from 'path';
import { matchers } from 'jest-json-schema';
Copy link
Contributor

Choose a reason for hiding this comment

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

oooh alright. so in the end you didn't use ajv to compile/validate the json when we build it, but instead you used this package which imports ajv internally, and you run it in the test. nice

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Going through this article, it became apparent that in order to integrate AJV with Jest, some global test suite configuration would need to be updated, which included expanding expect to include the needed custom matchers, and edit the global types for Jest.
These changes were all provided by this third-party package, jest-json-schema

@ofekatr ofekatr force-pushed the chore/add-tests-for-test-json-output branch from 0d999b6 to 88d2762 Compare July 31, 2022 09:19
@ofekatr ofekatr force-pushed the chore/add-tests-for-test-json-output branch from 88d2762 to f66ee0d Compare July 31, 2022 09:27
@ofekatr
Copy link
Contributor Author

ofekatr commented Jul 31, 2022

Should we also cover the --experimental run with a similar test as we are already on it and we are building that too?

At the moment we avoided creating any acceptance tests for the experimental test flow.
This is due to the network calls that are required to download resources for these executions.

We do however validate the IaC issue properties and values in the JSON output format in test/jest/unit/lib/iac/test/v2/json.spec.ts.

@ofekatr ofekatr requested a review from ipapast July 31, 2022 09:54
@ofekatr ofekatr merged commit 3016a01 into master Aug 1, 2022
@ofekatr ofekatr deleted the chore/add-tests-for-test-json-output branch August 1, 2022 08:36
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.

4 participants