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

[PBCKP-159] fix false directory init #489

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ do_init(CatalogState *catalogState)
elog(ERROR, "cannot open backup catalog directory \"%s\": %s",
catalogState->catalog_path, strerror(errno_tmp));
}
else if (results == 1)
{
/* Check whether we have all(rw) rights for directory */
if (access(catalogState->catalog_path, 6) == -1) /* == R_OK | W_OK */
{
int errno_tmp = errno;
elog(ERROR, "cannot access backup catalog directory \"%s\": %s",
catalogState->catalog_path, strerror(errno_tmp));
}
}

/* create backup catalog root directory */
dir_create_dir(catalogState->catalog_path, DIR_PERMISSION, false);
Expand Down
38 changes: 38 additions & 0 deletions tests/init.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import os
import stat # for chmod
import unittest
from .helpers.ptrack_helpers import dir_files, ProbackupTest, ProbackupException
import shutil

if os.name == 'nt':
import win32security
import ntsecuritycon as con

module_name = 'init'

Expand Down Expand Up @@ -86,6 +90,40 @@ def test_already_exist(self):

# Clean after yourself
self.del_test_dir(module_name, fname)

# @unittest.skip("skip")
def test_no_rights_for_directory(self):
"""Failure with backup catalog existed and empty but user has no writing permissions"""
fname = self.id().split(".")[3]
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
node = self.make_simple_node(base_dir=os.path.join(module_name, fname, 'node'))
os.mkdir(backup_dir)
# changing directory rights
if os.name == 'nt': # windows block
user, domain, type = win32security.LookupAccountName("", os.getlogin())
sd = win32securityGetFileSecurity(backup_dir, win32security.DACL_SECURIRY_INFORMATION)
dacl = sd.GetSecurityDescriptorDacl()

dacl.AddAccessDeniedAce(win32security.ACL_REVISION, con.FILE_WRITE_DATA, user) # deny writing permission
sd.SetSecurityDescriptorDacl(1, dacl, 0)
win32security.SetFileSecurity(backup_dir, win32security.DACL_SECURIRY_INFORMATION, sd)
else:
os.chmod(backup_dir, stat.S_IREAD) # set read-only flag for current user
assert os.access(backup_dir, os.R_OK) and not os.access(backup_dir, os.W_OK)

self.init_pb(backup_dir)
try:
self.show_pb(backup_dir, 'node')
self.assertEqual(1, 0, 'Expecting Error due to initialization in empty directory with no rithts for writing. Output: {0} \n CMD: {1}'.format(
repr(self.output), self.cmd))
except ProbackupException as e:
self.assertIn(
"ERROR: Instance 'node' does not exist in this backup catalog",
e.message,
'\n Unexpected Error Message: {0}\n CMD: {1}'.format(repr(e.message), self.cmd))

# Clean after yourself
self.del_test_dir(module_name, fname)

# @unittest.skip("skip")
def test_abs_path(self):
Expand Down