Skip to content

Commit

Permalink
[Fleet] Remove body from sendRemovePackage to fix broken uninstall as…
Browse files Browse the repository at this point in the history
…sets button (#195101)

## Summary

The "uninstall assets" button in the integrations UI got recently
broken. Trying to uninstall assets fails with 400.

The reason is that the [UI
request](https://github.com/elastic/kibana/blob/6001786d04388f5f79cea74b674e510fc7a60d3a/x-pack/plugins/fleet/public/hooks/use_request/epm.ts#L273-L282)
is using and old (and deprecated) endpoint, that sends `body` in the
payload. However the [schema
validation](https://github.com/elastic/kibana/blob/ccd0e17c0955163f82e15c464d82df45f391cf62/x-pack/plugins/fleet/server/types/rest_spec/epm.ts#L617-L631)
of the [delete
endpoint](https://github.com/elastic/kibana/blob/1a54aabd6d1806fbdd5309da9b06fefdd4fe0689/x-pack/plugins/fleet/server/routes/epm/index.ts#L540-L568)
doesn't accept it.

![Screenshot 2024-10-04 at 10 01
47 AM](https://github.com/user-attachments/assets/0f88fd52-eb1d-489f-9ad4-49e104f40430)

Here I'm updating the UI request to use the correct schema. The only
other place where this request was used with `body` was in the debug
page, so we should be fine.

### Testing
- Install any integration
- Try to uninstall the assets from the UI:
![Screenshot 2024-10-04 at 17 13
28](https://github.com/user-attachments/assets/f95b5c96-ca8f-4319-8d95-3a76ed588c17)
![Screenshot 2024-10-04 at 17 14
25](https://github.com/user-attachments/assets/aea994bd-2d35-4ca7-ab92-db80a1c5546c)

### Checklist
- [ ] [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: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
criamico and elasticmachine authored Oct 7, 2024
1 parent 94caafd commit 78bd739
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
3 changes: 3 additions & 0 deletions x-pack/plugins/fleet/common/types/rest_spec/epm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ export interface DeletePackageRequest {
pkgName: string;
pkgVersion: string;
};
query: {
force?: boolean;
};
}

export interface DeletePackageResponse {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,13 @@ export const IntegrationDebugger: React.FunctionComponent = () => {
const integrations = useQuery(['debug-integrations'], fetchInstalledIntegrations);

const uninstallMutation = useMutation(async (integration: PackageListItem) => {
const response = await sendRemovePackage(integration.name, integration.version, true);
const response = await sendRemovePackage(
{
pkgName: integration.name,
pkgVersion: integration.version,
},
{ force: true }
);

if (response.error) {
notifications.toasts.addError(response.error, {
Expand Down Expand Up @@ -92,7 +98,13 @@ export const IntegrationDebugger: React.FunctionComponent = () => {
});

const reinstallMutation = useMutation(async (integration: PackageListItem) => {
const uninstallResponse = await sendRemovePackage(integration.name, integration.version, true);
const uninstallResponse = await sendRemovePackage(
{
pkgName: integration.name,
pkgVersion: integration.version,
},
{ force: true }
);

if (uninstallResponse.error) {
notifications.toasts.addError(uninstallResponse.error, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ function usePackageInstall({ startServices }: { startServices: StartServices })
}: Pick<PackageInfo, 'name' | 'version' | 'title'> & { redirectToVersion: string }) => {
setPackageInstallStatus({ name, status: InstallStatus.uninstalling, version });

const res = await sendRemovePackage(name, version);
const res = await sendRemovePackage({ pkgName: name, pkgVersion: version });
if (res.error) {
setPackageInstallStatus({ name, status: InstallStatus.installed, version });
notifications.toasts.addWarning({
Expand Down
12 changes: 7 additions & 5 deletions x-pack/plugins/fleet/public/hooks/use_request/epm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type {
GetLimitedPackagesResponse,
GetInfoResponse,
InstallPackageResponse,
DeletePackageRequest,
DeletePackageResponse,
UpdatePackageRequest,
UpdatePackageResponse,
Expand Down Expand Up @@ -270,16 +271,17 @@ export const sendBulkInstallPackages = (
});
};

export const sendRemovePackage = (pkgName: string, pkgVersion: string, force: boolean = false) => {
export function sendRemovePackage(
{ pkgName, pkgVersion }: DeletePackageRequest['params'],
query?: DeletePackageRequest['query']
) {
return sendRequest<DeletePackageResponse>({
path: epmRouteService.getRemovePath(pkgName, pkgVersion),
method: 'delete',
version: API_VERSIONS.public.v1,
body: {
force,
},
query,
});
};
}

export const sendRequestReauthorizeTransforms = (
pkgName: string,
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/fleet/public/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export type {
GetLimitedPackagesResponse,
GetInfoResponse,
InstallPackageResponse,
DeletePackageRequest,
DeletePackageResponse,
InstallationStatus,
Installable,
Expand Down

0 comments on commit 78bd739

Please sign in to comment.