Skip to content

Commit

Permalink
chore: update charm libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
telcobot committed Mar 6, 2024
1 parent 1072640 commit efde99e
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions lib/charms/tls_certificates_interface/v3/tls_certificates.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,15 +278,15 @@ 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

from cryptography import x509
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,
Expand All @@ -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"]

Expand Down Expand Up @@ -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
)


Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit efde99e

Please sign in to comment.