Allow all three auth types

This commit is contained in:
Pim van Pelt
2025-07-06 22:16:01 +02:00
parent 96c7c3aeaa
commit 1afa1e6d43

View File

@ -135,7 +135,7 @@ func (rb *RouterBackup) Connect() error {
}
// If SSH agent didn't work, try key file
if len(config.Auth) == 0 && keyFile != "" {
if keyFile != "" {
// Expand ~ in keyFile path
if strings.HasPrefix(keyFile, "~/") {
homeDir, err := os.UserHomeDir()
@ -145,21 +145,19 @@ func (rb *RouterBackup) Connect() error {
}
key, err := ioutil.ReadFile(keyFile)
if err != nil {
return fmt.Errorf("unable to read private key: %v", err)
if err == nil {
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
fmt.Errorf("unable to parse private key: %v", err)
} else {
config.Auth = append(config.Auth, ssh.PublicKeys(signer))
}
}
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
return fmt.Errorf("unable to parse private key: %v", err)
}
config.Auth = []ssh.AuthMethod{ssh.PublicKeys(signer)}
}
// Fall back to password if available
if len(config.Auth) == 0 && rb.password != "" {
config.Auth = []ssh.AuthMethod{ssh.Password(rb.password)}
if rb.password != "" {
config.Auth = append(config.Auth, ssh.Password(rb.password))
}
if len(config.Auth) == 0 {