Skip to content

Commit

Permalink
support for new flag to define custom temp directory (#1217)
Browse files Browse the repository at this point in the history
Co-authored-by: Suvarna Rokade <suvarna.rokade@accurics.com>
  • Loading branch information
Rchanger and Suvarna Rokade committed Apr 1, 2022
1 parent 119958e commit 471df8b
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/usage/command_line_mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Flags:
-l, --log-level string log level (debug, info, warn, error, panic, fatal) (default "info")
-x, --log-type string log output type (console, json) (default "console")
-o, --output string output type (human, json, yaml, xml) (default "human")
--temp-dir string temporary directory path to download remote repository,module and templates

Use "terrascan [command] --help" for more information about a command.
```
Expand Down Expand Up @@ -291,6 +292,7 @@ aws_ecr_repository:
| -l | Use this to specify what log settings | debug, **info**, warn, error, panic, fatal |
| -x | Use this to specify the log file format | **console**, json |
| -o | Use this to specify the scan output type | **human**, json, yaml, xml, junit-xml, sarif, github-sarif |
| --temp-dir | Use this to specify temporary directory path to download remote repository,module and templates |



Expand Down Expand Up @@ -337,4 +339,5 @@ Global Flags:
-l, --log-level string log level (debug, info, warn, error, panic, fatal) (default "info")
-x, --log-type string log output type (console, json) (default "console")
-o, --output string output type (human, json, yaml, xml, junit-xml, sarif, github-sarif) (default "human")
--temp-dir string temporary directory path to download remote repository,module and templates
```
5 changes: 5 additions & 0 deletions pkg/cli/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/accurics/terrascan/pkg/config"
"github.com/accurics/terrascan/pkg/logging"
"github.com/accurics/terrascan/pkg/utils"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
Expand All @@ -40,6 +41,7 @@ func Execute() {
rootCmd.PersistentFlags().StringVarP(&LogType, "log-type", "x", "console", "log output type (console, json)")
rootCmd.PersistentFlags().StringVarP(&OutputType, "output", "o", "human", "output type (human, json, yaml, xml, junit-xml, sarif, github-sarif)")
rootCmd.PersistentFlags().StringVarP(&ConfigFile, "config-path", "c", "", "config file path")
rootCmd.PersistentFlags().StringVarP(&CustomTempDir, "temp-dir", "", "", "temporary directory path to download remote repository,module and templates")

//Added init here in case flag parsing failed we should log which flag was incorrect.
logging.Init(LogType, LogLevel)
Expand All @@ -59,6 +61,9 @@ func Execute() {
zap.S().Error("error while loading global config", zap.Error(err))
os.Exit(1)
}
if CustomTempDir != "" {
utils.CustomTempDir = CustomTempDir
}
})

// parse the flags but hack around to avoid exiting with error code 2 on help
Expand Down
3 changes: 3 additions & 0 deletions pkg/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ var (

// ConfigFile Config file path
ConfigFile string

// CustomTempDir Temporary directory path to download remote repository,module and templates
CustomTempDir string
)

