Be more strict, when parsing json

This commit is contained in:
Holger Obermaier 2023-10-13 09:53:49 +02:00
parent 595399e7d9
commit 0b509ca9e4
10 changed files with 45 additions and 23 deletions

View File

@ -1,6 +1,7 @@
package sinks
import (
"bytes"
"encoding/json"
"errors"
"fmt"
@ -94,9 +95,10 @@ func NewGangliaSink(name string, config json.RawMessage) (Sink, error) {
s.config.AddTagsAsDesc = false
s.config.AddGangliaGroup = false
if len(config) > 0 {
err := json.Unmarshal(config, &s.config)
if err != nil {
cclog.ComponentError(s.name, "Error reading config for", s.name, ":", err.Error())
d := json.NewDecoder(bytes.NewReader(config))
d.DisallowUnknownFields()
if err := d.Decode(&s.config); err != nil {
cclog.ComponentError(s.name, "Error reading config:", err.Error())
return nil, err
}
}

View File

@ -272,8 +272,10 @@ func NewHttpSink(name string, config json.RawMessage) (Sink, error) {
// Read config
if len(config) > 0 {
err := json.Unmarshal(config, &s.config)
if err != nil {
d := json.NewDecoder(bytes.NewReader(config))
d.DisallowUnknownFields()
if err := d.Decode(&s.config); err != nil {
cclog.ComponentError(s.name, "Error reading config:", err.Error())
return nil, err
}
}

View File

@ -1,6 +1,7 @@
package sinks
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
@ -180,8 +181,10 @@ func NewInfluxAsyncSink(name string, config json.RawMessage) (Sink, error) {
// 262144 524288
if len(config) > 0 {
err := json.Unmarshal(config, &s.config)
if err != nil {
d := json.NewDecoder(bytes.NewReader(config))
d.DisallowUnknownFields()
if err := d.Decode(&s.config); err != nil {
cclog.ComponentError(s.name, "Error reading config:", err.Error())
return nil, err
}
}

View File

@ -1,6 +1,7 @@
package sinks
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
@ -364,9 +365,11 @@ func NewInfluxSink(name string, config json.RawMessage) (Sink, error) {
// Read config
if len(config) > 0 {
err := json.Unmarshal(config, &s.config)
if err != nil {
return s, err
d := json.NewDecoder(bytes.NewReader(config))
d.DisallowUnknownFields()
if err := d.Decode(&s.config); err != nil {
cclog.ComponentError(s.name, "Error reading config:", err.Error())
return nil, err
}
}

View File

@ -66,6 +66,7 @@ void Ganglia_pool_destroy( Ganglia_pool pool );
import "C"
import (
"bytes"
"encoding/json"
"errors"
"fmt"
@ -233,8 +234,9 @@ func NewLibgangliaSink(name string, config json.RawMessage) (Sink, error) {
s.config.GmondConfig = string(GMOND_CONFIG_FILE)
s.config.GangliaLib = string(GANGLIA_LIB_NAME)
if len(config) > 0 {
err = json.Unmarshal(config, &s.config)
if err != nil {
d := json.NewDecoder(bytes.NewReader(config))
d.DisallowUnknownFields()
if err := d.Decode(&s.config); err != nil {
cclog.ComponentError(s.name, "Error reading config:", err.Error())
return nil, err
}

View File

@ -108,9 +108,10 @@ func NewNatsSink(name string, config json.RawMessage) (Sink, error) {
s.name = fmt.Sprintf("NatsSink(%s)", name)
s.flushDelay = 10 * time.Second
if len(config) > 0 {
err := json.Unmarshal(config, &s.config)
if err != nil {
cclog.ComponentError(s.name, "Error reading config for", s.name, ":", err.Error())
d := json.NewDecoder(bytes.NewReader(config))
d.DisallowUnknownFields()
if err := d.Decode(&s.config); err != nil {
cclog.ComponentError(s.name, "Error reading config:", err.Error())
return nil, err
}
}

View File

@ -1,6 +1,7 @@
package sinks
import (
"bytes"
"context"
"encoding/json"
"errors"
@ -167,9 +168,10 @@ func NewPrometheusSink(name string, config json.RawMessage) (Sink, error) {
s := new(PrometheusSink)
s.name = "PrometheusSink"
if len(config) > 0 {
err := json.Unmarshal(config, &s.config)
if err != nil {
cclog.ComponentError(s.name, "Error reading config for", s.name, ":", err.Error())
d := json.NewDecoder(bytes.NewReader(config))
d.DisallowUnknownFields()
if err := d.Decode(&s.config); err != nil {
cclog.ComponentError(s.name, "Error reading config:", err.Error())
return nil, err
}
}

View File

@ -1,6 +1,7 @@
package sinks
import (
"bytes"
"encoding/json"
"fmt"
"log"
@ -57,8 +58,10 @@ func NewSampleSink(name string, config json.RawMessage) (Sink, error) {
// Read in the config JSON
if len(config) > 0 {
err := json.Unmarshal(config, &s.config)
if err != nil {
d := json.NewDecoder(bytes.NewReader(config))
d.DisallowUnknownFields()
if err := d.Decode(&s.config); err != nil {
cclog.ComponentError(s.name, "Error reading config:", err.Error())
return nil, err
}
}

View File

@ -157,7 +157,7 @@ func (sm *sinkManager) AddOutput(name string, rawConfig json.RawMessage) error {
}
s, err := AvailableSinks[sinkConfig.Type](name, rawConfig)
if err != nil {
cclog.ComponentError("SinkManager", "SKIP", s.Name(), "initialization failed:", err.Error())
cclog.ComponentError("SinkManager", "SKIP", name, "initialization failed:", err.Error())
return err
}
sm.sinks[name] = s

View File

@ -1,12 +1,14 @@
package sinks
import (
"bytes"
"encoding/json"
"fmt"
"os"
"strings"
// "time"
cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
lp "github.com/ClusterCockpit/cc-metric-collector/pkg/ccMetric"
)
@ -42,8 +44,10 @@ func NewStdoutSink(name string, config json.RawMessage) (Sink, error) {
s := new(StdoutSink)
s.name = fmt.Sprintf("StdoutSink(%s)", name)
if len(config) > 0 {
err := json.Unmarshal(config, &s.config)
if err != nil {
d := json.NewDecoder(bytes.NewReader(config))
d.DisallowUnknownFields()
if err := d.Decode(&s.config); err != nil {
cclog.ComponentError(s.name, "Error reading config:", err.Error())
return nil, err
}
}