diff --git a/napalm_device_types/scheduled_jobs_posix.py b/napalm_device_types/scheduled_jobs_posix.py index 441a198..8fee6c9 100644 --- a/napalm_device_types/scheduled_jobs_posix.py +++ b/napalm_device_types/scheduled_jobs_posix.py @@ -244,8 +244,11 @@ class PosixScheduledJobsMixin(ScheduledJobsMixin): if user is not None: self._sj_write_crontab(user, text) return - mode, owner, group = self._sj_checked([*self.STAT_ARGV, path]).stdout.split()[:3] - self._sj_install(text, path, mode, owner, group) + # Through a symbolic link to the file it names, with that file's mode: the + # link's own mode is 777, and cron ignores a file anyone may write. + real = self._sj_checked(["readlink", "-f", "--", path]).stdout.strip() or path + mode, owner, group = self._sj_checked([*self.STAT_ARGV, real]).stdout.split()[:3] + self._sj_install(text, real, mode, owner, group) def _sj_line(self, job: dict[str, object], text: str) -> CronLine: """The job's line in *text* as it is now, read again for the write.""" diff --git a/tests/test_scheduled_jobs_posix.py b/tests/test_scheduled_jobs_posix.py index 57d515e..4b746e7 100644 --- a/tests/test_scheduled_jobs_posix.py +++ b/tests/test_scheduled_jobs_posix.py @@ -335,6 +335,19 @@ class TestSwitching: assert f"install -D -m {mode & 0o777:o}" in host.log() assert os.stat(path).st_mode == mode + def test_a_linked_file_is_written_through_its_link(self, root: Path): + real = root / "etc/vzdump.cron" + real.write_text("0 1 * * * root vzdump --all\n") + real.chmod(0o640) + link = root / "etc/cron.d/vzdump" + link.symlink_to(real) + host = Host(root) + job = self._cron(host, str(link)) + host.manage_scheduled_job(job["key"], "disable", digest=job["digest"]) + assert link.is_symlink() + assert real.read_text() == "#napalm-disabled: 0 1 * * * root vzdump --all\n" + assert real.stat().st_mode & 0o777 == 0o640 + def test_a_users_line_goes_through_crontab(self, root: Path): host = Host(root) job = self._cron(host, str(root / "spool/www-data"))