Skip to content

Commit 6b76c4c

Browse files
committed
Add tests for each auth type
Signed-off-by: Derek Ho <dxho@amazon.com>
1 parent ace02e2 commit 6b76c4c

File tree

7 files changed

+357
-0
lines changed

7 files changed

+357
-0
lines changed
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
import { httpServerMock } from '../../../../../../src/core/server/http/http_server.mocks';
17+
18+
import { SecurityPluginConfigType } from '../../../index';
19+
import { SecuritySessionCookie } from '../../../session/security_cookie';
20+
import {
21+
IRouter,
22+
CoreSetup,
23+
ILegacyClusterClient,
24+
Logger,
25+
SessionStorageFactory,
26+
} from '../../../../../../src/core/server';
27+
import { BasicAuthentication } from './basic_auth';
28+
29+
describe('Basic auth tests', () => {
30+
let router: IRouter;
31+
let core: CoreSetup;
32+
let esClient: ILegacyClusterClient;
33+
let sessionStorageFactory: SessionStorageFactory<SecuritySessionCookie>;
34+
let logger: Logger;
35+
36+
// Consistent with auth_handler_factory.test.ts
37+
beforeEach(() => {});
38+
39+
const config = ({
40+
saml: {
41+
extra_storage: {
42+
cookie_prefix: 'testcookie',
43+
additional_cookies: 5,
44+
},
45+
},
46+
session: {
47+
ttl: 1000,
48+
},
49+
} as unknown) as SecurityPluginConfigType;
50+
51+
test('getKeepAliveExpiry', () => {
52+
const realDateNow = Date.now.bind(global.Date);
53+
const dateNowStub = jest.fn(() => 0);
54+
global.Date.now = dateNowStub;
55+
const proxyAuthentication = new BasicAuthentication(
56+
config,
57+
sessionStorageFactory,
58+
router,
59+
esClient,
60+
core,
61+
logger
62+
);
63+
64+
const cookie: SecuritySessionCookie = {
65+
credentials: {
66+
authHeaderValueExtra: true,
67+
},
68+
expiryTime: 0,
69+
};
70+
71+
const request = httpServerMock.createOpenSearchDashboardsRequest({
72+
path: '/internal/v1',
73+
});
74+
75+
expect(proxyAuthentication.getKeepAliveExpiry(cookie, request)).toBe(1000);
76+
global.Date.now = realDateNow;
77+
});
78+
});

server/auth/types/jwt/jwt_auth.ts

+1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ export class JwtAuthentication extends AuthenticationType {
181181
}
182182

