-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Define a TelemetryBlob type to hold JSON blobs
The TelemetryBlob type provides helper methods that can be used to validate that provided blobs are: * Valid JSON * JSON objects * Contain a top-level "version" field * Are not too big. The validity of provided JSON blobs is checked when they are received via a Generate() interface. Also update client side item handling to leverage the json.RawMessage type for storing the JSON blobs; this avoid undesirable processing of the provided JSON blob data, which should remain untouched en-route to long term storage in the SUSE Telemetry service. Minor restructuring of the client side library, moving limits to it's own subpackage to avoid an import loop when adding the CheckLimits() helper method to the TelemetryBlob. Fixes: #41, #26
- Loading branch information
Showing
7 changed files
with
124 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package telemetrylib | ||
package limits | ||
|
||
import ( | ||
"errors" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package types | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log/slog" | ||
|
||
"github.com/SUSE/telemetry/pkg/limits" | ||
) | ||
|
||
type TelemetryBlob struct { | ||
bytes []byte | ||
} | ||
|
||
func NewTelemetryBlob(jsonBlob []byte) *TelemetryBlob { | ||
return &TelemetryBlob{bytes: jsonBlob} | ||
} | ||
|
||
func (tb *TelemetryBlob) String() string { | ||
return string(tb.bytes) | ||
} | ||
|
||
func (tb *TelemetryBlob) Bytes() []byte { | ||
return tb.bytes | ||
} | ||
|
||
func (tb *TelemetryBlob) errNotValidJson() error { | ||
return fmt.Errorf("not valid JSON blob") | ||
} | ||
|
||
func (tb *TelemetryBlob) errNotJsonObject(err error) error { | ||
return fmt.Errorf("not a JSON object: %s", err.Error()) | ||
} | ||
|
||
func (tb *TelemetryBlob) errNotVersionedObject() error { | ||
return fmt.Errorf("missing 'version' field in JSON object") | ||
} | ||
|
||
func (tb *TelemetryBlob) validJson() bool { | ||
return json.Valid(tb.Bytes()) | ||
} | ||
|
||
func (tb *TelemetryBlob) Valid() error { | ||
var data map[string]any | ||
|
||
if !tb.validJson() { | ||
return tb.errNotValidJson() | ||
} | ||
|
||
if err := json.Unmarshal(tb.Bytes(), &data); err != nil { | ||
slog.Debug( | ||
"Not a valid JSON object", | ||
slog.String("blob", tb.String()), | ||
slog.String("error", err.Error()), | ||
) | ||
newErr := tb.errNotJsonObject(err) | ||
return newErr | ||
} | ||
|
||
if _, found := data["version"]; !found { | ||
slog.Debug("Not a valid JSON object", slog.String("blob", tb.String())) | ||
return tb.errNotVersionedObject() | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (tb *TelemetryBlob) CheckLimits() error { | ||
return limits.NewTelemetryDataLimits().CheckLimits(tb.Bytes()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters