Skip to content

Commit

Permalink
Resolving issue when initially registering privileges
Browse files Browse the repository at this point in the history
  • Loading branch information
kobelb committed Jul 5, 2018
1 parent 19ddaea commit 1f48041
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,26 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { difference, isEqual } from 'lodash';
import { difference, isEmpty, isEqual } from 'lodash';
import { buildPrivilegeMap } from './privileges';
import { getClient } from '../../../../../server/lib/get_client_shield';



export async function registerPrivilegesWithCluster(server) {
const config = server.config();
const kibanaVersion = config.get('pkg.version');
const application = config.get('xpack.security.rbac.application');
const savedObjectTypes = server.savedObjects.types;

const shouldRemovePrivileges = (existingPrivileges, expectedPrivileges) => {
if (isEmpty(existingPrivileges)) {
return false;
}

return difference(Object.keys(existingPrivileges[application]), Object.keys(expectedPrivileges[application])).length > 0;
};

const expectedPrivileges = {
[application]: buildPrivilegeMap(savedObjectTypes, application, kibanaVersion)
};
Expand All @@ -35,7 +45,7 @@ export async function registerPrivilegesWithCluster(server) {
// remove unspecified privileges. We don't currently have a need to remove privileges, as this would be a
// backwards compatibility issue, and we'd have to figure out how to migrate roles, so we're throwing an Error if we
// unintentionally get ourselves in this position.
if (difference(Object.keys(existingPrivileges[application]), Object.keys(expectedPrivileges[application])).length > 0) {
if (shouldRemovePrivileges(existingPrivileges, expectedPrivileges)) {
throw new Error(`Privileges are missing and can't be removed, currently.`);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ const registerPrivilegesWithClusterTest = (description, {
throw throwErrorWhenGettingPrivileges;
}

// ES returns an empty object if we don't have any privileges
if (!existingPrivileges) {
return {};
}

return {
[defaultApplication]: existingPrivileges
};
Expand Down Expand Up @@ -172,6 +177,16 @@ registerPrivilegesWithClusterTest(`passes saved object types, application and ki
},
});

registerPrivilegesWithClusterTest(`inserts privileges when we don't have any existing privileges`, {
expectedPrivileges: {
expected: true
},
existingPrivileges: null,
assert: ({ expectUpdatedPrivileges }) => {
expectUpdatedPrivileges();
}
});

registerPrivilegesWithClusterTest(`updates privileges when simple top-level privileges values don't match`, {
expectedPrivileges: {
expected: true
Expand Down

0 comments on commit 1f48041

Please sign in to comment.