Skip to content

Commit

Permalink
[Fleet] use defaultFleetErrorHandler in all fleet routes
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet committed Nov 19, 2024
1 parent 0c66148 commit 96e0366
Show file tree
Hide file tree
Showing 24 changed files with 1,226 additions and 1,449 deletions.
299 changes: 122 additions & 177 deletions x-pack/plugins/fleet/server/routes/agent/handlers.ts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import type {
ElasticsearchClient,
KibanaResponseFactory,
RequestHandlerContext,
SavedObjectsClientContract,
KibanaRequest,
} from '@kbn/core/server';
Expand All @@ -18,12 +17,14 @@ import {
} from '@kbn/core/server/mocks';

import type { RequestDiagnosticsAdditionalMetrics } from '../../../common/types';

import { withDefaultErrorHandler } from '../../services/security/fleet_router';
import { getAgentById } from '../../services/agents';
import * as AgentService from '../../services/agents';
import { type FleetRequestHandlerContext } from '../..';

import { requestDiagnosticsHandler } from './request_diagnostics_handler';

const requestDiagnosticsWithErrorHandler = withDefaultErrorHandler(requestDiagnosticsHandler);
jest.mock('../../services/agents');

const mockGetAgentById = getAgentById as jest.Mock;
Expand All @@ -32,7 +33,7 @@ describe('request diagnostics handler', () => {
let mockResponse: jest.Mocked<KibanaResponseFactory>;
let mockSavedObjectsClient: jest.Mocked<SavedObjectsClientContract>;
let mockElasticsearchClient: jest.Mocked<ElasticsearchClient>;
let mockContext: RequestHandlerContext;
let mockContext: FleetRequestHandlerContext;
let mockRequest: KibanaRequest<
{ agentId: string },
undefined,
Expand All @@ -56,7 +57,7 @@ describe('request diagnostics handler', () => {
},
},
},
} as unknown as RequestHandlerContext;
} as unknown as FleetRequestHandlerContext;
mockRequest = httpServerMock.createKibanaRequest({
params: { agentId: 'agent1' },
body: { additional_metrics: ['CPU'] },
Expand All @@ -69,7 +70,7 @@ describe('request diagnostics handler', () => {
local_metadata: { elastic: { agent: { version: '8.7.0' } } },
});

await requestDiagnosticsHandler(mockContext, mockRequest, mockResponse);
await requestDiagnosticsWithErrorHandler(mockContext, mockRequest, mockResponse);

expect(mockResponse.ok).toHaveBeenCalledWith({ body: { actionId: '1' } });
});
Expand All @@ -80,7 +81,7 @@ describe('request diagnostics handler', () => {
local_metadata: { elastic: { agent: { version: '8.6.0' } } },
});

await requestDiagnosticsHandler(mockContext, mockRequest, mockResponse);
await requestDiagnosticsWithErrorHandler(mockContext, mockRequest, mockResponse);

expect(mockResponse.customError).toHaveBeenCalledWith({
body: { message: 'Agent agent1 does not support request diagnostics action.' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,48 @@
* 2.0.
*/

import type { RequestHandler } from '@kbn/core/server';
import type { TypeOf } from '@kbn/config-schema';

import { isAgentRequestDiagnosticsSupported } from '../../../common/services';

import * as AgentService from '../../services/agents';
import type {
FleetRequestHandler,
PostBulkRequestDiagnosticsActionRequestSchema,
PostRequestDiagnosticsActionRequestSchema,
} from '../../types';
import { defaultFleetErrorHandler } from '../../errors';
import { getAgentById } from '../../services/agents';

export const requestDiagnosticsHandler: RequestHandler<
export const requestDiagnosticsHandler: FleetRequestHandler<
TypeOf<typeof PostRequestDiagnosticsActionRequestSchema.params>,
undefined,
TypeOf<typeof PostRequestDiagnosticsActionRequestSchema.body>
> = async (context, request, response) => {
const coreContext = await context.core;
const esClient = coreContext.elasticsearch.client.asInternalUser;
const soClient = coreContext.savedObjects.client;
try {
const agent = await getAgentById(esClient, soClient, request.params.agentId);
const agent = await getAgentById(esClient, soClient, request.params.agentId);

if (!isAgentRequestDiagnosticsSupported(agent)) {
return response.customError({
statusCode: 400,
body: {
message: `Agent ${request.params.agentId} does not support request diagnostics action.`,
},
});
}
if (!isAgentRequestDiagnosticsSupported(agent)) {
return response.customError({
statusCode: 400,
body: {
message: `Agent ${request.params.agentId} does not support request diagnostics action.`,
},
});
}

const result = await AgentService.requestDiagnostics(
esClient,
soClient,
request.params.agentId,
request.body?.additional_metrics
);
const result = await AgentService.requestDiagnostics(
esClient,
soClient,
request.params.agentId,
request.body?.additional_metrics
);

return response.ok({ body: { actionId: result.actionId } });
} catch (error) {
return defaultFleetErrorHandler({ error, response });
}
return response.ok({ body: { actionId: result.actionId } });
};

export const bulkRequestDiagnosticsHandler: RequestHandler<
export const bulkRequestDiagnosticsHandler: FleetRequestHandler<
undefined,
undefined,
TypeOf<typeof PostBulkRequestDiagnosticsActionRequestSchema.body>
Expand All @@ -62,15 +57,11 @@ export const bulkRequestDiagnosticsHandler: RequestHandler<
const agentOptions = Array.isArray(request.body.agents)
? { agentIds: request.body.agents }
: { kuery: request.body.agents };
try {
const result = await AgentService.bulkRequestDiagnostics(esClient, soClient, {
...agentOptions,
batchSize: request.body.batchSize,
additionalMetrics: request.body.additional_metrics,
});
const result = await AgentService.bulkRequestDiagnostics(esClient, soClient, {
...agentOptions,
batchSize: request.body.batchSize,
additionalMetrics: request.body.additional_metrics,
});

return response.ok({ body: { actionId: result.actionId } });
} catch (error) {
return defaultFleetErrorHandler({ error, response });
}
return response.ok({ body: { actionId: result.actionId } });
};
Loading

0 comments on commit 96e0366

Please sign in to comment.