Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Authz] Superuser privileges #196586

Merged
merged 8 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -276,4 +276,19 @@ describe('RouteSecurity validation', () => {
`"[authz.requiredPrivileges]: anyRequired privileges must contain unique values"`
);
});

it('should fail validation when anyRequired has superuser privileges set', () => {
const invalidRouteSecurity = {
authz: {
requiredPrivileges: [
{ anyRequired: ['privilege1', 'privilege1'], allRequired: ['privilege4'] },
{ anyRequired: ['privilege5', 'superuser'] },
],
},
};

expect(() => validRouteSecurity(invalidRouteSecurity)).toThrowErrorMatchingInlineSnapshot(
`"[authz.requiredPrivileges]: superuser privileges set cannot be used in anyRequired"`
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ const requiredPrivilegesSchema = schema.arrayOf(
}
});

if (anyRequired.includes('superuser')) {
elena-shostak marked this conversation as resolved.
Show resolved Hide resolved
return 'superuser privileges set cannot be used in anyRequired';
}

if (anyRequired.length && allRequired.length) {
for (const privilege of anyRequired) {
if (allRequired.includes(privilege)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('Key rotation routes', () => {
it('correctly defines route.', () => {
expect(routeConfig.options).toEqual({
access: 'public',
tags: ['access:rotateEncryptionKey', 'oas-tag:saved objects'],
elena-shostak marked this conversation as resolved.
Show resolved Hide resolved
tags: ['oas-tag:saved objects'],
summary: `Rotate a key for encrypted saved objects`,
description: `If a saved object cannot be decrypted using the primary encryption key, Kibana attempts to decrypt it using the specified decryption-only keys. In most of the cases this overhead is negligible, but if you're dealing with a large number of saved objects and experiencing performance issues, you may want to rotate the encryption key.
NOTE: Bulk key rotation can consume a considerable amount of resources and hence only user with a superuser role can trigger it.`,
Expand Down Expand Up @@ -96,7 +96,7 @@ describe('Key rotation routes', () => {

expect(config.options).toEqual({
access: 'internal',
tags: ['access:rotateEncryptionKey', 'oas-tag:saved objects'],
tags: ['oas-tag:saved objects'],
summary: `Rotate a key for encrypted saved objects`,
description: `If a saved object cannot be decrypted using the primary encryption key, Kibana attempts to decrypt it using the specified decryption-only keys. In most of the cases this overhead is negligible, but if you're dealing with a large number of saved objects and experiencing performance issues, you may want to rotate the encryption key.
NOTE: Bulk key rotation can consume a considerable amount of resources and hence only user with a superuser role can trigger it.`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { schema } from '@kbn/config-schema';
import { ReservedPrivilegesSet } from '@kbn/security-plugin/common';

import type { RouteDefinitionParams } from '.';

Expand Down Expand Up @@ -39,9 +40,14 @@ export function defineKeyRotationRoutes({
type: schema.maybe(schema.string()),
}),
},
security: {
authz: {
requiredPrivileges: [ReservedPrivilegesSet.Superuser],
},
},
options: {
tags: ['access:rotateEncryptionKey', 'oas-tag:saved objects'],
access: buildFlavor === 'serverless' ? 'internal' : 'public',
tags: ['oas-tag:saved objects'],
summary: `Rotate a key for encrypted saved objects`,
description: `If a saved object cannot be decrypted using the primary encryption key, Kibana attempts to decrypt it using the specified decryption-only keys. In most of the cases this overhead is negligible, but if you're dealing with a large number of saved objects and experiencing performance issues, you may want to rotate the encryption key.
NOTE: Bulk key rotation can consume a considerable amount of resources and hence only user with a superuser role can trigger it.`,
Expand Down
11 changes: 11 additions & 0 deletions x-pack/plugins/security/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,14 @@ export const API_VERSIONS = {
},
},
};

/**
* Privileges that define the superuser role or the role equivalent to the superuser role.
*/
export const SUPER_USER_PRIVILEGES = {
elena-shostak marked this conversation as resolved.
Show resolved Hide resolved
kibana: ['*'],
elasticsearch: {
cluster: ['all'],
index: { '*': ['all'] },
},
};
2 changes: 2 additions & 0 deletions x-pack/plugins/security/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,5 @@ export type {
UserProfileLabels,
UserProfileUserInfoWithSecurity,
} from '@kbn/security-plugin-types-common';

export { ReservedPrivilegesSet } from './types';
5 changes: 5 additions & 0 deletions x-pack/plugins/security/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ export enum LogoutReason {
export interface SecurityCheckupState {
displayAlert: boolean;
}

export enum ReservedPrivilegesSet {
elena-shostak marked this conversation as resolved.
Show resolved Hide resolved
Operator = 'operator',
Superuser = 'superuser',
}
156 changes: 112 additions & 44 deletions x-pack/plugins/security/server/authorization/api_authorization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {

import { initAPIAuthorization } from './api_authorization';
import { authorizationMock } from './index.mock';
import { ReservedPrivilegesSet } from '../../common/types';

describe('initAPIAuthorization', () => {
test(`protected route when "mode.useRbacForRequest()" returns false continues`, async () => {
Expand Down Expand Up @@ -149,7 +150,10 @@ describe('initAPIAuthorization', () => {
asserts,
}: {
security?: RouteSecurity;
kibanaPrivilegesResponse?: Array<{ privilege: string; authorized: boolean }>;
kibanaPrivilegesResponse?: {
privileges: { kibana: Array<{ privilege: string; authorized: boolean }> };
hasAllRequested?: boolean;
};
kibanaPrivilegesRequestActions?: string[];
asserts: {
forbidden?: boolean;
Expand Down Expand Up @@ -180,11 +184,7 @@ describe('initAPIAuthorization', () => {
const mockResponse = httpServerMock.createResponseFactory();
const mockPostAuthToolkit = httpServiceMock.createOnPostAuthToolkit();

const mockCheckPrivileges = jest.fn().mockReturnValue({
privileges: {
kibana: kibanaPrivilegesResponse,
},
});
const mockCheckPrivileges = jest.fn().mockReturnValue(kibanaPrivilegesResponse);
mockAuthz.mode.useRbacForRequest.mockReturnValue(true);
mockAuthz.checkPrivilegesDynamicallyWithRequest.mockImplementation((request) => {
// hapi conceals the actual "request" from us, so we make sure that the headers are passed to
Expand All @@ -194,6 +194,12 @@ describe('initAPIAuthorization', () => {
return mockCheckPrivileges;
});

mockAuthz.checkPrivilegesWithRequest.mockImplementation((request) => {
expect(request.headers).toMatchObject(headers);

return { globally: () => kibanaPrivilegesResponse };
});

await postAuthHandler(mockRequest, mockResponse, mockPostAuthToolkit);

expect(mockAuthz.mode.useRbacForRequest).toHaveBeenCalledWith(mockRequest);
Expand All @@ -207,11 +213,13 @@ describe('initAPIAuthorization', () => {
return;
}

expect(mockCheckPrivileges).toHaveBeenCalledWith({
kibana: kibanaPrivilegesRequestActions!.map((action: string) =>
mockAuthz.actions.api.get(action)
),
});
if (kibanaPrivilegesRequestActions) {
expect(mockCheckPrivileges).toHaveBeenCalledWith({
kibana: kibanaPrivilegesRequestActions!.map((action: string) =>
mockAuthz.actions.api.get(action)
),
});
}

if (asserts.forbidden) {
expect(mockResponse.forbidden).toHaveBeenCalled();
Expand Down Expand Up @@ -239,11 +247,15 @@ describe('initAPIAuthorization', () => {
],
},
},
kibanaPrivilegesResponse: [
{ privilege: 'api:privilege1', authorized: true },
{ privilege: 'api:privilege2', authorized: true },
{ privilege: 'api:privilege3', authorized: false },
],
kibanaPrivilegesResponse: {
privileges: {
kibana: [
{ privilege: 'api:privilege1', authorized: true },
{ privilege: 'api:privilege2', authorized: true },
{ privilege: 'api:privilege3', authorized: false },
],
},
},
kibanaPrivilegesRequestActions: ['privilege1', 'privilege2', 'privilege3'],
asserts: {
authzResult: {
Expand All @@ -267,10 +279,14 @@ describe('initAPIAuthorization', () => {
],
},
},
kibanaPrivilegesResponse: [
{ privilege: 'api:privilege1', authorized: true },
{ privilege: 'api:privilege2', authorized: true },
],
kibanaPrivilegesResponse: {
privileges: {
kibana: [
{ privilege: 'api:privilege1', authorized: true },
{ privilege: 'api:privilege2', authorized: true },
],
},
},
kibanaPrivilegesRequestActions: ['privilege1', 'privilege2'],
asserts: {
authzResult: {
Expand All @@ -293,11 +309,15 @@ describe('initAPIAuthorization', () => {
],
},
},
kibanaPrivilegesResponse: [
{ privilege: 'api:privilege1', authorized: false },
{ privilege: 'api:privilege2', authorized: true },
{ privilege: 'api:privilege3', authorized: false },
],
kibanaPrivilegesResponse: {
privileges: {
kibana: [
{ privilege: 'api:privilege1', authorized: false },
{ privilege: 'api:privilege2', authorized: true },
{ privilege: 'api:privilege3', authorized: false },
],
},
},
kibanaPrivilegesRequestActions: ['privilege1', 'privilege2', 'privilege3'],
asserts: {
authzResult: {
Expand All @@ -317,10 +337,14 @@ describe('initAPIAuthorization', () => {
requiredPrivileges: ['privilege1', 'privilege2'],
},
},
kibanaPrivilegesResponse: [
{ privilege: 'api:privilege1', authorized: true },
{ privilege: 'api:privilege2', authorized: true },
],
kibanaPrivilegesResponse: {
privileges: {
kibana: [
{ privilege: 'api:privilege1', authorized: true },
{ privilege: 'api:privilege2', authorized: true },
],
},
},
kibanaPrivilegesRequestActions: ['privilege1', 'privilege2'],
asserts: {
authzResult: {
Expand All @@ -344,18 +368,54 @@ describe('initAPIAuthorization', () => {
],
},
},
kibanaPrivilegesResponse: [
{ privilege: 'api:privilege1', authorized: true },
{ privilege: 'api:privilege2', authorized: false },
{ privilege: 'api:privilege3', authorized: false },
],
kibanaPrivilegesRequestActions: ['privilege1', 'privilege2', 'privilege3'],
kibanaPrivilegesResponse: {
privileges: {
kibana: [
{ privilege: 'api:privilege1', authorized: true },
{ privilege: 'api:privilege2', authorized: false },
{ privilege: 'api:privilege3', authorized: false },
],
},
},
asserts: {
forbidden: true,
},
}
);

testSecurityConfig(
`protected route restricted to only superusers returns forbidden if user not a superuser`,
{
security: {
authz: {
requiredPrivileges: [ReservedPrivilegesSet.Superuser],
},
},
kibanaPrivilegesResponse: { privileges: { kibana: [] }, hasAllRequested: false },
asserts: {
forbidden: true,
},
}
);

testSecurityConfig(
`protected route allowed only for superuser access returns "authzResult" if user is superuser`,
{
security: {
authz: {
requiredPrivileges: [ReservedPrivilegesSet.Superuser],
},
},
kibanaPrivilegesResponse: { privileges: { kibana: [] }, hasAllRequested: true },
asserts: {
authzResult: {
[ReservedPrivilegesSet.Superuser]: true,
},
},
}
);

testSecurityConfig(
`protected route returns forbidden if user doesn't have at least one from allRequired privileges requested`,
{
Expand All @@ -369,12 +429,16 @@ describe('initAPIAuthorization', () => {
],
},
},
kibanaPrivilegesResponse: [
{ privilege: 'api:privilege1', authorized: true },
{ privilege: 'api:privilege2', authorized: false },
{ privilege: 'api:privilege3', authorized: false },
{ privilege: 'api:privilege4', authorized: true },
],
kibanaPrivilegesResponse: {
privileges: {
kibana: [
{ privilege: 'api:privilege1', authorized: true },
{ privilege: 'api:privilege2', authorized: false },
{ privilege: 'api:privilege3', authorized: false },
{ privilege: 'api:privilege4', authorized: true },
],
},
},
kibanaPrivilegesRequestActions: ['privilege1', 'privilege2', 'privilege3', 'privilege4'],
asserts: {
forbidden: true,
Expand All @@ -390,10 +454,14 @@ describe('initAPIAuthorization', () => {
requiredPrivileges: ['privilege1', 'privilege2'],
},
},
kibanaPrivilegesResponse: [
{ privilege: 'api:privilege1', authorized: true },
{ privilege: 'api:privilege2', authorized: false },
],
kibanaPrivilegesResponse: {
privileges: {
kibana: [
{ privilege: 'api:privilege1', authorized: true },
{ privilege: 'api:privilege2', authorized: false },
],
},
},
kibanaPrivilegesRequestActions: ['privilege1', 'privilege2'],
asserts: {
forbidden: true,
Expand Down
Loading