Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shown Wazuh API version is wrong or empty after upgrade #7203

Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
719963c
fix(get-updates): improve error handling by renaming variable for cla…
guidomodarelli Dec 16, 2024
85ef086
fix(plugin-services): update WazuhCheckUpdatesServices type to includ…
guidomodarelli Dec 16, 2024
0a3faff
fix(get-updates): use IAPIHost type for host management to enhance ty…
guidomodarelli Dec 16, 2024
e41576a
fix(get-updates): add logger for improved debugging of API status err…
guidomodarelli Dec 16, 2024
33befd2
fix(get-updates): refactor update retrieval to include version handli…
guidomodarelli Dec 16, 2024
0c018ba
Merge branch '4.10.0' into bug/440-shown-wazuh-api-version-is-wrong-o…
asteriscos Dec 17, 2024
b9740f1
fix(get-updates): update test cases to handle version formatting and …
guidomodarelli Dec 17, 2024
c00c7fb
fix(get-updates): enhance tests by mocking last_available_patch for c…
guidomodarelli Dec 17, 2024
0b42585
fix(get-updates): reorganize imports in test file for better readabil…
guidomodarelli Dec 17, 2024
aa4c03f
fix(get-updates): refactor test setup to improve mocking of saved obj…
guidomodarelli Dec 17, 2024
de85db7
fix(get-updates): standardize api_id and version formatting in tests …
guidomodarelli Dec 17, 2024
66ff7a1
fix(get-updates): improve error handling and add tests for undefined …
guidomodarelli Dec 17, 2024
1654ad9
fix(get-updates): enhance error response handling and add test for du…
guidomodarelli Dec 17, 2024
2e226f8
fix(get-updates): remove unnecessary mock data from tests for clearer…
guidomodarelli Dec 17, 2024
de89706
fix(get-updates): add test for handling API errors on secondary reque…
guidomodarelli Dec 17, 2024
d830303
fix(get-updates): add test for retrieving updates when initial API ve…
guidomodarelli Dec 17, 2024
baaa0e5
fix(get-updates): refactor test cases to use 'it' for better consiste…
guidomodarelli Dec 17, 2024
6b30220
fix(get-updates): simplify test descriptions to improve readability i…
guidomodarelli Dec 17, 2024
568e801
fix(get-updates): enhance test descriptions for clarity in update ava…
guidomodarelli Dec 17, 2024
2019bce
fix(changelog): update entries for issue #7177 and add fix for incorr…
guidomodarelli Dec 17, 2024
3252cf3
fix(changelog): correct rendering entry for vulnerability reference a…
guidomodarelli Dec 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion plugins/wazuh-check-updates/server/plugin-services.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
CoreStart,
ISavedObjectsRepository,
Logger,
} from 'opensearch-dashboards/server';
import { createGetterSetter } from '../../../src/plugins/opensearch_dashboards_utils/common';
import { WazuhCorePluginStart } from '../../wazuh-core/server';
Expand All @@ -11,4 +12,4 @@ export const [getCore, setCore] = createGetterSetter<CoreStart>('Core');
export const [getWazuhCore, setWazuhCore] =
createGetterSetter<WazuhCorePluginStart>('WazuhCore');
export const [getWazuhCheckUpdatesServices, setWazuhCheckUpdatesServices] =
createGetterSetter<any>('WazuhCheckUpdatesServices');
createGetterSetter<{ logger: Logger }>('WazuhCheckUpdatesServices');
93 changes: 62 additions & 31 deletions plugins/wazuh-check-updates/server/services/updates/get-updates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ import {
getWazuhCheckUpdatesServices,
getWazuhCore,
} from '../../plugin-services';
import { IAPIHost } from '../../../../wazuh-core/server/services';