183183
getKeepAliveExpiry(cookie: SecuritySessionCookie, request: OpenSearchDashboardsRequest): number {
184+
console.log(this.buildAuthHeaderFromCookie(cookie, request)[this.authHeaderName]);
184185
return getExpirationDate(
185186
this.buildAuthHeaderFromCookie(cookie, request)[this.authHeaderName],
186187
Date.now() + this.config.session.ttl

server/auth/types/jwt/jwt_helper.test.ts

+66
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ const JWT_TEST_NO_EXP =
3737
const JWT_TEST_FAR_EXP =
3838
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJleGFtcGxlLmNvbSIsInN1YiI6ImpvaG4uZG9lQGV4YW1wbGUuY29tIiwiZXhwIjoxMzAwODE5MzgwMCwibmFtZSI6IkpvaG4gRG9lIiwicm9sZXMiOiJhZG1pbiJ9.ciW9WWtIaA-QJqy0flPSfMNQfGs9GEFqcNFY_LqrdII'; // A test JWT with a far off exp claim
3939

40+
const JWT_TEST_NEAR_EXP =
41+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJleGFtcGxlLmNvbSIsInN1YiI6ImpvaG4uZG9lQGV4YW1wbGUuY29tIiwiZXhwIjo1MCwibmFtZSI6IkpvaG4gRG9lIiwicm9sZXMiOiJhZG1pbiJ9.96_h7V_OrO-bHzhh1DUIOJ2_J2sEI8y--cjBOBonk2o'; // A test JWT with exp claim of 50
42+
4043
const router: Partial<IRouter> = { post: (body) => {} };
4144
const core = {
4245
http: {
@@ -339,5 +342,68 @@ describe('test jwt auth library', () => {
339342
expect(cookieFromURL.expiryTime!).toBe(1000);
340343
});
341344

345+
test('getKeepAliveExpiry', () => {
346+
const keepAliveConfig = {
347+
multitenancy: {
348+
enabled: false,
349+
},
350+
auth: {
351+
unauthenticated_routes: [] as string[],
352+
},
353+
session: {
354+
keepalive: true,
355+
ttl: 100000,
356+
},
357+
jwt: {
358+
url_param: 'awesome',
359+
header: 'AUTHORIZATION',
360+
extra_storage: {
361+
cookie_prefix: 'testcookie',
362+
additional_cookies: 2,
363+
},
364+
},
365+
} as SecurityPluginConfigType;
366+
367+
const jwtAuth = new JwtAuthentication(
368+
keepAliveConfig,
369+
sessionStorageFactory,
370+
router,
371+
esClient,
372+
coreSetup,
373+
logger
374+
);
375+
376+
const requestWithHeaders = httpServerMock.createOpenSearchDashboardsRequest({
377+
path: '/internal/v1',
378+
headers: {
379+
authorization: `Bearer ${JWT_TEST}`,
380+
},
381+
});
382+
383+
const cookie: SecuritySessionCookie = {
384+
credentials: {},
385+
expiryTime: 1000,
386+
};
387+
388+
// Mock the method with a JWT with far exp
389+
jest.spyOn(jwtAuth, 'buildAuthHeaderFromCookie').mockReturnValue({
390+
authorization: `Bearer ${JWT_TEST_FAR_EXP}`,
391+
});
392+
393+
// getKeepAliveExpiry takes on the value of the ttl, since it is less than the exp claim * 1000
394+
expect(jwtAuth.getKeepAliveExpiry(cookie, requestWithHeaders)).toBe(100000);
395+
396+
// Mock the method with a JWT with near exp
397+
jest.spyOn(jwtAuth, 'buildAuthHeaderFromCookie').mockReturnValue({
398+
authorization: `Bearer ${JWT_TEST_NEAR_EXP}`,
399+
});
400+
401+
// getKeepAliveExpiry takes on the value of the exp claim * 1000, since it is less than the ttl
402+
expect(jwtAuth.getKeepAliveExpiry(cookie, requestWithHeaders)).toBe(50000);
403+
404+
// Restore the original method implementation after the test
405+
jwtAuth.buildAuthHeaderFromCookie.mockRestore();
406+
});
407+
342408
/* eslint-enable no-shadow, @typescript-eslint/no-var-requires */
343409
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
import { httpServerMock } from '../../../../../../src/core/server/http/http_server.mocks';
17+
18+
import { OpenSearchDashboardsRequest } from '../../../../../../src/core/server/http/router';
19+
20+
import { SecurityPluginConfigType } from '../../../index';
21+
import { SecuritySessionCookie } from '../../../session/security_cookie';
22+
import { deflateValue } from '../../../utils/compression';
23+
import {
24+
IRouter,
25+
CoreSetup,
26+
ILegacyClusterClient,
27+
Logger,
28+
SessionStorageFactory,
29+
} from '../../../../../../src/core/server';
30+
import { MultipleAuthentication } from './multi_auth';
31+
32+
describe('Multi auth tests', () => {
33+
let router: IRouter;
34+
let core: CoreSetup;
35+
let esClient: ILegacyClusterClient;
36+
let sessionStorageFactory: SessionStorageFactory<SecuritySessionCookie>;
37+
let logger: Logger;
38+
39+
// Consistent with auth_handler_factory.test.ts
40+
beforeEach(() => {});
41+
42+
const config = ({
43+
session: {
44+
ttl: 1000,
45+
},
46+
auth: {
47+
type: 'basic',
48+
},
49+
} as unknown) as SecurityPluginConfigType;
50+
51+
test('getKeepAliveExpiry', () => {
52+
const realDateNow = Date.now.bind(global.Date);
53+
const dateNowStub = jest.fn(() => 0);
54+
global.Date.now = dateNowStub;
55+
const proxyAuthentication = new MultipleAuthentication(
56+
config,
57+
sessionStorageFactory,
58+
router,
59+
esClient,
60+
core,
61+
logger
62+
);
63+
64+
const cookie: SecuritySessionCookie = {
65+
credentials: {
66+
authHeaderValueExtra: true,
67+
},
68+
expiryTime: 1000,
69+
};
70+
71+
const request = httpServerMock.createOpenSearchDashboardsRequest({
72+
path: '/internal/v1',
73+
});
74+
75+
expect(proxyAuthentication.getKeepAliveExpiry(cookie, request)).toBe(1000); // Multi auth using basic auth's implementation
76+
global.Date.now = realDateNow;
77+
});
78+
});

server/auth/types/openid/openid_auth.test.ts

+32
Original file line numberDiff line numberDiff line change
@@ -246,4 +246,36 @@ describe('test OpenId authHeaderValue', () => {
246246
expect(await openIdAuthentication.isValidCookie(testCookie, {})).toBe(true);
247247
global.Date.now = realDateNow;
248248
});
249+
250+
test('getKeepAliveExpiry', () => {
251+
const customConfig = {
252+
openid: {
253+
pfx: 'test/certs/keyStore.p12',
254+
certificate: 'test/certs/cert.pem',
255+
private_key: 'test/certs/private-key.pem',
256+
passphrase: '',
257+
header: 'authorization',
258+
scope: [],
259+
},
260+
};
261+
262+
const openidConfig = (customConfig as unknown) as SecurityPluginConfigType;
263+
264+
const openIdAuthentication = new OpenIdAuthentication(
265+
openidConfig,
266+
sessionStorageFactory,
267+
router,
268+
esClient,
269+
core,
270+
logger
271+
);
272+
const testCookie: SecuritySessionCookie = {
273+
credentials: {
274+
authHeaderValue: 'Bearer eyToken',
275+
},
276+
expiryTime: 1000,
277+
};
278+
279+
expect(openIdAuthentication.getKeepAliveExpiry(testCookie, {})).toBe(1000);
280+
});
249281
});
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
import { httpServerMock } from '../../../../../../src/core/server/http/http_server.mocks';
17+
18+
import { SecurityPluginConfigType } from '../../../index';
19+
import { SecuritySessionCookie } from '../../../session/security_cookie';
20+
import {
21+
IRouter,
22+
CoreSetup,
23+
ILegacyClusterClient,
24+
Logger,
25+
SessionStorageFactory,
26+
} from '../../../../../../src/core/server';
27+
import { ProxyAuthentication } from './proxy_auth';
28+
29+
describe('Proxy auth tests', () => {
30+
let router: IRouter;
31+
let core: CoreSetup;
32+
let esClient: ILegacyClusterClient;
33+
let sessionStorageFactory: SessionStorageFactory<SecuritySessionCookie>;
34+
let logger: Logger;
35+
36+
// Consistent with auth_handler_factory.test.ts
37+
beforeEach(() => {});
38+
39+
const config = ({
40+
saml: {
41+
extra_storage: {
42+
cookie_prefix: 'testcookie',
43+
additional_cookies: 5,
44+
},
45+
},
46+
session: {
47+
ttl: 1000,
48+
},
49+
} as unknown) as SecurityPluginConfigType;
50+
51+
test('getKeepAliveExpiry', () => {
52+
const realDateNow = Date.now.bind(global.Date);
53+
const dateNowStub = jest.fn(() => 0);
54+
global.Date.now = dateNowStub;
55+
const proxyAuthentication = new ProxyAuthentication(
56+
config,
57+
sessionStorageFactory,
58+
router,
59+
esClient,
60+
core,
61+
logger
62+
);
63+
64+
const cookie: SecuritySessionCookie = {
65+
credentials: {
66+
authHeaderValueExtra: true,
67+
},
68+
expiryTime: 1000,
69+
};
70+
71+
const request = httpServerMock.createOpenSearchDashboardsRequest({
72+
path: '/internal/v1',
73+
});
74+
75+
expect(proxyAuthentication.getKeepAliveExpiry(cookie, request)).toBe(1000);
76+
global.Date.now = realDateNow;
77+
});
78+
});

server/auth/types/saml/saml_auth.test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,28 @@ describe('test SAML authHeaderValue', () => {
115115

116116
expect(headers).toEqual(expectedHeaders);
117117
});
118+
119+
test('getKeepAliveExpiry', () => {
120+
const samlAuthentication = new SamlAuthentication(
121+
config,
122+
sessionStorageFactory,
123+
router,
124+
esClient,
125+
core,
126+
logger
127+
);
128+
129+
const cookie: SecuritySessionCookie = {
130+
credentials: {
131+
authHeaderValueExtra: true,
132+
},
133+
expiryTime: 1000,
134+
};
135+
136+
const request = httpServerMock.createOpenSearchDashboardsRequest({
137+
path: '/internal/v1',
138+
});
139+
140+
expect(samlAuthentication.getKeepAliveExpiry(cookie, request)).toBe(1000);
141+
});
118142
});

0 commit comments

Comments
 (0)