Skip to content

Commit fd19a03

Browse files
committed
Add builder_with_rsa and builder_with_ecdsa helpers
1 parent 1322779 commit fd19a03

File tree

5 files changed

+45
-9
lines changed

5 files changed

+45
-9
lines changed

crates/stackable-certs/src/ca/ca_builder.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@ where
8181
/// .build()
8282
/// .expect("failed to build CA");
8383
/// ```
84+
///
85+
/// Instead of using generics to determine the algorithm to use you can also use [`CertificateAuthority::builder_with_rsa`]
86+
/// or [`CertificateAuthority::builder_with_ecdsa`] instead:
87+
///
88+
/// ```no_run
89+
/// use stackable_certs::{
90+
/// keys::ecdsa, ca::CertificateAuthority,
91+
/// };
92+
///
93+
/// let ca = CertificateAuthority::builder_with_ecdsa()
94+
/// .build()
95+
/// .expect("failed to build CA");
96+
/// ```
8497
#[derive(Builder)]
8598
#[builder(start_fn = start_builder, finish_fn = finish_builder)]
8699
pub struct CertificateAuthorityBuilder<'a, SKP>
@@ -211,11 +224,11 @@ mod tests {
211224
use x509_cert::certificate::TbsCertificateInner;
212225

213226
use super::*;
214-
use crate::keys::{ecdsa, rsa};
227+
use crate::keys::rsa;
215228

216229
#[test]
217230
fn minimal_ca() {
218-
let ca: CertificateAuthority<ecdsa::SigningKey> = CertificateAuthority::builder()
231+
let ca = CertificateAuthority::builder_with_ecdsa()
219232
.build()
220233
.expect("failed to build CA");
221234

crates/stackable-certs/src/ca/mod.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ use std::fmt::Debug;
44

55
use x509_cert::{Certificate, name::RdnSequence, spki::EncodePublicKey};
66

7-
use crate::{CertificatePair, keys::CertificateKeypair};
7+
use crate::{
8+
CertificatePair,
9+
keys::{CertificateKeypair, ecdsa, rsa},
10+
};
811

912
mod ca_builder;
1013
mod consts;
@@ -13,8 +16,10 @@ pub use ca_builder::*;
1316
pub use consts::*;
1417
pub use k8s::*;
1518

16-
/// A certificate authority (CA) which is used to generate and sign
17-
/// intermediate or leaf certificates.
19+
/// A certificate authority (CA) which is used to generate and sign intermediate or leaf
20+
/// certificates.
21+
///
22+
/// Use [`CertificateAuthorityBuilder`] to create new certificates.
1823
#[derive(Debug)]
1924
pub struct CertificateAuthority<SK>
2025
where
@@ -33,6 +38,7 @@ where
3338
Self { certificate_pair }
3439
}
3540

41+
/// Use this function in combination with [`CertificateAuthorityBuilder`] to create new CAs.
3642
pub fn builder() -> CertificateAuthorityBuilderBuilder<'static, SK> {
3743
CertificateAuthorityBuilder::start_builder()
3844
}
@@ -49,3 +55,17 @@ where
4955
&self.ca_cert().tbs_certificate.issuer
5056
}
5157
}
58+
59+
impl CertificateAuthority<rsa::SigningKey> {
60+
/// Same as [`Self::builder`], but enforces the RSA algorithm for key creation.
61+
pub fn builder_with_rsa() -> CertificateAuthorityBuilderBuilder<'static, rsa::SigningKey> {
62+
Self::builder()
63+
}
64+
}
65+
66+
impl CertificateAuthority<ecdsa::SigningKey> {
67+
/// Same as [`Self::builder`], but enforces the ecdsa algorithm for key creation.
68+
pub fn builder_with_ecdsa() -> CertificateAuthorityBuilderBuilder<'static, ecdsa::SigningKey> {
69+
Self::builder()
70+
}
71+
}

crates/stackable-certs/src/cert_builder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,11 @@ mod tests {
267267
};
268268

269269
use super::*;
270-
use crate::keys::{ecdsa, rsa};
270+
use crate::keys::rsa;
271271

272272
#[test]
273273
fn minimal_certificate() {
274-
let ca = CertificateAuthority::<ecdsa::SigningKey>::builder()
274+
let ca = CertificateAuthority::builder_with_ecdsa()
275275
.build()
276276
.expect("failed to build CA");
277277

@@ -292,7 +292,7 @@ mod tests {
292292

293293
#[test]
294294
fn customized_certificate() {
295-
let ca = CertificateAuthority::builder()
295+
let ca = CertificateAuthority::builder_with_rsa()
296296
.build()
297297
.expect("failed to build CA");
298298

crates/stackable-certs/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ impl<E: snafu::Error + std::cmp::PartialEq> PartialEq for CertificatePairError<E
9999
/// internally to store the signing key pair which is used to sign the CA
100100
/// itself (self-signed) and all child leaf certificates. Leaf certificates on
101101
/// the other hand use this to store the bound keypair.
102+
///
103+
/// Use [`CertificateBuilder`] to create new certificates.
102104
#[derive(Debug)]
103105
pub struct CertificatePair<S>
104106
where
@@ -121,6 +123,7 @@ where
121123
}
122124
}
123125

126+
/// Use this function in combination with [`CertificateBuilder`] to create new CAs.
124127
pub fn builder() -> CertificateBuilderBuilder<'static, S> {
125128
CertificateBuilder::start_builder()
126129
}

crates/stackable-webhook/src/tls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl TlsServer {
109109
// blocked.
110110
// See https://docs.rs/tokio/latest/tokio/task/fn.spawn_blocking.html
111111
let task = tokio::task::spawn_blocking(move || {
112-
let ca = CertificateAuthority::<ecdsa::SigningKey>::builder()
112+
let ca = CertificateAuthority::builder_with_ecdsa()
113113
.build()
114114
.context(CreateCertificateAuthoritySnafu)?;
115115

0 commit comments

Comments
 (0)