Skip to content

Commit

Permalink
[Security Solution][Endpoint] Move Response Action's file download ap…
Browse files Browse the repository at this point in the history
…i to versioned router (#161272)

## Summary

- Moves the file download API of response actions to versioned router
(after getting support for this type of API via #160399 )
  • Loading branch information
paul-tavares committed Jul 6, 2023
1 parent d118fb4 commit 8a17024
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ describe('When using the `ResponseActionFileDownloadLink` component', () => {
expect(apiMocks.responseProvider.fileInfo).toHaveBeenCalled();
});

expect(renderResult.getByTestId('test-downloadButton')).not.toBeNull();
const downlaodButton = renderResult.getByTestId('test-downloadButton');

expect(downlaodButton.getAttribute('href')).toEqual(
'/api/endpoint/action/123/file/123.agent-a/download?apiVersion=2023-10-31'
);
expect(renderResult.getByTestId('test-passcodeMessage')).toHaveTextContent(
FILE_PASSCODE_INFO_MESSAGE
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ export const ResponseActionFileDownloadLink = memo<ResponseActionFileDownloadLin
}, [action.isCompleted, action.wasSuccessful]);

const downloadUrl: string = useMemo(() => {
return resolvePathVariables(ACTION_AGENT_FILE_DOWNLOAD_ROUTE, {
return `${resolvePathVariables(ACTION_AGENT_FILE_DOWNLOAD_ROUTE, {
action_id: action.id,
file_id: getFileDownloadId(action, agentId),
});
})}?apiVersion=2023-10-31`;
}, [action, agentId]);

const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ describe('Response Actions file download API', () => {

it('should register the route', () => {
expect(
apiTestSetup.getRegisteredRouteHandler('get', ACTION_AGENT_FILE_DOWNLOAD_ROUTE)
apiTestSetup.getRegisteredVersionedRoute(
'get',
ACTION_AGENT_FILE_DOWNLOAD_ROUTE,
'2023-10-31'
)
).toBeDefined();
});

Expand All @@ -59,11 +63,9 @@ describe('Response Actions file download API', () => {
(await httpHandlerContextMock.securitySolution).getEndpointAuthz as jest.Mock
).mockResolvedValue(getEndpointAuthzInitialStateMock({ canWriteFileOperations: false }));

await apiTestSetup.getRegisteredRouteHandler('get', ACTION_AGENT_FILE_DOWNLOAD_ROUTE)(
httpHandlerContextMock,
httpRequestMock,
httpResponseMock
);
await apiTestSetup
.getRegisteredVersionedRoute('get', ACTION_AGENT_FILE_DOWNLOAD_ROUTE, '2023-10-31')
.routeHandler(httpHandlerContextMock, httpRequestMock, httpResponseMock);

expect(httpResponseMock.forbidden).toHaveBeenCalledWith({
body: expect.any(EndpointAuthorizationError),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,29 @@ export const registerActionFileDownloadRoutes = (
) => {
const logger = endpointContext.logFactory.get('actionFileDownload');

// NOTE:
// This API (as of today - 2023-06-21) can not be versioned because it is used
// to download files from the UI, where its used as part of a `<a>` anchor, which
// has no way to define the version header.
router.get(
{
router.versioned
.get({
access: 'public',
// NOTE:
// Because this API is used in the browser via `href` (ex. on link to download a file),
// we need to enable setting the version number via query params
enableQueryVersion: true,
path: ACTION_AGENT_FILE_DOWNLOAD_ROUTE,
validate: EndpointActionFileDownloadSchema,
options: { authRequired: true, tags: ['access:securitySolution'] },
},
withEndpointAuthz(
{ all: ['canWriteFileOperations'] },
logger,
getActionFileDownloadRouteHandler(endpointContext)
)
);
})
.addVersion(
{
version: '2023-10-31',
validate: {
request: EndpointActionFileDownloadSchema,
},
},
withEndpointAuthz(
{ all: ['canWriteFileOperations'] },
logger,
getActionFileDownloadRouteHandler(endpointContext)
)
);
};

export const getActionFileDownloadRouteHandler = (
Expand Down

0 comments on commit 8a17024

Please sign in to comment.