Skip to content

Commit

Permalink
Fix bad formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
pjhampton committed Sep 1, 2021
1 parent 2e6ab24 commit 048f533
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* 2.0.
*/

export const TELEMETRY_MAX_BUFFER_SIZE = 100;

export const TELEMETRY_CHANNEL_LISTS = 'security-lists';

export const TELEMETRY_CHANNEL_ENDPOINT_META = 'endpoint-metadata';
Expand Down
108 changes: 46 additions & 62 deletions x-pack/plugins/security_solution/server/lib/telemetry/receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { getTrustedAppsList } from '../../endpoint/routes/trusted_apps/service';
import { AgentService, AgentPolicyServiceInterface } from '../../../../fleet/server';
import { ExceptionListClient } from '../../../../lists/server';
import { EndpointAppContextService } from '../../endpoint/endpoint_app_context_services';
import { TELEMETRY_MAX_BUFFER_SIZE } from './constants';
import { exceptionListItemToEndpointEntry } from './helpers';
import { TelemetryEvent, ESLicense, ESClusterInfo, GetEndpointListResponse } from './types';

Expand All @@ -27,7 +28,6 @@ export class TelemetryReceiver {
private exceptionListClient?: ExceptionListClient;
private soClient?: SavedObjectsClientContract;
private readonly max_records = 10_000;
private maxQueueSize = 100;

constructor(logger: Logger) {
this.logger = logger.get('telemetry_events');
Expand Down Expand Up @@ -58,14 +58,6 @@ export class TelemetryReceiver {
});
}

public async fetchClusterInfo(): Promise<ESClusterInfo> {
if (this.esClient === undefined || this.esClient === null) {
throw Error('elasticsearch client is unavailable: cannot retrieve cluster infomation');
}

return this.getClusterInfo(this.esClient);
}

public async fetchEndpointPolicyResponses(executeFrom: string, executeTo: string) {
if (this.esClient === undefined || this.esClient === null) {
throw Error(
Expand Down Expand Up @@ -171,7 +163,7 @@ export class TelemetryReceiver {
expand_wildcards: 'open,hidden',
index: '.logs-endpoint.diagnostic.collection-*',
ignore_unavailable: true,
size: this.maxQueueSize,
size: TELEMETRY_MAX_BUFFER_SIZE,
body: {
query: {
range: {
Expand All @@ -194,58 +186,6 @@ export class TelemetryReceiver {
return (await this.esClient.search<TelemetryEvent>(query)).body;
}

/**
* Get the cluster info from the connected cluster.
* Copied from:
* src/plugins/telemetry/server/telemetry_collection/get_cluster_info.ts
* This is the equivalent to GET /
*/
private async getClusterInfo(esClient: ElasticsearchClient) {
const { body } = await esClient.info();
return body;
}

public async fetchLicenseInfo(): Promise<ESLicense | undefined> {
if (this.esClient === undefined || this.esClient === null) {
throw Error('elasticsearch client is unavailable: cannot retrieve license information');
}

try {
const ret = await this.getLicense(this.esClient, true);
return ret.license;
} catch (err) {
this.logger.debug(`failed retrieving license: ${err}`);
return undefined;
}
}

private async getLicense(
esClient: ElasticsearchClient,
local: boolean
): Promise<{ license: ESLicense }> {
return (
await esClient.transport.request({
method: 'GET',
path: '/_license',
querystring: {
local,
// For versions >= 7.6 and < 8.0, this flag is needed otherwise 'platinum' is returned for 'enterprise' license.
accept_enterprise: 'true',
},
})
).body as Promise<{ license: ESLicense }>; // Note: We have to as cast since transport.request doesn't have generics
}

public copyLicenseFields(lic: ESLicense) {
return {
uid: lic.uid,
status: lic.status,
type: lic.type,
...(lic.issued_to ? { issued_to: lic.issued_to } : {}),
...(lic.issuer ? { issuer: lic.issuer } : {}),
};
}

public async fetchPolicyConfigs(id: string) {
if (this.soClient === undefined || this.soClient === null) {
throw Error(
Expand Down Expand Up @@ -289,4 +229,48 @@ export class TelemetryReceiver {
per_page: results?.per_page ?? this.max_records,
};
}

public async fetchClusterInfo(): Promise<ESClusterInfo> {
if (this.esClient === undefined || this.esClient === null) {
throw Error('elasticsearch client is unavailable: cannot retrieve cluster infomation');
}

const { body } = await this.esClient.info();
return body;
}

public async fetchLicenseInfo(): Promise<ESLicense | undefined> {
if (this.esClient === undefined || this.esClient === null) {
throw Error('elasticsearch client is unavailable: cannot retrieve license information');
}

try {
const ret = (
await this.esClient.transport.request({
method: 'GET',
path: '/_license',
querystring: {
local: true,
// For versions >= 7.6 and < 8.0, this flag is needed otherwise 'platinum' is returned for 'enterprise' license.
accept_enterprise: 'true',
},
})
).body as Promise<{ license: ESLicense }>;

return (await ret).license;
} catch (err) {
this.logger.debug(`failed retrieving license: ${err}`);
return undefined;
}
}

public copyLicenseFields(lic: ESLicense) {
return {
uid: lic.uid,
status: lic.status,
type: lic.type,
...(lic.issued_to ? { issued_to: lic.issued_to } : {}),
...(lic.issuer ? { issuer: lic.issuer } : {}),
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ import { AllowlistFields, allowlistEventFields } from './filters';
import { DiagnosticTask, EndpointTask, ExceptionListsTask } from './tasks';
import { createUsageCounterLabel } from './helpers';
import { TelemetryEvent } from './types';
import { TELEMETRY_MAX_BUFFER_SIZE } from './constants';

const usageLabelPrefix: string[] = ['security_telemetry', 'sender'];

export class TelemetryEventsSender {
private readonly initialCheckDelayMs = 10 * 1000;
private readonly checkIntervalMs = 60 * 1000;
private readonly logger: Logger;
private maxQueueSize = 100;
private maxQueueSize = TELEMETRY_MAX_BUFFER_SIZE;
private telemetryStart?: TelemetryPluginStart;
private telemetrySetup?: TelemetryPluginSetup;
private intervalId?: NodeJS.Timeout;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
import { schema, TypeOf } from '@kbn/config-schema';
import { TrustedApp } from '../../../common/endpoint/types';


type BaseSearchTypes = string | number | boolean | object;
export type SearchTypes = BaseSearchTypes | BaseSearchTypes[] | undefined;


// For getting cluster info. Copied from telemetry_collection/get_cluster_info.ts
export interface ESClusterInfo {
cluster_uuid: string;
Expand Down Expand Up @@ -45,7 +43,6 @@ export interface ESLicense {
start_date_in_millis?: number;
}


export interface TelemetryEvent {
[key: string]: SearchTypes;
'@timestamp'?: string;
Expand All @@ -64,7 +61,6 @@ export interface TelemetryEvent {
license?: ESLicense;
}


// EP Policy Response

export interface EndpointPolicyResponseAggregation {
Expand Down

0 comments on commit 048f533

Please sign in to comment.