Skip to content

Commit

Permalink
Merge pull request #6670 from tomk-orca/6306
Browse files Browse the repository at this point in the history
feat(cli): add new flag --max-file-size to control the max file size
  • Loading branch information
pereiramarco011 authored Dec 20, 2023
2 parents 9aa356d + 8f9a032 commit d6a8be2
Show file tree
Hide file tree
Showing 19 changed files with 111,704 additions and 18 deletions.
1 change: 1 addition & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Flags:
example: 'e69890e6-fce5-461d-98ad-cb98318dfc96,4728cd65-a20c-49da-8b31-9c08b423e4db'
--input-data string path to query input data files
-b, --libraries-path string path to directory with libraries (default "./assets/libraries")
--max-file-size int max file size permitted for scanning, in MB (default 5)
--minimal-ui simplified version of CLI output
--no-progress hides the progress bar
--output-name string name used on report creations (default "results")
Expand Down
26 changes: 26 additions & 0 deletions e2e/fixtures/E2E_CLI_076_RESULT.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"kics_version": "development",
"files_scanned": 0,
"lines_scanned": 0,
"files_parsed": 0,
"lines_parsed": 0,
"lines_ignored": 0,
"files_failed_to_scan": 0,
"queries_total": 0,
"queries_failed_to_execute": 0,
"queries_failed_to_compute_similarity_id": 0,
"scan_id": "console",
"severity_counters": {
"HIGH": 0,
"INFO": 0,
"LOW": 0,
"MEDIUM": 0,
"TRACE": 0
},
"total_counter": 0,
"total_bom_resources": 0,
"start": "2023-12-19T15:48:05.8014232Z",
"end": "2023-12-19T15:48:05.9361693Z",
"paths": [],
"queries": []
}
1 change: 1 addition & 0 deletions e2e/fixtures/assets/scan_help
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Flags:
example: 'e69890e6-fce5-461d-98ad-cb98318dfc96,4728cd65-a20c-49da-8b31-9c08b423e4db'
--input-data string path to query input data files
-b, --libraries-path string path to directory with libraries (default "./assets/libraries")
--max-file-size int max file size permitted for scanning, in MB (default 5)
--minimal-ui simplified version of CLI output
--no-progress hides the progress bar
--output-name string name used on report creations (default "results")
Expand Down
27 changes: 27 additions & 0 deletions e2e/testcases/e2e-cli-076_max_file_size.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package testcases

