Skip to content

Commit

Permalink
fix to detect invalid logger type in config (#573)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmachard authored Jan 27, 2024
1 parent 8fab14e commit a0ceb9f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkgconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,9 @@ func checkMultiplexerConfig(currentVal reflect.Value, currentRef []interface{},

// Type assertion to check if the value is a map
if value, ok := mapVal.Interface().(map[string]interface{}); ok {
if _, ok := refMap[strKey]; !ok {
return errors.Errorf("invalid logger `%s`", strKey)
}
if err := checkKeywordsPosition(value, refMap[strKey].(map[string]interface{}), defaultConf, nextSectionName); err != nil {
return err
}
Expand Down
38 changes: 38 additions & 0 deletions pkgconfig/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,44 @@ multiplexer:
}
}

// https://github.com/dmachard/go-dnscollector/issues/565
func TestConfig_CheckMultiplexerConfig_InvalidLogger(t *testing.T) {
userConfigFile, err := os.CreateTemp("", "user-config.yaml")
if err != nil {
t.Fatal("Error creating temporary file:", err)
}
defer os.Remove(userConfigFile.Name())
defer userConfigFile.Close()

// all keywords in this config are valid but the logger dnstap is not valid in this context
userConfigContent := `
global:
trace: false
multiplexer:
collectors:
- name: tap
dnstap:
listen-ip: 0.0.0.0
loggers:
- name: tapOut
dnstap:
listen-ip: 0.0.0.0
routes:
- from: [ tapIn ]
to: [ tapOut ]
`

err = os.WriteFile(userConfigFile.Name(), []byte(userConfigContent), 0644)
if err != nil {
t.Fatal("Error writing to user configuration file:", err)
}

dm := make(map[string]interface{})
if err := CheckConfig(userConfigFile.Name(), dm); err == nil {
t.Errorf("Expected error, but got %v", err)
}
}

// Valid pipeline configuration
func TestConfig_CheckPipelinesConfig_Valid(t *testing.T) {
userConfigFile, err := os.CreateTemp("", "user-config.yaml")
Expand Down

0 comments on commit a0ceb9f

Please sign in to comment.