Skip to content

Commit

Permalink
test: 💍 check for secret json missing data key
Browse files Browse the repository at this point in the history
  • Loading branch information
sj-williams committed Dec 6, 2024
1 parent 23288f3 commit 10fdae3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pkg/decodeSecret/decodeSecret.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions pkg/decodeSecret/decodeSecret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

0 comments on commit 10fdae3

Please sign in to comment.