IdentityPython / pysaml2

Python implementation of SAML2
Apache License 2.0
555 stars 422 forks source link

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

Open kfrydel opened 11 months ago

kfrydel commented 11 months ago

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) =============================================================================