Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Telemetry] Settings Collector: redact sensitive reported values #88675

Merged
merged 18 commits into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ import { UsageStats } from './telemetry_management_collector';
// Retrieved by changing all the current settings in Kibana (we'll need to revisit it in the future).
// I would suggest we use flattened type for the mappings of this collector.
Bamieh marked this conversation as resolved.
Show resolved Hide resolved
export const stackManagementSchema: MakeSchemaFrom<UsageStats> = {
// blacklisted
'timelion:quandl.key': { type: 'boolean' },
'securitySolution:newsFeedUrl': { type: 'boolean' },
'securitySolution:defaultIndex': { type: 'boolean' },
'xpackReporting:customPdfLogo': { type: 'boolean' },
'notifications:banner': { type: 'boolean' },
// whitelisted
'visualize:enableLabs': { type: 'boolean' },
'visualization:heatmap:maxBuckets': { type: 'long' },
'visualization:colorMapping': { type: 'text' },
Expand All @@ -38,14 +45,11 @@ export const stackManagementSchema: MakeSchemaFrom<UsageStats> = {
'timelion:min_interval': { type: 'keyword' },
'timelion:default_rows': { type: 'long' },
'timelion:default_columns': { type: 'long' },
'timelion:quandl.key': { type: 'keyword' },
'timelion:es.default_index': { type: 'keyword' },
'timelion:showTutorial': { type: 'boolean' },
'securitySolution:timeDefaults': { type: 'keyword' },
'securitySolution:defaultAnomalyScore': { type: 'long' },
'securitySolution:defaultIndex': { type: 'keyword' }, // it's an array
'securitySolution:refreshIntervalDefaults': { type: 'keyword' },
'securitySolution:newsFeedUrl': { type: 'keyword' },
'securitySolution:enableNewsFeed': { type: 'boolean' },
'search:includeFrozen': { type: 'boolean' },
'courier:maxConcurrentShardRequests': { type: 'long' },
Expand All @@ -54,21 +58,29 @@ export const stackManagementSchema: MakeSchemaFrom<UsageStats> = {
'courier:customRequestPreference': { type: 'keyword' },
'courier:ignoreFilterIfFieldNotInIndex': { type: 'boolean' },
'rollups:enableIndexPatterns': { type: 'boolean' },
'xpackReporting:customPdfLogo': { type: 'text' },
'notifications:lifetime:warning': { type: 'long' },
'notifications:lifetime:banner': { type: 'long' },
'notifications:lifetime:info': { type: 'long' },
'notifications:banner': { type: 'text' },
'notifications:lifetime:error': { type: 'long' },
'doc_table:highlight': { type: 'boolean' },
'discover:searchOnPageLoad': { type: 'boolean' },
// eslint-disable-next-line @typescript-eslint/naming-convention
'doc_table:hideTimeColumn': { type: 'boolean' },
'discover:sampleSize': { type: 'long' },
defaultColumns: { type: 'keyword' }, // it's an array
defaultColumns: {
type: 'array',
items: {
type: 'keyword',
},
},
'context:defaultSize': { type: 'long' },
'discover:aggs:terms:size': { type: 'long' },
'context:tieBreakerFields': { type: 'keyword' }, // it's an array
'context:tieBreakerFields': {
type: 'array',
items: {
type: 'keyword',
},
},
'discover:sort:defaultOrder': { type: 'keyword' },
'context:step': { type: 'long' },
'accessibility:disableAnimations': { type: 'boolean' },
Expand All @@ -91,7 +103,12 @@ export const stackManagementSchema: MakeSchemaFrom<UsageStats> = {
pageNavigation: { type: 'keyword' },
'metrics:max_buckets': { type: 'long' },
'query:allowLeadingWildcards': { type: 'boolean' },
metaFields: { type: 'keyword' }, // it's an array
metaFields: {
type: 'array',
items: {
type: 'keyword',
},
},
'indexPattern:placeholder': { type: 'keyword' },
'histogram:barTarget': { type: 'long' },
'histogram:maxBars': { type: 'long' },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* 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 { loggingSystemMock, uiSettingsServiceMock } from '../../../../../core/server/mocks';
import {
Collector,
createUsageCollectionSetupMock,
createCollectorFetchContextMock,
} from '../../../../usage_collection/server/usage_collection.mock';

import {
registerManagementUsageCollector,
createCollectorFetch,
} from './telemetry_management_collector';

const logger = loggingSystemMock.createLogger();

describe('telemetry_application_usage_collector', () => {
let collector: Collector<unknown>;

const usageCollectionMock = createUsageCollectionSetupMock();
usageCollectionMock.makeUsageCollector.mockImplementation((config) => {
collector = new Collector(logger, config);
return createUsageCollectionSetupMock().makeUsageCollector(config);
});

const uiSettingsClient = uiSettingsServiceMock.createClient();
const getUiSettingsClient = jest.fn(() => uiSettingsClient);
const mockedFetchContext = createCollectorFetchContextMock();

beforeAll(() => {
registerManagementUsageCollector(usageCollectionMock, getUiSettingsClient);
});

test('registered collector is set', () => {
expect(collector).not.toBeUndefined();
});

test('isReady() => false if no client', () => {
getUiSettingsClient.mockImplementationOnce(() => undefined as any);
expect(collector.isReady()).toBe(false);
});

test('isReady() => true', () => {
expect(collector.isReady()).toBe(true);
});

test('fetch()', async () => {
uiSettingsClient.getUserProvided.mockImplementationOnce(async () => ({
'visualization:colorMapping': { userValue: 'red' },
}));
await expect(collector.fetch(mockedFetchContext)).resolves.toEqual({
'visualization:colorMapping': 'red',
});
});

test('fetch() should not fail if invoked when not ready', async () => {
getUiSettingsClient.mockImplementationOnce(() => undefined as any);
await expect(collector.fetch(mockedFetchContext)).resolves.toBe(undefined);
});
});

describe('createCollectorFetch', () => {
const mockUserSettings = {
item1: { userValue: 'test' },
item2: { userValue: 123 },
item3: { userValue: false },
};
const mockWhitelist = ['item1', 'item2'] as any[];

it('returns #fetchUsageStats function', () => {
const getUiSettingsClient = jest.fn(() => undefined);
const fetchFunction = createCollectorFetch(getUiSettingsClient, mockWhitelist);
expect(typeof fetchFunction).toBe('function');
});

describe('#fetchUsageStats', () => {
it('returns undefined if no uiSettingsClient returned from getUiSettingsClient', async () => {
const getUiSettingsClient = jest.fn(() => undefined);
const fetchFunction = createCollectorFetch(getUiSettingsClient, mockWhitelist);
const result = await fetchFunction();
expect(result).toBe(undefined);
expect(getUiSettingsClient).toBeCalledTimes(1);
});

it('returns all user changed settings', async () => {
const uiSettingsClient = uiSettingsServiceMock.createClient();
const getUiSettingsClient = jest.fn(() => uiSettingsClient);
uiSettingsClient.getUserProvided.mockResolvedValue(mockUserSettings);
const fetchFunction = createCollectorFetch(getUiSettingsClient, mockWhitelist);
const result = await fetchFunction();
expect(typeof result).toBe('object');
expect(Object.keys(result!)).toEqual(Object.keys(mockUserSettings));
});

it('returns values of whitelisted items', async () => {
const uiSettingsClient = uiSettingsServiceMock.createClient();
const getUiSettingsClient = jest.fn(() => uiSettingsClient);
uiSettingsClient.getUserProvided.mockResolvedValue(mockUserSettings);
const fetchFunction = createCollectorFetch(getUiSettingsClient, mockWhitelist);
const result = await fetchFunction();
expect(typeof result).toBe('object');
expect(result!).toMatchObject({
item1: 'test',
item2: 123,
});
});

it('returns true as value of blacklisted items', async () => {
const uiSettingsClient = uiSettingsServiceMock.createClient();
const getUiSettingsClient = jest.fn(() => uiSettingsClient);
uiSettingsClient.getUserProvided.mockResolvedValue(mockUserSettings);
const fetchFunction = createCollectorFetch(getUiSettingsClient, mockWhitelist);
const result = await fetchFunction();
expect(typeof result).toBe('object');
expect(result!).toMatchObject({
item3: true,
});
});
});
});
Loading