Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { schema, type TypeOf } from '@kbn/config-schema';

/**
* WARNING: Do not modify the existing versioned schema(s) below, instead define a new version (ex: 2, 3, 4).
* This is required to support zero-downtime upgrades and rollbacks. See https://github.com/elastic/kibana/issues/155764.
*
* As you add a new schema version, don't forget to change latestTaskStateSchema variable to reference the latest schema.
* For example, changing stateSchemaByVersion[1].schema to stateSchemaByVersion[2].schema.
*/
export const stateSchemaByVersion = {
1: {
// A task that was created < 8.10 will go through this "up" migration
// to ensure it matches the v1 schema.
up: (state: Record<string, unknown>) => ({
lastSuccessfulReport: state.lastSuccessfulReport || null,
}),
schema: schema.object({
lastSuccessfulReport: schema.nullable(schema.string()),
}),
},
};

const latestTaskStateSchema = stateSchemaByVersion[1].schema;
export type LatestTaskStateSchema = TypeOf<typeof latestTaskStateSchema>;

export const emptyState: LatestTaskStateSchema = {
lastSuccessfulReport: null,
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { CoreSetup, Logger } from '@kbn/core/server';
import type { ConcreteTaskInstance } from '@kbn/task-manager-plugin/server';
import type { CloudSetup } from '@kbn/cloud-plugin/server';
import { throwUnrecoverableError } from '@kbn/task-manager-plugin/server';

import { usageReportingService } from '../common/services';
import type {
MeteringCallback,
Expand All @@ -19,6 +20,8 @@ import type {
} from '../types';
import type { ServerlessSecurityConfig } from '../config';

import { stateSchemaByVersion, emptyState } from './task_state';

const SCOPE = ['serverlessSecurity'];
const TIMEOUT = '1m';

Expand Down Expand Up @@ -58,6 +61,7 @@ export class SecurityUsageReportingTask {
[taskType]: {
title: taskTitle,
timeout: TIMEOUT,
stateSchemaByVersion,
createTaskRunner: ({ taskInstance }: { taskInstance: ConcreteTaskInstance }) => {
return {
run: async () => {
Expand Down Expand Up @@ -94,9 +98,7 @@ export class SecurityUsageReportingTask {
schedule: {
interval,
},
state: {
lastSuccessfulReport: null,
},
state: emptyState,
params: { version: this.version },
});
} catch (e) {
Expand Down