From 10fdae3cf6f2687b23904e50dc935486de579ef9 Mon Sep 17 00:00:00 2001 From: sj-williams Date: Fri, 6 Dec 2024 14:44:46 +0000 Subject: [PATCH] =?UTF-8?q?test:=20=F0=9F=92=8D=20check=20for=20secret=20j?= =?UTF-8?q?son=20missing=20data=20key?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/decodeSecret/decodeSecret.go | 5 ++++- pkg/decodeSecret/decodeSecret_test.go | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/pkg/decodeSecret/decodeSecret.go b/pkg/decodeSecret/decodeSecret.go index f4708399..efb504c3 100644 --- a/pkg/decodeSecret/decodeSecret.go +++ b/pkg/decodeSecret/decodeSecret.go @@ -77,7 +77,10 @@ func (sd *secretDecoder) processJson(jsn string, rawPrint bool) (string, error) return "", err } - data := result["data"].(map[string]interface{}) + data, valid := result["data"].(map[string]interface{}) + if !valid { + return "", errors.New("unable to decode secret, does it have a data key?") + } err = decodeKeys(data) if err != nil { diff --git a/pkg/decodeSecret/decodeSecret_test.go b/pkg/decodeSecret/decodeSecret_test.go index 9d572154..ed626e7d 100644 --- a/pkg/decodeSecret/decodeSecret_test.go +++ b/pkg/decodeSecret/decodeSecret_test.go @@ -106,4 +106,17 @@ func TestFormatJson(t *testing.T) { if err == nil { t.Fatal("Expected error and received nil") } + +} + +func TestJsonNoDataKey(t *testing.T) { + jsn := `{ "key1": "1", "key2": "2" }` + sd := secretDecoder{} + _, err := sd.processJson(jsn, false) + if err == nil { + t.Error("Expected an error when no data key is present") + } + if err.Error() != "unable to decode secret, does it have a data key?" { + t.Errorf("Expected error message 'unable to decode secret, does it have a data key?', got '%s'", err.Error()) + } }