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

policy download refactor #618

Merged
merged 1 commit into from
Mar 15, 2021
Merged
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
4 changes: 2 additions & 2 deletions pkg/config/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ const (
)

var (
policyRepoPath = filepath.Join(utils.GetHomeDir(), ".terrascan")
policyBasePath = filepath.Join(policyRepoPath, "pkg", "policies", "opa", "rego")
policyBasePath = filepath.Join(utils.GetHomeDir(), ".terrascan")
policyRepoPath = filepath.Join(policyBasePath, "pkg", "policies", "opa", "rego")
)

func init() {
Expand Down
44 changes: 23 additions & 21 deletions pkg/initialize/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package initialize

import (
"fmt"
"io/ioutil"
"net/http"
"os"

"github.com/accurics/terrascan/pkg/config"
Expand All @@ -29,23 +29,30 @@ import (
)

var (
basePath = config.GetPolicyRepoPath()
basePolicyPath = config.GetPolicyBasePath()
policyRepoPath = config.GetPolicyRepoPath()
policyBasePath = config.GetPolicyBasePath()
repoURL = config.GetPolicyRepoURL()
branch = config.GetPolicyBranch()
noConnectionErr = fmt.Errorf("could not connect to github.com")
)

const terrascanReadmeURL string = "https://raw.githubusercontent.com/accurics/terrascan/master/README.md"

// Run initializes terrascan if not done already
func Run(isScanCmd bool) error {
zap.S().Debug("initializing terrascan")

// check if policy paths exist
if path, err := os.Stat(basePolicyPath); err == nil && path.IsDir() {
if path, err := os.Stat(policyRepoPath); err == nil && path.IsDir() {
if isScanCmd {
return nil
}
}

if !connected(terrascanReadmeURL) {
return noConnectionErr
}

// download policies
if err := DownloadPolicies(); err != nil {
return err
Expand All @@ -59,17 +66,17 @@ func Run(isScanCmd bool) error {
func DownloadPolicies() error {
zap.S().Debug("downloading policies")

tempPath, err := ioutil.TempDir("", "terrascan-")
if err != nil {
return fmt.Errorf("failed to create temporary directory. error: '%v'", err)
}
zap.S().Debugf("base directory path : %s", policyBasePath)
zap.S().Debugf("policy directory path : %s", policyRepoPath)
zap.S().Debugf("policy repo url : %s", repoURL)
zap.S().Debugf("policy repo git branch : %s", branch)

defer os.RemoveAll(tempPath)
os.RemoveAll(policyBasePath)

zap.S().Debugf("cloning terrascan repo at %s", tempPath)
zap.S().Debugf("cloning terrascan repo at %s", policyBasePath)

// clone the repo
r, err := git.PlainClone(tempPath, false, &git.CloneOptions{
r, err := git.PlainClone(policyBasePath, false, &git.CloneOptions{
URL: repoURL,
})
if err != nil {
Expand Down Expand Up @@ -99,15 +106,10 @@ func DownloadPolicies() error {
return fmt.Errorf("failed to checkout git branch '%v'. error: '%v'", branch, err)
}

// cleaning the existing cached policies at basePath
if err = os.RemoveAll(basePath); err != nil {
return fmt.Errorf("failed to clean up the directory '%s'. error: '%v'", basePath, err)
}

// move the freshly cloned repo from tempPath to basePath
if err = os.Rename(tempPath, basePath); err != nil {
return fmt.Errorf("failed to install policies to '%s'. error: '%v'", basePath, err)
}

return nil
}

func connected(url string) bool {
_, err := http.Get(url)
return err == nil
jlk marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 1 addition & 1 deletion pkg/policy/cloud-providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var defaultIacType = make(map[supportedCloudType]supportedIacType)
var defaultIacVersion = make(map[supportedCloudType]supportedIacVersion)

var (
basePolicyPath = config.GetPolicyBasePath()
basePolicyPath = config.GetPolicyRepoPath()
)

func registerActualCloudProvider(cloudType supportedCloudType, iacTypeDefault supportedIacType, iacVersionDefault supportedIacVersion, isIndirect bool, getPolicyPaths func() []string) {
Expand Down