// E2E-CLI-076 - KICS scan
// should perform a scan without detecting anything since no files are scanned because of max file size
func init() { //nolint
testSample := TestCase{
Name: "should perform a scan without detecting anything since no files are scanned because of max file size [E2E-CLI-076]",
Args: args{
Args: []cmdArgs{
[]string{"scan", "-o", "/path/e2e/output",
"--output-name", "E2E_CLI_076_RESULT",
"-p", "\"/path/test/fixtures/max_file_size\"",
"--max-file-size", "3",
},
},
ExpectedResult: []ResultsValidation{
{
ResultsFile: "E2E_CLI_076_RESULT",
ResultsFormats: []string{"json"},
},
},
},
WantStatus: []int{00},
}

Tests = append(Tests, testSample)
}
6 changes: 4 additions & 2 deletions internal/console/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ func analyze() error {

func getAnalyzeParameters() *analyzer.Parameters {
analyzeParams := analyzer.Parameters{
Path: flags.GetMultiStrFlag(flags.AnalyzePath),
Results: flags.GetStrFlag(flags.AnalyzeResults),
Path: flags.GetMultiStrFlag(flags.AnalyzePath),
Results: flags.GetStrFlag(flags.AnalyzeResults),
MaxFileSize: flags.GetIntFlag(flags.MaxFileSizeFlag),
}

return &analyzeParams
Expand All @@ -90,6 +91,7 @@ func executeAnalyze(analyzeParams *analyzer.Parameters) error {
Exc: []string{""},
ExcludeGitIgnore: false,
GitIgnoreFileName: "",
MaxFileSize: analyzeParams.MaxFileSize,
}

analyzedPaths, err := analyzer.Analyze(analyzerStruct)
Expand Down
6 changes: 6 additions & 0 deletions internal/console/assets/scan-flags.json
Original file line number Diff line number Diff line change
Expand Up @@ -207,5 +207,11 @@
"shorthandFlag": "",
"defaultValue": "false",
"usage": "resolve the file reference, on OpenAPI files"
},
"max-file-size": {
"flagType": "int",
"shorthandFlag": "",
"defaultValue": "5",
"usage": "max file size permitted for scanning, in MB"
}
}
1 change: 1 addition & 0 deletions internal/console/flags/scan_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ const (
SecretsRegexesPathFlag = "secrets-regexes-path" //nolint:gosec
ExcludeGitIgnore = "exclude-gitignore"
OpenAPIReferencesFlag = "enable-openapi-refs"
MaxFileSizeFlag = "max-file-size"
)
2 changes: 2 additions & 0 deletions internal/console/pre_scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ func (console *console) preScan() {
cpu := consoleHelpers.GetNumCPU()
log.Info().Msgf("CPU: %.1f", cpu)

log.Info().Msgf("Max file size permitted for scanning: %d MB", flags.GetIntFlag(flags.MaxFileSizeFlag))

noProgress := flags.GetBoolFlag(flags.NoProgressFlag)
if strings.EqualFold(flags.GetStrFlag(flags.LogLevelFlag), "debug") {
noProgress = true
Expand Down
1 change: 1 addition & 0 deletions internal/console/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func getScanParameters(changedDefaultQueryPath, changedDefaultLibrariesPath bool
BillOfMaterials: flags.GetBoolFlag(flags.BomFlag),
ExcludeGitIgnore: flags.GetBoolFlag(flags.ExcludeGitIgnore),
OpenAPIResolveReferences: flags.GetBoolFlag(flags.OpenAPIReferencesFlag),
MaxFileSizeFlag: flags.GetIntFlag(flags.MaxFileSizeFlag),
}

return &scanParams
Expand Down
48 changes: 35 additions & 13 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,13 @@ const (
dockerfile = "dockerfile"
crossplane = "crossplane"
knative = "knative"
sizeMb = 1048576
)

type Parameters struct {
Results string
Path []string
Results string
Path []string
MaxFileSize int
}

// regexSlice is a struct to contain a slice of regex
Expand All @@ -149,6 +151,7 @@ type Analyzer struct {
Exc []string
GitIgnoreFileName string
ExcludeGitIgnore bool
MaxFileSize int
}

// types is a map that contains the regex by type
Expand Down Expand Up @@ -301,10 +304,7 @@ func Analyze(a *Analyzer) (model.AnalyzedPaths, error) {

ext := utils.GetExtension(path)

if (hasGitIgnoreFile && gitIgnore.MatchesPath(path)) || isDeadSymlink(path) {
ignoreFiles = append(ignoreFiles, path)
a.Exc = append(a.Exc, path)
}
ignoreFiles = a.checkIgnore(info.Size(), hasGitIgnoreFile, gitIgnore, path, ignoreFiles)

if isConfigFile(path, defaultConfigFiles) {
projectConfigFiles = append(projectConfigFiles, path)
Expand All @@ -324,13 +324,7 @@ func Analyze(a *Analyzer) (model.AnalyzedPaths, error) {
// unwanted is the channel shared by the workers that contains the unwanted files that the parser will ignore
unwanted := make(chan string, len(files))

for i := range a.Types {
a.Types[i] = strings.ToLower(a.Types[i])
}

for i := range a.ExcludeTypes {
a.ExcludeTypes[i] = strings.ToLower(a.ExcludeTypes[i])
}
a.Types, a.ExcludeTypes = typeLower(a.Types, a.ExcludeTypes)

// Start the workers
for _, file := range files {
Expand Down Expand Up @@ -725,3 +719,31 @@ func (a *analyzerInfo) isAvailableType(typeName string) bool {
// no valid behavior detected
return false
}

func (a *Analyzer) checkIgnore(fileSize int64, hasGitIgnoreFile bool,
gitIgnore *ignore.GitIgnore,
path string, ignoreFiles []string) []string {
exceededFileSize := a.MaxFileSize >= 0 && float64(fileSize)/float64(sizeMb) > float64(a.MaxFileSize)

if (hasGitIgnoreFile && gitIgnore.MatchesPath(path)) || isDeadSymlink(path) || exceededFileSize {
ignoreFiles = append(ignoreFiles, path)
a.Exc = append(a.Exc, path)

if exceededFileSize {
log.Error().Msgf("file %s exceeds maximum file size of %d Mb", path, a.MaxFileSize)
}
}
return ignoreFiles
}

func typeLower(types, exclTypes []string) (typesRes, exclTypesRes []string) {
for i := range types {
types[i] = strings.ToLower(types[i])
}

for i := range exclTypes {
exclTypes[i] = strings.ToLower(exclTypes[i])
}

return types, exclTypes
}
Loading

0 comments on commit d6a8be2

Please sign in to comment.