MCPcopy
hub / github.com/kubernetes/node-problem-detector / runTestSSHServer

Function runTestSSHServer

test/e2e/lib/ssh/lib_test.go:45–122  ·  view source on GitHub ↗
(user, password string)

Source from the content-addressed store, hash-verified

43}
44
45func runTestSSHServer(user, password string) (*testSSHServer, error) {
46 result := &testSSHServer{}
47 // Largely derived from https://godoc.org/golang.org/x/crypto/ssh#example-NewServerConn
48 config := &ssh.ServerConfig{
49 PasswordCallback: func(c ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
50 if c.User() == user && string(pass) == password {
51 return nil, nil
52 }
53 return nil, fmt.Errorf("password rejected for %s", c.User())
54 },
55 PublicKeyCallback: func(c ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
56 result.Type = key.Type()
57 result.Data = ssh.MarshalAuthorizedKey(key)
58 return nil, nil
59 },
60 }
61
62 privateKey, publicKey, err := GenerateKey(2048)
63 if err != nil {
64 return nil, err
65 }
66 privateBytes := EncodePrivateKey(privateKey)
67 signer, err := ssh.ParsePrivateKey(privateBytes)
68 if err != nil {
69 return nil, err
70 }
71 config.AddHostKey(signer)
72 result.PrivateKey = privateBytes
73
74 publicBytes, err := EncodePublicKey(publicKey)
75 if err != nil {
76 return nil, err
77 }
78 result.PublicKey = publicBytes
79
80 listener, err := net.Listen("tcp", "127.0.0.1:0")
81 if err != nil {
82 return nil, err
83 }
84
85 host, port, err := net.SplitHostPort(listener.Addr().String())
86 if err != nil {
87 return nil, err
88 }
89 result.Host = host
90 result.Port = port
91 go func() {
92 // TODO: return this port.
93 defer listener.Close()
94
95 conn, err := listener.Accept()
96 if err != nil {
97 klog.Errorf("Failed to accept: %v", err)
98 }
99 _, chans, reqs, err := ssh.NewServerConn(conn, config)
100 if err != nil {
101 klog.Errorf("Failed handshake: %v", err)
102 }

Callers 1

TestSSHTunnelFunction · 0.85

Calls 6

GenerateKeyFunction · 0.85
EncodePrivateKeyFunction · 0.85
EncodePublicKeyFunction · 0.85
TypeMethod · 0.80
StringMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected