logging_handler.py: Fix file permissions when root

This commit is contained in:
Mykola Grymalyuk
2023-02-09 11:04:15 -07:00
parent 3b5e4f10f6
commit 0671828c9b
2 changed files with 72 additions and 11 deletions
+24
View File
@@ -1,6 +1,8 @@
import logging
import sys
import threading
import os
import subprocess
from pathlib import Path
@@ -19,6 +21,9 @@ class InitializeLoggingSupport:
>>> from resources.logging_handler import InitializeLoggingSupport
>>> InitializeLoggingSupport()
FOR DEVELOPERS:
- Do not invoke logging until after '_attempt_initialize_logging_configuration()' has been invoked
"""
def __init__(self) -> None:
@@ -35,6 +40,7 @@ class InitializeLoggingSupport:
self._clean_log_file()
self._attempt_initialize_logging_configuration()
self._implement_custom_traceback_handler()
self._fix_file_permission()
def __del__(self):
@@ -81,6 +87,24 @@ class InitializeLoggingSupport:
print(f"- Failed to clean log file: {e}")
def _fix_file_permission(self):
"""
Fixes file permission for log file
If OCLP was invoked as root, file permission will only allow root to write to log file
This in turn breaks normal OCLP execution to write to log file
"""
if os.geteuid() != 0:
return
result = subprocess.run(["chmod", "777", self.log_filepath], capture_output=True)
if result.returncode != 0:
print(f"- Failed to fix log file permissions")
if result.stderr:
print(result.stderr.decode("utf-8"))
def _initialize_logging_configuration(self, log_to_file: bool = True):
"""
Initialize logging framework configuration