Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,101 @@ describe('MonitorConfigRepository', () => {
});
});

describe('getAcrossSpaces', () => {
it('issues a single multi-space lookup and one legacy lookup per namespace', async () => {
const id = 'test-id';
const namespaces = ['default', 'space-two'];
const mockMonitor = {
id,
attributes: { name: 'Test Monitor' },
type: syntheticsMonitorSavedObjectType,
references: [],
};
soClient.bulkGet.mockResolvedValue({ saved_objects: [mockMonitor] });

const result = await repository.getAcrossSpaces(id, namespaces);

expect(soClient.bulkGet).toHaveBeenCalledWith([
{ type: syntheticsMonitorSavedObjectType, id, namespaces: ['default', 'space-two'] },
{ type: legacySyntheticsMonitorTypeSingle, id, namespaces: ['default'] },
{ type: legacySyntheticsMonitorTypeSingle, id, namespaces: ['space-two'] },
]);
expect(result).toBe(mockMonitor);
});

it('returns the first saved object that has attributes and no error', async () => {
const id = 'test-id';
const errored = {
id,
type: syntheticsMonitorSavedObjectType,
attributes: {},
references: [],
error: { statusCode: 404, error: 'Not Found', message: 'not found' },
};
const found = {
id,
type: legacySyntheticsMonitorTypeSingle,
attributes: { name: 'Legacy' },
references: [],
};
soClient.bulkGet.mockResolvedValue({ saved_objects: [errored as any, found] });

const result = await repository.getAcrossSpaces(id, ['default']);

expect(result).toBe(found);
});

it('throws not-found when no namespace has the monitor', async () => {
const id = 'missing-id';
soClient.bulkGet.mockResolvedValue({
saved_objects: [
{
id,
type: syntheticsMonitorSavedObjectType,
error: { statusCode: 404, error: 'Not Found', message: 'not found' },
},
],
} as any);

await expect(repository.getAcrossSpaces(id, ['default'])).rejects.toMatchObject({
output: { statusCode: 404 },
});
});

it('deduplicates the namespaces array', async () => {
const id = 'dup-id';
soClient.bulkGet.mockResolvedValue({
saved_objects: [
{ id, type: syntheticsMonitorSavedObjectType, attributes: {}, references: [] },
],
} as any);

await repository.getAcrossSpaces(id, ['default', 'default', 'space-two']);

const calledWith = soClient.bulkGet.mock.calls[0][0];
expect(calledWith).toEqual([
{ type: syntheticsMonitorSavedObjectType, id, namespaces: ['default', 'space-two'] },
{ type: legacySyntheticsMonitorTypeSingle, id, namespaces: ['default'] },
{ type: legacySyntheticsMonitorTypeSingle, id, namespaces: ['space-two'] },
]);
});

it('uses the supplied saved objects client when provided', async () => {
const id = 'test-id';
const altClient = savedObjectsClientMock.create();
altClient.bulkGet.mockResolvedValue({
saved_objects: [
{ id, type: syntheticsMonitorSavedObjectType, attributes: {}, references: [] },
],
} as any);

await repository.getAcrossSpaces(id, ['default'], altClient);

expect(altClient.bulkGet).toHaveBeenCalledTimes(1);
expect(soClient.bulkGet).not.toHaveBeenCalled();
});
});

describe('getDecrypted', () => {
it('should get and decrypt a monitor by id and space', async () => {
const id = 'test-id';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
*/

import type {
ISavedObjectsRepository,
SavedObject,
SavedObjectReference,
SavedObjectsBulkGetObject,
SavedObjectsClientContract,
SavedObjectsFindOptions,
SavedObjectsFindResult,
Expand Down Expand Up @@ -75,6 +77,46 @@ export class MonitorConfigRepository {
return resolved;
}

/**
* Look up a monitor by id across the supplied spaces.
*
* Required for cross-space callers (e.g. the monitor health API) because
* `get` is bound to the request-scoped saved objects client and therefore
* only ever sees the request's space — see Kibana issue #270477.
*
* The multi-space type (`syntheticsMonitorSavedObjectType`,
* `namespaceType: 'multiple'`) supports a per-object `namespaces` array, so
* a single entry covers all spaces. The legacy type
* (`legacySyntheticsMonitorTypeSingle`, `namespaceType: 'single'`) only
* accepts one namespace per object, so we add one entry per space.
*/
async getAcrossSpaces(
id: string,
namespaces: string[],
soClient: SavedObjectsClientContract | ISavedObjectsRepository = this.soClient
): Promise<SavedObject<EncryptedSyntheticsMonitorAttributes>> {
const uniqueNamespaces = [...new Set(namespaces)];
const bulkObjects: SavedObjectsBulkGetObject[] = [
{ type: syntheticsMonitorSavedObjectType, id, namespaces: uniqueNamespaces },
...uniqueNamespaces.map((namespace) => ({
type: legacySyntheticsMonitorTypeSingle,
id,
namespaces: [namespace],
})),
];
const { saved_objects: results } = await soClient.bulkGet<EncryptedSyntheticsMonitorAttributes>(
bulkObjects
);
const resolved = results.find((obj) => obj?.attributes && !obj.error);
if (!resolved) {
throw SavedObjectsErrorHelpers.createGenericNotFoundError(
syntheticsMonitorSavedObjectType,
id
);
}
return resolved;
}

async getDecrypted(
id: string,
spaceId: string
Expand Down
Loading
Loading