forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
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: List beats (elastic#19086)
* WIP checkin * Add API integration test * Converting to Jest test * WIP checkin * 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 * Updating mapping
- Loading branch information
1 parent
7aad032
commit 6e5e0bd
Showing
6 changed files
with
178 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
47 changes: 47 additions & 0 deletions
47
x-pack/plugins/beats/server/routes/api/register_list_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,47 @@ | ||
/* | ||
* 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 { | ||
get, | ||
omit | ||
} from "lodash"; | ||
import { INDEX_NAMES } from "../../../common/constants"; | ||
import { callWithRequestFactory } from '../../lib/client'; | ||
import { wrapEsError } from "../../lib/error_wrappers"; | ||
|
||
async function getBeats(callWithRequest) { | ||
const params = { | ||
index: INDEX_NAMES.BEATS, | ||
type: '_doc', | ||
q: 'type:beat' | ||
}; | ||
|
||
const response = await callWithRequest('search', params); | ||
return get(response, 'hits.hits', []); | ||
} | ||
|
||
// TODO: add license check pre-hook | ||
export function registerListBeatsRoute(server) { | ||
server.route({ | ||
method: 'GET', | ||
path: '/api/beats/agents', | ||
handler: async (request, reply) => { | ||
const callWithRequest = callWithRequestFactory(server, request); | ||
let beats; | ||
|
||
try { | ||
beats = await getBeats(callWithRequest); | ||
} catch (err) { | ||
return reply(wrapEsError(err)); | ||
} | ||
|
||
const response = { | ||
beats: beats.map(beat => omit(beat._source.beat, ['access_token'])) | ||
}; | ||
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,46 @@ | ||
/* | ||
* 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('list_beats', () => { | ||
const archive = 'beats/list'; | ||
|
||
beforeEach('load beats archive', () => esArchiver.load(archive)); | ||
afterEach('unload beats archive', () => esArchiver.unload(archive)); | ||
|
||
it('should return all beats', async () => { | ||
const { body: apiResponse } = await supertest | ||
.get( | ||
'/api/beats/agents' | ||
) | ||
.expect(200); | ||
|
||
const beatsFromApi = apiResponse.beats; | ||
|
||
expect(beatsFromApi.length).to.be(3); | ||
expect(beatsFromApi.filter(beat => beat.hasOwnProperty('verified_on')).length).to.be(1); | ||
expect(beatsFromApi.find(beat => beat.hasOwnProperty('verified_on')).id).to.be('foo'); | ||
}); | ||
|
||
it('should not return access tokens', async () => { | ||
const { body: apiResponse } = await supertest | ||
.get( | ||
'/api/beats/agents' | ||
) | ||
.expect(200); | ||
|
||
const beatsFromApi = apiResponse.beats; | ||
|
||
expect(beatsFromApi.length).to.be(3); | ||
expect(beatsFromApi.filter(beat => beat.hasOwnProperty('access_token')).length).to.be(0); | ||
}); | ||
}); | ||
} |
Binary file not shown.
82 changes: 82 additions & 0 deletions
82
x-pack/test/functional/es_archives/beats/list/mappings.json
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,82 @@ | ||
{ | ||
"type": "index", | ||
"value": { | ||
"index": ".management-beats", | ||
"settings": { | ||
"index": { | ||
"codec": "best_compression", | ||
"number_of_shards": "1", | ||
"auto_expand_replicas": "0-1", | ||
"number_of_replicas": "0" | ||
} | ||
}, | ||
"mappings": { | ||
"_doc": { | ||
"dynamic": "strict", | ||
"properties": { | ||
"type": { | ||
"type": "keyword" | ||
}, | ||
"enrollment_token": { | ||
"properties": { | ||
"token": { | ||
"type": "keyword" | ||
}, | ||
"expires_on": { | ||
"type": "date" | ||
} | ||
} | ||
}, | ||
"configuration_block": { | ||
"properties": { | ||
"tag": { | ||
"type": "keyword" | ||
}, | ||
"type": { | ||
"type": "keyword" | ||
}, | ||
"block_yml": { | ||
"type": "text" | ||
} | ||
} | ||
}, | ||
"beat": { | ||
"properties": { | ||
"id": { | ||
"type": "keyword" | ||
}, | ||
"access_token": { | ||
"type": "keyword" | ||
}, | ||
"verified_on": { | ||
"type": "date" | ||
}, | ||
"type": { | ||
"type": "keyword" | ||
}, | ||
"host_ip": { | ||
"type": "ip" | ||
}, | ||
"host_name": { | ||
"type": "keyword" | ||
}, | ||
"ephemeral_id": { | ||
"type": "keyword" | ||
}, | ||
"local_configuration_yml": { | ||
"type": "text" | ||
}, | ||
"central_configuration_yml": { | ||
"type": "text" | ||
}, | ||
"metadata": { | ||
"dynamic": "true", | ||
"type": "object" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |