Skip to content

Commit

Permalink
fix(inspector): fix timeout secrets inspector (#5419)
Browse files Browse the repository at this point in the history
* fix timeout

Signed-off-by: joaorufi <[email protected]>

* Feat(e2e): adding e2e tests for timeout flag

* Fix(e2e): updating config validation in e2e tests

* Fix(e2e): moving testing configs to configs folder

Co-authored-by: Lucas Mendes <[email protected]>
  • Loading branch information
joaorufi and cxlucas authored Jun 6, 2022
1 parent 7977e7c commit f423529
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 5 deletions.
File renamed without changes.
3 changes: 3 additions & 0 deletions e2e/fixtures/samples/configs/config-dev.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
path: fixtures/samples/positive.dockerfile
queries-path: ../assets/queries
timeout: 12
File renamed without changes.
3 changes: 3 additions & 0 deletions e2e/fixtures/samples/configs/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
path: /path/e2e/fixtures/samples/positive.dockerfile
queries-path: /path/assets/queries
timeout: 12
6 changes: 3 additions & 3 deletions e2e/testcases/e2e-cli-029_scan_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ func init() { //nolint
Name: "should load a config file [E2E-CLI-029]",
Args: args{
Args: []cmdArgs{
[]string{"scan", "--config", "/path/e2e/fixtures/samples/config.json", "--type", "cloudformation"},
[]string{"scan", "--config", "/path/e2e/fixtures/samples/configs/config.json", "--type", "cloudformation"},

[]string{"scan", "--config", "/path/e2e/fixtures/samples/config.json"},
[]string{"scan", "--config", "/path/e2e/fixtures/samples/configs/config.json"},

[]string{"scan", "--config", "/path/e2e/fixtures/samples/config.json", "--silent"},
[]string{"scan", "--config", "/path/e2e/fixtures/samples/configs/config.json", "--silent"},
},
},
WantStatus: []int{50, 0, 126},
Expand Down
27 changes: 27 additions & 0 deletions e2e/testcases/e2e-cli-056_scan_timeout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package testcases

import (
"regexp"
)

// E2E-CLI-056 - Kics scan command with timeout flag
// should stop a query execution when reaching the provided timeout (seconds)
func init() { //nolint
testSample := TestCase{
Name: "should timeout queries when reaching the timeout limit [E2E-CLI-056]",
Args: args{
Args: []cmdArgs{
[]string{"scan", "--config", "/path/e2e/fixtures/samples/configs/config.yaml", "-v"},
[]string{"scan", "-p", "/path/e2e/fixtures/samples/positive.dockerfile", "--timeout", "1", "-v"},
[]string{"scan", "-p", "/path/e2e/fixtures/samples/positive.dockerfile", "--timeout", "0", "-v"},
},
},
WantStatus: []int{50, 50, 126},
Validation: func(outputText string) bool {
matchTimeoutLog, _ := regexp.MatchString("Query execution timeout=(0|1|12)s", outputText)
return matchTimeoutLog
},
}

Tests = append(Tests, testSample)
}
5 changes: 4 additions & 1 deletion e2e/utils/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ func RunCommand(kicsArgs []string, useDocker, useMock bool, kicsDockerImage stri

// KicsDevPathAdapter adapts the path to enable kics locally execution
func KicsDevPathAdapter(path string) string {
if path == "/path/e2e/fixtures/samples/config.json" {
// [e2e-029] and [e2e-056] config tests
if path == "/path/e2e/fixtures/samples/configs/config.json" {
path = strings.Replace(path, "config.json", "config-dev.json", -1)
} else if path == "/path/e2e/fixtures/samples/configs/config.yaml" {
path = strings.Replace(path, "config.yaml", "config-dev.yaml", -1)
}
regex := regexp.MustCompile(`/path/\w+/`)
matches := regex.FindString(path)
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/secrets/inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func NewInspector(

func (c *Inspector) inspectQuery(ctx context.Context, basePaths []string,
files model.FileMetadatas, i int) ([]model.Vulnerability, error) {
timeoutCtx, cancel := context.WithTimeout(ctx, c.queryExecutionTimeout*time.Second)
timeoutCtx, cancel := context.WithTimeout(ctx, c.queryExecutionTimeout)
defer cancel()

cleanFiles := cleanFiles(files)
Expand Down

0 comments on commit f423529

Please sign in to comment.