Skip to content

Commit

Permalink
managerd: Enchancing host healthcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
fihuer committed Jul 3, 2018
1 parent 86e36b6 commit f9f001f
Show file tree
Hide file tree
Showing 292 changed files with 71,662 additions and 0 deletions.
28 changes: 28 additions & 0 deletions route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ package route
import (
"math/rand"
"net"
"regexp"
"time"

"github.com/op/go-logging"

"golang.org/x/crypto/ssh"
)

var log = logging.MustGetLogger("sshproxy/route")
Expand Down Expand Up @@ -61,6 +64,31 @@ func CanConnect(hostport string) bool {
return true
}

// MightAuthenticate tests if a connection to host:port can initiate an handshake.
func MightAuthenticate(hostport string, user string) bool {
ssh_config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.Password("ThisIsNotIntendedToBeAValidPasswordButWeDontReallyCare"),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
log.Debug("Dialing %s with user %s", hostport, user)
client, err := ssh.Dial("tcp", hostport, ssh_config)
if err != nil {
re_auth_failure := regexp.MustCompile(`handshake failed: ssh: unable to authenticate`).MatchString(err.Error())
if re_auth_failure {
return true
} else {
log.Warning("Error while dialing %v", err)
return false
}
} else {
client.Close()
}
return true
}

// selectDestinationOrdered selects the first reachable destination from a list
// of destinations. It returns a string "host:port", an empty string (if no
// destination is found) or an error.
Expand Down
36 changes: 36 additions & 0 deletions sshproxy-managerd/sshproxy-managerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"flag"
"fmt"
"io/ioutil"
"math/rand"
"net"
"os"
"regexp"
Expand Down Expand Up @@ -165,10 +166,43 @@ func (hc *hostChecker) DoCheck(hostport string) State {
if route.CanConnect(hostport) {
state = Up
}
canary_user, err := PickUser()
if err == nil && canary_user != "" && state == Up {
if route.MightAuthenticate(hostport, canary_user) {
log.Debug("Succefully tried to authenticate to %s as %s", hostport, canary_user)
} else {
log.Debug("Unable to try to authenticate against %s as %s", hostport, canary_user)
state = Down
}
} else if err != nil {
log.Debugf("Unable to try to authenticate, found no user to spoof (%s)", err)
}
hc.Update(hostport, state, time.Now())
return state
}

func PickUser() (string, error) {
chosen_user := ""
if len(proxiedConnections) > 0 {
chosen_item := rand.Intn(len(proxiedConnections))
for k := range proxiedConnections {
if chosen_item == 0 {
user, err := getUserFromKey(k)
if err != nil {
return "", err
} else {
chosen_user = user
break
}
}
chosen_item--
}
} else {
return "", errors.New("No proxied connections, unable to pick a random user...")
}
return chosen_user, nil
}

// Update updates (or creates) the state of an host in the internal view.
func (hc *hostChecker) Update(hostport string, state State, ts time.Time) {
if s, ok := hc.States[hostport]; ok {
Expand Down Expand Up @@ -631,6 +665,8 @@ func main() {
}
defer l.Close()

rand.Seed(time.Now().Unix()) // initialize global pseudo random generator

log.Info("listening on %s\n", config.Listen)

queue := make(chan *request)
Expand Down
10 changes: 10 additions & 0 deletions vendor/golang.org/x/crypto/.gitattributes

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions vendor/golang.org/x/crypto/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/golang.org/x/crypto/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions vendor/golang.org/x/crypto/CONTRIBUTING.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/golang.org/x/crypto/CONTRIBUTORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions vendor/golang.org/x/crypto/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/golang.org/x/crypto/PATENTS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/golang.org/x/crypto/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f9f001f

Please sign in to comment.