From 34e2168734b1dcebe2dd03fabfab378eebb84c57 Mon Sep 17 00:00:00 2001 From: chrisronline Date: Mon, 20 Sep 2021 15:56:15 -0400 Subject: [PATCH 01/10] Config telemetry --- x-pack/plugins/actions/server/config.ts | 9 +++++++++ x-pack/plugins/actions/server/index.ts | 4 ++++ x-pack/plugins/alerting/server/index.ts | 7 +++++++ x-pack/plugins/task_manager/server/index.ts | 3 +++ 4 files changed, 23 insertions(+) diff --git a/x-pack/plugins/actions/server/config.ts b/x-pack/plugins/actions/server/config.ts index 54fd0d72bccee..9cf2ae067d725 100644 --- a/x-pack/plugins/actions/server/config.ts +++ b/x-pack/plugins/actions/server/config.ts @@ -58,6 +58,15 @@ export type CustomHostSettings = TypeOf; export const configSchema = schema.object({ enabled: schema.boolean({ defaultValue: true }), + /** + * @deprecated in favor of `allowedHosts` + **/ + whitelistedHosts: schema.arrayOf( + schema.oneOf([schema.string({ hostname: true }), schema.literal(AllowedHosts.Any)]), + { + defaultValue: [AllowedHosts.Any], + } + ), allowedHosts: schema.arrayOf( schema.oneOf([schema.string({ hostname: true }), schema.literal(AllowedHosts.Any)]), { diff --git a/x-pack/plugins/actions/server/index.ts b/x-pack/plugins/actions/server/index.ts index bf59a1a11687d..c0205898d186f 100644 --- a/x-pack/plugins/actions/server/index.ts +++ b/x-pack/plugins/actions/server/index.ts @@ -56,6 +56,10 @@ export const plugin = (initContext: PluginInitializerContext) => new ActionsPlug export const config: PluginConfigDescriptor = { schema: configSchema, + exposeToUsage: { + rejectUnauthorized: true, + proxyRejectUnauthorizedCertificates: true, + }, deprecations: ({ renameFromRoot, unused }) => [ renameFromRoot('xpack.actions.whitelistedHosts', 'xpack.actions.allowedHosts'), (settings, fromPath, addDeprecation) => { diff --git a/x-pack/plugins/alerting/server/index.ts b/x-pack/plugins/alerting/server/index.ts index 3b4688173e9b5..3d593963ef13b 100644 --- a/x-pack/plugins/alerting/server/index.ts +++ b/x-pack/plugins/alerting/server/index.ts @@ -48,6 +48,13 @@ export const plugin = (initContext: PluginInitializerContext) => new AlertingPlu export const config: PluginConfigDescriptor = { schema: configSchema, + exposeToUsage: { + healthCheck: true, + invalidateApiKeysTask: { + interval: true, + removalDelay: true, + }, + }, deprecations: ({ renameFromRoot }) => [ renameFromRoot('xpack.alerts.healthCheck', 'xpack.alerting.healthCheck'), renameFromRoot( diff --git a/x-pack/plugins/task_manager/server/index.ts b/x-pack/plugins/task_manager/server/index.ts index a9b24fdf545a1..78cba6e1cfbce 100644 --- a/x-pack/plugins/task_manager/server/index.ts +++ b/x-pack/plugins/task_manager/server/index.ts @@ -38,6 +38,9 @@ export { export const config: PluginConfigDescriptor = { schema: configSchema, + exposeToUsage: { + max_workers: true, + }, deprecations: () => [ (settings, fromPath, addDeprecation) => { const taskManager = get(settings, fromPath); From 8664894e8b977630e0267f52494423c1fc5452b0 Mon Sep 17 00:00:00 2001 From: chrisronline Date: Tue, 21 Sep 2021 11:43:32 -0400 Subject: [PATCH 02/10] Fix tests --- x-pack/plugins/actions/server/config.test.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/x-pack/plugins/actions/server/config.test.ts b/x-pack/plugins/actions/server/config.test.ts index d99b9349e977b..ceeb51335eeca 100644 --- a/x-pack/plugins/actions/server/config.test.ts +++ b/x-pack/plugins/actions/server/config.test.ts @@ -41,6 +41,9 @@ describe('config validation', () => { "proxyRejectUnauthorizedCertificates": true, "rejectUnauthorized": true, "responseTimeout": "PT1M", + "whitelistedHosts": Array [ + "*", + ], } `); }); @@ -91,6 +94,9 @@ describe('config validation', () => { "proxyRejectUnauthorizedCertificates": false, "rejectUnauthorized": false, "responseTimeout": "PT1M", + "whitelistedHosts": Array [ + "*", + ], } `); }); @@ -212,6 +218,9 @@ describe('config validation', () => { "proxyVerificationMode": "none", "verificationMode": "none", }, + "whitelistedHosts": Array [ + "*", + ], } `); }); From c92f5c46487e0cd5f5f4a02c5ede7b7772e959b5 Mon Sep 17 00:00:00 2001 From: chrisronline Date: Tue, 21 Sep 2021 12:41:57 -0400 Subject: [PATCH 03/10] Custom rename --- x-pack/plugins/actions/server/index.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/actions/server/index.ts b/x-pack/plugins/actions/server/index.ts index c0205898d186f..1be2f4f75746b 100644 --- a/x-pack/plugins/actions/server/index.ts +++ b/x-pack/plugins/actions/server/index.ts @@ -57,11 +57,32 @@ export const plugin = (initContext: PluginInitializerContext) => new ActionsPlug export const config: PluginConfigDescriptor = { schema: configSchema, exposeToUsage: { + whitelistedHosts: true, rejectUnauthorized: true, proxyRejectUnauthorizedCertificates: true, }, deprecations: ({ renameFromRoot, unused }) => [ - renameFromRoot('xpack.actions.whitelistedHosts', 'xpack.actions.allowedHosts'), + // Use a custom copy function here so we can perserve the telemetry provided for the deprecated config + // See https://github.com/elastic/kibana/issues/112585#issuecomment-923715363 + (settings, fromPath, addDeprecation) => { + const fullOldPath = 'xpack.actions.whitelistedHosts'; + const fullNewPath = 'xpack.actions.allowedHosts'; + const actions = get(settings, fromPath); + const whitelistedHosts = actions?.whitelistedHosts; + if (whitelistedHosts) { + addDeprecation({ + message: `Setting "${fullOldPath}" has been replaced by "${fullNewPath}"`, + correctiveActions: { + manualSteps: [ + `Replace "${fullOldPath}" with "${fullNewPath}" in the Kibana config file, CLI flag, or environment variable (in Docker only).`, + ], + }, + }); + return { + set: [{ path: fullNewPath, value: whitelistedHosts }], + }; + } + }, (settings, fromPath, addDeprecation) => { const actions = get(settings, fromPath); const customHostSettings = actions?.customHostSettings ?? []; From 90d8b2f41e404ed298b0ebb7093d4afbc6907041 Mon Sep 17 00:00:00 2001 From: chrisronline Date: Tue, 21 Sep 2021 13:13:44 -0400 Subject: [PATCH 04/10] Make optional --- x-pack/plugins/actions/server/config.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/x-pack/plugins/actions/server/config.ts b/x-pack/plugins/actions/server/config.ts index 9cf2ae067d725..a288c54bb0b0a 100644 --- a/x-pack/plugins/actions/server/config.ts +++ b/x-pack/plugins/actions/server/config.ts @@ -59,13 +59,15 @@ export type CustomHostSettings = TypeOf; export const configSchema = schema.object({ enabled: schema.boolean({ defaultValue: true }), /** - * @deprecated in favor of `allowedHosts` + * @deprecated in favor of `allowedHostsj` **/ - whitelistedHosts: schema.arrayOf( - schema.oneOf([schema.string({ hostname: true }), schema.literal(AllowedHosts.Any)]), - { - defaultValue: [AllowedHosts.Any], - } + whitelistedHosts: schema.maybe( + schema.arrayOf( + schema.oneOf([schema.string({ hostname: true }), schema.literal(AllowedHosts.Any)]), + { + defaultValue: [AllowedHosts.Any], + } + ) ), allowedHosts: schema.arrayOf( schema.oneOf([schema.string({ hostname: true }), schema.literal(AllowedHosts.Any)]), From eb127218efa60dab37bc405d33c7c3e2e2fa7eab Mon Sep 17 00:00:00 2001 From: chrisronline Date: Wed, 22 Sep 2021 13:44:31 -0400 Subject: [PATCH 05/10] PR feedback --- .../server/routes/stats/stats.ts | 2 +- x-pack/plugins/actions/server/config.ts | 2 +- x-pack/plugins/actions/server/index.test.ts | 22 ++++++++++++++++--- x-pack/plugins/actions/server/index.ts | 16 ++++++-------- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/plugins/usage_collection/server/routes/stats/stats.ts b/src/plugins/usage_collection/server/routes/stats/stats.ts index 6cae56afa281b..f1998ec391ab4 100644 --- a/src/plugins/usage_collection/server/routes/stats/stats.ts +++ b/src/plugins/usage_collection/server/routes/stats/stats.ts @@ -103,7 +103,7 @@ export function registerStatsRoute({ if (shouldGetUsage) { const collectorsReady = await collectorSet.areAllCollectorsReady(); if (!collectorsReady) { - return res.customError({ statusCode: 503, body: { message: STATS_NOT_READY_MESSAGE } }); + // return res.customError({ statusCode: 503, body: { message: STATS_NOT_READY_MESSAGE } }); } } diff --git a/x-pack/plugins/actions/server/config.ts b/x-pack/plugins/actions/server/config.ts index a288c54bb0b0a..f4539c93352ec 100644 --- a/x-pack/plugins/actions/server/config.ts +++ b/x-pack/plugins/actions/server/config.ts @@ -59,7 +59,7 @@ export type CustomHostSettings = TypeOf; export const configSchema = schema.object({ enabled: schema.boolean({ defaultValue: true }), /** - * @deprecated in favor of `allowedHostsj` + * @deprecated in favor of `allowedHosts` **/ whitelistedHosts: schema.maybe( schema.arrayOf( diff --git a/x-pack/plugins/actions/server/index.test.ts b/x-pack/plugins/actions/server/index.test.ts index dbe8fca806f17..f23ddcb3a611a 100644 --- a/x-pack/plugins/actions/server/index.test.ts +++ b/x-pack/plugins/actions/server/index.test.ts @@ -14,18 +14,20 @@ const applyStackAlertDeprecations = (settings: Record = {}) => const _config = { [CONFIG_PATH]: settings, }; - const { config: migrated } = applyDeprecations( + const { config: migrated, changedPaths } = applyDeprecations( _config, deprecations.map((deprecation) => ({ deprecation, path: CONFIG_PATH, })), () => - ({ message }) => - deprecationMessages.push(message) + ({ message }) => { + deprecationMessages.push(message); + } ); return { messages: deprecationMessages, + changedPaths, migrated, }; }; @@ -40,5 +42,19 @@ describe('index', () => { ] `); }); + + it('should perform a custom set on deprecated and removed configs', () => { + jest.spyOn(configDeprecationFactory, 'renameFromRoot'); + + const { changedPaths } = applyStackAlertDeprecations({ + whitelistedHosts: ['smtp.gmail.com'], + }); + + expect(configDeprecationFactory.renameFromRoot).toHaveBeenCalledWith( + 'xpack.actions.whitelistedHosts', + 'xpack.actions.allowedHosts' + ); + expect(changedPaths).toStrictEqual({ set: ['xpack.actions.allowedHosts'], unset: [] }); + }); }); }); diff --git a/x-pack/plugins/actions/server/index.ts b/x-pack/plugins/actions/server/index.ts index 1be2f4f75746b..fa0a5801c985c 100644 --- a/x-pack/plugins/actions/server/index.ts +++ b/x-pack/plugins/actions/server/index.ts @@ -61,7 +61,7 @@ export const config: PluginConfigDescriptor = { rejectUnauthorized: true, proxyRejectUnauthorizedCertificates: true, }, - deprecations: ({ renameFromRoot, unused }) => [ + deprecations: ({ renameFromRoot, unused, rename }) => [ // Use a custom copy function here so we can perserve the telemetry provided for the deprecated config // See https://github.com/elastic/kibana/issues/112585#issuecomment-923715363 (settings, fromPath, addDeprecation) => { @@ -69,19 +69,17 @@ export const config: PluginConfigDescriptor = { const fullNewPath = 'xpack.actions.allowedHosts'; const actions = get(settings, fromPath); const whitelistedHosts = actions?.whitelistedHosts; + const renameFn = renameFromRoot(fullOldPath, fullNewPath); + const result = renameFn(settings, fromPath, addDeprecation); + + // If it is set, make sure we return custom logic to ensure the usage is tracked if (whitelistedHosts) { - addDeprecation({ - message: `Setting "${fullOldPath}" has been replaced by "${fullNewPath}"`, - correctiveActions: { - manualSteps: [ - `Replace "${fullOldPath}" with "${fullNewPath}" in the Kibana config file, CLI flag, or environment variable (in Docker only).`, - ], - }, - }); return { set: [{ path: fullNewPath, value: whitelistedHosts }], }; } + + return result; }, (settings, fromPath, addDeprecation) => { const actions = get(settings, fromPath); From 8e5842e6897d6cee8254a887d43b1d713b2ed171 Mon Sep 17 00:00:00 2001 From: chrisronline Date: Wed, 22 Sep 2021 17:00:58 -0400 Subject: [PATCH 06/10] Distinguishing between legacy and updated path --- x-pack/plugins/actions/server/index.test.ts | 11 +-- x-pack/plugins/actions/server/index.ts | 69 +++++++++++++------ x-pack/plugins/alerting/server/config.ts | 12 ++++ x-pack/plugins/alerting/server/index.test.ts | 52 +++++++++++++-- x-pack/plugins/alerting/server/index.ts | 70 +++++++++++++++++--- x-pack/plugins/alerting/server/types.ts | 5 ++ 6 files changed, 178 insertions(+), 41 deletions(-) diff --git a/x-pack/plugins/actions/server/index.test.ts b/x-pack/plugins/actions/server/index.test.ts index f23ddcb3a611a..6c97ffa1ffa9b 100644 --- a/x-pack/plugins/actions/server/index.test.ts +++ b/x-pack/plugins/actions/server/index.test.ts @@ -6,14 +6,14 @@ */ import { config } from './index'; import { applyDeprecations, configDeprecationFactory } from '@kbn/config'; +import { set } from '@elastic/safer-lodash-set'; const CONFIG_PATH = 'xpack.actions'; const applyStackAlertDeprecations = (settings: Record = {}) => { const deprecations = config.deprecations!(configDeprecationFactory); const deprecationMessages: string[] = []; - const _config = { - [CONFIG_PATH]: settings, - }; + const _config = {}; + set(_config, CONFIG_PATH, settings); const { config: migrated, changedPaths } = applyDeprecations( _config, deprecations.map((deprecation) => ({ @@ -54,7 +54,10 @@ describe('index', () => { 'xpack.actions.whitelistedHosts', 'xpack.actions.allowedHosts' ); - expect(changedPaths).toStrictEqual({ set: ['xpack.actions.allowedHosts'], unset: [] }); + expect(changedPaths).toStrictEqual({ + set: ['xpack.actions.allowedHosts'], + unset: ['xpack.actions.whitelistedHosts'], + }); }); }); }); diff --git a/x-pack/plugins/actions/server/index.ts b/x-pack/plugins/actions/server/index.ts index fa0a5801c985c..e71d97a8ad154 100644 --- a/x-pack/plugins/actions/server/index.ts +++ b/x-pack/plugins/actions/server/index.ts @@ -5,7 +5,8 @@ * 2.0. */ import { get } from 'lodash'; -import type { PublicMethodsOf } from '@kbn/utility-types'; +import { ConfigDeprecation, AddConfigDeprecation } from 'kibana/server'; +import type { PublicMethodsOf, RecursiveReadonly } from '@kbn/utility-types'; import { PluginInitializerContext, PluginConfigDescriptor } from '../../../../src/core/server'; import { ActionsPlugin } from './plugin'; import { configSchema, ActionsConfig, CustomHostSettings } from './config'; @@ -54,6 +55,48 @@ export { ACTION_SAVED_OBJECT_TYPE } from './constants/saved_objects'; export const plugin = (initContext: PluginInitializerContext) => new ActionsPlugin(initContext); +// Use a custom copy function here so we can perserve the telemetry provided for the deprecated config +// See https://github.com/elastic/kibana/issues/112585#issuecomment-923715363 +function renameFromRootAndTrackDeprecatedUsage( + { + renameFromRoot, + }: { + renameFromRoot(oldKey: string, newKey: string): ConfigDeprecation; + }, + oldPath: string, + newPath: string, + renamedNewPath?: string +) { + return ( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + settings: RecursiveReadonly>, + fromPath: string, + addDeprecation: AddConfigDeprecation + ) => { + // const actions = get(settings, fromPath); + const value = get(settings, oldPath); + const renameFn = renameFromRoot(oldPath, newPath); + + const result = renameFn(settings, fromPath, addDeprecation); + + // If it is set, make sure we return custom logic to ensure the usage is tracked + if (value) { + const unsets = []; + const sets = [{ path: newPath, value }]; + if (renamedNewPath) { + sets.push({ path: renamedNewPath, value }); + unsets.push({ path: oldPath }); + } + return { + set: sets, + unset: unsets, + }; + } + + return result; + }; +} + export const config: PluginConfigDescriptor = { schema: configSchema, exposeToUsage: { @@ -62,25 +105,11 @@ export const config: PluginConfigDescriptor = { proxyRejectUnauthorizedCertificates: true, }, deprecations: ({ renameFromRoot, unused, rename }) => [ - // Use a custom copy function here so we can perserve the telemetry provided for the deprecated config - // See https://github.com/elastic/kibana/issues/112585#issuecomment-923715363 - (settings, fromPath, addDeprecation) => { - const fullOldPath = 'xpack.actions.whitelistedHosts'; - const fullNewPath = 'xpack.actions.allowedHosts'; - const actions = get(settings, fromPath); - const whitelistedHosts = actions?.whitelistedHosts; - const renameFn = renameFromRoot(fullOldPath, fullNewPath); - const result = renameFn(settings, fromPath, addDeprecation); - - // If it is set, make sure we return custom logic to ensure the usage is tracked - if (whitelistedHosts) { - return { - set: [{ path: fullNewPath, value: whitelistedHosts }], - }; - } - - return result; - }, + renameFromRootAndTrackDeprecatedUsage( + { renameFromRoot }, + 'xpack.actions.whitelistedHosts', + 'xpack.actions.allowedHosts' + ), (settings, fromPath, addDeprecation) => { const actions = get(settings, fromPath); const customHostSettings = actions?.customHostSettings ?? []; diff --git a/x-pack/plugins/alerting/server/config.ts b/x-pack/plugins/alerting/server/config.ts index 47ef451ceab92..a11744f1dfd80 100644 --- a/x-pack/plugins/alerting/server/config.ts +++ b/x-pack/plugins/alerting/server/config.ts @@ -13,6 +13,18 @@ export const configSchema = schema.object({ healthCheck: schema.object({ interval: schema.string({ validate: validateDurationSchema, defaultValue: '60m' }), }), + /* + * Do not use + */ + legacyTrackingPurposes: schema.object({ + healthCheck: schema.object({ + interval: schema.string({ validate: validateDurationSchema, defaultValue: '60m' }), + }), + invalidateApiKeysTask: schema.object({ + interval: schema.string({ validate: validateDurationSchema, defaultValue: '5m' }), + removalDelay: schema.string({ validate: validateDurationSchema, defaultValue: '1h' }), + }), + }), invalidateApiKeysTask: schema.object({ interval: schema.string({ validate: validateDurationSchema, defaultValue: '5m' }), removalDelay: schema.string({ validate: validateDurationSchema, defaultValue: '1h' }), diff --git a/x-pack/plugins/alerting/server/index.test.ts b/x-pack/plugins/alerting/server/index.test.ts index b1e64935d7cd9..98ed1eea23e07 100644 --- a/x-pack/plugins/alerting/server/index.test.ts +++ b/x-pack/plugins/alerting/server/index.test.ts @@ -6,15 +6,15 @@ */ import { config } from './index'; import { applyDeprecations, configDeprecationFactory } from '@kbn/config'; +import { set } from '@elastic/safer-lodash-set'; const CONFIG_PATH = 'xpack.alerting'; -const applyStackAlertDeprecations = (settings: Record = {}) => { +const applyStackAlertDeprecations = (_config: Record = {}) => { const deprecations = config.deprecations!(configDeprecationFactory); const deprecationMessages: string[] = []; - const _config = { - [CONFIG_PATH]: settings, - }; - const { config: migrated } = applyDeprecations( + // const _config = {}; + // set(_config, configPath, settings); + const { config: migrated, changedPaths } = applyDeprecations( _config, deprecations.map((deprecation) => ({ deprecation, @@ -27,18 +27,58 @@ const applyStackAlertDeprecations = (settings: Record = {}) => return { messages: deprecationMessages, migrated, + changedPaths, }; }; describe('index', () => { describe('deprecations', () => { it('should deprecate .enabled flag', () => { - const { messages } = applyStackAlertDeprecations({ enabled: false }); + const { messages } = applyStackAlertDeprecations({ [CONFIG_PATH]: { enabled: false } }); expect(messages).toMatchInlineSnapshot(` Array [ "\\"xpack.alerting.enabled\\" is deprecated. The ability to disable this plugin will be removed in 8.0.0.", ] `); }); + + it('should perform a custom set on deprecated and removed configs', () => { + jest.spyOn(configDeprecationFactory, 'renameFromRoot'); + + const customConfig = {}; + set(customConfig, 'xpack.alerts', { + healthCheck: { interval: '30m' }, + invalidateApiKeysTask: { interval: '30m', removalDelay: '1d' }, + }); + const { changedPaths } = applyStackAlertDeprecations(customConfig); + expect(configDeprecationFactory.renameFromRoot).toHaveBeenCalledTimes(3); + expect((configDeprecationFactory.renameFromRoot as jest.Mock).mock.calls[0]).toEqual([ + 'xpack.alerts.healthCheck.interval', + 'xpack.alerting.healthCheck.interval', + ]); + expect((configDeprecationFactory.renameFromRoot as jest.Mock).mock.calls[1]).toEqual([ + 'xpack.alerts.invalidateApiKeysTask.interval', + 'xpack.alerting.invalidateApiKeysTask.interval', + ]); + expect((configDeprecationFactory.renameFromRoot as jest.Mock).mock.calls[2]).toEqual([ + 'xpack.alerts.invalidateApiKeysTask.removalDelay', + 'xpack.alerting.invalidateApiKeysTask.removalDelay', + ]); + expect(changedPaths).toStrictEqual({ + set: [ + 'xpack.alerting.healthCheck.interval', + 'xpack.alerting.legacyTrackingPurposes.healthCheck.interval', + 'xpack.alerting.invalidateApiKeysTask.interval', + 'xpack.alerting.legacyTrackingPurposes.invalidateApiKeysTask.interval', + 'xpack.alerting.invalidateApiKeysTask.removalDelay', + 'xpack.alerting.legacyTrackingPurposes.invalidateApiKeysTask.removalDelay', + ], + unset: [ + 'xpack.alerts.healthCheck.interval', + 'xpack.alerts.invalidateApiKeysTask.interval', + 'xpack.alerts.invalidateApiKeysTask.removalDelay', + ], + }); + }); }); }); diff --git a/x-pack/plugins/alerting/server/index.ts b/x-pack/plugins/alerting/server/index.ts index 3d593963ef13b..18be41b9c1679 100644 --- a/x-pack/plugins/alerting/server/index.ts +++ b/x-pack/plugins/alerting/server/index.ts @@ -5,7 +5,8 @@ * 2.0. */ import { get } from 'lodash'; -import type { PublicMethodsOf } from '@kbn/utility-types'; +import { ConfigDeprecation, AddConfigDeprecation } from 'kibana/server'; +import type { PublicMethodsOf, RecursiveReadonly } from '@kbn/utility-types'; import { RulesClient as RulesClientClass } from './rules_client'; import { PluginConfigDescriptor, PluginInitializerContext } from '../../../../src/core/server'; import { AlertingPlugin } from './plugin'; @@ -46,24 +47,71 @@ export { export const plugin = (initContext: PluginInitializerContext) => new AlertingPlugin(initContext); +// Use a custom copy function here so we can perserve the telemetry provided for the deprecated config +// See https://github.com/elastic/kibana/issues/112585#issuecomment-923715363 +function renameFromRootAndTrackDeprecatedUsage( + { + renameFromRoot, + }: { + renameFromRoot(oldKey: string, newKey: string): ConfigDeprecation; + }, + oldPath: string, + newPath: string, + renamedNewPath?: string +) { + return ( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + settings: RecursiveReadonly>, + fromPath: string, + addDeprecation: AddConfigDeprecation + ) => { + // const actions = get(settings, fromPath); + const value = get(settings, oldPath); + const renameFn = renameFromRoot(oldPath, newPath); + + const result = renameFn(settings, fromPath, addDeprecation); + + // If it is set, make sure we return custom logic to ensure the usage is tracked + if (value) { + const unsets = []; + const sets = [{ path: newPath, value }]; + if (renamedNewPath) { + sets.push({ path: renamedNewPath, value }); + unsets.push({ path: oldPath }); + } + return { + set: sets, + unset: unsets, + }; + } + + return result; + }; +} + export const config: PluginConfigDescriptor = { schema: configSchema, exposeToUsage: { - healthCheck: true, - invalidateApiKeysTask: { - interval: true, - removalDelay: true, - }, + legacyTrackingPurposes: true, }, deprecations: ({ renameFromRoot }) => [ - renameFromRoot('xpack.alerts.healthCheck', 'xpack.alerting.healthCheck'), - renameFromRoot( + renameFromRootAndTrackDeprecatedUsage( + { renameFromRoot }, + 'xpack.alerts.healthCheck.interval', + 'xpack.alerting.healthCheck.interval', + 'xpack.alerting.legacyTrackingPurposes.healthCheck.interval' + ), + renameFromRootAndTrackDeprecatedUsage( + { renameFromRoot }, 'xpack.alerts.invalidateApiKeysTask.interval', - 'xpack.alerting.invalidateApiKeysTask.interval' + 'xpack.alerting.invalidateApiKeysTask.interval', + 'xpack.alerting.legacyTrackingPurposes.invalidateApiKeysTask.interval' ), - renameFromRoot( + renameFromRootAndTrackDeprecatedUsage( + { renameFromRoot }, 'xpack.alerts.invalidateApiKeysTask.removalDelay', - 'xpack.alerting.invalidateApiKeysTask.removalDelay' + 'xpack.alerting.invalidateApiKeysTask.removalDelay', + 'xpack.alerting.legacyTrackingPurposes.invalidateApiKeysTask.removalDelay' ), (settings, fromPath, addDeprecation) => { const alerting = get(settings, fromPath); diff --git a/x-pack/plugins/alerting/server/types.ts b/x-pack/plugins/alerting/server/types.ts index ba35890efd781..c3e2f91e265d5 100644 --- a/x-pack/plugins/alerting/server/types.ts +++ b/x-pack/plugins/alerting/server/types.ts @@ -254,6 +254,11 @@ export interface AlertsConfigType { healthCheck: { interval: string; }; + legacyTrackingPurposes: { + healthCheck: { + interval: string; + }; + }; } export interface AlertsConfigType { From 05f6942654e893d72c81a10c4de18893e9f78450 Mon Sep 17 00:00:00 2001 From: chrisronline Date: Wed, 22 Sep 2021 17:03:02 -0400 Subject: [PATCH 07/10] Fix tests --- x-pack/plugins/actions/server/config.test.ts | 9 --------- x-pack/plugins/alerting/server/config.test.ts | 9 +++++++++ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/x-pack/plugins/actions/server/config.test.ts b/x-pack/plugins/actions/server/config.test.ts index ceeb51335eeca..d99b9349e977b 100644 --- a/x-pack/plugins/actions/server/config.test.ts +++ b/x-pack/plugins/actions/server/config.test.ts @@ -41,9 +41,6 @@ describe('config validation', () => { "proxyRejectUnauthorizedCertificates": true, "rejectUnauthorized": true, "responseTimeout": "PT1M", - "whitelistedHosts": Array [ - "*", - ], } `); }); @@ -94,9 +91,6 @@ describe('config validation', () => { "proxyRejectUnauthorizedCertificates": false, "rejectUnauthorized": false, "responseTimeout": "PT1M", - "whitelistedHosts": Array [ - "*", - ], } `); }); @@ -218,9 +212,6 @@ describe('config validation', () => { "proxyVerificationMode": "none", "verificationMode": "none", }, - "whitelistedHosts": Array [ - "*", - ], } `); }); diff --git a/x-pack/plugins/alerting/server/config.test.ts b/x-pack/plugins/alerting/server/config.test.ts index a1ae77596ccbe..4e460a6a0fada 100644 --- a/x-pack/plugins/alerting/server/config.test.ts +++ b/x-pack/plugins/alerting/server/config.test.ts @@ -19,6 +19,15 @@ describe('config validation', () => { "interval": "5m", "removalDelay": "1h", }, + "legacyTrackingPurposes": Object { + "healthCheck": Object { + "interval": "60m", + }, + "invalidateApiKeysTask": Object { + "interval": "5m", + "removalDelay": "1h", + }, + }, "maxEphemeralActionsPerAlert": 10, } `); From 9c8d1cfc866e69de022de7743a970ca16945e3fa Mon Sep 17 00:00:00 2001 From: chrisronline Date: Wed, 22 Sep 2021 18:25:24 -0400 Subject: [PATCH 08/10] Remove unintentional change --- src/plugins/usage_collection/server/routes/stats/stats.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/usage_collection/server/routes/stats/stats.ts b/src/plugins/usage_collection/server/routes/stats/stats.ts index f1998ec391ab4..6cae56afa281b 100644 --- a/src/plugins/usage_collection/server/routes/stats/stats.ts +++ b/src/plugins/usage_collection/server/routes/stats/stats.ts @@ -103,7 +103,7 @@ export function registerStatsRoute({ if (shouldGetUsage) { const collectorsReady = await collectorSet.areAllCollectorsReady(); if (!collectorsReady) { - // return res.customError({ statusCode: 503, body: { message: STATS_NOT_READY_MESSAGE } }); + return res.customError({ statusCode: 503, body: { message: STATS_NOT_READY_MESSAGE } }); } } From 12aed8d939b113358ac0bce715ee6cd07733980b Mon Sep 17 00:00:00 2001 From: chrisronline Date: Thu, 23 Sep 2021 09:11:53 -0400 Subject: [PATCH 09/10] Fix tests --- x-pack/plugins/actions/server/index.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/actions/server/index.test.ts b/x-pack/plugins/actions/server/index.test.ts index 6c97ffa1ffa9b..56377eb53372c 100644 --- a/x-pack/plugins/actions/server/index.test.ts +++ b/x-pack/plugins/actions/server/index.test.ts @@ -56,7 +56,7 @@ describe('index', () => { ); expect(changedPaths).toStrictEqual({ set: ['xpack.actions.allowedHosts'], - unset: ['xpack.actions.whitelistedHosts'], + unset: [], }); }); }); From ed395cd000e4f9f27b893410a7c2f0aa69214be1 Mon Sep 17 00:00:00 2001 From: chrisronline Date: Thu, 23 Sep 2021 10:14:11 -0400 Subject: [PATCH 10/10] Fix types --- x-pack/plugins/alerting/server/config.ts | 20 +++++++++++--------- x-pack/plugins/alerting/server/types.ts | 2 +- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/x-pack/plugins/alerting/server/config.ts b/x-pack/plugins/alerting/server/config.ts index a11744f1dfd80..8836f7d56d125 100644 --- a/x-pack/plugins/alerting/server/config.ts +++ b/x-pack/plugins/alerting/server/config.ts @@ -16,15 +16,17 @@ export const configSchema = schema.object({ /* * Do not use */ - legacyTrackingPurposes: schema.object({ - healthCheck: schema.object({ - interval: schema.string({ validate: validateDurationSchema, defaultValue: '60m' }), - }), - invalidateApiKeysTask: schema.object({ - interval: schema.string({ validate: validateDurationSchema, defaultValue: '5m' }), - removalDelay: schema.string({ validate: validateDurationSchema, defaultValue: '1h' }), - }), - }), + legacyTrackingPurposes: schema.maybe( + schema.object({ + healthCheck: schema.object({ + interval: schema.string({ validate: validateDurationSchema, defaultValue: '60m' }), + }), + invalidateApiKeysTask: schema.object({ + interval: schema.string({ validate: validateDurationSchema, defaultValue: '5m' }), + removalDelay: schema.string({ validate: validateDurationSchema, defaultValue: '1h' }), + }), + }) + ), invalidateApiKeysTask: schema.object({ interval: schema.string({ validate: validateDurationSchema, defaultValue: '5m' }), removalDelay: schema.string({ validate: validateDurationSchema, defaultValue: '1h' }), diff --git a/x-pack/plugins/alerting/server/types.ts b/x-pack/plugins/alerting/server/types.ts index c3e2f91e265d5..f604a372a1b91 100644 --- a/x-pack/plugins/alerting/server/types.ts +++ b/x-pack/plugins/alerting/server/types.ts @@ -254,7 +254,7 @@ export interface AlertsConfigType { healthCheck: { interval: string; }; - legacyTrackingPurposes: { + legacyTrackingPurposes?: { healthCheck: { interval: string; };