Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
Cherry-pick: use ssh discover technique to check if SSH service is re…
Browse files Browse the repository at this point in the history
…ally run… (#469)

use ssh discover technique to check if SSH service is really running
  • Loading branch information
stealthybox committed Oct 8, 2019
1 parent 70bc01f commit ac4908b
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions cmd/ignite/run/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package run
import (
"fmt"
"net"
"strings"
"time"

"github.com/weaveworks/ignite/pkg/apis/ignite"

"github.com/weaveworks/ignite/pkg/operations"
"golang.org/x/crypto/ssh"
)

type StartFlags struct {
Expand Down Expand Up @@ -53,7 +54,7 @@ func Start(so *startOptions) error {
return nil
}

func waitForSSH(vm *ignite.VM, seconds int) error {
func dialSuccess(vm *ignite.VM, seconds int) error {
// When --ssh is enabled, wait until SSH service started on port 22 at most N seconds
ssh := vm.Spec.SSH
if ssh != nil && ssh.Generate && len(vm.Status.IPAddresses) > 0 {
Expand Down Expand Up @@ -81,3 +82,44 @@ func waitForSSH(vm *ignite.VM, seconds int) error {

return nil
}

func waitForSSH(vm *ignite.VM, seconds int) error {
if err := dialSuccess(vm, seconds); err != nil {
return err
}

certCheck := &ssh.CertChecker{
IsHostAuthority: func(auth ssh.PublicKey, address string) bool {
return true
},
IsRevoked: func(cert *ssh.Certificate) bool {
return false
},
HostKeyFallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
},
}

config := &ssh.ClientConfig{
User: "user",
Auth: []ssh.AuthMethod{
ssh.Password("password"),
},
HostKeyCallback: certCheck.CheckHostKey,
Timeout: 5 * time.Second,
}

addr := vm.Status.IPAddresses[0].String() + ":22"
sshConn, err := ssh.Dial("tcp", addr, config)
if err != nil {
// If error contains "unable to authenticate", it seems able to connect the server
errString := err.Error()
if strings.Contains(errString, "unable to authenticate") {
return nil
}
return err
}

sshConn.Close()
return fmt.Errorf("timed out checking SSH server")
}

0 comments on commit ac4908b

Please sign in to comment.