Skip to content

Commit

Permalink
[Fix] Redirect properly old siem app routes
Browse files Browse the repository at this point in the history
  • Loading branch information
patrykkopycinski committed Sep 7, 2020
1 parent 5a1d075 commit f1034d5
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 6 deletions.
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

0 comments on commit f1034d5

Please sign in to comment.