Skip to content

Commit

Permalink
Restructure optimize/wazuh folder (#2116)
Browse files Browse the repository at this point in the history
* Restructure optimize/wazuh folder

* Update protocol of initial config example

* Replace path with a constant

* Remove wazuh.yml file

* Update README upgrade guide

* Fix plugin helper error

Co-authored-by: Pablo Torres <[email protected]>
  • Loading branch information
juankaromo and pablotr9 committed Mar 11, 2020
1 parent f8122d0 commit fd36867
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 30 deletions.
3 changes: 1 addition & 2 deletions .kibana-plugin-helpers.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
"package.json",
"LICENSE",
"tsconfig.json",
"wazuh.yml",
"index.js",
"init.js",
"server/**/*",
"public/**/*",
"util/**/*"
]
}
}
2 changes: 1 addition & 1 deletion public/controllers/settings/components/add-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export class AddApi extends Component {
const editConfigChildren = (
<div>
<EuiText>
Modify <EuiCode>kibana/plugins/wazuh/wazuh.yml</EuiCode> to set the
Modify <EuiCode>/usr/share/kibana/optimize/wazuh/config/wazuh.yml</EuiCode> to set the
connection information.
</EuiText>
<EuiSpacer />
Expand Down
2 changes: 1 addition & 1 deletion public/controllers/settings/components/api-is-down.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ hosts:
<div>
<EuiText>
Review the settings in the{' '}
<EuiCode>kibana/plugins/wazuh/wazuh.yml</EuiCode> file.
<EuiCode>/usr/share/kibana/optimize/wazuh/config/wazuh.yml</EuiCode> file.
</EuiText>
<EuiSpacer />
<EuiCodeBlock language="yaml">{apiExample}</EuiCodeBlock>
Expand Down
2 changes: 1 addition & 1 deletion public/templates/settings/settings-configuration.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
configuration settings
</span>
<span class="md-subheader">Configuration file located at <span
class="wz-text-monospace">/usr/share/kibana/plugins/wazuh/wazuh.yml</span></span>
class="wz-text-monospace">/usr/share/kibana/optimize/wazuh/config/wazuh.yml</span></span>
</div>
<!-- End headline -->
<div layout="row" layout-align="start center" ng-if="!ctrl.load">
Expand Down
2 changes: 1 addition & 1 deletion public/templates/settings/settings-logs.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<react-component name="EuiIcon" props="{type:'visTable'}" /> Wazuh Kibana plugin log
messages</span>
<span class="md-subheader">Log file located at <span
class="wz-text-monospace">/usr/share/kibana/optimize/wazuh-logs/wazuhapp.log</span></span>
class="wz-text-monospace">/usr/share/kibana/optimize/wazuh/logs/wazuhapp.log</span></span>
</div>

<div ng-if="ctrl.loadingLogs" class="md-padding wz-margin-top-16">
Expand Down
9 changes: 8 additions & 1 deletion server/controllers/wazuh-reporting.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ import {

import { log } from '../logger';

const REPORTING_PATH = '../../../../optimize/wazuh-reporting';
const BASE_OPTIMIZE_PATH = '../../../../optimize';
const REPORTING_PATH = `${BASE_OPTIMIZE_PATH}/wazuh/downloads/reports`;

export class WazuhReportingCtrl {
/**
Expand Down Expand Up @@ -1811,6 +1812,12 @@ export class WazuhReportingCtrl {
// Init
this.printer = new PdfPrinter(this.fonts);
this.dd.content = [];
if (!fs.existsSync(path.join(__dirname, `${BASE_OPTIMIZE_PATH}/wazuh`))) {
fs.mkdirSync(path.join(__dirname, `${BASE_OPTIMIZE_PATH}/wazuh`));
}
if (!fs.existsSync(path.join(__dirname, `${BASE_OPTIMIZE_PATH}/wazuh/downloads`))) {
fs.mkdirSync(path.join(__dirname, `${BASE_OPTIMIZE_PATH}/wazuh/downloads`));
}
if (!fs.existsSync(path.join(__dirname, REPORTING_PATH))) {
fs.mkdirSync(path.join(__dirname, REPORTING_PATH));
}
Expand Down
4 changes: 2 additions & 2 deletions server/controllers/wazuh-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ export class WazuhUtilsCtrl {
async getAppLogs(req, reply) {
try {
const lastLogs = await simpleTail(
path.join(__dirname, '../../../../optimize/wazuh-logs/wazuhapp.log'),
20
path.join(__dirname, '../../../../optimize/wazuh/logs/wazuhapp.log'),
50
);
return lastLogs && Array.isArray(lastLogs)
? {
Expand Down
8 changes: 4 additions & 4 deletions server/lib/get-configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import yml from 'js-yaml';
import path from 'path';
let cachedConfiguration = null;
let lastAssign = new Date().getTime();
export function getConfiguration() {
export function getConfiguration(isUpdating = false) {
try {
const now = new Date().getTime();
const dateDiffer = now - lastAssign;
if (!cachedConfiguration || dateDiffer >= 10000) {
const customPath = path.join(__dirname, '../../wazuh.yml');
if (!cachedConfiguration || dateDiffer >= 10000 || isUpdating) {
const customPath = path.join(__dirname, '../../../../optimize/wazuh/config/wazuh.yml');
const raw = fs.readFileSync(customPath, { encoding: 'utf-8' });
const file = yml.load(raw);
cachedConfiguration = { ...file };
Expand All @@ -29,4 +29,4 @@ export function getConfiguration() {
} catch (error) {
return false;
}
}
}
18 changes: 16 additions & 2 deletions wazuh.yml → server/lib/initial-wazuh-config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
---
/*
* Wazuh app - Initial basic configuration file
* Copyright (C) 2015-2020 Wazuh, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Find more information about this on the LICENSE file.
*/

export const initialWazuhConfig = `---
#
# Wazuh app - App configuration file
# Copyright (C) 2015-2020 Wazuh, Inc.
Expand Down Expand Up @@ -129,4 +141,6 @@ hosts:
url: http://localhost
port: 55000
user: foo
password: bar
password: bar
`
15 changes: 14 additions & 1 deletion server/lib/manage-hosts.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ import yml from 'js-yaml';
import path from 'path';
import { log } from '../logger';
import { UpdateRegistry } from './update-registry';
import { initialWazuhConfig } from './initial-wazuh-config'

const BASE_LOGS_PATH = '../../../../optimize/wazuh';

export class ManageHosts {
constructor() {
this.busy = false;
this.file = path.join(__dirname, '../../wazuh.yml');
this.file = path.join(__dirname, `${BASE_LOGS_PATH}/config/wazuh.yml`);
this.updateRegistry = new UpdateRegistry();
this.initialConfig = initialWazuhConfig;
}

/**
Expand Down Expand Up @@ -64,6 +68,15 @@ export class ManageHosts {
try {
this.checkBusy();
this.busy = true;
if (!fs.existsSync(path.join(__dirname, BASE_LOGS_PATH))) {
fs.mkdirSync(path.join(__dirname, BASE_LOGS_PATH));
}
if (!fs.existsSync(path.join(__dirname, `${BASE_LOGS_PATH}/config`))) {
fs.mkdirSync(path.join(__dirname, `${BASE_LOGS_PATH}/config`));
}
if (!fs.existsSync(path.join(__dirname, '../../../../optimize/wazuh/config/wazuh.yml'))) {
await fs.writeFileSync(this.file, this.initialConfig, 'utf8');
}
const raw = fs.readFileSync(this.file, { encoding: 'utf-8' });
this.busy = false;
const content = yml.load(raw);
Expand Down
4 changes: 2 additions & 2 deletions server/lib/update-configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const needRestartFields = [
export class UpdateConfigurationFile {
constructor() {
this.busy = false;
this.file = path.join(__dirname, '../../wazuh.yml');
this.file = path.join(__dirname, '../../../../optimize/wazuh/config/wazuh.yml');
}

/**
Expand Down Expand Up @@ -64,7 +64,7 @@ export class UpdateConfigurationFile {
throw new Error('Another process is updating the configuration file');
}
this.busy = true;
const configuration = getConfiguration() || {};
const configuration = getConfiguration(true) || {};
const adminUndefined = !Object.keys(configuration).includes('admin');
const adminIsTrue = configuration.admin;

Expand Down
31 changes: 19 additions & 12 deletions server/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import { getConfiguration } from './lib/get-configuration';
let allowed = false;
let wazuhlogger = undefined;
let wazuhPlainLogger = undefined;
const logsBasePath = '../../../optimize/wazuh/logs'
const plainLogFilePath = `${logsBasePath}/wazuhapp-plain.log`
const rawLogFilePath = `${logsBasePath}/wazuhapp.log`

/**
* Here we create the loggers
Expand All @@ -37,7 +40,7 @@ const initLogger = () => {
new winston.transports.File({
filename: path.join(
__dirname,
'../../../optimize/wazuh-logs/wazuhapp.log'
rawLogFilePath
)
})
]
Expand All @@ -54,7 +57,7 @@ const initLogger = () => {
new winston.transports.File({
filename: path.join(
__dirname,
'../../../optimize/wazuh-logs/wazuhapp-plain.log'
plainLogFilePath
)
})
]
Expand All @@ -65,12 +68,16 @@ const initLogger = () => {
};

/**
* Checks if wazuh-logs exists. If it doesn't exist, it will be created.
* Checks if wazuh/logs exists. If it doesn't exist, it will be created.
*/
const initDirectory = async () => {
try {
if (!fs.existsSync(path.join(__dirname, '../../../optimize/wazuh-logs'))) {
fs.mkdirSync(path.join(__dirname, '../../../optimize/wazuh-logs'));

if (!fs.existsSync(path.join(__dirname, '../../../optimize/wazuh'))) {
fs.mkdirSync(path.join(__dirname, '../../../optimize/wazuh'));
}
if (!fs.existsSync(path.join(__dirname, logsBasePath))) {
fs.mkdirSync(path.join(__dirname, logsBasePath));
}
if (
typeof wazuhlogger === 'undefined' ||
Expand Down Expand Up @@ -109,18 +116,18 @@ const checkFiles = () => {
if (allowed) {
if (
getFilesizeInMegaBytes(
path.join(__dirname, '../../../optimize/wazuh-logs/wazuhapp.log')
path.join(__dirname, rawLogFilePath)
) >= 100
) {
fs.renameSync(
path.join(__dirname, '../../../optimize/wazuh-logs/wazuhapp.log'),
path.join(__dirname, rawLogFilePath),
path.join(
__dirname,
`../../../optimize/wazuh-logs/wazuhapp.${new Date().getTime()}.log`
`${logsBasePath}/wazuhapp.${new Date().getTime()}.log`
)
);
fs.writeFileSync(
path.join(__dirname, '../../../optimize/wazuh-logs/wazuhapp.log'),
path.join(__dirname, rawLogFilePath),
JSON.stringify({
date: new Date(),
level: 'info',
Expand All @@ -131,14 +138,14 @@ const checkFiles = () => {
}
if (
getFilesizeInMegaBytes(
path.join(__dirname, '../../../optimize/wazuh-logs/wazuhapp-plain.log')
path.join(__dirname, plainLogFilePath)
) >= 100
) {
fs.renameSync(
path.join(__dirname, '../../../optimize/wazuh-logs/wazuhapp-plain.log'),
path.join(__dirname, plainLogFilePath),
path.join(
__dirname,
`../../../optimize/wazuh-logs/wazuhapp-plain.${new Date().getTime()}.log`
`${plainLogFilePath}.${new Date().getTime()}.log`
)
);
}
Expand Down

0 comments on commit fd36867

Please sign in to comment.