Skip to content

Commit

Permalink
fix: small refactor on template render
Browse files Browse the repository at this point in the history
  • Loading branch information
meganwolf0 committed Sep 20, 2024
1 parent fc5a9cf commit 8c9b128
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 66 deletions.
4 changes: 2 additions & 2 deletions src/cmd/tools/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
49 changes: 21 additions & 28 deletions src/internal/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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+"\" }}")
}
}
Expand All @@ -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
}
Expand All @@ -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, "********")
}
}
Expand All @@ -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
}
Expand Down
72 changes: 36 additions & 36 deletions src/internal/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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")
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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")
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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")
}
Expand Down

0 comments on commit 8c9b128

Please sign in to comment.