diff --git a/.github/workflows/bats-postgres.yml b/.github/workflows/bats-postgres.yml index 5ce3cec4b..d3d62415d 100644 --- a/.github/workflows/bats-postgres.yml +++ b/.github/workflows/bats-postgres.yml @@ -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() }} diff --git a/cmd/crowdsec-cli/config.go b/cmd/crowdsec-cli/config.go index 4f30e8726..e60246db7 100644 --- a/cmd/crowdsec-cli/config.go +++ b/cmd/crowdsec-cli/config.go @@ -13,6 +13,7 @@ func NewConfigCmd() *cobra.Command { } cmdConfig.AddCommand(NewConfigShowCmd()) + cmdConfig.AddCommand(NewConfigShowYAMLCmd()) cmdConfig.AddCommand(NewConfigBackupCmd()) cmdConfig.AddCommand(NewConfigRestoreCmd()) cmdConfig.AddCommand(NewConfigFeatureFlagsCmd()) diff --git a/cmd/crowdsec-cli/config_showyaml.go b/cmd/crowdsec-cli/config_showyaml.go new file mode 100644 index 000000000..82bc67ffc --- /dev/null +++ b/cmd/crowdsec-cli/config_showyaml.go @@ -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 +} diff --git a/cmd/crowdsec-cli/main.go b/cmd/crowdsec-cli/main.go index 20872cb1b..21efdf0c7 100644 --- a/cmd/crowdsec-cli/main.go +++ b/cmd/crowdsec-cli/main.go @@ -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) } diff --git a/cmd/crowdsec/Makefile b/cmd/crowdsec/Makefile index ba795b11a..f18cd338c 100644 --- a/cmd/crowdsec/Makefile +++ b/cmd/crowdsec/Makefile @@ -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 diff --git a/cmd/crowdsec/main.go b/cmd/crowdsec/main.go index 1cd92c293..591f3a37b 100644 --- a/cmd/crowdsec/main.go +++ b/cmd/crowdsec/main.go @@ -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 } diff --git a/pkg/csconfig/config.go b/pkg/csconfig/config.go index 32f17cbf7..b226dde5b 100644 --- a/pkg/csconfig/config.go +++ b/pkg/csconfig/config.go @@ -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 { diff --git a/pkg/csconfig/config_test.go b/pkg/csconfig/config_test.go index 6f556245f..1ec1c8072 100644 --- a/pkg/csconfig/config_test.go +++ b/pkg/csconfig/config_test.go @@ -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") } diff --git a/test/bats/01_cscli.bats b/test/bats/01_cscli.bats index e96a9bc69..c765bf707 100644 --- a/test/bats/01_cscli.bats +++ b/test/bats/01_cscli.bats @@ -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 diff --git a/test/bin/assert-crowdsec-not-running b/test/bin/assert-crowdsec-not-running index 80de3dda7..b545ebf0a 100755 --- a/test/bin/assert-crowdsec-not-running +++ b/test/bin/assert-crowdsec-not-running @@ -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.