From 979c8f2835ac848cc62608fb3bbf5e6b0326dacd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Torrero=20Marijnissen?= Date: Thu, 28 Sep 2023 14:36:05 +0100 Subject: [PATCH 1/2] Force error for invalid JSON before publishing through collector --- internal/discovery/saptune.go | 36 +++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/internal/discovery/saptune.go b/internal/discovery/saptune.go index bae5ab6d..cbbf9ddf 100644 --- a/internal/discovery/saptune.go +++ b/internal/discovery/saptune.go @@ -20,9 +20,9 @@ type SaptuneDiscovery struct { } type SaptuneDiscoveryPayload struct { - PackageVersion string `json:"package_version"` - SaptuneInstalled bool `json:"saptune_installed"` - Status interface{} `json:"status"` + PackageVersion string `json:"package_version"` + SaptuneInstalled bool `json:"saptune_installed"` + Status json.RawMessage `json:"status"` } func NewSaptuneDiscovery(collectorClient collector.Client, config DiscoveriesConfig) Discovery { @@ -60,21 +60,16 @@ func (d SaptuneDiscovery) Discover() (string, error) { } default: saptuneData, _ := saptuneRetriever.RunCommandJSON("status") - unmarshalled := make(map[string]interface{}) - err = json.Unmarshal(saptuneData, &unmarshalled) - if err != nil { - log.Debugf("Error while unmarshalling saptune status: %s", err) - saptunePayload = SaptuneDiscoveryPayload{ - PackageVersion: saptuneRetriever.Version, - SaptuneInstalled: true, - Status: nil, - } - } else { - saptunePayload = SaptuneDiscoveryPayload{ - PackageVersion: saptuneRetriever.Version, - SaptuneInstalled: true, - Status: unmarshalled, - } + + if !isValidJSON(saptuneData) { + saptuneData = nil + log.Error("Error while parsing saptune status JSON") + } + + saptunePayload = SaptuneDiscoveryPayload{ + PackageVersion: saptuneRetriever.Version, + SaptuneInstalled: true, + Status: saptuneData, } } @@ -86,3 +81,8 @@ func (d SaptuneDiscovery) Discover() (string, error) { return "Saptune data discovery completed", nil } + +func isValidJSON(data json.RawMessage) bool { + var i interface{} + return json.Unmarshal(data, &i) == nil +} From 74e412805bbaae57a396f6c23c415620f6826fdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Torrero=20Marijnissen?= Date: Mon, 2 Oct 2023 14:38:45 +0100 Subject: [PATCH 2/2] Return the error from json.Unmarshal --- internal/discovery/saptune.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/internal/discovery/saptune.go b/internal/discovery/saptune.go index cbbf9ddf..378d1cce 100644 --- a/internal/discovery/saptune.go +++ b/internal/discovery/saptune.go @@ -61,9 +61,9 @@ func (d SaptuneDiscovery) Discover() (string, error) { default: saptuneData, _ := saptuneRetriever.RunCommandJSON("status") - if !isValidJSON(saptuneData) { + if ok, err := isValidJSON(saptuneData); !ok { saptuneData = nil - log.Error("Error while parsing saptune status JSON") + log.Error("Error while parsing saptune status JSON: %w", err) } saptunePayload = SaptuneDiscoveryPayload{ @@ -82,7 +82,8 @@ func (d SaptuneDiscovery) Discover() (string, error) { return "Saptune data discovery completed", nil } -func isValidJSON(data json.RawMessage) bool { +func isValidJSON(data json.RawMessage) (bool, error) { var i interface{} - return json.Unmarshal(data, &i) == nil + err := json.Unmarshal(data, &i) + return err == nil, err }