Skip to content

Commit

Permalink
[8.7] [Saved Objects] Logs warning when deprecated SO APIs are called (
Browse files Browse the repository at this point in the history
…#150775) (#151037)

# Backport

This will backport the following commits from `main` to `8.7`:
- [[Saved Objects] Logs warning when deprecated SO APIs are called
(#150775)](#150775)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Christiane (Tina)
Heiligers","email":"[email protected]"},"sourceCommit":{"committedDate":"2023-02-13T16:32:08Z","message":"[Saved
Objects] Logs warning when deprecated SO APIs are called
(#150775)\n\nCo-authored-by: Kibana Machine
<[email protected]>","sha":"8f4e5c53ff57e7b32d7d52d9ed9528ea216aafd1","branchLabelMapping":{"^v8.8.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Team:Core","Feature:Saved
Objects","release_note:skip","backport:prev-minor","v8.8.0"],"number":150775,"url":"https://github.com/elastic/kibana/pull/150775","mergeCommit":{"message":"[Saved
Objects] Logs warning when deprecated SO APIs are called
(#150775)\n\nCo-authored-by: Kibana Machine
<[email protected]>","sha":"8f4e5c53ff57e7b32d7d52d9ed9528ea216aafd1"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.8.0","labelRegex":"^v8.8.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/150775","number":150775,"mergeCommit":{"message":"[Saved
Objects] Logs warning when deprecated SO APIs are called
(#150775)\n\nCo-authored-by: Kibana Machine
<[email protected]>","sha":"8f4e5c53ff57e7b32d7d52d9ed9528ea216aafd1"}}]}]
BACKPORT-->

Co-authored-by: Christiane (Tina) Heiligers <[email protected]>
  • Loading branch information
kibanamachine and TinaHeiligers authored Feb 13, 2023
1 parent fc14852 commit fbf1f0e
Show file tree
Hide file tree
Showing 23 changed files with 247 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@

import { schema } from '@kbn/config-schema';
import type { InternalCoreUsageDataSetup } from '@kbn/core-usage-data-base-server-internal';
import type { Logger } from '@kbn/logging';
import type { InternalSavedObjectRouter } from '../internal_types';
import { catchAndReturnBoomErrors, throwIfAnyTypeNotVisibleByAPI } from './utils';

interface RouteDependencies {
coreUsageData: InternalCoreUsageDataSetup;
logger: Logger;
}

export const registerBulkCreateRoute = (
router: InternalSavedObjectRouter,
{ coreUsageData }: RouteDependencies
{ coreUsageData, logger }: RouteDependencies
) => {
router.post(
{
Expand Down Expand Up @@ -49,6 +51,9 @@ export const registerBulkCreateRoute = (
},
},
catchAndReturnBoomErrors(async (context, req, res) => {
logger.warn(
"The bulk create saved object API '/api/saved_objects/_bulk_create' is deprecated."
);
const { overwrite } = req.query;

const usageStatsClient = coreUsageData.getClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@

import { schema } from '@kbn/config-schema';
import type { InternalCoreUsageDataSetup } from '@kbn/core-usage-data-base-server-internal';
import type { Logger } from '@kbn/logging';
import type { InternalSavedObjectRouter } from '../internal_types';
import { catchAndReturnBoomErrors, throwIfAnyTypeNotVisibleByAPI } from './utils';

interface RouteDependencies {
coreUsageData: InternalCoreUsageDataSetup;
logger: Logger;
}

export const registerBulkDeleteRoute = (
router: InternalSavedObjectRouter,
{ coreUsageData }: RouteDependencies
{ coreUsageData, logger }: RouteDependencies
) => {
router.post(
{
Expand All @@ -35,6 +37,9 @@ export const registerBulkDeleteRoute = (
},
},
catchAndReturnBoomErrors(async (context, req, res) => {
logger.warn(
"The bulk update saved object API '/api/saved_objects/_bulk_update' is deprecated."
);
const { force } = req.query;
const usageStatsClient = coreUsageData.getClient();
usageStatsClient.incrementSavedObjectsBulkDelete({ request: req }).catch(() => {});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@

import { schema } from '@kbn/config-schema';
import type { InternalCoreUsageDataSetup } from '@kbn/core-usage-data-base-server-internal';
import type { Logger } from '@kbn/logging';
import type { InternalSavedObjectRouter } from '../internal_types';
import { catchAndReturnBoomErrors, throwIfAnyTypeNotVisibleByAPI } from './utils';

interface RouteDependencies {
coreUsageData: InternalCoreUsageDataSetup;
logger: Logger;
}

export const registerBulkGetRoute = (
router: InternalSavedObjectRouter,
{ coreUsageData }: RouteDependencies
{ coreUsageData, logger }: RouteDependencies
) => {
router.post(
{
Expand All @@ -34,6 +36,7 @@ export const registerBulkGetRoute = (
},
},
catchAndReturnBoomErrors(async (context, req, res) => {
logger.warn("The bulk get saved object API '/api/saved_objects/_bulk_get' is deprecated.");
const usageStatsClient = coreUsageData.getClient();
usageStatsClient.incrementSavedObjectsBulkGet({ request: req }).catch(() => {});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@

import { schema } from '@kbn/config-schema';
import type { InternalCoreUsageDataSetup } from '@kbn/core-usage-data-base-server-internal';
import type { Logger } from '@kbn/logging';
import type { InternalSavedObjectRouter } from '../internal_types';
import { catchAndReturnBoomErrors, throwIfAnyTypeNotVisibleByAPI } from './utils';

interface RouteDependencies {
coreUsageData: InternalCoreUsageDataSetup;
logger: Logger;
}

export const registerBulkResolveRoute = (
router: InternalSavedObjectRouter,
{ coreUsageData }: RouteDependencies
{ coreUsageData, logger }: RouteDependencies
) => {
router.post(
{
Expand All @@ -32,6 +34,9 @@ export const registerBulkResolveRoute = (
},
},
catchAndReturnBoomErrors(async (context, req, res) => {
logger.warn(
"The bulk resolve saved object API '/api/saved_objects/_bulk_resolve' is deprecated."
);
const usageStatsClient = coreUsageData.getClient();
usageStatsClient.incrementSavedObjectsBulkResolve({ request: req }).catch(() => {});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@

import { schema } from '@kbn/config-schema';
import type { InternalCoreUsageDataSetup } from '@kbn/core-usage-data-base-server-internal';
import type { Logger } from '@kbn/logging';
import type { InternalSavedObjectRouter } from '../internal_types';
import { catchAndReturnBoomErrors, throwIfAnyTypeNotVisibleByAPI } from './utils';

interface RouteDependencies {
coreUsageData: InternalCoreUsageDataSetup;
logger: Logger;
}

export const registerBulkUpdateRoute = (
router: InternalSavedObjectRouter,
{ coreUsageData }: RouteDependencies
{ coreUsageData, logger }: RouteDependencies
) => {
router.put(
{
Expand All @@ -44,6 +46,9 @@ export const registerBulkUpdateRoute = (
},
},
catchAndReturnBoomErrors(async (context, req, res) => {
logger.warn(
"The bulk update saved object API '/api/saved_objects/_bulk_update' is deprecated."
);
const usageStatsClient = coreUsageData.getClient();
usageStatsClient.incrementSavedObjectsBulkUpdate({ request: req }).catch(() => {});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@

import { schema } from '@kbn/config-schema';
import type { InternalCoreUsageDataSetup } from '@kbn/core-usage-data-base-server-internal';
import type { Logger } from '@kbn/logging';
import type { InternalSavedObjectRouter } from '../internal_types';
import { catchAndReturnBoomErrors, throwIfTypeNotVisibleByAPI } from './utils';

interface RouteDependencies {
coreUsageData: InternalCoreUsageDataSetup;
logger: Logger;
}

export const registerCreateRoute = (
router: InternalSavedObjectRouter,
{ coreUsageData }: RouteDependencies
{ coreUsageData, logger }: RouteDependencies
) => {
router.post(
{
Expand Down Expand Up @@ -48,6 +50,7 @@ export const registerCreateRoute = (
},
},
catchAndReturnBoomErrors(async (context, req, res) => {
logger.warn("The create saved object API '/api/saved_objects/{type}/{id}' is deprecated.");
const { type, id } = req.params;
const { overwrite } = req.query;
const { attributes, migrationVersion, coreMigrationVersion, references, initialNamespaces } =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@

import { schema } from '@kbn/config-schema';
import type { InternalCoreUsageDataSetup } from '@kbn/core-usage-data-base-server-internal';
import type { Logger } from '@kbn/logging';
import type { InternalSavedObjectRouter } from '../internal_types';
import { catchAndReturnBoomErrors, throwIfTypeNotVisibleByAPI } from './utils';

interface RouteDependencies {
coreUsageData: InternalCoreUsageDataSetup;
logger: Logger;
}

export const registerDeleteRoute = (
router: InternalSavedObjectRouter,
{ coreUsageData }: RouteDependencies
{ coreUsageData, logger }: RouteDependencies
) => {
router.delete(
{
Expand All @@ -33,6 +35,7 @@ export const registerDeleteRoute = (
},
},
catchAndReturnBoomErrors(async (context, req, res) => {
logger.warn("The delete saved object API '/api/saved_objects/{type}/{id}' is deprecated.");
const { type, id } = req.params;
const { force } = req.query;
const { getClient, typeRegistry } = (await context.core).savedObjects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@

import { schema } from '@kbn/config-schema';
import type { InternalCoreUsageDataSetup } from '@kbn/core-usage-data-base-server-internal';
import type { Logger } from '@kbn/logging';
import type { InternalSavedObjectRouter } from '../internal_types';
import { catchAndReturnBoomErrors, throwOnHttpHiddenTypes } from './utils';

interface RouteDependencies {
coreUsageData: InternalCoreUsageDataSetup;
logger: Logger;
}

export const registerFindRoute = (
router: InternalSavedObjectRouter,
{ coreUsageData }: RouteDependencies
{ coreUsageData, logger }: RouteDependencies
) => {
const referenceSchema = schema.object({
type: schema.string(),
Expand Down Expand Up @@ -59,6 +61,7 @@ export const registerFindRoute = (
},
},
catchAndReturnBoomErrors(async (context, req, res) => {
logger.warn("The find saved object API '/api/saved_objects/_find' is deprecated.");
const query = req.query;

const namespaces =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@

import { schema } from '@kbn/config-schema';
import type { InternalCoreUsageDataSetup } from '@kbn/core-usage-data-base-server-internal';
import type { Logger } from '@kbn/logging';
import type { InternalSavedObjectRouter } from '../internal_types';
import { catchAndReturnBoomErrors, throwIfTypeNotVisibleByAPI } from './utils';

interface RouteDependencies {
coreUsageData: InternalCoreUsageDataSetup;
logger: Logger;
}

export const registerGetRoute = (
router: InternalSavedObjectRouter,
{ coreUsageData }: RouteDependencies
{ coreUsageData, logger }: RouteDependencies
) => {
router.get(
{
Expand All @@ -30,6 +32,7 @@ export const registerGetRoute = (
},
},
catchAndReturnBoomErrors(async (context, req, res) => {
logger.warn("The get saved object API '/api/saved_objects/{type}/{id}' is deprecated.");
const { type, id } = req.params;

const usageStatsClient = coreUsageData.getClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ export function registerRoutes({
const router =
http.createRouter<InternalSavedObjectsRequestHandlerContext>('/api/saved_objects/');

registerGetRoute(router, { coreUsageData });
registerResolveRoute(router, { coreUsageData });
registerCreateRoute(router, { coreUsageData });
registerDeleteRoute(router, { coreUsageData });
registerFindRoute(router, { coreUsageData });
registerUpdateRoute(router, { coreUsageData });
registerBulkGetRoute(router, { coreUsageData });
registerBulkCreateRoute(router, { coreUsageData });
registerBulkResolveRoute(router, { coreUsageData });
registerBulkUpdateRoute(router, { coreUsageData });
registerBulkDeleteRoute(router, { coreUsageData });
registerGetRoute(router, { coreUsageData, logger });
registerResolveRoute(router, { coreUsageData, logger });
registerCreateRoute(router, { coreUsageData, logger });
registerDeleteRoute(router, { coreUsageData, logger });
registerFindRoute(router, { coreUsageData, logger });
registerUpdateRoute(router, { coreUsageData, logger });
registerBulkGetRoute(router, { coreUsageData, logger });
registerBulkCreateRoute(router, { coreUsageData, logger });
registerBulkResolveRoute(router, { coreUsageData, logger });
registerBulkUpdateRoute(router, { coreUsageData, logger });
registerBulkDeleteRoute(router, { coreUsageData, logger });
registerExportRoute(router, { config, coreUsageData });
registerImportRoute(router, { config, coreUsageData });
registerResolveImportErrorsRoute(router, { config, coreUsageData });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@

import { schema } from '@kbn/config-schema';
import type { InternalCoreUsageDataSetup } from '@kbn/core-usage-data-base-server-internal';
import type { Logger } from '@kbn/logging';
import type { InternalSavedObjectRouter } from '../internal_types';
import { throwIfTypeNotVisibleByAPI } from './utils';

interface RouteDependencies {
coreUsageData: InternalCoreUsageDataSetup;
logger: Logger;
}

export const registerResolveRoute = (
router: InternalSavedObjectRouter,
{ coreUsageData }: RouteDependencies
{ coreUsageData, logger }: RouteDependencies
) => {
router.get(
{
Expand All @@ -30,6 +32,9 @@ export const registerResolveRoute = (
},
},
router.handleLegacyErrors(async (context, req, res) => {
logger.warn(
"The resolve saved object API '/api/saved_objects/resolve/{type}/{id}' is deprecated."
);
const { type, id } = req.params;
const { savedObjects } = await context.core;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@

import { schema } from '@kbn/config-schema';
import type { SavedObjectsUpdateOptions } from '@kbn/core-saved-objects-api-server';
import type { Logger } from '@kbn/logging';
import type { InternalCoreUsageDataSetup } from '@kbn/core-usage-data-base-server-internal';
import type { InternalSavedObjectRouter } from '../internal_types';
import { catchAndReturnBoomErrors, throwIfTypeNotVisibleByAPI } from './utils';

interface RouteDependencies {
coreUsageData: InternalCoreUsageDataSetup;
logger: Logger;
}

export const registerUpdateRoute = (
router: InternalSavedObjectRouter,
{ coreUsageData }: RouteDependencies
{ coreUsageData, logger }: RouteDependencies
) => {
router.put(
{
Expand All @@ -45,6 +47,7 @@ export const registerUpdateRoute = (
},
},
catchAndReturnBoomErrors(async (context, req, res) => {
logger.warn("The update saved object API '/api/saved_objects/{type}/{id}' is deprecated.");
const { type, id } = req.params;
const { attributes, version, references, upsert } = req.body;
const options: SavedObjectsUpdateOptions = { version, references, upsert };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
type InternalSavedObjectsRequestHandlerContext,
} from '@kbn/core-saved-objects-server-internal';
import { createHiddenTypeVariants, setupServer } from '@kbn/core-test-helpers-test-utils';
import { loggerMock } from '@kbn/logging-mocks';

type SetupServerReturn = Awaited<ReturnType<typeof setupServer>>;

Expand All @@ -33,6 +34,7 @@ describe('POST /api/saved_objects/_bulk_create', () => {
let handlerContext: SetupServerReturn['handlerContext'];
let savedObjectsClient: ReturnType<typeof savedObjectsClientMock.create>;
let coreUsageStatsClient: jest.Mocked<ICoreUsageStatsClient>;
let loggerWarnSpy: jest.SpyInstance;

beforeEach(async () => {
({ server, httpSetup, handlerContext } = await setupServer());
Expand All @@ -50,7 +52,9 @@ describe('POST /api/saved_objects/_bulk_create', () => {
coreUsageStatsClient = coreUsageStatsClientMock.create();
coreUsageStatsClient.incrementSavedObjectsBulkCreate.mockRejectedValue(new Error('Oh no!')); // intentionally throw this error, which is swallowed, so we can assert that the operation does not fail
const coreUsageData = coreUsageDataServiceMock.createSetupContract(coreUsageStatsClient);
registerBulkCreateRoute(router, { coreUsageData });
const logger = loggerMock.create();
loggerWarnSpy = jest.spyOn(logger, 'warn').mockImplementation();
registerBulkCreateRoute(router, { coreUsageData, logger });

await server.start();
});
Expand Down Expand Up @@ -161,4 +165,21 @@ describe('POST /api/saved_objects/_bulk_create', () => {
'Unsupported saved object type(s): hidden-from-http: Bad Request'
);
});

it('logs a warning message when called', async () => {
await supertest(httpSetup.server.listener)
.post('/api/saved_objects/_bulk_create')
.send([
{
id: 'abc1234',
type: 'index-pattern',
attributes: {
title: 'foo',
},
references: [],
},
])
.expect(200);
expect(loggerWarnSpy).toHaveBeenCalledTimes(1);
});
});
Loading

0 comments on commit fbf1f0e

Please sign in to comment.