[Monitoring] Ensure we use the monitoring cluster for retrieving xpack info (#59075) (#59309)

* Ensure we use the monitoring cluster for retrieving xpack info

* Remove unnecessary code
This commit is contained in:
Chris Roberson 2020-03-04 11:13:28 -05:00 committed by GitHub
parent 3dc29793d5
commit 4f672d5d4f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 42 additions and 28 deletions

View file

@ -25,6 +25,7 @@ export function exposeClient({ elasticsearchConfig, events, log, elasticsearchPl
events.on('stop', bindKey(cluster, 'close'));
const configSource = isMonitoringCluster ? 'monitoring' : 'production';
log([LOGGING_TAG, 'es-client'], `config sourced from: ${configSource} cluster`);
return cluster;
}
export function hasMonitoringCluster(config) {

View file

@ -7,15 +7,26 @@
import { checkLicenseGenerator } from './cluster_alerts/check_license';
import { hasMonitoringCluster } from './es_client/instantiate_client';
import { LOGGING_TAG } from '../common/constants';
import { XPackInfo } from '../../xpack_main/server/lib/xpack_info';
/*
* Expose xpackInfo for the Monitoring cluster as server.plugins.monitoring.info
*/
export const initMonitoringXpackInfo = async ({ config, xpackMainPlugin, expose, log }) => {
export const initMonitoringXpackInfo = async ({
config,
server,
client,
xpackMainPlugin,
licensing,
expose,
log,
}) => {
const xpackInfo = hasMonitoringCluster(config)
? xpackMainPlugin.createXPackInfo({
clusterSource: 'monitoring',
pollFrequencyInMillis: config.get('xpack.monitoring.xpack_api_polling_frequency_millis'),
? new XPackInfo(server, {
licensing: licensing.createLicensePoller(
client,
config.get('xpack.monitoring.xpack_api_polling_frequency_millis')
),
})
: xpackMainPlugin.info;

View file

@ -38,19 +38,29 @@ export async function verifyMonitoringAuth(req) {
async function verifyHasPrivileges(req) {
const { callWithRequest } = req.server.plugins.elasticsearch.getCluster('monitoring');
const response = await callWithRequest(req, 'transport.request', {
method: 'POST',
path: '/_security/user/_has_privileges',
body: {
index: [
{
names: [INDEX_PATTERN], // uses wildcard
privileges: ['read'],
},
],
},
ignoreUnavailable: true, // we allow 404 incase the user shutdown security in-between the check and now
});
let response;
try {
response = await callWithRequest(req, 'transport.request', {
method: 'POST',
path: '/_security/user/_has_privileges',
body: {
index: [
{
names: [INDEX_PATTERN], // uses wildcard
privileges: ['read'],
},
],
},
ignoreUnavailable: true, // we allow 404 incase the user shutdown security in-between the check and now
});
} catch (err) {
if (
err.message === 'no handler found for uri [/_security/user/_has_privileges] and method [POST]'
) {
return;
}
throw err;
}
// we assume true because, if the response 404ed, then it will not exist but we should try to continue
const hasAllRequestedPrivileges = get(response, 'has_all_requested', true);

View file

@ -51,7 +51,7 @@ export class Plugin {
const uiEnabled = config.get('xpack.monitoring.ui.enabled');
if (uiEnabled) {
await instantiateClient({
const client = await instantiateClient({
log: core.log,
events: core.events,
elasticsearchConfig,
@ -59,6 +59,8 @@ export class Plugin {
}); // Instantiate the dedicated ES client
await initMonitoringXpackInfo({
config,
server: core._hapi,
client,
log: core.log,
xpackMainPlugin: plugins.xpack_main,
expose: core.expose,

View file

@ -19,15 +19,6 @@ export function setupXPackMain(server) {
const info = new XPackInfo(server, { licensing: server.newPlatform.setup.plugins.licensing });
server.expose('info', info);
server.expose('createXPackInfo', options => {
const client = server.newPlatform.setup.core.elasticsearch.createClient(options.clusterSource);
const monitoringLicensing = server.newPlatform.setup.plugins.licensing.createLicensePoller(
client,
options.pollFrequencyInMillis
);
return new XPackInfo(server, { licensing: monitoringLicensing });
});
server.ext('onPreResponse', (request, h) => injectXPackInfoSignature(info, request, h));

View file

@ -11,7 +11,6 @@ export { XPackFeature } from './lib/xpack_info';
export interface XPackMainPlugin {
info: XPackInfo;
createXPackInfo(options: XPackInfoOptions): XPackInfo;
getFeatures(): Feature[];
registerFeature(feature: FeatureWithAllOrReadPrivileges): void;
}