|
| 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 { SecurityPluginConfigType } from '../..'; |
| 17 | +import { AuthenticationType } from './authentication_type'; |
| 18 | +import { httpServerMock } from '../../../../../src/core/server/mocks'; |
| 19 | + |
| 20 | +class DummyAuthType extends AuthenticationType { |
| 21 | + buildAuthHeaderFromCookie() {} |
| 22 | + getAdditionalAuthHeader() {} |
| 23 | + handleUnauthedRequest() {} |
| 24 | + getCookie() { |
| 25 | + return {}; |
| 26 | + } |
| 27 | + isValidCookie() { |
| 28 | + return Promise.resolve(true); |
| 29 | + } |
| 30 | + requestIncludesAuthInfo() { |
| 31 | + return false; |
| 32 | + } |
| 33 | + resolveTenant(): Promise<string | undefined> { |
| 34 | + return Promise.resolve('dummy_tenant'); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +describe('test tenant header', () => { |
| 39 | + const config = { |
| 40 | + multitenancy: { |
| 41 | + enabled: true, |
| 42 | + }, |
| 43 | + auth: { |
| 44 | + unauthenticated_routes: [] as string[], |
| 45 | + }, |
| 46 | + session: { |
| 47 | + keepalive: false, |
| 48 | + }, |
| 49 | + } as SecurityPluginConfigType; |
| 50 | + const sessionStorageFactory = { |
| 51 | + asScoped: jest.fn(() => { |
| 52 | + return { |
| 53 | + clear: jest.fn(), |
| 54 | + get: () => { |
| 55 | + return { |
| 56 | + tenant: 'dummy_tenant', |
| 57 | + }; |
| 58 | + }, |
| 59 | + }; |
| 60 | + }), |
| 61 | + }; |
| 62 | + const router = jest.fn(); |
| 63 | + const esClient = { |
| 64 | + asScoped: jest.fn().mockImplementation(() => { |
| 65 | + return { |
| 66 | + callAsCurrentUser: jest.fn().mockImplementation(() => { |
| 67 | + return { username: 'dummy-username' }; |
| 68 | + }), |
| 69 | + }; |
| 70 | + }), |
| 71 | + }; |
| 72 | + const coreSetup = jest.fn(); |
| 73 | + const logger = { |
| 74 | + error: jest.fn(), |
| 75 | + }; |
| 76 | + |
| 77 | + const dummyAuthType = new DummyAuthType( |
| 78 | + config, |
| 79 | + sessionStorageFactory, |
| 80 | + router, |
| 81 | + esClient, |
| 82 | + coreSetup, |
| 83 | + logger |
| 84 | + ); |
| 85 | + |
| 86 | + it(`common API includes tenant info`, async () => { |
| 87 | + const request = httpServerMock.createOpenSearchDashboardsRequest({ |
| 88 | + path: '/api/v1', |
| 89 | + }); |
| 90 | + const response = jest.fn(); |
| 91 | + const toolkit = { |
| 92 | + authenticated: jest.fn((value) => value), |
| 93 | + }; |
| 94 | + const result = await dummyAuthType.authHandler(request, response, toolkit); |
| 95 | + expect(result.requestHeaders.securitytenant).toEqual('dummy_tenant'); |
| 96 | + }); |
| 97 | + |
| 98 | + it(`internal API includes tenant info`, async () => { |
| 99 | + const request = httpServerMock.createOpenSearchDashboardsRequest({ |
| 100 | + path: '/internal/v1', |
| 101 | + }); |
| 102 | + const response = jest.fn(); |
| 103 | + const toolkit = { |
| 104 | + authenticated: jest.fn((value) => value), |
| 105 | + }; |
| 106 | + const result = await dummyAuthType.authHandler(request, response, toolkit); |
| 107 | + expect(result.requestHeaders.securitytenant).toEqual('dummy_tenant'); |
| 108 | + }); |
| 109 | +}); |
0 commit comments