Skip to content
Closed
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
2 changes: 2 additions & 0 deletions x-pack/plugins/beats/server/routes/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { registerUpdateBeatRoute } from './register_update_beat_route';
import { registerSetTagRoute } from './register_set_tag_route';
import { registerAssignTagsToBeatsRoute } from './register_assign_tags_to_beats_route';
import { registerRemoveTagsFromBeatsRoute } from './register_remove_tags_from_beats_route';
import { registerGetBeatConfigurationRoute } from './register_get_beat_configuration_route';

export function registerApiRoutes(server) {
registerCreateEnrollmentTokensRoute(server);
Expand All @@ -22,4 +23,5 @@ export function registerApiRoutes(server) {
registerSetTagRoute(server);
registerAssignTagsToBeatsRoute(server);
registerRemoveTagsFromBeatsRoute(server);
registerGetBeatConfigurationRoute(server);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import Joi from 'joi';
import { get } from "lodash";
import { INDEX_NAMES } from "../../../common/constants";
import { callWithInternalUserFactory } from '../../lib/client';
import { wrapEsError } from "../../lib/error_wrappers";

async function getBeat(callWithInternalUser, beatId) {
const params = {
index: INDEX_NAMES.BEATS,
type: '_doc',
id: `beat:${beatId}`,
ignore: [ 404 ]
};

const response = await callWithInternalUser('get', params);
if (!response.found) {
return null;
}

return get(response, '_source.beat');
}

// TODO: add license check pre-hook
export function registerGetBeatConfigurationRoute(server) {
server.route({
method: 'GET',
path: '/api/beats/agent/{beatId}/configuration',
config: {
validate: {
headers: Joi.object({
'kbn-beats-access-token': Joi.string().required()
}).options({ allowUnknown: true })
},
auth: false
},
handler: async (request, reply) => {
const callWithInternalUser = callWithInternalUserFactory(server);
const beatId = request.params.beatId;
const accessToken = request.headers['kbn-beats-access-token'];

// TODO: remove conditional and hardcoding
if (beatId !== 'foo') { // foo is used by the API integration tests
return reply({
configuration_blocks: [
{
type: "output",
data: "elasticsearch:\n hosts: [\"localhost:9200\"]\n username: \"...\""
},
{
type: "metricbeat.modules",
data: "module: memcached\nhosts: [\"localhost:11211\"]",
},
{
type: "metricbeat.modules",
data: "module: munin\nhosts: [\"localhost:4949\"]\nnode.namespace: node",
}
]
});
}

let beat;
try {
beat = await getBeat(callWithInternalUser, beatId);
} catch (err) {
return reply(wrapEsError(err));
}

if (beat === null) {
return reply({ message: 'Beat not found' }).code(404);
}

const isAccessTokenValid = beat.access_token === accessToken;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!isAccessTokenValid) {
return reply({ message: 'Invalid access token' }).code(401);
}

const isBeatVerified = beat.hasOwnProperty('verified_on');
if (!isBeatVerified) {
return reply({ message: 'Beat has not been verified' }).code(400);
}

reply({ configuration_blocks: beat.central_configuration_blocks });
}
});
}
42 changes: 42 additions & 0 deletions x-pack/test/api_integration/apis/beats/get_beat_configuration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import expect from 'expect.js';

export default function ({ getService }) {
const supertest = getService('supertest');
const esArchiver = getService('esArchiver');

describe('get_beat_configuration', () => {
const archive = 'beats/list';

beforeEach('load beats archive', () => esArchiver.load(archive));
afterEach('unload beats archive', () => esArchiver.unload(archive));

it('should return merged configuration for the beat', async () => {
const { body: apiResponse } = await supertest
.get(
'/api/beats/agent/foo/configuration'
)
.set('kbn-beats-access-token', '93c4a4dd08564c189a7ec4e4f046b975')
.expect(200);

const configurationBlocks = apiResponse.configuration_blocks;

expect(configurationBlocks).to.be.an(Array);
expect(configurationBlocks.length).to.be(3);

expect(configurationBlocks[0].type).to.be('output');
expect(configurationBlocks[0].data).to.be('elasticsearch:\n hosts: ["localhost:9200"]\n username: ...');

expect(configurationBlocks[1].type).to.be('metricbeat.modules');
expect(configurationBlocks[1].data).to.be('module: memcached\nhosts: ["localhost:11211"]');

expect(configurationBlocks[2].type).to.be('metricbeat.modules');
expect(configurationBlocks[2].data).to.be('module: munin\nhosts: ["localhost:4949"]\nnode.namespace: node');
});
});
}
1 change: 1 addition & 0 deletions x-pack/test/api_integration/apis/beats/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ export default function ({ getService, loadTestFile }) {
loadTestFile(require.resolve('./set_tag'));
loadTestFile(require.resolve('./assign_tags_to_beats'));
loadTestFile(require.resolve('./remove_tags_from_beats'));
loadTestFile(require.resolve('./get_beat_configuration'));
});
}