From cb0dae7c1b8282d7a52d28eefaadb3be2558483c Mon Sep 17 00:00:00 2001 From: Ersin Erdal <92688503+ersin-erdal@users.noreply.github.com> Date: Tue, 26 Nov 2024 11:12:23 +0100 Subject: [PATCH] Delete connector usage reporting task in on-prem and cloud (#199650) Towards: https://github.com/elastic/response-ops-team/issues/209 `connector_usage_reporting` task is supposed to work only in serverless. As there is no check for that, even though it reports nothing, It was working for on-prem and cloud as well. This PR deletes the `connector_usage_reporting` task after the first run by checking if `plugins.cloud.serverless.projectId` is missing. ### To verify: Run Kibana on this branch, then check the reporting task in the console by running the below query. There shouldn't be any task. ``` GET .kibana_task_manager_*/_search { "query": { "prefix": { "task.taskType": { "value": "actions:" } } } } ``` Then remove or comment out the line (`shouldDeleteTask: true`) added by this PR and save the file, Kibana will restart and register the task again. You can use the above query again to see that the task is still registered. --- x-pack/plugins/actions/server/plugin.ts | 2 ++ .../connector_usage_reporting_task.test.ts | 26 ++++++++++++++++++- .../usage/connector_usage_reporting_task.ts | 6 ++++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/actions/server/plugin.ts b/x-pack/plugins/actions/server/plugin.ts index 57304c176c13d..edfd7606950c1 100644 --- a/x-pack/plugins/actions/server/plugin.ts +++ b/x-pack/plugins/actions/server/plugin.ts @@ -140,6 +140,7 @@ export interface PluginSetupContract { getActionsHealth: () => { hasPermanentEncryptionKey: boolean }; getActionsConfigurationUtilities: () => ActionsConfigurationUtilities; setEnabledConnectorTypes: (connectorTypes: EnabledConnectorTypes) => void; + isActionTypeEnabled(id: string, options?: { notifyUsage: boolean }): boolean; } @@ -168,6 +169,7 @@ export interface PluginStartContract { params: Params, variables: Record ): Params; + isSystemActionConnector: (connectorId: string) => boolean; } diff --git a/x-pack/plugins/actions/server/usage/connector_usage_reporting_task.test.ts b/x-pack/plugins/actions/server/usage/connector_usage_reporting_task.test.ts index 77dec7f15e156..2c09576cc2a2b 100644 --- a/x-pack/plugins/actions/server/usage/connector_usage_reporting_task.test.ts +++ b/x-pack/plugins/actions/server/usage/connector_usage_reporting_task.test.ts @@ -230,10 +230,11 @@ describe('ConnectorUsageReportingTask', () => { const response = await taskRunner.run(); expect(logger.warn).toHaveBeenCalledWith( - 'Missing required project id while running actions:connector_usage_reporting' + 'Missing required project id while running actions:connector_usage_reporting, reporting task will be deleted' ); expect(response).toEqual({ + shouldDeleteTask: true, state: { attempts: 0, lastReportedUsageDate, @@ -391,4 +392,27 @@ describe('ConnectorUsageReportingTask', () => { 'Usage data could not be pushed to usage-api. Stopped retrying after 5 attempts. Error:test-error' ); }); + + it('does not schedule the task when the project id is missing', async () => { + const core = createSetup(); + const taskManagerStart = taskManagerStartMock(); + + const task = new ConnectorUsageReportingTask({ + eventLogIndex: 'test-index', + projectId: undefined, + logger, + core, + taskManager: mockTaskManagerSetup, + config: { + url: 'usage-api', + ca: { + path: './ca.crt', + }, + }, + }); + + await task.start(taskManagerStart); + + expect(taskManagerStart.ensureScheduled).not.toBeCalled(); + }); }); diff --git a/x-pack/plugins/actions/server/usage/connector_usage_reporting_task.ts b/x-pack/plugins/actions/server/usage/connector_usage_reporting_task.ts index ce44718749006..ddaa930b15c34 100644 --- a/x-pack/plugins/actions/server/usage/connector_usage_reporting_task.ts +++ b/x-pack/plugins/actions/server/usage/connector_usage_reporting_task.ts @@ -82,6 +82,9 @@ export class ConnectorUsageReportingTask { } public start = async (taskManager?: TaskManagerStartContract) => { + if (!this.projectId) { + return; + } if (!taskManager) { this.logger.error( `Missing required task manager service during start of ${CONNECTOR_USAGE_REPORTING_TASK_TYPE}` @@ -111,10 +114,11 @@ export class ConnectorUsageReportingTask { if (!this.projectId) { this.logger.warn( - `Missing required project id while running ${CONNECTOR_USAGE_REPORTING_TASK_TYPE}` + `Missing required project id while running ${CONNECTOR_USAGE_REPORTING_TASK_TYPE}, reporting task will be deleted` ); return { state, + shouldDeleteTask: true, }; }