Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions ssh/knownhosts/reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package knownhosts

import (
"io"

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

// NewFromReader creates a host key callback from the given OpenSSH host io.Reader
// which is in SSH_KNOWN_HOSTS_FILE_FORMAT. The returned callback is for use in
// ssh.ClientConfig.HostKeyCallback. By preference, the key check
// operates on the hostname if available, i.e. if a server changes its
// IP address, the host key check will still succeed, even though a
// record of the new IP address is not available.
func NewFromReader(readers ...io.Reader) (ssh.HostKeyCallback, error) {
db := newHostKeyDB()
if err := db.Read(io.MultiReader(readers...), ""); err != nil {
return nil, err
}

var certChecker ssh.CertChecker
certChecker.IsHostAuthority = db.IsHostAuthority
certChecker.IsRevoked = db.IsRevoked
certChecker.HostKeyFallback = db.check

return certChecker.CheckHostKey, nil
}
17 changes: 17 additions & 0 deletions ssh/knownhosts/reader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package knownhosts

import (
"fmt"
"strings"
"testing"
)

func TestNewFromReader(t *testing.T) {
str := fmt.Sprintf("server*.domain %s", edKeyStr)

_, err := NewFromReader(strings.NewReader(str))
if err != nil {
t.Fatalf("cannot read from string: %v", err)
}

}