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

Allow configuration of global policy config, fix some typos #354

Merged
merged 7 commits into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
52 changes: 50 additions & 2 deletions pkg/config/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@
package config

import (
"encoding/json"
"fmt"
"io/ioutil"
"log" // we log from init(), so can't rely on zap to be available
"os"
)

const (
policyRepoURL = "https://github.com/accurics/terrascan.git"
policyBranch = "master"
policyRepoURL = "https://github.com/accurics/terrascan.git"
policyBranch = "master"
configEnvvarName = "TERRASCAN_CONFIG"
)

var (
Expand All @@ -31,12 +36,55 @@ var (
)

func init() {
loadGlobalConfig()
}

func loadGlobalConfig() {
// Start with the defaults
Global.Policy = PolicyConfig{
BasePath: policyBasePath,
RepoPath: policyRepoPath,
RepoURL: policyRepoURL,
Branch: policyBranch,
}

// If the user specifies a config file in TERRASCAN_CONFIG,
// overwrite the defaults with the values from that file.
// Retain the defaults for members not specified in the file.
configFile := os.Getenv(configEnvvarName)

if len(configFile) > 0 {
p, err := loadConfigFile(configFile)
if err != nil {
log.Println(err)
return
}
if len(p.Policy.BasePath) > 0 {
Global.Policy.BasePath = p.Policy.BasePath
}
if len(p.Policy.RepoPath) > 0 {
Global.Policy.RepoPath = p.Policy.RepoPath
}
if len(p.Policy.RepoURL) > 0 {
Global.Policy.RepoURL = p.Policy.RepoURL
}
if len(p.Policy.Branch) > 0 {
Global.Policy.Branch = p.Policy.Branch
}
}
}

func loadConfigFile(configFile string) (GlobalConfig, error) {
p := GlobalConfig{}
data, err := ioutil.ReadFile(configFile)
if err != nil {
return p, fmt.Errorf("unable to read config file: %v", err)
}

if err = json.Unmarshal(data, &p); err != nil {
return p, fmt.Errorf("unable to unmarshal config file %s: %v", configFile, err)
}
return p, nil
}

// GetPolicyBasePath returns policy base path as set in global config
Expand Down
57 changes: 57 additions & 0 deletions pkg/config/global_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copyright (C) 2020 Accurics, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package config

import (
"os"
"testing"
)

func testConfigEnv(configPath string, t *testing.T) {
// Load the test data to compare against
config, err := loadConfigFile(configPath)
if err != nil {
t.Error(err)
}

// Set the environment variable and make sure the global config is set
oldenv := os.Getenv(configEnvvarName)
os.Setenv(configEnvvarName, configPath)
defer os.Setenv(configEnvvarName, oldenv)

loadGlobalConfig()

if Global.Policy.BasePath != config.Policy.BasePath {
t.Errorf("BasePath not overridden! %v != %v", Global.Policy.BasePath, config.Policy.BasePath)
}

if Global.Policy.RepoPath != config.Policy.RepoPath {
t.Errorf("RepoPath not overridden! %v != %v", Global.Policy.RepoPath, config.Policy.RepoPath)
}

if Global.Policy.RepoURL != config.Policy.RepoURL {
t.Errorf("RepoURL not overridden! %v != %v", Global.Policy.RepoURL, config.Policy.RepoURL)
}

if Global.Policy.Branch != config.Policy.Branch {
t.Errorf("Branch not overridden! %v != %v", Global.Policy.Branch, config.Policy.Branch)
}
}

func TestConfigFileLoadsCorrectly(t *testing.T) {
testConfigEnv("./testdata/terrascan-config.json", t)
}
8 changes: 8 additions & 0 deletions pkg/config/testdata/terrascan-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"policy": {
"path": "custom-path",
"rego_subdir": "rego-subdir",
"repo_url": "https://repository/url",
"branch": "branch-name"
}
}
10 changes: 5 additions & 5 deletions pkg/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ var Global *GlobalConfig = &GlobalConfig{}

// GlobalConfig struct defines global variables/configurations across terrascan
type GlobalConfig struct {
Policy PolicyConfig
Policy PolicyConfig `json:"policy"`
}

// PolicyConfig struct define policy specific configurations
type PolicyConfig struct {
// policy local path
BasePath string
RepoPath string
BasePath string `json:"rego_subdir"`
RepoPath string `json:"path"`

// policy git url and branch
RepoURL string
Branch string
RepoURL string `json:"repo_url"`
Branch string `json:"branch"`
}
2 changes: 1 addition & 1 deletion pkg/writer/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func Write(format string, data interface{}, writer io.Writer) error {

writerFunc, present := writerMap[supportedFormat(format)]
if !present {
zap.S().Error("output format '%s' not supported", format)
zap.S().Errorf("output format '%s' not supported", format)
return errNotSupported
}

Expand Down
3 changes: 2 additions & 1 deletion scripts/validate-gofmt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ bad_files=$(find_files | xargs gofmt -d -s 2>&1)
if [[ -n "${bad_files}" ]]; then
echo "${bad_files}" >&2
echo >&2
echo "fix gofmt errors by running:\n./hack/fix-gofmt.sh" >&2
echo "fix gofmt errors by running:" >&2
echo "./scripts/fix-gofmt.sh" >&2
exit 1
fi