1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-30 19:35:06 +00:00

support for SHA256 jmespath function (#9144)

Signed-off-by: Kanha gupta <kanhag4163@gmail.com>
Co-authored-by: Vishal Choudhary <vishal.choudhary@nirmata.com>
This commit is contained in:
kanha gupta 2024-01-05 16:14:26 +05:30 committed by GitHub
parent c2e388a71c
commit f7a962fd11
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 0 deletions

View file

@ -4,9 +4,11 @@ import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/asn1"
"encoding/base64"
"encoding/hex"
"encoding/json"
"encoding/pem"
"errors"
@ -76,6 +78,7 @@ var (
x509_decode = "x509_decode"
imageNormalize = "image_normalize"
isExternalURL = "is_external_url"
SHA256 = "sha256"
)
func GetFunctions(configuration config.Configuration) []FunctionEntry {
@ -593,6 +596,16 @@ func GetFunctions(configuration config.Configuration) []FunctionEntry {
},
ReturnType: []jpType{jpBool},
Note: "determine if a URL points to an external network address",
}, {
FunctionEntry: gojmespath.FunctionEntry{
Name: SHA256,
Arguments: []argSpec{
{Types: []jpType{jpString}},
},
Handler: jpSha256,
},
ReturnType: []jpType{jpString},
Note: "generate unique resources name if length exceeds the limit",
}}
}
@ -1261,3 +1274,18 @@ func jpIsExternalURL(arguments []interface{}) (interface{}, error) {
}
return true, nil
}
func jpSha256(arguments []interface{}) (interface{}, error) {
var err error
str, err := validateArg("", arguments, 0, reflect.String)
if err != nil {
return nil, err
}
hasher := sha256.New()
_, err = hasher.Write([]byte(str.String()))
if err != nil {
return "", err
}
hashedBytes := hasher.Sum(nil)
return hex.EncodeToString(hashedBytes), nil
}

View file

@ -1733,3 +1733,15 @@ func Test_IsExternalURL(t *testing.T) {
})
}
}
func Test_SHA256(t *testing.T) {
jp, err := jmespathInterface.Query("sha256('alertmanager-kube-prometheus-stack-alertmanager')")
assert.NilError(t, err)
result, err := jp.Search("")
assert.NilError(t, err)
str, ok := result.(string)
assert.Assert(t, ok)
assert.Equal(t, str, "75c07bb807f2d80a85d34880b8af0c5f29f7c27577076ed5d0e4b427dee7dbcc")
}