Skip to content

Commit

Permalink
Add security support for cypress
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Li <[email protected]>
  • Loading branch information
joshuali925 committed Sep 1, 2021
1 parent b517bee commit 2ff59e6
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
41 changes: 41 additions & 0 deletions workbench/.cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,44 @@
//
// -- This will overwrite an existing command --
// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })

import { ADMIN_AUTH } from '../utils/constants';

Cypress.Commands.overwrite('visit', (originalFn, url, options) => {
// Add the basic auth header when security enabled in the OpenSearch cluster
// https://github.com/cypress-io/cypress/issues/1288
if (Cypress.env('security_enabled')) {
if (options) {
options.auth = ADMIN_AUTH;
} else {
options = { auth: ADMIN_AUTH };
}
// Add query parameters - select the default OpenSearch Dashboards tenant
options.qs = { security_tenant: 'private' };
return originalFn(url, options);
} else {
return originalFn(url, options);
}
});

// Be able to add default options to cy.request(), https://github.com/cypress-io/cypress/issues/726
Cypress.Commands.overwrite('request', (originalFn, ...args) => {
let defaults = {};
// Add the basic authentication header when security enabled in the OpenSearch cluster
if (Cypress.env('security_enabled')) {
defaults.auth = ADMIN_AUTH;
}

let options = {};
if (typeof args[0] === 'object' && args[0] !== null) {
options = Object.assign({}, args[0]);
} else if (args.length === 1) {
[options.url] = args;
} else if (args.length === 2) {
[options.method, options.url] = args;
} else if (args.length === 3) {
[options.method, options.url, options.body] = args;
}

return originalFn(Object.assign({}, defaults, options));
});
15 changes: 15 additions & 0 deletions workbench/.cypress/support/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

export const ADMIN_AUTH = {
username: 'admin',
password: 'admin',
};
5 changes: 5 additions & 0 deletions workbench/.cypress/support/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,8 @@ import './commands';

// Alternatively you can use CommonJS syntax:
// require('./commands')

// Switch the base URL of OpenSearch when security enabled in the cluster
if (Cypress.env('security_enabled')) {
Cypress.env('opensearch', 'https://localhost:9200');
}
7 changes: 6 additions & 1 deletion workbench/cypress.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@
"videosFolder": ".cypress/videos",
"requestTimeout": 60000,
"responseTimeout": 60000,
"defaultCommandTimeout": 60000
"defaultCommandTimeout": 60000,
"env": {
"opensearch": "localhost:9200",
"opensearchDashboards": "localhost:5601",
"security_enabled": true
}
}

0 comments on commit 2ff59e6

Please sign in to comment.