Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug with 2 templates, same ID, different paths #1573

Merged
merged 1 commit into from
Apr 12, 2022
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
23 changes: 10 additions & 13 deletions manager/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ type Runner struct {
outStream, errStream io.Writer
inStream io.Reader

// ctemplatesMap is a map of each template ID to the TemplateConfigs
// that made it.
ctemplatesMap map[string]*config.TemplateConfig

// templates is the list of calculated templates.
templates []*template.Template

Expand Down Expand Up @@ -919,7 +915,6 @@ func (r *Runner) init() error {

numTemplates := len(*r.config.Templates)
templates := make([]*template.Template, 0, numTemplates)
ctemplatesMap := make(map[string]*config.TemplateConfig)

// Iterate over each TemplateConfig, creating a new Template resource for each
// entry. Templates are parsed and saved, and a map of templates to their
Expand All @@ -945,14 +940,13 @@ func (r *Runner) init() error {
FunctionDenylist: ctmpl.FunctionDenylist,
SandboxPath: config.StringVal(ctmpl.SandboxPath),
Destination: config.StringVal(ctmpl.Destination),
Config: ctmpl,
})
if err != nil {
return err
}

templates = append(templates, tmpl)

ctemplatesMap[tmpl.ID()] = ctmpl
}

// Convert the map of templates (which was only used to ensure uniqueness)
Expand All @@ -965,7 +959,6 @@ func (r *Runner) init() error {
r.renderedCh = make(chan struct{}, 1)
r.renderEventCh = make(chan struct{}, 1)

r.ctemplatesMap = ctemplatesMap
r.inStream = os.Stdin
r.outStream = os.Stdout
r.errStream = os.Stderr
Expand Down Expand Up @@ -1018,18 +1011,22 @@ func (r *Runner) diffAndUpdateDeps(depsMap map[string]dep.Dependency) {

// TemplateConfigFor returns the TemplateConfig for the given Template
func (r *Runner) templateConfigFor(tmpl *template.Template) *config.TemplateConfig {
return r.ctemplatesMap[tmpl.ID()]
return tmpl.Config()
}

// TemplateConfigMapping returns a mapping between the template ID and the set
// of TemplateConfig represented by the template ID
func (r *Runner) TemplateConfigMapping() map[string]*config.TemplateConfig {
func (r *Runner) TemplateConfigMapping() map[string][]*config.TemplateConfig {
// this method is primarily used to support embedding consul-template
// in other applications (ex. Nomad)
m := make(map[string]*config.TemplateConfig)
m := make(map[string][]*config.TemplateConfig)

for _, tmpl := range r.templates {
m[tmpl.ID()] = []*config.TemplateConfig{}
}

for id, set := range r.ctemplatesMap {
m[id] = set
for _, tmpl := range r.templates {
m[tmpl.ID()] = append(m[tmpl.ID()], tmpl.Config())
}

return m
Expand Down
32 changes: 32 additions & 0 deletions manager/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,38 @@ import (
"github.com/hashicorp/consul-template/template"
)

func TestRunner_initTemplates(t *testing.T) {

c := config.TestConfig(
&config.Config{
Templates: &config.TemplateConfigs{
&config.TemplateConfig{
Contents: config.String(`template`),
},
&config.TemplateConfig{
Contents: config.String(`template`),
},
},
})

r, err := NewRunner(c, true)
if err != nil {
t.Fatal(err)
}

confMap := r.TemplateConfigMapping()

for _, tmpl := range r.templates {
if _, ok := confMap[tmpl.ID()]; !ok {
t.Errorf("config map missing template entry")
}
if confs := confMap[tmpl.ID()]; len(confs) != len(r.templates) {
t.Errorf("should be %v templates, but there are %v",
len(r.templates), len(confs))
}
}
}

func TestRunner_Receive(t *testing.T) {

c := config.TestConfig(&config.Config{Once: true})
Expand Down
13 changes: 13 additions & 0 deletions template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"text/template"

"github.com/Masterminds/sprig"
"github.com/hashicorp/consul-template/config"
dep "github.com/hashicorp/consul-template/dependency"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -61,6 +62,9 @@ type Template struct {
// and causes an error if a relative path tries to traverse outside that
// prefix.
sandboxPath string

// local reference to configuration for this template
config *config.TemplateConfig
}

// NewTemplateInput is used as input when creating the template.
Expand Down Expand Up @@ -94,6 +98,9 @@ type NewTemplateInput struct {
// and causes an error if a relative path tries to traverse outside that
// prefix.
SandboxPath string

// Config keeps local reference to config struct
Config *config.TemplateConfig
}

// NewTemplate creates and parses a new Consul Template template at the given
Expand Down Expand Up @@ -122,6 +129,7 @@ func NewTemplate(i *NewTemplateInput) (*Template, error) {
t.functionDenylist = i.FunctionDenylist
t.sandboxPath = i.SandboxPath
t.destination = i.Destination
t.config = i.Config

if i.Source != "" {
contents, err := ioutil.ReadFile(i.Source)
Expand All @@ -148,6 +156,11 @@ func (t *Template) Contents() string {
return t.contents
}

// Config returns the template's config
func (t *Template) Config() *config.TemplateConfig {
return t.config
}

// Source returns the filepath source of this template.
func (t *Template) Source() string {
if t.source == "" {
Expand Down