From 6e9be0396842b89d99a394f685cfba902ae65180 Mon Sep 17 00:00:00 2001 From: Shahzad Date: Thu, 31 Oct 2024 19:07:06 +0100 Subject: [PATCH] Revert "[Synthetics] Revert 8.15 public API responses (#195295)" (#198511) Fixes https://github.com/elastic/kibana/issues/198518 This reverts commit d21495bbce07ed39fdcf077c9170d012adab74a4 and ## Testing Create few HTTP, ICMP, TCP and Browser monitors via API in 8.15 and in this PR Compared JSOn diffs to make sure responses are same (cherry picked from commit 184d9a04486d2afdcbc428308a121cb377abda17) --- .../saved_object_to_monitor.test.ts | 94 ++++++------------- .../formatters/saved_object_to_monitor.ts | 78 +++++++-------- .../apis/synthetics/add_monitor.ts | 9 +- .../apis/synthetics/get_monitor.ts | 1 - 4 files changed, 67 insertions(+), 115 deletions(-) diff --git a/x-pack/plugins/observability_solution/synthetics/server/routes/monitor_cruds/formatters/saved_object_to_monitor.test.ts b/x-pack/plugins/observability_solution/synthetics/server/routes/monitor_cruds/formatters/saved_object_to_monitor.test.ts index 40aec3b468a37..18c4bf71cdfec 100644 --- a/x-pack/plugins/observability_solution/synthetics/server/routes/monitor_cruds/formatters/saved_object_to_monitor.test.ts +++ b/x-pack/plugins/observability_solution/synthetics/server/routes/monitor_cruds/formatters/saved_object_to_monitor.test.ts @@ -70,6 +70,9 @@ describe('mergeSourceMonitor', () => { const result = mapSavedObjectToMonitor({ monitor: { attributes: testMonitor } } as any); expect(result).toEqual({ + __ui: { + is_tls_enabled: false, + }, alert: { status: { enabled: true, @@ -78,53 +81,48 @@ describe('mergeSourceMonitor', () => { enabled: true, }, }, + 'check.request.method': 'GET', + 'check.response.status': ['404'], config_id: 'ae88f0aa-9c7d-4a5f-96dc-89d65a0ca947', custom_heartbeat_id: 'todos-lightweight-test-projects-default', enabled: true, id: 'todos-lightweight-test-projects-default', ipv4: true, ipv6: true, - locations: ['us_central', 'us_east'], - private_locations: ['pvt_us_east'], - max_redirects: 0, + locations: [ + { + geo: { + lat: 41.25, + lon: -95.86, + }, + id: 'us_central', + isServiceManaged: true, + label: 'North America - US Central', + }, + ], + max_attempts: 2, + max_redirects: '0', mode: 'any', name: 'Todos Lightweight', namespace: 'default', + origin: 'project', original_space: 'default', - proxy_url: '', + project_id: 'test-projects', + 'response.include_body': 'on_error', + 'response.include_body_max_bytes': '1024', + 'response.include_headers': true, retest_on_failure: true, revision: 21, schedule: { number: '3', unit: 'm', }, - 'service.name': '', - tags: [], + 'ssl.key': 'test-key', + 'ssl.supported_protocols': ['TLSv1.1', 'TLSv1.2', 'TLSv1.3'], + 'ssl.verification_mode': 'full', timeout: '16', type: 'http', url: '${devUrl}', - 'url.port': null, - ssl: { - certificate: '', - certificate_authorities: '', - supported_protocols: ['TLSv1.1', 'TLSv1.2', 'TLSv1.3'], - verification_mode: 'full', - key: 'test-key', - }, - response: { - include_body: 'on_error', - include_body_max_bytes: '1024', - include_headers: true, - }, - check: { - request: { - method: 'GET', - }, - response: { - status: ['404'], - }, - }, - params: {}, }); }); @@ -157,30 +155,12 @@ describe('mergeSourceMonitor', () => { locations: [ { geo: { - lon: -95.86, lat: 41.25, - }, - isServiceManaged: true, - id: 'us_central', - label: 'North America - US Central', - }, - { - geo: { lon: -95.86, - lat: 41.25, }, + id: 'us_central', isServiceManaged: true, - id: 'us-east4-a', - label: 'US East', - }, - { - geo: { - lon: -95.86, - lat: 41.25, - }, - isServiceManaged: false, - id: 'pvt_us_east', - label: 'US East (Private)', + label: 'North America - US Central', }, ], max_redirects: '0', @@ -249,24 +229,6 @@ const testMonitor = { id: 'us_central', label: 'North America - US Central', }, - { - geo: { - lon: -95.86, - lat: 41.25, - }, - isServiceManaged: true, - id: 'us-east4-a', - label: 'US East', - }, - { - geo: { - lon: -95.86, - lat: 41.25, - }, - isServiceManaged: false, - id: 'pvt_us_east', - label: 'US East (Private)', - }, ], namespace: 'default', origin: 'project', diff --git a/x-pack/plugins/observability_solution/synthetics/server/routes/monitor_cruds/formatters/saved_object_to_monitor.ts b/x-pack/plugins/observability_solution/synthetics/server/routes/monitor_cruds/formatters/saved_object_to_monitor.ts index 06746fc235769..4156620abdd78 100644 --- a/x-pack/plugins/observability_solution/synthetics/server/routes/monitor_cruds/formatters/saved_object_to_monitor.ts +++ b/x-pack/plugins/observability_solution/synthetics/server/routes/monitor_cruds/formatters/saved_object_to_monitor.ts @@ -7,7 +7,6 @@ import { SavedObject } from '@kbn/core/server'; import { mergeWith, omit, omitBy } from 'lodash'; -import { LocationsMap } from '../../../synthetics_service/project_monitor/normalizers/common_fields'; import { ConfigKey, EncryptedSyntheticsMonitor, @@ -22,14 +21,6 @@ const keysToOmit = [ ConfigKey.CONFIG_HASH, ConfigKey.JOURNEY_ID, ConfigKey.FORM_MONITOR_TYPE, - ConfigKey.MAX_ATTEMPTS, - ConfigKey.MONITOR_SOURCE_TYPE, - ConfigKey.METADATA, - ConfigKey.SOURCE_PROJECT_CONTENT, - ConfigKey.PROJECT_ID, - ConfigKey.JOURNEY_FILTERS_MATCH, - ConfigKey.JOURNEY_FILTERS_TAGS, - ConfigKey.MONITOR_SOURCE_TYPE, ]; type Result = MonitorFieldsResult & { @@ -39,14 +30,25 @@ type Result = MonitorFieldsResult & { ssl: Record; response: Record; check: Record; - locations: string[]; - private_locations: string[]; }; export const transformPublicKeys = (result: Result) => { + if (result[ConfigKey.SOURCE_INLINE]) { + result.inline_script = result[ConfigKey.SOURCE_INLINE]; + } + if (result[ConfigKey.HOSTS]) { + result.host = result[ConfigKey.HOSTS]; + } + if (result[ConfigKey.PARAMS]) { + try { + result[ConfigKey.PARAMS] = JSON.parse(result[ConfigKey.PARAMS] ?? '{}'); + } catch (e) { + // ignore + } + } + let formattedResult = { ...result, - ...formatLocations(result), [ConfigKey.PARAMS]: formatParams(result), retest_on_failure: (result[ConfigKey.MAX_ATTEMPTS] ?? 1) > 1, ...(result[ConfigKey.HOSTS] && { host: result[ConfigKey.HOSTS] }), @@ -58,20 +60,8 @@ export const transformPublicKeys = (result: Result) => { ...(result[ConfigKey.SOURCE_INLINE] && { inline_script: result[ConfigKey.SOURCE_INLINE] }), [ConfigKey.PLAYWRIGHT_OPTIONS]: formatPWOptions(result), }; - } else { - formattedResult.ssl = formatNestedFields(formattedResult, 'ssl'); - formattedResult.response = formatNestedFields(formattedResult, 'response'); - formattedResult.check = formatNestedFields(formattedResult, 'check'); - if (formattedResult[ConfigKey.MAX_REDIRECTS]) { - formattedResult[ConfigKey.MAX_REDIRECTS] = Number(formattedResult[ConfigKey.MAX_REDIRECTS]); - } } - const res = omit(formattedResult, keysToOmit) as Result; - - return omitBy( - res, - (_, key) => key.startsWith('response.') || key.startsWith('ssl.') || key.startsWith('check.') - ); + return omit(formattedResult, keysToOmit) as Result; }; export function mapSavedObjectToMonitor({ @@ -81,7 +71,7 @@ export function mapSavedObjectToMonitor({ monitor: SavedObject; internal?: boolean; }) { - const result = { + let result = { ...monitor.attributes, created_at: monitor.created_at, updated_at: monitor.updated_at, @@ -89,7 +79,9 @@ export function mapSavedObjectToMonitor({ if (internal) { return result; } - return transformPublicKeys(result); + result = transformPublicKeys(result); + // omit undefined value or null value + return omitBy(result, removeMonitorEmptyValues); } export function mergeSourceMonitor( normalizedPreviousMonitor: EncryptedSyntheticsMonitor, @@ -108,24 +100,6 @@ const customizer = (destVal: any, srcValue: any, key: string) => { } }; -const formatLocations = (config: MonitorFields) => { - const locMap = Object.entries(LocationsMap); - const locations = config[ConfigKey.LOCATIONS] - ?.filter((location) => location.isServiceManaged) - .map((location) => { - return locMap.find(([_key, value]) => value === location.id)?.[0] ?? location.id; - }); - - const privateLocations = config[ConfigKey.LOCATIONS] - ?.filter((location) => !location.isServiceManaged) - .map((location) => location.id); - - return { - ...(locations && { locations }), - ...(privateLocations && { private_locations: privateLocations }), - }; -}; - const formatParams = (config: MonitorFields) => { if (config[ConfigKey.PARAMS]) { try { @@ -177,3 +151,17 @@ const formatNestedFields = ( return obj; }; + +export const removeMonitorEmptyValues = (v: any) => { + // value is falsy + return ( + v === undefined || + v === null || + // value is empty string + (typeof v === 'string' && v.trim() === '') || + // is empty array + (Array.isArray(v) && v.length === 0) || + // object is has no values + (typeof v === 'object' && Object.keys(v).length === 0) + ); +}; diff --git a/x-pack/test/api_integration/apis/synthetics/add_monitor.ts b/x-pack/test/api_integration/apis/synthetics/add_monitor.ts index 01e5c175ee7d6..aee2e1e2c7212 100644 --- a/x-pack/test/api_integration/apis/synthetics/add_monitor.ts +++ b/x-pack/test/api_integration/apis/synthetics/add_monitor.ts @@ -8,7 +8,7 @@ import expect from '@kbn/expect'; import epct from 'expect'; import moment from 'moment/moment'; import { v4 as uuidv4 } from 'uuid'; -import { omit } from 'lodash'; +import { omit, omitBy } from 'lodash'; import { ConfigKey, MonitorTypeEnum, @@ -23,7 +23,10 @@ import { format as formatUrl } from 'url'; import supertest from 'supertest'; import { getServiceApiKeyPrivileges } from '@kbn/synthetics-plugin/server/synthetics_service/get_api_key'; import { syntheticsMonitorType } from '@kbn/synthetics-plugin/common/types/saved_objects'; -import { transformPublicKeys } from '@kbn/synthetics-plugin/server/routes/monitor_cruds/formatters/saved_object_to_monitor'; +import { + removeMonitorEmptyValues, + transformPublicKeys, +} from '@kbn/synthetics-plugin/server/routes/monitor_cruds/formatters/saved_object_to_monitor'; import { FtrProviderContext } from '../../ftr_provider_context'; import { getFixtureJson } from './helper/get_fixture_json'; import { SyntheticsMonitorTestService } from './services/synthetics_monitor_test_service'; @@ -61,7 +64,7 @@ export const keyToOmitList = [ ]; export const omitMonitorKeys = (monitor: any) => { - return omit(transformPublicKeys(monitor), keyToOmitList); + return omitBy(omit(transformPublicKeys(monitor), keyToOmitList), removeMonitorEmptyValues); }; export default function ({ getService }: FtrProviderContext) { diff --git a/x-pack/test/api_integration/apis/synthetics/get_monitor.ts b/x-pack/test/api_integration/apis/synthetics/get_monitor.ts index 16b42c4dfd0ce..af3f818ab5319 100644 --- a/x-pack/test/api_integration/apis/synthetics/get_monitor.ts +++ b/x-pack/test/api_integration/apis/synthetics/get_monitor.ts @@ -242,7 +242,6 @@ export default function ({ getService }: FtrProviderContext) { revision: 1, locations: [LOCAL_LOCATION], name: 'Test HTTP Monitor 044', - labels: {}, }) ); });