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..1c62088c 100644 --- a/pkg/decodeSecret/decodeSecret_test.go +++ b/pkg/decodeSecret/decodeSecret_test.go @@ -106,4 +106,18 @@ 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{} + expectedMsg := "unable to decode secret, does it have a data key?" + _, err := sd.processJson(jsn, false) + if err == nil { + t.Error("Expected an error when no data key is present") + } + if err.Error() != expectedMsg { + t.Errorf("Expected error message '%s', got '%s'", expectedMsg, err.Error()) + } }