Implement "cscli config show-yaml" (#2191)

This commit is contained in:
mmetc 2023-05-11 21:01:13 +02:00 committed by GitHub
parent 5ac33aab03
commit e1f5ed41df
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 48 additions and 29 deletions

View file

@ -86,24 +86,6 @@ jobs:
PGPASSWORD: "secret"
PGUSER: postgres
# - name: "Build crowdsec and fixture (DB_BACKEND: postgres)"
# run: make clean bats-build bats-fixture
# env:
# DB_BACKEND: postgres
# PGHOST: 127.0.0.1
# PGPORT: 5432
# PGPASSWORD: "secret"
# PGUSER: postgres
#
# - name: "Run tests (DB_BACKEND: postgres)"
# run: make bats-test
# env:
# DB_BACKEND: postgres
# PGHOST: 127.0.0.1
# PGPORT: 5432
# PGPASSWORD: "secret"
# PGUSER: postgres
- name: "Show stack traces"
run: for file in $(find /tmp/crowdsec-crash.*.txt); do echo ">>>>> $file"; cat $file; echo; done
if: ${{ always() }}

View file

@ -13,6 +13,7 @@ func NewConfigCmd() *cobra.Command {
}
cmdConfig.AddCommand(NewConfigShowCmd())
cmdConfig.AddCommand(NewConfigShowYAMLCmd())
cmdConfig.AddCommand(NewConfigBackupCmd())
cmdConfig.AddCommand(NewConfigRestoreCmd())
cmdConfig.AddCommand(NewConfigFeatureFlagsCmd())

View file

@ -0,0 +1,24 @@
package main
import (
"fmt"
"github.com/spf13/cobra"
)
func runConfigShowYAML(cmd *cobra.Command, args []string) error {
fmt.Println(mergedConfig)
return nil
}
func NewConfigShowYAMLCmd() *cobra.Command {
cmdConfigShow := &cobra.Command{
Use: "show-yaml",
Short: "Displays merged config.yaml + config.yaml.local",
Args: cobra.ExactArgs(0),
DisableAutoGenTag: true,
RunE: runConfigShowYAML,
}
return cmdConfigShow
}

View file

@ -36,6 +36,8 @@ var all bool
var prometheusURL string
var mergedConfig string
func initConfig() {
var err error
if trace_lvl {
@ -51,7 +53,7 @@ func initConfig() {
}
if !inSlice(os.Args[1], NoNeedConfig) {
csConfig, err = csconfig.NewConfig(ConfigFilePath, false, false, true)
csConfig, mergedConfig, err = csconfig.NewConfig(ConfigFilePath, false, false, true)
if err != nil {
log.Fatal(err)
}

View file

@ -31,7 +31,7 @@ test:
$(GOTEST) $(LD_OPTS) -v ./...
clean:
@$(RM) $(CROWDSEC_BIN) $(CROWDSEC_BIN).test $(WIN_IGNORE_ERR)
@$(RM) $(CROWDSEC_BIN) $(WIN_IGNORE_ERR)
.PHONY: install
install: install-conf install-bin

View file

@ -192,7 +192,7 @@ func newLogLevel(curLevelPtr *log.Level, f *Flags) *log.Level {
// LoadConfig returns a configuration parsed from configuration file
func LoadConfig(configFile string, disableAgent bool, disableAPI bool, quiet bool) (*csconfig.Config, error) {
cConfig, err := csconfig.NewConfig(configFile, disableAgent, disableAPI, quiet)
cConfig, _, err := csconfig.NewConfig(configFile, disableAgent, disableAPI, quiet)
if err != nil {
return nil, err
}

View file

@ -47,12 +47,12 @@ func (c *Config) Dump() error {
return nil
}
func NewConfig(configFile string, disableAgent bool, disableAPI bool, quiet bool) (*Config, error) {
func NewConfig(configFile string, disableAgent bool, disableAPI bool, quiet bool) (*Config, string, error) {
patcher := yamlpatch.NewPatcher(configFile, ".local")
patcher.SetQuiet(quiet)
fcontent, err := patcher.MergedPatchContent()
if err != nil {
return nil, err
return nil, "", err
}
configData := csstring.StrictExpand(string(fcontent), os.LookupEnv)
cfg := Config{
@ -64,9 +64,9 @@ func NewConfig(configFile string, disableAgent bool, disableAPI bool, quiet bool
err = yaml.UnmarshalStrict([]byte(configData), &cfg)
if err != nil {
// this is actually the "merged" yaml
return nil, errors.Wrap(err, configFile)
return nil, "", errors.Wrap(err, configFile)
}
return &cfg, nil
return &cfg, configData, nil
}
func NewDefaultConfig() *Config {

View file

@ -10,13 +10,13 @@ import (
)
func TestNormalLoad(t *testing.T) {
_, err := NewConfig("./tests/config.yaml", false, false, false)
_, _, err := NewConfig("./tests/config.yaml", false, false, false)
require.NoError(t, err)
_, err = NewConfig("./tests/xxx.yaml", false, false, false)
_, _, err = NewConfig("./tests/xxx.yaml", false, false, false)
assert.EqualError(t, err, "while reading yaml file: open ./tests/xxx.yaml: "+cstest.FileNotFoundMessage)
_, err = NewConfig("./tests/simulation.yaml", false, false, false)
_, _, err = NewConfig("./tests/simulation.yaml", false, false, false)
assert.EqualError(t, err, "./tests/simulation.yaml: yaml: unmarshal errors:\n line 1: field simulation not found in type csconfig.Config")
}

View file

@ -110,6 +110,16 @@ teardown() {
assert_output '["http://127.0.0.1:8080/","githubciXXXXXXXXXXXXXXXXXXXXXXXX"]'
}
@test "cscli config show-yaml" {
rune -0 cscli config show-yaml
rune -0 yq .common.log_level <(output)
assert_output "info"
echo 'common: {"log_level": "debug"}' >> "${CONFIG_YAML}.local"
rune -0 cscli config show-yaml
rune -0 yq .common.log_level <(output)
assert_output "debug"
}
@test "cscli config backup / restore" {
# test that we need a valid path
# disabled because in CI, the empty string is not passed as a parameter

View file

@ -2,7 +2,7 @@
is_crowdsec_running() {
# ignore processes in containers
PIDS=$(pgrep --ns $$ -x 'crowdsec|crowdsec.test')
PIDS=$(pgrep --ns $$ -x 'crowdsec')
}
# The process can be slow, especially on CI and during test coverage.