feat(tests): added verifications for all the fields regarding positive_expected_result and actual results#7987
Open
cx-ricardo-jesus wants to merge 23 commits intomasterfrom
Open
feat(tests): added verifications for all the fields regarding positive_expected_result and actual results#7987cx-ricardo-jesus wants to merge 23 commits intomasterfrom
cx-ricardo-jesus wants to merge 23 commits intomasterfrom
Conversation
Contributor
…tive_expected_result.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.








Closes #
Reason for Proposed Changes
Proposed Changes
Using scripts that I will detail below, I filled all the positive_expected_results from each query.
Also, changed the
queries_test.go, more specifically thevalidateQueryResultFieldswhich is the function that check's if the values from the results and the positve_expected_result from each query are equal. Basically, in this function I added verification's for the fields resourceType, resourceName, searchKey, searchValue, keyExpectedValue, keyActualValue and issueType. Also changed the functionvulnerabilityCompare, to order in a runtime context, the results taking into account the fields from the expected results and actual results in the following order: fileName, line, searchKey, searchValue, resourceType, resourceName, queryName, keyExpectedResult, keyActualValue. This is to be sure that the actual results nd the expected results is being compared in the exact same order which did not happen before this change, since that if a field returns several results for the same line in the same file, it had the chance of returning an error stating that the expected and actual results are not equal even though that isn't true.The scripts that were created to automate the generation and population of the positive_expectedresults.json files for each KICS query, adding fields such as queryName, severity, line, filename, resourceType, resourceName, searchKey, searchValue, expectedValue, actualValue and issueType. Below I will explain each one of the scripts:
The
models.py, defines the data models used across all the scripts. Basically, theTestListstructure has a list ofQueryInfostructures. TheseQueryInfostructures has the fieldstest_path,results_file_path,id,payload_path,results_info,return_codeandis_bomthat will be filled by thegenerate.pyand will be used by therunner.pyto run KICS scan comands using these fields for each query. The ResultInfo, is used to store the results of each KICS scan.The
main.pyorchestrates the full pipeline of execution, the first step is to build the test list, using the models explained above and run the scan and populate the resultsInfo for each query with the results of every single scan. This will be made by using the functionrun_allinside the `runner.py.This
run_allfunction will get the testList of all the queries using thebuild_test_listfunction from thegenerate.pyscript. After getting that testList with the QueryInfo fields with the information necessary to run the KICS scan commmands, it will run the scan for every single query sequentially.That helper function
build_test_list(), defined inside thegenerate.pyfile, walks through theassets/queriesand extract the necessary information to be included in the QueryInfo, to be later used in the runner.py script as mentioned above, to run the scan commands.After that the
main.py, will use the functionwrite_positive_expected_resultsfrom thewrite_expected_results.pyfile that basically iterates through the entireTestListstructure and writes the information in each query respectivepositive_expected_result.jsonfile inside thetest/directory.After running this, there are some queries that do not changed it's positive_expected__result.json file, because, there are no results, because the previous scripts only ran the scan command using the entire test directory at once. For those scenarios I created the script
run_skipped.pythat basically scans once per individual positive test file. In themain.pythe information about all of those skipped queries, is made by using thewrite_skipped_queries_reportfrom thewrite_expected_results.pyfile. The information of the skipped queries will be written in a json file.After that the main.py will run the
run_skipped.pyscript to open that skipped queries reports written as explained above. In each one of those skipped queries, it will use theprocess_skipped_queryfunction to run each positive file test individually instead of running the entire test directory at once. For each positive test it will run the individual scan using therun_individual_scanhelper function that just runs the KICS scan command and returns the path for th results file and the returncode from the scan. After that it will run theparse_results_from_filefunction that parse that results file in json format and return a list ofResultInfostructure to be usedAfter that there were some queries listed below that still got errors in the unit tests. After that I had to manually solve each case:

The script


add_issue_tyoe.pywas created after realizing the issueType field had been omitted from the original scripts. It parses each query's .rego file to extract the issueType values, and adds only the issueType field to each existing query in the positive_expected_result.json files without modifying any other field. After running this script 40 queries had theyir issueType values incorrectly assigned due to the limitations of the static rego analysis (dynamic issueType resolution, among other scenarios). These queries, listed below, were manually corrected one by one.I submit this contribution under the Apache-2.0 license.