(t *testing.T, hostKey ssh.Signer, authorizedKeys []ssh.PublicKey)
| 277 | } |
| 278 | |
| 279 | func startSSHServer(t *testing.T, hostKey ssh.Signer, authorizedKeys []ssh.PublicKey) net.Listener { |
| 280 | t.Helper() |
| 281 | |
| 282 | marshaledAuthorizedKeys := make([][]byte, len(authorizedKeys)) |
| 283 | for i, authorizedKey := range authorizedKeys { |
| 284 | marshaledAuthorizedKeys[i] = authorizedKey.Marshal() |
| 285 | } |
| 286 | |
| 287 | config := &ssh.ServerConfig{ |
| 288 | PublicKeyCallback: func(c ssh.ConnMetadata, pubKey ssh.PublicKey) (*ssh.Permissions, error) { |
| 289 | marshaledPubKey := pubKey.Marshal() |
| 290 | for _, marshaledAuthorizedKey := range marshaledAuthorizedKeys { |
| 291 | if bytes.Equal(marshaledPubKey, marshaledAuthorizedKey) { |
| 292 | return &ssh.Permissions{ |
| 293 | // Record the public key used for authentication. |
| 294 | Extensions: map[string]string{ |
| 295 | "pubkey-fp": ssh.FingerprintSHA256(pubKey), |
| 296 | }, |
| 297 | }, nil |
| 298 | } |
| 299 | } |
| 300 | t.Logf("unknown public key for %q:\n\t%+v\n\t%+v\n", c.User(), pubKey.Marshal(), authorizedKeys) |
| 301 | return nil, fmt.Errorf("unknown public key for %q", c.User()) |
| 302 | }, |
| 303 | } |
| 304 | config.AddHostKey(hostKey) |
| 305 | |
| 306 | listener, err := net.Listen("tcp", "localhost:0") |
| 307 | if err != nil { |
| 308 | t.Fatalf("Failed to listen for connection: %v", err) |
| 309 | } |
| 310 | |
| 311 | go func() { |
| 312 | nConn, err := listener.Accept() |
| 313 | if err != nil { |
| 314 | if strings.Contains(err.Error(), "use of closed network connection") { |
| 315 | return |
| 316 | } |
| 317 | t.Logf("Failed to accept incoming connection: %v", err) |
| 318 | return |
| 319 | } |
| 320 | defer nConn.Close() |
| 321 | |
| 322 | conn, chans, reqs, err := ssh.NewServerConn(nConn, config) |
| 323 | if err != nil { |
| 324 | t.Logf("failed to handshake: %+v, %+v", conn, err) |
| 325 | return |
| 326 | } |
| 327 | |
| 328 | // The incoming Request channel must be serviced. |
| 329 | go func() { |
| 330 | for newRequest := range reqs { |
| 331 | t.Logf("new request %v", newRequest) |
| 332 | } |
| 333 | }() |
| 334 | |
| 335 | // Service only the first channel request |
| 336 | newChannel := <-chans |
no test coverage detected
searching dependent graphs…