From 2cf95b1a9454704487d8205e97845f6b2139985f Mon Sep 17 00:00:00 2001 From: David Cui <53581635+davidcui-amzn@users.noreply.github.com> Date: Wed, 24 Mar 2021 09:51:30 -0700 Subject: [PATCH] add security enable support for gantt charts (#16) --- gantt-chart/.cypress/integration/ui.spec.js | 8 ++-- gantt-chart/.cypress/support/commands.js | 41 +++++++++++++++++++++ gantt-chart/.cypress/support/constants.js | 19 ++++++++++ gantt-chart/.cypress/support/index.js | 5 +++ gantt-chart/cypress.json | 8 +++- 5 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 gantt-chart/.cypress/support/constants.js diff --git a/gantt-chart/.cypress/integration/ui.spec.js b/gantt-chart/.cypress/integration/ui.spec.js index 5a72d77..c0148dd 100644 --- a/gantt-chart/.cypress/integration/ui.spec.js +++ b/gantt-chart/.cypress/integration/ui.spec.js @@ -19,7 +19,7 @@ import { delay, GANTT_VIS_NAME, Y_LABEL, X_LABEL, DEFAULT_SIZE } from '../utils/ describe('Save a gantt chart', () => { beforeEach(() => { - cy.visit('app/visualize#'); + cy.visit(`${Cypress.env('kibana')}/app/visualize#`); cy.wait(delay * 5); }); @@ -45,7 +45,7 @@ describe('Save a gantt chart', () => { describe('Render and configure a gantt chart', () => { beforeEach(() => { - cy.visit('app/visualize#'); + cy.visit(`${Cypress.env('kibana')}/app/visualize#`); cy.wait(delay * 5); cy.get('button').contains(GANTT_VIS_NAME).click({ force: true }); cy.wait(delay * 5); @@ -88,7 +88,7 @@ describe('Render and configure a gantt chart', () => { describe('Configure panel settings', () => { beforeEach(() => { - cy.visit('app/visualize#'); + cy.visit(`${Cypress.env('kibana')}/app/visualize#`); cy.wait(delay * 5); cy.get('button').contains(GANTT_VIS_NAME).click({ force: true }); cy.wait(delay * 5); @@ -176,7 +176,7 @@ describe('Configure panel settings', () => { describe('Add gantt chart to dashboard', () => { it('Adds gantt chart to dashboard', () => { - cy.visit('app/dashboards#/create'); + cy.visit(`${Cypress.env('kibana')}/app/dashboards#/create`); cy.wait(delay * 5); cy.get('.euiLink').contains('Add an existing').click({ force: true }); diff --git a/gantt-chart/.cypress/support/commands.js b/gantt-chart/.cypress/support/commands.js index 3d72c48..b4c0a2d 100644 --- a/gantt-chart/.cypress/support/commands.js +++ b/gantt-chart/.cypress/support/commands.js @@ -13,6 +13,8 @@ * permissions and limitations under the License. */ +const { ADMIN_AUTH } = require('./constants'); + // *********************************************** // This example commands.js shows you how to // create various custom commands and overwrite @@ -38,3 +40,42 @@ // // -- This will overwrite an existing command -- // Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... }) + +Cypress.Commands.overwrite('visit', (originalFn, url, options) => { + // Add the basic auth header when security enabled in the Elasticsearch 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 Kibana 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 Elasticsearch 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)); +}); \ No newline at end of file diff --git a/gantt-chart/.cypress/support/constants.js b/gantt-chart/.cypress/support/constants.js new file mode 100644 index 0000000..874818c --- /dev/null +++ b/gantt-chart/.cypress/support/constants.js @@ -0,0 +1,19 @@ +/* + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * 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. + */ + +export const ADMIN_AUTH = { + username: 'admin', + password: 'admin', +}; \ No newline at end of file diff --git a/gantt-chart/.cypress/support/index.js b/gantt-chart/.cypress/support/index.js index 103f798..95f4049 100644 --- a/gantt-chart/.cypress/support/index.js +++ b/gantt-chart/.cypress/support/index.js @@ -33,3 +33,8 @@ import './commands' // Alternatively you can use CommonJS syntax: // require('./commands') + +// Switch the base URL of Elasticsearch when security enabled in the cluster +if (Cypress.env('security_enabled')) { + Cypress.env('elasticsearch', 'https://localhost:9200'); +} \ No newline at end of file diff --git a/gantt-chart/cypress.json b/gantt-chart/cypress.json index 6575322..cefe648 100644 --- a/gantt-chart/cypress.json +++ b/gantt-chart/cypress.json @@ -1,5 +1,4 @@ { - "baseUrl": "http://localhost:5601", "video": true, "fixturesFolder": ".cypress/fixtures", "integrationFolder": ".cypress/integration", @@ -11,5 +10,10 @@ "viewportHeight": 1600, "requestTimeout": 60000, "responseTimeout": 60000, - "defaultCommandTimeout": 60000 + "defaultCommandTimeout": 60000, + "env": { + "elasticsearch": "localhost:9200", + "kibana": "localhost:5601", + "security_enabled": true + } } \ No newline at end of file