-
Notifications
You must be signed in to change notification settings - Fork 3
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
Don't retry SSH connection when auth fails #818
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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 |
---|---|---|
|
@@ -6,6 +6,7 @@ import ( | |
"net" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
|
||
"github.com/go-logr/logr" | ||
|
@@ -17,32 +18,32 @@ import ( | |
// returns a client connection. | ||
// | ||
// Only private key authentication is currently supported, with or without a | ||
// passphrase. | ||
// passphrase.SSH: %v", | ||
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. Looks like this was accidentally changed! 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. Oops! 🤦 |
||
func sshConnect(ctx context.Context, logger logr.Logger, cfg Config) (*ssh.Client, error) { | ||
// Load private key for authentication. | ||
keyBytes, err := os.ReadFile(filepath.Clean(cfg.PrivateKey.Path)) // #nosec G304 -- File data is validated below | ||
if err != nil { | ||
return nil, fmt.Errorf("read private key: %v", err) | ||
return nil, NewAuthError(fmt.Errorf("ssh: read private key: %v", err)) | ||
} | ||
|
||
// Create a signer from the private key, with or without a passphrase. | ||
var signer ssh.Signer | ||
if cfg.PrivateKey.Passphrase != "" { | ||
signer, err = ssh.ParsePrivateKeyWithPassphrase(keyBytes, []byte(cfg.PrivateKey.Passphrase)) | ||
if err != nil { | ||
return nil, fmt.Errorf("parse private key with passphrase: %v", err) | ||
return nil, NewAuthError(fmt.Errorf("ssh: parse private key with passphrase: %v", err)) | ||
} | ||
} else { | ||
signer, err = ssh.ParsePrivateKey(keyBytes) | ||
if err != nil { | ||
return nil, fmt.Errorf("parse private key: %v", err) | ||
return nil, NewAuthError(fmt.Errorf("ssh: parse private key: %v", err)) | ||
} | ||
} | ||
|
||
// Check that the host key is in the client's known_hosts file. | ||
hostcallback, err := knownhosts.New(filepath.Clean(cfg.KnownHostsFile)) | ||
if err != nil { | ||
return nil, fmt.Errorf("parse known_hosts: %v", err) | ||
return nil, NewAuthError(fmt.Errorf("ssh: parse known_hosts: %v", err)) | ||
} | ||
|
||
// Configure the SSH client. | ||
|
@@ -61,14 +62,18 @@ func sshConnect(ctx context.Context, logger logr.Logger, cfg Config) (*ssh.Clien | |
dialer := &net.Dialer{} | ||
conn, err := dialer.DialContext(ctx, "tcp", address) | ||
if err != nil { | ||
logger.V(2).Info("SSH dial failed", "address", address, "user", cfg.User) | ||
return nil, fmt.Errorf("connect: %v", err) | ||
logger.V(2).Info("ssh: dial failed", "address", address, "user", cfg.User) | ||
return nil, fmt.Errorf("ssh: connect: %v", err) | ||
} | ||
|
||
sshConn, chans, reqs, err := ssh.NewClientConn(conn, address, sshConfig) | ||
if err != nil { | ||
logger.V(2).Info("SSH dial failed", "address", address, "user", cfg.User) | ||
return nil, fmt.Errorf("connect: %v", err) | ||
if strings.Contains(err.Error(), "ssh: unable to authenticate") || strings.Contains(err.Error(), "knownhosts: key is unknown") { | ||
logger.V(2).Info("ssh: authentication failed", "address", address, "user", cfg.User) | ||
return nil, NewAuthError(err) | ||
} | ||
logger.V(2).Info("ssh: failed to connect", "address", address, "user", cfg.User) | ||
return nil, err | ||
} | ||
|
||
return ssh.NewClient(sshConn, chans, reqs), 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.
A possible alternative:
This version:
I think that we'd want the function to return a concrete type (*AuthError) if we were doing some performance-focused work, e.g. https://github.com/connectrpc/connect-go/blob/d88758dc0e89170db922ecd20f16cec57662ec23/error.go#L117-L128 could be a good example where the design was driven by performance objectives, like avoiding memory allocations. This is a guess though, not entirely sure.
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.
@sevein does
Reason
need to be exported?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.
Exporting the
Reason
field does have the nice benefit of being able to set an arbitrary string without having to create an error first, e.g.vs.