Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] Explicit typings for request handler context #88718

Merged
merged 50 commits into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
bb2cd94
move context to server part. couple with RequestHandlerContext
mshustov Jan 19, 2021
58387da
adopt core code
mshustov Jan 19, 2021
3e90e00
adopt bfetch code
mshustov Jan 19, 2021
4554253
adopt data code
mshustov Jan 19, 2021
e9eaf0a
adopt search examples
mshustov Jan 19, 2021
3348748
adopt vis_type_timelion
mshustov Jan 19, 2021
ccfcdca
adopt vis_type_timeseries
mshustov Jan 19, 2021
c800642
adopt plugin functional tests
mshustov Jan 19, 2021
33c45b2
adopt actions
mshustov Jan 19, 2021
258e3c1
adopt alerting plugin
mshustov Jan 19, 2021
b8016bc
adopt APM plugin
mshustov Jan 19, 2021
b3b0f1b
adopt beats_management
mshustov Jan 19, 2021
0f6f622
adopt case plugin
mshustov Jan 19, 2021
dfc44f6
adopt cross_cluster_replication
mshustov Jan 19, 2021
bedd6d0
adopt data_enhanced
mshustov Jan 19, 2021
274620d
adopt event_log
mshustov Jan 19, 2021
3cec9b2
adopt global_search
mshustov Jan 19, 2021
f635ed7
adopt index_management
mshustov Jan 19, 2021
adab1bc
adopt infra
mshustov Jan 19, 2021
96b4960
adopt licensing
mshustov Jan 19, 2021
036f3dd
adopt lists
mshustov Jan 19, 2021
5aa208e
adopt logstash
mshustov Jan 19, 2021
929efc2
adopt reporting
mshustov Jan 19, 2021
02172f1
adopt observability
mshustov Jan 19, 2021
f148f70
adopt monitoring
mshustov Jan 19, 2021
44aa3ec
adopt rollup
mshustov Jan 19, 2021
9a0bb86
adopt so tagging
mshustov Jan 19, 2021
75027b6
adopt security
mshustov Jan 19, 2021
0ea1243
adopt security_solutions
mshustov Jan 19, 2021
42d36f8
adopt watcher
mshustov Jan 19, 2021
d2f764e
adopt uptime
mshustov Jan 19, 2021
8a9770b
adopt spaces
mshustov Jan 19, 2021
eb8201b
adopt snapshot_restore
mshustov Jan 19, 2021
03b64f8
adopt features changes
mshustov Jan 19, 2021
e8990d5
Merge branch 'master' into explicit-request-hanlder-context
mshustov Jan 19, 2021
5faa2ef
mute error when null used to extend context
mshustov Jan 19, 2021
3e50c43
update docs
mshustov Jan 19, 2021
bb08a9f
small cleanup
mshustov Jan 19, 2021
298aec1
add type safety for return type
mshustov Jan 20, 2021
4633b39
refactor registerRouteHandlerContext type
mshustov Jan 20, 2021
6bfe83e
update docs
mshustov Jan 20, 2021
68aeb37
Merge branch 'master' into explicit-request-hanlder-context
mshustov Jan 20, 2021
84163ae
update license header
mshustov Jan 20, 2021
3639947
update docs
mshustov Jan 20, 2021
73cfc08
fix type error. fetch body does not accept array of strings
mshustov Jan 20, 2021
30c9c49
fix telemetry test
mshustov Jan 20, 2021
6334f55
remove unnecessary ts-ignore
mshustov Jan 20, 2021
f389948
Merge branch 'master' into explicit-request-hanlder-context
mshustov Jan 21, 2021
f2750b8
address comments
mshustov Jan 21, 2021
3185d54
update docs
mshustov Jan 21, 2021
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
Prev Previous commit
Next Next commit
adopt security_solutions
  • Loading branch information
