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

[Security Solution] Fix redirect properly old SIEM App routes #76868

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ const ABSOLUTE_DATE = {
startTime: '2019-08-01T20:03:29.186Z',
};

// FLAKY: https://github.com/elastic/kibana/issues/75697
describe.skip('URL compatibility', () => {
describe('URL compatibility', () => {
it('Redirects to Detection alerts from old Detections URL', () => {
loginAndWaitForPage(DETECTIONS);

Expand Down
55 changes: 55 additions & 0 deletions x-pack/plugins/security_solution/public/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { parseRoute } from './helpers';

describe('public helpers parseRoute', () => {
it('should properly parse hash route', () => {
const hashSearch =
'?timerange=(global:(linkTo:!(timeline),timerange:(from:%272020-09-06T11:43:55.814Z%27,fromStr:now-24h,kind:relative,to:%272020-09-07T11:43:55.814Z%27,toStr:now)),timeline:(linkTo:!(global),timerange:(from:%272020-09-06T11:43:55.814Z%27,fromStr:now-24h,kind:relative,to:%272020-09-07T11:43:55.814Z%27,toStr:now)))';
const hashLocation = {
hash: `#/detections/rules/id/78acc090-bbaa-4a86-916b-ea44784324ae/edit${hashSearch}`,
pathname: '/app/siem',
search: '',
};

expect(parseRoute(hashLocation)).toEqual({
pageName: 'detections',
path: `/rules/id/78acc090-bbaa-4a86-916b-ea44784324ae/edit${hashSearch}`,
search: hashSearch,
});
});

it('should properly parse non-hash route', () => {
const nonHashLocation = {
hash: '',
pathname: '/app/security/detections/rules/id/78acc090-bbaa-4a86-916b-ea44784324ae/edit',
search:
'?timerange=(global:(linkTo:!(timeline),timerange:(from:%272020-09-06T11:43:55.814Z%27,fromStr:now-24h,kind:relative,to:%272020-09-07T11:43:55.814Z%27,toStr:now)),timeline:(linkTo:!(global),timerange:(from:%272020-09-06T11:43:55.814Z%27,fromStr:now-24h,kind:relative,to:%272020-09-07T11:43:55.814Z%27,toStr:now)))',
};

expect(parseRoute(nonHashLocation)).toEqual({
pageName: 'detections',
path: `/rules/id/78acc090-bbaa-4a86-916b-ea44784324ae/edit${nonHashLocation.search}`,
search: nonHashLocation.search,
});
});

it('should properly parse non-hash subplugin route', () => {
const nonHashLocation = {
hash: '',
pathname: '/app/security/detections',
search:
'?timerange=(global:(linkTo:!(timeline),timerange:(from:%272020-09-06T11:43:55.814Z%27,fromStr:now-24h,kind:relative,to:%272020-09-07T11:43:55.814Z%27,toStr:now)),timeline:(linkTo:!(global),timerange:(from:%272020-09-06T11:43:55.814Z%27,fromStr:now-24h,kind:relative,to:%272020-09-07T11:43:55.814Z%27,toStr:now)))',
};

expect(parseRoute(nonHashLocation)).toEqual({
pageName: 'detections',
path: `${nonHashLocation.search}`,
search: nonHashLocation.search,
});
});
});
38 changes: 32 additions & 6 deletions x-pack/plugins/security_solution/public/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { isEmpty } from 'lodash/fp';

import { CoreStart } from '../../../../src/core/public';
import { APP_ID } from '../common/constants';
import {
Expand All @@ -13,13 +15,37 @@ import {
import { SecurityPageName } from './app/types';
import { InspectResponse } from './types';

export const parseRoute = (location: Pick<Location, 'hash' | 'pathname' | 'search'>) => {
if (!isEmpty(location.hash)) {
const hashPath = location.hash.split('?');
const search = hashPath.length >= 1 ? `?${hashPath[1]}` : '';
const pageRoute = hashPath.length > 0 ? hashPath[0].split('/') : [];
const pageName = pageRoute.length >= 1 ? pageRoute[1] : '';
const path = `/${pageRoute.slice(2).join('/') ?? ''}${search}`;

return {
pageName,
path,
search,
};
}

const search = location.search;
const pageRoute = location.pathname.split('/');
const pageName = pageRoute[3];
const subpluginPath = pageRoute.length > 4 ? `/${pageRoute.slice(4).join('/')}` : '';
const path = `${subpluginPath}${search}`;

return {
pageName,
path,
search,
};
};

export const manageOldSiemRoutes = async (coreStart: CoreStart) => {
const { application } = coreStart;
const hashPath = window.location.hash.split('?');
const search = hashPath.length >= 1 ? hashPath[1] : '';
const pageRoute = hashPath.length > 0 ? hashPath[0].split('/') : [];
const pageName = pageRoute.length >= 1 ? pageRoute[1] : '';
const path = `/${pageRoute.slice(2).join('/') ?? ''}?${search}`;
const { pageName, path, search } = parseRoute(window.location);

switch (pageName) {
case SecurityPageName.overview:
Expand Down Expand Up @@ -73,7 +99,7 @@ export const manageOldSiemRoutes = async (coreStart: CoreStart) => {
default:
application.navigateToApp(`${APP_ID}:${SecurityPageName.overview}`, {
replace: true,
path: `?${search}`,
path: `${search}`,
});
break;
}
Expand Down