-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prevent blocking forever when transport channel fails to open #11875
Changes from 3 commits
e385c9b
091d255
11d2c54
cbcdbc5
a59074c
de9f064
7e5a8fe
67a2a1e
1d11435
d230416
6c6ad59
6736276
732a2b3
3a97e02
dc53ae3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,172 @@ | ||||||
/* | ||||||
Copyright 2022 Gravitational, Inc. | ||||||
|
||||||
Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
you may not use this file except in compliance with the License. | ||||||
You may obtain a copy of the License at | ||||||
|
||||||
http://www.apache.org/licenses/LICENSE-2.0 | ||||||
|
||||||
Unless required by applicable law or agreed to in writing, software | ||||||
distributed under the License is distributed on an "AS IS" BASIS, | ||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
See the License for the specific language governing permissions and | ||||||
limitations under the License. | ||||||
*/ | ||||||
|
||||||
package sshutils | ||||||
|
||||||
import ( | ||||||
"crypto/rand" | ||||||
"crypto/rsa" | ||||||
"crypto/x509" | ||||||
"encoding/pem" | ||||||
"net" | ||||||
"sync" | ||||||
"testing" | ||||||
"time" | ||||||
|
||||||
"github.com/gravitational/teleport/api/constants" | ||||||
"github.com/stretchr/testify/require" | ||||||
"golang.org/x/crypto/ssh" | ||||||
) | ||||||
|
||||||
type server struct { | ||||||
listener net.Listener | ||||||
config *ssh.ServerConfig | ||||||
handler func(*ssh.ServerConn) | ||||||
t *testing.T | ||||||
mu sync.RWMutex | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently it's hard to say what this mutex is protecting. From what I see it's used for |
||||||
closed bool | ||||||
|
||||||
cSigner ssh.Signer | ||||||
hSigner ssh.Signer | ||||||
} | ||||||
|
||||||
func (s *server) Run() { | ||||||
for { | ||||||
conn, err := s.listener.Accept() | ||||||
|
||||||
s.mu.RLock() | ||||||
if s.closed { | ||||||
s.mu.RUnlock() | ||||||
return | ||||||
} | ||||||
s.mu.RUnlock() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really need the teleport/lib/srv/db/postgres/test.go Line 116 in 06fef2a
|
||||||
|
||||||
require.NoError(s.t, err) | ||||||
|
||||||
go func() { | ||||||
defer conn.Close() | ||||||
sconn, _, _, err := ssh.NewServerConn(conn, s.config) | ||||||
require.NoError(s.t, err) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whoops didn't realize this. Updated a59074c |
||||||
s.handler(sconn) | ||||||
}() | ||||||
} | ||||||
} | ||||||
|
||||||
func (s *server) Stop() error { | ||||||
s.mu.Lock() | ||||||
defer s.mu.Unlock() | ||||||
s.closed = true | ||||||
return s.listener.Close() | ||||||
} | ||||||
|
||||||
func generateSigner(t *testing.T) ssh.Signer { | ||||||
private, err := rsa.GenerateKey(rand.Reader, 2048) | ||||||
require.NoError(t, err) | ||||||
|
||||||
block := &pem.Block{ | ||||||
Type: "RSA PRIVATE KEY", | ||||||
Bytes: x509.MarshalPKCS1PrivateKey(private), | ||||||
} | ||||||
|
||||||
privatePEM := pem.EncodeToMemory(block) | ||||||
signer, err := ssh.ParsePrivateKey(privatePEM) | ||||||
require.NoError(t, err) | ||||||
|
||||||
return signer | ||||||
} | ||||||
Comment on lines
+65
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have a helper for that already teleport/lib/auth/native/native.go Line 152 in bb121d7
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we import any thing from |
||||||
|
||||||
func (s *server) GetClient() (ssh.Conn, <-chan ssh.NewChannel, <-chan *ssh.Request) { | ||||||
conn, err := net.Dial("tcp", s.listener.Addr().String()) | ||||||
require.NoError(s.t, err) | ||||||
|
||||||
sconn, nc, r, err := ssh.NewClientConn(conn, "", &ssh.ClientConfig{ | ||||||
Auth: []ssh.AuthMethod{ssh.PublicKeys(s.cSigner)}, | ||||||
HostKeyCallback: ssh.FixedHostKey(s.hSigner.PublicKey()), | ||||||
}) | ||||||
require.NoError(s.t, err) | ||||||
|
||||||
return sconn, nc, r | ||||||
} | ||||||
|
||||||
func newServer(t *testing.T, handler func(*ssh.ServerConn)) *server { | ||||||
listener, err := net.Listen("tcp", "localhost:0") | ||||||
require.NoError(t, err) | ||||||
|
||||||
cSigner := generateSigner(t) | ||||||
hSigner := generateSigner(t) | ||||||
|
||||||
config := &ssh.ServerConfig{ | ||||||
NoClientAuth: true, | ||||||
} | ||||||
config.AddHostKey(hSigner) | ||||||
|
||||||
return &server{ | ||||||
listener: listener, | ||||||
config: config, | ||||||
handler: handler, | ||||||
t: t, | ||||||
cSigner: cSigner, | ||||||
hSigner: hSigner, | ||||||
} | ||||||
} | ||||||
|
||||||
// TestTransportError ensures ConnectProxyTransport does not block forever | ||||||
// when an error occurs while opening the transport channel. | ||||||
func TestTransportError(t *testing.T) { | ||||||
errC := make(chan error) | ||||||
|
||||||
server := newServer(t, func(sconn *ssh.ServerConn) { | ||||||
_, _, err := ConnectProxyTransport(sconn, &DialReq{ | ||||||
Address: "test", ServerID: "test", | ||||||
}, false) | ||||||
errC <- err | ||||||
}) | ||||||
|
||||||
go server.Run() | ||||||
defer server.Stop() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
sconn, nc, _ := server.GetClient() | ||||||
defer sconn.Close() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
channel := <-nc | ||||||
require.Equal(t, channel.ChannelType(), constants.ChanTransport) | ||||||
|
||||||
sconn.Close() | ||||||
err := timeoutErrC(t, errC, time.Second*5) | ||||||
require.Error(t, err) | ||||||
|
||||||
sconn, nc, _ = server.GetClient() | ||||||
defer sconn.Close() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
channel = <-nc | ||||||
require.Equal(t, channel.ChannelType(), constants.ChanTransport) | ||||||
|
||||||
err = channel.Reject(ssh.ConnectionFailed, "test reject") | ||||||
require.NoError(t, err) | ||||||
|
||||||
err = timeoutErrC(t, errC, time.Second*5) | ||||||
require.Error(t, err) | ||||||
} | ||||||
|
||||||
func timeoutErrC(t *testing.T, errC <-chan error, d time.Duration) error { | ||||||
timeout := time.NewTimer(d) | ||||||
select { | ||||||
case err := <-errC: | ||||||
return err | ||||||
case <-timeout.C: | ||||||
require.FailNow(t, "failed to receive on err channel in time") | ||||||
} | ||||||
|
||||||
return nil | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: maybe this is my personal preference but I'd remove
t
from here and just pass it as an argument to each function that needs it. I think it's easier to understand the code if you know which function needst
, but if you want to keep it here I don't mind either.