htpasswd: change tmp permissions

This commit is contained in:
Devaev Maxim
2019-02-10 03:57:34 +03:00
parent 8550ace4a1
commit d603a216a3

View File

@@ -19,11 +19,17 @@ from .. import init
@contextlib.contextmanager @contextlib.contextmanager
def _get_htpasswd_for_write(config: Section) -> Generator[passlib.apache.HtpasswdFile, None, None]: def _get_htpasswd_for_write(config: Section) -> Generator[passlib.apache.HtpasswdFile, None, None]:
path = config.kvmd.auth.htpasswd path = config.kvmd.auth.htpasswd
(tmp_fd, tmp_path) = tempfile.mkstemp(prefix=".", dir=os.path.dirname(path)) (tmp_fd, tmp_path) = tempfile.mkstemp(
prefix=".%s." % (os.path.basename(path)),
dir=os.path.dirname(path),
)
try: try:
stat = os.stat(path)
try: try:
with open(path, "rb") as htpasswd_file: with open(path, "rb") as htpasswd_file:
os.write(tmp_fd, htpasswd_file.read()) os.write(tmp_fd, htpasswd_file.read())
os.fchown(tmp_fd, stat.st_uid, stat.st_gid)
os.fchmod(tmp_fd, stat.st_mode)
finally: finally:
os.close(tmp_fd) os.close(tmp_fd)
htpasswd = passlib.apache.HtpasswdFile(tmp_path) htpasswd = passlib.apache.HtpasswdFile(tmp_path)