Skip to content

Commit 017c2e5

Browse files
[Task Manager] Remove spaces plugin dependency in task manager plugin (#221596)
## Summary This PR removes spaces plugin dependency from task manager plugin and replaces `spacesService.getActiveSpace` with `getSpaceIdFromPath` from `@kbn/spaces-utils` package. This prevents a circular dependency from happening in #220138 > [!NOTE] > I'm changing the test case to use `test-space` id instead of `testSpace` as this was an invalid space id and it shouldn't have been used in the mock. --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
1 parent 661b96f commit 017c2e5

File tree

8 files changed

+17
-69
lines changed

8 files changed

+17
-69
lines changed

x-pack/platform/plugins/shared/task_manager/kibana.jsonc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
"optionalPlugins": [
1818
"cloud",
1919
"usageCollection",
20-
"spaces"
2120
]
2221
}
23-
}
22+
}

x-pack/platform/plugins/shared/task_manager/server/lib/api_key_utils.test.ts

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import {
1313
} from './api_key_utils';
1414
import { coreMock } from '@kbn/core/server/mocks';
1515
import { httpServerMock } from '@kbn/core-http-server-mocks';
16-
import type { SpacesPluginStart } from '@kbn/spaces-plugin/server';
17-
import { spacesMock } from '@kbn/spaces-plugin/server/mocks';
1816
import type { AuthenticatedUser } from '@kbn/core/server';
1917

