Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ssh/knownhosts: knownhost to support io.reader #269

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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(r io.Reader) (ssh.HostKeyCallback, error) {
db := newHostKeyDB()
if err := db.Read(r, ""); 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)
}

}