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

Remove utcnow() deprecation warning. #437

Closed
wants to merge 2 commits into from
Closed

Conversation

rthalley
Copy link
Contributor

Python 3.12 deprecates utcnow(), but we cannot do the recommended thing and replace utcnow() with datetime.now(timezone.utc) as the datetime objects that the cryptography x509 library puts in certificates do not have any timezone info. If we try to compare them with something that has timezone info we will get an exception:

TypeError: can't compare offset-naive and offset-aware datetimes

We must continue to make an explictly offset-naive timestamp.

Python 3.12 deprecates utcnow(), but we cannot do the recommended thing and replace
utcnow() with datetime.now(timezone.utc) as the datetime objects that the cryptography
x509 library puts in certificates do not have any timezone info.  If we try to
compare them with something that has timezone info we will get an exception:

    TypeError: can't compare offset-naive and offset-aware datetimes

We must continue to make an explictly offset-naive timestamp.
Copy link

codecov bot commented Dec 29, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (ac18ff6) 100.00% compared to head (346fe0c) 100.00%.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #437   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           25        25           
  Lines         4868      4868           
=========================================
  Hits          4868      4868           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

# TypeError: can't compare offset-naive and offset-aware datetimes
#
# We must continue to make an explicitly offset-naive timestamp.
utcnow = partial(datetime.datetime.now, None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, this doesn't look equivalent, I would have expected something like:

datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking into this a bit deeper it looks like we are going the wrong way here. We should be moving towards timezone-aware datetimes, as in:

Comment on lines +42 to +43
.not_valid_before(datetime.datetime.now(None))
.not_valid_after(datetime.datetime.now(None) + datetime.timedelta(days=10))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here we should use tls.utcnow() for consistency

@jlaine
Copy link
Contributor

jlaine commented Dec 29, 2023

Unless I'm mistaken, the "right" way to fix this would be to wait for cryptography 42 which supports timezone-aware datetimes, see my comments above.

@rthalley
Copy link
Contributor Author

If they are going to fix it, then yes, that is the right way!

@jlaine
Copy link
Contributor

jlaine commented Dec 29, 2023

If they are going to fix it, then yes, that is the right way!

We could also have a two-step approach.

Right now

  • make tests/utils.py use aioquic.tls.utcnow
  • replace utcnow by:
def utcnow() -> datetime.datetime:
    return datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)

When cryptography 42 comes out

Use timezone-aware datetimes everywhere!

It would be good to check that CertificateBuilder has also been updated to allow timezone-aware datetimes, as I only checked x509.Certificate attributes.

@rthalley
Copy link
Contributor Author

I checked CertificateBuilder in the dev version of 42, and it accepts either naive or non-naive date times, normalizing to naive before it calls into the rust code, so I think we're ok there.

I'm guessing cryptography 42 will probably release relatively soon, possibly sooner than we will, so it's probably not worth merging this branch. I'll redo things when 42 comes out.

@jlaine
Copy link
Contributor

jlaine commented Jan 23, 2024

cryptography 42 is out :)

@rthalley
Copy link
Contributor Author

I'm closing this PR in favor of a new one for cryptography 42 on a different branch.

@rthalley rthalley closed this Jan 23, 2024
@rthalley rthalley deleted the utcnow branch March 11, 2024 20:39
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 this pull request may close these issues.

2 participants