This repository has been archived by the owner on Nov 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ability to load the plugin settings from config file
- Loading branch information
1 parent
f726628
commit 4338092
Showing
12 changed files
with
421 additions
and
38 deletions.
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
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,73 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/aserto-dev/go-utils/logger" | ||
"github.com/pkg/errors" | ||
"github.com/rs/zerolog" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
type Config struct { | ||
Logging *logger.Config `mapstructure:"logging"` | ||
Plugins map[string]map[string]interface{} `mapstructure:"plugins"` | ||
} | ||
|
||
// Loads the config from a file. | ||
func NewConfig(configPath string, log *zerolog.Logger) (*Config, error) { | ||
if configPath == "" { | ||
return &Config{}, nil | ||
} | ||
configLogger := log.With().Str("component", "config").Logger() | ||
log = &configLogger | ||
|
||
exists, err := fileExists(configPath) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to determine if the config file %s exists", configPath) | ||
} | ||
|
||
if !exists { | ||
return nil, errors.New(fmt.Sprintf("Config file %s does not exist", configPath)) | ||
} | ||
|
||
v := viper.New() | ||
v.SetConfigFile("yaml") | ||
v.AddConfigPath(".") | ||
v.SetConfigFile(configPath) | ||
v.SetEnvPrefix("ASERTO_IDP_CLI") | ||
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) | ||
|
||
err = v.ReadInConfig() | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to open config file '%s'", configPath) | ||
} | ||
v.AutomaticEnv() | ||
|
||
cfg := new(Config) | ||
err = v.UnmarshalExact(cfg) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to unmarshal config file '%s'", configPath) | ||
} | ||
|
||
if cfg.Logging != nil { | ||
cfg.Logging.LogLevelParsed, err = zerolog.ParseLevel(cfg.Logging.LogLevel) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "cannot parse log level %s", cfg.Logging.LogLevel) | ||
} | ||
} | ||
|
||
return cfg, nil | ||
} | ||
|
||
func fileExists(path string) (bool, error) { | ||
if _, err := os.Stat(path); err == nil { | ||
return true, nil | ||
} else if os.IsNotExist(err) { | ||
return false, nil | ||
} else { | ||
return false, errors.Wrapf(err, "failed to stat file '%s'", path) | ||
} | ||
} |
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,108 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/rs/zerolog" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var log zerolog.Logger | ||
|
||
func getConfigFile(content string, t *testing.T) string { | ||
tmpDir := t.TempDir() | ||
|
||
configFilePath := filepath.Join(tmpDir, fmt.Sprintf("%s.yaml", t.Name())) | ||
configFile, err := os.Create(configFilePath) | ||
if err != nil { | ||
t.Error("cannot create configuration file") | ||
} | ||
defer configFile.Close() | ||
|
||
_, err = configFile.WriteString(content) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
return configFilePath | ||
} | ||
|
||
func TestConfigDoesNotExist(t *testing.T) { | ||
assert := require.New(t) | ||
configPath := filepath.Join(t.TempDir(), "not_exist.yaml") | ||
|
||
cfg, err := NewConfig(configPath, &log) | ||
assert.NotNil(err) | ||
assert.Nil(cfg) | ||
|
||
} | ||
|
||
func TestConfigFromFile(t *testing.T) { | ||
assert := require.New(t) | ||
content := ` | ||
--- | ||
logging: | ||
log_level: trace | ||
plugins: | ||
json: | ||
file: user_import.json | ||
aserto: | ||
tenant: "00000000-0000-0000-0000-000000000000" | ||
authorizer: "authorizer.prod.aserto.com:8443" | ||
api_key: "0000000000000000000000000000000000000000000000000000" | ||
` | ||
|
||
configFilePath := getConfigFile(content, t) | ||
cfg, err := NewConfig(configFilePath, &log) | ||
|
||
assert.Nil(err) | ||
assert.NotNil(cfg.Plugins["json"]) | ||
assert.NotNil(cfg.Plugins["aserto"]) | ||
assert.Equal(zerolog.LevelTraceValue, cfg.Logging.LogLevel) | ||
assert.Nil(cfg.Plugins["ion"]) | ||
|
||
jsonSettings := cfg.Plugins["json"] | ||
assert.Equal("user_import.json", jsonSettings["file"]) | ||
assert.Nil(jsonSettings["nosetting"]) | ||
|
||
asertoSettings := cfg.Plugins["aserto"] | ||
assert.Equal("00000000-0000-0000-0000-000000000000", asertoSettings["tenant"]) | ||
assert.Equal("authorizer.prod.aserto.com:8443", asertoSettings["authorizer"]) | ||
assert.Equal("0000000000000000000000000000000000000000000000000000", asertoSettings["api_key"]) | ||
|
||
} | ||
|
||
func TestConfigFromFileNoPlugins(t *testing.T) { | ||
assert := require.New(t) | ||
content := ` | ||
--- | ||
logging: | ||
log_level: error | ||
` | ||
|
||
configFilePath := getConfigFile(content, t) | ||
cfg, err := NewConfig(configFilePath, &log) | ||
|
||
assert.Nil(err) | ||
assert.Nil(cfg.Plugins["aserto"]) | ||
assert.Equal(zerolog.ErrorLevel, cfg.Logging.LogLevelParsed) | ||
} | ||
|
||
func TestConfigFromFileNoPLogging(t *testing.T) { | ||
assert := require.New(t) | ||
content := ` | ||
--- | ||
plugins: | ||
json: | ||
file: user_import.json | ||
` | ||
|
||
configFilePath := getConfigFile(content, t) | ||
cfg, err := NewConfig(configFilePath, &log) | ||
|
||
assert.Nil(err) | ||
assert.Nil(cfg.Logging) | ||
} |
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
Oops, something went wrong.