From 8c9b12889557cf8bd50567819e04df7f26f8fd1c Mon Sep 17 00:00:00 2001 From: Megan Wolf Date: Fri, 20 Sep 2024 08:19:43 -0400 Subject: [PATCH] fix: small refactor on template render --- src/cmd/tools/template.go | 4 +- src/internal/template/template.go | 49 ++++++++---------- src/internal/template/template_test.go | 72 +++++++++++++------------- 3 files changed, 59 insertions(+), 66 deletions(-) diff --git a/src/cmd/tools/template.go b/src/cmd/tools/template.go index c4b47c37..55c8b512 100644 --- a/src/cmd/tools/template.go +++ b/src/cmd/tools/template.go @@ -84,8 +84,8 @@ func TemplateCommand() *cobra.Command { return fmt.Errorf("error collecting templating data: %v", err) } - templateRenderer := template.NewTemplateRenderer(string(data), renderType, templateData) - output, err := templateRenderer.Render() + templateRenderer := template.NewTemplateRenderer(string(data), templateData) + output, err := templateRenderer.Render(renderType) if err != nil { return fmt.Errorf("error rendering template: %v", err) } diff --git a/src/internal/template/template.go b/src/internal/template/template.go index a9ff32a4..5bae9f57 100644 --- a/src/internal/template/template.go +++ b/src/internal/template/template.go @@ -27,26 +27,20 @@ const ( type TemplateRenderer struct { tpl *template.Template - TemplateString string - *TemplateData - RenderType + templateString string + templateData *TemplateData } -func NewTemplateRenderer(templateString string, renderType RenderType, templateData *TemplateData) *TemplateRenderer { +func NewTemplateRenderer(templateString string, templateData *TemplateData) *TemplateRenderer { return &TemplateRenderer{ tpl: createTemplate(), - TemplateString: templateString, - RenderType: renderType, - TemplateData: templateData, + templateString: templateString, + templateData: templateData, } } -func (r *TemplateRenderer) SetRenderType(t RenderType) { - r.RenderType = t -} - -func (r *TemplateRenderer) Render() ([]byte, error) { - switch r.RenderType { +func (r *TemplateRenderer) Render(t RenderType) ([]byte, error) { + switch t { case MASKED: return r.ExecuteMaskedTemplate() case CONSTANTS: @@ -82,16 +76,15 @@ type VariableConfig struct { // ExecuteFullTemplate templates everything func (r *TemplateRenderer) ExecuteFullTemplate() ([]byte, error) { - templateString := r.TemplateString - tpl, err := r.tpl.Parse(templateString) + tpl, err := r.tpl.Parse(r.templateString) if err != nil { return []byte{}, err } var buffer strings.Builder - allVars := concatStringMaps(r.TemplateData.Variables, r.TemplateData.SensitiveVariables) + allVars := concatStringMaps(r.templateData.Variables, r.templateData.SensitiveVariables) err = tpl.Execute(&buffer, map[string]interface{}{ - CONST: r.TemplateData.Constants, + CONST: r.templateData.Constants, VAR: allVars}) if err != nil { return []byte{}, err @@ -105,16 +98,16 @@ func (r *TemplateRenderer) ExecuteFullTemplate() ([]byte, error) { func (r *TemplateRenderer) ExecuteConstTemplate() ([]byte, error) { // Find anything {{ var.KEY }} and replace with {{ "{{ var.KEY }}" }} re := regexp.MustCompile(`{{\s*\.` + VAR + `\.([a-zA-Z0-9_]+)\s*}}`) - templateString := re.ReplaceAllString(r.TemplateString, "{{ \"{{ ."+VAR+".$1 }}\" }}") + templateStringReplaced := re.ReplaceAllString(r.templateString, "{{ \"{{ ."+VAR+".$1 }}\" }}") - tpl, err := r.tpl.Parse(templateString) + tpl, err := r.tpl.Parse(templateStringReplaced) if err != nil { return []byte{}, err } var buffer strings.Builder err = tpl.Execute(&buffer, map[string]interface{}{ - CONST: r.TemplateData.Constants}) + CONST: r.templateData.Constants}) if err != nil { return []byte{}, err } @@ -126,12 +119,12 @@ func (r *TemplateRenderer) ExecuteConstTemplate() ([]byte, error) { // used for compose operations func (r *TemplateRenderer) ExecuteNonSensitiveTemplate() ([]byte, error) { // Find any sensitive keys {{ var.KEY }}, where KEY is in templateData.SensitiveVariables and replace with {{ "{{ var.KEY }}" }} - templateString := r.TemplateString + templateString := r.templateString re := regexp.MustCompile(`{{\s*\.` + VAR + `\.([a-zA-Z0-9_]+)\s*}}`) varMatches := re.FindAllStringSubmatch(templateString, -1) uniqueMatches := returnUniqueMatches(varMatches, 1) for k, matches := range uniqueMatches { - if _, ok := r.TemplateData.SensitiveVariables[matches[0]]; ok { + if _, ok := r.templateData.SensitiveVariables[matches[0]]; ok { templateString = strings.ReplaceAll(templateString, k, "{{ \""+k+"\" }}") } } @@ -143,8 +136,8 @@ func (r *TemplateRenderer) ExecuteNonSensitiveTemplate() ([]byte, error) { var buffer strings.Builder err = tpl.Execute(&buffer, map[string]interface{}{ - CONST: r.TemplateData.Constants, - VAR: r.TemplateData.Variables}) + CONST: r.templateData.Constants, + VAR: r.templateData.Variables}) if err != nil { return []byte{}, err } @@ -156,12 +149,12 @@ func (r *TemplateRenderer) ExecuteNonSensitiveTemplate() ([]byte, error) { // for display/printing only func (r *TemplateRenderer) ExecuteMaskedTemplate() ([]byte, error) { // Find any sensitive keys {{ var.KEY }}, where KEY is in templateData.SensitiveVariables and replace with {{ var.KEY | mask }} - templateString := r.TemplateString + templateString := r.templateString re := regexp.MustCompile(`{{\s*\.` + VAR + `\.([a-zA-Z0-9_]+)\s*}}`) varMatches := re.FindAllStringSubmatch(templateString, -1) uniqueMatches := returnUniqueMatches(varMatches, 1) for k, matches := range uniqueMatches { - if _, ok := r.TemplateData.SensitiveVariables[matches[0]]; ok { + if _, ok := r.templateData.SensitiveVariables[matches[0]]; ok { templateString = strings.ReplaceAll(templateString, k, "********") } } @@ -173,8 +166,8 @@ func (r *TemplateRenderer) ExecuteMaskedTemplate() ([]byte, error) { var buffer strings.Builder err = tpl.Execute(&buffer, map[string]interface{}{ - CONST: r.TemplateData.Constants, - VAR: r.TemplateData.Variables}) + CONST: r.templateData.Constants, + VAR: r.templateData.Variables}) if err != nil { return []byte{}, err } diff --git a/src/internal/template/template_test.go b/src/internal/template/template_test.go index 4a917fbb..963dd543 100644 --- a/src/internal/template/template_test.go +++ b/src/internal/template/template_test.go @@ -10,10 +10,10 @@ import ( "github.com/defenseunicorns/lula/src/internal/template" ) -func testRender(t *testing.T, templateRenderer *template.TemplateRenderer, expected string) error { +func testRender(t *testing.T, templateRenderer *template.TemplateRenderer, renderType template.RenderType, expected string) error { t.Helper() - got, err := templateRenderer.Render() + got, err := templateRenderer.Render(renderType) if err != nil { return fmt.Errorf("error templating data: %v\n", err.Error()) } @@ -50,8 +50,8 @@ func TestExecuteFullTemplate(t *testing.T) { secret template: my-secret ` - tr := template.NewTemplateRenderer(templateString, template.ALL, templateData) - err := testRender(t, tr, expected) + tr := template.NewTemplateRenderer(templateString, templateData) + err := testRender(t, tr, template.ALL, expected) if err != nil { t.Fatalf("Expected no error, but got %v", err) } @@ -66,8 +66,8 @@ func TestExecuteFullTemplate(t *testing.T) { secret template: {{ .var.some_lula_secret }} ` - tr := template.NewTemplateRenderer(templateString, template.ALL, templateData) - err := testRender(t, tr, "") + tr := template.NewTemplateRenderer(templateString, templateData) + err := testRender(t, tr, template.ALL, "") if err == nil { t.Fatalf("Expected an error, but got nil") } @@ -81,8 +81,8 @@ func TestExecuteFullTemplate(t *testing.T) { constant template: {{ .constant.testVar }} ` - tr := template.NewTemplateRenderer(templateString, template.ALL, templateData) - err := testRender(t, tr, "") + tr := template.NewTemplateRenderer(templateString, templateData) + err := testRender(t, tr, template.ALL, "") if err == nil { t.Fatal("expected error, got nil") } @@ -105,8 +105,8 @@ func TestExecuteFullTemplate(t *testing.T) { constant template: {{ .const.test-var }} ` - tr := template.NewTemplateRenderer(templateString, template.ALL, templateData) - err := testRender(t, tr, "") + tr := template.NewTemplateRenderer(templateString, templateData) + err := testRender(t, tr, template.ALL, "") if err == nil { t.Fatal("expected error, got nil") } @@ -120,8 +120,8 @@ func TestExecuteFullTemplate(t *testing.T) { variable template: {{ .var.nokey.sub }} ` - tr := template.NewTemplateRenderer(templateString, template.ALL, templateData) - err := testRender(t, tr, "") + tr := template.NewTemplateRenderer(templateString, templateData) + err := testRender(t, tr, template.ALL, "") if err == nil { t.Fatal("expected error, got nil") } @@ -133,8 +133,8 @@ func TestExecuteFullTemplate(t *testing.T) { templateString := ` constant template: {{ constant.testVar }} ` - tr := template.NewTemplateRenderer(templateString, template.ALL, templateData) - err := testRender(t, tr, "") + tr := template.NewTemplateRenderer(templateString, templateData) + err := testRender(t, tr, template.ALL, "") if err == nil { t.Fatal("expected error, got nil") } @@ -153,8 +153,8 @@ func TestExecuteFullTemplate(t *testing.T) { expected := ` constant template: "one", "two", "three" ` - tr := template.NewTemplateRenderer(templateString, template.ALL, templateData) - err := testRender(t, tr, expected) + tr := template.NewTemplateRenderer(templateString, templateData) + err := testRender(t, tr, template.ALL, expected) if err != nil { t.Fatalf("Expected no error, but got %v", err) } @@ -188,8 +188,8 @@ func TestExecuteConstTemplate(t *testing.T) { secret template: {{ .var.some_lula_secret }} ` - tr := template.NewTemplateRenderer(templateString, template.CONSTANTS, templateData) - err := testRender(t, tr, expected) + tr := template.NewTemplateRenderer(templateString, templateData) + err := testRender(t, tr, template.CONSTANTS, expected) if err != nil { t.Fatalf("Expected no error, but got %v", err) } @@ -211,8 +211,8 @@ func TestExecuteConstTemplate(t *testing.T) { variable template: {{ .var.some_env_var }} secret template: {{ .var.some_lula_secret }} ` - tr := template.NewTemplateRenderer(templateString, template.CONSTANTS, templateData) - err := testRender(t, tr, expected) + tr := template.NewTemplateRenderer(templateString, templateData) + err := testRender(t, tr, template.CONSTANTS, expected) if err != nil { t.Fatalf("Expected no error, but got %v", err) } @@ -240,8 +240,8 @@ func TestExecuteConstTemplate(t *testing.T) { variable template: {{ .var.some_env_var }} secret template: {{ .var.some_lula_secret }} ` - tr := template.NewTemplateRenderer(templateString, template.CONSTANTS, templateData) - err := testRender(t, tr, expected) + tr := template.NewTemplateRenderer(templateString, templateData) + err := testRender(t, tr, template.CONSTANTS, expected) if err != nil { t.Fatalf("Expected no error, but got %v", err) } @@ -263,8 +263,8 @@ func TestExecuteConstTemplate(t *testing.T) { secret template: {{ .var.some_lula_secret }} ` - tr := template.NewTemplateRenderer(templateString, template.CONSTANTS, templateData) - err := testRender(t, tr, "") + tr := template.NewTemplateRenderer(templateString, templateData) + err := testRender(t, tr, template.CONSTANTS, "") if err == nil { t.Fatalf("Expected an error, but got nil") } @@ -302,8 +302,8 @@ func TestExecuteNonSensitiveTemplate(t *testing.T) { secret template2: {{ .var.some_lula_secret }} ` - tr := template.NewTemplateRenderer(templateString, template.NONSENSITIVE, templateData) - err := testRender(t, tr, expected) + tr := template.NewTemplateRenderer(templateString, templateData) + err := testRender(t, tr, template.NONSENSITIVE, expected) if err != nil { t.Fatalf("Expected no error, but got %v", err) } @@ -332,8 +332,8 @@ func TestExecuteNonSensitiveTemplate(t *testing.T) { secret template: {{.var.some_lula_secret }} ` - tr := template.NewTemplateRenderer(templateString, template.NONSENSITIVE, templateData) - err := testRender(t, tr, expected) + tr := template.NewTemplateRenderer(templateString, templateData) + err := testRender(t, tr, template.NONSENSITIVE, expected) if err != nil { t.Fatalf("Expected no error, but got %v", err) } @@ -355,8 +355,8 @@ func TestExecuteNonSensitiveTemplate(t *testing.T) { secret template: {{ .var.some_lula_secret }} ` - tr := template.NewTemplateRenderer(templateString, template.NONSENSITIVE, templateData) - err := testRender(t, tr, "") + tr := template.NewTemplateRenderer(templateString, templateData) + err := testRender(t, tr, template.NONSENSITIVE, "") if err == nil { t.Fatalf("Expected an error, but got nil") } @@ -394,8 +394,8 @@ func TestExecuteMaskedTemplate(t *testing.T) { secret template2: ******** ` - tr := template.NewTemplateRenderer(templateString, template.MASKED, templateData) - err := testRender(t, tr, expected) + tr := template.NewTemplateRenderer(templateString, templateData) + err := testRender(t, tr, template.MASKED, expected) if err != nil { t.Fatalf("Expected no error, but got %v", err) } @@ -424,8 +424,8 @@ func TestExecuteMaskedTemplate(t *testing.T) { secret template: ******** ` - tr := template.NewTemplateRenderer(templateString, template.MASKED, templateData) - err := testRender(t, tr, expected) + tr := template.NewTemplateRenderer(templateString, templateData) + err := testRender(t, tr, template.MASKED, expected) if err != nil { t.Fatalf("Expected no error, but got %v", err) } @@ -447,8 +447,8 @@ func TestExecuteMaskedTemplate(t *testing.T) { secret template: {{ .var.some_lula_secret }} ` - tr := template.NewTemplateRenderer(templateString, template.MASKED, templateData) - err := testRender(t, tr, "") + tr := template.NewTemplateRenderer(templateString, templateData) + err := testRender(t, tr, template.MASKED, "") if err == nil { t.Fatalf("Expected an error, but got nil") }