Skip to content

Commit

Permalink
Fix mac_shadow module
Browse files Browse the repository at this point in the history
Fixes an issue with the mac_shadow module where it would fail to
retrieve values that weren't set yet... last login for example.
Also adds some tests and a changelog
  • Loading branch information
twangboy committed Feb 2, 2024
1 parent 2c02887 commit 314ec39
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 18 deletions.
3 changes: 3 additions & 0 deletions changelog/34658.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix an issue with mac_shadow that was causing a command execution error when
retrieving values that were not yet set. For example, retrieving last login
before the user had logged in.
41 changes: 23 additions & 18 deletions salt/modules/mac_shadow.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,12 @@ def get_account_created(name):
salt '*' shadow.get_account_created admin
"""
ret = _get_account_policy_data_value(name, "creationTime")

unix_timestamp = salt.utils.mac_utils.parse_return(ret)

date_text = _convert_to_datetime(unix_timestamp)
try:
ret = _get_account_policy_data_value(name, "creationTime")
unix_timestamp = salt.utils.mac_utils.parse_return(ret)
date_text = _convert_to_datetime(unix_timestamp)
except CommandExecutionError:
date_text = "Creation date not defined"

return date_text

Expand All @@ -228,11 +229,12 @@ def get_last_change(name):
salt '*' shadow.get_last_change admin
"""
ret = _get_account_policy_data_value(name, "passwordLastSetTime")

unix_timestamp = salt.utils.mac_utils.parse_return(ret)

date_text = _convert_to_datetime(unix_timestamp)
try:
ret = _get_account_policy_data_value(name, "passwordLastSetTime")
unix_timestamp = salt.utils.mac_utils.parse_return(ret)
date_text = _convert_to_datetime(unix_timestamp)
except CommandExecutionError:
date_text = "Password last set date/time undefined"

return date_text

Expand All @@ -254,9 +256,11 @@ def get_login_failed_count(name):
salt '*' shadow.get_login_failed_count admin
"""
ret = _get_account_policy_data_value(name, "failedLoginCount")

return salt.utils.mac_utils.parse_return(ret)
try:
ret = _get_account_policy_data_value(name, "failedLoginCount")
return salt.utils.mac_utils.parse_return(ret)
except CommandExecutionError:
return "Failed login count undefined"


def get_login_failed_last(name):
Expand All @@ -277,11 +281,12 @@ def get_login_failed_last(name):
salt '*' shadow.get_login_failed_last admin
"""
ret = _get_account_policy_data_value(name, "failedLoginTimestamp")

unix_timestamp = salt.utils.mac_utils.parse_return(ret)

date_text = _convert_to_datetime(unix_timestamp)
try:
ret = _get_account_policy_data_value(name, "failedLoginTimestamp")
unix_timestamp = salt.utils.mac_utils.parse_return(ret)
date_text = _convert_to_datetime(unix_timestamp)
except CommandExecutionError:
date_text = "Last failed login undefined"

return date_text

Expand Down
77 changes: 77 additions & 0 deletions tests/pytests/unit/modules/test_mac_shadow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""
Unit Tests for the mac_desktop execution module.
"""

import pytest

import salt.modules.mac_shadow as mac_shadow
from salt.exceptions import CommandExecutionError
from tests.support.mock import patch

pytestmark = [
pytest.mark.skip_unless_on_darwin,
]


def test_get_account_created():
with patch.object(mac_shadow, "_get_account_policy_data_value", return_value="0"):
result = mac_shadow.get_account_created("junk")
expected = "1969-12-31 17:00:00"
assert result == expected


def test_get_account_created_error():
with patch.object(
mac_shadow, "_get_account_policy_data_value", side_effect=CommandExecutionError
):
result = mac_shadow.get_account_created("junk")
expected = "Creation date not defined"
assert result == expected


def test_get_last_change():
with patch.object(mac_shadow, "_get_account_policy_data_value", return_value="0"):
result = mac_shadow.get_last_change("junk")
expected = "1969-12-31 17:00:00"
assert result == expected


def test_get_last_change_error():
with patch.object(
mac_shadow, "_get_account_policy_data_value", side_effect=CommandExecutionError
):
result = mac_shadow.get_last_change("junk")
expected = "Password last set date/time undefined"
assert result == expected


def test_login_failed_count():
with patch.object(mac_shadow, "_get_account_policy_data_value", return_value="0"):
result = mac_shadow.get_login_failed_count("junk")
expected = "0"
assert result == expected


def test_get_login_failed_count_error():
with patch.object(
mac_shadow, "_get_account_policy_data_value", side_effect=CommandExecutionError
):
result = mac_shadow.get_login_failed_count("junk")
expected = "Failed login count undefined"
assert result == expected


def test_login_failed_last():
with patch.object(mac_shadow, "_get_account_policy_data_value", return_value="0"):
result = mac_shadow.get_login_failed_last("junk")
expected = "1969-12-31 17:00:00"
assert result == expected


def test_get_login_failed_last_error():
with patch.object(
mac_shadow, "_get_account_policy_data_value", side_effect=CommandExecutionError
):
result = mac_shadow.get_login_failed_last("junk")
expected = "Last failed login undefined"
assert result == expected

0 comments on commit 314ec39

Please sign in to comment.