ssh: validate the ssh-key parameter if given

pull/10293/head
Anders F Björklund 2021-01-28 07:50:42 +01:00
parent c2d824911b
commit 40e4521bb2
1 changed files with 13 additions and 0 deletions

View File

@ -18,6 +18,7 @@ package ssh
import (
"fmt"
"io/ioutil"
"net"
"os"
"os/exec"
@ -25,6 +26,8 @@ import (
"strconv"
"time"
"golang.org/x/crypto/ssh"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/engine"
"github.com/docker/machine/libmachine/log"
@ -102,6 +105,16 @@ func (d *Driver) PreCreateCheck() error {
if _, err := os.Stat(d.SSHKey); os.IsNotExist(err) {
return fmt.Errorf("SSH key does not exist: %q", d.SSHKey)
}
key, err := ioutil.ReadFile(d.SSHKey)
if err != nil {
return err
}
_, err = ssh.ParsePrivateKey(key)
if err != nil {
return errors.Wrapf(err, "SSH key does not parse: %q", d.SSHKey)
}
}
return nil