Skip to content

Commit

Permalink
[Monitoring] Handle setup mode if security is disabled (#53306) (#58772)
Browse files Browse the repository at this point in the history
* Handle setup mode if security is disabled

* Rename so the test actually runs, and fix/add tests

* Use es.js api instead of transport.request

* Revert "Use es.js api instead of transport.request"

This reverts commit ae0e48f.

* Explicitly handle security not enabled

Co-authored-by: Elastic Machine <[email protected]>

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
chrisronline and elasticmachine authored Feb 28, 2020
1 parent 0479d0d commit 535114d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

import expect from '@kbn/expect';
import sinon from 'sinon';
import { getCollectionStatus } from '../';
import { getCollectionStatus } from '..';
import { getIndexPatterns } from '../../../cluster/get_index_patterns';

const liveClusterUuid = 'a12';
const mockReq = (searchResult = {}) => {
const mockReq = (searchResult = {}, securityEnabled = true, userHasPermissions = true) => {
return {
server: {
newPlatform: {
Expand Down Expand Up @@ -40,6 +40,14 @@ const mockReq = (searchResult = {}) => {
},
},
plugins: {
xpack_main: {
info: {
isAvailable: () => true,
feature: () => ({
isEnabled: () => securityEnabled,
}),
},
},
elasticsearch: {
getCluster() {
return {
Expand All @@ -51,6 +59,13 @@ const mockReq = (searchResult = {}) => {
) {
return Promise.resolve({ cluster_uuid: liveClusterUuid });
}
if (
type === 'transport.request' &&
params &&
params.path === '/_security/user/_has_privileges'
) {
return Promise.resolve({ has_all_requested: userHasPermissions });
}
if (type === 'transport.request' && params && params.path === '/_nodes') {
return Promise.resolve({ nodes: {} });
}
Expand Down Expand Up @@ -218,24 +233,24 @@ describe('getCollectionStatus', () => {
});

it('should detect products based on other indices', async () => {
const req = mockReq(
{},
{
responses: [
{ hits: { total: { value: 1 } } },
{ hits: { total: { value: 1 } } },
{ hits: { total: { value: 1 } } },
{ hits: { total: { value: 1 } } },
{ hits: { total: { value: 1 } } },
],
}
);

const req = mockReq({ hits: { total: { value: 1 } } });
const result = await getCollectionStatus(req, getIndexPatterns(req.server), liveClusterUuid);

expect(result.kibana.detected.doesExist).to.be(true);
expect(result.elasticsearch.detected.doesExist).to.be(true);
expect(result.beats.detected.mightExist).to.be(true);
expect(result.logstash.detected.mightExist).to.be(true);
});

it('should work properly when security is disabled', async () => {
const req = mockReq({ hits: { total: { value: 1 } } }, false);
const result = await getCollectionStatus(req, getIndexPatterns(req.server), liveClusterUuid);
expect(result.kibana.detected.doesExist).to.be(true);
});

it('should not work if the user does not have the necessary permissions', async () => {
const req = mockReq({ hits: { total: { value: 1 } } }, true, false);
const result = await getCollectionStatus(req, getIndexPatterns(req.server), liveClusterUuid);
expect(result._meta.hasPermissions).to.be(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,25 @@ function isBeatFromAPM(bucket) {
}

async function hasNecessaryPermissions(req) {
const { callWithRequest } = req.server.plugins.elasticsearch.getCluster('data');
const response = await callWithRequest(req, 'transport.request', {
method: 'POST',
path: '/_security/user/_has_privileges',
body: {
cluster: ['monitor'],
},
});
// If there is some problem, assume they do not have access
return get(response, 'has_all_requested', false);
try {
const { callWithRequest } = req.server.plugins.elasticsearch.getCluster('data');
const response = await callWithRequest(req, 'transport.request', {
method: 'POST',
path: '/_security/user/_has_privileges',
body: {
cluster: ['monitor'],
},
});
// If there is some problem, assume they do not have access
return get(response, 'has_all_requested', false);
} catch (err) {
if (
err.message === 'no handler found for uri [/_security/user/_has_privileges] and method [POST]'
) {
return true;
}
return false;
}
}

/**
Expand Down

0 comments on commit 535114d

Please sign in to comment.