From 49f5998c88189c735129c573571f4b954dcb6f3c Mon Sep 17 00:00:00 2001 From: Ahmad Bamieh Date: Tue, 14 Jul 2020 13:33:03 +0300 Subject: [PATCH 1/2] add .filter --- .../server/plugin.ts | 6 ++- .../server/util.test.ts | 51 +++++++++++++++++++ .../server/util.ts | 22 ++++++++ 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 src/plugins/telemetry_collection_manager/server/util.test.ts create mode 100644 src/plugins/telemetry_collection_manager/server/util.ts diff --git a/src/plugins/telemetry_collection_manager/server/plugin.ts b/src/plugins/telemetry_collection_manager/server/plugin.ts index c3db0ca39e6ac..6223d2c916bb5 100644 --- a/src/plugins/telemetry_collection_manager/server/plugin.ts +++ b/src/plugins/telemetry_collection_manager/server/plugin.ts @@ -37,7 +37,7 @@ import { UsageStatsPayload, StatsCollectionContext, } from './types'; - +import { isClusterOptedIn } from './util'; import { encryptTelemetry } from './encryption'; interface TelemetryCollectionPluginsDepsSetup { @@ -240,7 +240,7 @@ export class TelemetryCollectionManagerPlugin collection.licenseGetter(clustersDetails, statsCollectionConfig, context), ]); - return stats.map((stat) => { + const clustersUsage = stats.map((stat) => { const license = licenses[stat.cluster_uuid]; return { ...(license ? { license } : {}), @@ -248,5 +248,7 @@ export class TelemetryCollectionManagerPlugin collectionSource: collection.title, }; }); + + return clustersUsage.filter(isClusterOptedIn); } } diff --git a/src/plugins/telemetry_collection_manager/server/util.test.ts b/src/plugins/telemetry_collection_manager/server/util.test.ts new file mode 100644 index 0000000000000..ba5d999c3bf9a --- /dev/null +++ b/src/plugins/telemetry_collection_manager/server/util.test.ts @@ -0,0 +1,51 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { isClusterOptedIn } from './util'; + +const createMockClusterUsage = (plugins: any) => { + return { + stack_stats: { + kibana: { plugins }, + }, + }; +}; + +describe('isClusterOptedIn', () => { + it('returns true if cluster has opt_in_status: true', () => { + const mockClusterUsage = createMockClusterUsage({ telemetry: { opt_in_status: true } }); + const result = isClusterOptedIn(mockClusterUsage); + expect(result).toBe(true); + }); + it('returns false if cluster has opt_in_status: false', () => { + const mockClusterUsage = createMockClusterUsage({ telemetry: { opt_in_status: false } }); + const result = isClusterOptedIn(mockClusterUsage); + expect(result).toBe(false); + }); + it('returns false if cluster has opt_in_status: undefined', () => { + const mockClusterUsage = createMockClusterUsage({ telemetry: {} }); + 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); + }); +}); diff --git a/src/plugins/telemetry_collection_manager/server/util.ts b/src/plugins/telemetry_collection_manager/server/util.ts new file mode 100644 index 0000000000000..d6e1b51663688 --- /dev/null +++ b/src/plugins/telemetry_collection_manager/server/util.ts @@ -0,0 +1,22 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export const isClusterOptedIn = (clusterUsage: any): boolean => { + return clusterUsage?.stack_stats?.kibana?.plugins?.telemetry?.opt_in_status === true; +}; From e5b501945f8846dddd45acfa3bff0a1d1e6803b2 Mon Sep 17 00:00:00 2001 From: Ahmad Bamieh Date: Tue, 14 Jul 2020 15:08:41 +0300 Subject: [PATCH 2/2] only before encryption --- src/plugins/telemetry_collection_manager/server/plugin.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/plugins/telemetry_collection_manager/server/plugin.ts b/src/plugins/telemetry_collection_manager/server/plugin.ts index 6223d2c916bb5..051bb3a11cb16 100644 --- a/src/plugins/telemetry_collection_manager/server/plugin.ts +++ b/src/plugins/telemetry_collection_manager/server/plugin.ts @@ -205,7 +205,9 @@ export class TelemetryCollectionManagerPlugin return usageData; } - return encryptTelemetry(usageData, { useProdKey: this.isDistributable }); + return encryptTelemetry(usageData.filter(isClusterOptedIn), { + useProdKey: this.isDistributable, + }); } } catch (err) { this.logger.debug( @@ -240,7 +242,7 @@ export class TelemetryCollectionManagerPlugin collection.licenseGetter(clustersDetails, statsCollectionConfig, context), ]); - const clustersUsage = stats.map((stat) => { + return stats.map((stat) => { const license = licenses[stat.cluster_uuid]; return { ...(license ? { license } : {}), @@ -248,7 +250,5 @@ export class TelemetryCollectionManagerPlugin collectionSource: collection.title, }; }); - - return clustersUsage.filter(isClusterOptedIn); } }