Skip to content

Commit

Permalink
[Cases] Make spaces plugin optional (#150252)
Browse files Browse the repository at this point in the history
## Summary

This PR makes the spaces plugin an optional dependency for the cases
plugin.

Related: #149687

### Checklist

Delete any items that are not applicable to this PR.

- [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

### For maintainers

- [x] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)
  • Loading branch information
cnasikas authored Feb 9, 2023
1 parent 959e238 commit 7981e47
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 12 deletions.
4 changes: 2 additions & 2 deletions x-pack/plugins/cases/kibana.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
"kibanaUtils",
"triggersActionsUi",
"management",
"spaces",
"security",
"notifications"
],
"optionalPlugins": [
"home",
"taskManager",
"usageCollection"
"usageCollection",
"spaces"
],
"requiredBundles": [
"savedObjects"
Expand Down
32 changes: 32 additions & 0 deletions x-pack/plugins/cases/server/authorization/authorization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,38 @@ describe('authorization', () => {
await expect(authPromise).resolves.not.toThrow();
});

it('creates an Authorization object without spaces', async () => {
expect.assertions(2);

const authPromise = Authorization.create({
request,
securityAuth: securityStart.authz,
features: featuresStart,
auditLogger: new AuthorizationAuditLogger(mockLogger),
logger: loggingSystemMock.createLogger(),
});

await expect(authPromise).resolves.toBeDefined();
await expect(authPromise).resolves.not.toThrow();
});

it('if spaces are disabled it does not filtered out disabled features', async () => {
(spacesStart.spacesService.getActiveSpace as jest.Mock).mockImplementation(() => {
return { disabledFeatures: ['1'] } as Space;
});

const auth = await Authorization.create({
request,
securityAuth: securityStart.authz,
features: featuresStart,
auditLogger: new AuthorizationAuditLogger(mockLogger),
logger: loggingSystemMock.createLogger(),
});

// @ts-expect-error: featureCaseOwners is a private method of the auth class
expect([...auth.featureCaseOwners.values()]).toEqual(['a']);
});

it('throws and error when a failure occurs', async () => {
expect.assertions(1);

Expand Down
10 changes: 6 additions & 4 deletions x-pack/plugins/cases/server/authorization/authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,21 @@ export class Authorization {
}: {
request: KibanaRequest;
securityAuth?: SecurityPluginStart['authz'];
spaces: SpacesPluginStart;
spaces?: SpacesPluginStart;
features: FeaturesPluginStart;
auditLogger: AuthorizationAuditLogger;
logger: Logger;
}): Promise<Authorization> {
const getSpace = async (): Promise<Space> => {
return spaces.spacesService.getActiveSpace(request);
const getSpace = async (): Promise<Space | undefined> => {
return spaces?.spacesService.getActiveSpace(request);
};

// Since we need to do async operations, this static method handles that before creating the Auth class
let caseOwners: Set<string>;

try {
const disabledFeatures = new Set((await getSpace()).disabledFeatures ?? []);
const maybeSpace = await getSpace();
const disabledFeatures = new Set(maybeSpace?.disabledFeatures ?? []);

caseOwners = new Set(
features
Expand Down
9 changes: 6 additions & 3 deletions x-pack/plugins/cases/server/client/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type { LensServerPluginSetup } from '@kbn/lens-plugin/server';
import type { SpacesPluginStart } from '@kbn/spaces-plugin/server';
import type { LicensingPluginStart } from '@kbn/licensing-plugin/server';
import type { NotificationsPluginStart } from '@kbn/notifications-plugin/server';
import { DEFAULT_SPACE_ID } from '@kbn/spaces-plugin/common';
import { SAVED_OBJECT_TYPES } from '../../common/constants';
import { Authorization } from '../authorization/authorization';
import {
Expand All @@ -49,7 +50,7 @@ import { EmailNotificationService } from '../services/notifications/email_notifi
interface CasesClientFactoryArgs {
securityPluginSetup: SecurityPluginSetup;
securityPluginStart: SecurityPluginStart;
spacesPluginStart: SpacesPluginStart;
spacesPluginStart?: SpacesPluginStart;
featuresPluginStart: FeaturesPluginStart;
actionsPluginStart: ActionsPluginStart;
licensingPluginStart: LicensingPluginStart;
Expand Down Expand Up @@ -144,7 +145,8 @@ export class CasesClientFactory {
externalReferenceAttachmentTypeRegistry: this.options.externalReferenceAttachmentTypeRegistry,
securityStartPlugin: this.options.securityPluginStart,
publicBaseUrl: this.options.publicBaseUrl,
spaceId: this.options.spacesPluginStart.spacesService.getSpaceId(request),
spaceId:
this.options.spacesPluginStart?.spacesService.getSpaceId(request) ?? DEFAULT_SPACE_ID,
savedObjectsSerializer,
});
}
Expand Down Expand Up @@ -197,7 +199,8 @@ export class CasesClientFactory {
notifications: this.options.notifications,
security: this.options.securityPluginStart,
publicBaseUrl: this.options.publicBaseUrl,
spaceId: this.options.spacesPluginStart.spacesService.getSpaceId(request),
spaceId:
this.options.spacesPluginStart?.spacesService.getSpaceId(request) ?? DEFAULT_SPACE_ID,
});

return {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/cases/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export interface PluginsStart {
licensing: LicensingPluginStart;
taskManager?: TaskManagerStartContract;
security: SecurityPluginStart;
spaces: SpacesPluginStart;
spaces?: SpacesPluginStart;
notifications: NotificationsPluginStart;
}

Expand Down
5 changes: 3 additions & 2 deletions x-pack/plugins/cases/server/services/user_profiles/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type { KibanaRequest, Logger } from '@kbn/core/server';
import type { SecurityPluginSetup, SecurityPluginStart } from '@kbn/security-plugin/server';
import type { UserProfile } from '@kbn/security-plugin/common';
import type { SpacesPluginStart } from '@kbn/spaces-plugin/server';
import { DEFAULT_SPACE_ID } from '@kbn/spaces-plugin/common';

import type { LicensingPluginStart } from '@kbn/licensing-plugin/server';
import { excess, SuggestUserProfilesRequestRt, throwErrors } from '../../../common/api';
Expand All @@ -28,7 +29,7 @@ const MIN_PROFILES_SIZE = 0;
interface UserProfileOptions {
securityPluginSetup: SecurityPluginSetup;
securityPluginStart: SecurityPluginStart;
spaces: SpacesPluginStart;
spaces?: SpacesPluginStart;
licensingPluginStart: LicensingPluginStart;
}

Expand Down Expand Up @@ -110,7 +111,7 @@ export class UserProfileService {
size,
owners,
securityPluginStart: this.options.securityPluginStart,
spaceId: spaces.spacesService.getSpaceId(request),
spaceId: spaces?.spacesService.getSpaceId(request) ?? DEFAULT_SPACE_ID,
});
} catch (error) {
throw createCaseError({
Expand Down

0 comments on commit 7981e47

Please sign in to comment.