Repository navigation
Add connectivity probe to WinRM Connect - #342
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
Restores early WinRM connectivity/authentication validation by running a no-op command immediately after establishing a WinRM client, and reuses the same probe logic for IsConnected().
Changes:
- Added a shared
probe(ctx)helper to runcmd.exe /c exit 0and classify WinRM HTTP 401/403 asprotocol.ErrNonRetryable. - Updated
IsConnected()to useprobe()instead of duplicating the no-op command logic. - Updated
Connect()to run the probe after client creation and clearc.clienton probe failure; added unit tests for auth-error string classification.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
protocol/winrm/connection.go |
Adds probe() and isAuthError(), uses probe in Connect() and IsConnected() to fail early on unreachable/unauthenticated endpoints. |
protocol/winrm/connection_test.go |
Adds tests for auth error detection and (intended) non-retryable classification behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (1)
protocol/winrm/connection.go:235
- In Connect(), the bastion SSH connection is stored on the Connection (c.bastionConn = bastionSSH) immediately after bastionSSH.Connect(). If winrm.NewClientWithParameters() fails (or any subsequent step returns before Disconnect is called), the bastion connection will remain open and c.bastionConn will remain set even though Connect() failed. Consider keeping bastionSSH as a local variable and only assigning c.bastionConn after the WinRM client + probe succeed, and ensure bastionSSH.Disconnect() is called on all error paths (e.g., via a defer that runs unless Connect succeeds).
if c.Bastion != nil {
bastionSSH, dialer, err := c.bastionDialer(ctx)
if err != nil {
return err
}
params.Dial = dialer
c.mu.Lock()
c.bastionConn = bastionSSH
c.mu.Unlock()
}
if c.UseNTLM {
params.TransportDecorator = func() winrm.Transporter { return &winrm.ClientNTLM{} }
}
if c.UseHTTPS && len(c.cert) > 0 {
params.TransportDecorator = func() winrm.Transporter { return &winrm.ClientAuthRequest{} }
}
client, err := winrm.NewClientWithParameters(endpoint, c.User, c.Password, params)
if err != nil {
return fmt.Errorf("create winrm client: %w", err)
}
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
protocol/winrm/connection.go:235
- When a bastion is configured, Connect() establishes and stores an SSH bastion connection before creating the WinRM client. If winrm.NewClientWithParameters(...) returns an error, the function returns without cleaning up the already-open bastion SSH connection, which can leak resources and leave c.bastionConn set. Consider deferring cleanup (e.g., disconnecting the bastion/clearing c.bastionConn) unless Connect completes successfully, or call c.Disconnect() on this error path as well.
if c.Bastion != nil {
bastionSSH, dialer, err := c.bastionDialer(ctx)
if err != nil {
return err
}
params.Dial = dialer
c.mu.Lock()
c.bastionConn = bastionSSH
c.mu.Unlock()
}
if c.UseNTLM {
params.TransportDecorator = func() winrm.Transporter { return &winrm.ClientNTLM{} }
}
if c.UseHTTPS && len(c.cert) > 0 {
params.TransportDecorator = func() winrm.Transporter { return &winrm.ClientAuthRequest{} }
}
client, err := winrm.NewClientWithParameters(endpoint, c.User, c.Password, params)
if err != nil {
return fmt.Errorf("create winrm client: %w", err)
}
v0.x ran a no-op command during Connect to verify credentials and reachability; v2 only constructed the HTTP client, so bad credentials or unreachable endpoints did not fail until the first real command. Restore that behaviour: after creating the winrm.Client, run `cmd.exe /c exit 0` as a probe using the caller's context. HTTP 401/403 responses are classified as ErrNonRetryable so retry loops don't hammer with bad credentials. On probe failure c.client is cleared so IsConnected() remains consistent. Extract probe() as a shared helper used by both Connect and IsConnected, replacing the inline StartProcess call in IsConnected. Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
v0.x ran a no-op command during Connect to verify credentials and reachability; v2 only constructed the HTTP client, so bad credentials or unreachable endpoints did not fail until the first real command.
Restore that behaviour: after creating the winrm.Client, run
cmd.exe /c exit 0as a probe using the caller's context. HTTP 401/403 responses are classified as ErrNonRetryable so retry loops don't hammer with bad credentials. On probe failure c.client is cleared so IsConnected() remains consistent.Extract probe() as a shared helper used by both Connect and IsConnected, replacing the inline StartProcess call in IsConnected.