-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Beats Management] APIs: Verify beats (#19103)
* WIP checkin * WIP checkin * Add API integration test * Converting to Jest test * Fixing API for default case + adding test for it * Fixing copy pasta typos * Fixing variable name * Using a single index * Implementing GET /api/beats/agents API * Creating POST /api/beats/agents/verify API * Refactoring: extracting out helper functions * Fleshing out remaining tests * Expanding TODO note so I won't forget :) * Fixing file name * Updating mapping * Moving TODO comment to right file * Rename determine* helper functions to find*
- Loading branch information
1 parent
86ea7e2
commit 07abacd
Showing
5 changed files
with
227 additions
and
0 deletions.
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 findNonExistentBeatIds(beatsFromEs, beatIdsFromRequest) { | ||
return beatsFromEs.reduce((nonExistentBeatIds, beatFromEs, idx) => { | ||
if (!beatFromEs.found) { | ||
nonExistentBeatIds.push(beatIdsFromRequest[idx]); | ||
} | ||
return nonExistentBeatIds; | ||
}, []); | ||
} | ||
|
||
function findAlreadyVerifiedBeatIds(beatsFromEs) { | ||
return beatsFromEs | ||
.filter(beat => beat.found) | ||
.filter(beat => beat._source.beat.hasOwnProperty('verified_on')) | ||
.map(beat => beat._source.beat.id); | ||
} | ||
|
||
function findToBeVerifiedBeatIds(beatsFromEs) { | ||
return beatsFromEs | ||
.filter(beat => beat.found) | ||
.filter(beat => !beat._source.beat.hasOwnProperty('verified_on')) | ||
.map(beat => beat._source.beat.id); | ||
} | ||
|
||
function findVerifiedBeatIds(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 = findNonExistentBeatIds(beatsFromEs, beatIds); | ||
alreadyVerifiedBeatIds = findAlreadyVerifiedBeatIds(beatsFromEs); | ||
const toBeVerifiedBeatIds = findToBeVerifiedBeatIds(beatsFromEs); | ||
|
||
const verifications = await verifyBeats(callWithRequest, toBeVerifiedBeatIds); | ||
verifiedBeatIds = findVerifiedBeatIds(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.