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

Fix bug 6384 by changing the implementation of locating the assets di... #6386

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 49 additions & 17 deletions internal/console/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,27 +130,59 @@ func GetExecutableDirectory() string {
}

// GetDefaultQueryPath - returns the default query path
func GetDefaultQueryPath(queriesPath string) (string, error) {
func GetDefaultQueryPath(path, queriesPath string) (string, error) {
log.Debug().Msg("helpers.GetDefaultQueryPath()")
executableDirPath := GetExecutableDirectory()
queriesDirectory := filepath.Join(executableDirPath, queriesPath)
if _, err := os.Stat(queriesDirectory); os.IsNotExist(err) {
currentWorkDir, err := os.Getwd()
if err != nil {
return "", err
}
idx := strings.Index(currentWorkDir, "kics")
if idx != -1 {
currentWorkDir = currentWorkDir[:strings.LastIndex(currentWorkDir, "kics")] + "kics"
}
queriesDirectory = filepath.Join(currentWorkDir, queriesPath)
if _, err := os.Stat(queriesDirectory); os.IsNotExist(err) {
return "", err
queriesPath, err := GetSubDirPath(path, queriesPath)
if err != nil {
return "", err
}

log.Debug().Msgf("Queries found in %s", queriesPath)
return queriesPath, nil
}

// GetSubDirPath - returns the full path of 'subDir' found by searching it as a sub-directory from 'path' upwards
// if 'path' is empty, take the executable path
func GetSubDirPath(path, subDir string) (string, error) {
var err error
var basePath string
if path == "" {
path = GetExecutableDirectory()
}
basePath = path

subDirPath := filepath.Join(basePath, subDir)
isDir, err := IsPathDir(subDirPath)
for err != nil && !isDir {
parentPath := filepath.Dir(basePath)
if basePath == parentPath {
err = fmt.Errorf("'%s' directory not found as sub-directory anywhere above '%s'", subDir, path)
break
}
basePath = parentPath
subDirPath = filepath.Join(basePath, subDir)
isDir, err = IsPathDir(subDirPath)
}
if err != nil {
return "", err
}
if !isDir {
return "", fmt.Errorf("'%s' path '%s' is not a directory", subDir, subDirPath)
}
return subDirPath, nil
}

log.Debug().Msgf("Queries found in %s", queriesDirectory)
return queriesDirectory, nil
// IsPathDir - is the given path a directory?
func IsPathDir(path string) (bool, error) {
fileInfo, err := os.Stat(path)
if err != nil {
return false, err
}
if fileInfo.IsDir() {
return true, nil
} else {
return false, nil
}
}

// ListReportFormats return a slice with all supported report formats
Expand Down
11 changes: 5 additions & 6 deletions internal/console/helpers/helpers_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package helpers

import (
"fmt"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -184,11 +185,9 @@ func TestHelpers_GenerateReport(t *testing.T) {
}

func TestHelpers_GetDefaultQueryPath(t *testing.T) {
if err := test.ChangeCurrentDir("kics"); err != nil {
t.Fatal(err)
}

cd, err := os.Getwd()
kicsPath := filepath.Dir(filepath.Dir(filepath.Dir(cd)))
fmt.Printf("kicsPath='%s', cd='%s'", kicsPath, cd)
require.NoError(t, err)

tests := []struct {
Expand All @@ -200,7 +199,7 @@ func TestHelpers_GetDefaultQueryPath(t *testing.T) {
{
name: "test_get_default_query_path",
queriesPath: filepath.FromSlash("assets/queries"),
want: filepath.Join(cd, filepath.FromSlash("assets/queries")),
want: filepath.Join(kicsPath, filepath.FromSlash("assets/queries")),
wantErr: false,
},
{
Expand All @@ -213,7 +212,7 @@ func TestHelpers_GetDefaultQueryPath(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetDefaultQueryPath(tt.queriesPath)
got, err := GetDefaultQueryPath(cd, tt.queriesPath)
if (err != nil) != tt.wantErr {
t.Errorf("GetDefaultQueryPath() = %v, wantErr = %v", err, tt.wantErr)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/scan/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (c *Client) GetQueryPath() (provider.ExtractedPath, error) {
}
} else {
log.Debug().Msgf("Looking for queries in executable path and in current work directory")
defaultQueryPath, errDefaultQueryPath := consoleHelpers.GetDefaultQueryPath(c.ScanParams.QueriesPath[0])
defaultQueryPath, errDefaultQueryPath := consoleHelpers.GetDefaultQueryPath("", c.ScanParams.QueriesPath[0])
if errDefaultQueryPath != nil {
return extPath, errors.Wrap(errDefaultQueryPath, "unable to find queries")
}
Expand Down