Skip to content

Commit

Permalink
remove code paths for cg<44
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasertl committed Dec 27, 2024
1 parent 9dd2e0d commit 71cc291
Show file tree
Hide file tree
Showing 12 changed files with 276 additions and 371 deletions.
9 changes: 6 additions & 3 deletions ca/django_ca/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,13 @@


class ExtensionOID(_ExtensionOID):
"""Extend the ExtensionOID object with any OIDs not known to cryptography."""
"""Extend the ExtensionOID object with any OIDs not known to cryptography.
if CRYPTOGRAPHY_VERSION < (44, 0): # pragma: cryptography<44 branch
ADMISSIONS = x509.ObjectIdentifier("1.3.36.8.3.3")
This subclass is usually empty but can be used in cases where some ExtensionOIDs are not supported in all
currently supported versions of cryptography.
"""

pass


ACCESS_METHOD_TYPES: MappingProxyType[AccessMethods, x509.ObjectIdentifier] = MappingProxyType(
Expand Down
8 changes: 4 additions & 4 deletions ca/django_ca/extensions/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from django_ca.utils import bytes_to_hex, format_general_name


def _naming_authority_as_text(value: "x509.NamingAuthority") -> str: # pragma: only cryptography>=44
def _naming_authority_as_text(value: "x509.NamingAuthority") -> str:
lines = ["* Naming Authority:"]
if value.id is None:
lines.append("* id: None")
Expand All @@ -34,7 +34,7 @@ def _naming_authority_as_text(value: "x509.NamingAuthority") -> str: # pragma:
return "\n".join(lines)


def _profession_info_as_text(value: "x509.ProfessionInfo") -> str: # pragma: only cryptography>=44
def _profession_info_as_text(value: "x509.ProfessionInfo") -> str:
lines = []

if value.naming_authority is not None:
Expand All @@ -56,7 +56,7 @@ def _profession_info_as_text(value: "x509.ProfessionInfo") -> str: # pragma: on
return "\n".join(lines)


def _admissions_as_text(value: "x509.Admissions") -> str: # pragma: only cryptography>=44
def _admissions_as_text(value: "x509.Admissions") -> str:
lines = []
if value.authority:
lines.append(f"* Authority: {format_general_name(value.authority)}")
Expand Down Expand Up @@ -239,7 +239,7 @@ def extension_as_text(value: x509.ExtensionType) -> str: # noqa: PLR0911
return _signed_certificate_timestamps_as_text(value)
if isinstance(value, (x509.AuthorityInformationAccess, x509.SubjectInformationAccess)):
return _authority_information_access_as_text(value)
if hasattr(x509, "Admissions") and isinstance(value, x509.Admissions): # pragma: only cryptography>=44
if isinstance(value, x509.Admissions):
return _admissions_as_text(value)
if isinstance(value, x509.AuthorityKeyIdentifier):
return _authority_key_identifier_as_text(value)
Expand Down
2 changes: 1 addition & 1 deletion ca/django_ca/management/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ def parse_x509_name(self, value: str, name_format: SubjectFormats) -> x509.Name:
if name_format == "rfc4514":
try:
return parse_name_rfc4514(value)
except ValueError as ex: # pragma: only cryptography>=43.0
except ValueError as ex:
raise CommandError(ex) from ex
# COVERAGE NOTE: Already covered by argparse
raise ValueError(f"{name_format}: Unknown subject format.") # pragma: no cover
Expand Down
30 changes: 6 additions & 24 deletions ca/django_ca/pydantic/extension_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import base64
from datetime import datetime
from typing import TYPE_CHECKING, Annotated, Any, Literal, NoReturn, Optional, Union
from typing import Annotated, Any, Literal, NoReturn, Optional, Union

from annotated_types import MaxLen, MinLen
from pydantic import AfterValidator, Base64Bytes, BeforeValidator, ConfigDict, Field, model_validator
Expand All @@ -32,25 +32,14 @@
from django_ca.pydantic.type_aliases import Base64EncodedBytes, NonEmptyOrderedSet, OIDType
from django_ca.typehints import DistributionPointReasons, LogEntryTypes

if TYPE_CHECKING: # pragma: only cryptography<44
# NOTE: we can use bases directly once instances are supported in every versoin
NamingAuthorityBase = CryptographyModel[x509.NamingAuthority]
ProfessionInfoBase = CryptographyModel[x509.ProfessionInfo]
AdmissionBase = CryptographyModel[x509.Admission]
AdmissionsValueModelBase = CryptographyModel[x509.Admissions]
else:
NamingAuthorityBase = ProfessionInfoBase = AdmissionBase = AdmissionsValueModelBase = CryptographyModel

_NOTICE_REFERENCE_DESCRIPTION = (
"A NoticeReferenceModel consists of an optional *organization* and an optional list of *notice_numbers*."
)


class NamingAuthorityModel(NamingAuthorityBase): # pragma: only cryptography>=44.0
class NamingAuthorityModel(CryptographyModel[x509.NamingAuthority]):
"""Pydantic model wrapping :py:class:`~cg:cryptography.x509.NamingAuthority`.
.. NOTE:: This class will not be able to produce a cryptography instance when using ``cryptography<44``.
.. versionadded:: 2.1.0
"""

Expand All @@ -69,11 +58,9 @@ def cryptography(self) -> "x509.NamingAuthority":
return x509.NamingAuthority(id=oid, url=self.url, text=self.text)


class ProfessionInfoModel(ProfessionInfoBase): # pragma: only cryptography>=44.0
class ProfessionInfoModel(CryptographyModel[x509.ProfessionInfo]):
"""Pydantic model wrapping :py:class:`~cg:cryptography.x509.ProfessionInfo`.
.. NOTE:: This class will not be able to produce a cryptography instance when using ``cryptography<44``.
.. versionadded:: 2.1.0
"""

Expand Down Expand Up @@ -109,11 +96,9 @@ def check_consistency(self) -> "ProfessionInfoModel":
return self


class AdmissionModel(AdmissionBase): # pragma: only cryptography>=44.0
class AdmissionModel(CryptographyModel[x509.Admission]):
"""Pydantic model wrapping :py:class:`~cg:cryptography.x509.Admission`.
.. NOTE:: This class will not be able to produce a cryptography instance when using ``cryptography<44``.
.. versionadded:: 2.1.0
"""

Expand All @@ -138,11 +123,9 @@ def cryptography(self) -> "x509.Admission":
)


