diff --git a/public/app/features/alerting/unified/utils/alertmanager.test.ts b/public/app/features/alerting/unified/utils/alertmanager.test.ts index a3bc0e6f067..c6372994465 100644 --- a/public/app/features/alerting/unified/utils/alertmanager.test.ts +++ b/public/app/features/alerting/unified/utils/alertmanager.test.ts @@ -31,16 +31,23 @@ describe('Alertmanager utils', () => { }); }); - it('should parse escaped values correctly', () => { - expect(parseMatcher('foo=~"bar\\"baz\\""')).toEqual({ + // Alertmanager has some strict requirements for label values; + // we should not automatically encode or decode any values sent + // and instead let AM return any errors like (matcher value contains unescaped double quote: bar"baz") + // and allow the user to update the values to the correct format + // + // see https://github.com/prometheus/alertmanager/blob/4030e3670b359b8814aa8340ea1144f32b1f5ab3/pkg/labels/parse.go#L55-L99 + // and https://github.com/prometheus/alertmanager/blob/4030e3670b359b8814aa8340ea1144f32b1f5ab3/pkg/labels/parse.go#L101-L178 + it('should not parse escaped values', () => { + expect(parseMatcher('foo="^[a-z0-9-]{1}[a-z0-9-]{0,30}$"')).toEqual({ name: 'foo', - value: 'bar"baz"', - isRegex: true, + value: '"^[a-z0-9-]{1}[a-z0-9-]{0,30}$"', + isRegex: false, isEqual: true, }); expect(parseMatcher('foo=~bar\\"baz\\"')).toEqual({ name: 'foo', - value: 'bar"baz"', + value: 'bar\\"baz\\"', isRegex: true, isEqual: true, }); diff --git a/public/app/features/alerting/unified/utils/alertmanager.ts b/public/app/features/alerting/unified/utils/alertmanager.ts index af69f38bc09..cfa9a963f44 100644 --- a/public/app/features/alerting/unified/utils/alertmanager.ts +++ b/public/app/features/alerting/unified/utils/alertmanager.ts @@ -118,14 +118,6 @@ const matcherOperators = [ MatcherOperator.equal, ]; -function unescapeMatcherValue(value: string) { - let trimmed = value.trim().replace(/\\"/g, '"'); - if (trimmed.startsWith('"') && trimmed.endsWith('"') && !trimmed.endsWith('\\"')) { - trimmed = trimmed.substr(1, trimmed.length - 2); - } - return trimmed.replace(/\\"/g, '"'); -} - export function parseMatcher(matcher: string): Matcher { const trimmed = matcher.trim(); if (trimmed.startsWith('{') && trimmed.endsWith('}')) { @@ -141,7 +133,7 @@ export function parseMatcher(matcher: string): Matcher { } const [operator, idx] = operatorsFound[0]; const name = trimmed.substr(0, idx).trim(); - const value = unescapeMatcherValue(trimmed.substr(idx + operator.length).trim()); + const value = trimmed.substr(idx + operator.length).trim(); if (!name) { throw new Error(`Invalid matcher: ${trimmed}`); }