From 14534c600891fce1082e708cb4acae845702060f Mon Sep 17 00:00:00 2001 From: Matt Apperson Date: Fri, 13 Jul 2018 11:31:23 -0400 Subject: [PATCH] update TSLinting --- .../plugins/beats/common/constants/index.ts | 5 +-- .../beats/elasticsearch_beats_adapter.ts | 29 ++++++---------- .../adapters/beats/memory_beats_adapter.ts | 34 ++++++++----------- .../database/__tests__/kibana.test.ts | 3 +- .../lib/adapters/database/adapter_types.ts | 34 ++++--------------- .../database/kibana_database_adapter.ts | 15 ++------ .../kibana/kibana_framework_adapter.ts | 5 +-- .../kibana/testing_framework_adapter.ts | 6 +--- .../lib/adapters/framework/adapter_types.ts | 8 ++--- .../framework/kibana_framework_adapter.ts | 10 +++--- .../framework/testing_framework_adapter.ts | 13 +++---- .../lib/adapters/tokens/adapter_types.ts | 5 +-- .../tokens/elasticsearch_tokens_adapter.ts | 31 +++++------------ .../adapters/tokens/memory_tokens_adapter.ts | 17 +++------- .../beats/server/lib/compose/kibana.ts | 20 ++++------- .../__tests__/beats/assign_tags.test.ts | 8 ++--- .../domains/__tests__/beats/enroll.test.ts | 8 ++--- .../lib/domains/__tests__/tokens.test.ts | 14 ++------ .../plugins/beats/server/lib/domains/beats.ts | 31 ++++------------- .../plugins/beats/server/lib/domains/tags.ts | 15 ++------ .../beats/server/lib/domains/tokens.ts | 25 ++++---------- .../server/rest_api/beats/configuration.ts | 16 +++------ .../server/rest_api/beats/tag_assignment.ts | 5 +-- .../server/rest_api/beats/tag_removal.ts | 5 +-- .../plugins/beats/server/rest_api/tags/set.ts | 6 +--- .../beats/server/rest_api/tokens/create.ts | 5 +-- .../error_wrappers/wrap_es_error.test.ts | 4 +-- .../utils/error_wrappers/wrap_es_error.ts | 4 +-- .../server/utils/find_non_existent_items.ts | 19 ++++------- 29 files changed, 110 insertions(+), 290 deletions(-) diff --git a/x-pack/plugins/beats/common/constants/index.ts b/x-pack/plugins/beats/common/constants/index.ts index 4662865e208a7..756ffcf07e3ea 100644 --- a/x-pack/plugins/beats/common/constants/index.ts +++ b/x-pack/plugins/beats/common/constants/index.ts @@ -6,7 +6,4 @@ export { PLUGIN } from './plugin'; export { INDEX_NAMES } from './index_names'; -export { - UNIQUENESS_ENFORCING_TYPES, - ConfigurationBlockTypes, -} from './configuration_blocks'; +export { UNIQUENESS_ENFORCING_TYPES, ConfigurationBlockTypes } from './configuration_blocks'; diff --git a/x-pack/plugins/beats/server/lib/adapters/beats/elasticsearch_beats_adapter.ts b/x-pack/plugins/beats/server/lib/adapters/beats/elasticsearch_beats_adapter.ts index e2321ac6739a8..c0be74e1629f6 100644 --- a/x-pack/plugins/beats/server/lib/adapters/beats/elasticsearch_beats_adapter.ts +++ b/x-pack/plugins/beats/server/lib/adapters/beats/elasticsearch_beats_adapter.ts @@ -30,10 +30,7 @@ export class ElasticsearchBeatsAdapter implements CMBeatsAdapter { type: '_doc', }; - const response = await this.database.get( - this.framework.internalUser, - params - ); + const response = await this.database.get(this.framework.internalUser, params); if (!response.found) { return null; } @@ -155,13 +152,11 @@ export class ElasticsearchBeatsAdapter implements CMBeatsAdapter { refresh: 'wait_for', type: '_doc', }); - return _get(response, 'items', []).map( - (item: any, resultIdx: number) => ({ - idxInRequest: removals[resultIdx].idxInRequest, - result: item.update.result, - status: item.update.status, - }) - ); + return _get(response, 'items', []).map((item: any, resultIdx: number) => ({ + idxInRequest: removals[resultIdx].idxInRequest, + result: item.update.result, + status: item.update.status, + })); } public async assignTagsToBeats( @@ -193,12 +188,10 @@ export class ElasticsearchBeatsAdapter implements CMBeatsAdapter { refresh: 'wait_for', type: '_doc', }); - return _get(response, 'items', []).map( - (item: any, resultIdx: any) => ({ - idxInRequest: assignments[resultIdx].idxInRequest, - result: item.update.result, - status: item.update.status, - }) - ); + return _get(response, 'items', []).map((item: any, resultIdx: any) => ({ + idxInRequest: assignments[resultIdx].idxInRequest, + result: item.update.result, + status: item.update.status, + })); } } diff --git a/x-pack/plugins/beats/server/lib/adapters/beats/memory_beats_adapter.ts b/x-pack/plugins/beats/server/lib/adapters/beats/memory_beats_adapter.ts index e3aa2f2a64f28..041b34d29b49e 100644 --- a/x-pack/plugins/beats/server/lib/adapters/beats/memory_beats_adapter.ts +++ b/x-pack/plugins/beats/server/lib/adapters/beats/memory_beats_adapter.ts @@ -65,17 +65,15 @@ export class MemoryBeatsAdapter implements CMBeatsAdapter { ): Promise { const beatIds = removals.map(r => r.beatId); - const response = this.beatsDB - .filter(beat => beatIds.includes(beat.id)) - .map(beat => { - const tagData = removals.find(r => r.beatId === beat.id); - if (tagData) { - if (beat.tags) { - beat.tags = beat.tags.filter(tag => tag !== tagData.tag); - } + const response = this.beatsDB.filter(beat => beatIds.includes(beat.id)).map(beat => { + const tagData = removals.find(r => r.beatId === beat.id); + if (tagData) { + if (beat.tags) { + beat.tags = beat.tags.filter(tag => tag !== tagData.tag); } - return beat; - }); + } + return beat; + }); return response.map((item: CMBeat, resultIdx: number) => ({ idxInRequest: removals[resultIdx].idxInRequest, @@ -100,9 +98,7 @@ export class MemoryBeatsAdapter implements CMBeatsAdapter { if (!beat.tags) { beat.tags = []; } - const nonExistingTags = tags.filter( - (t: string) => beat.tags && !beat.tags.includes(t) - ); + const nonExistingTags = tags.filter((t: string) => beat.tags && !beat.tags.includes(t)); if (nonExistingTags.length > 0) { beat.tags = beat.tags.concat(nonExistingTags); @@ -111,12 +107,10 @@ export class MemoryBeatsAdapter implements CMBeatsAdapter { return beat; }); - return assignments.map( - (item: BeatsTagAssignment, resultIdx: number) => ({ - idxInRequest: assignments[resultIdx].idxInRequest, - result: 'updated', - status: 200, - }) - ); + return assignments.map((item: BeatsTagAssignment, resultIdx: number) => ({ + idxInRequest: assignments[resultIdx].idxInRequest, + result: 'updated', + status: 200, + })); } } diff --git a/x-pack/plugins/beats/server/lib/adapters/database/__tests__/kibana.test.ts b/x-pack/plugins/beats/server/lib/adapters/database/__tests__/kibana.test.ts index 19bf05c3c777e..c18c5b5bc4c76 100644 --- a/x-pack/plugins/beats/server/lib/adapters/database/__tests__/kibana.test.ts +++ b/x-pack/plugins/beats/server/lib/adapters/database/__tests__/kibana.test.ts @@ -28,7 +28,6 @@ contractTests('Kibana Database Adapter', { return await es.cleanup(); }, adapterSetup: () => { - return new KibanaDatabaseAdapter(kbnServer.server.plugins - .elasticsearch as DatabaseKbnESPlugin); + return new KibanaDatabaseAdapter(kbnServer.server.plugins.elasticsearch as DatabaseKbnESPlugin); }, }); diff --git a/x-pack/plugins/beats/server/lib/adapters/database/adapter_types.ts b/x-pack/plugins/beats/server/lib/adapters/database/adapter_types.ts index 36b5a35742bc9..22ee7a34066b3 100644 --- a/x-pack/plugins/beats/server/lib/adapters/database/adapter_types.ts +++ b/x-pack/plugins/beats/server/lib/adapters/database/adapter_types.ts @@ -5,10 +5,7 @@ */ import { FrameworkRequest, FrameworkUser } from '../framework/adapter_types'; export interface DatabaseAdapter { - putTemplate( - user: FrameworkUser, - params: DatabasePutTemplateParams - ): Promise; + putTemplate(user: FrameworkUser, params: DatabasePutTemplateParams): Promise; get( user: FrameworkUser, params: DatabaseGetParams @@ -25,27 +22,17 @@ export interface DatabaseAdapter { user: FrameworkUser, params: DatabaseDeleteDocumentParams ): Promise; - mget( - user: FrameworkUser, - params: DatabaseMGetParams - ): Promise>; + mget(user: FrameworkUser, params: DatabaseMGetParams): Promise>; bulk( user: FrameworkUser, params: DatabaseBulkIndexDocumentsParams ): Promise; - search( - user: FrameworkUser, - params: DatabaseSearchParams - ): Promise>; + search(user: FrameworkUser, params: DatabaseSearchParams): Promise>; } export interface DatabaseKbnESCluster { callWithInternalUser(esMethod: string, options: {}): Promise; - callWithRequest( - req: FrameworkRequest, - esMethod: string, - options: {} - ): Promise; + callWithRequest(req: FrameworkRequest, esMethod: string, options: {}): Promise; } export interface DatabaseKbnESPlugin { @@ -142,14 +129,11 @@ export interface DatabaseBulkResponse { took: number; errors: boolean; items: Array< - | DatabaseDeleteDocumentResponse - | DatabaseIndexDocumentResponse - | DatabaseUpdateDocumentResponse + DatabaseDeleteDocumentResponse | DatabaseIndexDocumentResponse | DatabaseUpdateDocumentResponse >; } -export interface DatabaseBulkIndexDocumentsParams - extends DatabaseGenericParams { +export interface DatabaseBulkIndexDocumentsParams extends DatabaseGenericParams { waitForActiveShards?: string; refresh?: DatabaseRefresh; routing?: string; @@ -299,11 +283,7 @@ export interface DatabaseGetParams extends DatabaseGenericParams { export type DatabaseNameList = string | string[] | boolean; export type DatabaseRefresh = boolean | 'true' | 'false' | 'wait_for' | ''; -export type DatabaseVersionType = - | 'internal' - | 'external' - | 'external_gte' - | 'force'; +export type DatabaseVersionType = 'internal' | 'external' | 'external_gte' | 'force'; export type ExpandWildcards = 'open' | 'closed' | 'none' | 'all'; export type DefaultOperator = 'AND' | 'OR'; export type DatabaseConflicts = 'abort' | 'proceed'; diff --git a/x-pack/plugins/beats/server/lib/adapters/database/kibana_database_adapter.ts b/x-pack/plugins/beats/server/lib/adapters/database/kibana_database_adapter.ts index 6c4d96446abd4..ba593a793e34b 100644 --- a/x-pack/plugins/beats/server/lib/adapters/database/kibana_database_adapter.ts +++ b/x-pack/plugins/beats/server/lib/adapters/database/kibana_database_adapter.ts @@ -30,10 +30,7 @@ export class KibanaDatabaseAdapter implements DatabaseAdapter { constructor(kbnElasticSearch: DatabaseKbnESPlugin) { this.es = kbnElasticSearch.getCluster('admin'); } - public async putTemplate( - user: FrameworkUser, - params: DatabasePutTemplateParams - ): Promise { + public async putTemplate(user: FrameworkUser, params: DatabasePutTemplateParams): Promise { const callES = this.getCallType(user); const result = await callES('indices.putTemplate', params); return result; @@ -59,10 +56,7 @@ export class KibanaDatabaseAdapter implements DatabaseAdapter { // todo } - public async bulk( - user: FrameworkUser, - params: DatabaseBulkIndexDocumentsParams - ): Promise { + public async bulk(user: FrameworkUser, params: DatabaseBulkIndexDocumentsParams): Promise { const callES = this.getCallType(user); const result = await callES('bulk', params); return result; @@ -76,10 +70,7 @@ export class KibanaDatabaseAdapter implements DatabaseAdapter { const result = await callES('create', params); return result; } - public async index( - user: FrameworkUser, - params: DatabaseIndexDocumentParams - ): Promise { + public async index(user: FrameworkUser, params: DatabaseIndexDocumentParams): Promise { const callES = this.getCallType(user); const result = await callES('index', params); return result; diff --git a/x-pack/plugins/beats/server/lib/adapters/famework/kibana/kibana_framework_adapter.ts b/x-pack/plugins/beats/server/lib/adapters/famework/kibana/kibana_framework_adapter.ts index a54997370ac5d..71703da8632ec 100644 --- a/x-pack/plugins/beats/server/lib/adapters/famework/kibana/kibana_framework_adapter.ts +++ b/x-pack/plugins/beats/server/lib/adapters/famework/kibana/kibana_framework_adapter.ts @@ -12,10 +12,7 @@ import { } from '../../../lib'; import { IStrictReply, Request, Server } from 'hapi'; -import { - internalFrameworkRequest, - wrapRequest, -} from '../../../../utils/wrap_request'; +import { internalFrameworkRequest, wrapRequest } from '../../../../utils/wrap_request'; export class KibanaBackendFrameworkAdapter implements BackendFrameworkAdapter { public version: string; diff --git a/x-pack/plugins/beats/server/lib/adapters/famework/kibana/testing_framework_adapter.ts b/x-pack/plugins/beats/server/lib/adapters/famework/kibana/testing_framework_adapter.ts index 9c928a05cfd5a..757464fa6cdc7 100644 --- a/x-pack/plugins/beats/server/lib/adapters/famework/kibana/testing_framework_adapter.ts +++ b/x-pack/plugins/beats/server/lib/adapters/famework/kibana/testing_framework_adapter.ts @@ -69,11 +69,7 @@ export class TestingBackendFrameworkAdapter implements BackendFrameworkAdapter { return await api(options); } - public async callWithRequest( - req: FrameworkRequest, - esMethod: string, - options: {} - ) { + public async callWithRequest(req: FrameworkRequest, esMethod: string, options: {}) { const api = get(this.client, esMethod); api(options); diff --git a/x-pack/plugins/beats/server/lib/adapters/framework/adapter_types.ts b/x-pack/plugins/beats/server/lib/adapters/framework/adapter_types.ts index 4be3589a6f043..014c041b11f0a 100644 --- a/x-pack/plugins/beats/server/lib/adapters/framework/adapter_types.ts +++ b/x-pack/plugins/beats/server/lib/adapters/framework/adapter_types.ts @@ -54,10 +54,10 @@ export interface FrameworkRouteOptions< config?: {}; } -export type FrameworkRouteHandler< - RouteRequest extends FrameworkWrappableRequest, - RouteResponse -> = (request: FrameworkRequest, reply: any) => void; +export type FrameworkRouteHandler = ( + request: FrameworkRequest, + reply: any +) => void; export interface FrameworkWrappableRequest< Payload = any, diff --git a/x-pack/plugins/beats/server/lib/adapters/framework/kibana_framework_adapter.ts b/x-pack/plugins/beats/server/lib/adapters/framework/kibana_framework_adapter.ts index 7113baf5c26e6..d07170acb7050 100644 --- a/x-pack/plugins/beats/server/lib/adapters/framework/kibana_framework_adapter.ts +++ b/x-pack/plugins/beats/server/lib/adapters/framework/kibana_framework_adapter.ts @@ -53,12 +53,10 @@ export class KibanaBackendFrameworkAdapter implements BackendFrameworkAdapter { }); } - public registerRoute< - RouteRequest extends FrameworkWrappableRequest, - RouteResponse - >(route: FrameworkRouteOptions) { - const wrappedHandler = (request: any, reply: any) => - route.handler(wrapRequest(request), reply); + public registerRoute( + route: FrameworkRouteOptions + ) { + const wrappedHandler = (request: any, reply: any) => route.handler(wrapRequest(request), reply); this.server.route({ handler: wrappedHandler, diff --git a/x-pack/plugins/beats/server/lib/adapters/framework/testing_framework_adapter.ts b/x-pack/plugins/beats/server/lib/adapters/framework/testing_framework_adapter.ts index 3bffe274fba8d..3280220893892 100644 --- a/x-pack/plugins/beats/server/lib/adapters/framework/testing_framework_adapter.ts +++ b/x-pack/plugins/beats/server/lib/adapters/framework/testing_framework_adapter.ts @@ -49,10 +49,9 @@ export class TestingBackendFrameworkAdapter implements BackendFrameworkAdapter { // not yet testable } - public registerRoute< - RouteRequest extends FrameworkWrappableRequest, - RouteResponse - >(route: FrameworkRouteOptions) { + public registerRoute( + route: FrameworkRouteOptions + ) { // not yet testable } @@ -68,11 +67,7 @@ export class TestingBackendFrameworkAdapter implements BackendFrameworkAdapter { return await api(options); } - public async callWithRequest( - req: FrameworkRequest, - esMethod: string, - options: {} - ) { + public async callWithRequest(req: FrameworkRequest, esMethod: string, options: {}) { const api = get(this.client, esMethod); api(options); diff --git a/x-pack/plugins/beats/server/lib/adapters/tokens/adapter_types.ts b/x-pack/plugins/beats/server/lib/adapters/tokens/adapter_types.ts index 71fb719d9952a..2fe8c811c396e 100644 --- a/x-pack/plugins/beats/server/lib/adapters/tokens/adapter_types.ts +++ b/x-pack/plugins/beats/server/lib/adapters/tokens/adapter_types.ts @@ -13,8 +13,5 @@ export interface TokenEnrollmentData { export interface CMTokensAdapter { deleteEnrollmentToken(enrollmentToken: string): Promise; getEnrollmentToken(enrollmentToken: string): Promise; - upsertTokens( - user: FrameworkUser, - tokens: TokenEnrollmentData[] - ): Promise; + upsertTokens(user: FrameworkUser, tokens: TokenEnrollmentData[]): Promise; } diff --git a/x-pack/plugins/beats/server/lib/adapters/tokens/elasticsearch_tokens_adapter.ts b/x-pack/plugins/beats/server/lib/adapters/tokens/elasticsearch_tokens_adapter.ts index 6dbefce514052..6aa9ceff46629 100644 --- a/x-pack/plugins/beats/server/lib/adapters/tokens/elasticsearch_tokens_adapter.ts +++ b/x-pack/plugins/beats/server/lib/adapters/tokens/elasticsearch_tokens_adapter.ts @@ -7,10 +7,7 @@ import { flatten, get } from 'lodash'; import { INDEX_NAMES } from '../../../../common/constants'; import { DatabaseAdapter } from '../database/adapter_types'; -import { - BackendFrameworkAdapter, - FrameworkUser, -} from '../framework/adapter_types'; +import { BackendFrameworkAdapter, FrameworkUser } from '../framework/adapter_types'; import { CMTokensAdapter, TokenEnrollmentData } from './adapter_types'; export class ElasticsearchTokensAdapter implements CMTokensAdapter { @@ -32,9 +29,7 @@ export class ElasticsearchTokensAdapter implements CMTokensAdapter { await this.database.delete(this.framework.internalUser, params); } - public async getEnrollmentToken( - tokenString: string - ): Promise { + public async getEnrollmentToken(tokenString: string): Promise { const params = { id: `enrollment_token:${tokenString}`, ignore: [404], @@ -42,18 +37,11 @@ export class ElasticsearchTokensAdapter implements CMTokensAdapter { type: '_doc', }; - const response = await this.database.get( - this.framework.internalUser, - params - ); - const tokenDetails = get( - response, - '_source.enrollment_token', - { - expires_on: '0', - token: null, - } - ); + const response = await this.database.get(this.framework.internalUser, params); + const tokenDetails = get(response, '_source.enrollment_token', { + expires_on: '0', + token: null, + }); // Elasticsearch might return fast if the token is not found. OR it might return fast // if the token *is* found. Either way, an attacker could using a timing attack to figure @@ -65,10 +53,7 @@ export class ElasticsearchTokensAdapter implements CMTokensAdapter { ); } - public async upsertTokens( - user: FrameworkUser, - tokens: TokenEnrollmentData[] - ) { + public async upsertTokens(user: FrameworkUser, tokens: TokenEnrollmentData[]) { const body = flatten( tokens.map(token => [ { index: { _id: `enrollment_token:${token.token}` } }, diff --git a/x-pack/plugins/beats/server/lib/adapters/tokens/memory_tokens_adapter.ts b/x-pack/plugins/beats/server/lib/adapters/tokens/memory_tokens_adapter.ts index 1eed188d3f2e9..7cf132bb6ba31 100644 --- a/x-pack/plugins/beats/server/lib/adapters/tokens/memory_tokens_adapter.ts +++ b/x-pack/plugins/beats/server/lib/adapters/tokens/memory_tokens_adapter.ts @@ -15,31 +15,22 @@ export class MemoryTokensAdapter implements CMTokensAdapter { } public async deleteEnrollmentToken(enrollmentToken: string) { - const index = this.tokenDB.findIndex( - token => token.token === enrollmentToken - ); + const index = this.tokenDB.findIndex(token => token.token === enrollmentToken); if (index > -1) { this.tokenDB.splice(index, 1); } } - public async getEnrollmentToken( - tokenString: string - ): Promise { + public async getEnrollmentToken(tokenString: string): Promise { return new Promise(resolve => { return resolve(this.tokenDB.find(token => token.token === tokenString)); }); } - public async upsertTokens( - user: FrameworkAuthenticatedUser, - tokens: TokenEnrollmentData[] - ) { + public async upsertTokens(user: FrameworkAuthenticatedUser, tokens: TokenEnrollmentData[]) { tokens.forEach(token => { - const existingIndex = this.tokenDB.findIndex( - t => t.token === token.token - ); + const existingIndex = this.tokenDB.findIndex(t => t.token === token.token); if (existingIndex !== -1) { this.tokenDB[existingIndex] = token; } else { diff --git a/x-pack/plugins/beats/server/lib/compose/kibana.ts b/x-pack/plugins/beats/server/lib/compose/kibana.ts index adb81a262667a..685171669a887 100644 --- a/x-pack/plugins/beats/server/lib/compose/kibana.ts +++ b/x-pack/plugins/beats/server/lib/compose/kibana.ts @@ -22,19 +22,13 @@ export function compose(server: any): CMServerLibs { const database = new KibanaDatabaseAdapter(server.plugins.elasticsearch); const tags = new CMTagsDomain(new ElasticsearchTagsAdapter(database)); - const tokens = new CMTokensDomain( - new ElasticsearchTokensAdapter(database, framework), - { - framework, - } - ); - const beats = new CMBeatsDomain( - new ElasticsearchBeatsAdapter(database, framework), - { - tags, - tokens, - } - ); + const tokens = new CMTokensDomain(new ElasticsearchTokensAdapter(database, framework), { + framework, + }); + const beats = new CMBeatsDomain(new ElasticsearchBeatsAdapter(database, framework), { + tags, + tokens, + }); const domainLibs: CMDomainLibs = { beats, diff --git a/x-pack/plugins/beats/server/lib/domains/__tests__/beats/assign_tags.test.ts b/x-pack/plugins/beats/server/lib/domains/__tests__/beats/assign_tags.test.ts index f28fcda8004e4..c9f9f15256d43 100644 --- a/x-pack/plugins/beats/server/lib/domains/__tests__/beats/assign_tags.test.ts +++ b/x-pack/plugins/beats/server/lib/domains/__tests__/beats/assign_tags.test.ts @@ -101,9 +101,7 @@ describe('Beats Domain Lib', () => { { beatId: 'bar', tag: 'production' }, ]); - expect(apiResponse.assignments).toEqual([ - { status: 200, result: 'updated' }, - ]); + expect(apiResponse.assignments).toEqual([{ status: 200, result: 'updated' }]); }); it('should not re-add an existing tag to a beat', async () => { @@ -117,9 +115,7 @@ describe('Beats Domain Lib', () => { { beatId: 'foo', tag: 'production' }, ]); - expect(apiResponse.assignments).toEqual([ - { status: 200, result: 'updated' }, - ]); + expect(apiResponse.assignments).toEqual([{ status: 200, result: 'updated' }]); beat = beatsDB.find(b => b.id === 'foo') as any; expect(beat.tags).toEqual([...tags, 'qa']); diff --git a/x-pack/plugins/beats/server/lib/domains/__tests__/beats/enroll.test.ts b/x-pack/plugins/beats/server/lib/domains/__tests__/beats/enroll.test.ts index 174c427e4d2a0..f60c1ed0e009e 100644 --- a/x-pack/plugins/beats/server/lib/domains/__tests__/beats/enroll.test.ts +++ b/x-pack/plugins/beats/server/lib/domains/__tests__/beats/enroll.test.ts @@ -85,9 +85,7 @@ describe('Beats Domain Lib', () => { }); it('should enroll beat, returning an access token', async () => { - const { token } = await tokensLib.getEnrollmentToken( - validEnrollmentToken - ); + const { token } = await tokensLib.getEnrollmentToken(validEnrollmentToken); expect(token).toEqual(validEnrollmentToken); const { accessToken, status } = await beatsLib.enrollBeat( @@ -129,9 +127,7 @@ describe('Beats Domain Lib', () => { await tokensLib.deleteEnrollmentToken(validEnrollmentToken); expect(tokensDB.length).toEqual(0); - const { token } = await tokensLib.getEnrollmentToken( - validEnrollmentToken - ); + const { token } = await tokensLib.getEnrollmentToken(validEnrollmentToken); expect(token).toEqual(null); }); diff --git a/x-pack/plugins/beats/server/lib/domains/__tests__/tokens.test.ts b/x-pack/plugins/beats/server/lib/domains/__tests__/tokens.test.ts index 8002013fa4795..c89962dca7d3b 100644 --- a/x-pack/plugins/beats/server/lib/domains/__tests__/tokens.test.ts +++ b/x-pack/plugins/beats/server/lib/domains/__tests__/tokens.test.ts @@ -36,10 +36,7 @@ describe('Token Domain Lib', () => { }); it('should generate webtokens with a qty of 1', async () => { - const tokens = await tokensLib.createEnrollmentTokens( - framework.internalUser, - 1 - ); + const tokens = await tokensLib.createEnrollmentTokens(framework.internalUser, 1); expect(tokens.length).toBe(1); @@ -48,15 +45,10 @@ describe('Token Domain Lib', () => { it('should create the specified number of tokens', async () => { const numTokens = chance.integer({ min: 1, max: 20 }); - const tokensFromApi = await tokensLib.createEnrollmentTokens( - framework.internalUser, - numTokens - ); + const tokensFromApi = await tokensLib.createEnrollmentTokens(framework.internalUser, numTokens); expect(tokensFromApi.length).toEqual(numTokens); - expect(tokensFromApi).toEqual( - tokensDB.map((t: TokenEnrollmentData) => t.token) - ); + expect(tokensFromApi).toEqual(tokensDB.map((t: TokenEnrollmentData) => t.token)); }); it('should set token expiration to 10 minutes from now by default', async () => { diff --git a/x-pack/plugins/beats/server/lib/domains/beats.ts b/x-pack/plugins/beats/server/lib/domains/beats.ts index 6d15c878ad2ef..07a084e54b53d 100644 --- a/x-pack/plugins/beats/server/lib/domains/beats.ts +++ b/x-pack/plugins/beats/server/lib/domains/beats.ts @@ -9,10 +9,7 @@ import moment from 'moment'; import { findNonExistentItems } from '../../utils/find_non_existent_items'; import { CMBeat } from '../../../common/domain_types'; -import { - BeatsTagAssignment, - CMBeatsAdapter, -} from '../adapters/beats/adapter_types'; +import { BeatsTagAssignment, CMBeatsAdapter } from '../adapters/beats/adapter_types'; import { FrameworkUser } from '../adapters/framework/adapter_types'; import { CMAssignmentReturn } from '../adapters/beats/adapter_types'; @@ -37,11 +34,7 @@ export class CMBeatsDomain { return await this.adapter.get(beatId); } - public async update( - beatId: string, - accessToken: string, - beatData: Partial - ) { + public async update(beatId: string, accessToken: string, beatData: Partial) { const beat = await this.adapter.get(beatId); const { verified: isAccessTokenValid } = this.tokens.verifyToken( @@ -71,9 +64,7 @@ export class CMBeatsDomain { remoteAddress: string, beat: Partial ): Promise<{ status: string; accessToken?: string }> { - const { token, expires_on } = await this.tokens.getEnrollmentToken( - enrollmentToken - ); + const { token, expires_on } = await this.tokens.getEnrollmentToken(enrollmentToken); if (expires_on && moment(expires_on).isBefore(moment())) { return { status: BeatEnrollmentStatus.ExpiredEnrollmentToken }; @@ -134,10 +125,7 @@ export class CMBeatsDomain { .filter((removal, idx) => response.removals[idx].status === null); if (validRemovals.length > 0) { - const removalResults = await this.adapter.removeTagsFromBeats( - user, - validRemovals - ); + const removalResults = await this.adapter.removeTagsFromBeats(user, validRemovals); return addToResultsToResponse('removals', response, removalResults); } return response; @@ -183,10 +171,7 @@ export class CMBeatsDomain { .filter((assignment, idx) => response.assignments[idx].status === null); if (validAssignments.length > 0) { - const assignmentResults = await this.adapter.assignTagsToBeats( - user, - validAssignments - ); + const assignmentResults = await this.adapter.assignTagsToBeats(user, validAssignments); // TODO This should prob not mutate return addToResultsToResponse('assignments', response, assignmentResults); @@ -221,11 +206,7 @@ function addNonExistentItemToResponse( } // TODO dont mutate response -function addToResultsToResponse( - key: string, - response: any, - assignmentResults: any -) { +function addToResultsToResponse(key: string, response: any, assignmentResults: any) { assignmentResults.forEach((assignmentResult: any) => { const { idxInRequest, status, result } = assignmentResult; response[key][idxInRequest].status = status; diff --git a/x-pack/plugins/beats/server/lib/domains/tags.ts b/x-pack/plugins/beats/server/lib/domains/tags.ts index 194551721ab33..b6ce9f7e14821 100644 --- a/x-pack/plugins/beats/server/lib/domains/tags.ts +++ b/x-pack/plugins/beats/server/lib/domains/tags.ts @@ -22,14 +22,8 @@ export class CMTagsDomain { return await this.adapter.getTagsWithIds(user, tagIds); } - public async saveTag( - user: FrameworkUser, - tagId: string, - configs: ConfigurationBlock[] - ) { - const { isValid, message } = await this.validateConfigurationBlocks( - configs - ); + public async saveTag(user: FrameworkUser, tagId: string, configs: ConfigurationBlock[]) { + const { isValid, message } = await this.validateConfigurationBlocks(configs); if (!isValid) { return { isValid, result: message }; } @@ -49,10 +43,7 @@ export class CMTagsDomain { // If none of the types in the given configuration blocks are uniqueness-enforcing, // we don't need to perform any further validation checks. - const uniquenessEnforcingTypes = intersection( - types, - UNIQUENESS_ENFORCING_TYPES - ); + const uniquenessEnforcingTypes = intersection(types, UNIQUENESS_ENFORCING_TYPES); if (uniquenessEnforcingTypes.length === 0) { return { isValid: true }; } diff --git a/x-pack/plugins/beats/server/lib/domains/tokens.ts b/x-pack/plugins/beats/server/lib/domains/tokens.ts index 6cfb922c34d57..dc7ffa9a63356 100644 --- a/x-pack/plugins/beats/server/lib/domains/tokens.ts +++ b/x-pack/plugins/beats/server/lib/domains/tokens.ts @@ -18,10 +18,7 @@ export class CMTokensDomain { private adapter: CMTokensAdapter; private framework: BackendFrameworkAdapter; - constructor( - adapter: CMTokensAdapter, - libs: { framework: BackendFrameworkAdapter } - ) { + constructor(adapter: CMTokensAdapter, libs: { framework: BackendFrameworkAdapter }) { this.adapter = adapter; this.framework = libs.framework; } @@ -37,11 +34,7 @@ export class CMTokensDomain { }; } - const { verified, expired } = this.verifyToken( - enrollmentToken, - fullToken.token || '', - false - ); + const { verified, expired } = this.verifyToken(enrollmentToken, fullToken.token || '', false); if (!verified) { return { @@ -63,9 +56,7 @@ export class CMTokensDomain { let expired = false; if (decode) { - const enrollmentTokenSecret = this.framework.getSetting( - 'xpack.beats.encryptionKey' - ); + const enrollmentTokenSecret = this.framework.getSetting('xpack.beats.encryptionKey'); try { verifyToken(recivedToken, enrollmentTokenSecret); @@ -99,17 +90,13 @@ export class CMTokensDomain { return { expired, verified: - timingSafeEqual( - Buffer.from(recivedToken, 'utf8'), - Buffer.from(token2, 'utf8') - ) && tokenDecoded, + timingSafeEqual(Buffer.from(recivedToken, 'utf8'), Buffer.from(token2, 'utf8')) && + tokenDecoded, }; } public generateAccessToken() { - const enrollmentTokenSecret = this.framework.getSetting( - 'xpack.beats.encryptionKey' - ); + const enrollmentTokenSecret = this.framework.getSetting('xpack.beats.encryptionKey'); const tokenData = { created: moment().toJSON(), diff --git a/x-pack/plugins/beats/server/rest_api/beats/configuration.ts b/x-pack/plugins/beats/server/rest_api/beats/configuration.ts index e1f430320ecf3..81906ea9473e6 100644 --- a/x-pack/plugins/beats/server/rest_api/beats/configuration.ts +++ b/x-pack/plugins/beats/server/rest_api/beats/configuration.ts @@ -37,21 +37,15 @@ export const createGetBeatConfigurationRoute = (libs: CMServerLibs) => ({ return reply({ message: 'Invalid access token' }).code(401); } - tags = await libs.tags.getTagsWithIds( - libs.framework.internalUser, - beat.tags || [] - ); + tags = await libs.tags.getTagsWithIds(libs.framework.internalUser, beat.tags || []); } catch (err) { return reply(wrapEsError(err)); } - const configurationBlocks = tags.reduce( - (blocks: ConfigurationBlock[], tag: BeatTag) => { - blocks = blocks.concat(tag.configuration_blocks); - return blocks; - }, - [] - ); + const configurationBlocks = tags.reduce((blocks: ConfigurationBlock[], tag: BeatTag) => { + blocks = blocks.concat(tag.configuration_blocks); + return blocks; + }, []); reply({ configuration_blocks: configurationBlocks, diff --git a/x-pack/plugins/beats/server/rest_api/beats/tag_assignment.ts b/x-pack/plugins/beats/server/rest_api/beats/tag_assignment.ts index a6654250d196f..9c832ee3226b8 100644 --- a/x-pack/plugins/beats/server/rest_api/beats/tag_assignment.ts +++ b/x-pack/plugins/beats/server/rest_api/beats/tag_assignment.ts @@ -34,10 +34,7 @@ export const createTagAssignmentsRoute = (libs: CMServerLibs) => ({ })); try { - const response = await libs.beats.assignTagsToBeats( - request.user, - tweakedAssignments - ); + const response = await libs.beats.assignTagsToBeats(request.user, tweakedAssignments); reply(response); } catch (err) { // TODO move this to kibana route thing in adapter diff --git a/x-pack/plugins/beats/server/rest_api/beats/tag_removal.ts b/x-pack/plugins/beats/server/rest_api/beats/tag_removal.ts index 1b495b906a364..6a7af3b42d407 100644 --- a/x-pack/plugins/beats/server/rest_api/beats/tag_removal.ts +++ b/x-pack/plugins/beats/server/rest_api/beats/tag_removal.ts @@ -34,10 +34,7 @@ export const createTagRemovalsRoute = (libs: CMServerLibs) => ({ })); try { - const response = await libs.beats.removeTagsFromBeats( - request.user, - tweakedRemovals - ); + const response = await libs.beats.removeTagsFromBeats(request.user, tweakedRemovals); reply(response); } catch (err) { // TODO move this to kibana route thing in adapter diff --git a/x-pack/plugins/beats/server/rest_api/tags/set.ts b/x-pack/plugins/beats/server/rest_api/tags/set.ts index bdc49ce62a9a9..6c01959e75311 100644 --- a/x-pack/plugins/beats/server/rest_api/tags/set.ts +++ b/x-pack/plugins/beats/server/rest_api/tags/set.ts @@ -32,11 +32,7 @@ export const createSetTagRoute = (libs: CMServerLibs) => ({ }, }, handler: async (request: FrameworkRequest, reply: any) => { - const configurationBlocks = get( - request, - 'payload.configuration_blocks', - [] - ); + const configurationBlocks = get(request, 'payload.configuration_blocks', []); try { const { isValid, result } = await libs.tags.saveTag( request.user, diff --git a/x-pack/plugins/beats/server/rest_api/tokens/create.ts b/x-pack/plugins/beats/server/rest_api/tokens/create.ts index 9e20735d640e5..74278703347c3 100644 --- a/x-pack/plugins/beats/server/rest_api/tokens/create.ts +++ b/x-pack/plugins/beats/server/rest_api/tokens/create.ts @@ -27,10 +27,7 @@ export const createTokensRoute = (libs: CMServerLibs) => ({ const numTokens = get(request, 'payload.num_tokens', DEFAULT_NUM_TOKENS); try { - const tokens = await libs.tokens.createEnrollmentTokens( - request.user, - numTokens - ); + const tokens = await libs.tokens.createEnrollmentTokens(request.user, numTokens); reply({ tokens }); } catch (err) { // TODO move this to kibana route thing in adapter diff --git a/x-pack/plugins/beats/server/utils/error_wrappers/wrap_es_error.test.ts b/x-pack/plugins/beats/server/utils/error_wrappers/wrap_es_error.test.ts index 5087bf3224c42..e7fb1c2adda21 100644 --- a/x-pack/plugins/beats/server/utils/error_wrappers/wrap_es_error.test.ts +++ b/x-pack/plugins/beats/server/utils/error_wrappers/wrap_es_error.test.ts @@ -24,9 +24,7 @@ describe('wrap_es_error', () => { const wrappedError = wrapEsError(originalError); expect(wrappedError.output.statusCode).toEqual(originalError.statusCode); - expect(wrappedError.output.payload.message).toEqual( - originalError.message - ); + expect(wrappedError.output.payload.message).toEqual(originalError.message); }); it('should return invalid permissions message for 403 errors', () => { diff --git a/x-pack/plugins/beats/server/utils/error_wrappers/wrap_es_error.ts b/x-pack/plugins/beats/server/utils/error_wrappers/wrap_es_error.ts index 50ffbcb4a10c9..30328f2c6a833 100644 --- a/x-pack/plugins/beats/server/utils/error_wrappers/wrap_es_error.ts +++ b/x-pack/plugins/beats/server/utils/error_wrappers/wrap_es_error.ts @@ -16,9 +16,7 @@ import Boom from 'boom'; export function wrapEsError(err: any) { const statusCode = err.statusCode; if (statusCode === 403) { - return Boom.forbidden( - 'Insufficient user permissions for managing Beats configuration' - ); + return Boom.forbidden('Insufficient user permissions for managing Beats configuration'); } return Boom.wrap(err, err.statusCode); } diff --git a/x-pack/plugins/beats/server/utils/find_non_existent_items.ts b/x-pack/plugins/beats/server/utils/find_non_existent_items.ts index d6b2a0c9e143b..0e9b4f0b6fa5e 100644 --- a/x-pack/plugins/beats/server/utils/find_non_existent_items.ts +++ b/x-pack/plugins/beats/server/utils/find_non_existent_items.ts @@ -10,17 +10,10 @@ interface RandomItem { } export function findNonExistentItems(items: RandomItem[], requestedItems: any) { - return requestedItems.reduce( - (nonExistentItems: string[], requestedItem: string, idx: number) => { - if ( - items.findIndex( - (item: RandomItem) => item && item.id === requestedItem - ) === -1 - ) { - nonExistentItems.push(requestedItems[idx]); - } - return nonExistentItems; - }, - [] - ); + return requestedItems.reduce((nonExistentItems: string[], requestedItem: string, idx: number) => { + if (items.findIndex((item: RandomItem) => item && item.id === requestedItem) === -1) { + nonExistentItems.push(requestedItems[idx]); + } + return nonExistentItems; + }, []); }