diff --git a/pkg/engine/jmespath/functions.go b/pkg/engine/jmespath/functions.go index fd240bf41a..07585048b9 100644 --- a/pkg/engine/jmespath/functions.go +++ b/pkg/engine/jmespath/functions.go @@ -2,8 +2,10 @@ package jmespath import ( "bytes" + "crypto/md5" // #nosec G501 "crypto/rand" "crypto/rsa" + "crypto/sha1" // #nosec G505 "crypto/sha256" "crypto/x509" "encoding/asn1" @@ -79,6 +81,8 @@ var ( imageNormalize = "image_normalize" isExternalURL = "is_external_url" SHA256 = "sha256" + SHA1 = "sha1" + MD5 = "md5" ) func GetFunctions(configuration config.Configuration) []FunctionEntry { @@ -606,6 +610,26 @@ func GetFunctions(configuration config.Configuration) []FunctionEntry { }, ReturnType: []jpType{jpString}, Note: "generate unique resources name if length exceeds the limit", + }, { + FunctionEntry: gojmespath.FunctionEntry{ + Name: SHA1, + Arguments: []argSpec{ + {Types: []jpType{jpString}}, + }, + Handler: jpSha1, + }, + ReturnType: []jpType{jpString}, + Note: "generate a SHA-1 hash", + }, { + FunctionEntry: gojmespath.FunctionEntry{ + Name: MD5, + Arguments: []argSpec{ + {Types: []jpType{jpString}}, + }, + Handler: jpMd5, + }, + ReturnType: []jpType{jpString}, + Note: "generates an MD5 hash", }} } @@ -1289,3 +1313,33 @@ func jpSha256(arguments []interface{}) (interface{}, error) { hashedBytes := hasher.Sum(nil) return hex.EncodeToString(hashedBytes), nil } + +func jpSha1(arguments []interface{}) (interface{}, error) { + var err error + str, err := validateArg("", arguments, 0, reflect.String) + if err != nil { + return nil, err + } + hasher := sha1.New() // #nosec G401 + _, err = hasher.Write([]byte(str.String())) + if err != nil { + return "", err + } + hashedBytes := hasher.Sum(nil) + return hex.EncodeToString(hashedBytes), nil +} + +func jpMd5(arguments []interface{}) (interface{}, error) { + var err error + str, err := validateArg("", arguments, 0, reflect.String) + if err != nil { + return nil, err + } + hasher := md5.New() // #nosec G401 + _, err = hasher.Write([]byte(str.String())) + if err != nil { + return "", err + } + hashedBytes := hasher.Sum(nil) + return hex.EncodeToString(hashedBytes), nil +} diff --git a/pkg/engine/jmespath/functions_test.go b/pkg/engine/jmespath/functions_test.go index 8cb05735ea..f95f863499 100644 --- a/pkg/engine/jmespath/functions_test.go +++ b/pkg/engine/jmespath/functions_test.go @@ -1745,3 +1745,27 @@ func Test_SHA256(t *testing.T) { assert.Assert(t, ok) assert.Equal(t, str, "75c07bb807f2d80a85d34880b8af0c5f29f7c27577076ed5d0e4b427dee7dbcc") } + +func Test_SHA1(t *testing.T) { + jp, err := jmespathInterface.Query("sha1('Alice & Bob')") + assert.NilError(t, err) + + result, err := jp.Search("") + assert.NilError(t, err) + + str, ok := result.(string) + assert.Assert(t, ok) + assert.Equal(t, str, "e3ec484930685a61b0f824cd684a68699d2b98c1") +} + +func Test_MD5(t *testing.T) { + jp, err := jmespathInterface.Query("md5('Alice & Bob')") + assert.NilError(t, err) + + result, err := jp.Search("") + assert.NilError(t, err) + + str, ok := result.(string) + assert.Assert(t, ok) + assert.Equal(t, str, "def42e1abd2462df1f9f0a4b3d488221") +}