2018
const mockTask = {
@@ -156,13 +154,8 @@ describe('api_key_utils', () => {
156154

157155
describe('getUserScope', () => {
158156
test('should return the users scope based on their request', async () => {
159-
const request = httpServerMock.createKibanaRequest();
157+
const request = httpServerMock.createKibanaRequest({ path: '/s/test-space' });
160158
const coreStart = coreMock.createStart();
161-
const spacesStart: jest.Mocked<SpacesPluginStart> = spacesMock.createStart();
162-
163-
spacesStart.spacesService.getActiveSpace = jest.fn().mockResolvedValue({
164-
id: 'testSpace',
165-
});
166159

167160
const mockUser = {
168161
authentication_type: 'basic',
@@ -178,18 +171,13 @@ describe('api_key_utils', () => {
178171
api_key: 'apiKey',
179172
});
180173

181-
const result = await getApiKeyAndUserScope(
182-
[mockTask],
183-
request,
184-
coreStart.security,
185-
spacesStart
186-
);
174+
const result = await getApiKeyAndUserScope([mockTask], request, coreStart.security);
187175

188176
expect(result.get('task')).toEqual({
189177
apiKey: 'YXBpS2V5SWQ6YXBpS2V5',
190178
userScope: {
191179
apiKeyId: 'apiKeyId',
192-
spaceId: 'testSpace',
180+
spaceId: 'test-space',
193181
apiKeyCreatedByUser: false,
194182
},
195183
});
@@ -198,7 +186,6 @@ describe('api_key_utils', () => {
198186
test('should default space to default if space is not found', async () => {
199187
const request = httpServerMock.createKibanaRequest();
200188
const coreStart = coreMock.createStart();
201-
const spacesStart: jest.Mocked<SpacesPluginStart> = spacesMock.createStart();
202189

203190
const mockUser = {
204191
authentication_type: 'basic',
@@ -214,12 +201,7 @@ describe('api_key_utils', () => {
214201
api_key: 'apiKey',
215202
});
216203

217-
const result = await getApiKeyAndUserScope(
218-
[mockTask],
219-
request,
220-
coreStart.security,
221-
spacesStart
222-
);
204+
const result = await getApiKeyAndUserScope([mockTask], request, coreStart.security);
223205

224206
expect(result.get('task')).toEqual({
225207
apiKey: 'YXBpS2V5SWQ6YXBpS2V5',
@@ -240,7 +222,6 @@ describe('api_key_utils', () => {
240222
});
241223

242224
const coreStart = coreMock.createStart();
243-
const spacesStart: jest.Mocked<SpacesPluginStart> = spacesMock.createStart();
244225
const mockUser = {
245226
authentication_type: 'api_key',
246227
username: 'testUser',
@@ -249,12 +230,7 @@ describe('api_key_utils', () => {
249230
coreStart.security.authc.apiKeys.areAPIKeysEnabled = jest.fn().mockReturnValueOnce(true);
250231
coreStart.security.authc.getCurrentUser = jest.fn().mockReturnValue(mockUser);
251232

252-
const result = await getApiKeyAndUserScope(
253-
[mockTask],
254-
request,
255-
coreStart.security,
256-
spacesStart
257-
);
233+
const result = await getApiKeyAndUserScope([mockTask], request, coreStart.security);
258234

259235
expect(result.get('task')).toEqual({
260236
apiKey: 'YXBpS2V5SWQ6YXBpS2V5',

x-pack/platform/plugins/shared/task_manager/server/lib/api_key_utils.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
import type { AuthenticatedUser, SecurityServiceStart } from '@kbn/core/server';
99
import type { KibanaRequest } from '@kbn/core/server';
10-
import type { SpacesPluginStart } from '@kbn/spaces-plugin/server';
1110
import { truncate } from 'lodash';
11+
import { getSpaceIdFromPath } from '@kbn/spaces-utils';
1212
import type { TaskInstance, TaskUserScope } from '../task';
1313

1414
export interface APIKeyResult {
@@ -125,11 +125,10 @@ export const createApiKey = async (
125125
export const getApiKeyAndUserScope = async (
126126
taskInstances: TaskInstance[],
127127
request: KibanaRequest,
128-
security: SecurityServiceStart,
129-
spaces?: SpacesPluginStart
128+
security: SecurityServiceStart
130129
): Promise<Map<string, ApiKeyAndUserScope>> => {
131130
const apiKeyByTaskIdMap = await createApiKey(taskInstances, request, security);
132-
const space = await spaces?.spacesService.getActiveSpace(request);
131+
const space = getSpaceIdFromPath(request.url.pathname);
133132
const user = security.authc.getCurrentUser(request);
134133

135134
const apiKeyAndUserScopeByTaskId = new Map<string, ApiKeyAndUserScope>();
@@ -141,7 +140,7 @@ export const getApiKeyAndUserScope = async (
141140
apiKey: encodedApiKeyResult.apiKey,
142141
userScope: {
143142
apiKeyId: encodedApiKeyResult.apiKeyId,
144-
spaceId: space?.id || 'default',
143+
spaceId: space?.spaceId || 'default',
145144
// Set apiKeyCreatedByUser to true if the user passed in their own API key, since we do
146145
// not want to invalidate a specific API key that was not created by the task manager
147146
apiKeyCreatedByUser: isRequestApiKeyType(user),

x-pack/platform/plugins/shared/task_manager/server/plugin.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import type {
2323
} from '@kbn/core/server';
2424
import type { CloudSetup, CloudStart } from '@kbn/cloud-plugin/server';
2525
import type { EncryptedSavedObjectsClient } from '@kbn/encrypted-saved-objects-shared';
26-
import type { SpacesPluginStart } from '@kbn/spaces-plugin/server';
2726
import {
2827
registerDeleteInactiveNodesTaskDefinition,
2928
scheduleDeleteInactiveNodesTaskDefinition,
@@ -95,7 +94,6 @@ export type TaskManagerStartContract = Pick<
9594
export interface TaskManagerPluginsStart {
9695
cloud?: CloudStart;
9796
usageCollection?: UsageCollectionStart;
98-
spaces?: SpacesPluginStart;
9997
}
10098

10199
export interface TaskManagerPluginsSetup {
@@ -288,7 +286,7 @@ export class TaskManagerPlugin
288286

289287
public start(
290288
{ http, savedObjects, elasticsearch, executionContext, security }: CoreStart,
291-
{ cloud, spaces }: TaskManagerPluginsStart
289+
{ cloud }: TaskManagerPluginsStart
292290
): TaskManagerStartContract {
293291
const savedObjectsRepository = savedObjects.createInternalRepository([
294292
TASK_SO_NAME,
@@ -322,7 +320,6 @@ export class TaskManagerPlugin
322320
requestTimeouts: this.config.request_timeouts,
323321
security,
324322
canEncryptSavedObjects: this.canEncryptSavedObjects,
325-
spaces,
326323
});
327324

328325
const isServerless = this.initContext.env.packageInfo.buildFlavor === 'serverless';

x-pack/platform/plugins/shared/task_manager/server/task_running/task_runner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import type {
2525
} from '@kbn/core/server';
2626
import { SavedObjectsErrorHelpers } from '@kbn/core/server';
2727
import type { UsageCounter } from '@kbn/usage-collection-plugin/server';
28-
import { addSpaceIdToPath } from '@kbn/spaces-plugin/server';
28+
import { addSpaceIdToPath } from '@kbn/spaces-utils';
2929
import { kibanaRequestFactory } from '@kbn/core-http-server-utils';
3030
import type { Middleware } from '../lib/middleware';
3131
import type { Result } from '../lib/result_type';

x-pack/platform/plugins/shared/task_manager/server/task_store.test.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import { asErr, asOk } from './lib/result_type';
3333
import type { UpdateByQueryResponse } from '@elastic/elasticsearch/lib/api/types';
3434
import { MsearchError } from './lib/msearch_error';
3535
import { getApiKeyAndUserScope } from './lib/api_key_utils';
36-
import { spacesMock } from '@kbn/spaces-plugin/server/mocks';
3736
import type {
3837
EncryptedSavedObjectsClient,
3938
EncryptedSavedObjectsClientOptions,
@@ -75,7 +74,6 @@ const adHocTaskCounter = new AdHocTaskCounter();
7574
const randomId = () => `id-${_.random(1, 20)}`;
7675

7776
const coreStart = coreMock.createStart();
78-
const spacesStart = spacesMock.createStart();
7977

8078
beforeEach(() => {
8179
jest.resetAllMocks();
@@ -146,7 +144,6 @@ describe('TaskStore', () => {
146144
},
147145
savedObjectsService: coreStart.savedObjects,
148146
security: coreStart.security,
149-
spaces: spacesStart,
150147
canEncryptSavedObjects: true,
151148
});
152149

@@ -319,12 +316,7 @@ describe('TaskStore', () => {
319316
}
320317
);
321318

322-
expect(getApiKeyAndUserScope).toHaveBeenCalledWith(
323-
[task],
324-
request,
325-
coreStart.security,
326-
spacesStart
327-
);
319+
expect(getApiKeyAndUserScope).toHaveBeenCalledWith([task], request, coreStart.security);
328320

329321
expect(savedObjectsClient.create).not.toHaveBeenCalled();
330322

@@ -363,7 +355,6 @@ describe('TaskStore', () => {
363355
},
364356
savedObjectsService: coreStart.savedObjects,
365357
security: coreStart.security,
366-
spaces: spacesStart,
367358
canEncryptSavedObjects: false,
368359
});
369360

@@ -1688,7 +1679,6 @@ describe('TaskStore', () => {
16881679
},
16891680
savedObjectsService: coreStart.savedObjects,
16901681
security: coreStart.security,
1691-
spaces: spacesStart,
16921682
canEncryptSavedObjects: true,
16931683
});
16941684

@@ -1809,7 +1799,6 @@ describe('TaskStore', () => {
18091799
},
18101800
savedObjectsService: coreStart.savedObjects,
18111801
security: coreStart.security,
1812-
spaces: spacesStart,
18131802
canEncryptSavedObjects: true,
18141803
});
18151804

@@ -2123,7 +2112,6 @@ describe('TaskStore', () => {
21232112
},
21242113
savedObjectsService: coreStart.savedObjects,
21252114
security: coreStart.security,
2126-
spaces: spacesStart,
21272115
canEncryptSavedObjects: true,
21282116
});
21292117

@@ -2330,8 +2318,7 @@ describe('TaskStore', () => {
23302318
expect(getApiKeyAndUserScope).toHaveBeenCalledWith(
23312319
[task1, task2],
23322320
request,
2333-
coreStart.security,
2334-
spacesStart
2321+
coreStart.security
23352322
);
23362323

23372324
expect(savedObjectsClient.create).not.toHaveBeenCalled();
@@ -2392,7 +2379,6 @@ describe('TaskStore', () => {
23922379
},
23932380
savedObjectsService: coreStart.savedObjects,
23942381
security: coreStart.security,
2395-
spaces: spacesStart,
23962382
canEncryptSavedObjects: false,
23972383
});
23982384

x-pack/platform/plugins/shared/task_manager/server/task_store.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import type {
3030
} from '@kbn/core/server';
3131

3232
import { SECURITY_EXTENSION_ID, SPACES_EXTENSION_ID } from '@kbn/core/server';
33-
import type { SpacesPluginStart } from '@kbn/spaces-plugin/server';
3433

3534
import type { EncryptedSavedObjectsClient } from '@kbn/encrypted-saved-objects-shared';
3635

@@ -79,7 +78,6 @@ export interface StoreOpts {
7978
security: SecurityServiceStart;
8079
canEncryptSavedObjects?: boolean;
8180
esoClient?: EncryptedSavedObjectsClient;
82-
spaces?: SpacesPluginStart;
8381
}
8482

8583
export interface SearchOpts {
@@ -149,7 +147,6 @@ export class TaskStore {
149147
private requestTimeouts: RequestTimeoutsConfig;
150148
private security: SecurityServiceStart;
151149
private canEncryptSavedObjects?: boolean;
152-
private spaces?: SpacesPluginStart;
153150
private logger: Logger;
154151

155152
/**
@@ -183,7 +180,6 @@ export class TaskStore {
183180
});
184181
this.requestTimeouts = opts.requestTimeouts;
185182
this.security = opts.security;
186-
this.spaces = opts.spaces;
187183
this.canEncryptSavedObjects = opts.canEncryptSavedObjects;
188184
this.logger = opts.logger;
189185
}
@@ -224,12 +220,7 @@ export class TaskStore {
224220

225221
let userScopeAndApiKey;
226222
try {
227-
userScopeAndApiKey = await getApiKeyAndUserScope(
228-
taskInstances,
229-
request,
230-
this.security,
231-
this.spaces
232-
);
223+
userScopeAndApiKey = await getApiKeyAndUserScope(taskInstances, request, this.security);
233224
} catch (e) {
234225
this.errors$.next(e);
235226
throw e;

x-pack/platform/plugins/shared/task_manager/tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@
3131
"@kbn/core-elasticsearch-server",
3232
"@kbn/es-query",
3333
"@kbn/core-http-server-mocks",
34-
"@kbn/spaces-plugin",
3534
"@kbn/encrypted-saved-objects-shared",
3635
"@kbn/core-saved-objects-api-server-mocks",
3736
"@kbn/core-http-server-utils",
3837
"@kbn/rrule",
39-
"@kbn/logging-mocks"
38+
"@kbn/logging-mocks",
39+
"@kbn/spaces-utils",
4040
],
4141
"exclude": ["target/**/*"]
4242
}

0 commit comments

Comments
 (0)