Remove escaping of \ ( ) characters (#20915)

pull/20924/head
Erik Sundell 6 years ago committed by GitHub
parent 2a44cbd137
commit a533e00622
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      pkg/tsdb/cloudwatch/metric_data_query_builder.go
  2. 21
      pkg/tsdb/cloudwatch/metric_data_query_builder_test.go

@ -78,7 +78,7 @@ func buildSearchExpression(query *cloudWatchQuery, stat string) string {
} }
sort.Strings(keys) sort.Strings(keys)
for _, key := range keys { for _, key := range keys {
values := escape(knownDimensions[key]) values := escapeDoubleQuotes(knownDimensions[key])
valueExpression := join(values, " OR ", `"`, `"`) valueExpression := join(values, " OR ", `"`, `"`)
if len(knownDimensions[key]) > 1 { if len(knownDimensions[key]) > 1 {
valueExpression = fmt.Sprintf(`(%s)`, valueExpression) 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)) 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{} result := []string{}
for _, value := range arr { for _, value := range arr {
value = strings.ReplaceAll(value, `\`, `\\`)
value = strings.ReplaceAll(value, ")", `\)`)
value = strings.ReplaceAll(value, "(", `\(`)
value = strings.ReplaceAll(value, `"`, `\"`) value = strings.ReplaceAll(value, `"`, `\"`)
result = append(result, 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{ query := &cloudWatchQuery{
Namespace: "AWS/EC2", Namespace: "AWS/EC2",
MetricName: "CPUUtilization", MetricName: "CPUUtilization",
Dimensions: map[string][]string{ Dimensions: map[string][]string{
"lb1": {`lb\1\`},
"lb2": {`)lb2`},
"lb3": {`l(b3`},
"lb4": {`lb4""`}, "lb4": {`lb4""`},
"lb5": {`l\(b5"`},
"lb6": {`l\\(b5"`},
}, },
Period: 300, Period: 300,
Expression: "", Expression: "",
@ -184,20 +179,8 @@ func TestMetricDataQueryBuilder(t *testing.T) {
} }
res := buildSearchExpression(query, "Average") 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() { Convey("it should escape double quotes", func() {
So(res, ShouldContainSubstring, `"lb6"="l\\\\\(b5\""`) So(res, ShouldContainSubstring, `lb4\"\"`)
}) })
}) })

Loading…
Cancel
Save