|
| 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 * as osdTestServer from '../../../../src/core/test_helpers/osd_server'; |
| 17 | +import { Root } from '../../../../src/core/server/root'; |
| 18 | +import { resolve } from 'path'; |
| 19 | +import { describe, expect, it, beforeAll, afterAll } from '@jest/globals'; |
| 20 | +import { SignJWT } from 'jose'; |
| 21 | +import { |
| 22 | + ADMIN_CREDENTIALS, |
| 23 | + OPENSEARCH_DASHBOARDS_SERVER_USER, |
| 24 | + OPENSEARCH_DASHBOARDS_SERVER_PASSWORD, |
| 25 | +} from '../constant'; |
| 26 | +import wreck from '@hapi/wreck'; |
| 27 | +import { Builder, By, until } from 'selenium-webdriver'; |
| 28 | +import { Options } from 'selenium-webdriver/firefox'; |
| 29 | + |
| 30 | +describe('start OpenSearch Dashboards server', () => { |
| 31 | + let root: Root; |
| 32 | + let config; |
| 33 | + |
| 34 | + // XPath Constants |
| 35 | + const pageTitleXPath = '//*[@id="osdOverviewPageHeader__title"]'; |
| 36 | + // Browser Settings |
| 37 | + const browser = 'firefox'; |
| 38 | + const options = new Options().headless(); |
| 39 | + const rawKey = 'This is a very secure secret. No one will ever be able to guess it!'; |
| 40 | + const b = Buffer.from(rawKey); |
| 41 | + const signingKey = b.toString('base64'); |
| 42 | + |
| 43 | + beforeAll(async () => { |
| 44 | + root = osdTestServer.createRootWithSettings( |
| 45 | + { |
| 46 | + plugins: { |
| 47 | + scanDirs: [resolve(__dirname, '../..')], |
| 48 | + }, |
| 49 | + server: { |
| 50 | + host: 'localhost', |
| 51 | + port: 5601, |
| 52 | + }, |
| 53 | + logging: { |
| 54 | + silent: true, |
| 55 | + verbose: false, |
| 56 | + }, |
| 57 | + opensearch: { |
| 58 | + hosts: ['https://localhost:9200'], |
| 59 | + ignoreVersionMismatch: true, |
| 60 | + ssl: { verificationMode: 'none' }, |
| 61 | + username: OPENSEARCH_DASHBOARDS_SERVER_USER, |
| 62 | + password: OPENSEARCH_DASHBOARDS_SERVER_PASSWORD, |
| 63 | + requestHeadersWhitelist: ['authorization', 'securitytenant'], |
| 64 | + }, |
| 65 | + opensearch_security: { |
| 66 | + auth: { |
| 67 | + anonymous_auth_enabled: false, |
| 68 | + type: 'jwt', |
| 69 | + }, |
| 70 | + jwt: { |
| 71 | + url_param: 'token', |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + { |
| 76 | + // to make ignoreVersionMismatch setting work |
| 77 | + // can be removed when we have corresponding ES version |
| 78 | + dev: true, |
| 79 | + } |
| 80 | + ); |
| 81 | + |
| 82 | + console.log('Starting OpenSearchDashboards server..'); |
| 83 | + await root.setup(); |
| 84 | + await root.start(); |
| 85 | + |
| 86 | + await wreck.patch('https://localhost:9200/_plugins/_security/api/rolesmapping/all_access', { |
| 87 | + payload: [ |
| 88 | + { |
| 89 | + op: 'add', |
| 90 | + path: '/users', |
| 91 | + value: ['jwt_test'], |
| 92 | + }, |
| 93 | + ], |
| 94 | + rejectUnauthorized: false, |
| 95 | + headers: { |
| 96 | + 'Content-Type': 'application/json', |
| 97 | + authorization: ADMIN_CREDENTIALS, |
| 98 | + }, |
| 99 | + }); |
| 100 | + console.log('Starting to Download Flights Sample Data'); |
| 101 | + await wreck.post('http://localhost:5601/api/sample_data/flights', { |
| 102 | + payload: {}, |
| 103 | + rejectUnauthorized: false, |
| 104 | + headers: { |
| 105 | + 'Content-Type': 'application/json', |
| 106 | + authorization: ADMIN_CREDENTIALS, |
| 107 | + security_tenant: 'global', |
| 108 | + }, |
| 109 | + }); |
| 110 | + console.log('Downloaded Sample Data'); |
| 111 | + const getConfigResponse = await wreck.get( |
| 112 | + 'https://localhost:9200/_plugins/_security/api/securityconfig', |
| 113 | + { |
| 114 | + rejectUnauthorized: false, |
| 115 | + headers: { |
| 116 | + authorization: ADMIN_CREDENTIALS, |
| 117 | + }, |
| 118 | + } |
| 119 | + ); |
| 120 | + const responseBody = (getConfigResponse.payload as Buffer).toString(); |
| 121 | + config = JSON.parse(responseBody).config; |
| 122 | + const jwtConfig = { |
| 123 | + http_enabled: true, |
| 124 | + transport_enabled: false, |
| 125 | + order: 5, |
| 126 | + http_authenticator: { |
| 127 | + challenge: true, |
| 128 | + type: 'jwt', |
| 129 | + config: { |
| 130 | + signing_key: signingKey, |
| 131 | + jwt_header: 'Authorization', |
| 132 | + jwt_url_parameter: 'token', |
| 133 | + subject_key: 'sub', |
| 134 | + roles_key: 'roles', |
| 135 | + }, |
| 136 | + }, |
| 137 | + authentication_backend: { |
| 138 | + type: 'noop', |
| 139 | + config: {}, |
| 140 | + }, |
| 141 | + }; |
| 142 | + try { |
| 143 | + config.dynamic!.authc!.jwt_auth_domain = jwtConfig; |
| 144 | + config.dynamic!.authc!.basic_internal_auth_domain.http_authenticator.challenge = false; |
| 145 | + config.dynamic!.http!.anonymous_auth_enabled = false; |
| 146 | + await wreck.put('https://localhost:9200/_plugins/_security/api/securityconfig/config', { |
| 147 | + payload: config, |
| 148 | + rejectUnauthorized: false, |
| 149 | + headers: { |
| 150 | + 'Content-Type': 'application/json', |
| 151 | + authorization: ADMIN_CREDENTIALS, |
| 152 | + }, |
| 153 | + }); |
| 154 | + } catch (error) { |
| 155 | + console.log('Got an error while updating security config!!', error.stack); |
| 156 | + fail(error); |
| 157 | + } |
| 158 | + }); |
| 159 | + |
| 160 | + afterAll(async () => { |
| 161 | + console.log('Remove the Sample Data'); |
| 162 | + await wreck |
| 163 | + .delete('http://localhost:5601/api/sample_data/flights', { |
| 164 | + rejectUnauthorized: false, |
| 165 | + headers: { |
| 166 | + 'Content-Type': 'application/json', |
| 167 | + authorization: ADMIN_CREDENTIALS, |
| 168 | + }, |
| 169 | + }) |
| 170 | + .then((value) => { |
| 171 | + Promise.resolve(value); |
| 172 | + }) |
| 173 | + .catch((value) => { |
| 174 | + Promise.resolve(value); |
| 175 | + }); |
| 176 | + console.log('Remove the Role Mapping'); |
| 177 | + await wreck |
| 178 | + .patch('https://localhost:9200/_plugins/_security/api/rolesmapping/all_access', { |
| 179 | + payload: [ |
| 180 | + { |
| 181 | + op: 'remove', |
| 182 | + path: '/users', |
| 183 | + users: ['jwt_test'], |
| 184 | + }, |
| 185 | + ], |
| 186 | + rejectUnauthorized: false, |
| 187 | + headers: { |
| 188 | + 'Content-Type': 'application/json', |
| 189 | + authorization: ADMIN_CREDENTIALS, |
| 190 | + }, |
| 191 | + }) |
| 192 | + .then((value) => { |
| 193 | + Promise.resolve(value); |
| 194 | + }) |
| 195 | + .catch((value) => { |
| 196 | + Promise.resolve(value); |
| 197 | + }); |
| 198 | + console.log('Remove the Security Config'); |
| 199 | + await wreck |
| 200 | + .patch('https://localhost:9200/_plugins/_security/api/securityconfig', { |
| 201 | + payload: [ |
| 202 | + { |
| 203 | + op: 'remove', |
| 204 | + path: '/config/dynamic/authc/jwt_auth_domain', |
| 205 | + }, |
| 206 | + ], |
| 207 | + rejectUnauthorized: false, |
| 208 | + headers: { |
| 209 | + 'Content-Type': 'application/json', |
| 210 | + authorization: ADMIN_CREDENTIALS, |
| 211 | + }, |
| 212 | + }) |
| 213 | + .then((value) => { |
| 214 | + Promise.resolve(value); |
| 215 | + }) |
| 216 | + .catch((value) => { |
| 217 | + Promise.resolve(value); |
| 218 | + }); |
| 219 | + // shutdown OpenSearchDashboards server |
| 220 | + await root.shutdown(); |
| 221 | + }); |
| 222 | + |
| 223 | + it('Login to app/opensearch_dashboards_overview#/ when JWT is enabled', async () => { |
| 224 | + const payload = { |
| 225 | + sub: 'jwt_test', |
| 226 | + roles: 'admin,kibanauser', |
| 227 | + }; |
| 228 | + |
| 229 | + const key = new TextEncoder().encode(rawKey); |
| 230 | + |
| 231 | + const token = await new SignJWT(payload) // details to encode in the token |
| 232 | + .setProtectedHeader({ alg: 'HS256' }) // algorithm |
| 233 | + .setIssuedAt() |
| 234 | + .sign(key); |
| 235 | + const driver = getDriver(browser, options).build(); |
| 236 | + await driver.get(`http://localhost:5601/app/opensearch_dashboards_overview?token=${token}`); |
| 237 | + await driver.wait(until.elementsLocated(By.xpath(pageTitleXPath)), 10000); |
| 238 | + |
| 239 | + const cookie = await driver.manage().getCookies(); |
| 240 | + expect(cookie.length).toEqual(1); |
| 241 | + await driver.manage().deleteAllCookies(); |
| 242 | + await driver.quit(); |
| 243 | + }); |
| 244 | + |
| 245 | + it('Login to app/dev_tools#/console when JWT is enabled', async () => { |
| 246 | + const payload = { |
| 247 | + sub: 'jwt_test', |
| 248 | + roles: 'admin,kibanauser', |
| 249 | + }; |
| 250 | + |
| 251 | + const key = new TextEncoder().encode(rawKey); |
| 252 | + |
| 253 | + const token = await new SignJWT(payload) // details to encode in the token |
| 254 | + .setProtectedHeader({ alg: 'HS256' }) // algorithm |
| 255 | + .setIssuedAt() |
| 256 | + .sign(key); |
| 257 | + const driver = getDriver(browser, options).build(); |
| 258 | + await driver.get(`http://localhost:5601/app/dev_tools?token=${token}`); |
| 259 | + |
| 260 | + await driver.wait( |
| 261 | + until.elementsLocated(By.xpath('//*[@data-test-subj="sendRequestButton"]')), |
| 262 | + 10000 |
| 263 | + ); |
| 264 | + |
| 265 | + const cookie = await driver.manage().getCookies(); |
| 266 | + expect(cookie.length).toEqual(1); |
| 267 | + await driver.manage().deleteAllCookies(); |
| 268 | + await driver.quit(); |
| 269 | + }); |
| 270 | + |
| 271 | + it('Login to app/opensearch_dashboards_overview#/ when JWT is enabled with invalid token', async () => { |
| 272 | + const payload = { |
| 273 | + sub: 'jwt_test', |
| 274 | + roles: 'admin,kibanauser', |
| 275 | + }; |
| 276 | + |
| 277 | + const key = new TextEncoder().encode('wrongKey'); |
| 278 | + |
| 279 | + const token = await new SignJWT(payload) // details to encode in the token |
| 280 | + .setProtectedHeader({ alg: 'HS256' }) // algorithm |
| 281 | + .setIssuedAt() |
| 282 | + .sign(key); |
| 283 | + const driver = getDriver(browser, options).build(); |
| 284 | + await driver.get(`http://localhost:5601/app/opensearch_dashboards_overview?token=${token}`); |
| 285 | + |
| 286 | + const rep = await driver.getPageSource(); |
| 287 | + expect(rep).toContain( |
| 288 | + '"statusCode":401,"error":"Unauthorized","message":"Authentication Exception"' |
| 289 | + ); |
| 290 | + |
| 291 | + const cookie = await driver.manage().getCookies(); |
| 292 | + expect(cookie.length).toEqual(0); |
| 293 | + |
| 294 | + await driver.manage().deleteAllCookies(); |
| 295 | + await driver.quit(); |
| 296 | + }); |
| 297 | + |
| 298 | + it('Login to app/dev_tools#/console when JWT is enabled with invalid token', async () => { |
| 299 | + const payload = { |
| 300 | + sub: 'jwt_test', |
| 301 | + roles: 'admin,kibanauser', |
| 302 | + }; |
| 303 | + |
| 304 | + const key = new TextEncoder().encode('wrongKey'); |
| 305 | + |
| 306 | + const token = await new SignJWT(payload) // details to encode in the token |
| 307 | + .setProtectedHeader({ alg: 'HS256' }) // algorithm |
| 308 | + .setIssuedAt() |
| 309 | + .sign(key); |
| 310 | + const driver = getDriver(browser, options).build(); |
| 311 | + await driver.get(`http://localhost:5601/app/dev_tools?token=${token}`); |
| 312 | + |
| 313 | + const rep = await driver.getPageSource(); |
| 314 | + expect(rep).toContain( |
| 315 | + '"statusCode":401,"error":"Unauthorized","message":"Authentication Exception"' |
| 316 | + ); |
| 317 | + |
| 318 | + const cookie = await driver.manage().getCookies(); |
| 319 | + expect(cookie.length).toEqual(0); |
| 320 | + |
| 321 | + await driver.manage().deleteAllCookies(); |
| 322 | + await driver.quit(); |
| 323 | + }); |
| 324 | +}); |
| 325 | + |
| 326 | +function getDriver(browser: string, options: Options) { |
| 327 | + return new Builder().forBrowser(browser).setFirefoxOptions(options); |
| 328 | +} |
0 commit comments