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

[ml] migrate file_data_visualizer/import route to file_upload plugin #89640

Merged
merged 26 commits into from
Feb 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
f19cece
migrate file_upload plugin to maps_file_upload
nreese Jan 25, 2021
005170b
update plugins list
nreese Jan 25, 2021
be1e122
migrate ml import endpoint
nreese Jan 25, 2021
1b12e4c
merge with master
nreese Jan 27, 2021
179e7ff
migrate ml telemetry to file_upload plugin
nreese Jan 27, 2021
1da2023
add fileUpload plugin to ml
nreese Jan 27, 2021
89bbe88
Merge branch 'master' of github.com:elastic/kibana into ml_file_upload
nreese Jan 27, 2021
4d9680c
add TS project
nreese Jan 27, 2021
d1073bf
Merge branch 'master' of github.com:elastic/kibana into ml_file_upload
nreese Jan 28, 2021
044ec4f
update ML to use file_upload endpoint
nreese Jan 28, 2021
de0ce16
move types to file_upload plugin
nreese Jan 28, 2021
8e1c90e
ignore error
nreese Jan 28, 2021
dd57ecd
clean up
nreese Jan 28, 2021
7edbade
i18n clean-up
nreese Jan 28, 2021
1079c41
remove schemas from ml
nreese Jan 28, 2021
ed0e477
remove usageCollection from ml
nreese Jan 28, 2021
3bc38e2
node scripts/build_plugin_list_docs
nreese Jan 28, 2021
d7c3811
Merge branch 'master' into ml_file_upload
kibanamachine Jan 28, 2021
3b87cbf
update telemety collector
nreese Jan 28, 2021
baeb6db
Merge branch 'ml_file_upload' of github.com:nreese/kibana into ml_fil…
nreese Jan 28, 2021
0b31512
revert changes to ingestPipeline schema
nreese Jan 29, 2021
26e05b2
change name of TELEMETRY_DOC_ID to unique value
nreese Jan 29, 2021
0473f5a
remove ImportFile from ml/server/routes/apidoc.json
nreese Jan 29, 2021
c4d050e
merge with master
nreese Feb 1, 2021
6d3bf3a
fix typo in x=pack/tsconfig.json
nreese Feb 1, 2021
82b4a1b
Merge branch 'master' into ml_file_upload
kibanamachine Feb 2, 2021
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
4 changes: 4 additions & 0 deletions docs/developer/plugin-list.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,10 @@ and actions.
|The features plugin enhance Kibana with a per-feature privilege system.


|{kib-repo}blob/{branch}/x-pack/plugins/file_upload[fileUpload]
|WARNING: Missing README.


|{kib-repo}blob/{branch}/x-pack/plugins/fleet/README.md[fleet]
|Fleet needs to have Elasticsearch API keys enabled, and also to have TLS enabled on kibana, (if you want to run Kibana without TLS you can provide the following config flag --xpack.fleet.agents.tlsCheckDisabled=false)

Expand Down
6 changes: 3 additions & 3 deletions src/plugins/telemetry/schema/oss_plugins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4097,6 +4097,9 @@
"xpackDashboardMode:roles": {
"type": "keyword"
},
"securitySolution:ipReputationLinks": {
"type": "keyword"
},
"visualize:enableLabs": {
"type": "boolean"
},
Expand All @@ -4115,9 +4118,6 @@
"visualization:tileMap:maxPrecision": {
"type": "long"
},
"securitySolution:ipReputationLinks": {
"type": "keyword"
},
"csv:separator": {
"type": "keyword"
},
Expand Down
8 changes: 8 additions & 0 deletions x-pack/plugins/file_upload/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* 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 * from './constants';
export * from './types';
54 changes: 54 additions & 0 deletions x-pack/plugins/file_upload/common/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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 ImportResponse {
success: boolean;
id: string;
index?: string;
pipelineId?: string;
docCount: number;
failures: ImportFailure[];
error?: any;
ingestError?: boolean;
}

export interface ImportFailure {
item: number;
reason: string;
doc: ImportDoc;
}

export interface Doc {
message: string;
}

export type ImportDoc = Doc | string;

export interface Settings {
pipeline?: string;
index: string;
body: any[];
[key: string]: any;
}

export interface Mappings {
_meta?: {
created_by: string;
};
properties: {
[key: string]: any;
};
}