var rootCmd = &cobra.Command{
Expand Down
6 changes: 6 additions & 0 deletions pkg/utils/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import (
// download will happen in the provided directory
const customTempDir = "TERRRASCAN_CUSTOM_TEMP_DIR"

// CustomTempDir store the global flag --temp-dir value which will be used to download repository,module and template.
var CustomTempDir string

// GetHomeDir returns the home directory path
func GetHomeDir() (terrascanDir string) {
zap.S().Debug("looking up for the home directory path")
Expand All @@ -45,6 +48,9 @@ func GetHomeDir() (terrascanDir string) {
func GenerateTempDir() string {
// if env variable custom temp directory is set will be used for download/clone.
tempDir := os.Getenv(customTempDir)
if CustomTempDir != "" {
tempDir = CustomTempDir
}
if tempDir == "" {
tempDir = os.TempDir()
}
Expand Down
1 change: 1 addition & 0 deletions test/e2e/help/golden/help_command.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ Flags:
-l, --log-level string log level (debug, info, warn, error, panic, fatal) (default "info")
-x, --log-type string log output type (console, json) (default "console")
-o, --output string output type (human, json, yaml, xml, junit-xml, sarif, github-sarif) (default "human")
--temp-dir string temporary directory path to download remote repository,module and templates

Use "terrascan [command] --help" for more information about a command.
1 change: 1 addition & 0 deletions test/e2e/help/golden/help_flag.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ Flags:
-l, --log-level string log level (debug, info, warn, error, panic, fatal) (default "info")
-x, --log-type string log output type (console, json) (default "console")
-o, --output string output type (human, json, yaml, xml, junit-xml, sarif, github-sarif) (default "human")
--temp-dir string temporary directory path to download remote repository,module and templates

Use "terrascan [command] --help" for more information about a command.
1 change: 1 addition & 0 deletions test/e2e/help/golden/help_init.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ Global Flags:
-l, --log-level string log level (debug, info, warn, error, panic, fatal) (default "info")
-x, --log-type string log output type (console, json) (default "console")
-o, --output string output type (human, json, yaml, xml, junit-xml, sarif, github-sarif) (default "human")
--temp-dir string temporary directory path to download remote repository,module and templates
1 change: 1 addition & 0 deletions test/e2e/help/golden/help_scan.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ Global Flags:
-l, --log-level string log level (debug, info, warn, error, panic, fatal) (default "info")
-x, --log-type string log output type (console, json) (default "console")
-o, --output string output type (human, json, yaml, xml, junit-xml, sarif, github-sarif) (default "human")
--temp-dir string temporary directory path to download remote repository,module and templates
1 change: 1 addition & 0 deletions test/e2e/help/golden/help_server.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ Global Flags:
-l, --log-level string log level (debug, info, warn, error, panic, fatal) (default "info")
-x, --log-type string log output type (console, json) (default "console")
-o, --output string output type (human, json, yaml, xml, junit-xml, sarif, github-sarif) (default "human")
--temp-dir string temporary directory path to download remote repository,module and templates
1 change: 1 addition & 0 deletions test/e2e/help/golden/help_unsupported_command.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ Flags:
-l, --log-level string log level (debug, info, warn, error, panic, fatal) (default "info")
-x, --log-type string log output type (console, json) (default "console")
-o, --output string output type (human, json, yaml, xml, junit-xml, sarif, github-sarif) (default "human")
--temp-dir string temporary directory path to download remote repository,module and templates

Use "terrascan [command] --help" for more information about a command.
1 change: 1 addition & 0 deletions test/e2e/help/golden/help_version.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ Global Flags:
-l, --log-level string log level (debug, info, warn, error, panic, fatal) (default "info")
-x, --log-type string log output type (console, json) (default "console")
-o, --output string output type (human, json, yaml, xml, junit-xml, sarif, github-sarif) (default "human")
--temp-dir string temporary directory path to download remote repository,module and templates
1 change: 1 addition & 0 deletions test/e2e/help/golden/no_command.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ Flags:
-l, --log-level string log level (debug, info, warn, error, panic, fatal) (default "info")
-x, --log-type string log output type (console, json) (default "console")
-o, --output string output type (human, json, yaml, xml, junit-xml, sarif, github-sarif) (default "human")
--temp-dir string temporary directory path to download remote repository,module and templates

Use "terrascan [command] --help" for more information about a command.
15 changes: 15 additions & 0 deletions test/e2e/scan/scan_remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,19 @@ var _ = Describe("Scan Command using remote types", func() {
})
})
})
Context("when scan is run on remote dir and using flag --temp-dir to set custom temp dir", func() {
When("remote type is git", func() {
remoteURL := "github.com/accurics/KaiMonkey/terraform/aws"
tmpDir, err := filepath.Abs(filepath.Join(iacRootRelPath, "temp_dir"))
Expect(err).NotTo(HaveOccurred())
It("should download the resource in provided custom temp dir and generate scan results", func() {
scanArgs := []string{scanUtils.ScanCommand, "-o", "json", "-r", "git", "--remote-url", remoteURL, "--temp-dir", tmpDir}
session = helper.RunCommand(terrascanBinaryPath, outWriter, errWriter, scanArgs...)
// exit code is 5 because iac files in directory has violations
// and directory scan errors
Eventually(session, scanUtils.RemoteScanTimeout).Should(gexec.Exit(helper.ExitCodeFive))
helper.ContainsDirScanErrorSubString(session, tmpDir)
})
})
})
})

0 comments on commit 471df8b

Please sign in to comment.