ssh: add test case against ssh CLI
These tests try to ensure better compatibility of our server implementation with the ssh CLI. With these tests in place: 1) before merging CL 447757 we would have noticed that our server implementation was broken with OpenSSH 8.8+ 2) after merging CL 447757 we would have noticed that our server implementation was broken with OpenSSH 7.2-7.7 The ssh CLI from $PATH is used by default, but can be overridden using the SSH_CLI_PATH environment variable. Change-Id: I93d64be41c7613132b0364afac8397f57c2dcbca Reviewed-on: https://go-review.googlesource.com/c/crypto/+/506837 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Benny Siegert <bsiegert@gmail.com> Reviewed-by: Han-Wen Nienhuys <hanwen@google.com> Run-TryBot: Nicola Murino <nicola.murino@gmail.com>
This commit is contained in:
committed by
Benny Siegert
parent
4f30245552
commit
cf8dcb0f7d
@@ -0,0 +1,98 @@
|
||||
// Copyright 2023 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package test
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
type exitStatusMsg struct {
|
||||
Status uint32
|
||||
}
|
||||
|
||||
// goServer is a test Go SSH server that accepts public key and certificate
|
||||
// authentication and replies with a 0 exit status to any exec request without
|
||||
// running any commands.
|
||||
type goTestServer struct {
|
||||
listener net.Listener
|
||||
config *ssh.ServerConfig
|
||||
done <-chan struct{}
|
||||
}
|
||||
|
||||
func newTestServer(config *ssh.ServerConfig) (*goTestServer, error) {
|
||||
server := &goTestServer{
|
||||
config: config,
|
||||
}
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
server.listener = listener
|
||||
done := make(chan struct{}, 1)
|
||||
server.done = done
|
||||
go server.acceptConnections(done)
|
||||
|
||||
return server, nil
|
||||
}
|
||||
|
||||
func (s *goTestServer) port() (string, error) {
|
||||
_, port, err := net.SplitHostPort(s.listener.Addr().String())
|
||||
return port, err
|
||||
}
|
||||
|
||||
func (s *goTestServer) acceptConnections(done chan<- struct{}) {
|
||||
defer close(done)
|
||||
|
||||
for {
|
||||
c, err := s.listener.Accept()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, chans, reqs, err := ssh.NewServerConn(c, s.config)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
go ssh.DiscardRequests(reqs)
|
||||
defer c.Close()
|
||||
|
||||
for newChannel := range chans {
|
||||
if newChannel.ChannelType() != "session" {
|
||||
newChannel.Reject(ssh.UnknownChannelType, "unknown channel type")
|
||||
continue
|
||||
}
|
||||
|
||||
channel, requests, err := newChannel.Accept()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
go func(in <-chan *ssh.Request) {
|
||||
for req := range in {
|
||||
ok := false
|
||||
switch req.Type {
|
||||
case "exec":
|
||||
ok = true
|
||||
go func() {
|
||||
channel.SendRequest("exit-status", false, ssh.Marshal(&exitStatusMsg{Status: 0}))
|
||||
channel.Close()
|
||||
}()
|
||||
}
|
||||
if req.WantReply {
|
||||
req.Reply(ok, nil)
|
||||
}
|
||||
}
|
||||
}(requests)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *goTestServer) Close() error {
|
||||
err := s.listener.Close()
|
||||
// wait for the accept loop to exit
|
||||
<-s.done
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user