Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pysaml2 raises DeprecationWarning: datetime.datetime.utcnow() when running tests with pytests on python 3.12 #934

Open
kfrydel opened this issue Oct 16, 2023 · 0 comments · May be fixed by #939

Comments

@kfrydel
Copy link

kfrydel commented Oct 16, 2023

We are switching our project to Python 3.12. Our tests that use pysaml2 started to fail with:

  File "/home/user/.virtualenvs/py-3.12/lib/python3.12/site-packages/saml2/client_base.py", line 793, in parse_authn_request_response
    resp = self._parse_response(xmlstr, AuthnResponse, "assertion_consumer_service", binding, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.virtualenvs/py-3.12/lib/python3.12/site-packages/saml2/entity.py", line 1503, in _parse_response
    response.verify(keys)
  File "/home/user/.virtualenvs/py-3.12/lib/python3.12/site-packages/saml2/response.py", line 1016, in verify
    res = self._verify()
          ^^^^^^^^^^^^^^
  File "/home/user/.virtualenvs/py-3.12/lib/python3.12/site-packages/saml2/response.py", line 421, in _verify
    valid = self.issue_instant_ok() and self.status_ok()
            ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.virtualenvs/py-3.12/lib/python3.12/site-packages/saml2/response.py", line 395, in issue_instant_ok
    upper = time_util.shift_time(time_util.time_in_a_while(days=1), self.timeslack).timetuple()
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.virtualenvs/py-3.12/lib/python3.12/site-packages/saml2/time_util.py", line 178, in time_in_a_while
    return datetime.utcnow() + delta
           ^^^^^^^^^^^^^^^^^
DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).

I know I can add an ignore to pytest.ini but it would be good to avoid ignores.

Code Version

pysaml2==7.4.2

Expected Behavior

No warning is raised.

Current Behavior

DeprecationWarning is issued

Possible Solution

Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC) instead of datetime.datetime.utcnow().

Steps to Reproduce

The same can be reproduced when running tests in pysaml2 project on Python 3.12. It shows even more warnings:

# poetry run pytest
=============================================================================================== test session starts ===============================================================================================
platform linux -- Python 3.12.0, pytest-7.4.2, pluggy-1.3.0 -- /home/kamil/.cache/pypoetry/virtualenvs/pysaml2-0-dkQnin-py3.12/bin/python
cachedir: .pytest_cache
rootdir: /mnt/devel/sf/pysaml2
configfile: pyproject.toml
testpaths: tests
plugins: cov-4.1.0
collected 785 items  
...
================================================================================================ warnings summary =================================================================================================
../../../../home/kamil/.cache/pypoetry/virtualenvs/pysaml2-0-dkQnin-py3.12/lib/python3.12/site-packages/dateutil/tz/tz.py:37
  /home/kamil/.cache/pypoetry/virtualenvs/pysaml2-0-dkQnin-py3.12/lib/python3.12/site-packages/dateutil/tz/tz.py:37: DeprecationWarning: datetime.datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.fromtimestamp(timestamp, datetime.UTC).
    EPOCH = datetime.datetime.utcfromtimestamp(0)

tests/test_10_time_util.py: 2 warnings
tests/test_20_assertion.py: 6 warnings
tests/test_32_cache.py: 5 warnings
tests/test_34_population.py: 4 warnings
tests/test_41_response.py: 8 warnings
tests/test_42_enc.py: 6 warnings
tests/test_44_authnresp.py: 9 warnings
tests/test_50_server.py: 160 warnings
tests/test_51_client.py: 223 warnings
tests/test_52_default_sign_alg.py: 6 warnings
tests/test_62_vo.py: 2 warnings
tests/test_63_ecp.py: 5 warnings
tests/test_64_artifact.py: 4 warnings
tests/test_65_authn_query.py: 7 warnings
tests/test_66_name_id_mapping.py: 2 warnings
tests/test_67_manage_name_id.py: 3 warnings
tests/test_68_assertion_id.py: 4 warnings
tests/test_89_http_post_relay_state.py: 2 warnings
  /mnt/devel/sf/pysaml2/src/saml2/time_util.py:178: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
    return datetime.utcnow() + delta

tests/test_44_authnresp.py: 3 warnings
tests/test_50_server.py: 7 warnings
tests/test_51_client.py: 95 warnings
tests/test_63_ecp.py: 3 warnings
tests/test_64_artifact.py: 2 warnings
tests/test_65_authn_query.py: 5 warnings
tests/test_66_name_id_mapping.py: 2 warnings
tests/test_67_manage_name_id.py: 3 warnings
tests/test_68_assertion_id.py: 2 warnings
  /mnt/devel/sf/pysaml2/src/saml2/time_util.py:188: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
    return datetime.utcnow() - delta

tests/test_44_authnresp.py::TestAuthnResponse::test_verify_w_authn
  /mnt/devel/sf/pysaml2/tests/test_44_authnresp.py:134: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
    now = datetime.utcnow()

tests/test_50_server.py: 8 warnings
tests/test_81_certificates.py: 17 warnings
  /mnt/devel/sf/pysaml2/src/saml2/cert.py:281: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
    now = pytz.UTC.localize(datetime.datetime.utcnow())

tests/test_50_server.py: 8 warnings
tests/test_81_certificates.py: 17 warnings
  /home/kamil/.cache/pypoetry/virtualenvs/pysaml2-0-dkQnin-py3.12/lib/python3.12/site-packages/OpenSSL/crypto.py:1443: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
    return not_after < datetime.datetime.utcnow()

tests/test_92_aes.py: 35 warnings
  /mnt/devel/sf/pysaml2/src/saml2/cryptography/symmetric.py:124: DeprecationWarning: AESCipher type is deprecated. It will be removed in the next version. Use saml2.cryptography.symmetric.Default or saml2.cryptography.symmetric.Fernet instead.
    _warn(_deprecation_msg, DeprecationWarning)

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
============================================================================================= short test summary info =============================================================================================
SKIPPED [1] tests/test_37_entity_categories.py:296: Temporarily disabled
SKIPPED [1] tests/test_37_entity_categories.py:325: Temporarily disabled
SKIPPED [1] tests/test_37_entity_categories.py:358: Temporarily disabled
SKIPPED [1] tests/test_60_sp.py:59: s2repoze dependencies not installed
SKIPPED [1] tests/test_60_sp.py:62: s2repoze dependencies not installed
============================================================================ 780 passed, 5 skipped, 667 warnings in 200.32s (0:03:20) =============================================================================
kfrydel added a commit to kfrydel/pysaml2 that referenced this issue Nov 3, 2023
This fixes IdentityPython#934.
utcnow function, starting from Python 3.12 is deprecated:
https://docs.python.org/3/library/datetime.html#datetime.datetime.utcnow

Thus, this commit turns usages of utcnow() into now(UTC).
@kfrydel kfrydel linked a pull request Nov 3, 2023 that will close this issue
4 tasks
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 a pull request may close this issue.

1 participant