Skip to content

Commit 25c67f4

Browse files
[ResponseOps][MaintenanceWindows] Expose an internal maintenance windows client (#247390)
## Summary Fixes #246200 by creating MW client using SO's internal repository. It is used in synthetics plugin. ### Checklist Check the PR satisfies following conditions. - [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
1 parent 42e97b3 commit 25c67f4

File tree

10 files changed

+103
-40
lines changed

10 files changed

+103
-40
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -671,11 +671,11 @@ export class AlertingPlugin {
671671
return rulesSettingsClientFactory!.create(request);
672672
};
673673

674-
const getMaintenanceWindowClientInternal = (request: KibanaRequest) => {
674+
const getMaintenanceWindowClient = (request: KibanaRequest) => {
675675
if (!plugins.maintenanceWindows) {
676676
return;
677677
}
678-
return plugins.maintenanceWindows.getMaintenanceWindowClientInternal(request);
678+
return plugins.maintenanceWindows.getMaintenanceWindowClientWithoutAuth(request);
679679
};
680680

681681
taskRunnerFactory.initialize({
@@ -697,7 +697,7 @@ export class AlertingPlugin {
697697
maintenanceWindowsService: new MaintenanceWindowsService({
698698
cacheInterval: this.config.rulesSettings.cacheInterval,
699699
logger,
700-
getMaintenanceWindowClientInternal,
700+
getMaintenanceWindowClient,
701701
}),
702702
maxAlerts: this.config.rules.run.alerts.max,
703703
ruleTypeRegistry: this.ruleTypeRegistry!,

x-pack/platform/plugins/shared/alerting/server/task_runner/maintenance_windows/maintenance_windows_service.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ describe('MaintenanceWindowsService', () => {
6969
test('should load maintenance windows if none in cache', async () => {
7070
maintenanceWindowClient.getActiveMaintenanceWindows.mockResolvedValueOnce(maintenanceWindows);
7171
const maintenanceWindowsService = new MaintenanceWindowsService({
72-
getMaintenanceWindowClientInternal: jest.fn().mockReturnValue(maintenanceWindowClient),
72+
getMaintenanceWindowClient: jest.fn().mockReturnValue(maintenanceWindowClient),
7373
logger,
7474
});
7575
// @ts-ignore - accessing private variable
@@ -103,7 +103,7 @@ describe('MaintenanceWindowsService', () => {
103103
throw new Error('Test error');
104104
});
105105
const maintenanceWindowsService = new MaintenanceWindowsService({
106-
getMaintenanceWindowClientInternal: jest.fn().mockReturnValue(maintenanceWindowClient),
106+
getMaintenanceWindowClient: jest.fn().mockReturnValue(maintenanceWindowClient),
107107
logger,
108108
});
109109
// @ts-ignore - accessing private variable
@@ -139,7 +139,7 @@ describe('MaintenanceWindowsService', () => {
139139
maintenanceWindowClient.getActiveMaintenanceWindows.mockResolvedValueOnce(maintenanceWindows);
140140
maintenanceWindowClient.getActiveMaintenanceWindows.mockResolvedValueOnce(newSpaceMW);
141141
const maintenanceWindowsService = new MaintenanceWindowsService({
142-
getMaintenanceWindowClientInternal: jest.fn().mockReturnValue(maintenanceWindowClient),
142+
getMaintenanceWindowClient: jest.fn().mockReturnValue(maintenanceWindowClient),
143143
logger,
144144
});
145145
// @ts-ignore - accessing private variable
@@ -190,7 +190,7 @@ describe('MaintenanceWindowsService', () => {
190190
test('should use cached windows if cache has not expired', async () => {
191191
maintenanceWindowClient.getActiveMaintenanceWindows.mockResolvedValueOnce(maintenanceWindows);
192192
const maintenanceWindowsService = new MaintenanceWindowsService({
193-
getMaintenanceWindowClientInternal: jest.fn().mockReturnValue(maintenanceWindowClient),
193+
getMaintenanceWindowClient: jest.fn().mockReturnValue(maintenanceWindowClient),
194194
logger,
195195
});
196196

@@ -226,7 +226,7 @@ describe('MaintenanceWindowsService', () => {
226226
maintenanceWindows[0],
227227
]);
228228
const maintenanceWindowsService = new MaintenanceWindowsService({
229-
getMaintenanceWindowClientInternal: jest.fn().mockReturnValue(maintenanceWindowClient),
229+
getMaintenanceWindowClient: jest.fn().mockReturnValue(maintenanceWindowClient),
230230
logger,
231231
});
232232

@@ -263,7 +263,7 @@ describe('MaintenanceWindowsService', () => {
263263
throw new Error('Test error');
264264
});
265265
const maintenanceWindowsService = new MaintenanceWindowsService({
266-
getMaintenanceWindowClientInternal: jest.fn().mockReturnValue(maintenanceWindowClient),
266+
getMaintenanceWindowClient: jest.fn().mockReturnValue(maintenanceWindowClient),
267267
logger,
268268
});
269269

@@ -303,7 +303,7 @@ describe('MaintenanceWindowsService', () => {
303303
];
304304
maintenanceWindowClient.getActiveMaintenanceWindows.mockResolvedValueOnce(mw);
305305
const maintenanceWindowsService = new MaintenanceWindowsService({
306-
getMaintenanceWindowClientInternal: jest.fn().mockReturnValue(maintenanceWindowClient),
306+
getMaintenanceWindowClient: jest.fn().mockReturnValue(maintenanceWindowClient),
307307
logger,
308308
});
309309

@@ -353,7 +353,7 @@ describe('MaintenanceWindowsService', () => {
353353
}));
354354
maintenanceWindowClient.getActiveMaintenanceWindows.mockResolvedValueOnce(mw);
355355
const maintenanceWindowsService = new MaintenanceWindowsService({
356-
getMaintenanceWindowClientInternal: jest.fn().mockReturnValue(maintenanceWindowClient),
356+
getMaintenanceWindowClient: jest.fn().mockReturnValue(maintenanceWindowClient),
357357
logger,
358358
});
359359

@@ -407,7 +407,7 @@ describe('MaintenanceWindowsService', () => {
407407
];
408408
maintenanceWindowClient.getActiveMaintenanceWindows.mockResolvedValueOnce(mw);
409409
const maintenanceWindowsService = new MaintenanceWindowsService({
410-
getMaintenanceWindowClientInternal: jest.fn().mockReturnValue(maintenanceWindowClient),
410+
getMaintenanceWindowClient: jest.fn().mockReturnValue(maintenanceWindowClient),
411411
logger,
412412
});
413413

x-pack/platform/plugins/shared/alerting/server/task_runner/maintenance_windows/maintenance_windows_service.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ export const DEFAULT_CACHE_INTERVAL_MS = 60000; // 1 minute cache
1616

1717
interface MaintenanceWindowServiceOpts {
1818
cacheInterval?: number;
19-
getMaintenanceWindowClientInternal: (
20-
request: KibanaRequest
21-
) => MaintenanceWindowClient | undefined;
19+
getMaintenanceWindowClient: (request: KibanaRequest) => MaintenanceWindowClient | undefined;
2220
logger: Logger;
2321
}
2422

@@ -135,7 +133,7 @@ export class MaintenanceWindowsService {
135133
now: number
136134
): Promise<MaintenanceWindow[]> {
137135
return await withAlertingSpan('alerting:load-maintenance-windows', async () => {
138-
const maintenanceWindowClient = this.options.getMaintenanceWindowClientInternal(request);
136+
const maintenanceWindowClient = this.options.getMaintenanceWindowClient(request);
139137
const activeMaintenanceWindows = maintenanceWindowClient
140138
? await maintenanceWindowClient.getActiveMaintenanceWindows(this.cacheIntervalMs)
141139
: [];

x-pack/platform/plugins/shared/maintenance_windows/server/maintenance_window_client_factory.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ test('creates an unauthorized maintenance window client', async () => {
8787

8888
savedObjectsService.getScopedClient.mockReturnValue(savedObjectsClient);
8989

90-
factory.create(request);
90+
factory.createWithoutAuthorization(request);
9191

9292
expect(savedObjectsService.getScopedClient).toHaveBeenCalledWith(request, {
9393
excludedExtensions: [SECURITY_EXTENSION_ID],
@@ -103,6 +103,26 @@ test('creates an unauthorized maintenance window client', async () => {
103103
});
104104
});
105105

106+
test('creates an internal maintenance window client', async () => {
107+
const factory = new MaintenanceWindowClientFactory();
108+
factory.initialize(maintenanceWindowClientFactoryParams);
109+
const request = mockRouter.createKibanaRequest();
110+
const mockRepository = savedObjectsService.createInternalRepository();
111+
savedObjectsService.createInternalRepository.mockReturnValue(mockRepository);
112+
113+
factory.createInternal(request);
114+
115+
expect(savedObjectsService.createInternalRepository).toHaveBeenCalledWith();
116+
117+
const { MaintenanceWindowClient } = jest.requireMock('./client');
118+
119+
expect(MaintenanceWindowClient).toHaveBeenCalledWith({
120+
logger: maintenanceWindowClientFactoryParams.logger,
121+
savedObjectsClient: mockRepository,
122+
getUserName: expect.any(Function),
123+
});
124+
});
125+
106126
test('getUserName() returns null when security is disabled', async () => {
107127
const factory = new MaintenanceWindowClientFactory();
108128
factory.initialize(maintenanceWindowClientFactoryParams);

x-pack/platform/plugins/shared/maintenance_windows/server/maintenance_window_client_factory.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import type {
99
KibanaRequest,
1010
Logger,
11+
SavedObjectsClientContract,
1112
SavedObjectsServiceStart,
1213
SecurityServiceStart,
1314
UiSettingsServiceStart,
@@ -41,19 +42,18 @@ export class MaintenanceWindowClientFactory {
4142
this.uiSettings = options.uiSettings;
4243
}
4344

45+
private getSoClient(request: KibanaRequest, withAuth: boolean): SavedObjectsClientContract {
46+
return this.savedObjectsService.getScopedClient(request, {
47+
includedHiddenTypes: [MAINTENANCE_WINDOW_SAVED_OBJECT_TYPE],
48+
...(withAuth ? {} : { excludedExtensions: [SECURITY_EXTENSION_ID] }),
49+
});
50+
}
51+
4452
private createMaintenanceWindowClient(
4553
request: KibanaRequest,
46-
withAuth: boolean,
47-
excludedExtensions?: ['spaces']
54+
savedObjectsClient: SavedObjectsClientContract
4855
) {
4956
const { securityService } = this;
50-
const savedObjectsClient = this.savedObjectsService.getScopedClient(request, {
51-
includedHiddenTypes: [MAINTENANCE_WINDOW_SAVED_OBJECT_TYPE],
52-
...(withAuth
53-
? {}
54-
: { excludedExtensions: [...(excludedExtensions ?? []), SECURITY_EXTENSION_ID] }),
55-
});
56-
5757
const uiSettingClient = this.uiSettings.asScopedToClient(savedObjectsClient);
5858

5959
return new MaintenanceWindowClient({
@@ -68,10 +68,17 @@ export class MaintenanceWindowClientFactory {
6868
}
6969

7070
public createWithAuthorization(request: KibanaRequest) {
71-
return this.createMaintenanceWindowClient(request, true);
71+
const soClient = this.getSoClient(request, true);
72+
return this.createMaintenanceWindowClient(request, soClient);
73+
}
74+
75+
public createWithoutAuthorization(request: KibanaRequest) {
76+
const soClient = this.getSoClient(request, false);
77+
return this.createMaintenanceWindowClient(request, soClient);
7278
}
7379

74-
public create(request: KibanaRequest, excludedExtension?: ['spaces']) {
75-
return this.createMaintenanceWindowClient(request, false, excludedExtension);
80+
public createInternal(request: KibanaRequest) {
81+
const savedObjectsInternalClient = this.savedObjectsService.createInternalRepository();
82+
return this.createMaintenanceWindowClient(request, savedObjectsInternalClient);
7683
}
7784
}

x-pack/platform/plugins/shared/maintenance_windows/server/mocks.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ const createStartMock = () => {
1717
getMaintenanceWindowClientWithAuth: jest
1818
.fn()
1919
.mockResolvedValue(maintenanceWindowClientMock.create()),
20+
getMaintenanceWindowClientWithoutAuth: jest
21+
.fn()
22+
.mockResolvedValue(maintenanceWindowClientMock.create()),
2023
});
2124

2225
return mock;

x-pack/platform/plugins/shared/maintenance_windows/server/plugin.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,40 @@ describe('Maintenance Windows Plugin', () => {
9292
expect(client).toBeDefined();
9393
});
9494

95+
test(`exposes getMaintenanceWindowClientWithoutAuth()`, async () => {
96+
const context = coreMock.createPluginInitializerContext();
97+
const plugin = new MaintenanceWindowsPlugin(context);
98+
99+
plugin.setup(coreMock.createSetup(), {
100+
licensing: licensingMock.createSetup(),
101+
taskManager: taskManagerMock.createSetup(),
102+
features: featuresPluginMock.createSetup(),
103+
});
104+
105+
const startContract = plugin.start(coreMock.createStart(), {
106+
taskManager: taskManagerMock.createStart(),
107+
});
108+
109+
const fakeRequest = {
110+
headers: {},
111+
getBasePath: () => '',
112+
path: '/',
113+
route: { settings: {} },
114+
url: {
115+
href: '/',
116+
},
117+
raw: {
118+
req: {
119+
url: '/',
120+
},
121+
},
122+
getSavedObjectsClient: jest.fn(),
123+
} as unknown as KibanaRequest;
124+
125+
const client = startContract.getMaintenanceWindowClientWithoutAuth(fakeRequest);
126+
expect(client).toBeDefined();
127+
});
128+
95129
test(`exposes getMaintenanceWindowClientInternal()`, async () => {
96130
const context = coreMock.createPluginInitializerContext();
97131
const plugin = new MaintenanceWindowsPlugin(context);

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,23 @@ export class MaintenanceWindowsPlugin
107107
return maintenanceWindowClientFactory.createWithAuthorization(request);
108108
};
109109

110+
const getMaintenanceWindowClientWithoutAuth = (
111+
request: KibanaRequest
112+
): MaintenanceWindowClientApi => {
113+
return maintenanceWindowClientFactory.createWithoutAuthorization(request);
114+
};
115+
110116
const getMaintenanceWindowClientInternal = (
111-
request: KibanaRequest,
112-
excludedExtension?: ['spaces']
117+
request: KibanaRequest
113118
): MaintenanceWindowClientApi => {
114-
return maintenanceWindowClientFactory.create(request, excludedExtension);
119+
return maintenanceWindowClientFactory.createInternal(request);
115120
};
116121

117122
scheduleMaintenanceWindowEventsGenerator(this.logger, plugins.taskManager).catch(() => {});
118123

119124
return {
120125
getMaintenanceWindowClientWithAuth,
126+
getMaintenanceWindowClientWithoutAuth,
121127
getMaintenanceWindowClientInternal,
122128
};
123129
}

x-pack/platform/plugins/shared/maintenance_windows/server/types.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ export interface MaintenanceWindowsServerStartDependencies {
3838
}
3939

4040
export interface MaintenanceWindowsServerStart {
41-
getMaintenanceWindowClientInternal(
42-
request: KibanaRequest,
43-
excludedExtension?: string[]
44-
): MaintenanceWindowClientApi;
41+
getMaintenanceWindowClientInternal(request: KibanaRequest): MaintenanceWindowClientApi;
4542
getMaintenanceWindowClientWithAuth(request: KibanaRequest): MaintenanceWindowClientApi;
43+
getMaintenanceWindowClientWithoutAuth(request: KibanaRequest): MaintenanceWindowClientApi;
4644
}

x-pack/solutions/observability/plugins/synthetics/server/plugin.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import type {
1313
SavedObjectsClientContract,
1414
KibanaRequest,
1515
} from '@kbn/core/server';
16-
import { SPACES_EXTENSION_ID } from '@kbn/core/server';
1716
import { SavedObjectsClient } from '@kbn/core/server';
1817
import { mappingFromFieldMap } from '@kbn/alerting-plugin/common';
1918
import { Dataset } from '@kbn/rule-registry-plugin/server';
@@ -130,9 +129,7 @@ export class Plugin implements PluginType {
130129
return;
131130
}
132131

133-
return pluginsStart.maintenanceWindows?.getMaintenanceWindowClientInternal(request, [
134-
SPACES_EXTENSION_ID,
135-
]);
132+
return pluginsStart.maintenanceWindows?.getMaintenanceWindowClientInternal(request);
136133
};
137134

138135
if (this.server) {

0 commit comments

Comments
 (0)