-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: make background service more simplified
- Loading branch information
Showing
7 changed files
with
183 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,17 +14,11 @@ | |
|
||
use super::{Certificate, LOG_CATEGORY}; | ||
use crate::proxy::get_certificate_info_list; | ||
use crate::service::{CommonServiceTask, ServiceTask}; | ||
use crate::service::SimpleServiceTaskFuture; | ||
use crate::util; | ||
use crate::webhook; | ||
use async_trait::async_trait; | ||
use std::time::Duration; | ||
use tracing::warn; | ||
|
||
struct ValidityChecker { | ||
time_offset: i64, | ||
} | ||
|
||
// Verify the validity period of tls certificate, | ||
// include not after and not before. | ||
fn validity_check( | ||
|
@@ -57,51 +51,31 @@ fn validity_check( | |
Ok(()) | ||
} | ||
|
||
#[async_trait] | ||
impl ServiceTask for ValidityChecker { | ||
async fn run(&self) -> Option<bool> { | ||
let certificate_info_list = get_certificate_info_list(); | ||
if let Err(message) = | ||
validity_check(&certificate_info_list, self.time_offset) | ||
{ | ||
// certificate will be expired | ||
warn!(category = LOG_CATEGORY, message); | ||
webhook::send(webhook::SendNotificationParams { | ||
level: webhook::NotificationLevel::Warn, | ||
category: webhook::NotificationCategory::TlsValidity, | ||
msg: message, | ||
..Default::default() | ||
}); | ||
} | ||
None | ||
async fn do_validity_check(count: u32) -> Result<(), String> { | ||
// Add 1 every loop | ||
let offset = 24 * 60; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
vicanso
Author
Owner
|
||
if count % offset != 0 { | ||
return Ok(()); | ||
} | ||
fn description(&self) -> String { | ||
let mut names = vec![]; | ||
for (name, _) in get_certificate_info_list().iter() { | ||
if !names.contains(name) { | ||
names.push(name.clone()); | ||
} | ||
} | ||
|
||
let offset_human: humantime::Duration = | ||
Duration::from_secs(self.time_offset as u64).into(); | ||
format!("ValidityChecker: {names:?}, {offset_human}") | ||
let certificate_info_list = get_certificate_info_list(); | ||
let time_offset = 7 * 24 * 3600_i64; | ||
if let Err(message) = validity_check(&certificate_info_list, time_offset) { | ||
// certificate will be expired | ||
warn!(category = LOG_CATEGORY, message); | ||
webhook::send(webhook::SendNotificationParams { | ||
level: webhook::NotificationLevel::Warn, | ||
category: webhook::NotificationCategory::TlsValidity, | ||
msg: message, | ||
..Default::default() | ||
}); | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// Create a tls certificate validity checker service, | ||
/// if the certificate will be expired or not valid, | ||
/// it will send webhook notificateion message. | ||
pub fn new_tls_validity_service() -> CommonServiceTask { | ||
let checker = ValidityChecker { | ||
// cert will be expired 7 days later | ||
time_offset: 7 * 24 * 3600_i64, | ||
}; | ||
CommonServiceTask::new( | ||
// check interval: one day | ||
Duration::from_secs(24 * 60 * 60), | ||
checker, | ||
) | ||
pub fn new_certificate_validity_service() -> (String, SimpleServiceTaskFuture) { | ||
let task: SimpleServiceTaskFuture = | ||
Box::new(|count: u32| Box::pin(do_validity_check(count))); | ||
("validityChecker".to_string(), task) | ||
} | ||
|
||
#[cfg(test)] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
@vicanso I think you will want to run the check every 12 hours but check if the certificate will expire in 24 hours so that you don't have expired certificates that expire before the next time your run the check.