From b33f14ddd9544f286479f1183fb8b954d62ce979 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Mon, 29 Jun 2020 20:08:36 -0700 Subject: [PATCH] Correctly handle replacements with escaped values; closes #2041 --- parser/helpers.go | 16 +++++++--------- parser/parser.go | 14 +++++++------- parser/value.go | 4 +++- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/parser/helpers.go b/parser/helpers.go index d66d2b0..5b58ee9 100644 --- a/parser/helpers.go +++ b/parser/helpers.go @@ -96,12 +96,12 @@ func (f *ConfigurationFile) IterateOverJson(data []byte) (*gabs.Container, error // If the child is a null value, nothing will happen. Seems reasonable as of the // time this code is being written. for _, child := range parsed.Path(strings.Trim(parts[0], ".")).Children() { - if err := v.SetAtPathway(child, strings.Trim(parts[1], "."), value); err != nil { + if err := v.SetAtPathway(child, strings.Trim(parts[1], "."), []byte(value)); err != nil { return nil, err } } } else { - if err = v.SetAtPathway(parsed, v.Match, value); err != nil { + if err = v.SetAtPathway(parsed, v.Match, []byte(value)); err != nil { return nil, err } } @@ -149,12 +149,12 @@ func (cfr *ConfigurationFileReplacement) SetAtPathway(c *gabs.Container, path st } // Looks up a configuration value on the Daemon given a dot-notated syntax. -func (f *ConfigurationFile) LookupConfigurationValue(cfr ConfigurationFileReplacement) ([]byte, error) { +func (f *ConfigurationFile) LookupConfigurationValue(cfr ConfigurationFileReplacement) (string, error) { // If this is not something that we can do a regex lookup on then just continue // on our merry way. If the value isn't a string, we're not going to be doing anything // with it anyways. if cfr.ReplaceWith.Type() != jsonparser.String || !configMatchRegex.Match(cfr.ReplaceWith.Value()) { - return cfr.ReplaceWith.Value(), nil + return cfr.ReplaceWith.String(), nil } // If there is a match, lookup the value in the configuration for the Daemon. If no key @@ -174,17 +174,15 @@ func (f *ConfigurationFile) LookupConfigurationValue(cfr ConfigurationFileReplac match, _, _, err := jsonparser.Get(f.configuration, path...) if err != nil { if err != jsonparser.KeyPathNotFoundError { - return match, errors.WithStack(err) + return string(match), errors.WithStack(err) } log.WithFields(log.Fields{"path": path, "filename": f.FileName}).Debug("attempted to load a configuration value that does not exist") // If there is no key, keep the original value intact, that way it is obvious there // is a replace issue at play. - return match, nil + return string(match), nil } else { - replaced := []byte(configMatchRegex.ReplaceAllString(cfr.ReplaceWith.String(), string(match))) - - return replaced, nil + return configMatchRegex.ReplaceAllString(cfr.ReplaceWith.String(), string(match)), nil } } diff --git a/parser/parser.go b/parser/parser.go index af2aef4..69e1da1 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -236,13 +236,13 @@ func (f *ConfigurationFile) parseXmlFile(path string) error { // Iterate over the elements we found and update their values. for _, element := range doc.FindElements(path) { - if xmlValueMatchRegex.Match(value) { - k := xmlValueMatchRegex.ReplaceAllString(string(value), "$1") - v := xmlValueMatchRegex.ReplaceAllString(string(value), "$2") + if xmlValueMatchRegex.MatchString(value) { + k := xmlValueMatchRegex.ReplaceAllString(value, "$1") + v := xmlValueMatchRegex.ReplaceAllString(value, "$2") element.CreateAttr(k, v) } else { - element.SetText(string(value)) + element.SetText(value) } } } @@ -313,9 +313,9 @@ func (f *ConfigurationFile) parseIniFile(path string) error { // If the key exists in the file go ahead and set the value, otherwise try to // create it in the section. if s.HasKey(k) { - s.Key(k).SetValue(string(value)) + s.Key(k).SetValue(value) } else { - if _, err := s.NewKey(k, string(value)); err != nil { + if _, err := s.NewKey(k, value); err != nil { return err } } @@ -452,7 +452,7 @@ func (f *ConfigurationFile) parsePropertiesFile(path string) error { continue } - if _, _, err := p.Set(replace.Match, string(data)); err != nil { + if _, _, err := p.Set(replace.Match, data); err != nil { return err } } diff --git a/parser/value.go b/parser/value.go index 5fd307e..b9528fc 100644 --- a/parser/value.go +++ b/parser/value.go @@ -14,7 +14,9 @@ func (cv *ReplaceValue) Value() []byte { } func (cv *ReplaceValue) String() string { - return string(cv.value) + str, _ := jsonparser.ParseString(cv.value) + + return str } func (cv *ReplaceValue) Type() jsonparser.ValueType {