Skip to content

Commit 2d01aa2

Browse files
committed
BUG/MEDIUM: hcl: prevent multiple escaping
copier does not do a deep copy properly Signed-off-by: Zlatko Bratkovic <[email protected]>
1 parent 4f5643f commit 2d01aa2

File tree

3 files changed

+10
-17
lines changed

3 files changed

+10
-17
lines changed

configuration/file-storage-hcl.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
package configuration
1717

1818
import (
19+
"bytes"
20+
"encoding/gob"
1921
"io/ioutil"
2022
"strings"
2123

2224
"github.com/haproxytech/dataplaneapi/log"
2325
"github.com/hashicorp/hcl"
24-
"github.com/jinzhu/copier"
2526
"github.com/rodaine/hclencoder"
2627
)
2728

@@ -63,17 +64,17 @@ func (s *StorageHCL) SaveAs(filename string) error {
6364
var err error
6465
var hcl []byte
6566
var localCopy StorageDataplaneAPIConfiguration
66-
err = copier.Copy(&localCopy, &s.cfg)
67-
if err != nil {
67+
68+
var b bytes.Buffer
69+
e := gob.NewEncoder(&b)
70+
if err := e.Encode(s.cfg); err != nil {
6871
return err
6972
}
70-
// check if we have cluster log targets in config file
71-
if s.cfg.Cluster != nil && len(s.cfg.Cluster.ClusterLogTargets) > 0 {
72-
// since this can contain " character, escape it
73-
for index, value := range localCopy.Cluster.ClusterLogTargets {
74-
localCopy.Cluster.ClusterLogTargets[index].LogFormat = strings.Replace(value.LogFormat, `"`, `\"`, -1)
75-
}
73+
d := gob.NewDecoder(&b)
74+
if err := d.Decode(&localCopy); err != nil {
75+
return err
7676
}
77+
7778
// check if we have cluster log targets in config file
7879
if localCopy.Cluster != nil && len(localCopy.Cluster.ClusterLogTargets) > 0 {
7980
// since this can contain " character, escape it
@@ -84,7 +85,6 @@ func (s *StorageHCL) SaveAs(filename string) error {
8485
if localCopy.LogTargets != nil && len(*localCopy.LogTargets) > 0 {
8586
var logTargets []log.Target
8687
for _, value := range *localCopy.LogTargets {
87-
value.LogFormat = strings.Replace(value.LogFormat, `"`, `\"`, -1)
8888
value.ACLFormat = strings.Replace(value.ACLFormat, `"`, `\"`, -1)
8989
logTargets = append(logTargets, value)
9090
}
@@ -95,10 +95,6 @@ func (s *StorageHCL) SaveAs(filename string) error {
9595
aclF := strings.Replace(*localCopy.Log.ACLFormat, `"`, `\"`, -1)
9696
localCopy.Log.ACLFormat = &aclF
9797
}
98-
if localCopy.Log.LogFormat != nil {
99-
logF := strings.Replace(*localCopy.Log.LogFormat, `"`, `\"`, -1)
100-
localCopy.Log.LogFormat = &logF
101-
}
10298
}
10399

104100
hcl, err = hclencoder.Encode(localCopy)

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ require (
3131
github.com/hashicorp/hcl v1.0.0
3232
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 // indirect
3333
github.com/jessevdk/go-flags v1.4.0
34-
github.com/jinzhu/copier v0.3.4
3534
github.com/lestrrat-go/apache-logformat v0.0.0-20210106032603-24d066f940f8
3635
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc // indirect
3736
github.com/nathanaelle/syslog5424/v2 v2.0.5

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,6 @@ github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 h1:IPJ3dvxmJ4uc
222222
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869/go.mod h1:cJ6Cj7dQo+O6GJNiMx+Pa94qKj+TG8ONdKHgMNIyyag=
223223
github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA=
224224
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
225-
github.com/jinzhu/copier v0.3.4 h1:mfU6jI9PtCeUjkjQ322dlff9ELjGDu975C2p/nrubVI=
226-
github.com/jinzhu/copier v0.3.4/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg=
227225
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
228226
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
229227
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=

0 commit comments

Comments
 (0)