forked from Checkmarx/kics
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(E2E): Added E2E Test to validate CSV reports (Checkmarx#5065)
* added E2E tests to validate CSV Results * lint fixes & new error handling * update file close - csv * fix - Errors unhandled * changing csvSchema name
- Loading branch information
Showing
5 changed files
with
293 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"allOf": [ | ||
{ | ||
"$ref": "result-csv.json" | ||
}, | ||
{ | ||
"type": "array", | ||
"minItems": 1, | ||
"items": { | ||
"type": "object", | ||
"required": [ | ||
"CISDescriptionIDFormatted", | ||
"CISDescriptionTitle", | ||
"CISDescriptionTextFormatted" | ||
], | ||
"properties": { | ||
"CISDescriptionIDFormatted": { | ||
"type": "string", | ||
"minLength": 1 | ||
}, | ||
"CISDescriptionTitle": { | ||
"type": "string", | ||
"minLength": 1 | ||
}, | ||
"CISDescriptionTextFormatted": { | ||
"type": "string", | ||
"minLength": 1 | ||
} | ||
} | ||
} | ||
} | ||
] | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
{ | ||
"type": "array", | ||
"minItems": 1, | ||
"definitions": { | ||
"query_id_pattern": { | ||
"type": "string", | ||
"minLength": 1, | ||
"pattern": "^[a-f0-9]{8}-[a-f0-9]{4}-4{1}[a-f0-9]{3}-[89ab]{1}[a-f0-9]{3}-[a-f0-9]{12}$" | ||
}, | ||
"similarity_id_pattern": { | ||
"type": "string", | ||
"minLength": 1, | ||
"pattern": "^[A-Fa-f0-9]{64}$" | ||
}, | ||
"description_id_pattern": { | ||
"type": "string", | ||
"minLength": 1, | ||
"pattern": "^[a-f0-9]{8}$" | ||
}, | ||
"file_name_pattern": { | ||
"type": "string", | ||
"oneOf": [ | ||
{ | ||
"pattern": "^(.)*(:)*(http:|https:|www\\.)(.)+$" | ||
}, | ||
{ | ||
"pattern": "^([\\w\\-. ]+(\\\\|\\/))*([\\w\\-. ]+(\\\\|\\/).(.)*)$" | ||
} | ||
] | ||
} | ||
}, | ||
"items": { | ||
"type": "object", | ||
"additionalProperties": false, | ||
"required": [ | ||
"QueryName", | ||
"QueryID", | ||
"QueryURI", | ||
"Severity", | ||
"Platform", | ||
"CloudProvider", | ||
"Category", | ||
"DescriptionID", | ||
"Description", | ||
"CISDescriptionIDFormatted", | ||
"CISDescriptionTitle", | ||
"CISDescriptionTextFormatted", | ||
"FileName", | ||
"SimilarityID", | ||
"Line", | ||
"IssueType", | ||
"SearchKey", | ||
"SearchLine", | ||
"SearchValue", | ||
"ExpectedValue", | ||
"ActualValue" | ||
], | ||
"properties": { | ||
"QueryName": { | ||
"type": "string", | ||
"minLength": 1 | ||
}, | ||
"QueryID": { | ||
"$ref": "#/definitions/query_id_pattern" | ||
}, | ||
"QueryURI": { | ||
"type": "string", | ||
"format": "uri" | ||
}, | ||
"Severity": { | ||
"type": "string", | ||
"enum": [ | ||
"HIGH", | ||
"MEDIUM", | ||
"LOW", | ||
"INFO" | ||
] | ||
}, | ||
"Platform": { | ||
"type": "string", | ||
"enum": [ | ||
"Ansible", | ||
"AzureResourceManager", | ||
"Buildah", | ||
"CloudFormation", | ||
"Common", | ||
"Dockerfile", | ||
"DockerCompose", | ||
"GRPC", | ||
"GoogleDeploymentManager", | ||
"Kubernetes", | ||
"OpenAPI", | ||
"Terraform" | ||
] | ||
}, | ||
"CloudProvider": { | ||
"type": "string", | ||
"minLength": 1 | ||
}, | ||
"Category": { | ||
"type": "string", | ||
"minLength": 1 | ||
}, | ||
"DescriptionID": { | ||
"$ref": "#/definitions/description_id_pattern" | ||
}, | ||
"Description": { | ||
"type": "string", | ||
"minLength": 1 | ||
}, | ||
"CISDescriptionIDFormatted": { | ||
"type": "string" | ||
}, | ||
"CISDescriptionTitle": { | ||
"type": "string" | ||
}, | ||
"CISDescriptionTextFormatted": { | ||
"type": "string" | ||
}, | ||
"FileName": { | ||
"$ref": "#/definitions/file_name_pattern" | ||
}, | ||
"SimilarityID": { | ||
"$ref": "#/definitions/similarity_id_pattern" | ||
}, | ||
"Line": { | ||
"type": "integer", | ||
"minimum": 0 | ||
}, | ||
"IssueType": { | ||
"type": "string" | ||
}, | ||
"SearchKey": { | ||
"type": "string" | ||
}, | ||
"SearchLine": { | ||
"type": "integer", | ||
"minimum": 0 | ||
}, | ||
"SearchValue": { | ||
"type": "string" | ||
}, | ||
"ExpectedValue": { | ||
"type": "string", | ||
"minLength": 1 | ||
}, | ||
"ActualValue": { | ||
"type": "string", | ||
"minLength": 1 | ||
} | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package utils | ||
|
||
import ( | ||
"encoding/csv" | ||
"encoding/json" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// CSVToJSON - converts CSV to JSON Structure | ||
func CSVToJSON(t *testing.T, filename string) []byte { | ||
cwd, _ := os.Getwd() | ||
filePath := filepath.Join("output", filename) | ||
fullPath := filepath.Join(cwd, filePath) | ||
|
||
csvFile, err := os.Open(fullPath) | ||
require.NoError(t, err, "Error reading file: %s", fullPath) | ||
|
||
reader := csv.NewReader(csvFile) | ||
reader.FieldsPerRecord = -1 | ||
csvData, err := reader.ReadAll() | ||
require.NoError(t, err, "Error reading CSV file: %s", fullPath) | ||
|
||
err = csvFile.Close() | ||
require.NoError(t, err, "Error when closing file: %s", fullPath) | ||
|
||
var csvStruct csvSchema | ||
var csvItems []csvSchema | ||
|
||
for _, row := range csvData[1:] { | ||
line, lineErr := strconv.Atoi(row[14]) | ||
require.NoError(t, lineErr, "Error when converting CSV: %s", fullPath) | ||
searchLine, searchErr := strconv.Atoi(row[17]) | ||
require.NoError(t, searchErr, "Error when converting CSV: %s", fullPath) | ||
|
||
csvStruct.QueryName = row[0] | ||
csvStruct.QueryID = row[1] | ||
csvStruct.QueryURI = row[2] | ||
csvStruct.Severity = row[3] | ||
csvStruct.Platform = row[4] | ||
csvStruct.CloudProvider = row[5] | ||
csvStruct.Category = row[6] | ||
csvStruct.DescriptionID = row[7] | ||
csvStruct.Description = row[8] | ||
csvStruct.CISDescriptionIDFormatted = row[9] | ||
csvStruct.CISDescriptionTitle = row[10] | ||
csvStruct.CISDescriptionTextFormatted = row[11] | ||
csvStruct.FileName = row[12] | ||
csvStruct.SimilarityID = row[13] | ||
csvStruct.Line = line | ||
csvStruct.IssueType = row[15] | ||
csvStruct.SearchKey = row[16] | ||
csvStruct.SearchLine = searchLine | ||
csvStruct.SearchValue = row[18] | ||
csvStruct.ExpectedValue = row[19] | ||
csvStruct.ActualValue = row[20] | ||
csvItems = append(csvItems, csvStruct) | ||
} | ||
|
||
jsondata, err := json.Marshal(csvItems) | ||
require.NoError(t, err, "Error marshaling file: %s", fullPath) | ||
|
||
return jsondata | ||
} | ||
|
||
type csvSchema struct { | ||
QueryName string | ||
QueryID string | ||
QueryURI string | ||
Severity string | ||
Platform string | ||
CloudProvider string | ||
Category string | ||
DescriptionID string | ||
Description string | ||
CISDescriptionIDFormatted string | ||
CISDescriptionTitle string | ||
CISDescriptionTextFormatted string | ||
FileName string | ||
SimilarityID string | ||
Line int | ||
IssueType string | ||
SearchKey string | ||
SearchLine int | ||
SearchValue string | ||
ExpectedValue string | ||
ActualValue string | ||
} |