[v11.3.x] Alerting: Fix escaping of silence matchers in utf8 mode (#95347)

Co-authored-by: Gilles De Mey <gilles.de.mey@gmail.com>
Co-authored-by: Tom Ratcliffe <tom.ratcliffe@grafana.com>
Fix escaping of silence matchers in utf8 mode (#95067)
pull/95360/head
grafana-delivery-bot[bot] 8 months ago committed by GitHub
parent 54e5e6c1b5
commit c5d7a954ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 11
      public/app/features/alerting/unified/api/alertmanagerApi.ts
  2. 20
      public/app/features/alerting/unified/utils/misc.test.ts
  3. 7
      public/app/features/alerting/unified/utils/misc.ts

@ -1,5 +1,6 @@
import { isEmpty } from 'lodash';
import { encodeMatcher } from 'app/features/alerting/unified/utils/matchers';
import { dispatch } from 'app/store/store';
import { ReceiversStateDTO } from 'app/types/alerting';
@ -17,13 +18,13 @@ import {
} from '../../../../plugins/datasource/alertmanager/types';
import { NotifierDTO } from '../../../../types';
import { withPerformanceLogging } from '../Analytics';
import { matcherToOperator } from '../utils/alertmanager';
import { matcherToMatcherField } from '../utils/alertmanager';
import {
GRAFANA_RULES_SOURCE_NAME,
getDatasourceAPIUid,
isVanillaPrometheusAlertManagerDataSource,
} from '../utils/datasource';
import { retryWhile, wrapWithQuotes } from '../utils/misc';
import { retryWhile } from '../utils/misc';
import { messageFromError, withSerializedError } from '../utils/redux';
import { alertingApi } from './alertingApi';
@ -72,9 +73,9 @@ export const alertmanagerApi = alertingApi.injectEndpoints({
// TODO Add support for active, silenced, inhibited, unprocessed filters
const filterMatchers = filter?.matchers
?.filter((matcher) => matcher.name && matcher.value)
.map(
(matcher) => `${wrapWithQuotes(matcher.name)}${matcherToOperator(matcher)}${wrapWithQuotes(matcher.value)}`
);
.map((matcher) => {
return encodeMatcher(matcherToMatcherField(matcher));
});
const { silenced, inhibited, unprocessed, active } = filter || {};

@ -1,7 +1,5 @@
import {
sortAlerts,
wrapWithQuotes,
escapeQuotes,
createExploreLink,
makeLabelBasedSilenceLink,
makeDataSourceLink,
@ -46,24 +44,6 @@ function permute(inputArray: any[]): any[] {
}, []);
}
describe('wrapWithQuotes', () => {
it('should work as expected', () => {
expect(wrapWithQuotes('"hello, world!"')).toBe('\\"hello, world!\\"');
expect(wrapWithQuotes('hello, world!')).toBe('"hello, world!"');
expect(wrapWithQuotes('hello, "world"!')).toBe('"hello, \\"world\\"!"');
expect(wrapWithQuotes('"hello""')).toBe('\\"hello\\"\\"');
});
});
describe('escapeQuotes', () => {
it('should escape all quotes', () => {
expect(escapeQuotes('"hello, world!"')).toBe('\\"hello, world!\\"');
expect(escapeQuotes('hello, world!')).toBe('hello, world!');
expect(escapeQuotes('hello, "world"!')).toBe('hello, \\"world\\"!');
expect(escapeQuotes('hello"')).toBe('hello\\"');
});
});
describe('Unified Altering misc', () => {
describe('sortAlerts', () => {
describe('when using any sortOrder with a list of alert instances', () => {

@ -111,13 +111,6 @@ export function makeAMLink(path: string, alertManagerName?: string, options?: UR
return `${path}?${search.toString()}`;
}
export const escapeQuotes = (input: string) => input.replace(/\"/g, '\\"');
export function wrapWithQuotes(input: string) {
const alreadyWrapped = input.startsWith('"') && input.endsWith('"');
return alreadyWrapped ? escapeQuotes(input) : `"${escapeQuotes(input)}"`;
}
export function makeLabelBasedSilenceLink(alertManagerSourceName: string, labels: Labels) {
const silenceUrlParams = new URLSearchParams();
silenceUrlParams.append('alertmanager', alertManagerSourceName);

Loading…
Cancel
Save