Skip to content

Add connectivity probe to WinRM Connect - #342

Merged
kke merged 4 commits into
mainfrom
winrm-connect-probe
May 8, 2026
Merged

kke merged 4 commits into
mainfrom
winrm-connect-probe

Conversation

@kke

@kke kke commented May 8, 2026

Copy link
Copy Markdown
Contributor

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 run cmd.exe /c exit 0 and classify WinRM HTTP 401/403 as protocol.ErrNonRetryable.
  • Updated IsConnected() to use probe() instead of duplicating the no-op command logic.
  • Updated Connect() to run the probe after client creation and clear c.client on 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.

Comment thread protocol/winrm/connection.go Resolved
Comment thread protocol/winrm/connection_test.go Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment thread protocol/winrm/connection.go Resolved
@kke
kke force-pushed the winrm-connect-probe branch from c5140e0 to f4da305 Compare May 8, 2026 13:32
@kke
kke requested a review from Copilot May 8, 2026 13:32
@kke
kke marked this pull request as ready for review May 8, 2026 13:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment thread protocol/winrm/connection.go Resolved
Comment thread protocol/winrm/connection.go Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
	}

Comment thread protocol/winrm/connection_test.go Resolved
@kke
kke force-pushed the winrm-connect-probe branch from 94a5e0d to 2dc40c3 Compare May 8, 2026 13:54
@kke
kke requested a review from Copilot May 8, 2026 13:54

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@kke
kke force-pushed the winrm-connect-probe branch from 2dc40c3 to 72fd540 Compare May 8, 2026 14:01
@kke
kke requested a review from Copilot May 8, 2026 14:01

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment thread protocol/winrm/connection.go Resolved
Comment thread protocol/winrm/connection_test.go Resolved
Signed-off-by: Kimmo Lehto <klehto@mirantis.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

Comment thread protocol/winrm/connection.go Resolved
Comment thread protocol/winrm/connection.go Resolved
Comment thread protocol/winrm/connection.go Resolved
Signed-off-by: Kimmo Lehto <klehto@mirantis.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment thread protocol/winrm/connection.go Resolved
Signed-off-by: Kimmo Lehto <klehto@mirantis.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@kke
kke merged commit 3f37139 into main May 8, 2026
15 checks passed
@kke
kke deleted the winrm-connect-probe branch May 8, 2026 21:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants