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

[BeatsCM] Log token errors to the server #27170

Merged
merged 2 commits into from
Dec 14, 2018
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 @@ -32,7 +32,7 @@ export class KibanaDatabaseAdapter implements DatabaseAdapter {
}
public async putTemplate(user: FrameworkUser, params: DatabasePutTemplateParams): Promise<any> {
const callES = this.getCallType(user);
const result = await callES('indices.putTemplate', params);
const result: { acknowledged: boolean } = await callES('indices.putTemplate', params);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,17 @@ export class ElasticsearchTokensAdapter implements CMTokensAdapter {
])
);

await this.database.bulk(user, {
const result = await this.database.bulk(user, {
body,
index: INDEX_NAMES.BEATS,
refresh: 'wait_for',
type: '_doc',
});

if (result.errors) {
throw new Error(result.items[0].result);
}

return tokens;
}
}
5 changes: 3 additions & 2 deletions x-pack/plugins/beats_management/server/lib/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@

import Boom from 'boom';
import { difference } from 'lodash';
import { FrameworkRouteHandler } from './adapters/framework/adapter_types';
import { FrameworkRequest } from './adapters/framework/adapter_types';
import {
BackendFrameworkAdapter,
FrameworkRequest,
FrameworkResponse,
FrameworkRouteHandler,
FrameworkRouteOptions,
} from './adapters/framework/adapter_types';

export class BackendFrameworkLib {
public log = this.adapter.log;
public exposeStaticDir = this.adapter.exposeStaticDir;
public internalUser = this.adapter.internalUser;
constructor(private readonly adapter: BackendFrameworkAdapter) {
Expand Down
11 changes: 10 additions & 1 deletion x-pack/plugins/beats_management/server/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import { DatabaseAdapter } from './adapters/database/adapter_types';
import { FrameworkUser } from './adapters/framework/adapter_types';

import { CMBeatsDomain } from './beats';
import { BackendFrameworkLib } from './framework';
import { CMTagsDomain } from './tags';
Expand All @@ -27,3 +26,13 @@ export enum BeatEnrollmentStatus {
ExpiredEnrollmentToken = 'Expired enrollment token',
InvalidEnrollmentToken = 'Invalid enrollment token',
}

export interface AsyncResponse<DataType = any> {
error: {
code: number | string;
message: string;
};
}
export interface AsyncResponse<DataType = any> {
data: DataType;
}
7 changes: 4 additions & 3 deletions x-pack/plugins/beats_management/server/management_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { INDEX_NAMES } from '../common/constants/index_names';
import { CMServerLibs } from './lib/types';
import { createGetBeatConfigurationRoute } from './rest_api/beats/configuration';
import { createBeatEnrollmentRoute } from './rest_api/beats/enroll';
Expand All @@ -19,10 +20,10 @@ import { createSetTagRoute } from './rest_api/tags/set';
import { createTokensRoute } from './rest_api/tokens/create';
import { beatsIndexTemplate } from './utils/index_templates';

export const initManagementServer = (libs: CMServerLibs) => {
export const initManagementServer = async (libs: CMServerLibs) => {
if (libs.database) {
libs.database.putTemplate(libs.framework.internalUser, {
name: '.management-beats',
await libs.database.putTemplate(libs.framework.internalUser, {
name: INDEX_NAMES.BEATS,
body: beatsIndexTemplate,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
* you may not use this file except in compliance with the Elastic License.
*/

import Boom from 'boom';
import Joi from 'joi';
import { get } from 'lodash';
import { REQUIRED_LICENSES } from 'x-pack/plugins/beats_management/common/constants';
import { FrameworkRequest } from '../../lib/adapters/framework/adapter_types';
import { CMServerLibs } from '../../lib/types';
import { wrapEsError } from '../../utils/error_wrappers';

// TODO: write to Kibana audit log file
const DEFAULT_NUM_TOKENS = 1;
Expand All @@ -35,8 +35,8 @@ export const createTokensRoute = (libs: CMServerLibs) => ({
const tokens = await libs.tokens.createEnrollmentTokens(request.user, numTokens);
return { tokens };
} catch (err) {
// TODO move this to kibana route thing in adapter
return wrapEsError(err);
libs.framework.log(err.message);
return Boom.internal();
}
},
});