Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions packages/cmd/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/Infisical/infisical-merge/packages/api"
"github.com/Infisical/infisical-merge/packages/config"
"github.com/Infisical/infisical-merge/packages/models"
"github.com/Infisical/infisical-merge/packages/templates"
"github.com/Infisical/infisical-merge/packages/util"
"github.com/Infisical/infisical-merge/packages/util/cache"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -985,16 +986,10 @@ func ProcessTemplate(templateId int, templatePath string, data interface{}, acce
"listSecrets": secretFunction,
"dynamic_secret": dynamicSecretFunction,
"getSecretByName": getSingleSecretFunction,
"minus": func(a, b int) int {
return a - b
},
"add": func(a, b int) int {
return a + b
},
}

templateName := path.Base(templatePath)
tmpl, err := template.New(templateName).Funcs(funcs).ParseFiles(templatePath)
tmpl, err := template.New(templateName).Funcs(templates.CompileTemplateFunctions(funcs)).ParseFiles(templatePath)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1027,7 +1022,7 @@ func ProcessBase64Template(templateId int, encodedTemplate string, data interfac

templateName := "base64Template"

tmpl, err := template.New(templateName).Funcs(funcs).Parse(templateString)
tmpl, err := template.New(templateName).Funcs(templates.CompileTemplateFunctions(funcs)).Parse(templateString)
if err != nil {
return nil, err
}
Expand All @@ -1053,7 +1048,7 @@ func ProcessLiteralTemplate(templateId int, templateString string, data interfac

templateName := "literalTemplate"

tmpl, err := template.New(templateName).Funcs(funcs).Parse(templateString)
tmpl, err := template.New(templateName).Funcs(templates.CompileTemplateFunctions(funcs)).Parse(templateString)
if err != nil {
return nil, err
}
Expand Down
9 changes: 6 additions & 3 deletions packages/cmd/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"text/template"

"github.com/Infisical/infisical-merge/packages/api"
"github.com/Infisical/infisical-merge/packages/templates"
"github.com/Infisical/infisical-merge/packages/util"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
Expand All @@ -37,12 +38,14 @@ func handleK8SecretOutput(bootstrapResponse api.BootstrapInstanceResponse, k8Sec
return fmt.Errorf("failed to create Kubernetes client: %v", err)
}

// Parse and execute the template to render the data/stringData section
tmpl, err := template.New("k8-secret-template").Funcs(template.FuncMap{
templateFuncs := template.FuncMap{
"encodeBase64": func(s string) string {
return base64.StdEncoding.EncodeToString([]byte(s))
},
}).Parse(k8SecretTemplate)
}

// Parse and execute the template to render the data/stringData section
tmpl, err := template.New("k8-secret-template").Funcs(templates.CompileTemplateFunctions(templateFuncs)).Parse(k8SecretTemplate)

if err != nil {
return fmt.Errorf("failed to parse output template: %v", err)
Expand Down
23 changes: 23 additions & 0 deletions packages/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,30 @@ package templates

import (
"embed"
"text/template"

"github.com/Masterminds/sprig/v3"
)

//go:embed *.tmpl
var TemplatesFS embed.FS

func CompileTemplateFunctions(customFunctions template.FuncMap) template.FuncMap {

templates := customFunctions

sprigFuncs := sprig.TxtFuncMap()
// removed for security reasons
delete(sprigFuncs, "env")
delete(sprigFuncs, "expandenv")
Comment thread
varonix0 marked this conversation as resolved.

for k, v := range sprigFuncs {
// make sure we aren't overwriting any of our own functions
_, exists := templates[k]
if !exists {
templates[k] = v
}
}

return templates
}
Comment thread
varonix0 marked this conversation as resolved.