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

View File

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

View File

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

View File

@ -1,6 +1,7 @@
package sinks package sinks
import ( import (
"bytes"
"context" "context"
"crypto/tls" "crypto/tls"
"encoding/json" "encoding/json"
@ -364,9 +365,11 @@ func NewInfluxSink(name string, config json.RawMessage) (Sink, error) {
// Read config // Read config
if len(config) > 0 { if len(config) > 0 {
err := json.Unmarshal(config, &s.config) d := json.NewDecoder(bytes.NewReader(config))
if err != nil { d.DisallowUnknownFields()
return s, err 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 "C"
import ( import (
"bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@ -233,8 +234,9 @@ func NewLibgangliaSink(name string, config json.RawMessage) (Sink, error) {
s.config.GmondConfig = string(GMOND_CONFIG_FILE) s.config.GmondConfig = string(GMOND_CONFIG_FILE)
s.config.GangliaLib = string(GANGLIA_LIB_NAME) s.config.GangliaLib = string(GANGLIA_LIB_NAME)
if len(config) > 0 { if len(config) > 0 {
err = json.Unmarshal(config, &s.config) d := json.NewDecoder(bytes.NewReader(config))
if err != nil { d.DisallowUnknownFields()
if err := d.Decode(&s.config); err != nil {
cclog.ComponentError(s.name, "Error reading config:", err.Error()) cclog.ComponentError(s.name, "Error reading config:", err.Error())
return nil, err 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.name = fmt.Sprintf("NatsSink(%s)", name)
s.flushDelay = 10 * time.Second s.flushDelay = 10 * time.Second
if len(config) > 0 { if len(config) > 0 {
err := json.Unmarshal(config, &s.config) d := json.NewDecoder(bytes.NewReader(config))
if err != nil { d.DisallowUnknownFields()
cclog.ComponentError(s.name, "Error reading config for", s.name, ":", err.Error()) if err := d.Decode(&s.config); err != nil {
cclog.ComponentError(s.name, "Error reading config:", err.Error())
return nil, err return nil, err
} }
} }

View File

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

View File

@ -1,6 +1,7 @@
package sinks package sinks
import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
@ -57,8 +58,10 @@ func NewSampleSink(name string, config json.RawMessage) (Sink, error) {
// Read in the config JSON // Read in the config JSON
if len(config) > 0 { if len(config) > 0 {
err := json.Unmarshal(config, &s.config) d := json.NewDecoder(bytes.NewReader(config))
if err != nil { d.DisallowUnknownFields()
if err := d.Decode(&s.config); err != nil {
cclog.ComponentError(s.name, "Error reading config:", err.Error())
return nil, err 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) s, err := AvailableSinks[sinkConfig.Type](name, rawConfig)
if err != nil { if err != nil {
cclog.ComponentError("SinkManager", "SKIP", s.Name(), "initialization failed:", err.Error()) cclog.ComponentError("SinkManager", "SKIP", name, "initialization failed:", err.Error())
return err return err
} }
sm.sinks[name] = s sm.sinks[name] = s

View File

@ -1,12 +1,14 @@
package sinks package sinks
import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"os" "os"
"strings" "strings"
// "time" // "time"
cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
lp "github.com/ClusterCockpit/cc-metric-collector/pkg/ccMetric" 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 := new(StdoutSink)
s.name = fmt.Sprintf("StdoutSink(%s)", name) s.name = fmt.Sprintf("StdoutSink(%s)", name)
if len(config) > 0 { if len(config) > 0 {
err := json.Unmarshal(config, &s.config) d := json.NewDecoder(bytes.NewReader(config))
if err != nil { d.DisallowUnknownFields()
if err := d.Decode(&s.config); err != nil {
cclog.ComponentError(s.name, "Error reading config:", err.Error())
return nil, err return nil, err
} }
} }