Skip to content

Commit

Permalink
Allow URL params for Security plugin intercept calls (#1276)
Browse files Browse the repository at this point in the history
* Allow url params in intercept

Signed-off-by: Craig Perkins <[email protected]>

* Disable home modal

Signed-off-by: Craig Perkins <[email protected]>

* Update cypress/integration/plugins/security-dashboards-plugin/change_tenant_successfully.js

Signed-off-by: SuZhou-Joe <[email protected]>

* Fix CI check on main

Signed-off-by: Craig Perkins <[email protected]>

* Change text back

Signed-off-by: Craig Perkins <[email protected]>

* Move tenancy test over

Signed-off-by: Derek Ho <[email protected]>

* Use correct header when creating dashboard in tenancy_change_on_shortlink.js

Signed-off-by: Craig Perkins <[email protected]>

* Adjust import

Signed-off-by: Craig Perkins <[email protected]>

* Remove @

Signed-off-by: Craig Perkins <[email protected]>

* Add wildcard

Signed-off-by: Craig Perkins <[email protected]>

* Fix sanity_tests.spec.js

Signed-off-by: Craig Perkins <[email protected]>

* Close the modal

Signed-off-by: Craig Perkins <[email protected]>

---------

Signed-off-by: Craig Perkins <[email protected]>
Signed-off-by: SuZhou-Joe <[email protected]>
Signed-off-by: Derek Ho <[email protected]>
Co-authored-by: SuZhou-Joe <[email protected]>
Co-authored-by: Derek Ho <[email protected]>
  • Loading branch information
3 people authored May 16, 2024
1 parent 5c156d1 commit ed49fa5
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 74 deletions.
1 change: 1 addition & 0 deletions cypress.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"remoteDataSourceBasicAuthPassword": "admin",
"SECURITY_ENABLED": false,
"AGGREGATION_VIEW": false,
"MULTITENANCY_ENABLED": true,
"username": "admin",
"password": "myStrongPassword123!",
"ENDPOINT_WITH_PROXY": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ if (Cypress.env('SECURITY_ENABLED') && Cypress.env('AGGREGATION_VIEW')) {
ADMIN_AUTH.newUser = Cypress.env('username');
ADMIN_AUTH.newPassword = Cypress.env('password');
CURRENT_TENANT.newTenant = 'private';
cy.deleteIndexPattern('index-pattern1', { failOnStatusCode: false });
cy.deleteIndexPattern('index-pattern2', { failOnStatusCode: false });
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ if (Cypress.env('SECURITY_ENABLED')) {

before(() => {
cy.server();
localStorage.setItem('home:welcome:show', false);
localStorage.setItem('home:newThemeModal:show', false);
});
it('Checks that the tenant switcher can switch tenants despite a different tenant being present in the tenant query parameter.', function () {
CURRENT_TENANT.newTenant = tenantName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ if (Cypress.env('SECURITY_ENABLED')) {
indexPatternPrivateTenantHeaderSetUp
);
});
after(() => {
cy.deleteIndexPattern('index-pattern1', { failOnStatusCode: false });
cy.deleteIndexPattern('index-pattern2', { failOnStatusCode: false });
});
it('Test 1 Disable Multi Tenancy ', () => {
CURRENT_TENANT.newTenant = 'private';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ if (Cypress.env('SECURITY_ENABLED')) {
`"${TEST_CONFIG.tenant.name}"`
);
window.localStorage.setItem('home:newThemeModal:show', false);
window.localStorage.setItem('home:welcome:show', false);
},
});
cy.waitForLoader();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
import {
BASE_PATH,
SEC_UI_TENANTS_PATH,
SEC_TENANTS_FIXTURES_PATH,
SEC_INTERNALUSERS_FIXTURES_PATH,
SEC_API_INTERNAL_USERS_PATH,
SEC_API_INTERNAL_ACCOUNTS_PATH,
} from '../../../utils/constants';

if (Cypress.env('SECURITY_ENABLED')) {
Expand Down Expand Up @@ -48,17 +47,14 @@ if (Cypress.env('SECURITY_ENABLED')) {
tenantDescription
);

cy.mockTenantsAction(
SEC_TENANTS_FIXTURES_PATH + '/tenants_post_creation_response.json',
() => {
cy.get('button[id="submit"]').first().click({ force: true });
}
);
cy.get('button[id="submit"]').first().click({ force: true });

cy.url().should((url) => {
expect(url).to.contain('/tenants');
});

cy.reload();

cy.contains('h3', 'Tenants');
// should contain the new tenant that was just created
cy.contains('.euiTableCellContent', tenantName);
Expand All @@ -69,7 +65,7 @@ if (Cypress.env('SECURITY_ENABLED')) {
// Navigate to Security/Internal User Database section

cy.visit(`${BASE_PATH}/app/security-dashboards-plugin#/users`);
cy.intercept(SEC_API_INTERNAL_USERS_PATH, {
cy.intercept(`${SEC_API_INTERNAL_ACCOUNTS_PATH}*`, {
fixture:
SEC_INTERNALUSERS_FIXTURES_PATH + '/internalusers_info_response.json',
}).as('listUserResponse');
Expand All @@ -85,7 +81,7 @@ if (Cypress.env('SECURITY_ENABLED')) {
// Submit the form to create the user
cy.get('button').contains('Create').click();

cy.intercept(SEC_API_INTERNAL_USERS_PATH, {
cy.intercept(`${SEC_API_INTERNAL_ACCOUNTS_PATH}*`, {
fixture:
SEC_INTERNALUSERS_FIXTURES_PATH +
'/internalusers_response_post_new_user_creation.json',
Expand Down Expand Up @@ -161,7 +157,7 @@ if (Cypress.env('SECURITY_ENABLED')) {
// Step 3: Navigate to Manage data to add an index pattern
cy.visit(`${BASE_PATH}/app/home`);
cy.get('button[aria-label="Closes this modal window"]').click();
cy.get('a').contains('Manage').click(); // Adjust the selector as needed
cy.contains('Manage').click(); // Adjust the selector as needed

// Step 4: Add the index pattern
cy.get('[data-test-subj="indexPatterns"]').click();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ export function switchTenantTo(newTenant) {
cy.getElementByTestId('account-popover').click();
cy.intercept({
method: 'GET',
url: '/api/v1/auth/dashboardsinfo',
url: '/api/v1/auth/dashboardsinfo*',
}).as('waitForDashboardsInfo');

cy.intercept({
method: 'GET',
url: '/api/v1/configuration/account',
url: '/api/v1/configuration/account*',
}).as('waitForAccountInfo');

cy.getElementByTestId('switch-tenants').click();
Expand All @@ -37,7 +37,7 @@ export function switchTenantTo(newTenant) {

cy.intercept({
method: 'POST',
url: '/api/v1/multitenancy/tenant',
url: '/api/v1/multitenancy/tenant*',
}).as('waitForUpdatingTenants');
cy.getElementByTestId('tenant-switch-modal')
.find('[data-test-subj="confirm"]')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,11 @@

import { CURRENT_TENANT } from '../../../utils/commands';
import { switchTenantTo } from './switch_tenant';
import indexPatternGlobalTenantHeaderSetUp from '../../../fixtures/plugins/security-dashboards-plugin/indexpatterns/indexPatternGlobalTenantHeader.json';
import indexPatternPrivateTenantHeaderSetUp from '../../../fixtures/plugins/security-dashboards-plugin/indexpatterns/indexPatternPrivateTenantHeader.json';

if (Cypress.env('SECURITY_ENABLED')) {
describe('Multi Tenancy Tests: ', () => {
before(() => {
cy.server();

cy.createIndexPattern(
'index-pattern1',
{
title: 's*',
timeFieldName: 'timestamp',
},
indexPatternGlobalTenantHeaderSetUp
);

cy.createIndexPattern(
'index-pattern2',
{
title: 'se*',
timeFieldName: 'timestamp',
},
indexPatternPrivateTenantHeaderSetUp
);
});

it('Tests that when the short URL is copied and pasted, it will route correctly with the right tenant', function () {
Expand All @@ -43,7 +23,7 @@ if (Cypress.env('SECURITY_ENABLED')) {
title: dashboardName,
},
{
security_tenant: 'private',
securitytenant: 'private',
}
);

Expand Down Expand Up @@ -83,13 +63,10 @@ if (Cypress.env('SECURITY_ENABLED')) {
.should('be.visible')
.click();

cy.getElementByTestId('savedObjectTitle').type(dashboardName);

cy.intercept({
method: 'POST',
url: '/api/saved_objects/_bulk_get',
}).as('waitForReloadingDashboard');
cy.getElementByTestId('confirmSaveSavedObjectButton').click();
cy.wait('@waitForReloadingDashboard');
cy.wait(2000);

Expand All @@ -113,36 +90,26 @@ if (Cypress.env('SECURITY_ENABLED')) {
cy.log('Short url is ' + shortUrl);
// Navigate away to avoid the non existing dashboard in the next tenant.
switchTenantTo('global');
cy.waitForLoader();
cy.getElementByTestId('account-popover').click();
cy.get('#tenantName').should('contain.text', 'Global');

// Since we can't reliably read the clipboard data, we have to append the tenant parameter manually
cy.visit(shortUrl + '?security_tenant=private', {
excludeTenant: true, // We are passing the tenant as a query parameter. Mainly because of readability.
onBeforeLoad(window) {
// Here we are simulating the new tab scenario which isn't supported by Cypress
window.sessionStorage.clear();
},
});
waitForGetTenant: true, // We are passing the tenant as a query parameter. Mainly because of readability.
excludeTenant: true,
cache: 'clear',
}).reload();

cy.waitForLoader();

cy.url({ timeout: 10000 }).should('contain', 'security_tenant=');
cy.getElementByTestId('account-popover').should('be.visible').click();
cy.get('#tenantName').should('contain.text', 'Private');
cy.getElementByTestId('breadcrumb last').should(
'contain.text',
dashboardName
);
});
});
after(() => {
cy.deleteIndexPattern('index-pattern1', {
headers: {
securitytenant: ['global'],
'osd-xsrf': true,
},
});
cy.deleteIndexPattern('index-pattern2', {
headers: {
securitytenant: ['private'],
'osd-xsrf': true,
},
});
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { TENANTS_MANAGE_PATH } from '../../../utils/dashboards/constants';
import { SECURITY_PLUGIN_PATH } from '../../../utils/dashboards/constants';

if (Cypress.env('SECURITY_ENABLED')) {
if (Cypress.env('SECURITY_ENABLED') && !Cypress.env('MULTITENANCY_ENABLED')) {
describe('Multi Tenancy Tests: ', () => {
before(() => {
cy.server();
});
it('Test Dashboards tenancy features should not be accessible ', () => {
// This test is to ensure tenancy related features are not accessible when opensearch_security.multitenancy.enabled is disabled in the opensearchdashboard.yaml
cy.visit(TENANTS_MANAGE_PATH);
cy.visit(SECURITY_PLUGIN_PATH);
cy.waitForLoader();

cy.contains('You have not enabled multi tenancy').should('exist');
cy.get('[title="Tenants"]').should('not.exist');

// Switch tenants button should not exist when multi-tenancy is disabled.
cy.get('#user-icon-btn').click();
Expand Down
4 changes: 2 additions & 2 deletions cypress/integration/plugins/security/tenants_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ if (Cypress.env('SECURITY_ENABLED')) {
}
);

cy.contains('h3', 'Dashboards tenants');
cy.contains('h3', 'Tenants');

// One of the many tenants
cy.contains('.euiTableCellContent', 'Global');
Expand Down Expand Up @@ -69,7 +69,7 @@ if (Cypress.env('SECURITY_ENABLED')) {
expect(url).to.contain('/tenants');
});

cy.contains('h3', 'Dashboards tenants');
cy.contains('h3', 'Tenants');
// should contain the new tenant that was just created
cy.contains('.euiTableCellContent', tenantName);
cy.contains('span', tenantDescription);
Expand Down
2 changes: 1 addition & 1 deletion cypress/utils/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Cypress.Commands.overwrite('visit', (orig, url, options) => {
}

if (waitForGetTenant) {
cy.intercept('GET', '/api/v1/multitenancy/tenant').as('getTenant');
cy.intercept('GET', '/api/v1/multitenancy/tenant*').as('getTenant');
orig(url, newOptions);
supressNoRequestOccurred();
cy.wait('@getTenant');
Expand Down
3 changes: 3 additions & 0 deletions cypress/utils/dashboards/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { BASE_PATH } from '../base_constants';

// STACK MANAGEMENT PATH
export const STACK_MANAGEMENT_PATH = BASE_PATH + '/app/management';
export const SECURITY_PLUGIN_PATH =
BASE_PATH + '/app/security-dashboards-plugin#/';

export const TENANTS_MANAGE_PATH =
BASE_PATH + '/app/security-dashboards-plugin#/tenants';

Expand Down
16 changes: 8 additions & 8 deletions cypress/utils/plugins/security/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
Cypress.Commands.add(
'mockAuthAction',
function (fixtureFileName, funcMockedOn) {
cy.intercept(SEC_API_CONFIG_PATH, {
cy.intercept(`${SEC_API_CONFIG_PATH}*`, {
fixture: fixtureFileName,
}).as('getAuthDetails');

Expand All @@ -35,7 +35,7 @@ Cypress.Commands.add(
Cypress.Commands.add(
'mockRolesAction',
function (fixtureFileName, funcMockedOn) {
cy.intercept(SEC_API_ROLES_PATH, {
cy.intercept(`${SEC_API_ROLES_PATH}*`, {
fixture: fixtureFileName,
}).as('getRoleDetails');

Expand All @@ -48,7 +48,7 @@ Cypress.Commands.add(
Cypress.Commands.add(
'mockInternalUsersAction',
function (fixtureFileName, funcMockedOn) {
cy.intercept(SEC_API_INTERNAL_ACCOUNTS_PATH, {
cy.intercept(`${SEC_API_INTERNAL_ACCOUNTS_PATH}*`, {
fixture: fixtureFileName,
}).as('getInternalUsersDetails');

Expand All @@ -61,7 +61,7 @@ Cypress.Commands.add(
Cypress.Commands.add(
'mockPermissionsAction',
function (fixtureFileName, funcMockedOn) {
cy.intercept(SEC_API_ACTIONGROUPS_PATH, {
cy.intercept(`${SEC_API_ACTIONGROUPS_PATH}*`, {
fixture: fixtureFileName,
}).as('getPermissions');

Expand All @@ -74,7 +74,7 @@ Cypress.Commands.add(
Cypress.Commands.add(
'mockTenantsAction',
function (fixtureFileName, funcMockedOn) {
cy.intercept(SEC_API_TENANTS_PATH, {
cy.intercept(`${SEC_API_TENANTS_PATH}*`, {
fixture: fixtureFileName,
}).as('getTenants');

Expand All @@ -87,7 +87,7 @@ Cypress.Commands.add(
Cypress.Commands.add(
'mockAuditLogsAction',
function (fixtureFileName, funcMockedOn) {
cy.intercept(SEC_API_AUDIT_PATH, {
cy.intercept(`${SEC_API_AUDIT_PATH}*`, {
fixture: fixtureFileName,
}).as('getAuditInfo');

Expand All @@ -101,7 +101,7 @@ Cypress.Commands.add(
'mockAuditConfigUpdateAction',
function (fixtureFileName, funcMockedOn) {
cy.intercept(
{ method: 'POST', url: SEC_API_AUDIT_CONFIG_PATH },
{ method: 'POST', url: `${SEC_API_AUDIT_CONFIG_PATH}*` },
{
fixture: fixtureFileName,
}
Expand All @@ -117,7 +117,7 @@ Cypress.Commands.add(
'mockCachePurgeAction',
function (fixtureFileName, funcMockedOn) {
cy.intercept(
{ method: 'DELETE', url: SEC_API_CACHE_PURGE_PATH },
{ method: 'DELETE', url: `${SEC_API_CACHE_PURGE_PATH}*` },
{
fixture: fixtureFileName,
}
Expand Down

0 comments on commit ed49fa5

Please sign in to comment.