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

Terrascan init and config handling refactor #576

Merged
merged 2 commits into from
Mar 22, 2021

Conversation

devang-gaur
Copy link
Contributor

@devang-gaur devang-gaur commented Mar 2, 2021

fixes #550 . fixes #570, fixes #619

This refactor is mainly regarding how config is handled in terrascan code. These above mentioned bug fixes are a ripple effect of this refactor.

Regarding #619 , a warning will be logged every time the user specify only one of the two policy path params, path & rego_subdir . after the warning, the default value will be applied for the unspecified of the two policy path parameter.

Regarding config, the global scoped configuration will be loaded once and only once, into singleton object, to maintain a single source of truth. To access the config values, getter functions such as config.GetPolicyBasePath( ) must be used.

PS : make sure you don't store the config values in a package scoped variable like below or inside the init( ) method,

package demo

var demoPath = config.GetPolicyBasePath( )

func dpDemo( ) {
    ....
}

that won't work as those values are loaded in compile time whereas the config itself is loaded in runtime. So, that compile time loaded variable, demoPath's, value would be blank.

@devang-gaur devang-gaur force-pushed the terrascan_init_refactor branch 2 times, most recently from d03f7c3 to 2d64a97 Compare March 3, 2021 18:39
@devang-gaur devang-gaur force-pushed the terrascan_init_refactor branch 2 times, most recently from 1ca848a to 5b9f3fa Compare March 10, 2021 15:24
@devang-gaur devang-gaur changed the title Terrascan init refactor Terrascan init and config handling refactor Mar 10, 2021
@codecov
Copy link

codecov bot commented Mar 13, 2021

Codecov Report

Merging #576 (10d8469) into master (317a536) will decrease coverage by 0.05%.
The diff coverage is 75.49%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #576      +/-   ##
==========================================
- Coverage   78.13%   78.08%   -0.06%     
==========================================
  Files         103      104       +1     
  Lines        2570     2596      +26     
==========================================
+ Hits         2008     2027      +19     
- Misses        420      422       +2     
- Partials      142      147       +5     
Impacted Files Coverage Δ
pkg/cli/output_writer.go 33.33% <ø> (ø)
pkg/cli/register.go 4.54% <0.00%> (-1.02%) ⬇️
pkg/config/config-reader.go 86.36% <0.00%> (ø)
pkg/http-server/file-scan.go 81.00% <0.00%> (+6.00%) ⬆️
pkg/http-server/remote-repo.go 69.64% <0.00%> (ø)
pkg/termcolor/termcolor.go 85.71% <ø> (ø)
pkg/policy/opa/engine.go 65.56% <50.00%> (ø)
pkg/utils/policy.go 50.00% <50.00%> (ø)
pkg/initialize/run.go 64.28% <66.66%> (+2.74%) ⬆️
pkg/runtime/executor.go 86.36% <66.66%> (-4.55%) ⬇️
... and 8 more

@devang-gaur devang-gaur changed the title Terrascan init and config handling refactor [DO NOT MERGE]Terrascan init and config handling refactor Mar 15, 2021
@devang-gaur devang-gaur force-pushed the terrascan_init_refactor branch 2 times, most recently from eafa454 to b2f9759 Compare March 17, 2021 15:30
@devang-gaur devang-gaur changed the title [DO NOT MERGE]Terrascan init and config handling refactor Terrascan init and config handling refactor Mar 17, 2021
@devang-gaur devang-gaur force-pushed the terrascan_init_refactor branch 2 times, most recently from fe97d9b to 85e6077 Compare March 18, 2021 00:28
@devang-gaur devang-gaur requested a review from jlk March 18, 2021 11:29
@@ -43,8 +46,24 @@ func Execute() {
cobra.OnInitialize(func() {
// Set up the logger
logging.Init(LogType, LogLevel)

var configfile string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we can completely get rid of configfile variable

)

const configEnvvarName = "TERRASCAN_CONFIG"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can Globally export this env variable in config package

)

func TestLoadGlobalConfig(t *testing.T) {
testConfigFile := "./testdata/terrascan-config.toml"
absDefaultBasePolicyPath, absDefaultPolicyRepoPath, _ := utils.GetAbsPolicyConfigPaths(defaultBasePolicyPath, defaultPolicyRepoPath)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to handle errors here!

if isScanCmd {
if path, err := os.Stat(config.GetPolicyRepoPath()); err == nil && path.IsDir() {

zap.S().Debug("EXISTS AND IS A DIR")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please improve this debug message? or remove it?


zap.S().Debug("EXISTS AND IS A DIR")
if isNonInitCmd {
zap.S().Debug("IS NON INIT")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please improve this debug message? or remove it?

// ErrTomlKeyNotPresent will be returned when config file does not have notificationsConfigKey
ErrTomlKeyNotPresent = fmt.Errorf("key not present in toml config")
// ErrNotificationNotPresent error is caused when there isn't any notification present in the config
ErrNotificationNotPresent = fmt.Errorf("no notification present")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try to indicate that the notifications are not present in config file

return notifiers, ErrTomlKeyNotPresent
notifications := config.GetNotifications()
if len(notifications) == 0 {
zap.S().Debug("no notification detected from config")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be we can improve this message, something like no notification specified in config

@@ -18,16 +18,16 @@ package runtime

import (
"fmt"
iacProvider "github.com/accurics/terrascan/pkg/iac-providers"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please run goimports -e -w pkg/!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for pointing out

if len(configReader.GetSeverity().Level) > 0 {
e.severity = configReader.GetSeverity().Level
if len(config.GetSeverityLevel()) > 0 {
e.severity = config.GetSeverityLevel()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check why rules_test.go is deleted?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recall that rules_test.go was checking how rules are being parsed from the config file. and now that logic has been shifted inside LoadGlobalConfig itself now.

@@ -64,6 +65,12 @@ func Run(isScanCmd bool) error {

// DownloadPolicies clones the policies to a local folder
func DownloadPolicies() error {

policyBasePath := config.GetPolicyBasePath()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of assigning new variables, we can use values directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changes made where feasible

configfile = os.Getenv(configEnvvarName)
}

zap.S().Debugf("%s=%s", configEnvvarName, os.Getenv(configEnvvarName))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if env variable is not specified, we would log:
TERRASCAN_CONFIG=, is that okay?
also, we are using : when logging a value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changes made

Copy link
Contributor

@patilpankaj212 patilpankaj212 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added review.

@sonarcloud
Copy link

sonarcloud bot commented Mar 22, 2021

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

No Coverage information No Coverage information
0.0% 0.0% Duplication

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants