inlet/snmp: fix parsing of inlet.snmp.communities as a string

Fix #116
This commit is contained in:
Vincent Bernat
2022-08-30 20:07:50 +02:00
parent e775ddff0a
commit 8eab4277c6
3 changed files with 29 additions and 1 deletions

View File

@@ -14,6 +14,7 @@ identified with a specific icon:
## Unreleased
- 🩹 *orchestrator*: validate configuration of other services on start
- 🩹 *inlet*: correctly parse `inlet.snmp.communities` when it is just a string
- 🌱 *inlet*: add `inlet.snmp.ports` to configure SNMP exporter ports
## 1.5.7 - 2022-08-23

View File

@@ -105,13 +105,17 @@ func ConfigurationUnmarshallerHook() mapstructure.DecodeHookFunc {
if mapKey == nil {
// Use the fact we can set the default value directly.
from.SetMapIndex(reflect.ValueOf("communities"), from.MapIndex(*defaultKey))
} else if communities.Kind() == reflect.String {
return nil, errors.New("do not provide default-community when using communities")
} else {
communities.SetMapIndex(reflect.ValueOf("::/0"), from.MapIndex(*defaultKey))
}
} else {
// default-community should contain ::/0
// communities should contain ::/0
if mapKey == nil {
from.SetMapIndex(reflect.ValueOf("communities"), reflect.ValueOf("public"))
} else if communities.Kind() == reflect.String {
// Do nothing
} else if !communities.MapIndex(reflect.ValueOf("::/0")).IsValid() {
communities.SetMapIndex(reflect.ValueOf("::/0"), reflect.ValueOf("public"))
}

View File

@@ -101,6 +101,29 @@ func TestConfigurationUnmarshallerHook(t *testing.T) {
"::ffff:203.0.113.128/121": "private",
}),
},
}, {
Description: "communities as a string",
Initial: func() interface{} { return Configuration{} },
Configuration: func() interface{} {
return gin.H{
"communities": "private",
}
},
Expected: Configuration{
Communities: helpers.MustNewSubnetMap(map[string]string{
"::/0": "private",
}),
},
}, {
Description: "communities as a string, default-community",
Initial: func() interface{} { return Configuration{} },
Configuration: func() interface{} {
return gin.H{
"default-community": "nothing",
"communities": "private",
}
},
Error: true,
}, {
Description: "communities, default-community empty",
Initial: func() interface{} { return Configuration{} },