From 15e7e4464e94bdd248bc9320d99b1de6d4bc4cf2 Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Wed, 2 Mar 2022 16:04:01 -0500 Subject: [PATCH] Keep known_hosts cleaner When repeating osdeploy initialize of local known_hosts, more gracefeully avoid duplicate entries. --- confluent_server/bin/osdeploy | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/confluent_server/bin/osdeploy b/confluent_server/bin/osdeploy index 44a46b29..013063eb 100644 --- a/confluent_server/bin/osdeploy +++ b/confluent_server/bin/osdeploy @@ -297,9 +297,29 @@ def initialize(cmdset): if cmdset.l: local_node_trust_setup() if cmdset.k: - with open('/etc/ssh/ssh_known_hosts', 'a+b') as skh: + cas = set([]) + cakeys = set([]) + try: + with open('/etc/ssh/ssh_known_hosts', 'rb') as skh: + for line in skh.read().split(b'\n'): + try: + cakey = line.split()[3] + cakeys.add(cakey) + except IndexError: + pass + if line: + cas.add(line) + except IOError: + pass + with open('/etc/ssh/ssh_known_hosts', 'wb') as skh: + for ca in cas: + skh.write(ca) + skh.write(b'\n') for cafile in glob.glob('/var/lib/confluent/public/site/ssh/*.ca'): cacert = open(cafile, 'rb').read() + cakey = cacert.split()[1] + if cakey in cakeys: + continue cacert = b'@cert-authority * ' + cacert skh.write(cacert) if cmdset.g: