Skip to content

Correct pywinrm distribution name in HAS_WINRM detection (salt-cloud WinRM) - #69982

Open
fudianchn wants to merge 1 commit into
saltstack:masterfrom
fudianchn:fix-has-winrm-pywinrm-dist-name
Open

Correct pywinrm distribution name in HAS_WINRM detection (salt-cloud WinRM)#69982
fudianchn wants to merge 1 commit into
saltstack:masterfrom
fudianchn:fix-has-winrm-pywinrm-dist-name

Conversation

@fudianchn

Copy link
Copy Markdown

Problem

salt-cloud Windows deployments with use_winrm: true always fail with

[ERROR ] WinRM requested but module winrm could not be imported.
Ensure you are using version 0.3.0 or higher.

even when pywinrm (>= 0.3.0) is installed (#69976).

Root cause is in the HAS_WINRM detection block of salt/utils/cloud.py:

version = importlib.metadata.version("winrm")   # wrong distribution name

The PyPI package is distributed as pywinrm (imported as winrm). importlib.metadata.version("winrm") raises PackageNotFoundError, which is a subclass of ImportError, so the surrounding except ImportError: swallows it and HAS_WINRM is left False unconditionally.

Fix

Use the correct distribution name:

-    version = importlib.metadata.version("winrm")
+    version = importlib.metadata.version("pywinrm")

Notably the existing test_winrm_pinnned_version already queries version("pywinrm"), so the test suite was using the right name while the source wasn't.

Verification

Reproduced the detection logic directly (Python 3.12, pywinrm 0.5.0 installed):

lookup 'winrm'  -> raises PackageNotFoundError (subclass of ImportError) -> HAS_WINRM = False  (bug)
lookup 'pywinrm' -> '0.5.0'                                                -> HAS_WINRM = True   (fixed)

Added a regression test (test_has_winrm_detection_uses_pywinrm_distribution) that asserts salt.utils.cloud.HAS_WINRM is True when pywinrm is installed (skips otherwise) — it fails with the old "winrm" lookup and passes after the fix.

Closes #69976

salt.utils.cloud looked up the distribution name "winrm" with
importlib.metadata.version(), but the PyPI package is distributed as
"pywinrm" (imported as `winrm`). version("winrm") raises
PackageNotFoundError, which the surrounding `except ImportError` swallows,
so HAS_WINRM is always False and salt-cloud WinRM deployments fail with
"WinRM requested but module winrm could not be imported" even when pywinrm
is installed (>= 0.3.0).

Use the correct "pywinrm" distribution name. The existing
test_winrm_pinnned_version already queries version("pywinrm").

Closes saltstack#69976
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.

[Bug]: HAS_WINRM always False in salt-cloud due to wrong distribution name in importlib.metadata lookup

1 participant