From a1dd8de337bcdbc4fa690a8af8ea8e4ac37508d9 Mon Sep 17 00:00:00 2001 From: Adnan Maolood Date: Thu, 17 Dec 2020 17:15:24 -0500 Subject: [PATCH] Fix locking up of KnownHostsFile and CertificateDir --- cert.go | 5 +---- tofu.go | 12 ++---------- 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/cert.go b/cert.go index dd8a1f5..0c897dd 100644 --- a/cert.go +++ b/cert.go @@ -82,8 +82,6 @@ func (c *CertificateDir) Lookup(scope string) (tls.Certificate, bool) { // localhost.crt (certificate) and localhost.key (private key). // New certificates will be written to this directory. func (c *CertificateDir) Load(path string) error { - c.mu.Lock() - defer c.mu.Unlock() matches, err := filepath.Glob(filepath.Join(path, "*.crt")) if err != nil { return err @@ -99,8 +97,7 @@ func (c *CertificateDir) Load(path string) error { scope = strings.ReplaceAll(scope, ":", "/") c.Add(scope, cert) } - c.dir = true - c.path = path + c.SetDir(path) return nil } diff --git a/tofu.go b/tofu.go index 9fa08eb..68f2a96 100644 --- a/tofu.go +++ b/tofu.go @@ -90,20 +90,12 @@ func (k *KnownHostsFile) writeKnownHost(w io.Writer, hostname string, f Fingerpr // It creates the file if it does not exist. // New known hosts will be appended to the file. func (k *KnownHostsFile) Load(path string) error { - k.mu.Lock() - defer k.mu.Unlock() - f, err := os.OpenFile(path, os.O_CREATE|os.O_RDONLY, 0644) + f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0644) if err != nil { return err } k.Parse(f) - f.Close() - // Open the file for append-only use - f, err = os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644) - if err != nil { - return err - } - k.out = f + k.SetOutput(f) return nil }