Alerting: Fix escaping of silence matchers in utf8 mode (#95067)
Co-authored-by: Gilles De Mey <gilles.de.mey@gmail.com>
This commit is contained in:
co-authored by
Gilles De Mey
parent
c42f42223a
commit
9ec48075ee
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user