isClusterOptedIn should fallback to true when not found (#94980) (#95233)

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
# Conflicts:
#	src/plugins/telemetry_collection_manager/server/plugin.test.ts
#	test/api_integration/apis/telemetry/telemetry_local.ts
This commit is contained in:
Alejandro Fernández Haro 2021-03-23 22:30:30 +01:00 committed by GitHub
parent e7d73b1a9f
commit b99c242e00
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 39 additions and 22 deletions

View file

@ -22,6 +22,11 @@ describe('getKID', () => {
const kid = getKID(useProdKey);
expect(kid).toBe('kibana1');
});
it(`should fallback to development`, async () => {
const kid = getKID();
expect(kid).toBe('kibana_dev1');
});
});
describe('encryptTelemetry', () => {
@ -46,4 +51,10 @@ describe('encryptTelemetry', () => {
await encryptTelemetry(payload, { useProdKey: false });
expect(mockEncrypt).toBeCalledWith('kibana_dev1', payload);
});
it('should fallback to { useProdKey: false }', async () => {
const payload = { some: 'value' };
await encryptTelemetry(payload);
expect(mockEncrypt).toBeCalledWith('kibana_dev1', payload);
});
});

View file

@ -28,7 +28,6 @@ import {
UsageStatsPayload,
StatsCollectionContext,
} from './types';
import { isClusterOptedIn } from './util';
import { encryptTelemetry } from './encryption';
interface TelemetryCollectionPluginsDepsSetup {
@ -203,7 +202,7 @@ export class TelemetryCollectionManagerPlugin
return usageData;
}
return encryptTelemetry(usageData.filter(isClusterOptedIn), {
return await encryptTelemetry(usageData, {
useProdKey: this.isDistributable,
});
}

View file

@ -1,11 +0,0 @@
/*
* 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.
*/
export const isClusterOptedIn = (clusterUsage: any): boolean => {
return clusterUsage?.stack_stats?.kibana?.plugins?.telemetry?.opt_in_status === true;
};

View file

@ -9,6 +9,7 @@ import { StatsGetter } from 'src/plugins/telemetry_collection_manager/server';
import { TelemetryLocalStats, getLocalStats } from '../../../../../src/plugins/telemetry/server';
import { getXPackUsage } from './get_xpack';
import { ESLicense, getLicenseFromLocalOrMaster } from './get_license';
import { isClusterOptedIn } from './is_cluster_opted_in';
export type TelemetryAggregatedStats = TelemetryLocalStats & {
stack_stats: { xpack?: object };
@ -48,6 +49,10 @@ export const getStatsWithXpack: StatsGetter<TelemetryAggregatedStats> = async fu
if (monitoringTelemetry) {
delete stats.stack_stats.kibana!.plugins.monitoringTelemetry;
}
return [...acc, stats, ...(monitoringTelemetry || [])];
// From the monitoring-sourced telemetry, we need to filter out the clusters that are opted-out.
const onlyOptedInMonitoringClusters = (monitoringTelemetry || []).filter(isClusterOptedIn);
return [...acc, stats, ...onlyOptedInMonitoringClusters];
}, [] as TelemetryAggregatedStats[]);
};

View file

@ -1,12 +1,11 @@
/*
* 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.
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { isClusterOptedIn } from './util';
import { isClusterOptedIn } from './is_cluster_opted_in';
const createMockClusterUsage = (plugins: any) => {
return {
@ -32,9 +31,9 @@ describe('isClusterOptedIn', () => {
const result = isClusterOptedIn(mockClusterUsage);
expect(result).toBe(false);
});
it('returns false if cluster stats is malformed', () => {
expect(isClusterOptedIn(createMockClusterUsage({}))).toBe(false);
expect(isClusterOptedIn({})).toBe(false);
expect(isClusterOptedIn(undefined)).toBe(false);
it('returns true if kibana.plugins.telemetry does not exist', () => {
expect(isClusterOptedIn(createMockClusterUsage({}))).toBe(true);
expect(isClusterOptedIn({})).toBe(true);
expect(isClusterOptedIn(undefined)).toBe(true);
});
});

View file

@ -0,0 +1,14 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
export const isClusterOptedIn = (clusterUsage: any): boolean => {
return (
clusterUsage?.stack_stats?.kibana?.plugins?.telemetry?.opt_in_status === true ||
// If stack_stats.kibana.plugins.telemetry does not exist, assume opted-in for BWC
!clusterUsage?.stack_stats?.kibana?.plugins?.telemetry
);
};