Skip to content

Commit

Permalink
Merge pull request #844 from DefGuard/dev
Browse files Browse the repository at this point in the history
Merge dev to main
  • Loading branch information
t-aleksander authored Nov 5, 2024
2 parents d057a54 + 9de1101 commit 587ab06
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
12 changes: 12 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,18 @@ pub struct DefGuardConfig {
#[command(subcommand)]
#[serde(skip_serializing)]
pub cmd: Option<Command>,

#[arg(long, env = "DEFGUARD_CHECK_PERIOD", default_value = "12h")]
#[serde(skip_serializing)]
pub check_period: Duration,

#[arg(long, env = "DEFGUARD_CHECK_PERIOD_NO_LICENSE", default_value = "24h")]
#[serde(skip_serializing)]
pub check_period_no_license: Duration,

#[arg(long, env = "DEFGUARD_CHECK_RENEWAL_WINDOW", default_value = "1h")]
#[serde(skip_serializing)]
pub check_period_renewal_window: Duration,
}

#[derive(Clone, Debug, Subcommand)]
Expand Down
25 changes: 7 additions & 18 deletions src/enterprise/license.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use sqlx::{error::Error as SqlxError, PgPool};
use thiserror::Error;
use tokio::time::sleep;

use crate::{db::Settings, VERSION};
use crate::{db::Settings, server_config, VERSION};

const LICENSE_SERVER_URL: &str = "https://pkgs.defguard.net/api/license/renew";

Expand Down Expand Up @@ -532,24 +532,13 @@ pub fn update_cached_license(key: Option<&str>) -> Result<(), LicenseError> {

Ok(())
}

/// Amount of time before the license expiry date we should start the renewal attempts.
const RENEWAL_TIME: TimeDelta = TimeDelta::hours(24);

/// Maximum amount of time a license can be over its expiry date.
const MAX_OVERDUE_TIME: TimeDelta = TimeDelta::days(14);

/// Periodic license check task
const CHECK_PERIOD: Duration = Duration::from_secs(12 * 60 * 60);

/// Periodic license check task for the case when no license is present
const CHECK_PERIOD_NO_LICENSE: Duration = Duration::from_secs(24 * 60 * 60);

/// Periodic license check task for the case when the license is about to expire
const CHECK_PERIOD_RENEWAL_WINDOW: Duration = Duration::from_secs(60 * 60);

pub async fn run_periodic_license_check(pool: PgPool) -> Result<(), LicenseError> {
let mut check_period: Duration = CHECK_PERIOD;
let config = server_config();
let mut check_period: Duration = *config.check_period;
info!(
"Starting periodic license renewal check every {}",
format_duration(check_period)
Expand All @@ -559,7 +548,7 @@ pub async fn run_periodic_license_check(pool: PgPool) -> Result<(), LicenseError
// Check if the license is present in the mutex, if not skip the check
if get_cached_license().is_none() {
debug!("No license found, skipping license check");
sleep(CHECK_PERIOD_NO_LICENSE).await;
sleep(*config.check_period_no_license).await;
continue;
}

Expand All @@ -578,7 +567,7 @@ pub async fn run_periodic_license_check(pool: PgPool) -> Result<(), LicenseError
// check if we are pass the maximum expiration date, after which we don't
// want to try to renew the license anymore
if license.is_max_overdue() {
check_period = CHECK_PERIOD;
check_period = *config.check_period;
warn!("Your license has expired and reached its maximum overdue date, please contact sales at sales<at>defguard.net");
debug!("Changing check period to {}", format_duration(check_period));
false
Expand Down Expand Up @@ -607,13 +596,13 @@ pub async fn run_periodic_license_check(pool: PgPool) -> Result<(), LicenseError

if requires_renewal {
info!("License requires renewal, renewing license...");
check_period = CHECK_PERIOD_RENEWAL_WINDOW;
check_period = *config.check_period_renewal_window;
debug!("Changing check period to {}", format_duration(check_period));
match renew_license(&pool).await {
Ok(new_license_key) => match save_license_key(&pool, &new_license_key).await {
Ok(()) => {
update_cached_license(Some(&new_license_key))?;
check_period = CHECK_PERIOD;
check_period = *config.check_period;
debug!("Changing check period to {}", format_duration(check_period));
info!("Successfully renewed the license");
}
Expand Down

0 comments on commit 587ab06

Please sign in to comment.