Skip to content

Commit

Permalink
[Telemetry] Default to namespaces:['*'] in soClient.find requests
Browse files Browse the repository at this point in the history
  • Loading branch information
afharo committed Mar 2, 2021
1 parent 9fd28b6 commit 1cfefed
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
createCollectorFetchContextMock,
} from '../../../usage_collection/server/mocks';
import { elasticsearchServiceMock, httpServerMock } from '../../../../../src/core/server/mocks';
import { StatsCollectionConfig } from '../../../telemetry_collection_manager/server';

function mockUsageCollection(kibanaUsage = {}) {
const usageCollection = usageCollectionPluginMock.createSetupContract();
Expand Down Expand Up @@ -69,13 +70,16 @@ function mockGetLocalStats(clusterInfo: any, clusterStats: any) {
return esClient;
}

function mockStatsCollectionConfig(clusterInfo: any, clusterStats: any, kibana: {}) {
function mockStatsCollectionConfig(
clusterInfo: any,
clusterStats: any,
kibana: {}
): StatsCollectionConfig {
return {
...createCollectorFetchContextMock(),
esClient: mockGetLocalStats(clusterInfo, clusterStats),
usageCollection: mockUsageCollection(kibana),
kibanaRequest: httpServerMock.createKibanaRequest(),
timestamp: Date.now(),
};
}

Expand Down Expand Up @@ -227,7 +231,7 @@ describe('get_local_stats', () => {
const statsCollectionConfig = mockStatsCollectionConfig(clusterInfo, clusterStats, kibana);
const response = await getLocalStats(
[{ clusterUuid: 'abc123' }],
{ ...statsCollectionConfig },
statsCollectionConfig,
context
);
const result = response[0];
Expand All @@ -243,7 +247,7 @@ describe('get_local_stats', () => {

it('returns an empty array when no cluster uuid is provided', async () => {
const statsCollectionConfig = mockStatsCollectionConfig(clusterInfo, clusterStats, kibana);
const response = await getLocalStats([], { ...statsCollectionConfig }, context);
const response = await getLocalStats([], statsCollectionConfig, context);
expect(response).toBeDefined();
expect(response.length).toEqual(0);
});
Expand Down
6 changes: 4 additions & 2 deletions src/plugins/telemetry_collection_manager/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
} from './types';
import { isClusterOptedIn } from './util';
import { encryptTelemetry } from './encryption';
import { TelemetrySavedObjectsClient } from './telemetry_saved_objects_client';

interface TelemetryCollectionPluginsDepsSetup {
usageCollection: UsageCollectionSetup;
Expand Down Expand Up @@ -120,9 +121,10 @@ export class TelemetryCollectionManagerPlugin
? this.elasticsearchClient?.asScoped(config.request).asCurrentUser
: this.elasticsearchClient?.asInternalUser;
// Scope the saved objects client appropriately and pass to the stats collection config
const soClient = config.unencrypted
? this.savedObjectsService?.getScopedClient(config.request)
const soRepository = config.unencrypted
? this.savedObjectsService?.createScopedRepository(config.request)
: this.savedObjectsService?.createInternalRepository();
const soClient = soRepository && new TelemetrySavedObjectsClient(soRepository);
// Provide the kibanaRequest so opted-in plugins can scope their custom clients only if the request is not encrypted
const kibanaRequest = config.unencrypted ? config.request : void 0;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { SavedObjectsFindOptions, SavedObjectsFindResponse } from 'src/core/server';
import { SavedObjectsClient } from '../../../core/server';

/**
* Extends the SavedObjectsClient to fit the telemetry fetching requirements (i.e.: find objects from all namespaces by default)
*/
export class TelemetrySavedObjectsClient extends SavedObjectsClient {
/**
* Find the SavedObjects matching the search query in all the Spaces by default
* @param options
*/
async find<T = unknown>(options: SavedObjectsFindOptions): Promise<SavedObjectsFindResponse<T>> {
return super.find({ namespaces: ['*'], ...options });
}
}
3 changes: 1 addition & 2 deletions src/plugins/telemetry_collection_manager/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
Logger,
KibanaRequest,
SavedObjectsClientContract,
ISavedObjectsRepository,
} from 'src/core/server';
import { UsageCollectionSetup } from 'src/plugins/usage_collection/server';
import { TelemetryCollectionManagerPlugin } from './plugin';
Expand Down Expand Up @@ -57,7 +56,7 @@ export interface ClusterDetails {
export interface StatsCollectionConfig {
usageCollection: UsageCollectionSetup;
esClient: ElasticsearchClient;
soClient: SavedObjectsClientContract | ISavedObjectsRepository;
soClient: SavedObjectsClientContract;
kibanaRequest: KibanaRequest | undefined; // intentionally `| undefined` to enforce providing the parameter
}

Expand Down
5 changes: 2 additions & 3 deletions src/plugins/usage_collection/server/collector/collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
* Side Public License, v 1.
*/

import {
import type {
Logger,
ElasticsearchClient,
ISavedObjectsRepository,
SavedObjectsClientContract,
KibanaRequest,
} from 'src/core/server';
Expand Down Expand Up @@ -72,7 +71,7 @@ export type CollectorFetchContext<WithKibanaRequest extends boolean | undefined
* Request-scoped Saved Objects client
* @remark Bear in mind when testing your collector that your user has the same privileges as the Kibana Internal user to ensure the expected data is sent to the remote cluster (more info: {@link CollectorFetchContext})
*/
soClient: SavedObjectsClientContract | ISavedObjectsRepository;
soClient: SavedObjectsClientContract;
} & (WithKibanaRequest extends true
? {
/**
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/usage_collection/server/usage_collection.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
elasticsearchServiceMock,
httpServerMock,
loggingSystemMock,
savedObjectsRepositoryMock,
savedObjectsClientMock,
} from '../../../../src/core/server/mocks';

import { CollectorOptions, Collector, UsageCollector } from './collector';
Expand Down Expand Up @@ -40,7 +40,7 @@ export const createUsageCollectionSetupMock = () => {
export function createCollectorFetchContextMock(): jest.Mocked<CollectorFetchContext<false>> {
const collectorFetchClientsMock: jest.Mocked<CollectorFetchContext<false>> = {
esClient: elasticsearchServiceMock.createClusterClient().asInternalUser,
soClient: savedObjectsRepositoryMock.create(),
soClient: savedObjectsClientMock.create(),
};
return collectorFetchClientsMock;
}
Expand All @@ -50,7 +50,7 @@ export function createCollectorFetchContextWithKibanaMock(): jest.Mocked<
> {
const collectorFetchClientsMock: jest.Mocked<CollectorFetchContext<true>> = {
esClient: elasticsearchServiceMock.createClusterClient().asInternalUser,
soClient: savedObjectsRepositoryMock.create(),
soClient: savedObjectsClientMock.create(),
kibanaRequest: httpServerMock.createKibanaRequest(),
};
return collectorFetchClientsMock;
Expand Down

0 comments on commit 1cfefed

Please sign in to comment.