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

[eem] add option to delete indices when deleting definition #188116

Merged
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 @@ -10,6 +10,7 @@ import { SavedObjectsClientContract } from '@kbn/core-saved-objects-api-server';
import { EntityDefinition } from '@kbn/entities-schema';
import { Logger } from '@kbn/logging';
import { deleteEntityDefinition } from './delete_entity_definition';
import { deleteIndices } from './delete_index';
import { deleteHistoryIngestPipeline, deleteLatestIngestPipeline } from './delete_ingest_pipeline';
import { findEntityDefinitions } from './find_entity_definition';
import {
Expand All @@ -22,27 +23,34 @@ export async function uninstallEntityDefinition({
esClient,
soClient,
logger,
deleteData = false,
}: {
definition: EntityDefinition;
esClient: ElasticsearchClient;
soClient: SavedObjectsClientContract;
logger: Logger;
deleteData?: boolean;
}) {
await stopAndDeleteHistoryTransform(esClient, definition, logger);
await stopAndDeleteLatestTransform(esClient, definition, logger);
await deleteHistoryIngestPipeline(esClient, definition, logger);
await deleteLatestIngestPipeline(esClient, definition, logger);
await deleteEntityDefinition(soClient, definition, logger);
if (deleteData) {
await deleteIndices(esClient, definition, logger);
}
}

export async function uninstallBuiltInEntityDefinitions({
esClient,
soClient,
logger,
deleteData = false,
}: {
esClient: ElasticsearchClient;
soClient: SavedObjectsClientContract;
logger: Logger;
deleteData?: boolean;
}): Promise<EntityDefinition[]> {
const definitions = await findEntityDefinitions({
soClient,
Expand All @@ -52,7 +60,7 @@ export async function uninstallBuiltInEntityDefinitions({

await Promise.all(
definitions.map(async (definition) => {
await uninstallEntityDefinition({ definition, esClient, soClient, logger });
await uninstallEntityDefinition({ definition, esClient, soClient, logger, deleteData });
})
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { RequestHandlerContext } from '@kbn/core/server';
import { getFakeKibanaRequest } from '@kbn/security-plugin/server/authentication/api_keys/fake_kibana_request';
import { SetupRouteOptions } from '../types';
import { ENTITY_INTERNAL_API_PREFIX } from '../../../common/constants_entities';
import { ManagedEntityEnabledResponse } from '../../../common/types_api';
import { checkIfEntityDiscoveryAPIKeyIsValid, readEntityDiscoveryAPIKey } from '../../lib/auth';
import {
ERROR_API_KEY_NOT_FOUND,
Expand All @@ -25,7 +24,7 @@ export function checkEntityDiscoveryEnabledRoute<T extends RequestHandlerContext
server,
logger,
}: SetupRouteOptions<T>) {
router.get<unknown, unknown, ManagedEntityEnabledResponse>(
Copy link
Contributor Author

@klacabane klacabane Jul 11, 2024

Choose a reason for hiding this comment

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

wrongly assumed this parameter to be the response type instead of the body type

router.get<unknown, unknown, unknown>(
{
path: `${ENTITY_INTERNAL_API_PREFIX}/managed/enablement`,
validate: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { RequestHandlerContext } from '@kbn/core/server';
import { getFakeKibanaRequest } from '@kbn/security-plugin/server/authentication/api_keys/fake_kibana_request';
import { schema } from '@kbn/config-schema';
import { SetupRouteOptions } from '../types';
import { ENTITY_INTERNAL_API_PREFIX } from '../../../common/constants_entities';
import {
Expand All @@ -16,17 +17,20 @@ import {
} from '../../lib/auth';
import { ERROR_API_KEY_NOT_FOUND, ERROR_API_KEY_NOT_VALID } from '../../../common/errors';
import { uninstallBuiltInEntityDefinitions } from '../../lib/entities/uninstall_entity_definition';
import { DisableManagedEntityResponse } from '../../../common/types_api';

export function disableEntityDiscoveryRoute<T extends RequestHandlerContext>({
router,
server,
logger,
}: SetupRouteOptions<T>) {
router.delete<unknown, unknown, DisableManagedEntityResponse>(
router.delete<unknown, { deleteData?: boolean }, unknown>(
{
path: `${ENTITY_INTERNAL_API_PREFIX}/managed/enablement`,
validate: false,
validate: {
query: schema.object({
deleteData: schema.maybe(schema.boolean({ defaultValue: false })),
}),
},
Comment on lines +29 to +33
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we convert this to zod instead? Since that's what we're using for the other endpoints

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure, logged #188171

},
async (context, req, res) => {
try {
Expand All @@ -48,7 +52,12 @@ export function disableEntityDiscoveryRoute<T extends RequestHandlerContext>({
const soClient = server.core.savedObjects.getScopedClient(fakeRequest);
const esClient = server.core.elasticsearch.client.asScoped(fakeRequest).asCurrentUser;

await uninstallBuiltInEntityDefinitions({ soClient, esClient, logger });
await uninstallBuiltInEntityDefinitions({
soClient,
esClient,
logger,
deleteData: req.query.deleteData,
});

await deleteEntityDiscoveryAPIKey((await context.core).savedObjects.client);
await server.security.authc.apiKeys.invalidateAsInternalUser({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { RequestHandlerContext } from '@kbn/core/server';
import { getFakeKibanaRequest } from '@kbn/security-plugin/server/authentication/api_keys/fake_kibana_request';
import { SetupRouteOptions } from '../types';
import { ENTITY_INTERNAL_API_PREFIX } from '../../../common/constants_entities';
import { EnableManagedEntityResponse } from '../../../common/types_api';
import {
canEnableEntityDiscovery,
checkIfAPIKeysAreEnabled,
Expand All @@ -29,7 +28,7 @@ export function enableEntityDiscoveryRoute<T extends RequestHandlerContext>({
server,
logger,
}: SetupRouteOptions<T>) {
router.put<unknown, unknown, EnableManagedEntityResponse>(
router.put<unknown, unknown, unknown>(
{
path: `${ENTITY_INTERNAL_API_PREFIX}/managed/enablement`,
validate: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ export function deleteEntityDefinitionRoute<T extends RequestHandlerContext>({
router,
server,
}: SetupRouteOptions<T>) {
router.delete<{ id: string }, unknown, unknown>(
router.delete<{ id: string }, { deleteData?: boolean }, unknown>(
{
path: `${ENTITY_INTERNAL_API_PREFIX}/definition/{id}`,
validate: {
params: schema.object({
id: schema.string(),
}),
query: schema.object({
deleteData: schema.maybe(schema.boolean({ defaultValue: false })),
}),
Comment on lines +29 to +31
Copy link
Contributor

Choose a reason for hiding this comment

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

zod-ify please

},
},
async (context, req, res) => {
Expand All @@ -35,7 +38,13 @@ export function deleteEntityDefinitionRoute<T extends RequestHandlerContext>({
const esClient = (await context.core).elasticsearch.client.asCurrentUser;

const definition = await readEntityDefinition(soClient, req.params.id, logger);
await uninstallEntityDefinition({ definition, soClient, esClient, logger });
await uninstallEntityDefinition({
definition,
soClient,
esClient,
logger,
deleteData: req.query.deleteData,
});

return res.ok({ body: { acknowledged: true } });
} catch (e) {
Expand Down