Skip to content

Commit

Permalink
fix - source path for k8s file scan is absolute
Browse files Browse the repository at this point in the history
  • Loading branch information
patilpankaj212 committed May 31, 2021
1 parent eff166f commit 939a19c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 68 deletions.
26 changes: 2 additions & 24 deletions pkg/iac-providers/kubernetes/v1/load-dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func (*K8sV1) getFileType(file string) string {

// LoadIacDir loads all k8s files in the current directory
func (k *K8sV1) LoadIacDir(absRootDir string, nonRecursive bool) (output.AllResourceConfigs, error) {
// set the root directory being scanned
k.absRootDir = absRootDir

allResourcesConfig := make(map[string][]output.ResourceConfig)

Expand All @@ -48,34 +50,10 @@ func (k *K8sV1) LoadIacDir(absRootDir string, nonRecursive bool) (output.AllReso
}

for key := range configData {
// the source path formed for each resources is absolute, which should be relative
resourceConfigs := configData[key]
makeSourcePathRelative(absRootDir, resourceConfigs)

allResourcesConfig[key] = append(allResourcesConfig[key], configData[key]...)
}
}
}

return allResourcesConfig, k.errIacLoadDirs
}

// makeSourcePathRelative modifies the source path of each resource from absolute to relative path
func makeSourcePathRelative(absRootDir string, resourceConfigs []output.ResourceConfig) {
for i := range resourceConfigs {
r := &resourceConfigs[i]
var err error

oldSource := r.Source

// update the source path
r.Source, err = filepath.Rel(absRootDir, r.Source)

// though this error should never occur, but, if occurs for some reason, assign the old value of source back
if err != nil {
r.Source = oldSource
zap.S().Debug("error while getting the relative path for", zap.String("IAC file", oldSource), zap.Error(err))
continue
}
}
}
43 changes: 0 additions & 43 deletions pkg/iac-providers/kubernetes/v1/load-dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,46 +104,3 @@ func TestLoadIacDir(t *testing.T) {
}

}

func TestMakeSourcePathRelative(t *testing.T) {
dir1, dir2 := "Dir1", "Dir2"
sourcePath1 := filepath.Join(dir1, dir2, "filename.yaml")
sourcePath2 := filepath.Join(dir1, "someDir", "test.yaml")

testResourceConfigs := []output.ResourceConfig{
{
Source: sourcePath1,
},
{
Source: sourcePath2,
},
}

type args struct {
absRootDir string
resourceConfigs []output.ResourceConfig
}
tests := []struct {
name string
expectedSourceValues []string
args args
}{
{
name: "test to verify path becomes relative",
expectedSourceValues: []string{filepath.Join(dir2, "filename.yaml"), filepath.Join("someDir", "test.yaml")},
args: args{
absRootDir: dir1,
resourceConfigs: testResourceConfigs,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
makeSourcePathRelative(tt.args.absRootDir, tt.args.resourceConfigs)
updatedSourceValues := []string{tt.args.resourceConfigs[0].Source, tt.args.resourceConfigs[1].Source}
if !utils.IsSliceEqual(tt.expectedSourceValues, updatedSourceValues) {
t.Errorf("expected source values %v, got %v", tt.expectedSourceValues, updatedSourceValues)
}
})
}
}
18 changes: 17 additions & 1 deletion pkg/iac-providers/kubernetes/v1/load-file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package k8sv1

import (
"fmt"
"path/filepath"

"github.com/accurics/terrascan/pkg/utils"

Expand Down Expand Up @@ -42,9 +43,24 @@ func (k *K8sV1) LoadIacFile(absFilePath string) (allResourcesConfig output.AllRe
}

config.Line = doc.StartLine
config.Source = absFilePath
config.Source = k.getSourceRelativePath(absFilePath)

allResourcesConfig[config.Type] = append(allResourcesConfig[config.Type], *config)
}
return allResourcesConfig, nil
}

// getSourceRelativePath fetches the relative path of file being loaded
func (k *K8sV1) getSourceRelativePath(sourceFile string) string {

// rootDir should be empty when file scan was initiated by user
if k.absRootDir == "" {
return filepath.Base(sourceFile)
}
relPath, err := filepath.Rel(k.absRootDir, sourceFile)
if err != nil {
zap.S().Debug("error while getting the relative path for", zap.String("IAC file", sourceFile), zap.Error(err))
return sourceFile
}
return relPath
}
40 changes: 40 additions & 0 deletions pkg/iac-providers/kubernetes/v1/load-file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,43 @@ func TestLoadIacFile(t *testing.T) {
}

}

func Test_getSourceRelativePath(t *testing.T) {
dir1, dir2 := "Dir1", "Dir2"
sourcePath1 := filepath.Join(dir1, dir2, "filename.yaml")

type args struct {
absRootDir string
sourcePath string
}
tests := []struct {
name string
expectedRelPath string
args args
}{
{
name: "empty root directory",
args: args{
sourcePath: sourcePath1,
},
expectedRelPath: "filename.yaml",
},
{
name: "root directory not empty",
args: args{
absRootDir: dir1,
sourcePath: sourcePath1,
},
expectedRelPath: filepath.Join(dir2, "filename.yaml"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
k := &K8sV1{absRootDir: tt.args.absRootDir}
gotRelPath := k.getSourceRelativePath(tt.args.sourcePath)
if gotRelPath != tt.expectedRelPath {
t.Errorf("Test_getSourceRelativePath() = unexpected relative path; want relPath: %s, got relPath: %s", tt.expectedRelPath, gotRelPath)
}
})
}
}
3 changes: 3 additions & 0 deletions pkg/iac-providers/kubernetes/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import "github.com/hashicorp/go-multierror"
// K8sV1 struct implements the IacProvider interface
type K8sV1 struct {
errIacLoadDirs *multierror.Error
// absRootDir is the root directory being scanned.
// if a file scan was initiated, absRootDir should be empty.
absRootDir string
}

const (
Expand Down

0 comments on commit 939a19c

Please sign in to comment.