Skip to content

Commit

Permalink
[Security solution] Fix non-langchain error handling (#170672)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephmilovic authored Nov 6, 2023
1 parent 6f1fbce commit e2645a7
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 8 deletions.
79 changes: 79 additions & 0 deletions x-pack/plugins/elastic_assistant/server/lib/executor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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 { executeAction } from './executor';
import { KibanaRequest } from '@kbn/core-http-server';
import { RequestBody } from './langchain/types';
import { PluginStartContract as ActionsPluginStart } from '@kbn/actions-plugin/server';
const request = {
body: {
params: {},
},
} as KibanaRequest<unknown, unknown, RequestBody>;

describe('executeAction', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('should execute an action with the provided connectorId and request body params', async () => {
const actions = {
getActionsClientWithRequest: jest.fn().mockResolvedValue({
execute: jest.fn().mockResolvedValue({
status: 'ok',
data: {
message: 'Test message',
},
}),
}),
} as unknown as ActionsPluginStart;

const connectorId = '12345';

const response = await executeAction({ actions, request, connectorId });

expect(actions.getActionsClientWithRequest).toHaveBeenCalledWith(request);
expect(actions.getActionsClientWithRequest).toHaveBeenCalledTimes(1);
expect(response.connector_id).toBe(connectorId);
expect(response.data).toBe('Test message');
expect(response.status).toBe('ok');
});

it('should throw an error if action result status is "error"', async () => {
const actions = {
getActionsClientWithRequest: jest.fn().mockResolvedValue({
execute: jest.fn().mockResolvedValue({
status: 'error',
message: 'Error message',
serviceMessage: 'Service error message',
}),
}),
} as unknown as ActionsPluginStart;
const connectorId = '12345';

await expect(executeAction({ actions, request, connectorId })).rejects.toThrowError(
'Action result status is error: Error message - Service error message'
);
});

it('should throw an error if content of response data is not a string', async () => {
const actions = {
getActionsClientWithRequest: jest.fn().mockResolvedValue({
execute: jest.fn().mockResolvedValue({
status: 'ok',
data: {
message: 12345,
},
}),
}),
} as unknown as ActionsPluginStart;
const connectorId = '12345';

await expect(executeAction({ actions, request, connectorId })).rejects.toThrowError(
'Action result status is error: content should be a string, but it had an unexpected type: number'
);
});
});
22 changes: 15 additions & 7 deletions x-pack/plugins/elastic_assistant/server/lib/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,21 @@ export const executeAction = async ({
actionId: connectorId,
params: request.body.params,
});

if (actionResult.status === 'error') {
throw new Error(
`Action result status is error: ${actionResult?.message} - ${actionResult?.serviceMessage}`
);
}
const content = get('data.message', actionResult);
if (typeof content === 'string') {
return {
connector_id: connectorId,
data: content, // the response from the actions framework
status: 'ok',
};
if (typeof content !== 'string') {
throw new Error(
`Action result status is error: content should be a string, but it had an unexpected type: ${typeof content}`
);
}
throw new Error('Unexpected action result');
return {
connector_id: connectorId,
data: content, // the response from the actions framework
status: 'ok',
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class ActionsClientLlm extends LLM {
`${LLM_TYPE}: action result status is error: ${actionResult?.message} - ${actionResult?.serviceMessage}`
);
}
// TODO: handle errors from the connector

const content = get('data.message', actionResult);

if (typeof content !== 'string') {
Expand Down

0 comments on commit e2645a7

Please sign in to comment.