Skip to content

Commit

Permalink
Merge pull request #20 from kobelb/pr/cjcenizal/45740
Browse files Browse the repository at this point in the history
Using the Shield plugin methods instead
  • Loading branch information
cjcenizal authored Oct 11, 2019
2 parents 5988691 + e4b3d84 commit f0298cd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import Joi from 'joi';
import { wrapError } from '../../../../../../../../plugins/security/server';

export function initGetApiKeysApi(server, callWithRequest, routePreCheckLicenseFn) {
Expand All @@ -13,14 +14,12 @@ export function initGetApiKeysApi(server, callWithRequest, routePreCheckLicenseF
async handler(request) {
try {
const { isAdmin } = request.query;
const path = `/_security/api_key${isAdmin === 'true' ? '' : '?owner=true'}`;

const result = await callWithRequest(
request,
'transport.request',
'shield.getAPIKeys',
{
method: 'GET',
path,
owner: !isAdmin
}
);

Expand All @@ -34,7 +33,12 @@ export function initGetApiKeysApi(server, callWithRequest, routePreCheckLicenseF
}
},
config: {
pre: [routePreCheckLicenseFn]
pre: [routePreCheckLicenseFn],
validate: {
query: Joi.object().keys({
isAdmin: Joi.bool(),
}),
},
}
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ export function initCheckPrivilegesApi(server, callWithRequest, routePreCheckLic
const result = await Promise.all([
callWithRequest(
request,
'transport.request',
'shield.hasPrivileges',
{
method: 'POST',
path: '/_security/user/_has_privileges',
body: {
cluster: [
'manage_security',
Expand All @@ -31,10 +29,9 @@ export function initCheckPrivilegesApi(server, callWithRequest, routePreCheckLic
try {
const result = await callWithRequest(
request,
'transport.request',
'shield.getAPIKeys',
{
method: 'GET',
path: '/_security/api_key?owner=true',
owner: true
}
);
// If the API returns a truthy result that means it's enabled.
Expand Down
19 changes: 19 additions & 0 deletions x-pack/legacy/server/lib/esjs_shield_plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,25 @@
]
});

/**
* Gets API keys in Elasticsearch
* @param {boolean} owner A boolean flag that can be used to query API keys owned by the currently authenticated user.
* Defaults to false. The realm_name or username parameters cannot be specified when this parameter is set to true as
* they are assumed to be the currently authenticated ones.
*/
shield.getAPIKeys = ca({
method: 'GET',
urls: [{
fmt: `/_security/api_key?owner=<%=owner%>`,
req: {
owner: {
type: 'boolean',
required: true
}
}
}]
});

/**
* Creates an API key in Elasticsearch for the current user.
*
Expand Down

0 comments on commit f0298cd

Please sign in to comment.