Skip to content

Commit 39fe001

Browse files
committed
Refactor cert resolver
1 parent 8c13fd4 commit 39fe001

File tree

1 file changed

+17
-27
lines changed

1 file changed

+17
-27
lines changed

crates/stackable-webhook/src/tls/cert_resolver.rs

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use tokio::sync::mpsc;
77
use tokio_rustls::rustls::{
88
crypto::CryptoProvider, server::ResolvesServerCert, sign::CertifiedKey,
99
};
10-
use x509_cert::{Certificate, certificate::CertificateInner};
10+
use x509_cert::Certificate;
1111

1212
use super::{WEBHOOK_CA_LIFETIME, WEBHOOK_CERTIFICATE_LIFETIME};
1313

@@ -21,12 +21,6 @@ pub enum CertificateResolverError {
2121
#[snafu(display("failed to generate ECDSA signing key"))]
2222
GenerateEcdsaSigningKey { source: ecdsa::Error },
2323

24-
#[snafu(display("failed to generate new certificate"))]
25-
GenerateNewCertificate {
26-
#[snafu(source(from(CertificateResolverError, Box::new)))]
27-
source: Box<CertificateResolverError>,
28-
},
29-
3024
#[snafu(display("failed to create CA to generate and sign webhook leaf certificate"))]
3125
CreateCertificateAuthority { source: stackable_certs::ca::Error },
3226

@@ -74,11 +68,8 @@ impl CertificateResolver {
7468
cert_tx: mpsc::Sender<Certificate>,
7569
) -> Result<Self> {
7670
let subject_alterative_dns_names = Arc::new(subject_alterative_dns_names);
77-
let (cert, certified_key) = Self::generate_new_cert(subject_alterative_dns_names.clone())
78-
.await
79-
.context(GenerateNewCertificateSnafu)?;
80-
81-
Self::send_certificate_to_channel(cert, &cert_tx).await?;
71+
let certified_key =
72+
Self::generate_new_cert(&cert_tx, subject_alterative_dns_names.clone()).await?;
8273

8374
Ok(Self {
8475
subject_alterative_dns_names,
@@ -88,27 +79,29 @@ impl CertificateResolver {
8879
}
8980

9081
pub async fn rotate_certificate(&self) -> Result<()> {
91-
let (cert, certified_key) =
92-
Self::generate_new_cert(self.subject_alterative_dns_names.clone())
93-
.await
94-
.context(GenerateNewCertificateSnafu)?;
82+
let certified_key =
83+
Self::generate_new_cert(&self.cert_tx, self.subject_alterative_dns_names.clone())
84+
.await?;
9585

9686
// TODO: Sign the new cert somehow with the old cert. See https://github.com/stackabletech/decisions/issues/56
97-
98-
Self::send_certificate_to_channel(cert, &self.cert_tx).await?;
9987
self.current_certified_key.store(certified_key);
10088

10189
Ok(())
10290
}
10391

92+
/// Creates a new certificate and returns the certified key.
93+
///
94+
/// The certificate is send to the passed `cert_tx`.
95+
///
10496
/// FIXME: This should *not* construct a CA cert and cert, but only a cert!
10597
/// This needs some changes in stackable-certs though.
10698
/// See [the relevant decision](https://github.com/stackabletech/decisions/issues/56)
10799
async fn generate_new_cert(
100+
cert_tx: &mpsc::Sender<Certificate>,
108101
subject_alterative_dns_names: Arc<Vec<String>>,
109-
) -> Result<(Certificate, Arc<CertifiedKey>)> {
102+
) -> Result<Arc<CertifiedKey>> {
110103
// The certificate generations can take a while, so we use `spawn_blocking`
111-
tokio::task::spawn_blocking(move || {
104+
let (cert, certified_key) = tokio::task::spawn_blocking(move || {
112105
let tls_provider =
113106
CryptoProvider::get_default().context(NoDefaultCryptoProviderInstalledSnafu)?;
114107

@@ -142,17 +135,14 @@ impl CertificateResolver {
142135
))
143136
})
144137
.await
145-
.context(TokioSpawnBlockingSnafu)?
146-
}
138+
.context(TokioSpawnBlockingSnafu)??;
147139

148-
async fn send_certificate_to_channel(
149-
cert: CertificateInner,
150-
cert_tx: &mpsc::Sender<Certificate>,
151-
) -> Result<()> {
152140
cert_tx
153141
.send(cert)
154142
.await
155-
.map_err(|_err| CertificateResolverError::SendCertificateToChannel)
143+
.map_err(|_err| CertificateResolverError::SendCertificateToChannel)?;
144+
145+
Ok(certified_key)
156146
}
157147
}
158148

0 commit comments

Comments
 (0)