Skip to content

Commit 0a4a045

Browse files
committed
Additional testing and refactoring
1 parent 76538c7 commit 0a4a045

30 files changed

+309
-185
lines changed

x-pack/plugins/security/server/plugin.ts

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import { securityFeatures } from './features';
3737
import { ElasticsearchService } from './elasticsearch';
3838
import { SessionManagementService } from './session_management';
3939
import { registerSecurityUsageCollector } from './usage_collector';
40-
import { SecureSpacesClientWrapper, LegacySpacesAuditLogger } from './spaces';
40+
import { setupSpacesClient } from './spaces';
4141

4242
export type SpacesService = Pick<
4343
SpacesPluginSetup['spacesService'],
@@ -211,23 +211,11 @@ export class Plugin {
211211
getCurrentUser: this.authc.getCurrentUser,
212212
});
213213

214-
if (spaces) {
215-
spaces.spacesService.clientService.setClientRepositoryFactory(
216-
(request, savedObjectsStart) => {
217-
if (authz.mode.useRbacForRequest(request)) {
218-
return savedObjectsStart.createInternalRepository(['space']);
219-
}
220-
return savedObjectsStart.createScopedRepository(request, ['space']);
221-
}
222-
);
223-
224-
const spacesAuditLogger = new LegacySpacesAuditLogger(audit.getLogger());
225-
226-
spaces.spacesService.clientService.registerClientWrapper(
227-
(request, baseClient) =>
228-
new SecureSpacesClientWrapper(baseClient, request, authz, spacesAuditLogger)
229-
);
230-
}
214+
setupSpacesClient({
215+
spaces,
216+
audit,
217+
authz,
218+
});
231219

