Skip to content

Commit 825c07a

Browse files
committed
fix: make cert compile
1 parent 44c8667 commit 825c07a

File tree

2 files changed

+43
-41
lines changed

2 files changed

+43
-41
lines changed

src/api/metrics.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,11 @@ where
100100
I: Filter<Extract = (R,), Error = Rejection> + Clone + Send + Sync + 'static,
101101
C: Into<Option<&'static str>>,
102102
{
103-
let config = config.into().unwrap_or("");
103+
let config = match config.into() {
104+
Some("") => panic!("Empty str not allowed as param"),
105+
Some(config) => config,
106+
None => "",
107+
};
104108

105109
move |filter: I| {
106110
warp::filters::path::full()

src/cert.rs

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
use acme_lib::order::NewOrder;
12
use acme_lib::{create_p384_key, Directory, DirectoryUrl};
23
use anyhow::{anyhow, Result};
34
use sqlx::{Executor, FromRow, PgPool, Postgres};
45
use std::time::Duration;
6+
use tokio::runtime::Handle;
57
use tokio::time::Interval;
68
use tracing::{error, info, Instrument, Span};
79
use uuid::Uuid;
@@ -186,7 +188,7 @@ impl CertManager {
186188
info!("Skipping Interval");
187189
continue;
188190
}
189-
if let Err(e) = self.test().await {
191+
if let Err(e) = self.manage().await {
190192
error!("{}", e);
191193
continue;
192194
}
@@ -200,72 +202,68 @@ impl CertManager {
200202
Ok(())
201203
}
202204

203-
async fn test(&self) -> Result<()> {
205+
async fn manage(&self) -> Result<()> {
204206
// maybe context is not needed here
205-
let mut memory_cert = match CertFacade::start(&self.pool).await? {
207+
let memory_cert = match CertFacade::start(&self.pool).await? {
206208
Some(memory_cert) => memory_cert,
207209
None => return Ok(()),
208210
};
209211

210212
// todo: improve
211-
let mut domain = DomainFacade::find_by_id(&self.pool, &memory_cert.domain)
213+
let domain = DomainFacade::find_by_id(&self.pool, &memory_cert.domain)
212214
.await?
213215
.ok_or_else(|| anyhow!("Could not find domain: {}", &memory_cert.domain))?;
214216

215217
let directory = self.directory.clone();
216-
let mut order = tokio::task::spawn_blocking(move || {
218+
let pool = self.pool.clone();
219+
220+
let cert = tokio::task::spawn_blocking(move || {
217221
let account = directory.account("[email protected]")?;
218-
account.new_order("acme.wehrli.ml", &[])
222+
let order = account.new_order("acme.wehrli.ml", &[])?;
223+
CertManager::validate(memory_cert, domain, order, &pool)
219224
})
220225
.await??;
221226

222-
let mut auths = order.authorizations()?;
223-
let call = auths
224-
.pop()
225-
.ok_or_else(|| anyhow!("couldn't unpack auths"))?
226-
.dns_challenge();
227-
let proof = call.dns_proof();
227+
CertFacade::stop(&self.pool, cert).await?;
228228

229-
domain.txt = Some(proof);
230-
DomainFacade::update_domain(&self.pool, &domain).await?;
231-
232-
//error handling
233-
tokio::task::spawn_blocking(move || call.validate(5000)).await??;
229+
Ok(())
230+
}
234231

235-
let mut n = 0;
232+
fn validate(
233+
mut memory_cert: Cert,
234+
mut domain: Domain,
235+
mut order: NewOrder<DatabasePersist>,
236+
pool: &PgPool,
237+
) -> Result<Cert> {
236238
let ord_csr = loop {
237-
if n > 10 {
238-
return Err(anyhow!("Timed out {:?}", order.api_order()));
239-
}
240-
let (ord_csr, take) = tokio::task::spawn_blocking(move || match order.refresh() {
241-
Err(e) => Err(e),
242-
Ok(_) => Ok((order.confirm_validations(), order)),
243-
})
244-
.await??;
245-
246-
if let Some(ord_csr) = ord_csr {
239+
if let Some(ord_csr) = order.confirm_validations() {
247240
break ord_csr;
248241
}
249242

250-
order = take;
251-
tokio::time::delay_for(Duration::from_secs(1)).await;
252-
n += 1;
243+
let chall = order
244+
.authorizations()?
245+
.iter()
246+
.next()
247+
.ok_or_else(|| anyhow!("couldn't unpack auths"))?
248+
.dns_challenge();
249+
250+
domain.txt = Some(chall.dns_proof());
251+
let update = DomainFacade::update_domain(pool, &domain);
252+
Handle::current().block_on(update)?;
253+
254+
order.refresh()?;
253255
};
254256

255-
let cert = tokio::task::spawn_blocking(move || {
256-
let private = create_p384_key();
257-
let ord_crt = ord_csr.finalize_pkey(private, 5000)?;
258-
ord_crt.download_and_save_cert()
259-
})
260-
.await??;
257+
let private = create_p384_key();
258+
let ord_crt = ord_csr.finalize_pkey(private, 5000)?;
259+
let cert = ord_crt.download_and_save_cert()?;
261260

262261
let private = cert.private_key().to_string();
263262
let cert = cert.certificate().to_string();
264263

265-
memory_cert.cert = Some(cert);
266264
memory_cert.private = Some(private);
267-
CertFacade::stop(&self.pool, memory_cert).await?;
265+
memory_cert.cert = Some(cert);
268266

269-
Ok(())
267+
Ok(memory_cert)
270268
}
271269
}

0 commit comments

Comments
 (0)