ubuntu-advantage: compare date from two /aware/ datetime objects
The current implementation attempts to compare two datetime objects by comparing the value returned by .timestamp(). However, one of the objects is an /aware/ datetime and the other is a /naive/ datetime. When calling .timestamp() on a naive datetime object, the function expects the datetime object to represent local time. However, in our scenario, it was UTC time. Therefore, the comparison is slightly inaccurate and can end up rejecting a token that is not yet expired ; or accepting one that is about to expire. In practice, this should rarely be a problem (a few hours is not a big deal). But we now compare two /aware/ datetime objects to make the comparison more correct. Also, datetime.datetime.utcnow() (which returns a naive datetime object) is deprecated in favor of datetime.datetime.now() (which can return an aware datetime object if timezone information is passed). Signed-off-by: Olivier Gayot <olivier.gayot@canonical.com>
This commit is contained in:
parent
4695078c84
commit
fa8e49457b
|
@ -17,6 +17,7 @@ helper. """
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import contextlib
|
import contextlib
|
||||||
|
import datetime
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
@ -331,7 +332,7 @@ class UAInterface:
|
||||||
# is specified in RFC 3339 but not supported by fromisoformat before
|
# is specified in RFC 3339 but not supported by fromisoformat before
|
||||||
# Python 3.11. See https://bugs.python.org/issue35829
|
# Python 3.11. See https://bugs.python.org/issue35829
|
||||||
expiration = dt.fromisoformat(info["expires"].replace("Z", "+00:00"))
|
expiration = dt.fromisoformat(info["expires"].replace("Z", "+00:00"))
|
||||||
if expiration.timestamp() <= dt.utcnow().timestamp():
|
if expiration <= dt.now(datetime.timezone.utc):
|
||||||
raise ExpiredTokenError(token, expires=info["expires"])
|
raise ExpiredTokenError(token, expires=info["expires"])
|
||||||
|
|
||||||
def is_activable_service(service: dict) -> bool:
|
def is_activable_service(service: dict) -> bool:
|
||||||
|
|
Loading…
Reference in New Issue