Compare commits

...

3 Commits

Author SHA1 Message Date
Viktor Liu
6e22e8a6fb Register temp file cleanup before close in ssh config writer 2026-05-04 19:14:03 +02:00
Viktor Liu
9db7bec233 Use unique temp file and clean up on failure when writing ssh config 2026-05-04 18:25:46 +02:00
Lauri Tirkkonen
4268a5cfb7 [client] Use atomic write/rename pattern for ssh config 2026-05-04 18:24:52 +02:00

View File

@@ -229,8 +229,26 @@ func (m *Manager) writeSSHConfig(sshConfig string) error {
return fmt.Errorf("create SSH config directory %s: %w", m.sshConfigDir, err) return fmt.Errorf("create SSH config directory %s: %w", m.sshConfigDir, err)
} }
if err := writeFileWithTimeout(sshConfigPath, []byte(sshConfig), 0644); err != nil { tmp, err := os.CreateTemp(m.sshConfigDir, m.sshConfigFile+".*.tmp")
return fmt.Errorf("write SSH config file %s: %w", sshConfigPath, err) if err != nil {
return fmt.Errorf("create temp SSH config: %w", err)
}
tmpPath := tmp.Name()
defer func() {
if err := os.Remove(tmpPath); err != nil && !os.IsNotExist(err) {
log.Debugf("remove temp SSH config %s: %v", tmpPath, err)
}
}()
if err := tmp.Close(); err != nil {
return fmt.Errorf("close temp SSH config %s: %w", tmpPath, err)
}
if err := writeFileWithTimeout(tmpPath, []byte(sshConfig), 0644); err != nil {
return fmt.Errorf("write SSH config file %s: %w", tmpPath, err)
}
if err := os.Rename(tmpPath, sshConfigPath); err != nil {
return fmt.Errorf("rename SSH config %s -> %s: %w", tmpPath, sshConfigPath, err)
} }
log.Infof("Created NetBird SSH client config: %s", sshConfigPath) log.Infof("Created NetBird SSH client config: %s", sshConfigPath)