-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Beats Management] Move tokens to use JWT, add more complete test sui…
…te (#20317) * inital effort to move to JWT and added jest based tests on libs * assign beats tests all passing * token tests now pass * add more tests * all tests now green * fix broken test, this is beats CM not logstash 😊 * added readme * move enrollment token back to a hash * remove un-needed comment * alias lodash get to avoid confusion * isolated hash creation
- Loading branch information
1 parent
71a847d
commit f130bbe
Showing
30 changed files
with
1,422 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Documentation for Beats CM in x-pack kibana | ||
|
||
### Run tests | ||
|
||
``` | ||
node scripts/jest.js plugins/beats --watch | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
x-pack/plugins/beats/server/lib/adapters/beats/memory_beats_adapter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { omit } from 'lodash'; | ||
import moment from 'moment'; | ||
|
||
import { | ||
CMBeat, | ||
CMBeatsAdapter, | ||
CMTagAssignment, | ||
FrameworkRequest, | ||
} from '../../lib'; | ||
|
||
export class MemoryBeatsAdapter implements CMBeatsAdapter { | ||
private beatsDB: CMBeat[]; | ||
|
||
constructor(beatsDB: CMBeat[]) { | ||
this.beatsDB = beatsDB; | ||
} | ||
|
||
public async get(id: string) { | ||
return this.beatsDB.find(beat => beat.id === id); | ||
} | ||
|
||
public async insert(beat: CMBeat) { | ||
this.beatsDB.push(beat); | ||
} | ||
|
||
public async update(beat: CMBeat) { | ||
const beatIndex = this.beatsDB.findIndex(b => b.id === beat.id); | ||
|
||
this.beatsDB[beatIndex] = { | ||
...this.beatsDB[beatIndex], | ||
...beat, | ||
}; | ||
} | ||
|
||
public async getWithIds(req: FrameworkRequest, beatIds: string[]) { | ||
return this.beatsDB.filter(beat => beatIds.includes(beat.id)); | ||
} | ||
|
||
public async verifyBeats(req: FrameworkRequest, beatIds: string[]) { | ||
if (!Array.isArray(beatIds) || beatIds.length === 0) { | ||
return []; | ||
} | ||
|
||
const verifiedOn = moment().toJSON(); | ||
|
||
this.beatsDB.forEach((beat, i) => { | ||
if (beatIds.includes(beat.id)) { | ||
this.beatsDB[i].verified_on = verifiedOn; | ||
} | ||
}); | ||
|
||
return this.beatsDB.filter(beat => beatIds.includes(beat.id)); | ||
} | ||
|
||
public async getAll(req: FrameworkRequest) { | ||
return this.beatsDB.map((beat: any) => omit(beat, ['access_token'])); | ||
} | ||
|
||
public async removeTagsFromBeats( | ||
req: FrameworkRequest, | ||
removals: CMTagAssignment[] | ||
): Promise<CMTagAssignment[]> { | ||
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); | ||
} | ||
} | ||
return beat; | ||
}); | ||
|
||
return response.map<any>((item: CMBeat, resultIdx: number) => ({ | ||
idxInRequest: removals[resultIdx].idxInRequest, | ||
result: 'updated', | ||
status: 200, | ||
})); | ||
} | ||
|
||
public async assignTagsToBeats( | ||
req: FrameworkRequest, | ||
assignments: CMTagAssignment[] | ||
): Promise<CMTagAssignment[]> { | ||
const beatIds = assignments.map(r => r.beatId); | ||
|
||
this.beatsDB.filter(beat => beatIds.includes(beat.id)).map(beat => { | ||
// get tags that need to be assigned to this beat | ||
const tags = assignments | ||
.filter(a => a.beatId === beat.id) | ||
.map((t: CMTagAssignment) => t.tag); | ||
|
||
if (tags.length > 0) { | ||
if (!beat.tags) { | ||
beat.tags = []; | ||
} | ||
const nonExistingTags = tags.filter( | ||
(t: string) => beat.tags && !beat.tags.includes(t) | ||
); | ||
|
||
if (nonExistingTags.length > 0) { | ||
beat.tags = beat.tags.concat(nonExistingTags); | ||
} | ||
} | ||
return beat; | ||
}); | ||
|
||
return assignments.map<any>((item: CMTagAssignment, resultIdx: number) => ({ | ||
idxInRequest: assignments[resultIdx].idxInRequest, | ||
result: 'updated', | ||
status: 200, | ||
})); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
x-pack/plugins/beats/server/lib/adapters/famework/kibana/testing_framework_adapter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { Client } from 'elasticsearch'; | ||
import { Request } from 'hapi'; | ||
import { get } from 'lodash'; | ||
import { | ||
BackendFrameworkAdapter, | ||
FrameworkRequest, | ||
FrameworkRouteOptions, | ||
WrappableRequest, | ||
} from '../../../lib'; | ||
|
||
interface TestSettings { | ||
enrollmentTokensTtlInSeconds: number; | ||
encryptionKey: string; | ||
} | ||
|
||
export class TestingBackendFrameworkAdapter implements BackendFrameworkAdapter { | ||
public version: string; | ||
private client: Client | null; | ||
private settings: TestSettings; | ||
|
||
constructor(client: Client | null, settings: TestSettings) { | ||
this.client = client; | ||
this.settings = settings || { | ||
encryptionKey: 'something_who_cares', | ||
enrollmentTokensTtlInSeconds: 10 * 60, // 10 minutes | ||
}; | ||
this.version = 'testing'; | ||
} | ||
|
||
public getSetting(settingPath: string) { | ||
switch (settingPath) { | ||
case 'xpack.beats.enrollmentTokensTtlInSeconds': | ||
return this.settings.enrollmentTokensTtlInSeconds; | ||
case 'xpack.beats.encryptionKey': | ||
return this.settings.encryptionKey; | ||
} | ||
} | ||
|
||
public exposeStaticDir(urlPath: string, dir: string): void { | ||
// not yet testable | ||
} | ||
|
||
public registerRoute<RouteRequest extends WrappableRequest, RouteResponse>( | ||
route: FrameworkRouteOptions<RouteRequest, RouteResponse> | ||
) { | ||
// not yet testable | ||
} | ||
|
||
public installIndexTemplate(name: string, template: {}) { | ||
if (this.client) { | ||
return this.client.indices.putTemplate({ | ||
body: template, | ||
name, | ||
}); | ||
} | ||
} | ||
|
||
public async callWithInternalUser(esMethod: string, options: {}) { | ||
const api = get<any>(this.client, esMethod); | ||
|
||
api(options); | ||
|
||
return await api(options); | ||
} | ||
|
||
public async callWithRequest( | ||
req: FrameworkRequest<Request>, | ||
esMethod: string, | ||
options: {} | ||
) { | ||
const api = get<any>(this.client, esMethod); | ||
|
||
api(options); | ||
|
||
return await api(options); | ||
} | ||
} |
Oops, something went wrong.