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

POC - Export/Import Hidden Types #84976

Closed
wants to merge 7 commits into from
Closed
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
10 changes: 9 additions & 1 deletion src/core/server/core_route_handler_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
import { InternalCoreStart } from './internal_types';
import { KibanaRequest } from './http/router';
import { SavedObjectsClientContract } from './saved_objects/types';
import { InternalSavedObjectsServiceStart, ISavedObjectTypeRegistry } from './saved_objects';
import {
InternalSavedObjectsServiceStart,
ISavedObjectTypeRegistry,
SavedObjectsClientProviderOptions,
} from './saved_objects';
import {
InternalElasticsearchServiceStart,
IScopedClusterClient,
Expand Down Expand Up @@ -78,6 +82,10 @@ class CoreSavedObjectsRouteHandlerContext {
}
return this.#typeRegistry;
}

public getClient(options?: SavedObjectsClientProviderOptions) {
return this.savedObjectsStart.getScopedClient(this.request, options);
}
}

class CoreUiSettingsRouteHandlerContext {
Expand Down
2 changes: 2 additions & 0 deletions src/core/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import { IUiSettingsClient, UiSettingsServiceSetup, UiSettingsServiceStart } fro
import { SavedObjectsClientContract } from './saved_objects/types';
import {
ISavedObjectTypeRegistry,
SavedObjectsClientProviderOptions,
SavedObjectsServiceSetup,
SavedObjectsServiceStart,
} from './saved_objects';
Expand Down Expand Up @@ -390,6 +391,7 @@ export interface RequestHandlerContext {
savedObjects: {
client: SavedObjectsClientContract;
typeRegistry: ISavedObjectTypeRegistry;
getClient(options?: SavedObjectsClientProviderOptions): SavedObjectsClientContract;
};
elasticsearch: {
client: IScopedClusterClient;
Expand Down
8 changes: 7 additions & 1 deletion src/core/server/saved_objects/routes/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ export const registerExportRoute = (router: IRouter, config: SavedObjectConfig)
},
},
router.handleLegacyErrors(async (context, req, res) => {
const savedObjectsClient = context.core.savedObjects.client;
const {
type,
hasReference,
Expand All @@ -74,6 +73,13 @@ export const registerExportRoute = (router: IRouter, config: SavedObjectConfig)
const supportedTypes = context.core.savedObjects.typeRegistry
.getImportableAndExportableTypes()
.map((t) => t.name);

const includedHiddenTypes = supportedTypes.filter((supportedType) =>
context.core.savedObjects.typeRegistry.isHidden(supportedType)
);

const savedObjectsClient = context.core.savedObjects.getClient({ includedHiddenTypes });

if (types) {
const validationError = validateTypes(types, supportedTypes);
if (validationError) {
Expand Down
12 changes: 11 additions & 1 deletion src/core/server/saved_objects/routes/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,18 @@ export const registerImportRoute = (router: IRouter, config: SavedObjectConfig)
});
}

const supportedTypes = context.core.savedObjects.typeRegistry
.getImportableAndExportableTypes()
.map((t) => t.name);

const includedHiddenTypes = supportedTypes.filter((supportedType) =>
context.core.savedObjects.typeRegistry.isHidden(supportedType)
);

const savedObjectsClient = context.core.savedObjects.getClient({ includedHiddenTypes });

const result = await importSavedObjectsFromStream({
savedObjectsClient: context.core.savedObjects.client,
savedObjectsClient,
typeRegistry: context.core.savedObjects.typeRegistry,
readStream,
objectLimit: maxImportExportSize,
Expand Down
12 changes: 11 additions & 1 deletion src/core/server/saved_objects/routes/resolve_import_errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,19 @@ export const registerResolveImportErrorsRoute = (router: IRouter, config: SavedO
});
}

const supportedTypes = context.core.savedObjects.typeRegistry
.getImportableAndExportableTypes()
.map((t) => t.name);

const includedHiddenTypes = supportedTypes.filter((supportedType) =>
context.core.savedObjects.typeRegistry.isHidden(supportedType)
);

const savedObjectsClient = context.core.savedObjects.getClient({ includedHiddenTypes });

const result = await resolveSavedObjectsImportErrors({
typeRegistry: context.core.savedObjects.typeRegistry,
savedObjectsClient: context.core.savedObjects.client,
savedObjectsClient,
readStream,
retries: req.body.retries,
objectLimit: maxImportExportSize,
Expand Down
4 changes: 3 additions & 1 deletion src/plugins/saved_objects_management/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ export class SavedObjectsManagementPlugin
this.logger = this.context.logger.get();
}

public async setup({ http, capabilities }: CoreSetup) {
public async setup({ http, capabilities, getStartServices }: CoreSetup) {
this.logger.debug('Setting up SavedObjectsManagement plugin');
const savedObjectsStartPromise = getStartServices().then(([core]) => core.savedObjects);
registerRoutes({
savedObjectsStartPromise,
http,
managementServicePromise: this.managementService$.pipe(first()).toPromise(),
});
Expand Down
14 changes: 11 additions & 3 deletions src/plugins/saved_objects_management/server/routes/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
*/

import { schema } from '@kbn/config-schema';
import { IRouter } from 'src/core/server';
import { IRouter, SavedObjectsServiceStart } from 'src/core/server';
import { injectMetaAttributes } from '../lib';
import { ISavedObjectsManagement } from '../services';

export const registerFindRoute = (
router: IRouter,
managementServicePromise: Promise<ISavedObjectsManagement>
managementServicePromise: Promise<ISavedObjectsManagement>,
savedObjectsStartPromise: Promise<SavedObjectsServiceStart>
) => {
const referenceSchema = schema.object({
type: schema.string(),
Expand Down Expand Up @@ -57,14 +58,21 @@ export const registerFindRoute = (
},
router.handleLegacyErrors(async (context, req, res) => {
const managementService = await managementServicePromise;
const { client } = context.core.savedObjects;
const savedObjects = await savedObjectsStartPromise;
const searchTypes = Array.isArray(req.query.type) ? req.query.type : [req.query.type];
const includedFields = Array.isArray(req.query.fields)
? req.query.fields
: [req.query.fields];
const importAndExportableTypes = searchTypes.filter((type) =>
managementService.isImportAndExportable(type)
);
const includedHiddenTypes = importAndExportableTypes.filter((type) =>
managementService.isHidden(type)
);

const client = savedObjects.getScopedClient(req, {
includedHiddenTypes,
});

const searchFields = new Set<string>();
importAndExportableTypes.forEach((type) => {
Expand Down
11 changes: 8 additions & 3 deletions src/plugins/saved_objects_management/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { HttpServiceSetup } from 'src/core/server';
import { HttpServiceSetup, SavedObjectsServiceStart } from 'src/core/server';
import { ISavedObjectsManagement } from '../services';
import { registerFindRoute } from './find';
import { registerGetRoute } from './get';
Expand All @@ -29,11 +29,16 @@ import { registerGetAllowedTypesRoute } from './get_allowed_types';
interface RegisterRouteOptions {
http: HttpServiceSetup;
managementServicePromise: Promise<ISavedObjectsManagement>;
savedObjectsStartPromise: Promise<SavedObjectsServiceStart>;
}

export function registerRoutes({ http, managementServicePromise }: RegisterRouteOptions) {
export function registerRoutes({
http,
managementServicePromise,
savedObjectsStartPromise,
}: RegisterRouteOptions) {
const router = http.createRouter();
registerFindRoute(router, managementServicePromise);
registerFindRoute(router, managementServicePromise, savedObjectsStartPromise);
registerGetRoute(router, managementServicePromise);
registerScrollForCountRoute(router);
registerScrollForExportRoute(router);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export class SavedObjectsManagement {
return this.registry.isImportableAndExportable(type);
}

public isHidden(type: string) {
return this.registry.isHidden(type);
}

public getDefaultSearchField(type: string) {
return this.registry.getType(type)?.management?.defaultSearchField;
}
Expand Down
9 changes: 9 additions & 0 deletions x-pack/plugins/actions/server/saved_objects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ export function setupSavedObjects(
namespaceType: 'single',
mappings: mappings.action,
migrations: getMigrations(encryptedSavedObjects),
management: {
importableAndExportable: true,
icon: 'tokenEvent',
defaultSearchField: 'name',
getTitle(obj) {
return obj.attributes.name;
},
},
});

// Encrypted attributes
Expand All @@ -33,6 +41,7 @@ export function setupSavedObjects(
type: ACTION_SAVED_OBJECT_TYPE,
attributesToEncrypt: new Set(['secrets']),
attributesToExcludeFromAAD: new Set(['name']),
allowPredefinedID: true,
});

savedObjects.registerType({
Expand Down
9 changes: 9 additions & 0 deletions x-pack/plugins/alerts/server/saved_objects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ export function setupSavedObjects(
namespaceType: 'single',
migrations: getMigrations(encryptedSavedObjects),
mappings: mappings.alert,
management: {
importableAndExportable: true,
icon: 'alert',
defaultSearchField: 'name',
getTitle(obj) {
return obj.attributes.name;
},
},
});

savedObjects.registerType({
Expand All @@ -65,6 +73,7 @@ export function setupSavedObjects(
type: 'alert',
attributesToEncrypt: new Set(['apiKey']),
attributesToExcludeFromAAD: new Set(AlertAttributesExcludedFromAAD),
allowPredefinedID: true,
});

// Encrypted attributes
Expand Down
13 changes: 8 additions & 5 deletions x-pack/plugins/features/server/oss_features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
*/
import { i18n } from '@kbn/i18n';
import { KibanaFeatureConfig } from '../common';
import { DEFAULT_APP_CATEGORIES } from '../../../../src/core/server';
import { DEFAULT_APP_CATEGORIES, ISavedObjectTypeRegistry } from '../../../../src/core/server';

export interface BuildOSSFeaturesParams {
savedObjectTypes: string[];
savedObjectTypeRegistry: ISavedObjectTypeRegistry;
includeTimelion: boolean;
}

export const buildOSSFeatures = ({ savedObjectTypes, includeTimelion }: BuildOSSFeaturesParams) => {
export const buildOSSFeatures = ({
savedObjectTypeRegistry,
includeTimelion,
}: BuildOSSFeaturesParams) => {
return [
{
id: 'discover',
Expand Down Expand Up @@ -346,7 +349,7 @@ export const buildOSSFeatures = ({ savedObjectTypes, includeTimelion }: BuildOSS
},
api: ['copySavedObjectsToSpaces'],
savedObject: {
all: [...savedObjectTypes],
all: [...savedObjectTypeRegistry.getImportableAndExportableTypes().map((t) => t.name)],
read: [],
},
ui: ['read', 'edit', 'delete', 'copyIntoSpace', 'shareIntoSpace'],
Expand All @@ -360,7 +363,7 @@ export const buildOSSFeatures = ({ savedObjectTypes, includeTimelion }: BuildOSS
api: ['copySavedObjectsToSpaces'],
savedObject: {
all: [],
read: [...savedObjectTypes],
read: [...savedObjectTypeRegistry.getImportableAndExportableTypes().map((t) => t.name)],
},
ui: ['read'],
},
Expand Down
11 changes: 3 additions & 8 deletions x-pack/plugins/features/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,11 @@ export class Plugin {
public stop() {}

private registerOssFeatures(savedObjects: SavedObjectsServiceStart) {
const registry = savedObjects.getTypeRegistry();
const savedObjectTypes = registry.getVisibleTypes().map((t) => t.name);
const savedObjectTypeRegistry = savedObjects.getTypeRegistry();

this.logger.debug(
`Registering OSS features with SO types: ${savedObjectTypes.join(', ')}. "includeTimelion": ${
this.isTimelionEnabled
}.`
);
this.logger.debug(`Registering OSS features. "includeTimelion": ${this.isTimelionEnabled}.`);
const features = buildOSSFeatures({
savedObjectTypes,
savedObjectTypeRegistry,
includeTimelion: this.isTimelionEnabled,
});

Expand Down