Skip to content

Commit

Permalink
Merge pull request #668 from ministryofjustice/check-secret-data
Browse files Browse the repository at this point in the history
test: 💍 check for secret json missing data key
  • Loading branch information
sj-williams authored Dec 6, 2024
2 parents 23288f3 + a6e1280 commit 47b420a
Show file tree
Hide file tree
Showing 2 changed files with 18 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
14 changes: 14 additions & 0 deletions pkg/decodeSecret/decodeSecret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

0 comments on commit 47b420a

Please sign in to comment.