-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsshkeys_test.go
97 lines (85 loc) · 2.5 KB
/
sshkeys_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package sshkeys_test
import (
"context"
"crypto/elliptic"
"errors"
"log"
"net"
"strconv"
"testing"
"time"
"github.com/Eun/sshkeys"
"github.com/gliderlabs/ssh"
"github.com/stretchr/testify/require"
xssh "golang.org/x/crypto/ssh"
)
func TestGetVersion(t *testing.T) {
t.Parallel()
l, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
defer l.Close()
expectedVersion := strconv.FormatInt(time.Now().Unix(), 36)
server := ssh.Server{
ServerConfigCallback: func(ctx ssh.Context) *xssh.ServerConfig {
return &xssh.ServerConfig{
Config: xssh.Config{},
NoClientAuth: false,
NoClientAuthCallback: nil,
MaxAuthTries: 0,
PasswordCallback: nil,
PublicKeyCallback: nil,
KeyboardInteractiveCallback: nil,
AuthLogCallback: nil,
ServerVersion: expectedVersion,
BannerCallback: nil,
GSSAPIWithMICConfig: nil,
}
},
}
defer server.Close()
go func() {
if sshServerErr := server.Serve(l); sshServerErr != nil {
if errors.Is(sshServerErr, ssh.ErrServerClosed) {
return
}
log.Fatal(sshServerErr)
}
}()
version, err := sshkeys.GetVersion(context.Background(), l.Addr().String())
require.NoError(t, err)
require.Equal(t, expectedVersion, version)
}
func TestGetKeys(t *testing.T) {
t.Parallel()
l, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
defer l.Close()
privateRSAKey, err := createRSAKey(2047)
require.NoError(t, err)
privateECKey, err := createECDSAKey(elliptic.P256())
require.NoError(t, err)
server := ssh.Server{
HostSigners: []ssh.Signer{privateRSAKey, privateECKey},
}
defer server.Close()
go func() {
if sshServerErr := server.Serve(l); sshServerErr != nil {
if errors.Is(sshServerErr, ssh.ErrServerClosed) {
return
}
log.Fatal(sshServerErr)
}
}()
keys, err := sshkeys.GetKeys(context.Background(), l.Addr().String(), 4, time.Minute, sshkeys.DefaultKeyAlgorithms()...)
require.NoError(t, err)
fingerprints := make(map[string]string)
for k, v := range keys {
fingerprints[k] = xssh.FingerprintSHA256(v)
}
require.Equal(t, map[string]string{
xssh.KeyAlgoRSA: xssh.FingerprintSHA256(privateRSAKey.PublicKey()),
xssh.KeyAlgoRSASHA256: xssh.FingerprintSHA256(privateRSAKey.PublicKey()),
xssh.KeyAlgoRSASHA512: xssh.FingerprintSHA256(privateRSAKey.PublicKey()),
xssh.KeyAlgoECDSA256: xssh.FingerprintSHA256(privateECKey.PublicKey()),
}, fingerprints)
}