Browse Source

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

mmetc 2 years ago
parent
commit
e1f5ed41df

+ 0 - 18
.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() }}

+ 1 - 0
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())

+ 24 - 0
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
+}

+ 3 - 1
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)
 		}

+ 1 - 1
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

+ 1 - 1
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
 	}

+ 4 - 4
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 {

+ 3 - 3
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")
 }
 

+ 10 - 0
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

+ 1 - 1
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.