Skip to content

Commit

Permalink
Migrate certificate to declarative modules API (#11237)
Browse files Browse the repository at this point in the history
refs #11158
  • Loading branch information
alex authored Jul 10, 2024
1 parent 0c2467a commit d3eda71
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 34 deletions.
12 changes: 5 additions & 7 deletions src/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ mod _rust {

#[pyo3::pymodule]
mod x509 {
#[pymodule_export]
use crate::x509::certificate::{
create_x509_certificate, load_der_x509_certificate, load_pem_x509_certificate,
load_pem_x509_certificates, Certificate,
};
#[pymodule_export]
use crate::x509::common::{encode_extension_value, encode_name_bytes};
#[pymodule_export]
Expand All @@ -127,13 +132,6 @@ mod _rust {
PolicyBuilder, PyClientVerifier, PyServerVerifier, PyStore, PyVerifiedClient,
VerificationError,
};

#[pymodule_init]
fn init(x509_mod: &pyo3::Bound<'_, pyo3::types::PyModule>) -> pyo3::PyResult<()> {
crate::x509::certificate::add_to_module(x509_mod)?;

Ok(())
}
}

#[pyo3::pymodule]
Expand Down
31 changes: 4 additions & 27 deletions src/rust/src/x509/certificate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use cryptography_x509::extensions::{
use cryptography_x509::extensions::{Extension, SubjectAlternativeName};
use cryptography_x509::{common, oid};
use cryptography_x509_verification::ops::CryptoOps;
use pyo3::types::{PyAnyMethods, PyListMethods, PyModuleMethods};
use pyo3::types::{PyAnyMethods, PyListMethods};
use pyo3::{IntoPy, ToPyObject};

use crate::asn1::{
Expand Down Expand Up @@ -366,7 +366,7 @@ fn cert_version(

#[pyo3::pyfunction]
#[pyo3(signature = (data, backend=None))]
fn load_pem_x509_certificate(
pub(crate) fn load_pem_x509_certificate(
py: pyo3::Python<'_>,
data: &[u8],
backend: Option<pyo3::Bound<'_, pyo3::PyAny>>,
Expand All @@ -388,7 +388,7 @@ fn load_pem_x509_certificate(
}

#[pyo3::pyfunction]
fn load_pem_x509_certificates(
pub(crate) fn load_pem_x509_certificates(
py: pyo3::Python<'_>,
data: &[u8],
) -> CryptographyResult<Vec<Certificate>> {
Expand Down Expand Up @@ -886,7 +886,7 @@ pub(crate) fn time_from_datetime(dt: asn1::DateTime) -> CryptographyResult<commo
}

#[pyo3::pyfunction]
fn create_x509_certificate(
pub(crate) fn create_x509_certificate(
py: pyo3::Python<'_>,
builder: &pyo3::Bound<'_, pyo3::PyAny>,
private_key: &pyo3::Bound<'_, pyo3::PyAny>,
Expand Down Expand Up @@ -975,26 +975,3 @@ pub(crate) fn set_bit(vals: &mut [u8], n: usize, set: bool) {
vals[idx] |= v;
}
}

pub(crate) fn add_to_module(module: &pyo3::Bound<'_, pyo3::types::PyModule>) -> pyo3::PyResult<()> {
module.add_function(pyo3::wrap_pyfunction_bound!(
load_der_x509_certificate,
module
)?)?;
module.add_function(pyo3::wrap_pyfunction_bound!(
load_pem_x509_certificate,
module
)?)?;
module.add_function(pyo3::wrap_pyfunction_bound!(
load_pem_x509_certificates,
module
)?)?;
module.add_function(pyo3::wrap_pyfunction_bound!(
create_x509_certificate,
module
)?)?;

module.add_class::<Certificate>()?;

Ok(())
}

0 comments on commit d3eda71

Please sign in to comment.