Skip to content

Commit 916e9e1

Browse files
poespasJonathan Visser
andauthored
Add permission error when user cannot write to /etc/nginx folder (#62)
* Add permission error when user cannot write to /etc/nginx folder * Fake main directory in tests, add unit test for checking permissions * Remove unneeded import * Change unit test to test the check_can_write_to_main_config_dir function * Fix linting issues --------- Co-authored-by: Jonathan Visser <jonathan.visser@hypernode.com>
1 parent 249c38d commit 916e9e1

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

nginx_config_reloader/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
MAGENTO1_CONF,
3131
MAGENTO2_CONF,
3232
MAGENTO_CONF,
33+
MAIN_CONFIG_DIR,
3334
NGINX,
3435
NGINX_PID_FILE,
3536
UNPRIVILEGED_GID,
@@ -133,6 +134,9 @@ def install_magento_config(self):
133134
# Move temporary symlink to actual location, overwriting existing link or file
134135
os.rename(MAGENTO_CONF_NEW, MAGENTO_CONF)
135136

137+
def check_can_write_to_main_config_dir(self):
138+
return os.access(MAIN_CONFIG_DIR, os.W_OK)
139+
136140
def check_no_forbidden_config_directives_are_present(self):
137141
"""
138142
Loop over the :FORBIDDEN_CONFIG_REGEX: to check if nginx config directory contains forbidden configuration
@@ -197,6 +201,12 @@ def _apply(self):
197201
if self.check_no_forbidden_config_directives_are_present():
198202
return False
199203

204+
if not self.check_can_write_to_main_config_dir():
205+
self.logger.error(
206+
"No write permissions to main nginx config directory, please check your permissions."
207+
)
208+
return False
209+
200210
if not self.no_magento_config:
201211
try:
202212
self.install_magento_config()

tests/test_nginx_config_reloader.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ def setUp(self):
2626
self.source = mkdtemp()
2727
self.dest = mkdtemp()
2828
self.backup = mkdtemp()
29+
self.main = mkdtemp()
2930
_, self.mag_conf = mkstemp(text=True)
3031
_, self.mag1_conf = mkstemp(text=True)
3132
_, self.mag2_conf = mkstemp(text=True)
3233

34+
nginx_config_reloader.MAIN_CONFIG_DIR = self.main
3335
nginx_config_reloader.DIR_TO_WATCH = self.source
3436
nginx_config_reloader.CUSTOM_CONFIG_DIR = self.dest
3537
nginx_config_reloader.BACKUP_CONFIG_DIR = self.backup
@@ -49,6 +51,7 @@ def tearDown(self):
4951
shutil.rmtree(self.source, ignore_errors=True)
5052
shutil.rmtree(self.dest, ignore_errors=True)
5153
shutil.rmtree(self.backup, ignore_errors=True)
54+
shutil.rmtree(self.main, ignore_errors=True)
5255
for f in [self.mag_conf, self.mag1_conf, self.mag2_conf]:
5356
try:
5457
os.unlink(f)
@@ -646,6 +649,17 @@ def test_permissions_are_masked_for_file_in_subdir(self):
646649
& stat.S_IXOTH
647650
)
648651

652+
def test_no_permission_to_main_config_dir(self):
653+
os.chmod(self.main, 0o400) # Read-only
654+
655+
tm = self._get_nginx_config_reloader_instance()
656+
try:
657+
result = tm.check_can_write_to_main_config_dir()
658+
self.assertFalse(result)
659+
finally:
660+
# Restore permissions after test
661+
os.chmod(self.main, 0o700)
662+
649663
def _get_nginx_config_reloader_instance(
650664
self,
651665
no_magento_config=False,

0 commit comments

Comments
 (0)