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()) + } }