-
Notifications
You must be signed in to change notification settings - Fork 509
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ae5d650
allow config file for global policy config
acc-jon 6889c14
fix typo
acc-jon 43c4970
fix typo
acc-jon e1df4c1
Merge branch 'master' of https://github.com/accurics/terrascan
acc-jon 5960227
load global config from toml config file
acc-jon 24c4948
fix logging init. clean up tests. use globlal toml config file for po…
acc-jon 577d5ca
fix style
acc-jon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
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 ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/pelletier/go-toml" | ||
"go.uber.org/zap" | ||
) | ||
|
||
var ( | ||
// ErrTomlLoadConfig indicates error: Failed to load toml config | ||
ErrTomlLoadConfig = fmt.Errorf("failed to load toml config") | ||
// ErrNotPresent indicates error: Config file not present | ||
ErrNotPresent = fmt.Errorf("config file not present") | ||
) | ||
|
||
// LoadConfig loads a configuration from specified path and | ||
// returns a *toml.Tree with the contents of the config file | ||
func LoadConfig(configFile string) (*toml.Tree, error) { | ||
|
||
// empty config file path | ||
if configFile == "" { | ||
zap.S().Debug("no config file specified") | ||
return nil, nil | ||
} | ||
|
||
// check if file exists | ||
_, err := os.Stat(configFile) | ||
if err != nil { | ||
zap.S().Errorf("Can't find '%s'", configFile) | ||
return nil, ErrNotPresent | ||
} | ||
|
||
// parse toml config file | ||
config, err := toml.LoadFile(configFile) | ||
if err != nil { | ||
zap.S().Errorf("Error loading '%s': %v", configFile, err) | ||
return nil, ErrTomlLoadConfig | ||
} | ||
|
||
// return config Tree | ||
return config, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
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 ( | ||
"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) | ||
} | ||
|
||
LoadGlobalConfig(configPath) | ||
|
||
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.toml", t) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[policy] | ||
path = "custom-path" | ||
rego_subdir = "rego-subdir" | ||
repo_url = "https://repository/url" | ||
branch = "branch-name" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small suggestion to not have error strings capitalized: https://github.com/golang/go/wiki/CodeReviewComments#error-strings