diff --git a/CHANGELOG.md b/CHANGELOG.md index ec60b122f5..5ef51fe150 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ All notable changes to the Wazuh app project will be documented in this file. - Fixed a bug where the Wazuh logo was used instead of the custom one [#4539](https://github.com/wazuh/wazuh-kibana-app/pull/4539) - Fixed rendering problems of the `Agent Overview` section in low resolutions [#4516](https://github.com/wazuh/wazuh-kibana-app/pull/4516) - Fixed issue when logging out from Wazuh when SAML is enabled [#4595](https://github.com/wazuh/wazuh-kibana-app/issues/4595) +- Fixed server errors with code 500 when the Wazuh API is not reachable / up. [#4710](https://github.com/wazuh/wazuh-kibana-app/pull/4710) - Fixed pagination to SCA table [#4653](https://github.com/wazuh/wazuh-kibana-app/issues/4653) ## Wazuh v4.3.9 - Kibana 7.10.2, 7.16.x, 7.17.x - Revision 4310 diff --git a/common/constants.ts b/common/constants.ts index 6412dfba86..f323c32a4c 100644 --- a/common/constants.ts +++ b/common/constants.ts @@ -416,3 +416,63 @@ export const DOCUMENTATION_WEB_BASE_URL = "https://documentation.wazuh.com"; // Default Elasticsearch user name context export const ELASTIC_NAME = 'elastic'; + + +export enum HTTP_STATUS_CODES { + CONTINUE = 100, + SWITCHING_PROTOCOLS = 101, + PROCESSING = 102, + OK = 200, + CREATED = 201, + ACCEPTED = 202, + NON_AUTHORITATIVE_INFORMATION = 203, + NO_CONTENT = 204, + RESET_CONTENT = 205, + PARTIAL_CONTENT = 206, + MULTI_STATUS = 207, + MULTIPLE_CHOICES = 300, + MOVED_PERMANENTLY = 301, + MOVED_TEMPORARILY = 302, + SEE_OTHER = 303, + NOT_MODIFIED = 304, + USE_PROXY = 305, + TEMPORARY_REDIRECT = 307, + PERMANENT_REDIRECT = 308, + BAD_REQUEST = 400, + UNAUTHORIZED = 401, + PAYMENT_REQUIRED = 402, + FORBIDDEN = 403, + NOT_FOUND = 404, + METHOD_NOT_ALLOWED = 405, + NOT_ACCEPTABLE = 406, + PROXY_AUTHENTICATION_REQUIRED = 407, + REQUEST_TIMEOUT = 408, + CONFLICT = 409, + GONE = 410, + LENGTH_REQUIRED = 411, + PRECONDITION_FAILED = 412, + REQUEST_TOO_LONG = 413, + REQUEST_URI_TOO_LONG = 414, + UNSUPPORTED_MEDIA_TYPE = 415, + REQUESTED_RANGE_NOT_SATISFIABLE = 416, + EXPECTATION_FAILED = 417, + IM_A_TEAPOT = 418, + INSUFFICIENT_SPACE_ON_RESOURCE = 419, + METHOD_FAILURE = 420, + MISDIRECTED_REQUEST = 421, + UNPROCESSABLE_ENTITY = 422, + LOCKED = 423, + FAILED_DEPENDENCY = 424, + PRECONDITION_REQUIRED = 428, + TOO_MANY_REQUESTS = 429, + REQUEST_HEADER_FIELDS_TOO_LARGE = 431, + UNAVAILABLE_FOR_LEGAL_REASONS = 451, + INTERNAL_SERVER_ERROR = 500, + NOT_IMPLEMENTED = 501, + BAD_GATEWAY = 502, + SERVICE_UNAVAILABLE = 503, + GATEWAY_TIMEOUT = 504, + HTTP_VERSION_NOT_SUPPORTED = 505, + INSUFFICIENT_STORAGE = 507, + NETWORK_AUTHENTICATION_REQUIRED = 511 +} diff --git a/server/controllers/wazuh-api.ts b/server/controllers/wazuh-api.ts index dca72d0a82..f7ab270038 100644 --- a/server/controllers/wazuh-api.ts +++ b/server/controllers/wazuh-api.ts @@ -17,6 +17,7 @@ import { log } from '../lib/logger'; import { KeyEquivalence } from '../../common/csv-key-equivalence'; import { ApiErrorEquivalence } from '../lib/api-errors-equivalence'; import apiRequestList from '../../common/api-info/endpoints'; +import { HTTP_STATUS_CODES } from '../../common/constants'; import { addJobToQueue } from '../start/queue'; import fs from 'fs'; import { ManageHosts } from '../lib/manage-hosts'; @@ -85,7 +86,7 @@ export class WazuhApiCtrl { return ErrorResponse( `Error getting the authorization token: ${errorMessage}`, 3000, - 500, + error?.response?.status || HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR, response ); } @@ -123,13 +124,13 @@ export class WazuhApiCtrl { return ErrorResponse( `ERROR3099 - ${responseManagerInfo.data.detail || 'Wazuh not ready yet'}`, 3099, - 500, + HTTP_STATUS_CODES.SERVICE_UNAVAILABLE, response ); } // If we have a valid response from the Wazuh API - if (responseManagerInfo.status === 200 && responseManagerInfo.data) { + if (responseManagerInfo.status === HTTP_STATUS_CODES.OK && responseManagerInfo.data) { // Clear and update cluster information before being sent back to frontend delete api.cluster_info; const responseAgents = await context.wazuh.api.client.asInternalUser.request( @@ -139,7 +140,7 @@ export class WazuhApiCtrl { { apiHostID: id } ); - if (responseAgents.status === 200) { + if (responseAgents.status === HTTP_STATUS_CODES.OK) { const managerName = responseAgents.data.data.affected_items[0].manager; const responseClusterStatus = await context.wazuh.api.client.asInternalUser.request( @@ -148,7 +149,7 @@ export class WazuhApiCtrl { {}, { apiHostID: id } ); - if (responseClusterStatus.status === 200) { + if (responseClusterStatus.status === HTTP_STATUS_CODES.OK) { if (responseClusterStatus.data.data.enabled === 'yes') { const responseClusterLocalInfo = await context.wazuh.api.client.asInternalUser.request( 'GET', @@ -156,7 +157,7 @@ export class WazuhApiCtrl { {}, { apiHostID: id } ); - if (responseClusterLocalInfo.status === 200) { + if (responseClusterLocalInfo.status === HTTP_STATUS_CODES.OK) { const clusterEnabled = responseClusterStatus.data.data.enabled === 'yes'; api.cluster_info = { status: clusterEnabled ? 'enabled' : 'disabled', @@ -195,7 +196,7 @@ export class WazuhApiCtrl { return response.ok({ body: { - statusCode: 200, + statusCode: HTTP_STATUS_CODES.OK, data: copied, idChanged: request.body.idChanged || null, } @@ -210,14 +211,14 @@ export class WazuhApiCtrl { if (error.code === 'EPROTO') { return response.ok({ body: { - statusCode: 200, + statusCode: HTTP_STATUS_CODES.OK, data: { apiIsDown: true }, } }); } else if (error.code === 'ECONNREFUSED') { return response.ok({ body: { - statusCode: 200, + statusCode: HTTP_STATUS_CODES.OK, data: { apiIsDown: true }, } }); @@ -239,11 +240,11 @@ export class WazuhApiCtrl { return ErrorResponse( `ERROR3099 - ${response.data.detail || 'Wazuh not ready yet'}`, 3099, - 500, + HTTP_STATUS_CODES.SERVICE_UNAVAILABLE, response ); } - if (responseManagerInfo.status === 200) { + if (responseManagerInfo.status === HTTP_STATUS_CODES.OK) { request.body.id = id; request.body.idChanged = id; return await this.checkStoredAPI(context, request, response); @@ -252,10 +253,20 @@ export class WazuhApiCtrl { } } catch (error) { log('wazuh-api:checkStoredAPI', error.message || error); - return ErrorResponse(error.message || error, 3020, 500, response); + return ErrorResponse( + error.message || error, + 3020, + error?.response?.status || HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR, + response + ); } log('wazuh-api:checkStoredAPI', error.message || error); - return ErrorResponse(error.message || error, 3002, 500, response); + return ErrorResponse( + error.message || error, + 3002, + error?.response?.status || HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR, + response + ); } } } @@ -299,7 +310,7 @@ export class WazuhApiCtrl { try { let apiAvailable = null; // const notValid = this.validateCheckApiParams(request.body); - // if (notValid) return ErrorResponse(notValid, 3003, 500, response); + // if (notValid) return ErrorResponse(notValid, 3003, HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR, response); log('wazuh-api:checkAPI', `${request.body.id} is valid`, 'debug'); // Check if a Wazuh API id is given (already stored API) const data = await this.manageHosts.getHostById(request.body.id); @@ -307,7 +318,12 @@ export class WazuhApiCtrl { apiAvailable = data; } else { log('wazuh-api:checkAPI', `API ${request.body.id} not found`); - return ErrorResponse(`The API ${request.body.id} was not found`, 3029, 500, response); + return ErrorResponse( + `The API ${request.body.id} was not found`, + 3029, + HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR, + response + ); } const options = { apiHostID: request.body.id }; if (request.body.forceRefresh) { @@ -325,13 +341,13 @@ export class WazuhApiCtrl { return ErrorResponse( `ERROR3099 - ${error.response?.data?.detail || 'Wazuh not ready yet'}`, 3099, - 500, + error?.response?.status || HTTP_STATUS_CODES.SERVICE_UNAVAILABLE, response ); } log('wazuh-api:checkAPI', `${request.body.id} credentials are valid`, 'debug'); - if (responseManagerInfo.status === 200 && responseManagerInfo.data) { + if (responseManagerInfo.status === HTTP_STATUS_CODES.OK && responseManagerInfo.data) { let responseAgents = await context.wazuh.api.client.asInternalUser.request( 'GET', `/agents`, @@ -339,7 +355,7 @@ export class WazuhApiCtrl { { apiHostID: request.body.id } ); - if (responseAgents.status === 200) { + if (responseAgents.status === HTTP_STATUS_CODES.OK) { const managerName = responseAgents.data.data.affected_items[0].manager; let responseCluster = await context.wazuh.api.client.asInternalUser.request( @@ -357,7 +373,7 @@ export class WazuhApiCtrl { {}, { apiHostID: request.body.id } ); - if (responseApiUserAllowRunAs.status === 200) { + if (responseApiUserAllowRunAs.status === HTTP_STATUS_CODES.OK) { const allow_run_as = responseApiUserAllowRunAs.data.data.affected_items[0].allow_run_as; if (allow_run_as && apiAvailable && apiAvailable.run_as) // HOST AND USER ENABLED @@ -378,7 +394,7 @@ export class WazuhApiCtrl { apiUserAllowRunAs ); - if (responseCluster.status === 200) { + if (responseCluster.status === HTTP_STATUS_CODES.OK) { log('wazuh-api:checkStoredAPI', `Wazuh API response is valid`, 'debug'); if (responseCluster.data.data.enabled === 'yes') { // If cluster mode is active @@ -389,7 +405,7 @@ export class WazuhApiCtrl { { apiHostID: request.body.id } ); - if (responseClusterLocal.status === 200) { + if (responseClusterLocal.status === HTTP_STATUS_CODES.OK) { return response.ok({ body: { manager: managerName, @@ -417,19 +433,19 @@ export class WazuhApiCtrl { } catch (error) { log('wazuh-api:checkAPI', error.message || error); - if (error && error.response && error.response.status === 401) { + if (error && error.response && error.response.status === HTTP_STATUS_CODES.UNAUTHORIZED) { return ErrorResponse( `Unathorized. Please check API credentials. ${error.response.data.message}`, - 401, - 401, + HTTP_STATUS_CODES.UNAUTHORIZED, + HTTP_STATUS_CODES.UNAUTHORIZED, response ); } if (error && error.response && error.response.data && error.response.data.detail) { return ErrorResponse( error.response.data.detail, - error.response.status || 500, - error.response.status || 500, + error.response.status || HTTP_STATUS_CODES.SERVICE_UNAVAILABLE, + error.response.status || HTTP_STATUS_CODES.SERVICE_UNAVAILABLE, response ); } @@ -437,16 +453,21 @@ export class WazuhApiCtrl { return ErrorResponse( 'Wrong protocol being used to connect to the Wazuh API', 3005, - 500, + HTTP_STATUS_CODES.BAD_REQUEST, response ); } - return ErrorResponse(error.message || error, 3005, 500, response); + return ErrorResponse( + error.message || error, + 3005, + error?.response?.status || HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR, + response + ); } } checkResponseIsDown(response) { - if (response.status !== 200) { + if (response.status !== HTTP_STATUS_CODES.OK) { // Avoid "Error communicating with socket" like errors const socketErrorCodes = [1013, 1014, 1017, 1018, 1019]; const status = (response.data || {}).status || 1 @@ -539,7 +560,7 @@ export class WazuhApiCtrl { * @returns {Object} API response or ErrorResponse */ async makeRequest(context, method, path, data, id, response) { - + const devTools = !!(data || {}).devTools; try { const api = await this.manageHosts.getHostById(id); @@ -550,7 +571,7 @@ export class WazuhApiCtrl { if (!Object.keys(api).length) { log('wazuh-api:makeRequest', 'Could not get host credentials'); //Can not get credentials from wazuh-hosts - return ErrorResponse('Could not get host credentials', 3011, 404, response); + return ErrorResponse('Could not get host credentials', 3011, HTTP_STATUS_CODES.NOT_FOUND, response); } if (!data) { @@ -608,7 +629,7 @@ export class WazuhApiCtrl { return ErrorResponse( `ERROR3099 - ${error.message || 'Wazuh not ready yet'}`, 3099, - 500, + HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR, response ); } @@ -637,7 +658,7 @@ export class WazuhApiCtrl { return ErrorResponse( `ERROR3099 - ${response.body.message || 'Wazuh not ready yet'}`, 3099, - 500, + HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR, response ); } @@ -649,7 +670,7 @@ export class WazuhApiCtrl { : false; response.data = responseBody; } - const responseError = response.status !== 200 ? response.status : false; + const responseError = response.status !== HTTP_STATUS_CODES.OK ? response.status : false; if (!responseError && responseBody) { //cleanKeys(response); @@ -667,11 +688,11 @@ export class WazuhApiCtrl { ? { message: responseBody.detail, code: responseError } : new Error('Unexpected error fetching data from the Wazuh API'); } catch (error) { - if (error && error.response && error.response.status === 401) { + if (error && error.response && error.response.status === HTTP_STATUS_CODES.UNAUTHORIZED) { return ErrorResponse( error.message || error, error.code ? `Wazuh API error: ${error.code}` : 3013, - 401, + HTTP_STATUS_CODES.UNAUTHORIZED, response ); } @@ -688,7 +709,7 @@ export class WazuhApiCtrl { return ErrorResponse( errorMsg.detail || error, error.code ? `Wazuh API error: ${error.code}` : 3013, - 500, + HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR, response ); } @@ -707,24 +728,24 @@ export class WazuhApiCtrl { const idApi = getCookieValueByName(request.headers.cookie, 'wz-api'); if (idApi !== request.body.id) { // if the current token belongs to a different API id, we relogin to obtain a new token return ErrorResponse( - 'status code 401', - 401, - 401, + 'status code HTTP_STATUS_CODES.UNAUTHORIZED', + HTTP_STATUS_CODES.UNAUTHORIZED, + HTTP_STATUS_CODES.UNAUTHORIZED, response ); } if (!request.body.method) { - return ErrorResponse('Missing param: method', 3015, 400, response); + return ErrorResponse('Missing param: method', 3015, HTTP_STATUS_CODES.BAD_REQUEST, response); } else if (!request.body.method.match(/^(?:GET|PUT|POST|DELETE)$/)) { log('wazuh-api:makeRequest', 'Request method is not valid.'); //Method is not a valid HTTP request method - return ErrorResponse('Request method is not valid.', 3015, 400, response); + return ErrorResponse('Request method is not valid.', 3015, HTTP_STATUS_CODES.BAD_REQUEST, response); } else if (!request.body.path) { - return ErrorResponse('Missing param: path', 3016, 400, response); + return ErrorResponse('Missing param: path', 3016, HTTP_STATUS_CODES.BAD_REQUEST, response); } else if (!request.body.path.startsWith('/')) { log('wazuh-api:makeRequest', 'Request path is not valid.'); //Path doesn't start with '/' - return ErrorResponse('Request path is not valid.', 3015, 400, response); + return ErrorResponse('Request path is not valid.', 3015, HTTP_STATUS_CODES.BAD_REQUEST, response); } else { return this.makeRequest( @@ -762,7 +783,7 @@ export class WazuhApiCtrl { log('wazuh-api:csv', `Report ${tmpPath}`, 'debug'); // Real limit, regardless the user query - const params = { limit: 500 }; + const params = { limit: HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR }; if (filters.length) { for (const filter of filters) { @@ -873,7 +894,7 @@ export class WazuhApiCtrl { } } catch (error) { log('wazuh-api:csv', error.message || error); - return ErrorResponse(error.message || error, 3034, 500, response); + return ErrorResponse(error.message || error, 3034, HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR, response); } } @@ -914,8 +935,8 @@ export class WazuhApiCtrl { log('wazuh-api:getTimeStamp', error.message || error); return ErrorResponse( error.message || 'Could not fetch wazuh-version registry', - 4001, - 500, + HTTP_STATUS_CODES.BAD_REQUEST1, + HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR, response ); } @@ -935,15 +956,15 @@ export class WazuhApiCtrl { await this.updateRegistry.updateAPIExtensions(id, extensions); return response.ok({ body: { - statusCode: 200 + statusCode: HTTP_STATUS_CODES.OK } }); } catch (error) { log('wazuh-api:setExtensions', error.message || error); return ErrorResponse( error.message || 'Could not set extensions', - 4001, - 500, + HTTP_STATUS_CODES.BAD_REQUEST1, + HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR, response ); } @@ -970,8 +991,8 @@ export class WazuhApiCtrl { log('wazuh-api:getExtensions', error.message || error); return ErrorResponse( error.message || 'Could not fetch wazuh-version registry', - 4001, - 500, + HTTP_STATUS_CODES.BAD_REQUEST1, + HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR, response ); } @@ -989,7 +1010,7 @@ export class WazuhApiCtrl { const source = JSON.parse(fs.readFileSync(this.updateRegistry.file, 'utf8')); return response.ok({ body: { - statusCode: 200, + statusCode: HTTP_STATUS_CODES.OK, data: !Object.values(source).length ? '' : source } }); @@ -997,8 +1018,8 @@ export class WazuhApiCtrl { log('wazuh-api:getSetupInfo', error.message || error); return ErrorResponse( `Could not get data from wazuh-version registry due to ${error.message || error}`, - 4005, - 500, + HTTP_STATUS_CODES.BAD_REQUEST5, + HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR, response ); } @@ -1045,19 +1066,19 @@ export class WazuhApiCtrl { }); } catch (error) { log('wazuh-api:getSyscollector', error.message || error); - return ErrorResponse(error.message || error, 3035, 500, response); + return ErrorResponse(error.message || error, 3035, HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR, response); } } /** * Check if user assigned roles disable Wazuh Plugin - * @param context - * @param request - * @param response - * @returns {object} Returns { isWazuhDisabled: boolean parsed integer } + * @param context + * @param request + * @param response + * @returns {object} Returns { isWazuhDisabled: boolean parsed integer } */ async isWazuhDisabled(context: RequestHandlerContext, request: KibanaRequest, response: KibanaResponseFactory) { try { - + const disabledRoles = ( await getConfiguration() )['disabled_roles'] || []; const logoSidebar = ( await getConfiguration() )['customization.logo.sidebar']; const data = (await context.wazuh.security.getCurrentUser(request, context)).authContext; @@ -1069,16 +1090,16 @@ export class WazuhApiCtrl { }); } catch (error) { log('wazuh-api:isWazuhDisabled', error.message || error); - return ErrorResponse(error.message || error, 3035, 500, response); + return ErrorResponse(error.message || error, 3035, HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR, response); } - + } /** * Gets custom logos configuration (path) - * @param context - * @param request - * @param response + * @param context + * @param request + * @param response */ async getAppLogos(context: RequestHandlerContext, request: KibanaRequest, response: KibanaResponseFactory) { try { @@ -1098,8 +1119,8 @@ export class WazuhApiCtrl { }); } catch (error) { log('wazuh-api:getAppLogos', error.message || error); - return ErrorResponse(error.message || error, 3035, 500, response); + return ErrorResponse(error.message || error, 3035, HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR, response); } - + } } diff --git a/server/routes/wazuh-api-http-status.test.ts b/server/routes/wazuh-api-http-status.test.ts new file mode 100644 index 0000000000..fb7958be4f --- /dev/null +++ b/server/routes/wazuh-api-http-status.test.ts @@ -0,0 +1,117 @@ +// To launch this file +// yarn test:jest --testEnvironment node --verbose server/routes/wazuh-api +import { Router } from '../../../../src/core/server/http/router/router'; +import { HttpServer } from '../../../../src/core/server/http/http_server'; +import { loggingSystemMock } from '../../../../src/core/server/logging/logging_system.mock'; +import { ByteSizeValue } from '@kbn/config-schema'; +import supertest from 'supertest'; +import { WazuhApiRoutes } from './wazuh-api'; +import { WazuhApiCtrl } from '../controllers/wazuh-api'; +import { createDataDirectoryIfNotExists, createDirectoryIfNotExists } from '../lib/filesystem'; +import { + HTTP_STATUS_CODES, + WAZUH_DATA_ABSOLUTE_PATH, + WAZUH_DATA_CONFIG_APP_PATH, + WAZUH_DATA_CONFIG_DIRECTORY_PATH, + WAZUH_DATA_LOGS_DIRECTORY_PATH +} from '../../common/constants'; +import { execSync } from 'child_process'; +import fs from 'fs'; + +const loggingService = loggingSystemMock.create(); +const logger = loggingService.get(); +const context = { + wazuh: { + security: { + getCurrentUser: () => 'wazuh' + } + } +}; + +const enhanceWithContext = (fn: (...args: any[]) => any) => fn.bind(null, context); +let server, innerServer; + +beforeAll(async () => { + // Create /data/wazuh directory. + createDataDirectoryIfNotExists(); + // Create /data/wazuh/config directory. + createDirectoryIfNotExists(WAZUH_DATA_CONFIG_DIRECTORY_PATH); + + // Create /data/wazuh/logs directory. + createDirectoryIfNotExists(WAZUH_DATA_LOGS_DIRECTORY_PATH); + + // Create server + const config = { + name: 'plugin_platform', + host: '127.0.0.1', + maxPayload: new ByteSizeValue(1024), + port: 10002, + ssl: { enabled: false }, + compression: { enabled: true }, + requestId: { + allowFromAnyIp: true, + ipAllowlist: [], + }, + } as any; + server = new HttpServer(loggingService, 'tests'); + const router = new Router('', logger, enhanceWithContext); + const { registerRouter, server: innerServerTest, ...rest } = await server.setup(config); + innerServer = innerServerTest; + + // const spyRouteDecoratorProtectedAdministratorRoleValidToken = jest.spyOn(WazuhApiCtrl.prototype as any, 'routeDecoratorProtectedAdministratorRoleValidToken') + // .mockImplementation((handler) => async (...args) => handler(...args)); + + // Register routes + WazuhApiRoutes(router); + + // Register router + registerRouter(router); + + // start server + await server.start(); +}); + +afterAll(async () => { + // Stop server + await server.stop(); + + // Clear all mocks + jest.clearAllMocks(); + + // Remove /data/wazuh directory. + execSync(`rm -rf ${WAZUH_DATA_ABSOLUTE_PATH}`); +}); + +describe('[endpoint] GET /api/check-api', () => { + beforeAll(() => { + // Create the configuration file with custom content + const fileContent = `--- +pattern: test-alerts-* +hosts: + - default: + url: https://localhost + port: 55000 + username: wazuh-wui + password: wazuh-wui + run_as: false +`; + + fs.writeFileSync(WAZUH_DATA_CONFIG_APP_PATH, fileContent, 'utf8'); + }); + + afterAll(() => { + // Remove the configuration file + fs.unlinkSync(WAZUH_DATA_CONFIG_APP_PATH); + }); + + it.each` + apiId | statusCode + ${'default'} | ${HTTP_STATUS_CODES.SERVICE_UNAVAILABLE} + `(`Get API configuration POST /api/check-api - apiID - $statusCode`, async ({ apiId, statusCode }) => { + const body = { id: apiId, forceRefresh: false }; + const response = await supertest(innerServer.listener) + .post('/api/check-api') + .send(body) + .expect(statusCode); + }); +});