-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RAM] Adds Bulk Edit API to rulesClient (#126904)
Addresses - #124715 ## Summary - adds bulkEdit method to rulesClient - adds multi_terms bucket aggregations to savedObjectClient - adds createPointInTimeFinderAsInternalUser to encryptedSavedObjectClient - adds alerting API for bulkEdit ```bash curl --location --request POST 'http://localhost:5601/kbn/internal/alerting/rules/_bulk_edit' \ --header 'kbn-xsrf: reporting' \ --header 'Authorization: Basic ZWxhc3RpYzpjaGFuZ2VtZQ==' \ --header 'Content-Type: application/json' \ --data-raw '{ "ids": ["4cb80374-b5c7-11ec-8f1e-adaa7d7d57e5"], "operations": [{ "operation": "add", "field": "tags", "value": ["foo"] }] }' ``` ### Checklist Delete any items that are not applicable to this PR. - [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 ## Release note Adds new `bulkEdit` method to alerting rulesClient and internal _bulk_edit API, that allow bulk editing of rules.
- Loading branch information
Showing
50 changed files
with
3,887 additions
and
462 deletions.
There are no files selected for viewing
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
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
63 changes: 63 additions & 0 deletions
63
...s/alerting/server/invalidate_pending_api_keys/bulk_mark_api_keys_for_invalidation.test.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,63 @@ | ||
/* | ||
* 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 { loggingSystemMock, savedObjectsClientMock } from '@kbn/core/server/mocks'; | ||
import { bulkMarkApiKeysForInvalidation } from './bulk_mark_api_keys_for_invalidation'; | ||
|
||
describe('bulkMarkApiKeysForInvalidation', () => { | ||
test('should call savedObjectsClient bulkCreate with the proper params', async () => { | ||
const unsecuredSavedObjectsClient = savedObjectsClientMock.create(); | ||
unsecuredSavedObjectsClient.bulkCreate.mockResolvedValueOnce({ saved_objects: [] }); | ||
|
||
await bulkMarkApiKeysForInvalidation( | ||
{ apiKeys: [Buffer.from('123').toString('base64'), Buffer.from('456').toString('base64')] }, | ||
loggingSystemMock.create().get(), | ||
unsecuredSavedObjectsClient | ||
); | ||
|
||
const bulkCreateCallMock = unsecuredSavedObjectsClient.bulkCreate.mock.calls[0]; | ||
const savedObjects = bulkCreateCallMock[0]; | ||
|
||
expect(unsecuredSavedObjectsClient.bulkCreate).toHaveBeenCalledTimes(1); | ||
expect(bulkCreateCallMock).toHaveLength(1); | ||
|
||
expect(savedObjects).toHaveLength(2); | ||
expect(savedObjects[0]).toHaveProperty('type', 'api_key_pending_invalidation'); | ||
expect(savedObjects[0]).toHaveProperty('attributes.apiKeyId', '123'); | ||
expect(savedObjects[0]).toHaveProperty('attributes.createdAt', expect.any(String)); | ||
expect(savedObjects[1]).toHaveProperty('type', 'api_key_pending_invalidation'); | ||
expect(savedObjects[1]).toHaveProperty('attributes.apiKeyId', '456'); | ||
expect(savedObjects[1]).toHaveProperty('attributes.createdAt', expect.any(String)); | ||
}); | ||
|
||
test('should log the proper error when savedObjectsClient create failed', async () => { | ||
const logger = loggingSystemMock.create().get(); | ||
const unsecuredSavedObjectsClient = savedObjectsClientMock.create(); | ||
unsecuredSavedObjectsClient.bulkCreate.mockRejectedValueOnce(new Error('Fail')); | ||
await bulkMarkApiKeysForInvalidation( | ||
{ apiKeys: [Buffer.from('123').toString('base64'), Buffer.from('456').toString('base64')] }, | ||
logger, | ||
unsecuredSavedObjectsClient | ||
); | ||
expect(logger.error).toHaveBeenCalledWith( | ||
'Failed to bulk mark list of API keys ["MTIz", "NDU2"] for invalidation: Fail' | ||
); | ||
}); | ||
|
||
test('should not call savedObjectsClient bulkCreate if list of apiKeys empty', async () => { | ||
const unsecuredSavedObjectsClient = savedObjectsClientMock.create(); | ||
unsecuredSavedObjectsClient.bulkCreate.mockResolvedValueOnce({ saved_objects: [] }); | ||
|
||
await bulkMarkApiKeysForInvalidation( | ||
{ apiKeys: [] }, | ||
loggingSystemMock.create().get(), | ||
unsecuredSavedObjectsClient | ||
); | ||
|
||
expect(unsecuredSavedObjectsClient.bulkCreate).not.toHaveBeenCalled(); | ||
}); | ||
}); |
39 changes: 39 additions & 0 deletions
39
...lugins/alerting/server/invalidate_pending_api_keys/bulk_mark_api_keys_for_invalidation.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,39 @@ | ||
/* | ||
* 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 { Logger, SavedObjectsClientContract } from '@kbn/core/server'; | ||
|
||
export const bulkMarkApiKeysForInvalidation = async ( | ||
{ apiKeys }: { apiKeys: string[] }, | ||
logger: Logger, | ||
savedObjectsClient: SavedObjectsClientContract | ||
): Promise<void> => { | ||
if (apiKeys.length === 0) { | ||
return; | ||
} | ||
|
||
try { | ||
const apiKeyIds = apiKeys.map( | ||
(apiKey) => Buffer.from(apiKey, 'base64').toString().split(':')[0] | ||
); | ||
await savedObjectsClient.bulkCreate( | ||
apiKeyIds.map((apiKeyId) => ({ | ||
attributes: { | ||
apiKeyId, | ||
createdAt: new Date().toISOString(), | ||
}, | ||
type: 'api_key_pending_invalidation', | ||
})) | ||
); | ||
} catch (e) { | ||
logger.error( | ||
`Failed to bulk mark list of API keys [${apiKeys | ||
.map((key) => `"${key}"`) | ||
.join(', ')}] for invalidation: ${e.message}` | ||
); | ||
} | ||
}; |
48 changes: 0 additions & 48 deletions
48
...plugins/alerting/server/invalidate_pending_api_keys/mark_api_key_for_invalidation.test.ts
This file was deleted.
Oops, something went wrong.
27 changes: 0 additions & 27 deletions
27
x-pack/plugins/alerting/server/invalidate_pending_api_keys/mark_api_key_for_invalidation.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.