From ae8b6c8beb3ad0b3f30de3f520fdf9dcb4e23e01 Mon Sep 17 00:00:00 2001 From: "Lucas F. da Costa" Date: Mon, 23 May 2022 09:29:11 +0100 Subject: [PATCH] [Uptime] Fix bug causing all monitors to be saved to all locations [solves #132314] (#132325) Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> --- .../formatters/format_configs.test.ts | 2 + .../formatters/format_configs.ts | 1 - .../synthetics_service.test.ts | 72 ++++++++++++++++++- .../synthetics_service/synthetics_service.ts | 4 +- 4 files changed, 75 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/synthetics/server/synthetics_service/formatters/format_configs.test.ts b/x-pack/plugins/synthetics/server/synthetics_service/formatters/format_configs.test.ts index c30d9af766b48..48d052d35a1f8 100644 --- a/x-pack/plugins/synthetics/server/synthetics_service/formatters/format_configs.test.ts +++ b/x-pack/plugins/synthetics/server/synthetics_service/formatters/format_configs.test.ts @@ -53,6 +53,7 @@ describe('formatMonitorConfig', () => { expect(yamlConfig).toEqual({ 'check.request.method': 'GET', enabled: true, + locations: [], max_redirects: '0', name: 'Test', password: '3z9SBOQWW5F0UrdqLVFqlF6z', @@ -110,6 +111,7 @@ describe('formatMonitorConfig', () => { 'filter_journeys.tags': ['dev'], ignore_https_errors: false, name: 'Test', + locations: [], schedule: '@every 3m', screenshots: 'on', 'source.inline.script': diff --git a/x-pack/plugins/synthetics/server/synthetics_service/formatters/format_configs.ts b/x-pack/plugins/synthetics/server/synthetics_service/formatters/format_configs.ts index e2a1bf1b869ed..ea298992d2246 100644 --- a/x-pack/plugins/synthetics/server/synthetics_service/formatters/format_configs.ts +++ b/x-pack/plugins/synthetics/server/synthetics_service/formatters/format_configs.ts @@ -15,7 +15,6 @@ const UI_KEYS_TO_SKIP = [ ConfigKey.DOWNLOAD_SPEED, ConfigKey.LATENCY, ConfigKey.IS_THROTTLING_ENABLED, - ConfigKey.LOCATIONS, ConfigKey.REVISION, 'secrets', ]; diff --git a/x-pack/plugins/synthetics/server/synthetics_service/synthetics_service.test.ts b/x-pack/plugins/synthetics/server/synthetics_service/synthetics_service.test.ts index 305f1d15a4823..952e18ce9c884 100644 --- a/x-pack/plugins/synthetics/server/synthetics_service/synthetics_service.test.ts +++ b/x-pack/plugins/synthetics/server/synthetics_service/synthetics_service.test.ts @@ -5,10 +5,13 @@ * 2.0. */ -import { SyntheticsService } from './synthetics_service'; +jest.mock('axios', () => jest.fn()); + +import { SyntheticsService, SyntheticsConfig } from './synthetics_service'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths import { loggerMock } from '@kbn/core/server/logging/logger.mock'; import { UptimeServerSetup } from '../legacy_uptime/lib/adapters'; +import axios, { AxiosResponse } from 'axios'; describe('SyntheticsService', () => { const mockEsClient = { @@ -67,4 +70,71 @@ describe('SyntheticsService', () => { }, ]); }); + + describe('addConfig', () => { + afterEach(() => jest.restoreAllMocks()); + + it('saves configs only to the selected locations', async () => { + serverMock.config = { service: { devUrl: 'http://localhost' } }; + const service = new SyntheticsService(logger, serverMock, { + username: 'dev', + password: '12345', + }); + + service.apiClient.locations = [ + { + id: 'selected', + label: 'Selected Location', + url: 'example.com/1', + geo: { + lat: 0, + lon: 0, + }, + isServiceManaged: true, + }, + { + id: 'not selected', + label: 'Not Selected Location', + url: 'example.com/2', + geo: { + lat: 0, + lon: 0, + }, + isServiceManaged: true, + }, + ]; + + jest.spyOn(service, 'getApiKey').mockResolvedValue({ name: 'example', id: 'i', apiKey: 'k' }); + jest.spyOn(service, 'getOutput').mockResolvedValue({ hosts: ['es'], api_key: 'i:k' }); + + const payload = { + type: 'http', + enabled: true, + schedule: { + number: '3', + unit: 'm', + }, + name: 'my mon', + locations: [{ id: 'selected', isServiceManaged: true }], + urls: 'http://google.com', + max_redirects: '0', + password: '', + proxy_url: '', + id: '7af7e2f0-d5dc-11ec-87ac-bdfdb894c53d', + fields: { config_id: '7af7e2f0-d5dc-11ec-87ac-bdfdb894c53d' }, + fields_under_root: true, + }; + + (axios as jest.MockedFunction).mockResolvedValue({} as AxiosResponse); + + await service.addConfig(payload as SyntheticsConfig); + + expect(axios).toHaveBeenCalledTimes(1); + expect(axios).toHaveBeenCalledWith( + expect.objectContaining({ + url: 'example.com/1/monitors', + }) + ); + }); + }); }); diff --git a/x-pack/plugins/synthetics/server/synthetics_service/synthetics_service.ts b/x-pack/plugins/synthetics/server/synthetics_service/synthetics_service.ts index f655dd6d4cc8c..b1af1717e1a1c 100644 --- a/x-pack/plugins/synthetics/server/synthetics_service/synthetics_service.ts +++ b/x-pack/plugins/synthetics/server/synthetics_service/synthetics_service.ts @@ -48,7 +48,7 @@ const SYNTHETICS_SERVICE_SYNC_MONITORS_TASK_TYPE = const SYNTHETICS_SERVICE_SYNC_MONITORS_TASK_ID = 'UPTIME:SyntheticsService:sync-task'; const SYNTHETICS_SERVICE_SYNC_INTERVAL_DEFAULT = '5m'; -type SyntheticsConfig = SyntheticsMonitorWithId & { +export type SyntheticsConfig = SyntheticsMonitorWithId & { fields_under_root?: boolean; fields?: { config_id: string; run_once?: boolean; test_run_id?: string }; }; @@ -56,7 +56,7 @@ type SyntheticsConfig = SyntheticsMonitorWithId & { export class SyntheticsService { private logger: Logger; private readonly server: UptimeServerSetup; - private apiClient: ServiceAPIClient; + public apiClient: ServiceAPIClient; private readonly config: ServiceConfig; private readonly esHosts: string[];