From 82e0d9c434482688b9278178cf741db69ced07bd Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Wed, 14 Aug 2024 16:08:02 -0400 Subject: [PATCH] Rework ssh key init to reset key and use context management --- confluent_server/confluent/sshutil.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/confluent_server/confluent/sshutil.py b/confluent_server/confluent/sshutil.py index 1f8960d8..40512648 100644 --- a/confluent_server/confluent/sshutil.py +++ b/confluent_server/confluent/sshutil.py @@ -215,12 +215,13 @@ def initialize_root_key(generate, automation=False): suffix = 'rootpubkey' keyname = '/var/lib/confluent/public/site/ssh/{0}.{1}'.format( myname, suffix) + if authorized: + with open(keyname, 'w'): + pass for auth in authorized: - local_key = open(auth, 'r') - dest = open(keyname, 'a') - dest.write(local_key.read()) - local_key.close() - dest.close() + with open(auth, 'r') as local_key: + with open(keyname, 'a') as dest: + dest.write(local_key.read()) if os.path.exists(keyname): os.chmod(keyname, 0o644) os.chown(keyname, neededuid, -1)