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

Add permission error when user cannot write to /etc/nginx folder #62

Merged
merged 6 commits into from
Jan 17, 2025
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
8 changes: 8 additions & 0 deletions nginx_config_reloader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from nginx_config_reloader.settings import (
BACKUP_CONFIG_DIR,
CUSTOM_CONFIG_DIR,
MAIN_CONFIG_DIR,
DIR_TO_WATCH,
ERROR_FILE,
FORBIDDEN_CONFIG_REGEX,
Expand Down Expand Up @@ -133,6 +134,9 @@ def install_magento_config(self):
# Move temporary symlink to actual location, overwriting existing link or file
os.rename(MAGENTO_CONF_NEW, MAGENTO_CONF)

def check_can_write_to_main_config_dir(self):
return os.access(MAIN_CONFIG_DIR, os.W_OK)

def check_no_forbidden_config_directives_are_present(self):
"""
Loop over the :FORBIDDEN_CONFIG_REGEX: to check if nginx config directory contains forbidden configuration
Expand Down Expand Up @@ -196,6 +200,10 @@ def _apply(self):
logger.debug("Applying new config")
if self.check_no_forbidden_config_directives_are_present():
return False

if not self.check_can_write_to_main_config_dir():
self.logger.error("No write permissions to main nginx config directory, please check your permissions.")
return False

if not self.no_magento_config:
try:
Expand Down
2 changes: 1 addition & 1 deletion nginx_config_reloader/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
import subprocess

import os
tdgroot marked this conversation as resolved.
Show resolved Hide resolved

def directory_is_unmounted(path):
output = subprocess.check_output(
Expand Down
19 changes: 19 additions & 0 deletions tests/test_nginx_config_reloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ def setUp(self):
self.source = mkdtemp()
self.dest = mkdtemp()
self.backup = mkdtemp()
self.main = mkdtemp()
_, self.mag_conf = mkstemp(text=True)
_, self.mag1_conf = mkstemp(text=True)
_, self.mag2_conf = mkstemp(text=True)

nginx_config_reloader.MAIN_CONFIG_DIR = self.main
nginx_config_reloader.DIR_TO_WATCH = self.source
nginx_config_reloader.CUSTOM_CONFIG_DIR = self.dest
nginx_config_reloader.BACKUP_CONFIG_DIR = self.backup
Expand All @@ -49,6 +51,7 @@ def tearDown(self):
shutil.rmtree(self.source, ignore_errors=True)
shutil.rmtree(self.dest, ignore_errors=True)
shutil.rmtree(self.backup, ignore_errors=True)
shutil.rmtree(self.main, ignore_errors=True)
for f in [self.mag_conf, self.mag1_conf, self.mag2_conf]:
try:
os.unlink(f)
Expand Down Expand Up @@ -646,6 +649,22 @@ def test_permissions_are_masked_for_file_in_subdir(self):
& stat.S_IXOTH
)

def test_no_permission_to_main_config_dir(self):
os.chmod(self.main, 0o400) # Read-only

tm = self._get_nginx_config_reloader_instance()
try:
with self.assertLogs('root', level='ERROR') as cm:
result = tm.apply_new_config()
self.assertFalse(result)
self.assertTrue(
any("No write permissions to main nginx config directory" in message for message in cm.output),
"Expected error message not found in logs."
)
finally:
# Restore permissions after test
os.chmod(self.main, 0o700)

def _get_nginx_config_reloader_instance(
self,
no_magento_config=False,
Expand Down