Skip to content

Commit

Permalink
[Backport 4.4-7.16] Enhance the getConfiguration backend service (#5431)
Browse files Browse the repository at this point in the history
* Enhance the getConfiguration backend service (#5428)

* fix(enhance): enhance the the backend service to get the plugin configuration

- Enhance the backend service to get the plugin configuration
- Tests:
  - Add testing for the getConfiguration service
  - Add test to ensure the `GET /utils/configuration` returns the expected host
    response

* changelog: add pull request entry

* changelog: fix changelog

* Fix get configuration test unlink command error

* fix: fix comment on unit test

---------

Co-authored-by: Maximiliano Ibarra <[email protected]>
(cherry picked from commit 6290e99)

* fix: fix accessing to object key
  • Loading branch information
Desvelao authored May 11, 2023
1 parent d36e05c commit 3c3a4b6
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ All notable changes to the Wazuh app project will be documented in this file.

- Support for Wazuh 4.4.2

### Fixed

- Fixed a problem in the backend service to get the plugin configuration [#5428](https://github.com/wazuh/wazuh-kibana-app/pull/5428)

## Wazuh v4.4.1 - Kibana 7.10.2, 7.16.x, 7.17.x - Revision 00

### Fixed
Expand Down
87 changes: 87 additions & 0 deletions server/lib/get-configuration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { WAZUH_DATA_ABSOLUTE_PATH, WAZUH_DATA_CONFIG_DIRECTORY_PATH, WAZUH_DATA_CONFIG_APP_PATH } from '../../common/constants';
import { createDataDirectoryIfNotExists, createDirectoryIfNotExists } from './filesystem';
import { getConfiguration } from './get-configuration';
import { execSync } from 'child_process';
import { unlinkSync, writeFileSync } from 'fs';

beforeAll(() => {
// Create <PLUGIN_PLATFORM_PATH>/data/wazuh directory.
createDataDirectoryIfNotExists();
// Create <PLUGIN_PLATFORM_PATH>/data/wazuh/config directory.
createDirectoryIfNotExists(WAZUH_DATA_CONFIG_DIRECTORY_PATH);
});

afterAll(() => {
// Remove <PLUGIN_PLATFORM_PATH>/data/wazuh directory.
execSync(`rm -rf ${WAZUH_DATA_ABSOLUTE_PATH}`);
});

describe('[service] get-configuration', () => {

afterEach(() => {
// Remove <PLUGIN_PLATFORM_PATH>/data/wazuh/config/wazuh.yml file.
execSync(`rm ${WAZUH_DATA_ABSOLUTE_PATH}/config/wazuh.yml || echo ""`);
});

const pluginConfigurationText = [
`hosts:
- default:
- url: http://wazuh.manager
- port: 55000
- username: user
- password: password
- run_as: false
`,
`hosts:
- default:
- url: http://wazuh.manager
- port: 55000
- username: user
- password: password
- run_as: false
- custom:
- url: http://custom.manager
- port: 55000
- username: custom
- password: custompassword
- run_as: false
`,
`pattern: wazuh-alerts-*
hosts:
- default:
- url: http://wazuh.manager
- port: 55000
- username: user
- password: password
- run_as: false
- custom:
- url: http://custom.manager
- port: 55000
- username: custom
- password: custompassword
- run_as: false
- custom2:
- url: http://custom2.manager
- port: 55000
- username: custom2
- password: custompassword2
- run_as: false
`
];

it.each`
pluginConfiguration
${pluginConfigurationText[0]}
${pluginConfigurationText[1]}
${pluginConfigurationText[2]}
`('Obfuscate the hosts password', ({pluginConfiguration}) => {
// Create plugin configuration file
writeFileSync(WAZUH_DATA_CONFIG_APP_PATH, pluginConfiguration, { encoding: 'utf8' });
const configuration = getConfiguration();
configuration.hosts.forEach(host => {
const hostID = Object.keys(host)[0];
expect(Object.keys(host).length).toEqual(1);
expect(host[hostID].password).toEqual('*****');
});
});
});
17 changes: 10 additions & 7 deletions server/lib/get-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,19 @@ export function getConfiguration(options: {force?: boolean} = {}) {
*/
function obfuscateHostsConfiguration(configuration: any, obfuscateHostConfigurationKeys: string[]){
if(configuration.hosts){
configuration.hosts = Object.entries(configuration.hosts)
.reduce((accum, [hostID, hostConfiguration]) => {
return {...accum, [hostID]: {
...hostConfiguration,
...(obfuscateHostConfigurationKeys
configuration.hosts = configuration.hosts
.map((host) => {
const hostID = Object.keys(host)[0];
return {
[hostID]: {
...host[hostID],
...(obfuscateHostConfigurationKeys
.reduce((accumObfuscateHostConfigurationKeys, obfuscateHostConfigurationKey) =>
({...accumObfuscateHostConfigurationKeys, [obfuscateHostConfigurationKey]: '*****'}), {})
)
}}
}, {});
}
}
});
};
return configuration;
};
5 changes: 5 additions & 0 deletions server/routes/wazuh-utils/wazuh-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ hosts:
expect(response.body.data).toBeDefined();
expect(response.body.data.pattern).toBeDefined();
expect(response.body.data.hosts).toBeDefined();
response?.body?.data?.hosts?.map(host => {
const hostID = Object.keys(host)[0];
expect(Object.keys(host).length).toEqual(1);
expect(host[hostID].password).toEqual('*****');
});
});
});

Expand Down

0 comments on commit 3c3a4b6

Please sign in to comment.