forked from opensearch-project/security-dashboards-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Stephen Crawford <[email protected]>
- Loading branch information
1 parent
2af71cf
commit 3be0167
Showing
6 changed files
with
494 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { httpServerMock } from '../../../../../../src/core/server/http/http_server.mocks'; | ||
Check failure on line 1 in server/auth/types/proxy/proxy_auth.test.ts GitHub Actions / Run unit tests (ubuntu-latest)
|
||
import { IRouter, OpenSearchDashboardsRequest } from '../../../../../../src/core/server/http/router'; | ||
Check failure on line 2 in server/auth/types/proxy/proxy_auth.test.ts GitHub Actions / Run unit tests (ubuntu-latest)
|
||
import { SecurityPluginConfigType } from '../../../index'; | ||
import { SecuritySessionCookie } from '../../../session/security_cookie'; | ||
import { deflateValue } from '../../../utils/compression'; | ||
import { | ||
CoreSetup, | ||
ILegacyClusterClient, | ||
Logger, | ||
SessionStorageFactory, | ||
} from '../../../../../../src/core/server'; | ||
import { ProxyAuthentication } from './proxy_auth'; | ||
|
||
describe('ProxyAuthentication', () => { | ||
let proxyAuthentication: ProxyAuthentication; | ||
let router: IRouter; | ||
let core: CoreSetup; | ||
let esClient: ILegacyClusterClient; | ||
let sessionStorageFactory: SessionStorageFactory<SecuritySessionCookie>; | ||
let logger: Logger; | ||
|
||
beforeEach(() => { | ||
// Set up mock dependencies | ||
router = {} as IRouter; | ||
core = {} as CoreSetup; | ||
esClient = {} as ILegacyClusterClient; | ||
sessionStorageFactory = {} as SessionStorageFactory<SecuritySessionCookie>; | ||
logger = {} as Logger; | ||
|
||
const config = ({ | ||
proxy: { | ||
extra_storage: { | ||
cookie_prefix: 'testcookie', | ||
additional_cookies: 5, | ||
}, | ||
}, | ||
} as unknown) as SecurityPluginConfigType; | ||
|
||
proxyAuthentication = new ProxyAuthentication( | ||
config, | ||
sessionStorageFactory, | ||
router, | ||
esClient, | ||
core, | ||
logger | ||
); | ||
}); | ||
|
||
it('should build auth header from cookie with authHeaderValue', () => { | ||
Check failure on line 49 in server/auth/types/proxy/proxy_auth.test.ts GitHub Actions / Run unit tests (ubuntu-latest)
|
||
|
||
const cookie: SecuritySessionCookie = { | ||
credentials: { | ||
authHeaderValue: 'Bearer eyToken', | ||
}, | ||
}; | ||
|
||
const expectedHeaders = { | ||
authorization: 'Bearer eyToken', | ||
}; | ||
|
||
const headers = proxyAuthentication.buildAuthHeaderFromCookie(cookie); | ||
|
||
expect(headers).toEqual(expectedHeaders); | ||
}); | ||
|
||
it('should get authHeaderValue from split cookies', () => { | ||
const testString = 'Bearer eyCombinedToken'; | ||
const testStringBuffer: Buffer = deflateValue(testString); | ||
const cookieValue = testStringBuffer.toString('base64'); | ||
const cookiePrefix = 'testcookie'; | ||
const splitValueAt = Math.ceil(cookieValue.length / 5); | ||
const mockRequest = httpServerMock.createRawRequest({ | ||
state: { | ||
[cookiePrefix + '1']: cookieValue.substring(0, splitValueAt), | ||
[cookiePrefix + '2']: cookieValue.substring(splitValueAt), | ||
}, | ||
}); | ||
OpenSearchDashboardsRequest.from(mockRequest); | ||
const cookie: SecuritySessionCookie = { | ||
credentials: { | ||
authHeaderValueExtra: true, | ||
}, | ||
}; | ||
|
||
const expectedHeaders = { | ||
authorization: testString, | ||
}; | ||
|
||
const headers = proxyAuthentication.buildAuthHeaderFromCookie(cookie); | ||
|
||
expect(headers).toEqual(expectedHeaders); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
import * as osdTestServer from '../../../../src/core/test_helpers/osd_server'; | ||
import { Root } from '../../../../src/core/server/root'; | ||
import { resolve } from 'path'; | ||
import { describe, expect, it, beforeAll, afterAll } from '@jest/globals'; | ||
import { | ||
OPENSEARCH_DASHBOARDS_SERVER_USER, | ||
OPENSEARCH_DASHBOARDS_SERVER_PASSWORD, | ||
ADMIN_USER, | ||
PROXY_USER, | ||
PROXY_ROLE, | ||
PROXY_ADMIN_ROLE, | ||
} from '../constant'; | ||
|
||
describe('start OpenSearch Dashboards server', () => { | ||
let root: Root; | ||
|
||
beforeAll(async () => { | ||
root = osdTestServer.createRootWithSettings( | ||
{ | ||
plugins: { | ||
scanDirs: [resolve(__dirname, '../..')], | ||
}, | ||
opensearch: { | ||
hosts: ['https://localhost:9200'], | ||
ignoreVersionMismatch: true, | ||
ssl: { verificationMode: 'none' }, | ||
username: OPENSEARCH_DASHBOARDS_SERVER_USER, | ||
password: OPENSEARCH_DASHBOARDS_SERVER_PASSWORD, | ||
requestHeadersAllowlist: [ | ||
'securitytenant', | ||
'Authorization', | ||
'x-forwarded-for', | ||
'x-proxy-user', | ||
'x-proxy-roles', | ||
], | ||
}, | ||
opensearch_security: { | ||
auth: { | ||
type: 'proxy', | ||
}, | ||
proxycache: { | ||
user_header: 'x-proxy-user', | ||
roles_header: 'x-proxy-roles', | ||
}, | ||
}, | ||
}, | ||
{ | ||
// to make ignoreVersionMismatch setting work | ||
// can be removed when we have corresponding ES version | ||
dev: true, | ||
} | ||
); | ||
}); | ||
|
||
afterAll(async () => { | ||
// shutdown OpenSearchDashboards server | ||
await root.shutdown(); | ||
}); | ||
|
||
it('can access home page with proxy header', async () => { | ||
const response = await osdTestServer.request | ||
.get(root, 'app/home#/') | ||
.set(PROXY_USER, ADMIN_USER) | ||
.set(PROXY_ROLE, PROXY_ADMIN_ROLE); | ||
expect(response.status).toEqual(200); | ||
}); | ||
|
||
it('cannot access home page without proxy header', async () => { | ||
const response = await osdTestServer.request.get(root, 'app/home#/'); | ||
expect(response.status).toEqual(401); | ||
}); | ||
|
||
it('cannot access home page with partial proxy header', async () => { | ||
const response = await osdTestServer.request | ||
.get(root, 'app/home#/') | ||
.set(PROXY_USER, ADMIN_USER); | ||
expect(response.status).toEqual(401); | ||
}); | ||
|
||
it('cannot access home page with partial proxy header2', async () => { | ||
const response = await osdTestServer.request | ||
.get(root, 'app/home#/') | ||
.set(PROXY_ROLE, PROXY_ADMIN_ROLE); | ||
expect(response.status).toEqual(401); | ||
}); | ||
}); |
Oops, something went wrong.