Skip to content

Commit

Permalink
[Monitoring] Ensure cloud cannot see setup mode (elastic#49223)
Browse files Browse the repository at this point in the history
* Ensure cloud cannot see setup mode

* Remove cloud check from collection status, as it's an injected var now

* Man these tests suck
  • Loading branch information
chrisronline authored Nov 4, 2019
1 parent 6cd624f commit f48497b
Show file tree
Hide file tree
Showing 15 changed files with 71 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React, { useState } from 'react';
import React, { Fragment, useState } from 'react';
import PropTypes from 'prop-types';
import {
EuiSpacer,
Expand Down Expand Up @@ -43,7 +43,7 @@ function NoDataMessage(props) {

export function NoData(props) {
const [isLoading, setIsLoading] = useState(false);
const [useInternalCollection, setUseInternalCollection] = useState(false);
const [useInternalCollection, setUseInternalCollection] = useState(props.isOnCloud);

async function startSetup() {
setIsLoading(true);
Expand All @@ -64,15 +64,19 @@ export function NoData(props) {
<EuiSpacer size="m" />
<NoDataMessage {...props} />
<CheckerErrors errors={props.errors} />
<EuiHorizontalRule size="half" />
<EuiButtonEmpty isDisabled={props.isCollectionEnabledUpdated} onClick={() => setUseInternalCollection(false)}>
<EuiTextColor color="default">
<FormattedMessage
id="xpack.monitoring.noData.setupMetricbeatInstead"
defaultMessage="Or, set up with Metricbeat (recommended)"
/>
</EuiTextColor>
</EuiButtonEmpty>
{ !props.isOnCloud ? (
<Fragment>
<EuiHorizontalRule size="half" />
<EuiButtonEmpty isDisabled={props.isCollectionEnabledUpdated} onClick={() => setUseInternalCollection(false)}>
<EuiTextColor color="default">
<FormattedMessage
id="xpack.monitoring.noData.setupMetricbeatInstead"
defaultMessage="Or, set up with Metricbeat (recommended)"
/>
</EuiTextColor>
</EuiButtonEmpty>
</Fragment>
) : null }
</EuiPageContent>
</EuiPageBody>
</EuiPage>
Expand Down
17 changes: 10 additions & 7 deletions x-pack/legacy/plugins/monitoring/public/lib/setup_mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { ajaxErrorHandlersProvider } from './ajax_error_handler';
import { get, contains } from 'lodash';
import chrome from 'ui/chrome';
import { i18n } from '@kbn/i18n';

function isOnPage(hash) {
return contains(window.location.hash, hash);
Expand Down Expand Up @@ -80,7 +81,7 @@ export const updateSetupModeData = async (uuid, fetchWithoutClusterUuid = false)
const oldData = setupModeState.data;
const data = await fetchCollectionData(uuid, fetchWithoutClusterUuid);
setupModeState.data = data;
if (get(data, '_meta.isOnCloud', false)) {
if (chrome.getInjected('isOnCloud')) {
return toggleSetupMode(false); // eslint-disable-line no-use-before-define
}
notifySetupModeDataChange(oldData);
Expand Down Expand Up @@ -139,15 +140,17 @@ export const setSetupModeMenuItem = () => {
}

const globalState = angularState.injector.get('globalState');
const navItems = globalState.inSetupMode
? []
: [{
const navItems = [];
if (!globalState.inSetupMode && !chrome.getInjected('isOnCloud')) {
navItems.push({
id: 'enter',
label: 'Enter Setup Mode',
description: 'Enter setup',
label: i18n.translate('xpack.monitoring.setupMode.enter', {
defaultMessage: 'Enter Setup Mode'
}),
run: () => toggleSetupMode(true),
testId: 'enterSetupMode'
}];
});
}

angularState.scope.topNavMenu = [...navItems];
// LOL angular
Expand Down
55 changes: 36 additions & 19 deletions x-pack/legacy/plugins/monitoring/public/lib/setup_mode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
* you may not use this file except in compliance with the Elastic License.
*/

import {
toggleSetupMode,
initSetupModeState,
getSetupModeState,
updateSetupModeData,
setSetupModeMenuItem
} from './setup_mode';
let toggleSetupMode;
let initSetupModeState;
let getSetupModeState;
let updateSetupModeData;
let setSetupModeMenuItem;

jest.mock('./ajax_error_handler', () => ({
ajaxErrorHandlersProvider: err => {
Expand Down Expand Up @@ -52,16 +50,31 @@ function waitForSetupModeData(action) {
process.nextTick(action);
}

function setModules() {
jest.resetModules();
injectorModulesMock.globalState.inSetupMode = false;

const setupMode = require('./setup_mode');
toggleSetupMode = setupMode.toggleSetupMode;
initSetupModeState = setupMode.initSetupModeState;
getSetupModeState = setupMode.getSetupModeState;
updateSetupModeData = setupMode.updateSetupModeData;
setSetupModeMenuItem = setupMode.setSetupModeMenuItem;
}

describe('setup_mode', () => {
describe('setup', () => {
afterEach(async () => {
try {
toggleSetupMode(false);
} catch (err) {
// Do nothing...
beforeEach(async () => {
jest.doMock('ui/chrome', () => ({
getInjected: (key) => {
if (key === 'isOnCloud') {
return false;
}
}
});
}));
setModules();
});

describe('setup', () => {
it('should require angular state', async () => {
let error;
try {
Expand Down Expand Up @@ -99,21 +112,25 @@ describe('setup_mode', () => {
describe('in setup mode', () => {
afterEach(async () => {
data = {};
toggleSetupMode(false);
});

it('should enable it through clicking top nav item', async () => {
initSetupModeState(angularStateMock.scope, angularStateMock.injector);
setSetupModeMenuItem();
expect(injectorModulesMock.globalState.inSetupMode).toBe(false);
await angularStateMock.scope.topNavMenu[0].run();
expect(injectorModulesMock.globalState.inSetupMode).toBe(true);
});

it('should not fetch data if on cloud', async (done) => {
data = {
_meta: {
isOnCloud: true
jest.doMock('ui/chrome', () => ({
getInjected: (key) => {
if (key === 'isOnCloud') {
return true;
}
}
};
}));
setModules();
initSetupModeState(angularStateMock.scope, angularStateMock.injector);
await toggleSetupMode(true);
waitForSetupModeData(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import React from 'react';
import chrome from 'ui/chrome';
import {
ClusterSettingsChecker,
NodeSettingsChecker,
Expand Down Expand Up @@ -99,7 +100,12 @@ export class NoDataController extends MonitoringViewBaseController {

this.renderReact(
<I18nContext>
<NoData {...props} enabler={enabler} changePath={this.changePath} />
<NoData
{...props}
enabler={enabler}
changePath={this.changePath}
isOnCloud={chrome.getInjected('isOnCloud')}
/>
</I18nContext>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,6 @@ export const getCollectionStatus = async (req, indexPatterns, clusterUuid, nodeU
status._meta = {
secondsAgo: NUMBER_OF_SECONDS_AGO_TO_LOOK,
liveClusterUuid,
isOnCloud: get(req.server.plugins, 'cloud.config.isCloudEnabled', false)
};

return status;
Expand Down
4 changes: 3 additions & 1 deletion x-pack/legacy/plugins/monitoring/ui_exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import { i18n } from '@kbn/i18n';
import { get } from 'lodash';
import { resolve } from 'path';

/**
Expand All @@ -28,7 +29,8 @@ export const getUiExports = () => ({
injectDefaultVars(server) {
const config = server.config();
return {
monitoringUiEnabled: config.get('xpack.monitoring.ui.enabled')
monitoringUiEnabled: config.get('xpack.monitoring.ui.enabled'),
isOnCloud: get(server.plugins, 'cloud.config.isCloudEnabled', false)
};
},
hacks: [ 'plugins/monitoring/hacks/toggle_app_link_in_nav' ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
},
"_meta": {
"secondsAgo": 30,
"isOnCloud": false,
"liveClusterUuid": null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
},
"_meta": {
"secondsAgo": 30,
"isOnCloud": false,
"liveClusterUuid": null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
},
"_meta": {
"secondsAgo": 30,
"isOnCloud": false,
"liveClusterUuid": null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
},
"_meta": {
"secondsAgo": 30,
"isOnCloud": false,
"liveClusterUuid": null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
},
"_meta": {
"secondsAgo": 30,
"isOnCloud": false,
"liveClusterUuid": null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
},
"_meta": {
"secondsAgo": 30,
"isOnCloud": false,
"liveClusterUuid": null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
},
"_meta": {
"secondsAgo": 30,
"isOnCloud": false,
"liveClusterUuid": null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
},
"_meta": {
"secondsAgo": 30,
"isOnCloud": false,
"liveClusterUuid": null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
},
"_meta": {
"secondsAgo": 30,
"isOnCloud": false,
"liveClusterUuid": null
}
}

0 comments on commit f48497b

Please sign in to comment.