Skip to content

Commit

Permalink
Add Endpoint Details route
Browse files Browse the repository at this point in the history
  • Loading branch information
pzl committed Jan 23, 2020
1 parent cda6b13 commit bed9b49
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
32 changes: 31 additions & 1 deletion x-pack/plugins/endpoint/server/routes/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import { IRouter } from 'kibana/server';
import { SearchResponse } from 'elasticsearch';
import { schema } from '@kbn/config-schema';
import { EndpointAppContext, EndpointData } from '../types';
import { kibanaRequestToEndpointListQuery } from '../services/endpoint/endpoint_query_builders';
import {
kibanaRequestToEndpointListQuery,
kibanaRequestToEndpointFetchQuery,
} from '../services/endpoint/endpoint_query_builders';

interface HitSource {
_source: EndpointData;
Expand Down Expand Up @@ -60,6 +63,33 @@ export function registerEndpointRoutes(router: IRouter, endpointAppContext: Endp
}
}
);

router.get(
{
path: '/api/endpoint/endpoints/{id}',
validate: {
params: schema.object({ id: schema.string() }),
},
options: { authRequired: true },
},
async (context, req, res) => {
try {
const query = kibanaRequestToEndpointFetchQuery(req, endpointAppContext);
const response = (await context.core.elasticsearch.dataClient.callAsCurrentUser(
'search',
query
)) as SearchResponse<EndpointData>;

if (response.hits.hits.length === 0) {
return res.notFound({ body: 'Endpoint Not Found' });
}

return res.ok({ body: response.hits.hits[0]._source });
} catch (err) {
return res.internalError({ body: err });
}
}
);
}

function mapToEndpointResultList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,27 @@ async function getPagingProperties(
pageIndex: pagingProperties.page_index || config.endpointResultListDefaultFirstPageIndex,
};
}

export const kibanaRequestToEndpointFetchQuery = (
request: KibanaRequest<any, any, any>,
endpointAppContext: EndpointAppContext
) => {
return {
body: {
query: {
match: {
machine_id: request.params.id,
},
},
sort: [
{
created_at: {
order: 'desc',
},
},
],
size: 1,
},
index: EndpointAppConstants.ENDPOINT_INDEX_NAME,
};
};

0 comments on commit bed9b49

Please sign in to comment.