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

Enhance the getConfiguration backend service #5428

Merged
merged 6 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
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

### Fix

- Fix 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 - OpenSearch Dashboards 2.6.0 - 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/config directory.
unlinkSync(WAZUH_DATA_ABSOLUTE_PATH);
});

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

afterEach(() => {
// Remove <PLUGIN_PLATFORM_PATH>/data/wazuh/config/wazuh-registry 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);
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 @@ -112,6 +112,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