class AdmissionsValueModel(AdmissionsValueModelBase): # pragma: only cryptography>=44.0
class AdmissionsValueModel(CryptographyModel[x509.Admissions]):
"""Pydantic model wrapping :py:class:`~cg:cryptography.x509.Admissions`.
.. NOTE:: This class will not be able to produce a cryptography instance when using ``cryptography<44``.
.. versionadded:: 2.1.0
"""

Expand All @@ -164,8 +147,7 @@ def cryptography(self) -> "x509.Admissions":
@classmethod
def parse_cryptography(cls, data: Any) -> Any:
"""Parse cryptography instance."""
# pragma: only cryptography<44 # remove hasattr() call when cg<44 is dropped
if hasattr(x509, "Admissions") and isinstance(data, x509.Admissions):
if isinstance(data, x509.Admissions):
return {"authority": data.authority, "admissions": data._admissions} # pylint: disable=protected-access
return data

Expand Down
4 changes: 1 addition & 3 deletions ca/django_ca/pydantic/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,11 +323,9 @@ def extension_type(self) -> NoReturn: # pragma: no cover
########################


class AdmissionsModel(ExtensionModel["x509.Admissions"]): # pragma: only cryptography>=44.0
class AdmissionsModel(ExtensionModel[x509.Admissions]):
"""Pydantic model for a :py:class:`~cg:cryptography.x509.Admissions` extension.
.. NOTE:: This class will not be able to produce a cryptography instance when using ``cryptography<44``.
.. versionadded:: 2.1.0
The `value` is a :py:class:`~django_ca.pydantic.extension_attributes.AdmissionsValueModel`:
Expand Down
4 changes: 2 additions & 2 deletions ca/django_ca/templatetags/django_ca.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@


@register.filter
def admissions(value: "x509.Admissions") -> list["x509.Admission"]: # pragma: only cryptography>=44
def admissions(value: "x509.Admissions") -> list["x509.Admission"]:
"""Return list of admissions (templates cannot contain underscores in variables)."""
return value._admissions # pylint: disable=protected-access; only way to get admissions

Expand Down Expand Up @@ -97,7 +97,7 @@ def enum(mod: Any, cls_name_and_member: str) -> Any:


@register.filter
def format_general_name(value: x509.GeneralName) -> str: # pragma: only cryptography>=44
def format_general_name(value: x509.GeneralName) -> str:
"""A template tag to format general name."""
return _format_general_name(value)

Expand Down
22 changes: 2 additions & 20 deletions ca/django_ca/tests/extensions/test_admin_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
from django_ca.tests.base.mixins import TestCaseMixin
from django_ca.utils import bytes_to_hex

if hasattr(x509, "Admissions"): # pragma: only cryptography<44.0
ALL_EXTENSIONS_ADMISSIONS = """
ALL_EXTENSIONS_ADMISSIONS = """
<ul>
<li>Authority: URI:https://default-authority.admissions.example.com</li>
<li>Admissions:
Expand Down Expand Up @@ -84,7 +83,7 @@
</ul>
</li>
</ul>"""
ALT_EXTENSIONS_ADMISSIONS = """
ALT_EXTENSIONS_ADMISSIONS = """
<ul>
<li>Admissions:
<ul>
Expand All @@ -104,23 +103,6 @@
</ul>
</li>
</ul>"""
else:
ALL_EXTENSIONS_ADMISSIONS = (
"30:82:01:9B:86:30:68:74:74:70:73:3A:2F:2F:64:65:66:61:75:6C:74:2D:61:75:74:68:6F:72:69:74:79:2E:61:"
"64:6D:69:73:73:69:6F:6E:73:2E:65:78:61:6D:70:6C:65:2E:63:6F:6D:30:82:01:65:30:82:01:61:A0:2A:86:28:"
"68:74:74:70:73:3A:2F:2F:61:75:74:68:6F:72:69:74:79:2E:61:64:6D:69:73:73:69:6F:6E:73:2E:65:78:61:6D:"
"70:6C:65:2E:63:6F:6D:A1:5B:30:59:06:02:2A:03:16:2A:68:74:74:70:73:3A:2F:2F:6E:61:6D:69:6E:67:2D:61:"
"75:74:68:2E:61:64:6D:69:73:73:69:6F:6E:73:2E:65:78:61:6D:70:6C:65:2E:63:6F:6D:0C:27:6E:61:6D:69:6E:"
"67:2D:61:75:74:68:2E:61:64:6D:69:73:73:69:6F:6E:73:2E:65:78:61:6D:70:6C:65:2E:63:6F:6D:20:74:65:78:"
"74:30:81:D5:30:81:B7:A0:77:30:75:16:3A:68:74:74:70:73:3A:2F:2F:6E:61:6D:69:6E:67:2D:61:75:74:68:2E:"
"70:72:6F:66:65:73:73:69:6F:6E:2D:69:6E:66:6F:2E:61:64:6D:69:73:73:69:6F:6E:73:2E:65:78:61:6D:70:6C:"
"65:2E:63:6F:6D:0C:37:6E:61:6D:69:6E:67:2D:61:75:74:68:2E:70:72:6F:66:65:73:73:69:6F:6E:2D:69:6E:66:"
"6F:2E:61:64:6D:69:73:73:69:6F:6E:73:2E:65:78:61:6D:70:6C:65:2E:63:6F:6D:20:74:65:78:74:30:0B:0C:09:"
"70:72:6F:66:5F:69:74:65:6D:30:05:06:03:2A:03:05:13:13:72:65:67:69:73:74:72:61:74:69:6F:6E:2D:6E:75:"
"6D:62:65:72:04:13:61:64:64:2D:70:72:6F:66:65:73:73:69:6F:6E:2D:69:6E:66:6F:30:19:A0:02:30:00:30:13:"
"0C:11:70:72:6F:66:5F:69:74:65:6D:5F:6D:69:6E:69:6D:61:6C"
)
ALT_EXTENSIONS_ADMISSIONS = "30:15:30:13:30:11:30:0F:30:0D:30:0B:0C:09:70:72:6F:66:5F:69:74:65:6D"


class CertificateExtensionTestCase(TestCaseMixin, TestCase):
Expand Down
Loading

0 comments on commit 71cc291

Please sign in to comment.