Skip to content

Commit 5012a8c

Browse files
opensearch-trigger-bot[bot]prabhask5DarshitChanpura
authored
[Refactor-1160] Different Values Pointing to Basic Auth, Need to Unify (opensearch-project#1619) (opensearch-project#1649)
--------- Signed-off-by: Prabhas Kurapati <prabhask@berkeley.edu> (cherry picked from commit 2131598df843c3d171250067a8a3c26d2fc4e414) Co-authored-by: Prabhas Kurapati <66924475+prabhask5@users.noreply.github.com> Co-authored-by: Darshit Chanpura <35282393+DarshitChanpura@users.noreply.github.com>
1 parent 25c41bb commit 5012a8c

File tree

10 files changed

+55
-51
lines changed

10 files changed

+55
-51
lines changed

public/apps/account/test/log-out-button.test.tsx

+7-13
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,13 @@ import { shallow } from 'enzyme';
1717
import React from 'react';
1818
import { LogoutButton } from '../log-out-button';
1919
import { logout } from '../utils';
20+
import { AuthType } from '../../../../common';
2021

2122
jest.mock('../utils', () => ({
2223
logout: jest.fn(),
2324
}));
2425

2526
describe('Account menu - Log out button', () => {
26-
enum authType {
27-
OpenId = 'openid',
28-
SAML = 'saml',
29-
Proxy = 'proxy',
30-
Basic = 'basicauth',
31-
}
32-
3327
const mockHttpStart = {
3428
basePath: {
3529
serverBasePath: '',
@@ -39,42 +33,42 @@ describe('Account menu - Log out button', () => {
3933
describe('renders', () => {
4034
it('renders when auth type is MultiAuth: openid', () => {
4135
const component = shallow(
42-
<LogoutButton authType={authType.OpenId} http={mockHttpStart} divider={mockDivider} />
36+
<LogoutButton authType={AuthType.OPEN_ID} http={mockHttpStart} divider={mockDivider} />
4337
);
4438
expect(component).toMatchSnapshot();
4539
});
4640

4741
it('renders when auth type is MultiAuth: saml', () => {
4842
const component = shallow(
49-
<LogoutButton authType={authType.SAML} http={mockHttpStart} divider={mockDivider} />
43+
<LogoutButton authType={AuthType.SAML} http={mockHttpStart} divider={mockDivider} />
5044
);
5145
expect(component).toMatchSnapshot();
5246
});
5347

5448
it('renders when auth type is MultiAuth: basicauth', () => {
5549
const component = shallow(
56-
<LogoutButton authType={authType.Basic} http={mockHttpStart} divider={mockDivider} />
50+
<LogoutButton authType={AuthType.BASIC} http={mockHttpStart} divider={mockDivider} />
5751
);
5852
expect(component).toMatchSnapshot();
5953
});
6054

6155
it('renders when auth type is OpenId', () => {
6256
const component = shallow(
63-
<LogoutButton authType={authType.OpenId} http={mockHttpStart} divider={mockDivider} />
57+
<LogoutButton authType={AuthType.OPEN_ID} http={mockHttpStart} divider={mockDivider} />
6458
);
6559
expect(component).toMatchSnapshot();
6660
});
6761

6862
it('renders when auth type is SAML', () => {
6963
const component = shallow(
70-
<LogoutButton authType={authType.SAML} http={mockHttpStart} divider={mockDivider} />
64+
<LogoutButton authType={AuthType.SAML} http={mockHttpStart} divider={mockDivider} />
7165
);
7266
expect(component).toMatchSnapshot();
7367
});
7468

7569
it('renders when auth type is Proxy', () => {
7670
const component = shallow(
77-
<LogoutButton authType={authType.Proxy} http={mockHttpStart} divider={mockDivider} />
71+
<LogoutButton authType={AuthType.PROXY} http={mockHttpStart} divider={mockDivider} />
7872
);
7973
expect(component).toMatchSnapshot();
8074
});

public/apps/customerror/custom-error.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import ReactDOM from 'react-dom';
2020
import { Router, Route } from 'react-router-dom';
2121
import { ERROR_MISSING_ROLE_PATH } from '../../../common';
2222
import { ClientConfigType } from '../../types';
23+
import { AuthType } from '../../../common';
2324
import './_index.scss';
2425
import { logout } from '../account/utils';
2526

@@ -28,7 +29,7 @@ interface CustomErrorDeps {
2829
subtitle: string;
2930
http: CoreStart['http'];
3031
chrome: CoreStart['chrome'];
31-
config: ClientConfigType['ui']['basicauth']['login'];
32+
config: ClientConfigType['ui'][AuthType.BASIC]['login'];
3233
}
3334

3435
export function CustomErrorPage(props: CustomErrorDeps) {

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { LoginPage, extractNextUrlFromWindowLocation } 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';
23+
import { AuthType } from '../../../../common';
2324

2425
jest.mock('../../../utils/login-utils', () => ({
2526
validateCurrentPassword: jest.fn(),
@@ -101,7 +102,7 @@ describe('Login page', () => {
101102
const config: ClientConfigType = {
102103
ui: configUI,
103104
auth: {
104-
type: ['basicauth'],
105+
type: [AuthType.BASIC],
105106
logout_url: API_AUTH_LOGOUT,
106107
},
107108
};
@@ -115,7 +116,7 @@ describe('Login page', () => {
115116
const config: ClientConfigType = {
116117
ui: configUI,
117118
auth: {
118-
type: 'basicauth',
119+
type: AuthType.BASIC,
119120
logout_url: API_AUTH_LOGOUT,
120121
},
121122
};
@@ -129,7 +130,7 @@ describe('Login page', () => {
129130
const config: ClientConfigType = {
130131
ui: configUI,
131132
auth: {
132-
type: ['basicauth', 'openid', 'saml'],
133+
type: [AuthType.BASIC, 'openid', AuthType.SAML],
133134
logout_url: API_AUTH_LOGOUT,
134135
},
135136
};
@@ -173,7 +174,7 @@ describe('Login page', () => {
173174
const config: ClientConfigType = {
174175
ui: configUiDefault,
175176
auth: {
176-
type: 'basicauth',
177+
type: AuthType.BASIC,
177178
},
178179
};
179180
beforeEach(() => {
@@ -207,7 +208,7 @@ describe('Login page', () => {
207208
const config: ClientConfigType = {
208209
ui: configUiDefault,
209210
auth: {
210-
type: 'basicauth',
211+
type: AuthType.BASIC,
211212
},
212213
};
213214
beforeEach(() => {

server/auth/auth_handler_factory.test.ts

+24-20
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@ import {
2323
import { SecurityPluginConfigType } from '..';
2424
import { SecuritySessionCookie } from '../session/security_cookie';
2525
import { getAuthenticationHandler } from './auth_handler_factory';
26+
import { AuthType } from '../../common';
27+
28+
const mockBasicAuthType = AuthType.BASIC;
29+
const mockSAMLAuthType = AuthType.SAML;
2630

2731
jest.mock('./types', () => {
2832
return {
2933
BasicAuthentication: jest.fn().mockImplementation(() => {
3034
return {
3135
authHandler: () => {},
32-
type: 'basicauth',
36+
type: mockBasicAuthType,
3337
init: () => {},
3438
};
3539
}),
@@ -57,14 +61,14 @@ jest.mock('./types', () => {
5761
SamlAuthentication: jest.fn().mockImplementation(() => {
5862
return {
5963
authHandler: () => {},
60-
type: 'saml',
64+
type: mockSAMLAuthType,
6165
init: () => {},
6266
};
6367
}),
6468
MultipleAuthentication: jest.fn().mockImplementation(() => {
6569
return {
6670
authHandler: () => {},
67-
type: ['openid', 'saml', 'basiauth'],
71+
type: ['openid', mockSAMLAuthType, mockBasicAuthType],
6872
init: () => {},
6973
};
7074
}),
@@ -83,54 +87,54 @@ describe('test authentication factory', () => {
8387

8488
test('get basic auth: string array', async () => {
8589
const auth = await getAuthenticationHandler(
86-
['basicauth'],
90+
[AuthType.BASIC],
8791
router,
8892
config,
8993
core,
9094
esClient,
9195
sessionStorageFactory,
9296
logger
9397
);
94-
expect(auth.type).toEqual('basicauth');
98+
expect(auth.type).toEqual(AuthType.BASIC);
9599
});
96100

97101
test('get basic auth: string', async () => {
98102
const auth = await getAuthenticationHandler(
99-
'basicauth',
103+
AuthType.BASIC,
100104
router,
101105
config,
102106
core,
103107
esClient,
104108
sessionStorageFactory,
105109
logger
106110
);
107-
expect(auth.type).toEqual('basicauth');
111+
expect(auth.type).toEqual(AuthType.BASIC);
108112
});
109113

110-
test('get basic auth with empty auth type: string array', async () => {
114+
test('get basic auth with empty auth type: string', async () => {
111115
const auth = await getAuthenticationHandler(
112-
[''],
116+
'',
113117
router,
114118
config,
115119
core,
116120
esClient,
117121
sessionStorageFactory,
118122
logger
119123
);
120-
expect(auth.type).toEqual('basicauth');
124+
expect(auth.type).toEqual(AuthType.BASIC);
121125
});
122126

123-
test('get basic auth with empty auth type: string', async () => {
127+
test('get basic auth with empty auth type: string array', async () => {
124128
const auth = await getAuthenticationHandler(
125-
'',
129+
[''],
126130
router,
127131
config,
128132
core,
129133
esClient,
130134
sessionStorageFactory,
131135
logger
132136
);
133-
expect(auth.type).toEqual('basicauth');
137+
expect(auth.type).toEqual(AuthType.BASIC);
134138
});
135139

136140
test('get jwt auth: string array', async () => {
@@ -213,28 +217,28 @@ describe('test authentication factory', () => {
213217

214218
test('get saml auth: string array', async () => {
215219
const auth = await getAuthenticationHandler(
216-
['saml'],
220+
[AuthType.SAML],
217221
router,
218222
config,
219223
core,
220224
esClient,
221225
sessionStorageFactory,
222226
logger
223227
);
224-
expect(auth.type).toEqual('saml');
228+
expect(auth.type).toEqual(AuthType.SAML);
225229
});
226230

227231
test('get saml auth: string', async () => {
228232
const auth = await getAuthenticationHandler(
229-
'saml',
233+
AuthType.SAML,
230234
router,
231235
config,
232236
core,
233237
esClient,
234238
sessionStorageFactory,
235239
logger
236240
);
237-
expect(auth.type).toEqual('saml');
241+
expect(auth.type).toEqual(AuthType.SAML);
238242
});
239243

240244
test('multiple_auth_enabled is on, get multi auth', async () => {
@@ -244,15 +248,15 @@ describe('test authentication factory', () => {
244248
},
245249
};
246250
const auth = await getAuthenticationHandler(
247-
['openid', 'saml', 'basiauth'],
251+
['openid', AuthType.SAML, AuthType.BASIC],
248252
router,
249253
config,
250254
core,
251255
esClient,
252256
sessionStorageFactory,
253257
logger
254258
);
255-
expect(auth.type).toEqual(['openid', 'saml', 'basiauth']);
259+
expect(auth.type).toEqual(['openid', AuthType.SAML, AuthType.BASIC]);
256260
});
257261

258262
test('multiple_auth_enabled is off, get multi auth', async () => {
@@ -263,7 +267,7 @@ describe('test authentication factory', () => {
263267
};
264268
try {
265269
await getAuthenticationHandler(
266-
['openid', 'saml', 'basiauth'],
270+
['openid', AuthType.SAML, AuthType.BASIC],
267271
router,
268272
config,
269273
core,

server/auth/types/basic/routes.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
} from '../../../../common';
3131
import { resolveTenant } from '../../../multitenancy/tenant_resolver';
3232
import { encodeUriQuery } from '../../../../../../src/plugins/opensearch_dashboards_utils/common/url/encode_uri_query';
33+
import { AuthType } from '../../../../common';
3334

3435
export class BasicAuthRoutes {
3536
constructor(
@@ -112,7 +113,7 @@ export class BasicAuthRoutes {
112113
credentials: {
113114
authHeaderValue: `Basic ${encodedCredentials}`,
114115
},
115-
authType: 'basicauth',
116+
authType: AuthType.BASIC,
116117
isAnonymousAuth: false,
117118
expiryTime: Date.now() + this.config.session.ttl,
118119
};
@@ -202,7 +203,7 @@ export class BasicAuthRoutes {
202203
this.sessionStorageFactory.asScoped(request).clear();
203204
const sessionStorage: SecuritySessionCookie = {
204205
username: user.username,
205-
authType: 'basicauth',
206+
authType: AuthType.BASIC,
206207
isAnonymousAuth: true,
207208
expiryTime: Date.now() + this.config.session.ttl,
208209
};

server/auth/types/saml/saml_auth.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import {
4545
export class SamlAuthentication extends AuthenticationType {
4646
public static readonly AUTH_HEADER_NAME = 'authorization';
4747

48-
public readonly type: string = 'saml';
48+
public readonly type: string = AuthType.SAML;
4949

5050
constructor(
5151
config: SecurityPluginConfigType,

server/index.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
import { schema, TypeOf } from '@osd/config-schema';
1717
import { PluginInitializerContext, PluginConfigDescriptor } from '../../../src/core/server';
1818
import { SecurityPlugin } from './plugin';
19+
import { AuthType } from '../common';
1920

2021
const validateAuthType = (value: string[]) => {
2122
const supportedAuthTypes = [
2223
'',
23-
'basicauth',
24+
AuthType.BASIC,
2425
'jwt',
2526
'openid',
26-
'saml',
27+
AuthType.SAML,
2728
'proxy',
2829
'kerberos',
2930
'proxycache',
@@ -88,7 +89,7 @@ export const configSchema = schema.object({
8889

8990
if (value.length > 1) {
9091
const includeBasicAuth = value.find((element) => {
91-
return element.toLowerCase() === 'basicauth';
92+
return element.toLowerCase() === AuthType.BASIC;
9293
});
9394

9495
if (!includeBasicAuth) {

server/routes/auth_type_routes.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
import { IRouter } from 'opensearch-dashboards/server';
1717
import { SecurityPluginConfigType } from '..';
18-
18+
import { AuthType } from '../../common';
1919
export function defineAuthTypeRoutes(router: IRouter, config: SecurityPluginConfigType) {
2020
/**
2121
* Auth type API that returns current auth type configured on OpenSearchDashboards Server.
@@ -30,7 +30,7 @@ export function defineAuthTypeRoutes(router: IRouter, config: SecurityPluginConf
3030
router.get(
3131
{ path: '/api/authtype', validate: false, options: { authRequired: false } },
3232
async (context, request, response) => {
33-
const authType = config.auth.type || 'basicauth';
33+
const authType = config.auth.type || AuthType.BASIC;
3434
return response.ok({
3535
body: {
3636
authtype: authType,

0 commit comments

Comments
 (0)