-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RAM][HTTP Versioning] Version DELETE Rule Route (#181521)
## Summary Parent Issue: #157883 Issue: #181513 Versions the DELETE rule endpoint with added input validation. `DELETE /api/alerting/rule/{id}` ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios
- Loading branch information
Showing
19 changed files
with
278 additions
and
145 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
x-pack/plugins/alerting/common/routes/rule/apis/delete/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export { deleteRuleRequestParamsSchema } from './schemas/latest'; | ||
export type { DeleteRuleRequestParams } from './types/latest'; | ||
|
||
export { deleteRuleRequestParamsSchema as deleteRuleRequestParamsSchemaV1 } from './schemas/v1'; | ||
export type { DeleteRuleRequestParams as DeleteRuleRequestParamsV1 } from './types/v1'; |
8 changes: 8 additions & 0 deletions
8
x-pack/plugins/alerting/common/routes/rule/apis/delete/schemas/latest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export * from './v1'; |
12 changes: 12 additions & 0 deletions
12
x-pack/plugins/alerting/common/routes/rule/apis/delete/schemas/v1.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { schema } from '@kbn/config-schema'; | ||
|
||
export const deleteRuleRequestParamsSchema = schema.object({ | ||
id: schema.string(), | ||
}); |
8 changes: 8 additions & 0 deletions
8
x-pack/plugins/alerting/common/routes/rule/apis/delete/types/latest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export * from './v1'; |
11 changes: 11 additions & 0 deletions
11
x-pack/plugins/alerting/common/routes/rule/apis/delete/types/v1.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type { TypeOf } from '@kbn/config-schema'; | ||
import { deleteRuleRequestParamsSchemaV1 } from '..'; | ||
|
||
export type DeleteRuleRequestParams = TypeOf<typeof deleteRuleRequestParamsSchemaV1>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
x-pack/plugins/alerting/server/application/rule/methods/delete/delete_rule.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import Boom from '@hapi/boom'; | ||
import { AlertConsumers } from '@kbn/rule-data-utils'; | ||
import { RawRule } from '../../../../types'; | ||
import { WriteOperations, AlertingAuthorizationEntity } from '../../../../authorization'; | ||
import { retryIfConflicts } from '../../../../lib/retry_if_conflicts'; | ||
import { bulkMarkApiKeysForInvalidation } from '../../../../invalidate_pending_api_keys/bulk_mark_api_keys_for_invalidation'; | ||
import { ruleAuditEvent, RuleAuditAction } from '../../../../rules_client/common/audit_events'; | ||
import { RulesClientContext } from '../../../../rules_client/types'; | ||
import { untrackRuleAlerts, migrateLegacyActions } from '../../../../rules_client/lib'; | ||
import { RuleAttributes } from '../../../../data/rule/types'; | ||
import { RULE_SAVED_OBJECT_TYPE } from '../../../../saved_objects'; | ||
import { DeleteRuleParams } from './types'; | ||
import { deleteRuleParamsSchema } from './schemas'; | ||
import { deleteRuleSo, getDecryptedRuleSo, getRuleSo } from '../../../../data/rule'; | ||
|
||
export async function deleteRule(context: RulesClientContext, params: DeleteRuleParams) { | ||
try { | ||
deleteRuleParamsSchema.validate(params); | ||
} catch (error) { | ||
throw Boom.badRequest(`Error validating delete params - ${error.message}`); | ||
} | ||
|
||
const { id } = params; | ||
|
||
return await retryIfConflicts( | ||
context.logger, | ||
`rulesClient.delete('${id}')`, | ||
async () => await deleteRuleWithOCC(context, { id }) | ||
); | ||
} | ||
|
||
async function deleteRuleWithOCC(context: RulesClientContext, { id }: { id: string }) { | ||
let taskIdToRemove: string | undefined | null; | ||
let apiKeyToInvalidate: string | null = null; | ||
let apiKeyCreatedByUser: boolean | undefined | null = false; | ||
let attributes: RuleAttributes; | ||
|
||
try { | ||
const decryptedRule = await getDecryptedRuleSo({ | ||
encryptedSavedObjectsClient: context.encryptedSavedObjectsClient, | ||
id, | ||
savedObjectsGetOptions: { | ||
namespace: context.namespace, | ||
}, | ||
}); | ||
apiKeyToInvalidate = decryptedRule.attributes.apiKey; | ||
apiKeyCreatedByUser = decryptedRule.attributes.apiKeyCreatedByUser; | ||
taskIdToRemove = decryptedRule.attributes.scheduledTaskId; | ||
attributes = decryptedRule.attributes; | ||
} catch (e) { | ||
// We'll skip invalidating the API key since we failed to load the decrypted saved object | ||
context.logger.error( | ||
`delete(): Failed to load API key to invalidate on alert ${id}: ${e.message}` | ||
); | ||
|
||
// Still attempt to load the scheduledTaskId using SOC | ||
const rule = await getRuleSo({ | ||
savedObjectsClient: context.unsecuredSavedObjectsClient, | ||
id, | ||
}); | ||
taskIdToRemove = rule.attributes.scheduledTaskId; | ||
attributes = rule.attributes; | ||
} | ||
|
||
try { | ||
await context.authorization.ensureAuthorized({ | ||
ruleTypeId: attributes.alertTypeId, | ||
consumer: attributes.consumer, | ||
operation: WriteOperations.Delete, | ||
entity: AlertingAuthorizationEntity.Rule, | ||
}); | ||
} catch (error) { | ||
context.auditLogger?.log( | ||
ruleAuditEvent({ | ||
action: RuleAuditAction.DELETE, | ||
savedObject: { type: RULE_SAVED_OBJECT_TYPE, id }, | ||
error, | ||
}) | ||
); | ||
throw error; | ||
} | ||
|
||
await untrackRuleAlerts(context, id, attributes); | ||
|
||
// migrate legacy actions only for SIEM rules | ||
// TODO (http-versioning): Remove this cast, this enables us to move forward | ||
// without fixing all of other solution types | ||
if (attributes.consumer === AlertConsumers.SIEM) { | ||
await migrateLegacyActions(context, { | ||
ruleId: id, | ||
attributes: attributes as RawRule, | ||
skipActionsValidation: true, | ||
}); | ||
} | ||
|
||
context.auditLogger?.log( | ||
ruleAuditEvent({ | ||
action: RuleAuditAction.DELETE, | ||
outcome: 'unknown', | ||
savedObject: { type: RULE_SAVED_OBJECT_TYPE, id }, | ||
}) | ||
); | ||
const removeResult = await deleteRuleSo({ | ||
savedObjectsClient: context.unsecuredSavedObjectsClient, | ||
id, | ||
}); | ||
|
||
await Promise.all([ | ||
taskIdToRemove ? context.taskManager.removeIfExists(taskIdToRemove) : null, | ||
apiKeyToInvalidate && !apiKeyCreatedByUser | ||
? bulkMarkApiKeysForInvalidation( | ||
{ apiKeys: [apiKeyToInvalidate] }, | ||
context.logger, | ||
context.unsecuredSavedObjectsClient | ||
) | ||
: null, | ||
]); | ||
|
||
return removeResult; | ||
} |
9 changes: 9 additions & 0 deletions
9
x-pack/plugins/alerting/server/application/rule/methods/delete/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export type { DeleteRuleParams } from './types'; | ||
export { deleteRule } from './delete_rule'; |
12 changes: 12 additions & 0 deletions
12
...gins/alerting/server/application/rule/methods/delete/schemas/delete_rule_params_schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { schema } from '@kbn/config-schema'; | ||
|
||
export const deleteRuleParamsSchema = schema.object({ | ||
id: schema.string(), | ||
}); |
8 changes: 8 additions & 0 deletions
8
x-pack/plugins/alerting/server/application/rule/methods/delete/schemas/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export * from './delete_rule_params_schema'; |
11 changes: 11 additions & 0 deletions
11
x-pack/plugins/alerting/server/application/rule/methods/delete/types/delete_rule_params.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { TypeOf } from '@kbn/config-schema'; | ||
import { deleteRuleParamsSchema } from '../schemas'; | ||
|
||
export type DeleteRuleParams = TypeOf<typeof deleteRuleParamsSchema>; |
8 changes: 8 additions & 0 deletions
8
x-pack/plugins/alerting/server/application/rule/methods/delete/types/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export * from './delete_rule_params'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.