Selaa lähdekoodia

feat(templating): add hexdec function (#6422)

Co-authored-by: Gergely Bräutigam <gergely.brautigam@sap.com>
Signed-off-by: Tristan Stenner <ts@ppi.de>
Tristan Stenner 1 viikko sitten
vanhempi
sitoutus
94259afd10

+ 1 - 0
docs/guides/templating.md

@@ -222,6 +222,7 @@ In addition to that you can use over 200+ [sprig functions](http://masterminds.g
 | rsaDecrypt | Decrypts RSA ciphertext using a PEM private key. Usage: ``<rsaDecrypt "SCHEME" "HASH" ciphertext privateKeyPEM>`` or ``<privateKeyPEM \| rsaDecrypt "SCHEME" "HASH" ciphertext>``. **SCHEME**: supported values are `"None"` and `"RSA-OAEP"`. **HASH**: supported values are `"SHA1"` and `"SHA256"`. **Ciphertext** must be binary — use `b64dec` or `decodingStrategy: Base64` to convert Base64 payloads. |
 | toYaml           | Takes an interface, marshals it to yaml. It returns a string, even on marshal error (empty string).                                                                                                                          |
 | fromYaml         | Function converts a YAML document into a map[string]any.                                                                                                                                                             |
+| hexdec           | decodes hexadecimal values                                                                                                                                                                                                   |
 
 ## Migrating from v1
 

+ 1 - 0
runtime/template/v2/sprig/functions.go

@@ -204,6 +204,7 @@ var genericMap = map[string]interface{}{
 	"b64dec": base64decode,
 	"b32enc": base32encode,
 	"b32dec": base32decode,
+	"hexdec": hexdecode,
 
 	// Data Structures:
 	"tuple":              list,

+ 9 - 0
runtime/template/v2/sprig/strings.go

@@ -3,6 +3,7 @@ package sprig
 import (
 	"encoding/base32"
 	"encoding/base64"
+	"encoding/hex"
 	"fmt"
 	"reflect"
 	"strconv"
@@ -35,6 +36,14 @@ func base32decode(v string) string {
 	return string(data)
 }
 
+func hexdecode(v string) string {
+	data, err := hex.DecodeString(v)
+	if err != nil {
+		return err.Error()
+	}
+	return string(data)
+}
+
 func abbrev(width int, s string) string {
 	if width < 4 {
 		return s

+ 12 - 0
runtime/template/v2/template_test.go

@@ -425,6 +425,18 @@ func TestExecute(t *testing.T) {
 			},
 			expErr: "", // silent error
 		},
+		{
+			name: "hexdec",
+			tpl: map[string][]byte{
+				"key": []byte(`{{ .example | sha256sum | hexdec | b64enc }}`),
+			},
+			data: map[string][]byte{
+				"example": []byte("example"),
+			},
+			expectedData: map[string][]byte{
+				"key": []byte("UNhY4JhezH9gQYqvDMWrWH9CwlcKiECVqejMrND2VFw="),
+			},
+		},
 		{
 			name: "pkcs12 key wrong password",
 			tpl: map[string][]byte{