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

Create the ftrSoApis FTR plugin #149188

Merged
merged 18 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from 13 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
4 changes: 4 additions & 0 deletions docs/developer/plugin-list.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ for use in their own application.
|Minimal interface for admins to manage files in Kibana.


|{kib-repo}blob/{branch}/src/plugins/ftr_apis/README.md[ftrApis]
|Set of APIs used internally by the FTR.


|{kib-repo}blob/{branch}/src/plugins/guided_onboarding/README.md[guidedOnboarding]
|This plugin contains the code for the Guided Onboarding project. Guided onboarding consists of guides for Solutions (Enterprise Search, Observability, Security) that can be completed as a checklist of steps. The guides help users to ingest their data and to navigate to the correct Solutions pages.

Expand Down
28 changes: 16 additions & 12 deletions packages/kbn-test/src/kbn_client/kbn_client_saved_objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

import { inspect } from 'util';

import * as Rx from 'rxjs';
import { mergeMap } from 'rxjs/operators';
import { isAxiosResponseError } from '@kbn/dev-utils';
Expand Down Expand Up @@ -90,6 +89,11 @@ async function concurrently<T>(maxConcurrency: number, arr: T[], fn: (item: T) =
}
}

/**
* SO client for FTR.
*
* @remarks: Leverage the `ftrApis` plugin under the hood.
*/
export class KbnClientSavedObjects {
constructor(private readonly log: ToolingLog, private readonly requester: KbnClientRequester) {}

Expand Down Expand Up @@ -117,8 +121,8 @@ export class KbnClientSavedObjects {
const { data } = await this.requester.request<SavedObjectResponse<Attributes>>({
description: 'get saved object',
path: options.space
? uriencode`/s/${options.space}/api/saved_objects/${options.type}/${options.id}`
: uriencode`/api/saved_objects/${options.type}/${options.id}`,
? uriencode`/s/${options.space}/internal/ftr/kbn_client_so/${options.type}/${options.id}`
: uriencode`/internal/ftr/kbn_client_so/${options.type}/${options.id}`,
method: 'GET',
});
return data;
Expand All @@ -133,8 +137,8 @@ export class KbnClientSavedObjects {
const { data } = await this.requester.request<SavedObjectResponse<Attributes>>({
description: 'update saved object',
path: options.id
? uriencode`/api/saved_objects/${options.type}/${options.id}`
: uriencode`/api/saved_objects/${options.type}`,
? uriencode`/internal/ftr/kbn_client_so/${options.type}/${options.id}`
: uriencode`/internal/ftr/kbn_client_so/${options.type}`,
query: {
overwrite: options.overwrite,
},
Expand All @@ -156,7 +160,7 @@ export class KbnClientSavedObjects {

const { data } = await this.requester.request<SavedObjectResponse<Attributes>>({
description: 'update saved object',
path: uriencode`/api/saved_objects/${options.type}/${options.id}`,
path: uriencode`/internal/ftr/kbn_client_so/${options.type}/${options.id}`,
query: {
overwrite: options.overwrite,
},
Expand All @@ -179,8 +183,8 @@ export class KbnClientSavedObjects {
const { data } = await this.requester.request({
description: 'delete saved object',
path: options.space
? uriencode`/s/${options.space}/api/saved_objects/${options.type}/${options.id}`
: uriencode`/api/saved_objects/${options.type}/${options.id}`,
? uriencode`/s/${options.space}/internal/ftr/kbn_client_so/${options.type}/${options.id}`
: uriencode`/internal/ftr/kbn_client_so/${options.type}/${options.id}`,
method: 'DELETE',
});

Expand All @@ -196,8 +200,8 @@ export class KbnClientSavedObjects {
const resp = await this.requester.request<FindApiResponse>({
method: 'GET',
path: options.space
? uriencode`/s/${options.space}/api/saved_objects/_find`
: '/api/saved_objects/_find',
? uriencode`/s/${options.space}/internal/ftr/kbn_client_so/_find`
: `/internal/ftr/kbn_client_so/_find`,
query: {
per_page: 1000,
type: options.types,
Expand Down Expand Up @@ -270,8 +274,8 @@ export class KbnClientSavedObjects {
await this.requester.request({
method: 'DELETE',
path: options.space
? uriencode`/s/${options.space}/api/saved_objects/${obj.type}/${obj.id}?force=true`
: uriencode`/api/saved_objects/${obj.type}/${obj.id}?force=true`,
? uriencode`/s/${options.space}/internal/ftr/kbn_client_so/${obj.type}/${obj.id}`
: uriencode`/internal/ftr/kbn_client_so/${obj.type}/${obj.id}`,
});
deleted++;
} catch (error) {
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/ftr_apis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# ftrApis plugin

Set of APIs used internally by the FTR.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: We should be more specific about what APIs these are and that they are only for ftr testing. e.g.
Set of APIs for the kbn server's saved object's client used internally by the functional test runner

or, more concisely:
Set of kbnServer saved objects' client APIs used internally by the functional test runner

16 changes: 16 additions & 0 deletions src/plugins/ftr_apis/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

module.exports = {
preset: '@kbn/test',
rootDir: '../../..',
roots: ['<rootDir>/src/plugins/ftr_apis'],
coverageDirectory: '<rootDir>/target/kibana-coverage/jest/src/plugins/ftr_apis',
coverageReporters: ['text', 'html'],
collectCoverageFrom: ['<rootDir>/src/plugins/ftr_apis/{common,public,server}/**/*.{js,ts,tsx}'],
};
11 changes: 11 additions & 0 deletions src/plugins/ftr_apis/kibana.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"id": "ftrApis",
"owner": {
"name": "Core",
"githubTeam": "kibana-core"
},
"version": "kibana",
"configPath": ["ftr_apis"],
"server": true,
"ui": false
}
20 changes: 20 additions & 0 deletions src/plugins/ftr_apis/server/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { schema, type TypeOf } from '@kbn/config-schema';
import type { PluginConfigDescriptor } from '@kbn/core/server';

const configSchema = schema.object({
disableApis: schema.boolean({ defaultValue: false }),
});

export type ConfigType = TypeOf<typeof configSchema>;

export const config: PluginConfigDescriptor<ConfigType> = {
schema: configSchema,
};
16 changes: 16 additions & 0 deletions src/plugins/ftr_apis/server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { PluginInitializerContext } from '@kbn/core/server';
import { FtrApisPlugin } from './plugin';

export function plugin(initializerContext: PluginInitializerContext) {
return new FtrApisPlugin(initializerContext);
}

export { config } from './config';
28 changes: 28 additions & 0 deletions src/plugins/ftr_apis/server/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { CoreSetup, Plugin, PluginInitializerContext } from '@kbn/core/server';
import { registerRoutes } from './routes';
import type { ConfigType } from './config';

export class FtrApisPlugin implements Plugin {
private readonly config: ConfigType;

constructor(initializerContext: PluginInitializerContext) {
this.config = initializerContext.config.get<ConfigType>();
}

public setup({ http, savedObjects }: CoreSetup) {
const router = http.createRouter();
if (!this.config.disableApis) {
registerRoutes(router);
}
}

public start() {}
}
14 changes: 14 additions & 0 deletions src/plugins/ftr_apis/server/routes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { IRouter } from '@kbn/core/server';
import { registerKbnClientSoRoutes } from './kbn_client_so';

export const registerRoutes = (router: IRouter) => {
registerKbnClientSoRoutes(router);
};
38 changes: 38 additions & 0 deletions src/plugins/ftr_apis/server/routes/kbn_client_so/bulk_delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { IRouter } from '@kbn/core/server';
import { schema } from '@kbn/config-schema';
import { KBN_CLIENT_API_PREFIX, listHiddenTypes, catchAndReturnBoomErrors } from './utils';

export const registerBulkDeleteRoute = (router: IRouter) => {
router.post(
{
path: `${KBN_CLIENT_API_PREFIX}/_bulk_delete`,
options: {
tags: ['access:ftrApis'],
},
validate: {
body: schema.arrayOf(
schema.object({
type: schema.string(),
id: schema.string(),
})
),
},
},
catchAndReturnBoomErrors(async (ctx, req, res) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

FYI: I asked about catchAndReturnBoomErrors and apparently it should be dead / unecessary code and safe to remove. We're probably less likely to replace these APIs in the short to medium term so should we remove it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I wanted to, but it's actually used by the FTR KbnClient. Removing it causes some errors to not be properly wrapped, causing a lot of test suites to fail.

const { savedObjects } = await ctx.core;
const hiddenTypes = listHiddenTypes(savedObjects.typeRegistry);
const soClient = savedObjects.getClient({ includedHiddenTypes: hiddenTypes });

const statuses = await soClient.bulkDelete(req.body, { force: true });
return res.ok({ body: statuses });
})
);
};
62 changes: 62 additions & 0 deletions src/plugins/ftr_apis/server/routes/kbn_client_so/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { IRouter } from '@kbn/core/server';
import { schema } from '@kbn/config-schema';
import { KBN_CLIENT_API_PREFIX, listHiddenTypes, catchAndReturnBoomErrors } from './utils';

export const registerCreateRoute = (router: IRouter) => {
router.post(
{
path: `${KBN_CLIENT_API_PREFIX}/{type}/{id?}`,
options: {
tags: ['access:ftrApis'],
},
validate: {
params: schema.object({
type: schema.string(),
id: schema.maybe(schema.string()),
}),
query: schema.object({
overwrite: schema.boolean({ defaultValue: false }),
}),
body: schema.object({
attributes: schema.recordOf(schema.string(), schema.any()),
migrationVersion: schema.maybe(schema.recordOf(schema.string(), schema.string())),
references: schema.maybe(
schema.arrayOf(
schema.object({
name: schema.string(),
type: schema.string(),
id: schema.string(),
})
)
),
}),
},
},
catchAndReturnBoomErrors(async (ctx, req, res) => {
const { type, id } = req.params;
const { overwrite } = req.query;
const { attributes, migrationVersion, references } = req.body;
const { savedObjects } = await ctx.core;

const hiddenTypes = listHiddenTypes(savedObjects.typeRegistry);
const soClient = savedObjects.getClient({ includedHiddenTypes: hiddenTypes });

const options = {
id,
overwrite,
migrationVersion,
references,
};
const result = await soClient.create(type, attributes, options);
return res.ok({ body: result });
})
);
};
38 changes: 38 additions & 0 deletions src/plugins/ftr_apis/server/routes/kbn_client_so/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { IRouter } from '@kbn/core/server';
import { schema } from '@kbn/config-schema';
import { KBN_CLIENT_API_PREFIX, listHiddenTypes, catchAndReturnBoomErrors } from './utils';

export const registerDeleteRoute = (router: IRouter) => {
router.delete(
{
path: `${KBN_CLIENT_API_PREFIX}/{type}/{id}`,
options: {
tags: ['access:ftrApis'],
},
validate: {
params: schema.object({
type: schema.string(),
id: schema.string(),
}),
},
},
catchAndReturnBoomErrors(async (ctx, req, res) => {
const { type, id } = req.params;
const { savedObjects } = await ctx.core;

const hiddenTypes = listHiddenTypes(savedObjects.typeRegistry);
const soClient = savedObjects.getClient({ includedHiddenTypes: hiddenTypes });

const result = await soClient.delete(type, id, { force: true });
return res.ok({ body: result });
})
);
};
48 changes: 48 additions & 0 deletions src/plugins/ftr_apis/server/routes/kbn_client_so/find.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { IRouter } from '@kbn/core/server';
import { schema } from '@kbn/config-schema';
import { KBN_CLIENT_API_PREFIX, listHiddenTypes, catchAndReturnBoomErrors } from './utils';

export const registerFindRoute = (router: IRouter) => {
router.get(
{
path: `${KBN_CLIENT_API_PREFIX}/_find`,
options: {
tags: ['access:ftrApis'],
},
validate: {
query: schema.object({
per_page: schema.number({ min: 0, defaultValue: 20 }),
page: schema.number({ min: 0, defaultValue: 1 }),
type: schema.oneOf([schema.string(), schema.arrayOf(schema.string())]),
search: schema.maybe(schema.string()),
fields: schema.maybe(schema.oneOf([schema.string(), schema.arrayOf(schema.string())])),
}),
},
},
catchAndReturnBoomErrors(async (ctx, req, res) => {
const query = req.query;

const { savedObjects } = await ctx.core;
const hiddenTypes = listHiddenTypes(savedObjects.typeRegistry);
const soClient = savedObjects.getClient({ includedHiddenTypes: hiddenTypes });

const result = await soClient.find({
perPage: query.per_page,
page: query.page,
type: Array.isArray(query.type) ? query.type : [query.type],
search: query.search,
fields: typeof query.fields === 'string' ? [query.fields] : query.fields,
});

return res.ok({ body: result });
})
);
};
Loading