Skip to content

Commit

Permalink
[Monitoring] Ensure we use the monitoring cluster for retrieving xpac…
Browse files Browse the repository at this point in the history
…k info (elastic#59075) (elastic#59309)

* Ensure we use the monitoring cluster for retrieving xpack info

* Remove unnecessary code
  • Loading branch information
chrisronline authored Mar 4, 2020
1 parent 3dc2979 commit 4f672d5
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion x-pack/legacy/plugins/monitoring/server/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,16 @@ 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,
elasticsearchPlugin: plugins.elasticsearch,
}); // Instantiate the dedicated ES client
await initMonitoringXpackInfo({
config,
server: core._hapi,
client,
log: core.log,
xpackMainPlugin: plugins.xpack_main,
expose: core.expose,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
1 change: 0 additions & 1 deletion x-pack/legacy/plugins/xpack_main/server/xpack_main.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 4f672d5

Please sign in to comment.