From 25c1f43ca863958cb0b40ade42f82f9b06a191da Mon Sep 17 00:00:00 2001 From: John Mulligan Date: Tue, 6 Jun 2023 13:16:29 -0400 Subject: [PATCH] cephadm: convert SNMPGateway create_daemon_conf to use write_new While it is not entirely clear why this pattern of using os.open and posix open flags instead of `open` directly was used I determined (using strace) that the only major difference between these open flags and those used by `open` was the lack of O_TRUNC. Unlike some other cases this function does not use an intermediate temporary file. This means that if the file being written already exists and the data being written is smaller then the remaining data will not be over-written. Example: ``` $ cat existing AAAAAAAA $ cat existing bbbAAAAA ``` I looked over the context that this function is used in and decided that this behavior must not be intentional. Thus it should be safe to convert this function to `write_new`. Signed-off-by: John Mulligan (cherry picked from commit 6ec5a5ead36a79ce331b6943296c401feea7a896) --- src/cephadm/cephadm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cephadm/cephadm.py b/src/cephadm/cephadm.py index e797cfe7d4c..3284046ca61 100755 --- a/src/cephadm/cephadm.py +++ b/src/cephadm/cephadm.py @@ -511,7 +511,7 @@ class SNMPGateway: def create_daemon_conf(self) -> None: """Creates the environment file holding 'secrets' passed to the snmp-notifier daemon""" - with open(os.open(self.conf_file_path, os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f: + with write_new(self.conf_file_path, perms=0o600) as f: if self.snmp_version == 'V2c': f.write(f'SNMP_NOTIFIER_COMMUNITY={self.snmp_community}\n') else: -- 2.39.5