Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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