export interface IngestPipelineWrapper {
id: string;
pipeline: IngestPipeline;
}

export interface IngestPipeline {
description: string;
processors: any[];
}
11 changes: 11 additions & 0 deletions x-pack/plugins/file_upload/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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.
*/

module.exports = {
preset: '@kbn/test',
rootDir: '../../..',
roots: ['<rootDir>/x-pack/plugins/file_upload'],
};
8 changes: 8 additions & 0 deletions x-pack/plugins/file_upload/kibana.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": "fileUpload",
"version": "8.0.0",
"kibanaVersion": "kibana",
"server": true,
"ui": false,
"requiredPlugins": ["usageCollection"]
}
23 changes: 23 additions & 0 deletions x-pack/plugins/file_upload/server/error_wrapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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 { boomify, isBoom } from '@hapi/boom';
import { ResponseError, CustomHttpResponseOptions } from 'kibana/server';

export function wrapError(error: any): CustomHttpResponseOptions<ResponseError> {
const boom = isBoom(error)
? error
: boomify(error, { statusCode: error.status ?? error.statusCode });
const statusCode = boom.output.statusCode;
return {
body: {
message: boom,
...(statusCode !== 500 && error.body ? { attributes: { body: error.body } } : {}),
},
headers: boom.output.headers as { [key: string]: string },
statusCode,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
*/

import { IScopedClusterClient } from 'kibana/server';
import { INDEX_META_DATA_CREATED_BY } from '../../../common/constants/file_datavisualizer';
import { INDEX_META_DATA_CREATED_BY } from '../common/constants';
import {
ImportResponse,
ImportFailure,
Settings,
Mappings,
IngestPipelineWrapper,
} from '../../../common/types/file_datavisualizer';
import { InputData } from './file_data_visualizer';
} from '../common';

export type InputData = any[];

export function importDataProvider({ asCurrentUser }: IScopedClusterClient) {
async function importData(
id: string,
id: string | undefined,
index: string,
settings: Settings,
mappings: Mappings,
Expand Down Expand Up @@ -77,7 +78,7 @@ export function importDataProvider({ asCurrentUser }: IScopedClusterClient) {
} catch (error) {
return {
success: false,
id,
id: id!,
index: createdIndex,
pipelineId: createdPipelineId,
error: error.body !== undefined ? error.body : error,
Expand Down
9 changes: 9 additions & 0 deletions x-pack/plugins/file_upload/server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* 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 { FileUploadPlugin } from './plugin';

export const plugin = () => new FileUploadPlugin();
24 changes: 24 additions & 0 deletions x-pack/plugins/file_upload/server/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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 { CoreSetup, CoreStart, Plugin } from 'src/core/server';
import { fileUploadRoutes } from './routes';
import { initFileUploadTelemetry } from './telemetry';
import { UsageCollectionSetup } from '../../../../src/plugins/usage_collection/server';

interface SetupDeps {
usageCollection: UsageCollectionSetup;
}

export class FileUploadPlugin implements Plugin {
async setup(coreSetup: CoreSetup, plugins: SetupDeps) {
fileUploadRoutes(coreSetup.http.createRouter());

initFileUploadTelemetry(coreSetup, plugins.usageCollection);
}

start(core: CoreStart) {}
}
85 changes: 85 additions & 0 deletions x-pack/plugins/file_upload/server/routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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 { IRouter, IScopedClusterClient } from 'kibana/server';
import { MAX_FILE_SIZE_BYTES, IngestPipelineWrapper, Mappings, Settings } from '../common';
import { wrapError } from './error_wrapper';
import { InputData, importDataProvider } from './import_data';

import { updateTelemetry } from './telemetry';
import { importFileBodySchema, importFileQuerySchema } from './schemas';

function importData(
client: IScopedClusterClient,
id: string | undefined,
index: string,
settings: Settings,
mappings: Mappings,
ingestPipeline: IngestPipelineWrapper,
data: InputData
) {
const { importData: importDataFunc } = importDataProvider(client);
return importDataFunc(id, index, settings, mappings, ingestPipeline, data);
}

/**
* Routes for the file upload.
*/
export function fileUploadRoutes(router: IRouter) {
/**
* @apiGroup FileDataVisualizer
*
* @api {post} /api/file_upload/import Import file data
* @apiName ImportFile
* @apiDescription Imports file data into elasticsearch index.
*
* @apiSchema (query) importFileQuerySchema
* @apiSchema (body) importFileBodySchema
*/
router.post(
{
path: '/api/file_upload/import',
validate: {
query: importFileQuerySchema,
body: importFileBodySchema,
},
options: {
body: {
accepts: ['application/json'],
maxBytes: MAX_FILE_SIZE_BYTES,
},
tags: ['access:ml:canFindFileStructure'],
},
},
async (context, request, response) => {
try {
const { id } = request.query;
const { index, data, settings, mappings, ingestPipeline } = request.body;

// `id` being `undefined` tells us that this is a new import due to create a new index.
// follow-up import calls to just add additional data will include the `id` of the created
// index, we'll ignore those and don't increment the counter.
if (id === undefined) {
await updateTelemetry();
}

const result = await importData(
context.core.elasticsearch.client,
id,
index,
settings,
mappings,
// @ts-expect-error
ingestPipeline,
data
);
return response.ok({ body: result });
} catch (e) {
return response.customError(wrapError(e));
}
}
);
}
24 changes: 24 additions & 0 deletions x-pack/plugins/file_upload/server/schemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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 { schema } from '@kbn/config-schema';

export const importFileQuerySchema = schema.object({
id: schema.maybe(schema.string()),
});

export const importFileBodySchema = schema.object({
index: schema.string(),
data: schema.arrayOf(schema.any()),
settings: schema.maybe(schema.any()),
/** Mappings */
mappings: schema.any(),
/** Ingest pipeline definition */
ingestPipeline: schema.object({
id: schema.maybe(schema.string()),
pipeline: schema.maybe(schema.any()),
}),
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
* you may not use this file except in compliance with the Elastic License.
*/

export { initMlTelemetry } from './ml_usage_collector';
export { initFileUploadTelemetry } from './usage_collector';
export { updateTelemetry } from './telemetry';
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import { SavedObjectsType } from 'src/core/server';
import { TELEMETRY_DOC_ID } from './telemetry';

export const mlTelemetryMappingsType: SavedObjectsType = {
export const telemetryMappingsType: SavedObjectsType = {
name: TELEMETRY_DOC_ID,
hidden: false,
namespaceType: 'agnostic',
mappings: {
properties: {
file_data_visualizer: {
file_upload: {
properties: {
index_creation_count: {
nreese marked this conversation as resolved.
Show resolved Hide resolved
type: 'long',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('ml plugin telemetry', () => {
it('should update existing telemetry', async () => {
const internalRepo = mockInit({
attributes: {
file_data_visualizer: {
file_upload: {
index_creation_count: 2,
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import { ISavedObjectsRepository } from 'kibana/server';

import { getInternalRepository } from './internal_repository';

export const TELEMETRY_DOC_ID = 'ml-telemetry';
export const TELEMETRY_DOC_ID = 'file-upload-usage-collection-telemetry';

export interface Telemetry {
file_data_visualizer: {
file_upload: {
index_creation_count: number;
};
}
Expand All @@ -23,7 +23,7 @@ export interface TelemetrySavedObject {

export function initTelemetry(): Telemetry {
return {
file_data_visualizer: {
file_upload: {
index_creation_count: 0,
},
};
Expand Down Expand Up @@ -74,8 +74,8 @@ export async function updateTelemetry(internalRepo?: ISavedObjectsRepository) {

function incrementCounts(telemetry: Telemetry) {
Copy link
Member

Choose a reason for hiding this comment

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

Any reason not using SavedObjectsRepository.incrementCounter?

Recently we've added support to increment multiple fields with custom increments:

incrementCounter<T = unknown>(type: string, id: string, counterFields: Array<string | SavedObjectsIncrementCounterField>, options?: SavedObjectsIncrementCounterOptions): Promise<SavedObject<T>>;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Bamieh This PR is migrating code from ML to a separate plugin, trying to change as little as possible. I can create an issue to track using SavedObjectsRepository.incrementCounter but would like to leave any refactoring out of this initial file_upload creation pull request.

return {
file_data_visualizer: {
index_creation_count: telemetry.file_data_visualizer.index_creation_count + 1,
file_upload: {
index_creation_count: telemetry.file_upload.index_creation_count + 1,
},
};
}
Loading