Skip to content

Commit

Permalink
Only log deprecation warnings for calls to Saved Objects routes from …
Browse files Browse the repository at this point in the history
…non-kibana request (elastic#152971)

Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
TinaHeiligers and kibanamachine authored Mar 9, 2023
1 parent ee5acfd commit e37e834
Show file tree
Hide file tree
Showing 15 changed files with 200 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import { SavedObjectConfig } from '@kbn/core-saved-objects-base-server-internal'
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';
import {
catchAndReturnBoomErrors,
logWarnOnExternalRequest,
throwIfAnyTypeNotVisibleByAPI,
} from './utils';

interface RouteDependencies {
config: SavedObjectConfig;
Expand Down Expand Up @@ -54,9 +58,12 @@ export const registerBulkCreateRoute = (
},
},
catchAndReturnBoomErrors(async (context, req, res) => {
logger.warn(
"The bulk create saved object API '/api/saved_objects/_bulk_create' is deprecated."
);
logWarnOnExternalRequest({
method: 'post',
path: '/api/saved_objects/_bulk_create',
req,
logger,
});
const { overwrite } = req.query;

const usageStatsClient = coreUsageData.getClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import { SavedObjectConfig } from '@kbn/core-saved-objects-base-server-internal'
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';
import {
catchAndReturnBoomErrors,
logWarnOnExternalRequest,
throwIfAnyTypeNotVisibleByAPI,
} from './utils';

interface RouteDependencies {
config: SavedObjectConfig;
Expand Down Expand Up @@ -40,9 +44,12 @@ export const registerBulkDeleteRoute = (
},
},
catchAndReturnBoomErrors(async (context, req, res) => {
logger.warn(
"The bulk update saved object API '/api/saved_objects/_bulk_update' is deprecated."
);
logWarnOnExternalRequest({
method: 'post',
path: '/api/saved_objects/_bulk_delete',
req,
logger,
});
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 @@ -11,7 +11,11 @@ import { SavedObjectConfig } from '@kbn/core-saved-objects-base-server-internal'
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';
import {
catchAndReturnBoomErrors,
logWarnOnExternalRequest,
throwIfAnyTypeNotVisibleByAPI,
} from './utils';

interface RouteDependencies {
config: SavedObjectConfig;
Expand Down Expand Up @@ -39,7 +43,12 @@ export const registerBulkGetRoute = (
},
},
catchAndReturnBoomErrors(async (context, req, res) => {
logger.warn("The bulk get saved object API '/api/saved_objects/_bulk_get' is deprecated.");
logWarnOnExternalRequest({
method: 'post',
path: '/api/saved_objects/_bulk_get',
req,
logger,
});
const usageStatsClient = coreUsageData.getClient();
usageStatsClient.incrementSavedObjectsBulkGet({ request: req }).catch(() => {});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import { SavedObjectConfig } from '@kbn/core-saved-objects-base-server-internal'
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';
import {
catchAndReturnBoomErrors,
logWarnOnExternalRequest,
throwIfAnyTypeNotVisibleByAPI,
} from './utils';

interface RouteDependencies {
config: SavedObjectConfig;
Expand All @@ -37,9 +41,12 @@ export const registerBulkResolveRoute = (
},
},
catchAndReturnBoomErrors(async (context, req, res) => {
logger.warn(
"The bulk resolve saved object API '/api/saved_objects/_bulk_resolve' is deprecated."
);
logWarnOnExternalRequest({
method: 'post',
path: '/api/saved_objects/_bulk_resolve',
req,
logger,
});
const usageStatsClient = coreUsageData.getClient();
usageStatsClient.incrementSavedObjectsBulkResolve({ request: req }).catch(() => {});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import { SavedObjectConfig } from '@kbn/core-saved-objects-base-server-internal'
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';
import {
catchAndReturnBoomErrors,
logWarnOnExternalRequest,
throwIfAnyTypeNotVisibleByAPI,
} from './utils';

interface RouteDependencies {
config: SavedObjectConfig;
Expand Down Expand Up @@ -49,9 +53,12 @@ export const registerBulkUpdateRoute = (
},
},
catchAndReturnBoomErrors(async (context, req, res) => {
logger.warn(
"The bulk update saved object API '/api/saved_objects/_bulk_update' is deprecated."
);
logWarnOnExternalRequest({
method: 'put',
path: '/api/saved_objects/_bulk_update',
req,
logger,
});
const usageStatsClient = coreUsageData.getClient();
usageStatsClient.incrementSavedObjectsBulkUpdate({ request: req }).catch(() => {});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import { SavedObjectConfig } from '@kbn/core-saved-objects-base-server-internal'
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';
import {
catchAndReturnBoomErrors,
logWarnOnExternalRequest,
throwIfTypeNotVisibleByAPI,
} from './utils';

interface RouteDependencies {
config: SavedObjectConfig;
Expand Down Expand Up @@ -53,7 +57,12 @@ export const registerCreateRoute = (
},
},
catchAndReturnBoomErrors(async (context, req, res) => {
logger.warn("The create saved object API '/api/saved_objects/{type}/{id}' is deprecated.");
logWarnOnExternalRequest({
method: 'post',
path: '/api/saved_objects/{type}/{id?}',
req,
logger,
});
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 @@ -11,7 +11,11 @@ import { SavedObjectConfig } from '@kbn/core-saved-objects-base-server-internal'
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';
import {
catchAndReturnBoomErrors,
logWarnOnExternalRequest,
throwIfTypeNotVisibleByAPI,
} from './utils';

interface RouteDependencies {
config: SavedObjectConfig;
Expand All @@ -38,7 +42,12 @@ export const registerDeleteRoute = (
},
},
catchAndReturnBoomErrors(async (context, req, res) => {
logger.warn("The delete saved object API '/api/saved_objects/{type}/{id}' is deprecated.");
logWarnOnExternalRequest({
method: 'delete',
path: '/api/saved_objects/{type}/{id}',
req,
logger,
});
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 @@ -12,7 +12,7 @@ import type { InternalCoreUsageDataSetup } from '@kbn/core-usage-data-base-serve
import type { Logger } from '@kbn/logging';
import type { InternalSavedObjectRouter } from '../internal_types';
import { catchAndReturnBoomErrors, throwOnHttpHiddenTypes } from './utils';

import { logWarnOnExternalRequest } from './utils';
interface RouteDependencies {
config: SavedObjectConfig;
coreUsageData: InternalCoreUsageDataSetup;
Expand Down Expand Up @@ -63,7 +63,12 @@ export const registerFindRoute = (
},
},
catchAndReturnBoomErrors(async (context, req, res) => {
logger.warn("The find saved object API '/api/saved_objects/_find' is deprecated.");
logWarnOnExternalRequest({
method: 'get',
path: '/api/saved_objects/_find',
req,
logger,
});
const query = req.query;

const namespaces =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import { SavedObjectConfig } from '@kbn/core-saved-objects-base-server-internal'
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';
import {
catchAndReturnBoomErrors,
logWarnOnExternalRequest,
throwIfTypeNotVisibleByAPI,
} from './utils';

interface RouteDependencies {
config: SavedObjectConfig;
Expand All @@ -35,7 +39,12 @@ export const registerGetRoute = (
},
},
catchAndReturnBoomErrors(async (context, req, res) => {
logger.warn("The get saved object API '/api/saved_objects/{type}/{id}' is deprecated.");
logWarnOnExternalRequest({
method: 'get',
path: '/api/saved_objects/{type}/{id}',
req,
logger,
});
const { type, id } = req.params;

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

registerGetRoute(router, { config, coreUsageData, logger });
registerResolveRoute(router, { config, coreUsageData, logger });
registerCreateRoute(router, { config, coreUsageData, logger });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { SavedObjectConfig } from '@kbn/core-saved-objects-base-server-internal'
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';
import { throwIfTypeNotVisibleByAPI, logWarnOnExternalRequest } from './utils';

interface RouteDependencies {
config: SavedObjectConfig;
Expand All @@ -35,9 +35,12 @@ export const registerResolveRoute = (
},
},
router.handleLegacyErrors(async (context, req, res) => {
logger.warn(
"The resolve saved object API '/api/saved_objects/resolve/{type}/{id}' is deprecated."
);
logWarnOnExternalRequest({
method: 'get',
path: '/api/saved_objects/resolve/{type}/{id}',
req,
logger,
});
const { type, id } = req.params;
const { savedObjects } = await context.core;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import type { Logger } from '@kbn/logging';
import { SavedObjectConfig } from '@kbn/core-saved-objects-base-server-internal';
import type { InternalCoreUsageDataSetup } from '@kbn/core-usage-data-base-server-internal';
import type { InternalSavedObjectRouter } from '../internal_types';
import { catchAndReturnBoomErrors, throwIfTypeNotVisibleByAPI } from './utils';
import {
catchAndReturnBoomErrors,
logWarnOnExternalRequest,
throwIfTypeNotVisibleByAPI,
} from './utils';

interface RouteDependencies {
config: SavedObjectConfig;
Expand Down Expand Up @@ -50,7 +54,12 @@ export const registerUpdateRoute = (
},
},
catchAndReturnBoomErrors(async (context, req, res) => {
logger.warn("The update saved object API '/api/saved_objects/{type}/{id}' is deprecated.");
logWarnOnExternalRequest({
method: 'get',
path: '/api/saved_objects/{type}/{id}',
req,
logger,
});
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 @@ -14,6 +14,7 @@ import {
throwOnGloballyHiddenTypes,
throwIfTypeNotVisibleByAPI,
throwIfAnyTypeNotVisibleByAPI,
logWarnOnExternalRequest,
} from './utils';
import { Readable } from 'stream';
import { createPromiseFromStreams, createConcatStream } from '@kbn/utils';
Expand All @@ -27,6 +28,8 @@ import type {
} from '@kbn/core-http-server';
import { kibanaResponseFactory } from '@kbn/core-http-router-server-internal';
import { typeRegistryInstanceMock } from '../saved_objects_service.test.mocks';
import { httpServerMock } from '@kbn/core-http-server-mocks';
import { loggerMock, type MockedLogger } from '@kbn/logging-mocks';

async function readStreamToCompletion(stream: Readable) {
return createPromiseFromStreams([stream, createConcatStream([])]);
Expand Down Expand Up @@ -341,3 +344,60 @@ describe('throwIfAnyTypeNotVisibleByAPI', () => {
expect(() => throwIfAnyTypeNotVisibleByAPI(['config'], registry)).not.toThrowError();
});
});

describe('logWarnOnExternalRequest', () => {
let logger: MockedLogger;
const firstPartyRequestHeaders = { 'kbn-version': 'a', referer: 'b' };
const kibRequest = httpServerMock.createKibanaRequest({ headers: firstPartyRequestHeaders });
const extRequest = httpServerMock.createKibanaRequest();

beforeEach(() => {
logger = loggerMock.create();
});
afterEach(() => {
jest.clearAllMocks();
});

it('logs on external requests to non-bulk apis', () => {
logWarnOnExternalRequest({
method: 'get',
path: '/resolve/{type}/{id}',
req: extRequest,
logger,
});
expect(logger.warn).toHaveBeenCalledTimes(1);
expect(logger.warn).toHaveBeenCalledWith(
'The get saved object API /resolve/{type}/{id} is deprecated.'
);
});

it('logs on external requests to bulk apis', () => {
logWarnOnExternalRequest({
method: 'post',
path: '/_bulk_resolve',
req: extRequest,
logger,
});
expect(logger.warn).toHaveBeenCalledTimes(1);
expect(logger.warn).toHaveBeenCalledWith(
'The post saved object API /_bulk_resolve is deprecated.'
);
});

it('does not log a warning on internal requests', () => {
logWarnOnExternalRequest({
method: 'get',
path: '/resolve/{type}/{id}',
req: kibRequest,
logger,
});
expect(logger.warn).toHaveBeenCalledTimes(0);
logWarnOnExternalRequest({
method: 'post',
path: '/_bulk_resolve',
req: kibRequest,
logger,
});
expect(logger.warn).toHaveBeenCalledTimes(0);
});
});
Loading

0 comments on commit e37e834

Please sign in to comment.