Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import { CMBeat } from '../../../../common/domain_types';
export interface CMBeatsAdapter {
get(id: string): Promise<CMBeat | null>;
getAll(): Promise<CMBeat[]>;
getWithIds(beatIds: string[]): Promise<CMBeat[]>;
removeTagsFromBeats(removals: BeatsTagAssignment[]): Promise<BeatsTagAssignment[]>;
assignTagsToBeats(assignments: BeatsTagAssignment[]): Promise<BeatsTagAssignment[]>;
removeTagsFromBeats(removals: BeatsTagAssignment[]): Promise<BeatsRemovalReturn[]>;
assignTagsToBeats(assignments: BeatsTagAssignment[]): Promise<CMAssignmentReturn[]>;
}

export interface BeatsTagAssignment {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
import { omit } from 'lodash';

import { CMBeat } from '../../../../common/domain_types';
import { BeatsTagAssignment, CMBeatsAdapter } from './adapter_types';
import {
BeatsRemovalReturn,
BeatsTagAssignment,
CMAssignmentReturn,
CMBeatsAdapter,
} from './adapter_types';

export class MemoryBeatsAdapter implements CMBeatsAdapter {
private beatsDB: CMBeat[];
Expand All @@ -20,15 +25,11 @@ export class MemoryBeatsAdapter implements CMBeatsAdapter {
return this.beatsDB.find(beat => beat.id === id) || null;
}

public async getWithIds(beatIds: string[]) {
return this.beatsDB.filter(beat => beatIds.includes(beat.id));
}

public async getAll() {
return this.beatsDB.map<CMBeat>((beat: any) => omit(beat, ['access_token']));
}

public async removeTagsFromBeats(removals: BeatsTagAssignment[]): Promise<BeatsTagAssignment[]> {
public async removeTagsFromBeats(removals: BeatsTagAssignment[]): Promise<BeatsRemovalReturn[]> {
const beatIds = removals.map(r => r.beatId);

const response = this.beatsDB.filter(beat => beatIds.includes(beat.id)).map(beat => {
Expand All @@ -48,7 +49,7 @@ export class MemoryBeatsAdapter implements CMBeatsAdapter {
}));
}

public async assignTagsToBeats(assignments: BeatsTagAssignment[]): Promise<BeatsTagAssignment[]> {
public async assignTagsToBeats(assignments: BeatsTagAssignment[]): Promise<CMAssignmentReturn[]> {
const beatIds = assignments.map(r => r.beatId);

this.beatsDB.filter(beat => beatIds.includes(beat.id)).map(beat => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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 { CMBeat } from '../../../../common/domain_types';
import { RestAPIAdapter } from '../rest_api/adapter_types';
import {
BeatsRemovalReturn,
BeatsTagAssignment,
CMAssignmentReturn,
CMBeatsAdapter,
} from './adapter_types';
export class RestBeatsAdapter implements CMBeatsAdapter {
constructor(private readonly REST: RestAPIAdapter) {}

public async get(id: string): Promise<CMBeat | null> {
return await this.REST.get<CMBeat>(`/api/beats/agent/${id}`);
}

public async getAll(): Promise<CMBeat[]> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this where we'll put an endpoint that returns a CMPopulatedBeat[] once we have the updated types merged as well? Containing the full tag data, etc.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

return await this.REST.get<CMBeat[]>('/api/beats/agents');
}

public async removeTagsFromBeats(removals: BeatsTagAssignment[]): Promise<BeatsRemovalReturn[]> {
return await this.REST.post<BeatsRemovalReturn[]>(`/api/beats/agents_tags/removals`, {
removals,
});
}

public async assignTagsToBeats(assignments: BeatsTagAssignment[]): Promise<CMAssignmentReturn[]> {
return await this.REST.post<CMAssignmentReturn[]>(`/api/beats/agents_tags/assignments`, {
assignments,
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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.
*/

export interface RestAPIAdapter {
get<ResponseData>(url: string): Promise<ResponseData>;
post<ResponseData>(url: string, body?: { [key: string]: any }): Promise<ResponseData>;
delete<T>(url: string): Promise<T>;
put<ResponseData>(url: string, body?: any): Promise<ResponseData>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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 axios, { AxiosInstance } from 'axios';
import { RestAPIAdapter } from './adapter_types';
let globalAPI: AxiosInstance;

export class AxiosRestAPIAdapter implements RestAPIAdapter {
constructor(
private readonly kbnVersion: string,
private readonly xsrfToken: string,
private readonly basePath: string
) {}

public async get<ResponseData>(url: string): Promise<ResponseData> {
return await this.REST.get(url).then(resp => resp.data);
}

public async post<ResponseData>(
url: string,
body?: { [key: string]: any }
): Promise<ResponseData> {
return await this.REST.post(url, body).then(resp => resp.data);
}

public async delete<T>(url: string): Promise<T> {
return await this.REST.delete(url).then(resp => resp.data);
}

public async put<ResponseData>(url: string, body?: any): Promise<ResponseData> {
return await this.REST.put(url, body).then(resp => resp.data);
}

private get REST() {
if (globalAPI) {
return globalAPI;
}

globalAPI = axios.create({
baseURL: this.basePath,
withCredentials: true,
responseType: 'json',
timeout: 30000,
headers: {
Accept: 'application/json',
credentials: 'same-origin',
'Content-Type': 'application/json',
'kbn-version': this.kbnVersion,
'kbn-xsrf': this.xsrfToken,
},
});
// Add a request interceptor
globalAPI.interceptors.request.use(
config => {
// Do something before request is sent
return config;
},
error => {
// Do something with request error
return Promise.reject(error);
}
);

// Add a response interceptor
globalAPI.interceptors.response.use(
response => {
// Do something with response data
return response;
},
error => {
// Do something with response error
return Promise.reject(error);
}
);

return globalAPI;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import { BeatTag } from '../../../../common/domain_types';
export interface CMTagsAdapter {
getTagsWithIds(tagIds: string[]): Promise<BeatTag[]>;
getAll(): Promise<BeatTag[]>;
upsertTag(tag: BeatTag): Promise<{}>;
upsertTag(tag: BeatTag): Promise<BeatTag | null>;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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 { BeatTag } from '../../../../common/domain_types';
import { RestAPIAdapter } from '../rest_api/adapter_types';
import { CMTagsAdapter } from './adapter_types';

export class RestTagsAdapter implements CMTagsAdapter {
constructor(private readonly REST: RestAPIAdapter) {}

public async getTagsWithIds(tagIds: string[]): Promise<BeatTag[]> {
return await this.REST.get<BeatTag[]>(`/api/beats/tags/${tagIds.join(',')}`);
}

public async getAll(): Promise<BeatTag[]> {
return await this.REST.get<BeatTag[]>(`/api/beats/tags`);
}

public async upsertTag(tag: BeatTag): Promise<BeatTag | null> {
return await this.REST.put<BeatTag>(`/api/beats/tag/{tag}`, {
configuration_blocks: tag.configuration_blocks,
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* 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 { RestAPIAdapter } from '../rest_api/adapter_types';
import { CMTokensAdapter, TokenEnrollmentData } from './adapter_types';

export class RestTokensAdapter implements CMTokensAdapter {
constructor(private readonly REST: RestAPIAdapter) {}

public async createEnrollmentToken(): Promise<TokenEnrollmentData> {
const tokens = await this.REST.post<TokenEnrollmentData[]>('/api/beats/enrollment_tokens');
return tokens[0];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just out of curiosity - why do we only return the first element here? What could be in the rest of the list?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the endpoint supports multiple tokens being created (for use with things like puppet and such), the UI only needs one. One is also the default amount

}
}
19 changes: 12 additions & 7 deletions x-pack/plugins/beats_management/public/lib/compose/kibana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,28 @@

import 'ui/autoload/all';
// @ts-ignore: path dynamic for kibana
import chrome from 'ui/chrome';
// @ts-ignore: path dynamic for kibana
import { management } from 'ui/management';
// @ts-ignore: path dynamic for kibana
import { uiModules } from 'ui/modules';
// @ts-ignore: path dynamic for kibana
import routes from 'ui/routes';
// @ts-ignore: path dynamic for kibana
import { MemoryBeatsAdapter } from '../adapters/beats/memory_beats_adapter';
import { RestBeatsAdapter } from '../adapters/beats/rest_beats_adapter';
import { KibanaFrameworkAdapter } from '../adapters/framework/kibana_framework_adapter';
import { MemoryTagsAdapter } from '../adapters/tags/memory_tags_adapter';
import { MemoryTokensAdapter } from '../adapters/tokens/memory_tokens_adapter';
import { AxiosRestAPIAdapter } from '../adapters/rest_api/axios_rest_api_adapter';
import { RestTagsAdapter } from '../adapters/tags/rest_tags_adapter';
import { RestTokensAdapter } from '../adapters/tokens/rest_tokens_adapter';
import { FrontendDomainLibs, FrontendLibs } from '../lib';

export function compose(): FrontendLibs {
// const kbnVersion = (window as any).__KBN__.version;
const tags = new MemoryTagsAdapter([]);
const tokens = new MemoryTokensAdapter();
const beats = new MemoryBeatsAdapter([]);
const kbnVersion = (window as any).__KBN__.version;
const api = new AxiosRestAPIAdapter(kbnVersion, chrome.getXsrfToken(), chrome.getBasePath());

const tags = new RestTagsAdapter(api);
const tokens = new RestTokensAdapter(api);
const beats = new RestBeatsAdapter(api);

const domainLibs: FrontendDomainLibs = {
tags,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { FrameworkUser } from '../framework/adapter_types';
export interface CMBeatsAdapter {
insert(beat: CMBeat): Promise<void>;
update(beat: CMBeat): Promise<void>;
get(id: string): any;
get(user: FrameworkUser, id: string): any;
getAll(user: FrameworkUser): any;
getWithIds(user: FrameworkUser, beatIds: string[]): any;
removeTagsFromBeats(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ export class ElasticsearchBeatsAdapter implements CMBeatsAdapter {
this.framework = framework;
}

public async get(id: string) {
public async get(user: FrameworkUser, id: string) {
const params = {
id: `beat:${id}`,
ignore: [404],
index: INDEX_NAMES.BEATS,
type: '_doc',
};

const response = await this.database.get(this.framework.internalUser, params);
const response = await this.database.get(user, params);
if (!response.found) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class MemoryBeatsAdapter implements CMBeatsAdapter {
this.beatsDB = beatsDB;
}

public async get(id: string) {
public async get(user: FrameworkUser, id: string) {
return this.beatsDB.find(beat => beat.id === id) || null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
* 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 { get } from 'lodash';

import { FrameworkInternalUser } from './adapter_types';

import {
BackendFrameworkAdapter,
FrameworkRequest,
FrameworkRouteOptions,
FrameworkWrappableRequest,
} from './adapter_types';
Expand All @@ -24,15 +22,15 @@ export class TestingBackendFrameworkAdapter implements BackendFrameworkAdapter {
kind: 'internal',
};
public version: string;
private client: Client | null;
private settings: TestSettings;

constructor(client: Client | null, settings: TestSettings) {
this.client = client;
this.settings = settings || {
constructor(
settings: TestSettings = {
encryptionKey: 'something_who_cares',
enrollmentTokensTtlInSeconds: 10 * 60, // 10 minutes
};
}
) {
this.settings = settings;
this.version = 'testing';
}

Expand All @@ -54,24 +52,4 @@ export class TestingBackendFrameworkAdapter implements BackendFrameworkAdapter {
) {
// not yet testable
}

public installIndexTemplate(name: string, template: {}) {
return;
}

public async callWithInternalUser(esMethod: string, options: {}) {
const api = get<any>(this.client, esMethod);

api(options);

return await api(options);
}

public async callWithRequest(req: FrameworkRequest, esMethod: string, options: {}) {
const api = get<any>(this.client, esMethod);

api(options);

return await api(options);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { BeatTag } from '../../../../common/domain_types';
import { FrameworkUser } from '../framework/adapter_types';

export interface CMTagsAdapter {
getAll(user: FrameworkUser): Promise<BeatTag[]>;
getTagsWithIds(user: FrameworkUser, tagIds: string[]): Promise<BeatTag[]>;
upsertTag(user: FrameworkUser, tag: BeatTag): Promise<{}>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ export class ElasticsearchTagsAdapter implements CMTagsAdapter {
this.database = database;
}

public async getAll(user: FrameworkUser) {
const params = {
index: INDEX_NAMES.BEATS,
q: 'type:tag',
type: '_doc',
};
const response = await this.database.search(user, params);

return get<any>(response, 'hits.hits', []);
}

public async getTagsWithIds(user: FrameworkUser, tagIds: string[]) {
const ids = tagIds.map(tag => `tag:${tag}`);

Expand Down
Loading