mirror of https://github.com/fluxcd/flux2.git
internal: add simple keyscanner
This is an initial implementation and not a replacement candidate for ssh-keyscan since it does only scan the key of the algorithm the client and server agreed upon. This agreement may change depending on the key being used, making it useless for distributed usages.pull/20/head
parent
4c7f133315
commit
c5491b9da8
@ -0,0 +1,40 @@
|
||||
package keyscan
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"golang.org/x/crypto/ssh"
|
||||
"golang.org/x/crypto/ssh/knownhosts"
|
||||
)
|
||||
|
||||
func ScanKeys(host string) ([]byte, error) {
|
||||
col := &collector{}
|
||||
config := &ssh.ClientConfig{
|
||||
User: "git",
|
||||
HostKeyCallback: col.StoreKey(),
|
||||
}
|
||||
client, err := ssh.Dial("tcp", host, config)
|
||||
if err == nil {
|
||||
defer client.Close()
|
||||
}
|
||||
if len(col.knownKeys) > 0 {
|
||||
return col.knownKeys, nil
|
||||
}
|
||||
return col.knownKeys, err
|
||||
}
|
||||
|
||||
type collector struct {
|
||||
knownKeys []byte
|
||||
}
|
||||
|
||||
func (c *collector) StoreKey() ssh.HostKeyCallback {
|
||||
return func(hostname string, remote net.Addr, key ssh.PublicKey) error {
|
||||
c.knownKeys = append(
|
||||
c.knownKeys,
|
||||
fmt.Sprintf("%s %s %s\n", knownhosts.Normalize(hostname), key.Type(), base64.StdEncoding.EncodeToString(key.Marshal()))...,
|
||||
)
|
||||
return nil
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue