From e264b4dda9f6a5ae6357ed873001e57d05c1d53f Mon Sep 17 00:00:00 2001 From: Felipe <89969424+grafanalf@users.noreply.github.com> Date: Fri, 16 Jun 2023 16:36:12 +0200 Subject: [PATCH] chore: add support for native `sha256()` function (#881) * chore: add support for native `sha256()` function * Add documentation --- docs/docs/jsonnet/native-functions.md | 26 ++++++++++++++++++++++++++ pkg/jsonnet/native/funcs.go | 17 +++++++++++++++++ pkg/jsonnet/native/funcs_test.go | 8 ++++++++ 3 files changed, 51 insertions(+) diff --git a/docs/docs/jsonnet/native-functions.md b/docs/docs/jsonnet/native-functions.md index 38ed6dfb1..da7eb4b92 100644 --- a/docs/docs/jsonnet/native-functions.md +++ b/docs/docs/jsonnet/native-functions.md @@ -18,6 +18,32 @@ To use them in your code, you need to access them using `std.native` from the st `std.native` takes the native function's name as a `string` argument and returns a `function`, which is called using the second set of parentheses. +## sha256 + +### Signature + +```ts +sha256(string str) string +``` + +`sha256` computes the SHA256 sum of the given string. + +### Examples + +```jsonnet +{ + sum: std.native('sha256')('Hello, World!'), +} +``` + +Evaluating with Tanka results in the JSON: + +```json +{ + "sum": "dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f" +} +``` + ## parseJson ### Signature diff --git a/pkg/jsonnet/native/funcs.go b/pkg/jsonnet/native/funcs.go index e6ef0f066..d42fc33d3 100644 --- a/pkg/jsonnet/native/funcs.go +++ b/pkg/jsonnet/native/funcs.go @@ -2,7 +2,9 @@ package native import ( "bytes" + "crypto/sha256" "encoding/json" + "fmt" "io" "regexp" "strings" @@ -32,6 +34,9 @@ func Funcs() []*jsonnet.NativeFunction { regexMatch(), regexSubst(), + // Hash functions + hashSha256(), + helm.NativeFunc(helm.ExecHelm{}), kustomize.NativeFunc(kustomize.ExecKustomize{}), } @@ -50,6 +55,18 @@ func parseJSON() *jsonnet.NativeFunction { } } +func hashSha256() *jsonnet.NativeFunction { + return &jsonnet.NativeFunction{ + Name: "sha256", + Params: ast.Identifiers{"str"}, + Func: func(dataString []interface{}) (interface{}, error) { + h := sha256.New() + h.Write([]byte(dataString[0].(string))) + return fmt.Sprintf("%x", h.Sum(nil)), nil + }, + } +} + // parseYAML wraps `yaml.Unmarshal` to convert a string of yaml document(s) into a (set of) dicts func parseYAML() *jsonnet.NativeFunction { return &jsonnet.NativeFunction{ diff --git a/pkg/jsonnet/native/funcs_test.go b/pkg/jsonnet/native/funcs_test.go index 5cb8b70ed..afa13a8f3 100644 --- a/pkg/jsonnet/native/funcs_test.go +++ b/pkg/jsonnet/native/funcs_test.go @@ -21,6 +21,14 @@ func callNative(name string, data []interface{}) (res interface{}, err error, ca return nil, nil, fmt.Errorf("could not find native function %s", name) } +func TestSha256(t *testing.T) { + ret, err, callerr := callNative("sha256", []interface{}{"foo"}) + + assert.Empty(t, callerr) + assert.Equal(t, "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae", ret) + assert.Empty(t, err) +} + func TestParseJSONEmptyDict(t *testing.T) { ret, err, callerr := callNative("parseJson", []interface{}{"{}"})