-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e385c9b
Prevent blocking forever when transport channel fails to open
dboslee 091d255
Add license to conn_test.go
dboslee 11d2c54
Merge branch 'master' into david/fix-ssh-reject
dboslee cbcdbc5
Fix tests use assert over require in goroutines`
dboslee a59074c
Remove use of assert and require in goroutines
dboslee de9f064
Merge branch 'master' into david/fix-ssh-reject
dboslee 7e5a8fe
Merge branch 'master' into david/fix-ssh-reject
dboslee 67a2a1e
Merge branch 'master' into david/fix-ssh-reject
dboslee 1d11435
Close connections in main goroutine to prevent flaky test
dboslee d230416
Merge branch 'master' into david/fix-ssh-reject
dboslee 6c6ad59
Merge branch 'master' into david/fix-ssh-reject
dboslee 6736276
Merge branch 'master' into david/fix-ssh-reject
dboslee 732a2b3
Merge branch 'master' into david/fix-ssh-reject
dboslee 3a97e02
Merge branch 'master' into david/fix-ssh-reject
dboslee dc53ae3
Merge branch 'master' into david/fix-ssh-reject
dboslee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
/* | ||
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" | ||
"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) | ||
|
||
cSigner ssh.Signer | ||
hSigner ssh.Signer | ||
} | ||
|
||
func (s *server) Run(errC chan error) { | ||
for { | ||
conn, err := s.listener.Accept() | ||
if err != nil { | ||
errC <- err | ||
return | ||
} | ||
|
||
go func() { | ||
sconn, _, _, err := ssh.NewServerConn(conn, s.config) | ||
if err != nil { | ||
errC <- err | ||
return | ||
} | ||
s.handler(sconn) | ||
}() | ||
} | ||
} | ||
|
||
func (s *server) Stop() error { | ||
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 | ||
} | ||
|
||
func (s *server) GetClient(t *testing.T) (ssh.Conn, <-chan ssh.NewChannel, <-chan *ssh.Request) { | ||
conn, err := net.Dial("tcp", s.listener.Addr().String()) | ||
require.NoError(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(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, | ||
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) { | ||
handlerErrC := make(chan error, 1) | ||
serverErrC := make(chan error, 1) | ||
|
||
server := newServer(t, func(sconn *ssh.ServerConn) { | ||
_, _, err := ConnectProxyTransport(sconn, &DialReq{ | ||
Address: "test", ServerID: "test", | ||
}, false) | ||
handlerErrC <- err | ||
}) | ||
|
||
go server.Run(serverErrC) | ||
t.Cleanup(func() { require.NoError(t, server.Stop()) }) | ||
|
||
sconn1, nc, _ := server.GetClient(t) | ||
t.Cleanup(func() { require.Error(t, sconn1.Close()) }) | ||
|
||
channel := <-nc | ||
require.Equal(t, channel.ChannelType(), constants.ChanTransport) | ||
|
||
sconn1.Close() | ||
err := timeoutErrC(t, handlerErrC, time.Second*5) | ||
require.Error(t, err) | ||
|
||
sconn2, nc, _ := server.GetClient(t) | ||
t.Cleanup(func() { require.NoError(t, sconn2.Close()) }) | ||
|
||
channel = <-nc | ||
require.Equal(t, channel.ChannelType(), constants.ChanTransport) | ||
|
||
err = channel.Reject(ssh.ConnectionFailed, "test reject") | ||
require.NoError(t, err) | ||
|
||
err = timeoutErrC(t, handlerErrC, time.Second*5) | ||
require.Error(t, err) | ||
|
||
select { | ||
case err = <-serverErrC: | ||
require.FailNow(t, err.Error()) | ||
default: | ||
} | ||
} | ||
|
||
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 | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we import any thing from
/lib
into/api
so leaving this as is.