Skip to content

Commit cf84d33

Browse files
committed
Add underlying OSError info into Persistence exceptions
1 parent cc6fd00 commit cf84d33

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

msal_extensions/persistence.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ def save(self, content):
194194
except OSError as exception:
195195
raise PersistenceEncryptionError(
196196
err_no=getattr(exception, "winerror", None), # Exists in Python 3 on Windows
197-
message="Unable to encrypt data. You may consider disable encryption.")
197+
message="Encryption failed: {}. Consider disable encryption.".format(exception), # pylint: disable=consider-using-f-string
198+
)
198199
with os.fdopen(_open(self._location), 'wb+') as handle:
199200
handle.write(data)
200201

@@ -220,7 +221,7 @@ def load(self):
220221
except OSError as exception:
221222
raise PersistenceDecryptionError(
222223
err_no=getattr(exception, "winerror", None), # Exists in Python 3 on Windows
223-
message="Unable to decrypt data. You may have to delete the file.",
224+
message="Decryption failed: {}. You may have to delete the file.".format(exception), # pylint: disable=consider-using-f-string
224225
location=self._location,
225226
)
226227

msal_extensions/windows.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def raw(self):
4646
"The computer must be trusted for delegation and "
4747
"the current user account must be configured to allow delegation. "
4848
"See also https://docs.microsoft.com/en-us/windows/security/threat-protection/security-policy-settings/enable-computer-and-user-accounts-to-be-trusted-for-delegation",
49+
13: "The data is invalid",
4950
}
5051

5152
# This code is modeled from a StackOverflow question, which can be found here:

tests/test_persistence.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,18 @@
99
from msal_extensions.persistence import *
1010

1111

12-
is_running_on_travis_ci = bool( # (WTF) What-The-Finding:
12+
def _is_env_var_defined(env_var):
13+
return bool( # (WTF) What-The-Finding:
1314
# The bool(...) is necessary, otherwise skipif(...) would treat "true" as
1415
# string conditions and then raise an undefined "true" exception.
1516
# https://docs.pytest.org/en/latest/historical-notes.html#string-conditions
16-
os.getenv("TRAVIS"))
17+
os.getenv(env_var))
18+
19+
20+
# Note: If you use tox, remember to pass them through via tox.ini
21+
# https://tox.wiki/en/latest/example/basic.html#passing-down-environment-variables
22+
is_running_on_travis_ci = _is_env_var_defined("TRAVIS")
23+
is_running_on_github_ci = _is_env_var_defined("GITHUB_ACTIONS")
1724

1825
@pytest.fixture
1926
def temp_location():
@@ -42,7 +49,16 @@ def test_nonexistent_file_persistence(temp_location):
4249
is_running_on_travis_ci or not sys.platform.startswith('win'),
4350
reason="Requires Windows Desktop")
4451
def test_file_persistence_with_data_protection(temp_location):
45-
_test_persistence_roundtrip(FilePersistenceWithDataProtection(temp_location))
52+
try:
53+
print(is_running_on_github_ci, is_running_on_travis_ci)
54+
for k, v in os.environ.items():
55+
print(k, v)
56+
_test_persistence_roundtrip(FilePersistenceWithDataProtection(temp_location))
57+
except PersistenceDecryptionError:
58+
if is_running_on_github_ci or is_running_on_travis_ci:
59+
logging.warning("DPAPI tends to fail on Windows VM. Run this on your desktop to double check.")
60+
else:
61+
raise
4662

4763
@pytest.mark.skipif(
4864
is_running_on_travis_ci or not sys.platform.startswith('win'),

tox.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ envlist = py27,py35,py36,py37,py38
55
deps = pytest
66
passenv =
77
TRAVIS
8+
GITHUB_ACTIONS
9+
810
commands =
911
pytest

0 commit comments

Comments
 (0)