-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Beats Management] APIs: Verify beats #19103
Merged
ycombinator
merged 17 commits into
elastic:feature/x-pack/management/beats
from
ycombinator:x-pack/management/beats/apis/verify-beats
May 17, 2018
+227
−0
Merged
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
21be31a
WIP checkin
ycombinator af197f8
Add API integration test
ycombinator e687505
Converting to Jest test
ycombinator e4d7b0a
WIP checkin
ycombinator 26be0bc
Fixing API for default case + adding test for it
ycombinator 1e87ac1
Fixing copy pasta typos
ycombinator d192174
Fixing variable name
ycombinator 6ac7202
Using a single index
ycombinator a865a9d
Implementing GET /api/beats/agents API
ycombinator 76dc950
Updating mapping
ycombinator 475f868
Creating POST /api/beats/agents/verify API
ycombinator 038a7d0
Refactoring: extracting out helper functions
ycombinator c558d3b
Fleshing out remaining tests
ycombinator 02ee113
Expanding TODO note so I won't forget :)
ycombinator 7b297c7
Fixing file name
ycombinator 77e5b7c
Moving TODO comment to right file
ycombinator 774ea75
Rename determine* helper functions to find*
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 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
143 changes: 143 additions & 0 deletions
143
x-pack/plugins/beats/server/routes/api/register_verify_beats_route.js
This file contains 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,143 @@ | ||
/* | ||
* 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 moment from 'moment'; | ||
import { | ||
get, | ||
flatten | ||
} from 'lodash'; | ||
import { INDEX_NAMES } from '../../../common/constants'; | ||
import { callWithRequestFactory } from '../../lib/client'; | ||
import { wrapEsError } from '../../lib/error_wrappers'; | ||
|
||
async function getBeats(callWithRequest, beatIds) { | ||
const ids = beatIds.map(beatId => `beat:${beatId}`); | ||
const params = { | ||
index: INDEX_NAMES.BEATS, | ||
type: '_doc', | ||
body: { ids }, | ||
_sourceInclude: [ 'beat.id', 'beat.verified_on' ] | ||
}; | ||
|
||
const response = await callWithRequest('mget', params); | ||
return get(response, 'docs', []); | ||
} | ||
|
||
async function verifyBeats(callWithRequest, beatIds) { | ||
if (!Array.isArray(beatIds) || (beatIds.length === 0)) { | ||
return []; | ||
} | ||
|
||
const verifiedOn = moment().toJSON(); | ||
const body = flatten(beatIds.map(beatId => [ | ||
{ update: { _id: `beat:${beatId}` } }, | ||
{ doc: { beat: { verified_on: verifiedOn } } } | ||
])); | ||
|
||
const params = { | ||
index: INDEX_NAMES.BEATS, | ||
type: '_doc', | ||
body, | ||
refresh: 'wait_for' | ||
}; | ||
|
||
const response = await callWithRequest('bulk', params); | ||
return get(response, 'items', []); | ||
} | ||
|
||
function determineNonExistentBeatIds(beatsFromEs, beatIdsFromRequest) { | ||
return beatsFromEs.reduce((nonExistentBeatIds, beatFromEs, idx) => { | ||
if (!beatFromEs.found) { | ||
nonExistentBeatIds.push(beatIdsFromRequest[idx]); | ||
} | ||
return nonExistentBeatIds; | ||
}, []); | ||
} | ||
|
||
function determineAlreadyVerifiedBeatIds(beatsFromEs) { | ||
return beatsFromEs | ||
.filter(beat => beat.found) | ||
.filter(beat => beat._source.beat.hasOwnProperty('verified_on')) | ||
.map(beat => beat._source.beat.id); | ||
} | ||
|
||
function determineToBeVerifiedBeatIds(beatsFromEs) { | ||
return beatsFromEs | ||
.filter(beat => beat.found) | ||
.filter(beat => !beat._source.beat.hasOwnProperty('verified_on')) | ||
.map(beat => beat._source.beat.id); | ||
} | ||
|
||
function determineVerifiedBeatIds(verifications, toBeVerifiedBeatIds) { | ||
return verifications.reduce((verifiedBeatIds, verification, idx) => { | ||
if (verification.update.status === 200) { | ||
verifiedBeatIds.push(toBeVerifiedBeatIds[idx]); | ||
} | ||
return verifiedBeatIds; | ||
}, []); | ||
} | ||
|
||
// TODO: add license check pre-hook | ||
// TODO: write to Kibana audit log file (include who did the verification as well) | ||
export function registerVerifyBeatsRoute(server) { | ||
server.route({ | ||
method: 'POST', | ||
path: '/api/beats/agents/verify', | ||
config: { | ||
validate: { | ||
payload: Joi.object({ | ||
beats: Joi.array({ | ||
id: Joi.string().required() | ||
}).min(1) | ||
}).required() | ||
} | ||
}, | ||
handler: async (request, reply) => { | ||
const callWithRequest = callWithRequestFactory(server, request); | ||
|
||
const beats = [...request.payload.beats]; | ||
const beatIds = beats.map(beat => beat.id); | ||
|
||
let nonExistentBeatIds; | ||
let alreadyVerifiedBeatIds; | ||
let verifiedBeatIds; | ||
|
||
try { | ||
const beatsFromEs = await getBeats(callWithRequest, beatIds); | ||
|
||
nonExistentBeatIds = determineNonExistentBeatIds(beatsFromEs, beatIds); | ||
alreadyVerifiedBeatIds = determineAlreadyVerifiedBeatIds(beatsFromEs); | ||
const toBeVerifiedBeatIds = determineToBeVerifiedBeatIds(beatsFromEs); | ||
|
||
const verifications = await verifyBeats(callWithRequest, toBeVerifiedBeatIds); | ||
verifiedBeatIds = determineVerifiedBeatIds(verifications, toBeVerifiedBeatIds); | ||
|
||
} catch (err) { | ||
return reply(wrapEsError(err)); | ||
} | ||
|
||
beats.forEach(beat => { | ||
if (nonExistentBeatIds.includes(beat.id)) { | ||
beat.status = 404; | ||
beat.result = 'not found'; | ||
} else if (alreadyVerifiedBeatIds.includes(beat.id)) { | ||
beat.status = 200; | ||
beat.result = 'already verified'; | ||
} else if (verifiedBeatIds.includes(beat.id)) { | ||
beat.status = 200; | ||
beat.result = 'verified'; | ||
} else { | ||
beat.status = 400; | ||
beat.result = 'not verified'; | ||
} | ||
}); | ||
|
||
const response = { beats }; | ||
reply(response); | ||
} | ||
}); | ||
} |
This file contains 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
This file contains 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,81 @@ | ||
/* | ||
* 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'); | ||
const chance = getService('chance'); | ||
|
||
describe('verify_beats', () => { | ||
const archive = 'beats/list'; | ||
|
||
beforeEach('load beats archive', () => esArchiver.load(archive)); | ||
afterEach('unload beats archive', () => esArchiver.unload(archive)); | ||
|
||
it('verify the given beats', async () => { | ||
const { body: apiResponse } = await supertest | ||
.post( | ||
'/api/beats/agents/verify' | ||
) | ||
.set('kbn-xsrf', 'xxx') | ||
.send({ | ||
beats: [ | ||
{ id: 'bar' }, | ||
{ id: 'baz' } | ||
] | ||
}) | ||
.expect(200); | ||
|
||
expect(apiResponse.beats).to.eql([ | ||
{ id: 'bar', status: 200, result: 'verified' }, | ||
{ id: 'baz', status: 200, result: 'verified' }, | ||
]); | ||
}); | ||
|
||
it('should not re-verify already-verified beats', async () => { | ||
const { body: apiResponse } = await supertest | ||
.post( | ||
'/api/beats/agents/verify' | ||
) | ||
.set('kbn-xsrf', 'xxx') | ||
.send({ | ||
beats: [ | ||
{ id: 'foo' }, | ||
{ id: 'bar' } | ||
] | ||
}) | ||
.expect(200); | ||
|
||
expect(apiResponse.beats).to.eql([ | ||
{ id: 'foo', status: 200, result: 'already verified' }, | ||
{ id: 'bar', status: 200, result: 'verified' } | ||
]); | ||
}); | ||
|
||
it('should return errors for non-existent beats', async () => { | ||
const nonExistentBeatId = chance.word(); | ||
const { body: apiResponse } = await supertest | ||
.post( | ||
'/api/beats/agents/verify' | ||
) | ||
.set('kbn-xsrf', 'xxx') | ||
.send({ | ||
beats: [ | ||
{ id: 'bar' }, | ||
{ id: nonExistentBeatId } | ||
] | ||
}) | ||
.expect(200); | ||
|
||
expect(apiResponse.beats).to.eql([ | ||
{ id: 'bar', status: 200, result: 'verified' }, | ||
{ id: nonExistentBeatId, status: 404, result: 'not found' }, | ||
]); | ||
}); | ||
}); | ||
} |
Binary file modified
BIN
+28 Bytes
(110%)
x-pack/test/functional/es_archives/beats/list/data.json.gz
Binary file not shown.
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.
I'm wondering if we can simplify these function names.
Is it misleading to call them
find
instead ofdetermine
?I've also tried a number of ways of simplifying the suffixes but I have come to the conclusion that they're pretty well-named already.
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.
Yeah, I had a hard time naming these as well.
find*
works for me.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.
I think
find
might be more readable.I've approved the PR as everything else LGTM.