Skip to content

Commit 3bff6db

Browse files
cwperksDarshitChanpurapeternied
authored
Fixes issue with multi-tenancy and default route to use corresponding default route for the selected tenant (#1820)
* Include saved tenant in login request for basic auth Signed-off-by: Craig Perkins <cwperx@amazon.com> * Add tests for including security_tenant in login Signed-off-by: Craig Perkins <cwperx@amazon.com> * Simplify test Signed-off-by: Craig Perkins <cwperx@amazon.com> --------- Signed-off-by: Craig Perkins <cwperx@amazon.com> Co-authored-by: Darshit Chanpura <35282393+DarshitChanpura@users.noreply.github.com> Co-authored-by: Peter Nied <petern@amazon.com>
1 parent f0cd1cf commit 3bff6db

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

public/apps/login/login-page.tsx

+22-3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
OPENID_AUTH_LOGIN_WITH_FRAGMENT,
3636
SAML_AUTH_LOGIN_WITH_FRAGMENT,
3737
} from '../../../common';
38+
import { getSavedTenant } from '../../utils/storage-utils';
3839

3940
interface LoginPageDeps {
4041
http: CoreStart['http'];
@@ -49,16 +50,34 @@ interface LoginButtonConfig {
4950
buttonstyle: string;
5051
}
5152

52-
function redirect(serverBasePath: string) {
53-
// navigate to nextUrl
53+
export function getNextPath(serverBasePath: string) {
5454
const urlParams = new URLSearchParams(window.location.search);
5555
let nextUrl = urlParams.get('nextUrl');
5656
if (!nextUrl || nextUrl.toLowerCase().includes('//')) {
5757
// Appending the next url with trailing slash. We do so because in case the serverBasePath is empty, we can simply
5858
// redirect to '/'.
5959
nextUrl = serverBasePath + '/';
6060
}
61-
window.location.href = nextUrl + window.location.hash;
61+
const savedTenant = getSavedTenant();
62+
const url = new URL(
63+
window.location.protocol + '//' + window.location.host + nextUrl + window.location.hash
64+
);
65+
if (
66+
!!savedTenant &&
67+
!(
68+
url.searchParams.has('security_tenant') ||
69+
url.searchParams.has('securitytenant') ||
70+
url.searchParams.has('securityTenant_')
71+
)
72+
) {
73+
url.searchParams.append('security_tenant', savedTenant);
74+
}
75+
return url.pathname + url.search + url.hash;
76+
}
77+
78+
function redirect(serverBasePath: string) {
79+
// navigate to nextUrl
80+
window.location.href = getNextPath(serverBasePath);
6281
}
6382

6483
export function extractNextUrlFromWindowLocation(): string {

public/apps/login/test/login-page.test.tsx

+41-1
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
import { shallow } from 'enzyme';
1717
import React from 'react';
1818
import { ClientConfigType } from '../../../types';
19-
import { LoginPage, extractNextUrlFromWindowLocation } from '../login-page';
19+
import { LoginPage, extractNextUrlFromWindowLocation, getNextPath } from '../login-page';
2020
import { validateCurrentPassword } from '../../../utils/login-utils';
2121
import { API_AUTH_LOGOUT } from '../../../../common';
2222
import { chromeServiceMock } from '../../../../../../src/core/public/mocks';
2323
import { AuthType } from '../../../../common';
24+
import { setSavedTenant } from '../../../utils/storage-utils';
2425

2526
jest.mock('../../../utils/login-utils', () => ({
2627
validateCurrentPassword: jest.fn(),
@@ -85,6 +86,45 @@ describe('test extractNextUrlFromWindowLocation', () => {
8586
});
8687
});
8788

89+
describe('test redirect', () => {
90+
test('extract redirect excludes security_tenant when no tenant in local storage', () => {
91+
// Trick to mock window.location
92+
const originalLocation = window.location;
93+
delete window.location;
94+
window.location = new URL('http://localhost:5601/app/login?nextUrl=%2Fapp%2Fdashboards') as any;
95+
setSavedTenant(null);
96+
const nextPath = getNextPath('');
97+
expect(nextPath).toEqual('/app/dashboards');
98+
window.location = originalLocation;
99+
});
100+
101+
test('extract redirect includes security_tenant when tenant in local storage', () => {
102+
const originalLocation = window.location;
103+
delete window.location;
104+
window.location = new URL('http://localhost:5601/app/login?nextUrl=%2Fapp%2Fdashboards');
105+
setSavedTenant('custom');
106+
const nextPath = getNextPath('');
107+
expect(nextPath).toEqual('/app/dashboards?security_tenant=custom');
108+
setSavedTenant(null);
109+
window.location = originalLocation;
110+
});
111+
112+
test('extract redirect includes security_tenant when tenant in local storage, existing url params and hash', () => {
113+
const originalLocation = window.location;
114+
delete window.location;
115+
window.location = new URL(
116+
"http://localhost:5601/app/login?nextUrl=%2Fapp%2Fdashboards?param1=value1#/view/7adfa750-4c81-11e8-b3d7-01146121b73d?_g=(filters:!(),refreshInterval:(pause:!f,value:900000),time:(from:now-24h,to:now))&_a=(description:'Analyze%20mock%20flight%20data%20for%20OpenSearch-Air,%20Logstash%20Airways,%20OpenSearch%20Dashboards%20Airlines%20and%20BeatsWest',filters:!(),fullScreenMode:!f,options:(hidePanelTitles:!f,useMargins:!t),query:(language:kuery,query:''),timeRestore:!t,title:'%5BFlights%5D%20Global%20Flight%20Dashboard',viewMode:view)"
117+
);
118+
setSavedTenant('custom');
119+
const nextPath = getNextPath('');
120+
expect(nextPath).toEqual(
121+
"/app/dashboards?param1=value1&security_tenant=custom#/view/7adfa750-4c81-11e8-b3d7-01146121b73d?_g=(filters:!(),refreshInterval:(pause:!f,value:900000),time:(from:now-24h,to:now))&_a=(description:'Analyze%20mock%20flight%20data%20for%20OpenSearch-Air,%20Logstash%20Airways,%20OpenSearch%20Dashboards%20Airlines%20and%20BeatsWest',filters:!(),fullScreenMode:!f,options:(hidePanelTitles:!f,useMargins:!t),query:(language:kuery,query:''),timeRestore:!t,title:'%5BFlights%5D%20Global%20Flight%20Dashboard',viewMode:view)"
122+
);
123+
setSavedTenant(null);
124+
window.location = originalLocation;
125+
});
126+
});
127+
88128
describe('Login page', () => {
89129
let chrome: ReturnType<typeof chromeServiceMock.createStartContract>;
90130
const mockHttpStart = {

0 commit comments

Comments
 (0)