File tree 2 files changed +32
-0
lines changed
2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -315,6 +315,18 @@ func (srv *Server) ListenAndServe() error {
315
315
func (srv * Server ) AddHostKey (key Signer ) {
316
316
// these are later added via AddHostKey on ServerConfig, which performs the
317
317
// check for one of every algorithm.
318
+
319
+ // This check is based on the AddHostKey method from the x/crypto/ssh
320
+ // library. This allows us to only keep one active key for each type on a
321
+ // server at once. So, if you're dynamically updating keys at runtime, this
322
+ // list will not keep growing.
323
+ for i , k := range srv .HostSigners {
324
+ if k .PublicKey ().Type () == key .PublicKey ().Type () {
325
+ srv .HostSigners [i ] = key
326
+ return
327
+ }
328
+ }
329
+
318
330
srv .HostSigners = append (srv .HostSigners , key )
319
331
}
320
332
Original file line number Diff line number Diff line change @@ -8,6 +8,26 @@ import (
8
8
"time"
9
9
)
10
10
11
+ func TestAddHostKey (t * testing.T ) {
12
+ s := Server {}
13
+ signer , err := generateSigner ()
14
+ if err != nil {
15
+ t .Fatal (err )
16
+ }
17
+ s .AddHostKey (signer )
18
+ if len (s .HostSigners ) != 1 {
19
+ t .Fatal ("Key was not properly added" )
20
+ }
21
+ signer , err = generateSigner ()
22
+ if err != nil {
23
+ t .Fatal (err )
24
+ }
25
+ s .AddHostKey (signer )
26
+ if len (s .HostSigners ) != 1 {
27
+ t .Fatal ("Key was not properly replaced" )
28
+ }
29
+ }
30
+
11
31
func TestServerShutdown (t * testing.T ) {
12
32
l := newLocalListener ()
13
33
testBytes := []byte ("Hello world\n " )
You can’t perform that action at this time.
0 commit comments