Browse Source

feat: non standard templating delimiters (#4558)

Signed-off-by: Gustavo Carvalho <gusfcarvalho@gmail.com>
Gustavo Fernandes de Carvalho 1 year ago
parent
commit
505bea6d56
2 changed files with 34 additions and 0 deletions
  1. 11 0
      pkg/template/v2/template.go
  2. 23 0
      pkg/template/v2/template_test.go

+ 11 - 0
pkg/template/v2/template.go

@@ -20,10 +20,12 @@ import (
 	tpl "text/template"
 	tpl "text/template"
 
 
 	"github.com/Masterminds/sprig/v3"
 	"github.com/Masterminds/sprig/v3"
+	"github.com/spf13/pflag"
 	corev1 "k8s.io/api/core/v1"
 	corev1 "k8s.io/api/core/v1"
 	"k8s.io/apimachinery/pkg/util/yaml"
 	"k8s.io/apimachinery/pkg/util/yaml"
 
 
 	esapi "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
 	esapi "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
+	"github.com/external-secrets/external-secrets/pkg/feature"
 )
 )
 
 
 var tplFuncs = tpl.FuncMap{
 var tplFuncs = tpl.FuncMap{
@@ -47,6 +49,8 @@ var tplFuncs = tpl.FuncMap{
 	"fromYaml": fromYAML,
 	"fromYaml": fromYAML,
 }
 }
 
 
+var leftDelim, rightDelim string
+
 // So other templating calls can use the same extra functions.
 // So other templating calls can use the same extra functions.
 func FuncMap() tpl.FuncMap {
 func FuncMap() tpl.FuncMap {
 	return tplFuncs
 	return tplFuncs
@@ -71,6 +75,12 @@ func init() {
 	for k, v := range sprigFuncs {
 	for k, v := range sprigFuncs {
 		tplFuncs[k] = v
 		tplFuncs[k] = v
 	}
 	}
+	fs := pflag.NewFlagSet("template", pflag.ExitOnError)
+	fs.StringVar(&leftDelim, "template-left-delimiter", "{{", "templating left delimiter")
+	fs.StringVar(&rightDelim, "template-right-delimiter", "}}", "templating right delimiter")
+	feature.Register(feature.Feature{
+		Flags: fs,
+	})
 }
 }
 
 
 func applyToTarget(k, val string, target esapi.TemplateTarget, secret *corev1.Secret) {
 func applyToTarget(k, val string, target esapi.TemplateTarget, secret *corev1.Secret) {
@@ -154,6 +164,7 @@ func execute(k, val string, data map[string][]byte) ([]byte, error) {
 	t, err := tpl.New(k).
 	t, err := tpl.New(k).
 		Option("missingkey=error").
 		Option("missingkey=error").
 		Funcs(tplFuncs).
 		Funcs(tplFuncs).
+		Delims("{{", "}}").
 		Parse(val)
 		Parse(val)
 	if err != nil {
 	if err != nil {
 		return nil, fmt.Errorf(errParse, k, err)
 		return nil, fmt.Errorf(errParse, k, err)

+ 23 - 0
pkg/template/v2/template_test.go

@@ -150,6 +150,8 @@ func TestExecute(t *testing.T) {
 		expectedStringData  map[string]string
 		expectedStringData  map[string]string
 		expectedLabels      map[string]string
 		expectedLabels      map[string]string
 		expectedAnnotations map[string]string
 		expectedAnnotations map[string]string
+		leftDelimiter       string
+		rightDelimiter      string
 		expErr              string
 		expErr              string
 		expLblErr           string
 		expLblErr           string
 		expAnnoErr          string
 		expAnnoErr          string
@@ -488,6 +490,21 @@ func TestExecute(t *testing.T) {
 				"foo": "1234",
 				"foo": "1234",
 			},
 			},
 		},
 		},
+		{
+			name: "NonStandardDelimiters",
+			stringDataTpl: map[string][]byte{
+				"foo": []byte("<< .secret | b64dec >>"),
+			},
+			leftDelimiter:  "<<",
+			rightDelimiter: ">>",
+			data: map[string][]byte{
+				"secret": []byte("MTIzNA=="),
+				"env":    []byte("ZGV2"),
+			},
+			expectedStringData: map[string]string{
+				"foo": "1234",
+			},
+		},
 	}
 	}
 
 
 	for i := range tbl {
 	for i := range tbl {
@@ -498,6 +515,12 @@ func TestExecute(t *testing.T) {
 				StringData: make(map[string]string),
 				StringData: make(map[string]string),
 				ObjectMeta: v1.ObjectMeta{Labels: make(map[string]string), Annotations: make(map[string]string)},
 				ObjectMeta: v1.ObjectMeta{Labels: make(map[string]string), Annotations: make(map[string]string)},
 			}
 			}
+			if row.leftDelimiter != "" {
+				leftDelim = row.leftDelimiter
+			}
+			if row.rightDelimiter != "" {
+				rightDelim = row.rightDelimiter
+			}
 			err := Execute(row.tpl, row.data, esapi.TemplateScopeValues, esapi.TemplateTargetData, sec)
 			err := Execute(row.tpl, row.data, esapi.TemplateScopeValues, esapi.TemplateTargetData, sec)
 			if !ErrorContains(err, row.expErr) {
 			if !ErrorContains(err, row.expErr) {
 				t.Errorf("unexpected error: %s, expected: %s", err, row.expErr)
 				t.Errorf("unexpected error: %s, expected: %s", err, row.expErr)