Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog/68612.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix user.info when querying domain users. Uses DsGetDcName for more
dependable domain controller lookup.
5 changes: 3 additions & 2 deletions salt/modules/win_useradd.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,9 +811,10 @@ def info(name):

if domain_name != ".":
try:
server = win32net.NetGetAnyDCName(None, domain_name)
dc_info = win32security.DsGetDcName(None, domain_name)
server = dc_info["DomainControllerName"]
log.debug("Found DC: %s", server)
except win32net.error:
except pywintypes.error:
# Restore username to original
log.debug("DC not found. Using username: %s", str(name))
user_name = str(name)
Expand Down
13 changes: 13 additions & 0 deletions tests/pytests/functional/modules/test_win_useradd.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,19 @@ def test_info_int(user, account_int):
assert ret["uid"].startswith("S-1-5")


def test_info_domain_local(user, account_str):
domain = "." # localhost or hostname doesn't work, only .
ret = user.info(f"{domain}\\{account_str.username}")
assert ret["name"] == account_str.username
assert ret["uid"].startswith("S-1-5")


def test_info_domain_not_found(user, account_str):
domain = "junk.com"
ret = user.info(f"{domain}\\{account_str.username}")
assert ret == {}


def test_list_groups_str(user, account_str):
ret = user.list_groups(account_str.username)
assert ret == ["Users"]
Expand Down
7 changes: 4 additions & 3 deletions tests/pytests/unit/modules/test_win_useradd.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from tests.support.mock import MagicMock, patch

try:
import win32net
import pywintypes

WINAPI = True
except ImportError:
Expand Down Expand Up @@ -64,8 +64,9 @@ def test_info(account, caplog):
def test_info_domain(account, caplog):
domain = "mydomain"
dc = "myDC"
dc_info = {"DomainControllerName": dc}
with caplog.at_level(logging.DEBUG), patch(
"win32net.NetGetAnyDCName", MagicMock(return_value=dc)
"win32security.DsGetDcName", MagicMock(return_value=dc_info)
):
account.username = f"{domain}\\{account.username}"
win_useradd.info(account.username)
Expand All @@ -77,7 +78,7 @@ def test_info_error(account, caplog):
domain = "mydomain"
dc = "myDC"
with caplog.at_level(logging.DEBUG), patch(
"win32net.NetGetAnyDCName", MagicMock(side_effect=win32net.error)
"win32security.DsGetDcName", MagicMock(side_effect=pywintypes.error)
):
account.username = f"{domain}\\{account.username}"
win_useradd.info(account.username)
Expand Down