forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Catching saved object not found error, do not delete referenced packa…
…ge assets (elastic#146274) ## Summary Fixes elastic#142112 Improvements coming out of SDHs, to handle Fleet errors more robustly. ### 1. Catching package assets not found error and logging a warning, to prevent it blocking setup/install. ### 2. In assets cleanup logic, doing a check that the assets are not referenced by packages before deleting them. Verifying 1. and 2. hard, because I don't have a way to reproduce the assets not found error. Tried to install/delete different versions of a package, but didn't run into the issues. ### 3. Force removing a package deletes package policies as well To verify: - add at least one integration (e.g. system package) - force delete integration with API ``` DELETE http://elastic:changeme@localhost:5601/julia/api/fleet/epm/packages/system/1.20.4 kbn-xsrf: kibana { "force": true } ``` - verify that package policies are deleted as well - there is an info log added: ``` [2022-11-24T14:10:29.138+01:00][INFO ][plugins.fleet] deleting package policies of system package because force flag was enabled: 84a4e5d4-e363-4045-9240-9ab151f2b376,6d34a544-7467-48fd-a779-c2d30d808aa6 ``` ### 4. Catching saved object not found error when tagging package assets To verify: - follow the steps above to force delete a package - add system integration again - verify that it succeeds, and there is a warning log ``` [2022-11-24T14:07:47.960+01:00][WARN ][plugins.fleet] Error: Saved object [dashboard/system-01c54730-fee6-11e9-8405-516218e3d268] not found ``` ### 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 Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> (cherry picked from commit 7198faf)
- Loading branch information
1 parent
e1a7551
commit 0de6f54
Showing
9 changed files
with
231 additions
and
39 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
33 changes: 33 additions & 0 deletions
33
x-pack/plugins/fleet/server/services/epm/archive/storage.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,33 @@ | ||
/* | ||
* 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 { SavedObjectsErrorHelpers } from '@kbn/core-saved-objects-utils-server'; | ||
|
||
import { getAsset } from './storage'; | ||
|
||
jest.mock('../../app_context', () => { | ||
return { | ||
appContextService: { | ||
getLogger: jest.fn().mockReturnValue({ | ||
warn: jest.fn(), | ||
}), | ||
}, | ||
}; | ||
}); | ||
|
||
describe('getAsset', () => { | ||
it('should not throw error if saved object not found', async () => { | ||
const soClientMock = { | ||
get: jest.fn().mockRejectedValue(SavedObjectsErrorHelpers.createGenericNotFoundError()), | ||
} as any; | ||
const result = await getAsset({ | ||
savedObjectsClient: soClientMock, | ||
path: 'path', | ||
}); | ||
expect(result).toBeUndefined(); | ||
}); | ||
}); |
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
69 changes: 69 additions & 0 deletions
69
x-pack/plugins/fleet/server/services/epm/packages/remove.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,69 @@ | ||
/* | ||
* 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 { packagePolicyService } from '../..'; | ||
|
||
import { removeInstallation } from './remove'; | ||
|
||
jest.mock('../..', () => { | ||
return { | ||
appContextService: { | ||
getLogger: jest.fn().mockReturnValue({ | ||
info: jest.fn(), | ||
error: jest.fn(), | ||
}), | ||
}, | ||
packagePolicyService: { | ||
list: jest.fn().mockResolvedValue({ total: 1, items: [{ id: 'system-1' }] }), | ||
delete: jest.fn(), | ||
}, | ||
}; | ||
}); | ||
|
||
const mockPackagePolicyService = packagePolicyService as jest.Mocked<typeof packagePolicyService>; | ||
|
||
describe('removeInstallation', () => { | ||
let soClientMock: any; | ||
const esClientMock = {} as any; | ||
beforeEach(() => { | ||
soClientMock = { | ||
get: jest.fn().mockResolvedValue({ attributes: { installed_kibana: [], installed_es: [] } }), | ||
delete: jest.fn(), | ||
find: jest.fn().mockResolvedValue({ saved_objects: [] }), | ||
bulkResolve: jest.fn().mockResolvedValue({ resolved_objects: [] }), | ||
} as any; | ||
}); | ||
it('should remove package policies when force', async () => { | ||
await removeInstallation({ | ||
savedObjectsClient: soClientMock, | ||
pkgName: 'system', | ||
pkgVersion: '1.0.0', | ||
esClient: esClientMock, | ||
force: true, | ||
}); | ||
expect(mockPackagePolicyService.delete).toHaveBeenCalledWith( | ||
expect.anything(), | ||
expect.anything(), | ||
['system-1'], | ||
{ force: true } | ||
); | ||
}); | ||
|
||
it('should throw when trying to remove package with package policies when not force', async () => { | ||
await expect( | ||
removeInstallation({ | ||
savedObjectsClient: soClientMock, | ||
pkgName: 'system', | ||
pkgVersion: '1.0.0', | ||
esClient: esClientMock, | ||
force: false, | ||
}) | ||
).rejects.toThrowError( | ||
`unable to remove package with existing package policy(s) in use by agent(s)` | ||
); | ||
}); | ||
}); |
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