1
+ use acme_lib:: order:: NewOrder ;
1
2
use acme_lib:: { create_p384_key, Directory , DirectoryUrl } ;
2
3
use anyhow:: { anyhow, Result } ;
3
4
use sqlx:: { Executor , FromRow , PgPool , Postgres } ;
4
5
use std:: time:: Duration ;
6
+ use tokio:: runtime:: Handle ;
5
7
use tokio:: time:: Interval ;
6
8
use tracing:: { error, info, Instrument , Span } ;
7
9
use uuid:: Uuid ;
@@ -186,7 +188,7 @@ impl CertManager {
186
188
info ! ( "Skipping Interval" ) ;
187
189
continue ;
188
190
}
189
- if let Err ( e) = self . test ( ) . await {
191
+ if let Err ( e) = self . manage ( ) . await {
190
192
error ! ( "{}" , e) ;
191
193
continue ;
192
194
}
@@ -200,72 +202,68 @@ impl CertManager {
200
202
Ok ( ( ) )
201
203
}
202
204
203
- async fn test ( & self ) -> Result < ( ) > {
205
+ async fn manage ( & self ) -> Result < ( ) > {
204
206
// 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 ? {
206
208
Some ( memory_cert) => memory_cert,
207
209
None => return Ok ( ( ) ) ,
208
210
} ;
209
211
210
212
// 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 )
212
214
. await ?
213
215
. ok_or_else ( || anyhow ! ( "Could not find domain: {}" , & memory_cert. domain) ) ?;
214
216
215
217
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 || {
217
221
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)
219
224
} )
220
225
. await ??;
221
226
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 ?;
228
228
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
+ }
234
231
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 > {
236
238
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 ( ) {
247
240
break ord_csr;
248
241
}
249
242
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 ( ) ?;
253
255
} ;
254
256
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 ( ) ?;
261
260
262
261
let private = cert. private_key ( ) . to_string ( ) ;
263
262
let cert = cert. certificate ( ) . to_string ( ) ;
264
263
265
- memory_cert. cert = Some ( cert) ;
266
264
memory_cert. private = Some ( private) ;
267
- CertFacade :: stop ( & self . pool , memory_cert ) . await ? ;
265
+ memory_cert . cert = Some ( cert ) ;
268
266
269
- Ok ( ( ) )
267
+ Ok ( memory_cert )
270
268
}
271
269
}
0 commit comments