232220
setupSavedObjects({
233221
legacyAuditLogger,

x-pack/plugins/security/server/spaces/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
export { LegacySpacesAuditLogger } from './legacy_audit_logger';
8-
export { SecureSpacesClientWrapper } from './secure_spaces_client_wrapper';
7+
export { setupSpacesClient } from './setup_spaces_client';

x-pack/plugins/security/server/spaces/secure_spaces_client_wrapper.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import { httpServerMock } from '../../../../../src/core/server/mocks';
88

9-
import { SecureSpacesClientWrapper } from '.';
9+
import { SecureSpacesClientWrapper } from './secure_spaces_client_wrapper';
1010

1111
import { spacesClientMock } from '../../../spaces/server/mocks';
1212
import { deepFreeze } from '@kbn/std';
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import { spacesMock } from '../../../spaces/server/mocks';
8+
9+
import { auditServiceMock } from '../audit/index.mock';
10+
import { authorizationMock } from '../authorization/index.mock';
11+
import { setupSpacesClient } from './setup_spaces_client';
12+
13+
describe('setupSpacesClient', () => {
14+
it('does not setup the spaces client when spaces is disabled', () => {
15+
const authz = authorizationMock.create();
16+
const audit = auditServiceMock.create();
17+
18+
setupSpacesClient({ authz, audit });
19+
20+
expect(audit.getLogger).not.toHaveBeenCalled();
21+
});
22+
23+
it('configures the repository factory, wrapper, and audit logger', () => {
24+
const authz = authorizationMock.create();
25+
const audit = auditServiceMock.create();
26+
const spaces = spacesMock.createSetup();
27+
28+
setupSpacesClient({ authz, audit, spaces });
29+
30+
expect(spaces.spacesClient.registerClientWrapper).toHaveBeenCalledTimes(1);
31+
expect(spaces.spacesClient.setClientRepositoryFactory).toHaveBeenCalledTimes(1);
32+
expect(audit.getLogger).toHaveBeenCalledTimes(1);
33+
});
34+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import { SpacesPluginSetup } from '../../../spaces/server';
8+
import { AuditServiceSetup } from '../audit';
9+
import { AuthorizationServiceSetup } from '../authorization';
10+
import { LegacySpacesAuditLogger } from './legacy_audit_logger';
11+
import { SecureSpacesClientWrapper } from './secure_spaces_client_wrapper';
12+
13+
interface Deps {
14+
audit: AuditServiceSetup;
15+
authz: AuthorizationServiceSetup;
16+
spaces?: SpacesPluginSetup;
17+
}
18+
19+
export const setupSpacesClient = ({ audit, authz, spaces }: Deps) => {
20+
if (!spaces) {
21+
return;
22+
}
23+
const { spacesClient } = spaces;
24+
25+
spacesClient.setClientRepositoryFactory((request, savedObjectsStart) => {
26+
if (authz.mode.useRbacForRequest(request)) {
27+
return savedObjectsStart.createInternalRepository(['space']);
28+
}
29+
return savedObjectsStart.createScopedRepository(request, ['space']);
30+
});
31+
32+
const spacesAuditLogger = new LegacySpacesAuditLogger(audit.getLogger());
33+
34+
spacesClient.registerClientWrapper(
35+
(request, baseClient) =>
36+
new SecureSpacesClientWrapper(baseClient, request, authz, spacesAuditLogger)
37+
);
38+
};

x-pack/plugins/spaces/server/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { Plugin } from './plugin';
1717

1818
export { SpacesPluginSetup, SpacesPluginStart } from './plugin';
1919
export { SpacesServiceSetup, SpacesServiceStart } from './spaces_service';
20-
export { ISpacesClient } from './lib/spaces_client';
20+
export { ISpacesClient } from './spaces_client';
2121
export { Space } from '../common/model/space';
2222

2323
export const config = { schema: ConfigSchema };

x-pack/plugins/spaces/server/lib/spaces_tutorial_context_factory.test.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import * as Rx from 'rxjs';
87
import { DEFAULT_SPACE_ID } from '../../common/constants';
98
import { createSpacesTutorialContextFactory } from './spaces_tutorial_context_factory';
109
import { SpacesService } from '../spaces_service';
11-
import { coreMock, loggingSystemMock } from '../../../../../src/core/server/mocks';
10+
import { coreMock } from '../../../../../src/core/server/mocks';
1211
import { spacesServiceMock } from '../spaces_service/spaces_service.mock';
13-
import { spacesConfig } from './__fixtures__';
12+
import { spacesClientServiceMock } from '../spaces_client/spaces_client_service.mock';
1413

15-
const log = loggingSystemMock.createLogger();
16-
17-
const service = new SpacesService(log);
14+
const service = new SpacesService();
1815

1916
describe('createSpacesTutorialContextFactory', () => {
2017
it('should create a valid context factory', async () => {
@@ -36,11 +33,13 @@ describe('createSpacesTutorialContextFactory', () => {
3633

3734
it('should create context with the current space id for the default space', async () => {
3835
service.setup({
39-
http: coreMock.createSetup().http,
40-
config$: Rx.of(spacesConfig),
36+
basePath: coreMock.createSetup().http.basePath,
4137
});
4238
const contextFactory = createSpacesTutorialContextFactory(() =>
43-
service.start(coreMock.createStart())
39+
service.start({
40+
basePath: coreMock.createStart().http.basePath,
41+
spacesClientService: spacesClientServiceMock.createStart(),
42+
})
4443
);
4544

4645
const request = {};

x-pack/plugins/spaces/server/mocks.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
* or more contributor license agreements. Licensed under the Elastic License;
44
* you may not use this file except in compliance with the Elastic License.
55
*/
6+
import { spacesClientServiceMock } from './spaces_client/spaces_client_service.mock';
67
import { spacesServiceMock } from './spaces_service/spaces_service.mock';
78

89
function createSetupMock() {
9-
return { spacesService: spacesServiceMock.createSetupContract() };
10+
return {
11+
spacesService: spacesServiceMock.createSetupContract(),
12+
spacesClient: spacesClientServiceMock.createSetup(),
13+
};
1014
}
1115

1216
function createStartMock() {
@@ -20,4 +24,4 @@ export const spacesMock = {
2024
createStart: createStartMock,
2125
};
2226

23-
export { spacesClientMock } from './lib/spaces_client/spaces_client.mock';
27+
export { spacesClientMock } from './spaces_client/spaces_client.mock';

x-pack/plugins/spaces/server/plugin.test.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ describe('Spaces Plugin', () => {
2828
"setClientRepositoryFactory": [Function],
2929
},
3030
"spacesService": Object {
31-
"clientService": Object {
32-
"registerClientWrapper": [Function],
33-
"setClientRepositoryFactory": [Function],
34-
},
3531
"getSpaceId": [Function],
3632
"namespaceToSpaceId": [Function],
3733
"spaceIdToNamespace": [Function],

x-pack/plugins/spaces/server/plugin.ts

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ import { setupCapabilities } from './capabilities';
3131
import { SpacesSavedObjectsService } from './saved_objects';
3232
import { DefaultSpaceService } from './default_space';
3333
import { SpacesLicenseService } from '../common/licensing';
34-
import { SpacesClientRepositoryFactory, SpacesClientWrapper } from './lib/spaces_client';
34+
import {
35+
SpacesClientRepositoryFactory,
36+
SpacesClientService,
37+
SpacesClientWrapper,
38+
} from './spaces_client';
3539

3640
export interface PluginsSetup {
3741
features: FeaturesPluginSetup;
@@ -65,6 +69,8 @@ export class Plugin {
6569

6670
private readonly spacesLicenseService = new SpacesLicenseService();
6771

72+
private readonly spacesClientService: SpacesClientService;
73+
6874
private readonly spacesService: SpacesService;
6975

7076
private spacesServiceStart?: SpacesServiceStart;
@@ -75,23 +81,18 @@ export class Plugin {
7581
this.config$ = initializerContext.config.create<ConfigType>();
7682
this.kibanaIndexConfig$ = initializerContext.config.legacy.globalConfig$;
7783
this.log = initializerContext.logger.get();
78-
this.spacesService = new SpacesService(this.log);
79-
}
80-
81-
public start(core: CoreStart) {
82-
this.spacesServiceStart = this.spacesService.start(core);
83-
return {
84-
spacesService: this.spacesServiceStart,
85-
};
84+
this.spacesService = new SpacesService();
85+
this.spacesClientService = new SpacesClientService((message) => this.log.debug(message));
8686
}
8787

8888
public async setup(
8989
core: CoreSetup<PluginsStart>,
9090
plugins: PluginsSetup
9191
): Promise<SpacesPluginSetup> {
92+
const spacesClientSetup = this.spacesClientService.setup({ config$: this.config$ });
93+
9294
const spacesServiceSetup = this.spacesService.setup({
93-
http: core.http,
94-
config$: this.config$,
95+
basePath: core.http.basePath,
9596
});
9697

9798
const getSpacesService = () => {
@@ -139,12 +140,7 @@ export class Plugin {
139140
initSpacesRequestInterceptors({
140141
http: core.http,
141142
log: this.log,
142-
getSpacesService: () => {
143-
if (!this.spacesServiceStart) {
144-
throw new Error('Spaces Service is not yet initialized');
145-
}
146-
return this.spacesServiceStart;
147-
},
143+
getSpacesService,
148144
features: plugins.features,
149145
});
150146

@@ -165,10 +161,21 @@ export class Plugin {
165161
}
166162

167163
return {
164+
spacesClient: spacesClientSetup,
168165
spacesService: spacesServiceSetup,
169-
spacesClient: {
170-
...spacesServiceSetup.clientService,
171-
},
166+
};
167+
}
168+
169+
public start(core: CoreStart) {
170+
const spacesClientStart = this.spacesClientService.start(core);
171+
172+
this.spacesServiceStart = this.spacesService.start({
173+
basePath: core.http.basePath,
174+
spacesClientService: spacesClientStart,
175+
});
176+
177+
return {
178+
spacesService: this.spacesServiceStart,
172179
};
173180
}
174181

0 commit comments

Comments
 (0)