Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fleet] Fix package install with older version (#156257)
## Summary This PR fixes a bug where an older version of a package cannot be installed when creating a new agent policy. Closes #152095 ### Reproducing the bug 1. Run Kibana off the `main` branch. 2. Install a package of your choice for the first time (it should not already be installed) on a version lower than the latest (cf. screenshot 1). In the configuration page under `Where to add this integration?`, leave `New hosts` selected to ensure a new agent policy is created (cf. screenshot 2). 3. After the package is installed, go to the package settings: notice that the installed version is actually the latest (cf. screenshot 3). 4. Note: if you go to the `Integration policies` tab of the package, you will notice that the integration policy has the version number you intended to install (cf. screenshot 4). ### Cause of the bug The `onSubmit` hook used by the package install form [makes use the of the `epm/packages/_bulk` endpoint](https://github.com/elastic/kibana/blob/e62581f8bf6098f2813768af2f31647c592645a2/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/single_page_layout/hooks/form.tsx#L272) to bulk install multiple packages if a new agent policy is to be created. The `epm/packages/_bulk` endpoint accepts an array of packages which are either specified by a package name (string) or name and version (object). In the current implementation, this hook only provides the package names, which resolves to the latest version of the package being installed. ### How this PR fixes the bug This PR make the following changes: * Make the `BulkInstallPackagesFromRegistryRequestSchema` validation accepts an array of either strings or objects of shape `{ name: string, version: string }` for packages. * Modify the `sendBulkInstallPackages` request hook to do the same. * Make the `onSubmit` hook used by the package install form pass the package name and version to the `epm/packages/_bulk` endpoint. ### Additional changes * Add API integration test for bulk package install * Amend OpenAPI definition: [Swagger link](https://petstore.swagger.io/?url=https://raw.githubusercontent.com/elastic/kibana/a4a3209905a5cf099fa22779e889a57acb124fb2/x-pack/plugins/fleet/common/openapi/bundled.json#/Elastic%20Package%20Manager%20(EPM)/bulk-install-packages) (cf. screenshot 7) ### Testing steps 1. (Optional) Test that the `epm/packages/_bulk` endpoint accepts package name as strings or package name and version as objects, or even a mix of both (cf. screenshot 6). 2. Same steps as `Reproducing the bug` above on this branch. The correct version of the package should be installed (cf screenshot 5). ### Screenshots Screenshot 1: preparing to install an older version of a package: <img width="1917" alt="Screenshot 2023-05-02 at 11 21 12" src="https://user-images.githubusercontent.com/23701614/235631501-aa03d2b0-6fe8-4e9a-8e0c-5547f8fccea0.png"> Screenshot 2: creating a new agent policy when installing the package: <img width="1917" alt="Screenshot 2023-05-02 at 11 21 33" src="https://user-images.githubusercontent.com/23701614/235631650-3d9bfc19-ace3-4488-a7c7-48078aae3e7c.png"> Screenshot 3: integration settings after installing an older version onto a new agent policy, showing that the installed version is actually the latest version: <img width="1917" alt="Screenshot 2023-05-02 at 11 29 07" src="https://user-images.githubusercontent.com/23701614/235631920-3ca8f606-d225-464e-8094-46aeee4869df.png"> Screenshot 4: `Integration policies` tab, showing that the integration policy has the older version: <img width="1917" alt="Screenshot 2023-05-02 at 11 28 43" src="https://user-images.githubusercontent.com/23701614/235632212-c82a3ddd-cb33-4179-a4ab-b57e33e7490d.png"> Screenshot 5: integration settings after installing an older version onto a new agent policy **with the bug fix**, showing that the correct version was installed: <img width="1917" alt="Screenshot 2023-05-02 at 11 23 09" src="https://user-images.githubusercontent.com/23701614/235632590-bb26183d-ce60-439c-9bee-eadb9fdb9654.png"> Screenshot 6: API call showing the two ways of specifying a package using the `epm/packages/_bulk` endpoint (package name only, or name and version): <img width="1917" alt="Screenshot 2023-05-02 at 11 59 11" src="https://user-images.githubusercontent.com/23701614/235637532-ae32072b-ca84-48bb-9760-120a2149f0e5.png"> Screenshot 7: `epm/packages/_bulk` endpoint reference on Swagger: <img width="1429" alt="Screenshot 2023-05-02 at 15 13 14" src="https://user-images.githubusercontent.com/23701614/235677504-e8ba1329-1a70-44f8-92c5-0f32e5da7e46.png"> ### 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: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
- Loading branch information