mshustov committed Jan 19, 2021
commit 0ea12433f57fc9050d1938a6fe1010a343ee51e9
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
getPackagePolicyCreateCallback,
getPackagePolicyUpdateCallback,
} from './ingest_integration';
import { KibanaRequest, RequestHandlerContext } from 'kibana/server';
import { KibanaRequest } from 'kibana/server';
import { createMockConfig, requestContextMock } from '../lib/detection_engine/routes/__mocks__';
import { EndpointAppContextServiceStartContract } from './endpoint_app_context_services';
import { createMockEndpointAppContextServiceStartContract } from './mocks';
Expand All @@ -25,11 +25,12 @@ import { Subject } from 'rxjs';
import { ILicense } from '../../../licensing/common/types';
import { EndpointDocGenerator } from '../../common/endpoint/generate_data';
import { ProtectionModes } from '../../common/endpoint/types';
import type { SecuritySolutionRequestHandlerContext } from '../types';

describe('ingest_integration tests ', () => {
let endpointAppContextMock: EndpointAppContextServiceStartContract;
let req: KibanaRequest;
let ctx: RequestHandlerContext;
let ctx: SecuritySolutionRequestHandlerContext;
const maxTimelineImportExportSize = createMockConfig().maxTimelineImportExportSize;
let licenseEmitter: Subject<ILicense>;
let licenseService: LicenseService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,15 @@ export const getPackagePolicyCreateCallback = (

// prep for detection rules creation
const appClient = appClientFactory.create(request);
// This callback is called by fleet plugin.
// It doesn't have access to SecuritySolutionRequestHandlerContext in runtime.
// Muting the error to have green CI.
// @ts-expect-error
const frameworkRequest = await buildFrameworkRequest(context, securitySetup, request);

// Create detection index & rules (if necessary). move past any failure, this is just a convenience
try {
// @ts-expect-error
await createDetectionIndex(context, appClient);
} catch (err) {
if (err.statusCode !== 409) {
Expand All @@ -115,6 +120,7 @@ export const getPackagePolicyCreateCallback = (
// this checks to make sure index exists first, safe to try in case of failure above
// may be able to recover from minor errors
await createPrepackagedRules(
// @ts-expect-error
context,
appClient,
alerts.getAlertsClientWithRequest(request),
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/security_solution/server/endpoint/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { EndpointAppContext } from './types';
import { MetadataRequestContext } from './routes/metadata/handlers';
// import { licenseMock } from '../../../licensing/common/licensing.mock';
import { LicenseService } from '../../common/license/license';
import { SecuritySolutionRequestHandlerContext } from '../types';

/**
* Creates a mocked EndpointAppContext.
Expand Down Expand Up @@ -116,7 +117,7 @@ export const createMockMetadataRequestContext = (): jest.Mocked<MetadataRequestC
return {
endpointAppContextService: createMockEndpointAppContextService(),
logger: loggingSystemMock.create().get('mock_endpoint_app_context'),
requestHandlerContext: xpackMocks.createRequestHandlerContext(),
requestHandlerContext: (xpackMocks.createRequestHandlerContext() as unknown) as jest.Mocked<SecuritySolutionRequestHandlerContext>,
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
*/
import { deflateSync, inflateSync } from 'zlib';
import LRU from 'lru-cache';
import {
import type {
ILegacyClusterClient,
IRouter,
SavedObjectsClientContract,
ILegacyScopedClusterClient,
RouteConfig,
RequestHandler,
KibanaResponseFactory,
RequestHandlerContext,
SavedObject,
} from 'kibana/server';
import {
Expand All @@ -29,6 +28,7 @@ import { EndpointAppContextService } from '../../endpoint_app_context_services';
import { createMockEndpointAppContextServiceStartContract } from '../../mocks';
import { createMockConfig } from '../../../lib/detection_engine/routes/__mocks__';
import { WrappedTranslatedExceptionList } from '../../schemas/artifacts/lists';
import type { SecuritySolutionRequestHandlerContext } from '../../../types';

const mockArtifactName = `${ArtifactConstants.GLOBAL_ALLOWLIST_NAME}-windows-v1`;
const expectedEndpointExceptions: WrappedTranslatedExceptionList = {
Expand Down Expand Up @@ -173,7 +173,7 @@ describe('test alerts route', () => {
client: mockSavedObjectClient,
},
},
} as unknown) as RequestHandlerContext,
} as unknown) as SecuritySolutionRequestHandlerContext,
mockRequest,
mockResponse
);
Expand Down Expand Up @@ -217,7 +217,7 @@ describe('test alerts route', () => {
client: mockSavedObjectClient,
},
},
} as unknown) as RequestHandlerContext,
} as unknown) as SecuritySolutionRequestHandlerContext,
mockRequest,
mockResponse
);
Expand Down Expand Up @@ -251,7 +251,7 @@ describe('test alerts route', () => {
client: mockSavedObjectClient,
},
},
} as unknown) as RequestHandlerContext,
} as unknown) as SecuritySolutionRequestHandlerContext,
mockRequest,
mockResponse
);
Expand Down Expand Up @@ -279,7 +279,7 @@ describe('test alerts route', () => {
client: mockSavedObjectClient,
},
},
} as unknown) as RequestHandlerContext,
} as unknown) as SecuritySolutionRequestHandlerContext,
mockRequest,
mockResponse
);
Expand Down Expand Up @@ -313,7 +313,7 @@ describe('test alerts route', () => {
client: mockSavedObjectClient,
},
},
} as unknown) as RequestHandlerContext,
} as unknown) as SecuritySolutionRequestHandlerContext,
mockRequest,
mockResponse
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import Boom from '@hapi/boom';
import { RequestHandlerContext, Logger, RequestHandler } from 'kibana/server';
import type { Logger, RequestHandler } from 'kibana/server';
import { TypeOf } from '@kbn/config-schema';
import {
HostInfo,
Expand All @@ -14,6 +14,8 @@ import {
HostStatus,
MetadataQueryStrategyVersions,
} from '../../../../common/endpoint/types';
import type { SecuritySolutionRequestHandlerContext } from '../../../types';

import { getESQueryHostMetadataByID, kibanaRequestToMetadataListESQuery } from './query_builders';
import { Agent, AgentStatus, PackagePolicy } from '../../../../../fleet/common/types/models';
import { EndpointAppContext, HostListQueryResult } from '../../types';
Expand All @@ -25,7 +27,7 @@ import { EndpointAppContextService } from '../../endpoint_app_context_services';
export interface MetadataRequestContext {
endpointAppContextService: EndpointAppContextService;
logger: Logger;
requestHandlerContext: RequestHandlerContext;
requestHandlerContext: SecuritySolutionRequestHandlerContext;
}

const HOST_STATUS_MAPPING = new Map<AgentStatus, HostStatus>([
Expand All @@ -52,7 +54,12 @@ export const getMetadataListRequestHandler = function (
endpointAppContext: EndpointAppContext,
logger: Logger,
queryStrategyVersion?: MetadataQueryStrategyVersions
): RequestHandler<undefined, undefined, TypeOf<typeof GetMetadataListRequestSchema.body>> {
): RequestHandler<
unknown,
unknown,
TypeOf<typeof GetMetadataListRequestSchema.body>,
SecuritySolutionRequestHandlerContext
> {
return async (context, request, response) => {
try {
const agentService = endpointAppContext.service.getAgentService();
Expand Down Expand Up @@ -110,7 +117,12 @@ export const getMetadataRequestHandler = function (
endpointAppContext: EndpointAppContext,
logger: Logger,
queryStrategyVersion?: MetadataQueryStrategyVersions
): RequestHandler<TypeOf<typeof GetMetadataRequestSchema.params>, undefined, undefined> {
): RequestHandler<
TypeOf<typeof GetMetadataRequestSchema.params>,
unknown,
unknown,
SecuritySolutionRequestHandlerContext
> {
return async (context, request, response) => {
const agentService = endpointAppContext.service.getAgentService();
if (agentService === undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { IRouter } from 'kibana/server';
import { schema } from '@kbn/config-schema';

import { HostStatus, MetadataQueryStrategyVersions } from '../../../../common/endpoint/types';
import { EndpointAppContext } from '../../types';
import { getLogger, getMetadataListRequestHandler, getMetadataRequestHandler } from './handlers';
import type { SecuritySolutionPluginRouter } from '../../../types';

export const BASE_ENDPOINT_ROUTE = '/api/endpoint';
export const METADATA_REQUEST_V1_ROUTE = `${BASE_ENDPOINT_ROUTE}/v1/metadata`;
Expand Down Expand Up @@ -60,7 +60,10 @@ export const GetMetadataListRequestSchema = {
),
};

export function registerEndpointRoutes(router: IRouter, endpointAppContext: EndpointAppContext) {
export function registerEndpointRoutes(
router: SecuritySolutionPluginRouter,
endpointAppContext: EndpointAppContext
) {
const logger = getLogger(endpointAppContext);
router.post(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
import {
ILegacyClusterClient,
IRouter,
ILegacyScopedClusterClient,
KibanaResponseFactory,
RequestHandler,
Expand Down Expand Up @@ -43,16 +42,17 @@ import {
import { createV1SearchResponse, createV2SearchResponse } from './support/test_support';
import { PackageService } from '../../../../../fleet/server/services';
import { metadataTransformPrefix } from '../../../../common/endpoint/constants';
import type { SecuritySolutionPluginRouter } from '../../../types';

describe('test endpoint route', () => {
let routerMock: jest.Mocked<IRouter>;
let routerMock: jest.Mocked<SecuritySolutionPluginRouter>;
let mockResponse: jest.Mocked<KibanaResponseFactory>;
let mockClusterClient: jest.Mocked<ILegacyClusterClient>;
let mockScopedClient: jest.Mocked<ILegacyScopedClusterClient>;
let mockSavedObjectClient: jest.Mocked<SavedObjectsClientContract>;
let mockPackageService: jest.Mocked<PackageService>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let routeHandler: RequestHandler<any, any, any>;
let routeHandler: RequestHandler<any, any, any, any>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let routeConfig: RouteConfig<any, any, any, any>;
// tests assume that fleet is enabled, and thus agentService is available
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
import {
ILegacyClusterClient,
IRouter,
ILegacyScopedClusterClient,
KibanaResponseFactory,
RequestHandler,
Expand Down Expand Up @@ -38,16 +37,17 @@ import { EndpointDocGenerator } from '../../../../common/endpoint/generate_data'
import { Agent, EsAssetReference } from '../../../../../fleet/common/types/models';
import { createV1SearchResponse } from './support/test_support';
import { PackageService } from '../../../../../fleet/server/services';
import type { SecuritySolutionPluginRouter } from '../../../types';

describe('test endpoint route v1', () => {
let routerMock: jest.Mocked<IRouter>;
let routerMock: jest.Mocked<SecuritySolutionPluginRouter>;
let mockResponse: jest.Mocked<KibanaResponseFactory>;
let mockClusterClient: jest.Mocked<ILegacyClusterClient>;
let mockScopedClient: jest.Mocked<ILegacyScopedClusterClient>;
let mockSavedObjectClient: jest.Mocked<SavedObjectsClientContract>;
let mockPackageService: jest.Mocked<PackageService>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let routeHandler: RequestHandler<any, any, any>;
let routeHandler: RequestHandler<any, any, any, any>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let routeConfig: RouteConfig<any, any, any, any>;
// tests assume that fleet is enabled, and thus agentService is available
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
getTrustedAppsListRouteHandler,
getTrustedAppsSummaryRouteHandler,
} from './handlers';
import type { SecuritySolutionRequestHandlerContext } from '../../../types';

const exceptionsListClient = listMock.getExceptionListClient() as jest.Mocked<ExceptionListClient>;

Expand All @@ -31,13 +32,14 @@ const createAppContextMock = () => ({
config: () => Promise.resolve(createMockConfig()),
});

const createHandlerContextMock = () => ({
...xpackMocks.createRequestHandlerContext(),
lists: {
getListClient: jest.fn(),
getExceptionListClient: jest.fn().mockReturnValue(exceptionsListClient),
},
});
const createHandlerContextMock = () =>
(({
...xpackMocks.createRequestHandlerContext(),
lists: {
getListClient: jest.fn(),
getExceptionListClient: jest.fn().mockReturnValue(exceptionsListClient),
},
} as unknown) as jest.Mocked<SecuritySolutionRequestHandlerContext>);

const assertResponse = <T>(
response: jest.Mocked<KibanaResponseFactory>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { RequestHandler, RequestHandlerContext } from 'kibana/server';
import type { RequestHandler } from 'kibana/server';
import type { SecuritySolutionRequestHandlerContext } from '../../../types';

import { ExceptionListClient } from '../../../../../lists/server';

Expand All @@ -23,7 +24,9 @@ import {
MissingTrustedAppException,
} from './service';

const exceptionListClientFromContext = (context: RequestHandlerContext): ExceptionListClient => {
const exceptionListClientFromContext = (
context: SecuritySolutionRequestHandlerContext
): ExceptionListClient => {
const exceptionLists = context.lists?.getExceptionListClient();

if (!exceptionLists) {
Expand All @@ -35,7 +38,12 @@ const exceptionListClientFromContext = (context: RequestHandlerContext): Excepti

export const getTrustedAppsDeleteRouteHandler = (
endpointAppContext: EndpointAppContext
): RequestHandler<DeleteTrustedAppsRequestParams, undefined, undefined> => {
): RequestHandler<
DeleteTrustedAppsRequestParams,
unknown,
unknown,
SecuritySolutionRequestHandlerContext
> => {
const logger = endpointAppContext.logFactory.get('trusted_apps');

return async (context, req, res) => {
Expand All @@ -56,7 +64,12 @@ export const getTrustedAppsDeleteRouteHandler = (

export const getTrustedAppsListRouteHandler = (
endpointAppContext: EndpointAppContext
): RequestHandler<undefined, GetTrustedAppsListRequest> => {
): RequestHandler<
unknown,
GetTrustedAppsListRequest,
unknown,
SecuritySolutionRequestHandlerContext
> => {
const logger = endpointAppContext.logFactory.get('trusted_apps');

return async (context, req, res) => {
Expand All @@ -73,7 +86,12 @@ export const getTrustedAppsListRouteHandler = (

export const getTrustedAppsCreateRouteHandler = (
endpointAppContext: EndpointAppContext
): RequestHandler<undefined, undefined, PostTrustedAppCreateRequest> => {
): RequestHandler<
unknown,
unknown,
PostTrustedAppCreateRequest,
SecuritySolutionRequestHandlerContext
> => {
const logger = endpointAppContext.logFactory.get('trusted_apps');

return async (context, req, res) => {
Expand All @@ -90,7 +108,12 @@ export const getTrustedAppsCreateRouteHandler = (

export const getTrustedAppsSummaryRouteHandler = (
endpointAppContext: EndpointAppContext
): RequestHandler<undefined, undefined, PostTrustedAppCreateRequest> => {
): RequestHandler<
unknown,
unknown,
PostTrustedAppCreateRequest,
SecuritySolutionRequestHandlerContext
> => {
const logger = endpointAppContext.logFactory.get('trusted_apps');

return async (context, req, res) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { IRouter } from 'kibana/server';
import {
DeleteTrustedAppsRequestSchema,
GetTrustedAppsRequestSchema,
Expand All @@ -23,9 +22,10 @@ import {
getTrustedAppsSummaryRouteHandler,
} from './handlers';
import { EndpointAppContext } from '../../types';
import { SecuritySolutionPluginRouter } from '../../../types';

export const registerTrustedAppsRoutes = (
router: IRouter,
router: SecuritySolutionPluginRouter,
endpointAppContext: EndpointAppContext
) => {
// DELETE one
Expand Down
Loading