Fix locking up of KnownHostsFile and CertificateDir

This commit is contained in:
Adnan Maolood 2020-12-17 17:15:24 -05:00
parent 7be0715d39
commit a1dd8de337
2 changed files with 3 additions and 14 deletions

View File

@ -82,8 +82,6 @@ func (c *CertificateDir) Lookup(scope string) (tls.Certificate, bool) {
// localhost.crt (certificate) and localhost.key (private key). // localhost.crt (certificate) and localhost.key (private key).
// New certificates will be written to this directory. // New certificates will be written to this directory.
func (c *CertificateDir) Load(path string) error { func (c *CertificateDir) Load(path string) error {
c.mu.Lock()
defer c.mu.Unlock()
matches, err := filepath.Glob(filepath.Join(path, "*.crt")) matches, err := filepath.Glob(filepath.Join(path, "*.crt"))
if err != nil { if err != nil {
return err return err
@ -99,8 +97,7 @@ func (c *CertificateDir) Load(path string) error {
scope = strings.ReplaceAll(scope, ":", "/") scope = strings.ReplaceAll(scope, ":", "/")
c.Add(scope, cert) c.Add(scope, cert)
} }
c.dir = true c.SetDir(path)
c.path = path
return nil return nil
} }

12
tofu.go
View File

@ -90,20 +90,12 @@ func (k *KnownHostsFile) writeKnownHost(w io.Writer, hostname string, f Fingerpr
// It creates the file if it does not exist. // It creates the file if it does not exist.
// New known hosts will be appended to the file. // New known hosts will be appended to the file.
func (k *KnownHostsFile) Load(path string) error { func (k *KnownHostsFile) Load(path string) error {
k.mu.Lock() f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0644)
defer k.mu.Unlock()
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDONLY, 0644)
if err != nil { if err != nil {
return err return err
} }
k.Parse(f) k.Parse(f)
f.Close() k.SetOutput(f)
// 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
return nil return nil
} }