-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[Beats Management] APIs: Get beat configuration #19195
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
Closed
ycombinator
wants to merge
16
commits into
elastic:feature/x-pack/management/beats
from
ycombinator:x-pack/management/beats/apis/get-beat-configuration
Closed
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
a013fb9
[Beats Management] APIs: Create enrollment tokens (#19018)
ycombinator 52f20a1
WIP checkin
ycombinator bb2155e
Add API integration test
ycombinator e8d8894
Converting to Jest test
ycombinator ee3850b
WIP checkin
ycombinator e9bb40d
Fixing API for default case + adding test for it
ycombinator 8e90206
Using a single index
ycombinator 06c686d
Implementing GET /api/beats/agents API
ycombinator 5aac44f
Updating mapping
ycombinator 130178b
Creating POST /api/beats/agents/verify API
ycombinator b7d279b
Refactoring: extracting out helper functions
ycombinator efe1f07
Expanding TODO note so I won't forget :)
ycombinator 1081c52
Fixing file name
ycombinator ff9d22b
Fixing minor typo in TODO comment
ycombinator 8227550
WIP checkin: GET /api/beats/agent/{beat ID}/configuration API
ycombinator ae66814
Fixing changes messed up in rebase
ycombinator File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
x-pack/plugins/beats/server/routes/api/register_get_beat_configuration_route.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| 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
42
x-pack/test/api_integration/apis/beats/get_beat_configuration.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'); | ||
| }); | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to use https://github.com/elastic/kibana/blob/feature/x-pack/management/beats/x-pack/plugins/beats/server/lib/crypto/are_tokens_equal.js#L12.