diff --git a/lib/charms/tls_certificates_interface/v3/tls_certificates.py b/lib/charms/tls_certificates_interface/v3/tls_certificates.py index 8840b4b..1914860 100644 --- a/lib/charms/tls_certificates_interface/v3/tls_certificates.py +++ b/lib/charms/tls_certificates_interface/v3/tls_certificates.py @@ -278,7 +278,7 @@ def _on_all_certificates_invalidated(self, event: AllCertificatesInvalidatedEven import uuid from contextlib import suppress from dataclasses import dataclass -from datetime import datetime, timedelta +from datetime import datetime, timedelta, timezone from ipaddress import IPv4Address from typing import List, Literal, Optional, Union @@ -286,7 +286,7 @@ def _on_all_certificates_invalidated(self, event: AllCertificatesInvalidatedEven from cryptography.hazmat._oid import ExtensionOID from cryptography.hazmat.primitives import hashes, serialization from cryptography.hazmat.primitives.asymmetric import rsa -from jsonschema import exceptions, validate # type: ignore[import-untyped] +from jsonschema import exceptions, validate from ops.charm import ( CharmBase, CharmEvents, @@ -312,7 +312,7 @@ def _on_all_certificates_invalidated(self, event: AllCertificatesInvalidatedEven # Increment this PATCH version before using `charmcraft publish-lib` or reset # to 0 if you are raising the major API version -LIBPATCH = 4 +LIBPATCH = 5 PYDEPS = ["cryptography", "jsonschema"] @@ -664,7 +664,9 @@ def _get_closest_future_time( datetime: expiry_notification_time if not in the past, expiry_time otherwise """ return ( - expiry_notification_time if datetime.utcnow() < expiry_notification_time else expiry_time + expiry_notification_time + if datetime.now(timezone.utc) < expiry_notification_time + else expiry_time ) @@ -679,7 +681,7 @@ def _get_certificate_expiry_time(certificate: str) -> Optional[datetime]: """ try: certificate_object = x509.load_pem_x509_certificate(data=certificate.encode()) - return certificate_object.not_valid_after + return certificate_object.not_valid_after_utc except ValueError: logger.warning("Could not load certificate.") return None @@ -734,8 +736,8 @@ def generate_ca( .issuer_name(subject_name) .public_key(private_key_object.public_key()) # type: ignore[arg-type] .serial_number(x509.random_serial_number()) - .not_valid_before(datetime.utcnow()) - .not_valid_after(datetime.utcnow() + timedelta(days=validity)) + .not_valid_before(datetime.now(timezone.utc)) + .not_valid_after(datetime.now(timezone.utc) + timedelta(days=validity)) .add_extension(x509.SubjectKeyIdentifier(digest=subject_identifier), critical=False) .add_extension( x509.AuthorityKeyIdentifier( @@ -889,8 +891,8 @@ def generate_certificate( .issuer_name(issuer) .public_key(csr_object.public_key()) .serial_number(x509.random_serial_number()) - .not_valid_before(datetime.utcnow()) - .not_valid_after(datetime.utcnow() + timedelta(days=validity)) + .not_valid_before(datetime.now(timezone.utc)) + .not_valid_after(datetime.now(timezone.utc) + timedelta(days=validity)) ) extensions = get_certificate_extensions( authority_key_identifier=ca_pem.extensions.get_extension_for_class( @@ -1088,7 +1090,7 @@ class CertificatesRequirerCharmEvents(CharmEvents): class TLSCertificatesProvidesV3(Object): """TLS certificates provider class to be instantiated by TLS certificates providers.""" - on = CertificatesProviderCharmEvents() + on = CertificatesProviderCharmEvents() # type: ignore[reportAssignmentType] def __init__(self, charm: CharmBase, relationship_name: str): super().__init__(charm, relationship_name) @@ -1455,7 +1457,7 @@ def certificate_issued_for_csr( class TLSCertificatesRequiresV3(Object): """TLS certificates requirer class to be instantiated by TLS certificates requirers.""" - on = CertificatesRequirerCharmEvents() + on = CertificatesRequirerCharmEvents() # type: ignore[reportAssignmentType] def __init__( self, @@ -1698,7 +1700,7 @@ def get_expiring_certificates(self) -> List[ProviderCertificate]: expiry_notification_time = expiry_time - timedelta( hours=self.expiry_notification_time ) - if datetime.utcnow() > expiry_notification_time: + if datetime.now(timezone.utc) > expiry_notification_time: expiring_certificates.append(cert) return expiring_certificates @@ -1855,7 +1857,7 @@ def _on_secret_expired(self, event: SecretExpiredEvent) -> None: event.secret.remove_all_revisions() return - if datetime.utcnow() < expiry_time: + if datetime.now(timezone.utc) < expiry_time: logger.warning("Certificate almost expired") self.on.certificate_expiring.emit( certificate=provider_certificate.certificate,