Skip to content

Commit

Permalink
fix filepaths and home directory lookup (tenable#566)
Browse files Browse the repository at this point in the history
* fix filepaths and home directory lookup

* fixed typo
  • Loading branch information
Devang Gaur authored Mar 1, 2021
1 parent 98e57b3 commit 1027346
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 11 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/hashicorp/terraform v0.14.4
github.com/iancoleman/strcase v0.1.3
github.com/mattn/go-isatty v0.0.12
github.com/mitchellh/go-homedir v1.1.0
github.com/onsi/ginkgo v1.12.1
github.com/onsi/gomega v1.10.5
github.com/open-policy-agent/opa v0.22.0
Expand Down
8 changes: 4 additions & 4 deletions pkg/cli/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func setup() {

func shutdown() {
// remove the downloaded policies
os.RemoveAll(os.Getenv("HOME") + "/.terrascan")
os.RemoveAll(filepath.Join(utils.GetHomeDir(), ".terrascan"))
}

func TestRun(t *testing.T) {
Expand All @@ -55,9 +55,9 @@ func TestRun(t *testing.T) {
}

testDirPath := "testdata/run-test"
kustomizeTestDirPath := testDirPath + "/kustomize-test"
testTerraformFilePath := testDirPath + "/config-only.tf"
testRemoteModuleFilePath := testDirPath + "/remote-modules.tf"
kustomizeTestDirPath := filepath.Join(testDirPath, "kustomize-test")
testTerraformFilePath := filepath.Join(testDirPath, "config-only.tf")
testRemoteModuleFilePath := filepath.Join(testDirPath, "remote-modules.tf")

ruleSlice := []string{"AWS.ECR.DataSecurity.High.0579", "AWS.SecurityGroup.NetworkPortsSecurity.Low.0561"}

Expand Down
6 changes: 4 additions & 2 deletions pkg/config/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package config

import (
"os"
"path/filepath"

"github.com/accurics/terrascan/pkg/utils"
"go.uber.org/zap"
)

Expand All @@ -30,8 +32,8 @@ const (
)

var (
policyRepoPath = os.Getenv("HOME") + "/.terrascan"
policyBasePath = policyRepoPath + "/pkg/policies/opa/rego"
policyRepoPath = filepath.Join(utils.GetHomeDir(), ".terrascan")
policyBasePath = filepath.Join(policyRepoPath, "pkg", "policies", "opa", "rego")
)

func init() {
Expand Down
19 changes: 19 additions & 0 deletions pkg/utils/dir.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package utils

import (
"github.com/mitchellh/go-homedir"
"go.uber.org/zap"
)

// GetHomeDir returns the home directory path
func GetHomeDir() (terrascanDir string) {
zap.S().Debug("looking up for the home directory path")

terrascanDir, err := homedir.Dir()

if err != nil {
zap.S().Warnf("unable to determine the home directory: %v\n", err)
}

return
}
2 changes: 1 addition & 1 deletion pkg/utils/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func GetAbsPath(path string) (string, error) {

// Only shell resolves `~` to home so handle it specially
if strings.HasPrefix(path, "~") {
homeDir := os.Getenv("HOME")
homeDir := GetHomeDir()
if len(path) > 1 {
path = filepath.Join(homeDir, path[1:])
} else {
Expand Down
7 changes: 4 additions & 3 deletions pkg/utils/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package utils

import (
"os"
"path/filepath"
"reflect"
"testing"
)
Expand All @@ -39,19 +40,19 @@ func TestGetAbsPath(t *testing.T) {
{
name: "user HOME dir",
path: "~",
want: os.Getenv("HOME"),
want: GetHomeDir(),
wantErr: nil,
},
{
name: "dir in HOME dir",
path: "~/somedir",
want: os.Getenv("HOME") + "/somedir",
want: filepath.Join(GetHomeDir(), "somedir"),
wantErr: nil,
},
{
name: "testdata dir",
path: "./testdata",
want: os.Getenv("PWD") + "/testdata",
want: filepath.Join(os.Getenv("PWD"), "testdata"),
wantErr: nil,
},
}
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/init/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path/filepath"
"time"

"github.com/accurics/terrascan/pkg/utils"
initUtil "github.com/accurics/terrascan/test/e2e/init"
"github.com/accurics/terrascan/test/helper"
. "github.com/onsi/ginkgo"
Expand All @@ -17,7 +18,7 @@ import (

var (
initCommand string = "init"
defaultPolicyRepoPath string = os.Getenv("HOME") + "/.terrascan"
defaultPolicyRepoPath string = filepath.Join(utils.GetHomeDir(), ".terrascan")
terrascanGitURL string = "https://github.com/accurics/terrascan.git"
terrascanDefaultBranch string = "master"
terrascanConfigEnvName string = "TERRASCAN_CONFIG"
Expand Down

0 comments on commit 1027346

Please sign in to comment.