Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Monitoring] Check for security feature first when entering setup mode #73821

Merged
merged 3 commits into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import { getCollectionStatus } from '..';
import { getIndexPatterns } from '../../../cluster/get_index_patterns';

const liveClusterUuid = 'a12';
const mockReq = (searchResult = {}, securityEnabled = true, userHasPermissions = true) => {
const mockReq = (
searchResult = {},
securityEnabled = true,
userHasPermissions = true,
securityErrorMessage = null
) => {
return {
server: {
newPlatform: {
Expand All @@ -37,12 +42,14 @@ const mockReq = (searchResult = {}, securityEnabled = true, userHasPermissions =
},
},
plugins: {
xpack_main: {
monitoring: {
info: {
isAvailable: () => true,
feature: () => ({
isEnabled: () => securityEnabled,
}),
getSecurityFeature: () => {
return {
isAvailable: securityEnabled,
isEnabled: securityEnabled,
};
},
},
},
elasticsearch: {
Expand All @@ -61,6 +68,11 @@ const mockReq = (searchResult = {}, securityEnabled = true, userHasPermissions =
params &&
params.path === '/_security/user/_has_privileges'
) {
if (securityErrorMessage !== null) {
return Promise.reject({
message: securityErrorMessage,
});
}
return Promise.resolve({ has_all_requested: userHasPermissions });
}
if (type === 'transport.request' && params && params.path === '/_nodes') {
Expand Down Expand Up @@ -245,6 +257,34 @@ describe('getCollectionStatus', () => {
expect(result.kibana.detected.doesExist).to.be(true);
});

it('should work properly with an unknown security message', async () => {
const req = mockReq({ hits: { total: { value: 1 } } }, true, true, 'foobar');
const result = await getCollectionStatus(req, getIndexPatterns(req.server), liveClusterUuid);
expect(result._meta.hasPermissions).to.be(false);
});

it('should work properly with a known security message', async () => {
const req = mockReq(
{ hits: { total: { value: 1 } } },
true,
true,
'no handler found for uri [/_security/user/_has_privileges] and method [POST]'
);
const result = await getCollectionStatus(req, getIndexPatterns(req.server), liveClusterUuid);
expect(result.kibana.detected.doesExist).to.be(true);
});

it('should work properly with another known security message', async () => {
const req = mockReq(
{ hits: { total: { value: 1 } } },
true,
true,
'Invalid index name [_security]'
);
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ function isBeatFromAPM(bucket) {
}

async function hasNecessaryPermissions(req) {
const securityFeature = req.server.plugins.monitoring.info.getSecurityFeature();
if (!securityFeature.isAvailable || !securityFeature.isEnabled) {
return true;
}
try {
const { callWithRequest } = req.server.plugins.elasticsearch.getCluster('data');
const response = await callWithRequest(req, 'transport.request', {
Expand All @@ -250,6 +254,9 @@ async function hasNecessaryPermissions(req) {
) {
return true;
}
if (err.message.includes('Invalid index name [_security]')) {
return true;
}
return false;
}
}
Expand Down