From 3a84b1d4f8223621896164c84c0d4f76574819ae Mon Sep 17 00:00:00 2001 From: Meyazhagan Date: Tue, 11 Apr 2023 17:16:43 +0530 Subject: [PATCH 1/3] feat:add configuration for schema_locations, policy_config in config file and env --- cmd/config/main.go | 2 +- cmd/test/main.go | 31 +++++++++++++++++++++---- pkg/localConfig/localConfig.go | 42 ++++++++++++++++++++++++++-------- 3 files changed, 60 insertions(+), 15 deletions(-) diff --git a/cmd/config/main.go b/cmd/config/main.go index 80358e383f..2c6a463a25 100644 --- a/cmd/config/main.go +++ b/cmd/config/main.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -var ConfigAvailableKeys = []string{"token", "offline"} +var ConfigAvailableKeys = []string{"token", "offline", "policy_config", "schema_locations"} type Messager interface { LoadVersionMessages(cliVersion string) chan *messager.VersionMessage diff --git a/cmd/test/main.go b/cmd/test/main.go index 3705804506..03f1df1ec6 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -278,6 +278,11 @@ func (flags *TestCommandFlags) AddFlags(cmd *cobra.Command) { cmd.Flags().BoolVarP(&flags.Quiet, "quiet", "", false, "Don't print skipped rules messages") } +const ( + DatreePolicyConfig = "DATREE_POLICY_CONFIG" + DatreeSchemaLocations = "DATREE_SCHEMA_LOCATION" +) + func GenerateTestCommandData(testCommandFlags *TestCommandFlags, localConfigContent *localConfig.LocalConfig, evaluationPrerunDataResp *cliClient.EvaluationPrerunDataResponse) (*TestCommandData, error) { k8sVersion := testCommandFlags.K8sVersion if k8sVersion == "" { @@ -293,13 +298,22 @@ func GenerateTestCommandData(testCommandFlags *TestCommandFlags, localConfigCont var policies *defaultPolicies.EvaluationPrerunPolicies var err error + policyConfigEnv := os.Getenv(DatreePolicyConfig) - if testCommandFlags.PolicyConfig != "" { + if testCommandFlags.PolicyConfig != "" || localConfigContent.PolicyConfig != "" || policyConfigEnv != "" { if localConfigContent.Offline != "local" && !evaluationPrerunDataResp.IsPolicyAsCodeMode { - return nil, fmt.Errorf("to use --policy-config flag you must first enable policy-as-code mode: https://hub.datree.io/policy-as-code") + return nil, fmt.Errorf("to use custom policy-config you must first enable policy-as-code mode: https://hub.datree.io/policy-as-code") + } + + policyConfig := testCommandFlags.PolicyConfig + if policyConfig == "" { + policyConfig = policyConfigEnv + } + if policyConfig == "" { + policyConfig = localConfigContent.PolicyConfig } - policies, err = policy.GetPoliciesFileFromPath(testCommandFlags.PolicyConfig) + policies, err = policy.GetPoliciesFileFromPath(policyConfig) if err != nil { return nil, err } @@ -317,6 +331,15 @@ func GenerateTestCommandData(testCommandFlags *TestCommandFlags, localConfigCont return nil, err } + schemaLocations := testCommandFlags.SchemaLocations + if len(schemaLocations) == 0 { + schemaLocationsEnv := os.Getenv(DatreeSchemaLocations) + schemaLocations = strings.Split(schemaLocationsEnv, ",") + } + if len(schemaLocations) == 0 { + schemaLocations = localConfigContent.SchemaLocations + } + testCommandOptions := &TestCommandData{Output: testCommandFlags.Output, K8sVersion: k8sVersion, IgnoreMissingSchemas: testCommandFlags.IgnoreMissingSchemas, @@ -324,7 +347,7 @@ func GenerateTestCommandData(testCommandFlags *TestCommandFlags, localConfigCont Verbose: testCommandFlags.Verbose, NoRecord: testCommandFlags.NoRecord, Policy: policy, - SchemaLocations: testCommandFlags.SchemaLocations, + SchemaLocations: schemaLocations, Token: localConfigContent.Token, ClientId: localConfigContent.ClientId, RegistrationURL: evaluationPrerunDataResp.RegistrationURL, diff --git a/pkg/localConfig/localConfig.go b/pkg/localConfig/localConfig.go index de1ed6146a..b8e834fe30 100644 --- a/pkg/localConfig/localConfig.go +++ b/pkg/localConfig/localConfig.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "github.com/datreeio/datree/pkg/networkValidator" @@ -13,10 +14,12 @@ import ( ) type LocalConfig struct { - Token string - ClientId string - SchemaVersion string - Offline string + Token string + ClientId string + SchemaVersion string + Offline string + PolicyConfig string + SchemaLocations []string } type TokenClient interface { @@ -36,10 +39,12 @@ func NewLocalConfigClient(t TokenClient, nv *networkValidator.NetworkValidator) } const ( - clientIdKey = "client_id" - tokenKey = "token" - schemaVersionKey = "schema_version" - offlineKey = "offline" + clientIdKey = "client_id" + tokenKey = "token" + schemaVersionKey = "schema_version" + offlineKey = "offline" + policyConfigKey = "policy_config" + schemaLocationsKey = "schema_locations" ) func (lc *LocalConfigClient) GetLocalConfiguration() (*LocalConfig, error) { @@ -55,6 +60,8 @@ func (lc *LocalConfigClient) GetLocalConfiguration() (*LocalConfig, error) { clientId := viper.GetString(clientIdKey) schemaVersion := viper.GetString(schemaVersionKey) offline := viper.GetString(offlineKey) + policyConfig := viper.GetString(policyConfigKey) + schemaLocations := viper.GetStringSlice(schemaLocationsKey) if offline == "" { offline = "fail" @@ -87,7 +94,7 @@ func (lc *LocalConfigClient) GetLocalConfiguration() (*LocalConfig, error) { } } - return &LocalConfig{Token: token, ClientId: clientId, SchemaVersion: schemaVersion, Offline: offline}, nil + return &LocalConfig{Token: token, ClientId: clientId, SchemaVersion: schemaVersion, Offline: offline, PolicyConfig: policyConfig, SchemaLocations: schemaLocations}, nil } func (lc *LocalConfigClient) Set(key string, value string) error { @@ -101,7 +108,9 @@ func (lc *LocalConfigClient) Set(key string, value string) error { return err } - viper.Set(key, value) + tranformedValue := transformValue(key, value) + + viper.Set(key, tranformedValue) writeClientIdErr := viper.WriteConfig() if writeClientIdErr != nil { return writeClientIdErr @@ -109,6 +118,19 @@ func (lc *LocalConfigClient) Set(key string, value string) error { return nil } +func transformValue(key string, value string) interface{} { + if key == policyConfigKey { + absPath, _ := filepath.Abs(value) + return absPath + } + + if key == schemaLocationsKey { + return strings.Split(value, ",") + } + + return value +} + func InitLocalConfigFile() error { configHome, configName, configType, err := setViperConfig() if err != nil { From e42444dccafd1caf033b09800083fc19ecb43e04 Mon Sep 17 00:00:00 2001 From: Meyazhagan Date: Thu, 20 Apr 2023 17:35:42 +0530 Subject: [PATCH 2/3] change transformedValue to inline if-else --- pkg/localConfig/localConfig.go | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/pkg/localConfig/localConfig.go b/pkg/localConfig/localConfig.go index b8e834fe30..bf8bf32bf4 100644 --- a/pkg/localConfig/localConfig.go +++ b/pkg/localConfig/localConfig.go @@ -108,9 +108,15 @@ func (lc *LocalConfigClient) Set(key string, value string) error { return err } - tranformedValue := transformValue(key, value) + if key == policyConfigKey { + absPath, _ := filepath.Abs(value) + viper.Set(policyConfigKey, absPath) + } else if key == schemaLocationsKey { + viper.Set(schemaLocationsKey, strings.Split(value, ",")) + } else { + viper.Set(key, value) + } - viper.Set(key, tranformedValue) writeClientIdErr := viper.WriteConfig() if writeClientIdErr != nil { return writeClientIdErr @@ -118,19 +124,6 @@ func (lc *LocalConfigClient) Set(key string, value string) error { return nil } -func transformValue(key string, value string) interface{} { - if key == policyConfigKey { - absPath, _ := filepath.Abs(value) - return absPath - } - - if key == schemaLocationsKey { - return strings.Split(value, ",") - } - - return value -} - func InitLocalConfigFile() error { configHome, configName, configType, err := setViperConfig() if err != nil { From 893cb5a3add35f8333f7d09cec51d53b0b6a0f62 Mon Sep 17 00:00:00 2001 From: Meyazhagan Date: Thu, 20 Apr 2023 17:38:26 +0530 Subject: [PATCH 3/3] restructed identical if statement --- cmd/test/main.go | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/cmd/test/main.go b/cmd/test/main.go index 03f1df1ec6..4fb36d3582 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -298,21 +298,21 @@ func GenerateTestCommandData(testCommandFlags *TestCommandFlags, localConfigCont var policies *defaultPolicies.EvaluationPrerunPolicies var err error - policyConfigEnv := os.Getenv(DatreePolicyConfig) - if testCommandFlags.PolicyConfig != "" || localConfigContent.PolicyConfig != "" || policyConfigEnv != "" { + var policyConfig string + if testCommandFlags.PolicyConfig != "" { + policyConfig = testCommandFlags.PolicyConfig + } else if policyConfigEnv, ok := os.LookupEnv(DatreePolicyConfig); ok { + policyConfig = policyConfigEnv + } else if localConfigContent.PolicyConfig != "" { + policyConfig = localConfigContent.PolicyConfig + } + + if policyConfig != "" { if localConfigContent.Offline != "local" && !evaluationPrerunDataResp.IsPolicyAsCodeMode { return nil, fmt.Errorf("to use custom policy-config you must first enable policy-as-code mode: https://hub.datree.io/policy-as-code") } - policyConfig := testCommandFlags.PolicyConfig - if policyConfig == "" { - policyConfig = policyConfigEnv - } - if policyConfig == "" { - policyConfig = localConfigContent.PolicyConfig - } - policies, err = policy.GetPoliciesFileFromPath(policyConfig) if err != nil { return nil, err @@ -331,12 +331,12 @@ func GenerateTestCommandData(testCommandFlags *TestCommandFlags, localConfigCont return nil, err } - schemaLocations := testCommandFlags.SchemaLocations - if len(schemaLocations) == 0 { - schemaLocationsEnv := os.Getenv(DatreeSchemaLocations) + var schemaLocations []string + if len(testCommandFlags.SchemaLocations) != 0 { + schemaLocations = testCommandFlags.SchemaLocations + } else if schemaLocationsEnv, ok := os.LookupEnv(DatreeSchemaLocations); ok { schemaLocations = strings.Split(schemaLocationsEnv, ",") - } - if len(schemaLocations) == 0 { + } else if len(localConfigContent.SchemaLocations) != 0 { schemaLocations = localConfigContent.SchemaLocations }