Correctly handle replacements with escaped values; closes #2041

This commit is contained in:
Dane Everitt 2020-06-29 20:08:36 -07:00
parent 1f6789cba3
commit b33f14ddd9
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
3 changed files with 17 additions and 17 deletions

View File

@ -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
}
}

View File

@ -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
}
}

View File

@ -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 {