Skip to content

Fix FFDH CryptographyDeprecationWarning raised when importing spiffe - #446

Merged
maxlambrecht merged 1 commit into
HewlettPackard:mainfrom
c4rlo:fix-private-key-types-ffdh-warning
Aug 8, 2026
Merged

Fix FFDH CryptographyDeprecationWarning raised when importing spiffe#446
maxlambrecht merged 1 commit into
HewlettPackard:mainfrom
c4rlo:fix-private-key-types-ffdh-warning

Conversation

@c4rlo

@c4rlo c4rlo commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Summary

certificate_utils.PRIVATE_KEY_TYPES is a hand-rolled Union that duplicates cryptography's PrivateKeyTypes. It was added as _PRIVATE_KEY_TYPES in 77537c6 (#41), and dh.DHPrivateKey was added to it later in 95b2e32 (#107).

cryptography 50.0.0 deprecated Diffie-Hellman over finite fields (FFDH), so merely touching dh.DHPrivateKey now emits a CryptographyDeprecationWarning at module import time:

cryptography.utils.CryptographyDeprecationWarning: Diffie-Hellman over finite fields (FFDH) is deprecated and support will be removed in a future release. Use a more modern key exchange algorithm.

Because CryptographyDeprecationWarning subclasses UserWarning (not DeprecationWarning), any downstream project that runs under a strict filterwarnings = error policy fails to import spiffe at all — the warning is raised as an error during import, before any code runs.

This became reachable once the cryptography upper bound was removed in 429c165 (>=46,<48>=46, relaxing the pin from 8deed17), allowing cryptography 50 to resolve.

The copied union had also drifted from cryptography: it no longer matches what the key loaders can return (e.g. it predates the ML-DSA / ML-KEM key types).

Change

Alias cryptography's canonical type instead of hand-maintaining a copy:

from cryptography.hazmat.primitives.asymmetric.types import PrivateKeyTypes
PRIVATE_KEY_TYPES = PrivateKeyTypes

This is exactly what load_der_private_key / load_pem_private_key are declared to return — both are annotated -> PrivateKeyTypes in cryptography's type stub hazmat/bindings/_rust/openssl/keys.pyi — and parse_*_private_key simply returns those calls, so the annotations now match precisely what is produced. It also removes the deprecated attribute reference and stays in sync with cryptography going forward.

PRIVATE_KEY_TYPES is only ever used as a type annotation (no runtime isinstance), so there is no runtime behaviour change. Lint (ruff/mypy/pyright, 100% type completeness) and the full unit suite (376 passed) are green.

🤖 Generated with Claude Code

@c4rlo c4rlo changed the title Alias cryptography's PrivateKeyTypes for PRIVATE_KEY_TYPES Fix FFDH CryptographyDeprecationWarning raised when importing spiffe Aug 7, 2026
`certificate_utils.PRIVATE_KEY_TYPES` was a hand-rolled `Union` that duplicated
cryptography's `PrivateKeyTypes`. It was added as `_PRIVATE_KEY_TYPES` in 77537c6,
and `dh.DHPrivateKey` was added to it later in 95b2e32. That copy had two problems:

- It referenced `dh.DHPrivateKey`. cryptography 50.0.0 deprecated Diffie-Hellman
  over finite fields (FFDH)
  (https://cryptography.io/en/latest/changelog/#v50-0-0), so touching that
  attribute now emits a `CryptographyDeprecationWarning` at module import time.
  Because that warning subclasses `UserWarning` (not `DeprecationWarning`), a
  strict `filterwarnings = error` policy turns importing `spiffe` into an
  ImportError before any code runs. This became reachable once the cryptography
  upper bound was removed in 429c165, relaxing the `>=46,<48` pin from 8deed17 to
  `>=46` and letting cryptography 50 resolve.
- The copy had drifted from cryptography and no longer matched what the loaders
  return (e.g. it predated the ML-DSA/ML-KEM key types).

Alias `cryptography.hazmat.primitives.asymmetric.types.PrivateKeyTypes` directly.
That is exactly what `load_der_private_key`/`load_pem_private_key` are declared to
return: both are annotated `-> PrivateKeyTypes` in cryptography's type stub
`hazmat/bindings/_rust/openssl/keys.pyi`
(https://github.com/pyca/cryptography/blob/50.0.0/src/cryptography/hazmat/bindings/_rust/openssl/keys.pyi#L13-L26).
The `parse_*_private_key` wrappers just return those calls, so they now annotate
precisely what they produce. The alias also avoids the deprecated attribute
reference and stays in sync with cryptography. `PRIVATE_KEY_TYPES` is only used as
a type annotation, so there is no runtime behaviour change.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Carlo Teubner <cteubner1@bloomberg.net>
@c4rlo
c4rlo force-pushed the fix-private-key-types-ffdh-warning branch from 8873d12 to 40e5ed7 Compare August 7, 2026 11:40
@maxlambrecht

Copy link
Copy Markdown
Collaborator

Hi @c4rlo, thanks for this. Is it ready for review?

@c4rlo

c4rlo commented Aug 7, 2026

Copy link
Copy Markdown
Contributor Author

Hi, yes it's ready for review, thanks!

I've still got it in draft because I still need to get internal signoff to contribute code to this project, which I hope won't take long to arrive. So please don't merge it just yet, if that's okay.

@maxlambrecht

Copy link
Copy Markdown
Collaborator

Hi, yes it's ready for review, thanks!

I've still got it in draft because I still need to get internal signoff to contribute code to this project, which I hope won't take long to arrive. So please don't merge it just yet, if that's okay.

Sounds good, thanks! I'll review it and hold off on merging.

@maxlambrecht maxlambrecht left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @c4rlo!

@c4rlo
c4rlo marked this pull request as ready for review August 7, 2026 16:59
@c4rlo

c4rlo commented Aug 7, 2026

Copy link
Copy Markdown
Contributor Author

Thank you, good to merge from my end!

I'd much appreciate it if this could make its way into a new release soon.

@maxlambrecht
maxlambrecht merged commit e9ea503 into HewlettPackard:main Aug 8, 2026
6 checks passed
@maxlambrecht

maxlambrecht commented Aug 8, 2026

Copy link
Copy Markdown
Collaborator

Released in v0.3.1. Thanks again @c4rlo!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants