mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Remove escaping of \ ( ) characters (#20915)
This commit is contained in:
parent
2a44cbd137
commit
a533e00622
@ -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…
Reference in New Issue
Block a user