export const getUpdates = async (
queryApi = false,
forceQuery = false,
): Promise<AvailableUpdates> => {
const { logger } = getWazuhCheckUpdatesServices();

try {
if (!queryApi) {
const availableUpdates = (await getSavedObject(
Expand All @@ -23,9 +26,30 @@ export const getUpdates = async (
return availableUpdates;
}

const getStatus = ({
update_check,
last_available_major,
last_available_minor,
last_available_patch,
}: ResponseApiAvailableUpdates) => {
if (update_check === false) {
return API_UPDATES_STATUS.DISABLED;
}

if (
last_available_major?.tag ||
last_available_minor?.tag ||
last_available_patch?.tag
) {
return API_UPDATES_STATUS.AVAILABLE_UPDATES;
}

return API_UPDATES_STATUS.UP_TO_DATE;
};

const { manageHosts, api: wazuhApiClient } = getWazuhCore();

const hosts: { id: string }[] = await manageHosts.get();
const hosts = (await manageHosts.get()) as IAPIHost[];

const apisAvailableUpdates = await Promise.all(
hosts?.map(async api => {
Expand All @@ -36,6 +60,25 @@ export const getUpdates = async (
apiHostID: api.id,
forceRefresh: true,
};
let currentVersion: string | undefined = undefined;
let availableUpdates: ResponseApiAvailableUpdates = {};

try {
const {
data: {
data: { api_version },
},
} = await wazuhApiClient.client.asInternalUser.request(
'GET',
'/',
{},
options,
);
currentVersion = `v${api_version}`;
} catch {
logger.debug('[ERROR]: Cannot get the API version');
}

try {
const response = await wazuhApiClient.client.asInternalUser.request(
method,
Expand All @@ -44,53 +87,43 @@ export const getUpdates = async (
options,
);

const update = response.data.data as ResponseApiAvailableUpdates;
availableUpdates = response.data.data as ResponseApiAvailableUpdates;

const {
current_version,
update_check,
last_available_major,
last_available_minor,
last_available_patch,
last_check_date,
} = update;

const getStatus = () => {
if (update_check === false) {
return API_UPDATES_STATUS.DISABLED;
}

if (
last_available_major?.tag ||
last_available_minor?.tag ||
last_available_patch?.tag
) {
return API_UPDATES_STATUS.AVAILABLE_UPDATES;
}

return API_UPDATES_STATUS.UP_TO_DATE;
};
} = availableUpdates;

// If for some reason, the previous request fails
if (currentVersion === undefined) {
currentVersion = availableUpdates.current_version;
}

return {
current_version,
current_version: currentVersion,
update_check,
last_available_major,
last_available_minor,
last_available_patch,
last_check_date: last_check_date || undefined,
api_id: api.id,
status: getStatus(),
status: getStatus(availableUpdates),
};
} catch (e: any) {
const error = {
title: e.response?.data?.title,
detail: e.response?.data?.detail ?? e.message,
} catch (error: any) {
logger.debug('[ERROR]: Cannot get the API status');
const errorResponse = {
title: error.response?.data?.title,
detail: error.response?.data?.detail ?? error.message,
};

return {
api_id: api.id,
status: API_UPDATES_STATUS.ERROR,
error,
current_version: currentVersion,
error: errorResponse,
};
}
}),
Expand All @@ -109,10 +142,8 @@ export const getUpdates = async (
error instanceof Error
? error.message
: typeof error === 'string'
? error
: 'Error trying to get available updates';

const { logger } = getWazuhCheckUpdatesServices();
? error
: 'Error trying to get available updates';

logger.error(message);
return Promise.reject(error);
Expand Down
2 changes: 1 addition & 1 deletion plugins/wazuh-core/server/services/manage-hosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { ServerAPIClient } from './server-api-client';
import { API_USER_STATUS_RUN_AS } from '../../common/api-user-status-run-as';
import { HTTP_STATUS_CODES } from '../../common/constants';

interface IAPIHost {
export interface IAPIHost {
id: string;
url: string;
username: string;
Expand Down
Loading