1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-28 18:38:40 +00:00

Add SHA1 and MD5 hash functions to JMESPath (#11564)

Signed-off-by: ayu-ch <ayu.chandekar@gmail.com>
Co-authored-by: shuting <shuting@nirmata.com>
This commit is contained in:
Ayush Chandekar 2024-11-12 14:23:44 +05:30 committed by GitHub
parent 07f5a60fa3
commit e64a5ff647
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 78 additions and 0 deletions

View file

@ -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
}

View file

@ -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")
}