Skip to content

Commit

Permalink
[8.8] Revert "[Synthetics] quote monitor name to prevent invalid yaml" (
Browse files Browse the repository at this point in the history
#157143) (#157199)

# Backport

This will backport the following commits from `main` to `8.8`:
- [Revert "[Synthetics] quote monitor name to prevent invalid yaml"
(#157143)](#157143)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT
[{"author":{"name":"Shahzad","email":"shahzad31comp@gmail.com"},"sourceCommit":{"committedDate":"2023-05-09T11:15:46Z","message":"Revert
\"[Synthetics] quote monitor name to prevent invalid yaml\"
(#157143)","sha":"6220fae002425f4e955537c312d0e4e3c9d2bc2d","branchLabelMapping":{"^v8.9.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Team:uptime","release_note:skip","v8.8.0","v8.9.0"],"number":157143,"url":"#157143
\"[Synthetics] quote monitor name to prevent invalid yaml\"
(#157143)","sha":"6220fae002425f4e955537c312d0e4e3c9d2bc2d"}},"sourceBranch":"main","suggestedTargetBranches":["8.8"],"targetPullRequestStates":[{"branch":"8.8","label":"v8.8.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.9.0","labelRegex":"^v8.9.0$","isSourceBranch":true,"state":"MERGED","url":"#157143
\"[Synthetics] quote monitor name to prevent invalid yaml\"
(#157143)","sha":"6220fae002425f4e955537c312d0e4e3c9d2bc2d"}}]}]
BACKPORT-->

Co-authored-by: Shahzad <shahzad31comp@gmail.com>
  • Loading branch information
dominiqueclarke and shahzad31 committed May 9, 2023
1 parent a0ebedf commit d10ba61
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ export const browserFormatters: BrowserFormatMap = {
[ConfigKey.SCREENSHOTS]: null,
[ConfigKey.IGNORE_HTTPS_ERRORS]: null,
[ConfigKey.PLAYWRIGHT_OPTIONS]: null,
[ConfigKey.TEXT_ASSERTION]: stringToJsonFormatter,
[ConfigKey.PORT]: stringToJsonFormatter,
[ConfigKey.URLS]: stringToJsonFormatter,
[ConfigKey.TEXT_ASSERTION]: null,
[ConfigKey.PORT]: null,
[ConfigKey.URLS]: null,
[ConfigKey.METADATA]: objectToJsonFormatter,
[ConfigKey.SOURCE_INLINE]: stringToJsonFormatter,
[ConfigKey.SYNTHETICS_ARGS]: arrayToJsonFormatter,
Expand Down
18 changes: 9 additions & 9 deletions x-pack/plugins/synthetics/common/formatters/common/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@
*/

import { CommonFields, ConfigKey, SourceType } from '../../runtime_types/monitor_management';
import { arrayToJsonFormatter, stringToJsonFormatter, FormatterFn } from '../formatting_utils';
import { arrayToJsonFormatter, FormatterFn } from '../formatting_utils';

export type Formatter = null | FormatterFn;

export type CommonFormatMap = Record<keyof CommonFields | ConfigKey.NAME, Formatter>;

export const commonFormatters: CommonFormatMap = {
[ConfigKey.APM_SERVICE_NAME]: stringToJsonFormatter,
[ConfigKey.NAME]: stringToJsonFormatter,
[ConfigKey.APM_SERVICE_NAME]: null,
[ConfigKey.NAME]: null,
[ConfigKey.LOCATIONS]: null,
[ConfigKey.MONITOR_TYPE]: null,
[ConfigKey.ENABLED]: null,
[ConfigKey.ALERT_CONFIG]: null,
[ConfigKey.CONFIG_ID]: null,
[ConfigKey.NAMESPACE]: stringToJsonFormatter,
[ConfigKey.NAMESPACE]: null,
[ConfigKey.REVISION]: null,
[ConfigKey.MONITOR_SOURCE_TYPE]: null,
[ConfigKey.FORM_MONITOR_TYPE]: null,
[ConfigKey.JOURNEY_ID]: stringToJsonFormatter,
[ConfigKey.PROJECT_ID]: stringToJsonFormatter,
[ConfigKey.CUSTOM_HEARTBEAT_ID]: stringToJsonFormatter,
[ConfigKey.ORIGINAL_SPACE]: stringToJsonFormatter,
[ConfigKey.JOURNEY_ID]: null,
[ConfigKey.PROJECT_ID]: null,
[ConfigKey.CUSTOM_HEARTBEAT_ID]: null,
[ConfigKey.ORIGINAL_SPACE]: null,
[ConfigKey.CONFIG_HASH]: null,
[ConfigKey.MONITOR_QUERY_ID]: stringToJsonFormatter,
[ConfigKey.MONITOR_QUERY_ID]: null,
[ConfigKey.SCHEDULE]: (fields) =>
JSON.stringify(
`@every ${fields[ConfigKey.SCHEDULE]?.number}${fields[ConfigKey.SCHEDULE]?.unit}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ describe('formatSyntheticsPolicy', () => {
},
id: {
type: 'text',
value: '"00bb3ceb-a242-4c7a-8405-8da963661374"',
value: '00bb3ceb-a242-4c7a-8405-8da963661374',
},
ignore_https_errors: {
type: 'bool',
Expand All @@ -372,7 +372,7 @@ describe('formatSyntheticsPolicy', () => {
},
name: {
type: 'text',
value: '"Test HTTP Monitor 03"',
value: 'Test HTTP Monitor 03',
},
origin: {
type: 'text',
Expand Down Expand Up @@ -401,7 +401,7 @@ describe('formatSyntheticsPolicy', () => {
},
'service.name': {
type: 'text',
value: '"Local Service"',
value: '',
},
'source.inline.script': {
type: 'yaml',
Expand Down Expand Up @@ -532,7 +532,7 @@ describe('formatSyntheticsPolicy', () => {
},
id: {
type: 'text',
value: '"51ccd9d9-fc3f-4718-ba9d-b6ef80e73fc5"',
value: '51ccd9d9-fc3f-4718-ba9d-b6ef80e73fc5',
},
location_name: {
type: 'text',
Expand All @@ -550,19 +550,19 @@ describe('formatSyntheticsPolicy', () => {
},
name: {
type: 'text',
value: '"Test Monitor"',
value: 'Test Monitor',
},
origin: {
type: 'text',
value: 'ui',
},
password: {
type: 'password',
value: '"changeme"',
value: 'changeme',
},
proxy_url: {
type: 'text',
value: '"https://proxy.com"',
value: 'https://proxy.com',
},
'response.include_body': {
type: 'text',
Expand All @@ -582,7 +582,7 @@ describe('formatSyntheticsPolicy', () => {
},
'service.name': {
type: 'text',
value: '"LocalService"',
value: 'LocalService',
},
'ssl.certificate': {
type: 'yaml',
Expand Down Expand Up @@ -622,11 +622,11 @@ describe('formatSyntheticsPolicy', () => {
},
urls: {
type: 'text',
value: '"https://www.google.com"',
value: 'https://www.google.com',
},
username: {
type: 'text',
value: '"admin"',
value: '',
},
},
},
Expand Down Expand Up @@ -1110,7 +1110,7 @@ const browserConfig: any = {
enabled: true,
alert: { status: { enabled: true } },
schedule: { number: '3', unit: 'm' },
'service.name': 'Local Service',
'service.name': '',
config_id: '00bb3ceb-a242-4c7a-8405-8da963661374',
tags: ['cookie-test', 'browser'],
timeout: '16',
Expand Down Expand Up @@ -1198,7 +1198,7 @@ const httpPolicy: any = {
'check.request.body': { type: 'text', value: '' },
'check.request.headers': {},
'check.request.method': 'GET',
username: 'admin',
username: '',
'ssl.certificate_authorities': '',
'ssl.certificate': '',
'ssl.key': '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,6 @@ export const stringToJsonFormatter: FormatterFn = (fields, key) => {
return value ? JSON.stringify(value) : null;
};

export const stringifyString = (value?: string) => {
if (!value) return value;
try {
return JSON.stringify(value);
} catch (e) {
return value;
}
};

export const replaceStringWithParams = (
value: string | boolean | {} | [],
params: Record<string, string>,
Expand Down
16 changes: 6 additions & 10 deletions x-pack/plugins/synthetics/common/formatters/http/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ import { tlsFormatters } from '../tls/formatters';
import { HTTPFields, ConfigKey } from '../../runtime_types/monitor_management';

import { Formatter, commonFormatters } from '../common/formatters';
import {
stringToJsonFormatter,
arrayToJsonFormatter,
objectToJsonFormatter,
} from '../formatting_utils';
import { arrayToJsonFormatter, objectToJsonFormatter } from '../formatting_utils';

export type HTTPFormatMap = Record<keyof HTTPFields, Formatter>;

Expand All @@ -23,12 +19,12 @@ export const httpFormatters: HTTPFormatMap = {
[ConfigKey.RESPONSE_BODY_INDEX]: null,
[ConfigKey.RESPONSE_HEADERS_INDEX]: null,
[ConfigKey.METADATA]: objectToJsonFormatter,
[ConfigKey.URLS]: stringToJsonFormatter,
[ConfigKey.USERNAME]: stringToJsonFormatter,
[ConfigKey.PASSWORD]: stringToJsonFormatter,
[ConfigKey.PROXY_URL]: stringToJsonFormatter,
[ConfigKey.URLS]: null,
[ConfigKey.USERNAME]: null,
[ConfigKey.PASSWORD]: null,
[ConfigKey.PROXY_URL]: null,
[ConfigKey.PROXY_HEADERS]: objectToJsonFormatter,
[ConfigKey.PORT]: stringToJsonFormatter,
[ConfigKey.PORT]: null,
[ConfigKey.RESPONSE_BODY_CHECK_NEGATIVE]: arrayToJsonFormatter,
[ConfigKey.RESPONSE_BODY_CHECK_POSITIVE]: arrayToJsonFormatter,
[ConfigKey.RESPONSE_JSON_CHECK]: arrayToJsonFormatter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ import { secondsToCronFormatter } from '../formatting_utils';
import { ICMPFields, ConfigKey } from '../../runtime_types/monitor_management';

import { Formatter, commonFormatters } from '../common/formatters';
import { stringToJsonFormatter } from '../formatting_utils';

export type ICMPFormatMap = Record<keyof ICMPFields, Formatter>;

export const icmpFormatters: ICMPFormatMap = {
[ConfigKey.HOSTS]: stringToJsonFormatter,
[ConfigKey.HOSTS]: null,
[ConfigKey.WAIT]: secondsToCronFormatter,
[ConfigKey.MODE]: null,
[ConfigKey.IPV4]: null,
Expand Down
14 changes: 7 additions & 7 deletions x-pack/plugins/synthetics/common/formatters/tcp/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import { TCPFields, ConfigKey } from '../../runtime_types/monitor_management';
import { Formatter, commonFormatters } from '../common/formatters';
import { objectToJsonFormatter } from '../formatting_utils';
import { tlsFormatters } from '../tls/formatters';
import { stringToJsonFormatter } from '../formatting_utils';

export type TCPFormatMap = Record<keyof TCPFields, Formatter>;

export const tcpFormatters: TCPFormatMap = {
[ConfigKey.METADATA]: objectToJsonFormatter,
[ConfigKey.HOSTS]: stringToJsonFormatter,
[ConfigKey.HOSTS]: null,
[ConfigKey.PROXY_USE_LOCAL_RESOLVER]: null,
[ConfigKey.RESPONSE_RECEIVE_CHECK]: stringToJsonFormatter,
[ConfigKey.REQUEST_SEND_CHECK]: stringToJsonFormatter,
[ConfigKey.PROXY_URL]: stringToJsonFormatter,
[ConfigKey.PORT]: stringToJsonFormatter,
[ConfigKey.URLS]: stringToJsonFormatter,
[ConfigKey.RESPONSE_RECEIVE_CHECK]: null,
[ConfigKey.REQUEST_SEND_CHECK]: null,
[ConfigKey.PROXY_URL]: null,
[ConfigKey.PROXY_URL]: null,
[ConfigKey.PORT]: null,
[ConfigKey.URLS]: null,
[ConfigKey.MODE]: null,
[ConfigKey.IPV4]: null,
[ConfigKey.IPV6]: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ describe('formatMonitorConfig', () => {
enabled: true,
locations: [],
max_redirects: '0',
name: '"Test"',
password: '"3z9SBOQWW5F0UrdqLVFqlF6z"',
name: 'Test',
password: '3z9SBOQWW5F0UrdqLVFqlF6z',
'response.include_body': 'on_error',
'response.include_headers': true,
schedule: '@every 3m',
timeout: '16s',
type: 'http',
urls: '"https://www.google.com"',
proxy_url: '"https://www.google.com"',
urls: 'https://www.google.com',
proxy_url: 'https://www.google.com',
});
});

Expand Down Expand Up @@ -158,15 +158,15 @@ describe('formatMonitorConfig', () => {
enabled: true,
locations: [],
max_redirects: '0',
name: '"Test"',
password: '"3z9SBOQWW5F0UrdqLVFqlF6z"',
proxy_url: '"https://www.google.com"',
name: 'Test',
password: '3z9SBOQWW5F0UrdqLVFqlF6z',
proxy_url: 'https://www.google.com',
'response.include_body': 'on_error',
'response.include_headers': true,
schedule: '@every 3m',
timeout: '16s',
type: 'http',
urls: '"https://www.google.com"',
urls: 'https://www.google.com',
...(isTLSEnabled ? { 'ssl.verification_mode': 'none' } : {}),
});
}
Expand All @@ -183,7 +183,7 @@ describe('browser fields', () => {
enabled: true,
'filter_journeys.tags': ['dev'],
ignore_https_errors: false,
name: '"Test"',
name: 'Test',
locations: [],
schedule: '@every 3m',
screenshots: 'on',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('SyntheticsPrivateLocation', () => {
type: 'http',
enabled: true,
schedule: '@every 3m',
'service.name': 'test service',
'service.name': '',
locations: [mockPrivateLocation],
tags: [],
timeout: '16',
Expand Down Expand Up @@ -226,7 +226,7 @@ describe('SyntheticsPrivateLocation', () => {
},
name: {
type: 'text',
value: '"Browser monitor"',
value: 'Browser monitor',
},
params: {
type: 'yaml',
Expand All @@ -246,7 +246,7 @@ describe('SyntheticsPrivateLocation', () => {
},
'service.name': {
type: 'text',
value: '"test service"',
value: '',
},
'source.inline.script': {
type: 'yaml',
Expand Down Expand Up @@ -286,7 +286,7 @@ const dummyBrowserConfig: Partial<MonitorFields> & {
type: DataStream.BROWSER,
enabled: true,
schedule: { unit: ScheduleUnit.MINUTES, number: '10' },
'service.name': 'test service',
'service.name': '',
tags: [],
timeout: null,
name: 'Browser monitor',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { NewPackagePolicy } from '@kbn/fleet-plugin/common';
import { NewPackagePolicyWithId } from '@kbn/fleet-plugin/server/services/package_policy';
import { cloneDeep } from 'lodash';
import { SavedObjectError } from '@kbn/core-saved-objects-common';
import { stringifyString } from '../../../common/formatters/formatting_utils';
import { formatSyntheticsPolicy } from '../../../common/formatters/format_synthetics_policy';
import {
ConfigKey,
Expand Down Expand Up @@ -87,9 +86,9 @@ export class SyntheticsPrivateLocation {
{
...(config as Partial<MonitorFields>),
config_id: config.fields?.config_id,
location_name: stringifyString(privateLocation.label),
'monitor.project.id': stringifyString(config.fields?.['monitor.project.name']),
'monitor.project.name': stringifyString(config.fields?.['monitor.project.name']),
location_name: privateLocation.label,
'monitor.project.id': config.fields?.['monitor.project.name'],
'monitor.project.name': config.fields?.['monitor.project.name'],
},
globalParams
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export default function ({ getService }: FtrProviderContext) {
it('does not add a monitor if there is an error in creating integration', async () => {
const newMonitor = { ...httpMonitorJson };

const invalidName = '!@#$%^&*()_++[\\-\\]- wow';
const invalidName = '[] - invalid name';

newMonitor.locations.push({
id: testFleetPolicyID,
Expand All @@ -109,7 +109,7 @@ export default function ({ getService }: FtrProviderContext) {
expect(apiResponse.body).eql({
statusCode: 500,
message:
'YAMLException: unknown escape sequence at line 3, column 34:\n name: "!@#$,%,^,&,*,(,),_,+,+,[,\\,\\,-,\\,\\,],-, ,w,o,w,"\n ^',
'YAMLException: end of the stream or a document separator is expected at line 3, column 10:\n name: [] - invalid name\n ^',
error: 'Internal Server Error',
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ export default function ({ getService }: FtrProviderContext) {
};
const testMonitors = [
projectMonitors.monitors[0],
{ ...secondMonitor, name: '!@#$%^&*()_++[\\-\\]- wow name' },
{ ...secondMonitor, name: '[] - invalid name' },
];
try {
const body = await monitorTestService.addProjectMonitors(project, testMonitors);
expect(body.createdMonitors.length).eql(1);
expect(body.failedMonitors[0].reason).eql(
'unknown escape sequence at line 3, column 34:\n name: "!@#$,%,^,&,*,(,),_,+,+,[,\\,\\,-,\\,\\,],-, ,w,o,w, ,n,a,m,e,"\n ^'
'end of the stream or a document separator is expected at line 3, column 10:\n name: [] - invalid name\n ^'
);
} finally {
await Promise.all([
Expand All @@ -97,7 +97,7 @@ export default function ({ getService }: FtrProviderContext) {
expect(editedBody.createdMonitors.length).eql(0);
expect(editedBody.updatedMonitors.length).eql(2);

testMonitors[1].name = '!@#$%^&*()_++[\\-\\]- wow name';
testMonitors[1].name = '[] - invalid name';

const editedBodyError = await monitorTestService.addProjectMonitors(project, testMonitors);
expect(editedBodyError.createdMonitors.length).eql(0);
Expand All @@ -107,7 +107,7 @@ export default function ({ getService }: FtrProviderContext) {
'Failed to update journey: test-id-2'
);
expect(editedBodyError.failedMonitors[0].reason).eql(
'unknown escape sequence at line 3, column 34:\n name: "!@#$,%,^,&,*,(,),_,+,+,[,\\,\\,-,\\,\\,],-, ,w,o,w, ,n,a,m,e,"\n ^'
'end of the stream or a document separator is expected at line 3, column 10:\n name: [] - invalid name\n ^'
);
} finally {
await Promise.all([
Expand Down

0 comments on commit d10ba61

Please sign in to comment.