Skip to content

Commit

Permalink
Migrate authc dependency from security plugin to core security service (
Browse files Browse the repository at this point in the history
#187124)

## Summary

Part of #186574

Background: This PR is an example of a plugin migrating away from
depending on the Security plugin, which is a high-priority effort for
the last release before 9.0. The Cases plugin uses authc.getCurrentUser
from the security plugin's start contract on the server side.

This PR migrates authc.getCurrentUser from the security plugin start
contract to the core security service.

Checklist
- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

---------

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
TinaHeiligers and kibanamachine authored Jun 30, 2024
1 parent 28cad8d commit 4eb07b1
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 9 deletions.
10 changes: 5 additions & 5 deletions x-pack/plugins/cases/server/client/factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('CasesClientFactory', () => {
});

expect(args.securityPluginStart.userProfiles.getCurrent).toHaveBeenCalled();
expect(args.securityPluginStart.authc.getCurrentUser).not.toHaveBeenCalled();
expect(args.securityServiceStart.authc.getCurrentUser).not.toHaveBeenCalled();
expect(createCasesClientMocked.mock.calls[0][0].user).toEqual({
username: 'my_user',
full_name: 'My user',
Expand All @@ -63,7 +63,7 @@ describe('CasesClientFactory', () => {
it('constructs the user info from the authc service if the user profile is not available', async () => {
const scopedClusterClient = coreStart.elasticsearch.client.asScoped(request).asCurrentUser;
// @ts-expect-error: not all fields are needed
args.securityPluginStart.authc.getCurrentUser.mockReturnValueOnce({
args.securityServiceStart.authc.getCurrentUser.mockReturnValueOnce({
username: 'my_user_2',
full_name: 'My user 2',
email: '[email protected]',
Expand All @@ -76,7 +76,7 @@ describe('CasesClientFactory', () => {
});

expect(args.securityPluginStart.userProfiles.getCurrent).toHaveBeenCalled();
expect(args.securityPluginStart.authc.getCurrentUser).toHaveBeenCalled();
expect(args.securityServiceStart.authc.getCurrentUser).toHaveBeenCalled();
expect(createCasesClientMocked.mock.calls[0][0].user).toEqual({
username: 'my_user_2',
full_name: 'My user 2',
Expand All @@ -95,7 +95,7 @@ describe('CasesClientFactory', () => {
});

expect(args.securityPluginStart.userProfiles.getCurrent).toHaveBeenCalled();
expect(args.securityPluginStart.authc.getCurrentUser).toHaveBeenCalled();
expect(args.securityServiceStart.authc.getCurrentUser).toHaveBeenCalled();
expect(createCasesClientMocked.mock.calls[0][0].user).toEqual({
username: 'elastic/kibana',
full_name: null,
Expand All @@ -113,7 +113,7 @@ describe('CasesClientFactory', () => {
});

expect(args.securityPluginStart.userProfiles.getCurrent).toHaveBeenCalled();
expect(args.securityPluginStart.authc.getCurrentUser).toHaveBeenCalled();
expect(args.securityServiceStart.authc.getCurrentUser).toHaveBeenCalled();
expect(createCasesClientMocked.mock.calls[0][0].user).toEqual({
username: null,
full_name: null,
Expand Down
5 changes: 4 additions & 1 deletion x-pack/plugins/cases/server/client/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
ElasticsearchClient,
SavedObjectsClientContract,
IBasePath,
SecurityServiceStart,
} from '@kbn/core/server';
import type { ISavedObjectsSerializer } from '@kbn/core-saved-objects-server';
import { SECURITY_EXTENSION_ID } from '@kbn/core-saved-objects-server';
Expand Down Expand Up @@ -57,6 +58,7 @@ import { EmailNotificationService } from '../services/notifications/email_notifi
interface CasesClientFactoryArgs {
securityPluginSetup: SecurityPluginSetup;
securityPluginStart: SecurityPluginStart;
securityServiceStart: SecurityServiceStart;
spacesPluginStart?: SpacesPluginStart;
featuresPluginStart: FeaturesPluginStart;
actionsPluginStart: ActionsPluginStart;
Expand Down Expand Up @@ -257,6 +259,7 @@ export class CasesClientFactory {

try {
const userProfile = await this.options.securityPluginStart.userProfiles.getCurrent({
// todo: Access userProfiles from core's UserProfileService contract
request,
});

Expand All @@ -273,7 +276,7 @@ export class CasesClientFactory {
}

try {
const user = this.options.securityPluginStart.authc.getCurrentUser(request);
const user = this.options.securityServiceStart.authc.getCurrentUser(request);

if (user != null) {
return {
Expand Down
7 changes: 6 additions & 1 deletion x-pack/plugins/cases/server/client/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
*/

import type { PublicContract, PublicMethodsOf } from '@kbn/utility-types';
import { loggingSystemMock, savedObjectsClientMock } from '@kbn/core/server/mocks';
import {
loggingSystemMock,
savedObjectsClientMock,
securityServiceMock,
} from '@kbn/core/server/mocks';
import type { ISavedObjectsSerializer } from '@kbn/core-saved-objects-server';

import {
Expand Down Expand Up @@ -226,6 +230,7 @@ export const createCasesClientFactoryMockArgs = () => {
return {
securityPluginSetup: securityMock.createSetup(),
securityPluginStart: securityMock.createStart(),
securityServiceStart: securityServiceMock.createStart(),
spacesPluginStart: spacesMock.createStart(),
featuresPluginStart: featuresPluginMock.createSetup(),
actionsPluginStart: actionsMock.createStart(),
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/cases/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ export class CasePlugin
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
securityPluginSetup: this.securityPluginSetup!,
securityPluginStart: plugins.security,
securityServiceStart: core.security,
spacesPluginStart: plugins.spaces,
featuresPluginStart: plugins.features,
actionsPluginStart: plugins.actions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class EmailNotificationService implements NotificationService {
);

const uids = new Set(assignees.map((assignee) => assignee.uid));
const userProfiles = await this.security.userProfiles.bulkGet({ uids });
const userProfiles = await this.security.userProfiles.bulkGet({ uids }); // todo: access userProfiles from core security service start contract
const users = userProfiles.map((profile) => profile.user);

const to = users
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/cases/server/services/user_profiles/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const MIN_PROFILES_SIZE = 0;

interface UserProfileOptions {
securityPluginSetup: SecurityPluginSetup;
securityPluginStart: SecurityPluginStart;
securityPluginStart: SecurityPluginStart; // TODO: Use core's UserProfileService
spaces?: SpacesPluginStart;
licensingPluginStart: LicensingPluginStart;
}
Expand Down Expand Up @@ -58,6 +58,7 @@ export class UserProfileService {
size?: number;
owners: string[];
}) {
// TODO: Use core's UserProfileService
return securityPluginStart.userProfiles.suggest({
name: searchTerm,
size,
Expand Down

0 comments on commit 4eb07b1

Please sign in to comment.