Skip to content

Commit

Permalink
Fixed source to get libraries path based on queries path flag #1964 (#…
Browse files Browse the repository at this point in the history
…1967)

* Corrected source to get libraries path based on queries path flag

* Normalized path flag to work on Linux and Windows
  • Loading branch information
felipe-avelar authored Feb 5, 2021
1 parent ac5830a commit 1f8b8fe
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
2 changes: 1 addition & 1 deletion internal/console/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func scan() error {
scanStartTime := time.Now()

querySource := &query.FilesystemSource{
Source: queryPath,
Source: filepath.FromSlash(queryPath),
}

t := &tracker.CITracker{}
Expand Down
22 changes: 21 additions & 1 deletion pkg/engine/inspector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package engine
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"
Expand Down Expand Up @@ -277,7 +278,7 @@ func TestNewInspector(t *testing.T) { // nolint
require.NoError(t, err)

track := &tracker.CITracker{}
sources := &query.FilesystemSource{
sources := &mockSource{
Source: filepath.FromSlash("./test/fixtures/all_auth_users_get_read_access"),
}
vbs := DefaultVulnerabilityBuilder
Expand Down Expand Up @@ -344,3 +345,22 @@ func TestNewInspector(t *testing.T) { // nolint
})
}
}

type mockSource struct {
Source string
}

func (m *mockSource) GetQueries() ([]model.QueryMetadata, error) {
sources := &query.FilesystemSource{
Source: m.Source,
}
return sources.GetQueries()
}
func (m *mockSource) GetGenericQuery(platform string) (string, error) {
currentWorkdir, _ := os.Getwd()

pathToLib := query.GetPathToLibrary(platform, currentWorkdir)
content, err := ioutil.ReadFile(filepath.Clean(pathToLib))

return string(content), err
}
33 changes: 17 additions & 16 deletions pkg/engine/query/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,41 @@ const (
MetadataFileName = "metadata.json"
// LibraryFileName The default library file name
LibraryFileName = "library.rego"
// LibrariesBasePath the path to rego libraries
LibrariesBasePath = "./assets/libraries/"
// LibrariesDefaultBasePath the path to rego libraries
LibrariesDefaultBasePath = "./assets/libraries/"
)

// GetPathToLibrary returns the libraries path for a given platform
func GetPathToLibrary(platform, relativeBasePath string) string {
libraryPath := filepath.Join(relativeBasePath, LibrariesBasePath)
var libraryPath string
if strings.LastIndex(relativeBasePath, filepath.FromSlash("/queries")) > -1 {
libraryPath = relativeBasePath[:strings.LastIndex(relativeBasePath, filepath.FromSlash("/queries"))] + filepath.FromSlash("/libraries")
} else {
libraryPath = filepath.Join(relativeBasePath, LibrariesDefaultBasePath)
}

libraryFilePath := filepath.FromSlash(libraryPath + "/common/" + LibraryFileName)

if strings.Contains(strings.ToUpper(platform), strings.ToUpper("ansible")) {
return filepath.FromSlash(libraryPath + "/ansible/" + LibraryFileName)
libraryFilePath = filepath.FromSlash(libraryPath + "/ansible/" + LibraryFileName)
} else if strings.Contains(strings.ToUpper(platform), strings.ToUpper("cloudFormation")) {
return filepath.FromSlash(libraryPath + "/cloudformation/" + LibraryFileName)
libraryFilePath = filepath.FromSlash(libraryPath + "/cloudformation/" + LibraryFileName)
} else if strings.Contains(strings.ToUpper(platform), strings.ToUpper("dockerfile")) {
return filepath.FromSlash(libraryPath + "/dockerfile/" + LibraryFileName)
libraryFilePath = filepath.FromSlash(libraryPath + "/dockerfile/" + LibraryFileName)
} else if strings.Contains(strings.ToUpper(platform), strings.ToUpper("k8s")) {
return filepath.FromSlash(libraryPath + "/k8s/" + LibraryFileName)
libraryFilePath = filepath.FromSlash(libraryPath + "/k8s/" + LibraryFileName)
} else if strings.Contains(strings.ToUpper(platform), strings.ToUpper("terraform")) {
return filepath.FromSlash(libraryPath + "/terraform/" + LibraryFileName)
libraryFilePath = filepath.FromSlash(libraryPath + "/terraform/" + LibraryFileName)
}

return filepath.FromSlash(libraryPath + "/common/" + LibraryFileName)
return libraryFilePath
}

// GetGenericQuery returns the library.rego for the platform passed in the argument
func (s *FilesystemSource) GetGenericQuery(platform string) (string, error) {
currentWorkdir, err := os.Getwd()
pathToLib := GetPathToLibrary(platform, s.Source)

if err != nil {
log.Err(err)
}

pathToLib := GetPathToLibrary(platform, currentWorkdir)
content, err := ioutil.ReadFile(filepath.Clean(pathToLib))

if err != nil {
log.Err(err)
}
Expand Down

0 comments on commit 1f8b8fe

Please sign in to comment.