From b12152e51e48f00918a15462cef7a8a26eebfa59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20=C3=81ngel?= Date: Thu, 24 Jan 2019 14:58:47 +0100 Subject: [PATCH] Using chownSync and chmodSync. Added try/catch block too (#295) --- CHANGELOG.md | 3 ++- helpers/logger.js | 36 +++++++++++++++++++++++------------- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b2b6628..40898291 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,8 @@ All notable changes to this project will be documented in this file. ## [v3.8.1] ### Fixed -- Fixed improper error handling ([#296](https://github.com/wazuh/wazuh-api/pull/296)) +- Fixed improper error handling ([#296](https://github.com/wazuh/wazuh-api/pull/296)). +- Fix bug setting file permissions when rotating API logs file ([#295](https://github.com/wazuh/wazuh-api/pull/295)). ## [v3.8.0] diff --git a/helpers/logger.js b/helpers/logger.js index d2b3b1ea..fed79f2f 100755 --- a/helpers/logger.js +++ b/helpers/logger.js @@ -123,18 +123,28 @@ var stream = rfs(generator, { }); stream.on('rotated', function(filename) { - // rotation job completed with success producing given filename - // setting correct permissions for generated files - logger.log("Rotated: " + filename); - fs.chmod(filename, 0o640); - fs.chmod(path.dirname(filename), 0o750); - fs.chmod(path.dirname(path.dirname(filename)), 0o750); - - // if the API is running as root, set the user of the created files to ossec - if (!config.drop_privileges) { - fs.chown(filename, ossec_uid, ossec_gid); - fs.chown(path.dirname(filename), ossec_uid, ossec_gid); - fs.chown(path.dirname(path.dirname(filename)), ossec_uid, ossec_gid); - fs.chown(absolute_path_log, ossec_uid, ossec_gid); + try { + // rotation job completed with success producing given filename + // setting correct permissions for generated files + logger.log("Rotated: " + filename); + fs.chmodSync(filename, 0o640); + fs.chmodSync(path.dirname(filename), 0o750); + fs.chmodSync(path.dirname(path.dirname(filename)), 0o750); + + // if the API is running as root, set the user of the created files to ossec + if (!config.drop_privileges) { + fs.chownSync(filename, ossec_uid, ossec_gid); + fs.chownSync(path.dirname(filename), ossec_uid, ossec_gid); + fs.chownSync(path.dirname(path.dirname(filename)), ossec_uid, ossec_gid); + fs.chownSync(absolute_path_log, ossec_uid, ossec_gid); + } + + // Prevents from crashing the service if the above instructions fail + } catch (error) { + try { + logger.error(error.message || error); + } catch (err) { + console.log(err.message || err) + } } });