From fa8e49457b6975e8ee15438a1bc3fc54c23c1f46 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Thu, 25 Apr 2024 15:59:56 +0200 Subject: [PATCH] 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 --- subiquity/server/ubuntu_advantage.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/subiquity/server/ubuntu_advantage.py b/subiquity/server/ubuntu_advantage.py index 0b43c207..640d4637 100644 --- a/subiquity/server/ubuntu_advantage.py +++ b/subiquity/server/ubuntu_advantage.py @@ -17,6 +17,7 @@ helper. """ import asyncio import contextlib +import datetime import json import logging import os @@ -331,7 +332,7 @@ class UAInterface: # is specified in RFC 3339 but not supported by fromisoformat before # Python 3.11. See https://bugs.python.org/issue35829 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"]) def is_activable_service(service: dict) -> bool: