Skip to content

Commit 4747458

Browse files
oktalzmjuraga
authored andcommitted
BUG/MEDIUM: hcl: escape log targets on save
library used for saving hcl data does not escape strings, log targets can contain " char that needs to be escaped
1 parent aa95b20 commit 4747458

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

configuration/file-storage-hcl.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ package configuration
1717

1818
import (
1919
"io/ioutil"
20+
"strings"
2021

22+
"github.com/haproxytech/dataplaneapi/log"
2123
"github.com/hashicorp/hcl"
24+
"github.com/jinzhu/copier"
2225
"github.com/rodaine/hclencoder"
2326
)
2427

@@ -57,7 +60,48 @@ func (s *StorageHCL) Set(cfg *StorageDataplaneAPIConfiguration) {
5760
}
5861

5962
func (s *StorageHCL) SaveAs(filename string) error {
60-
hcl, err := hclencoder.Encode(s.cfg)
63+
var err error
64+
var hcl []byte
65+
var localCopy StorageDataplaneAPIConfiguration
66+
err = copier.Copy(&localCopy, &s.cfg)
67+
if err != nil {
68+
return err
69+
}
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+
}
76+
}
77+
// check if we have cluster log targets in config file
78+
if localCopy.Cluster != nil && len(localCopy.Cluster.ClusterLogTargets) > 0 {
79+
// since this can contain " character, escape it
80+
for index, value := range localCopy.Cluster.ClusterLogTargets {
81+
localCopy.Cluster.ClusterLogTargets[index].LogFormat = strings.Replace(value.LogFormat, `"`, `\"`, -1)
82+
}
83+
}
84+
if localCopy.LogTargets != nil && len(*localCopy.LogTargets) > 0 {
85+
var logTargets []log.Target
86+
for _, value := range *localCopy.LogTargets {
87+
value.LogFormat = strings.Replace(value.LogFormat, `"`, `\"`, -1)
88+
value.ACLFormat = strings.Replace(value.ACLFormat, `"`, `\"`, -1)
89+
logTargets = append(logTargets, value)
90+
}
91+
localCopy.LogTargets = (*log.Targets)(&logTargets)
92+
}
93+
if localCopy.Log != nil {
94+
if localCopy.Log.ACLFormat != nil {
95+
aclF := strings.Replace(*localCopy.Log.ACLFormat, `"`, `\"`, -1)
96+
localCopy.Log.ACLFormat = &aclF
97+
}
98+
if localCopy.Log.LogFormat != nil {
99+
logF := strings.Replace(*localCopy.Log.LogFormat, `"`, `\"`, -1)
100+
localCopy.Log.LogFormat = &logF
101+
}
102+
}
103+
104+
hcl, err = hclencoder.Encode(localCopy)
61105
if err != nil {
62106
return err
63107
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ 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
3435
github.com/lestrrat-go/apache-logformat v0.0.0-20210106032603-24d066f940f8
3536
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc // indirect
3637
github.com/nathanaelle/syslog5424/v2 v2.0.5

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ 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=
225227
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
226228
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
227229
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=

0 commit comments

Comments
 (0)