From 82895eddfe3a582a02b906d25c811e91364a149d Mon Sep 17 00:00:00 2001 From: RisingOpsNinja <175097282+RisingOpsNinja@users.noreply.github.com.> Date: Wed, 4 Sep 2024 11:37:31 +0200 Subject: [PATCH] Allow to set sequoia StandardPolicy with a toml configuration file using the sequoia_policy_config crate. --- backend/Cargo.toml | 1 + backend/src/settings.rs | 14 +++++++++++++- backend/src/utils.rs | 3 ++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/backend/Cargo.toml b/backend/Cargo.toml index cf317ff..8228ad0 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -20,3 +20,4 @@ tokio = { version = "1.27.0", features = ["time"] } toml = "0.7.3" url = { version = "2.3.1", features = ["serde"] } zbase32 = "0.1.2" +sequoia-policy-config = { version = "0.6" } diff --git a/backend/src/settings.rs b/backend/src/settings.rs index 3b330f1..8ec2473 100644 --- a/backend/src/settings.rs +++ b/backend/src/settings.rs @@ -2,6 +2,7 @@ use lettre::{transport::smtp::authentication::Credentials, AsyncSmtpTransport, T use log::{debug, error}; use once_cell::sync::Lazy; use sequoia_openpgp::policy::StandardPolicy; +use sequoia_policy_config::ConfiguredStandardPolicy; use serde::{Deserialize, Serialize}; use std::path::PathBuf; use url::Url; @@ -93,8 +94,19 @@ fn get_mailer() -> AsyncSmtpTransport { .build() } +fn get_policy<'a>() -> StandardPolicy<'a> { + let mut p = ConfiguredStandardPolicy::new(); + + match p.parse_default_config() { + Ok(_) => {}, + Err(e) => error!("{e}"), + } + + p.build() +} + pub const ERROR_TEXT: &str = "An error occoured:"; -pub const POLICY: &StandardPolicy = &StandardPolicy::new(); +pub static POLICY: Lazy = Lazy::new(get_policy); pub const ROOT_FOLDER: &str = "data"; pub static SETTINGS: Lazy = Lazy::new(get_settings); pub static MAILER: Lazy> = Lazy::new(get_mailer); diff --git a/backend/src/utils.rs b/backend/src/utils.rs index 9a8f94e..cd2238a 100644 --- a/backend/src/utils.rs +++ b/backend/src/utils.rs @@ -4,6 +4,7 @@ use crate::log_err; use crate::settings::Variant; use crate::settings::ROOT_FOLDER; use crate::settings::SETTINGS; +use crate::settings::POLICY; use actix_web::ResponseError; use actix_web::{ @@ -26,7 +27,7 @@ use std::{ }; pub fn validate_cert(cert: &Cert) -> Result { - let validcert = match log_err!(cert.with_policy(crate::settings::POLICY, None), debug) { + let validcert = match log_err!(cert.with_policy(&*POLICY, None), debug) { Ok(validcert) => validcert, Err(_) => Err(SpecialErrors::InvalidCert)?, };