Commit a533e006 by Erik Sundell Committed by GitHub

Remove escaping of \ ( ) characters (#20915)

parent 2a44cbd1
......@@ -78,7 +78,7 @@ func buildSearchExpression(query *cloudWatchQuery, stat string) string {
}
sort.Strings(keys)
for _, key := range keys {
values := escape(knownDimensions[key])
values := escapeDoubleQuotes(knownDimensions[key])
valueExpression := join(values, " OR ", `"`, `"`)
if len(knownDimensions[key]) > 1 {
valueExpression = fmt.Sprintf(`(%s)`, valueExpression)
......@@ -102,12 +102,9 @@ func buildSearchExpression(query *cloudWatchQuery, stat string) string {
return fmt.Sprintf(`REMOVE_EMPTY(SEARCH('Namespace="%s" %s', '%s', %s))`, query.Namespace, searchTerm, stat, strconv.Itoa(query.Period))
}
func escape(arr []string) []string {
func escapeDoubleQuotes(arr []string) []string {
result := []string{}
for _, value := range arr {
value = strings.ReplaceAll(value, `\`, `\\`)
value = strings.ReplaceAll(value, ")", `\)`)
value = strings.ReplaceAll(value, "(", `\(`)
value = strings.ReplaceAll(value, `"`, `\"`)
result = append(result, value)
}
......
......@@ -166,17 +166,12 @@ func TestMetricDataQueryBuilder(t *testing.T) {
})
})
Convey("and query has has invalid characters in dimension values", func() {
Convey("and query has invalid characters in dimension values", func() {
query := &cloudWatchQuery{
Namespace: "AWS/EC2",
MetricName: "CPUUtilization",
Dimensions: map[string][]string{
"lb1": {`lb\1\`},
"lb2": {`)lb2`},
"lb3": {`l(b3`},
"lb4": {`lb4""`},
"lb5": {`l\(b5"`},
"lb6": {`l\\(b5"`},
},
Period: 300,
Expression: "",
......@@ -184,20 +179,8 @@ func TestMetricDataQueryBuilder(t *testing.T) {
}
res := buildSearchExpression(query, "Average")
Convey("it should escape backslash", func() {
So(res, ShouldContainSubstring, `"lb1"="lb\\1\\"`)
})
Convey("it should escape closing parenthesis", func() {
So(res, ShouldContainSubstring, `"lb2"="\)lb2"`)
})
Convey("it should escape open parenthesis", func() {
So(res, ShouldContainSubstring, `"lb3"="l\(b3"`)
})
Convey("it should escape double quotes", func() {
So(res, ShouldContainSubstring, `"lb6"="l\\\\\(b5\""`)
So(res, ShouldContainSubstring, `lb4\"\"`)
})
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment