From 0f54bf73ef736ecafcd26600ef715661eede2e15 Mon Sep 17 00:00:00 2001 From: Tim Bruijnzeels Date: Thu, 10 Dec 2020 15:27:50 +0100 Subject: [PATCH] Fix test environment variables. --- src/bin/krill.rs | 5 ++--- src/commons/bgp/analyser.rs | 6 +++--- src/constants.rs | 32 +++++++++++++++++++++++++++----- src/daemon/ca/certauth.rs | 3 ++- src/daemon/config.rs | 4 ---- src/daemon/scheduler.rs | 15 +++++++-------- src/pubd/repository.rs | 6 ++++-- src/test.rs | 10 ++++------ 8 files changed, 49 insertions(+), 32 deletions(-) diff --git a/src/bin/krill.rs b/src/bin/krill.rs index 321c9858..30ec0204 100644 --- a/src/bin/krill.rs +++ b/src/bin/krill.rs @@ -8,10 +8,9 @@ extern crate krill; -use std::env; use std::sync::Arc; -use krill::constants::KRILL_ENV_TESTBED_ENABLED; +use krill::constants::testbed_enabled; use krill::daemon::http::server; use krill::daemon::krillserver::KrillMode; @@ -19,7 +18,7 @@ use krill::daemon::krillserver::KrillMode; async fn main() { match server::parse_config() { Ok(config) => { - let mode = if env::var(KRILL_ENV_TESTBED_ENABLED).is_ok() { + let mode = if testbed_enabled() { KrillMode::Testbed } else { KrillMode::Ca diff --git a/src/commons/bgp/analyser.rs b/src/commons/bgp/analyser.rs index ccb2c9af..5b333db8 100644 --- a/src/commons/bgp/analyser.rs +++ b/src/commons/bgp/analyser.rs @@ -1,4 +1,4 @@ -use std::{env, fmt}; +use std::fmt; use chrono::Duration; use tokio::sync::RwLock; @@ -11,7 +11,7 @@ use crate::commons::bgp::{ BgpAnalysisEntry, BgpAnalysisReport, BgpAnalysisState, BgpAnalysisSuggestion, IpRange, RisDumpError, RisDumpLoader, ValidatedAnnouncement, }; -use crate::constants::{BGP_RIS_REFRESH_MINUTES, KRILL_ENV_TEST_ANN}; +use crate::constants::{test_announcements_enabled, BGP_RIS_REFRESH_MINUTES}; //------------ BgpAnalyser ------------------------------------------------- @@ -23,7 +23,7 @@ pub struct BgpAnalyser { impl BgpAnalyser { pub fn new(ris_enabled: bool, ris_v4_uri: &str, ris_v6_uri: &str) -> Self { - if env::var(KRILL_ENV_TEST_ANN).is_ok() { + if test_announcements_enabled() { Self::with_test_announcements() } else { let dumploader = if ris_enabled { diff --git a/src/constants.rs b/src/constants.rs index dc61297a..36c71989 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -13,17 +13,39 @@ pub const KRILL_DEFAULT_CONFIG_FILE: &str = "./defaults/krill.conf"; #[cfg(feature = "multi-user")] pub const KRILL_DEFAULT_CONFIG_FILE: &str = "./defaults/krill-multi-user.conf"; -pub const KRILL_ENV_TEST_ANN: &str = "KRILL_TEST_ANN"; -pub const KRILL_ENV_TEST_UNIT_DATA: &str = "KRILL_TEST_UNIT_DATA"; +const KRILL_ENV_TEST: &str = "KRILL_TEST"; +const KRILL_ENV_TEST_ANN: &str = "KRILL_TEST_ANN"; +const KRILL_ENV_TESTBED_ENABLED: &str = "KRILL_TESTBED_ENABLED"; pub const KRILL_ENV_UPGRADE_ONLY: &str = "KRILL_UPGRADE_ONLY"; pub const KRILL_ENV_FORCE_RECOVER: &str = "KRILL_FORCE_RECOVER"; -pub const KRILL_ENV_REPO_ENABLED: &str = "KRILL_REPO_ENABLED"; -pub const KRILL_ENV_TESTBED_ENABLED: &str = "KRILL_TESTBED_ENABLED"; -pub const KRILL_ENV_USE_TA: &str = "KRILL_USE_TA"; pub const KRILL_ENV_LOG_LEVEL: &str = "KRILL_LOG_LEVEL"; pub const KRILL_ENV_AUTH_TOKEN: &str = "KRILL_AUTH_TOKEN"; pub const KRILL_ENV_SERVER_PORT: &str = "KRILL_SERVER_PORT"; +pub fn enable_test_mode() { + std::env::set_var(KRILL_ENV_TEST, "1"); +} + +pub fn test_mode_enabled() -> bool { + std::env::var(KRILL_ENV_TEST).is_ok() +} + +pub fn enable_testbed() { + std::env::set_var(KRILL_ENV_TESTBED_ENABLED, "1"); +} + +pub fn testbed_enabled() -> bool { + std::env::var(KRILL_ENV_TESTBED_ENABLED).is_ok() +} + +pub fn enable_test_announcements() { + std::env::set_var(KRILL_ENV_TEST_ANN, "1"); +} + +pub fn test_announcements_enabled() -> bool { + std::env::var(KRILL_ENV_TEST_ANN).is_ok() +} + pub const CASERVER_DIR: &str = "cas"; pub const PUBSERVER_DFLT: &str = "0"; diff --git a/src/daemon/ca/certauth.rs b/src/daemon/ca/certauth.rs index a25cf0be..43007a8f 100644 --- a/src/daemon/ca/certauth.rs +++ b/src/daemon/ca/certauth.rs @@ -25,6 +25,7 @@ use crate::commons::eventsourcing::{Aggregate, StoredEvent}; use crate::commons::remote::rfc6492; use crate::commons::remote::rfc8183; use crate::commons::KrillResult; +use crate::constants::test_mode_enabled; use crate::daemon::ca::events::ChildCertificateUpdates; use crate::daemon::ca::rc::PublishMode; use crate::daemon::ca::{ @@ -659,7 +660,7 @@ impl CertAuth { let (rcn, limit, csr) = request.unpack(); let csr_info = CsrInfo::try_from(&csr)?; - if !csr_info.global_uris() && cfg!(not(debug_assertions)) { + if !csr_info.global_uris() && !test_mode_enabled() { return Err(Error::invalid_csr( "MUST use hostnames in URIs for certificate requests.", )); diff --git a/src/daemon/config.rs b/src/daemon/config.rs index c7927d0a..2f75f918 100644 --- a/src/daemon/config.rs +++ b/src/daemon/config.rs @@ -367,10 +367,6 @@ impl Config { 0 } } - - pub fn testbed_enabled(&self) -> bool { - env::var(KRILL_ENV_TESTBED_ENABLED).is_ok() - } } /// # Create diff --git a/src/daemon/scheduler.rs b/src/daemon/scheduler.rs index 81563484..9d74143e 100644 --- a/src/daemon/scheduler.rs +++ b/src/daemon/scheduler.rs @@ -9,8 +9,9 @@ use tokio::runtime::Runtime; use rpki::x509::Time; -use crate::commons::{actor::Actor, api::Handle}; use crate::commons::bgp::BgpAnalyser; +use crate::commons::{actor::Actor, api::Handle}; +use crate::constants::test_mode_enabled; #[cfg(feature = "multi-user")] use crate::daemon::auth::common::session::LoginSessionCache; use crate::daemon::ca::CaServer; @@ -68,7 +69,6 @@ impl Scheduler { event_queue, caserver.clone(), pubserver.clone(), - config.testbed_enabled(), actor.clone(), )); @@ -77,7 +77,8 @@ impl Scheduler { } let announcements_refresh = make_announcements_refresh(bgp_analyser); - let archive_old_commands = make_archive_old_commands(caserver, pubserver, config.archive_threshold_days, actor.clone()); + let archive_old_commands = + make_archive_old_commands(caserver, pubserver, config.archive_threshold_days, actor.clone()); #[cfg(feature = "multi-user")] let login_cache_sweeper_sh = make_login_cache_sweeper_sh(login_session_cache); @@ -98,7 +99,6 @@ fn make_cas_event_triggers( event_queue: Arc, caserver: Arc, pubserver: Option>, - test_mode: bool, actor: Actor, ) -> ScheduleHandle { let mut scheduler = clokwerk::Scheduler::new(); @@ -127,11 +127,11 @@ fn make_cas_event_triggers( } QueueEvent::Delta(handle, _version) => { - try_publish(&event_queue, caserver.clone(), pubserver.clone(), handle, test_mode, &actor).await + try_publish(&event_queue, caserver.clone(), pubserver.clone(), handle, &actor).await } QueueEvent::ReschedulePublish(handle, last_try) => { if Time::five_minutes_ago().timestamp() > last_try.timestamp() { - try_publish(&event_queue, caserver.clone(), pubserver.clone(), handle, test_mode, &actor).await + try_publish(&event_queue, caserver.clone(), pubserver.clone(), handle, &actor).await } else { event_queue.push_back(QueueEvent::ReschedulePublish(handle, last_try)); } @@ -217,14 +217,13 @@ async fn try_publish( caserver: Arc, pubserver: Option>, ca: Handle, - test_mode: bool, actor: &Actor, ) { info!("Try to publish for '{}'", ca); let publisher = CaPublisher::new(caserver.clone(), pubserver); if let Err(e) = publisher.publish(&ca, actor).await { - if test_mode { + if test_mode_enabled() { error!("Failed to publish for '{}', error: {}", ca, e); } else { error!("Failed to publish for '{}' will reschedule, error: {}", ca, e); diff --git a/src/pubd/repository.rs b/src/pubd/repository.rs index 17df7048..126457f8 100644 --- a/src/pubd/repository.rs +++ b/src/pubd/repository.rs @@ -20,7 +20,9 @@ use crate::commons::eventsourcing::Aggregate; use crate::commons::remote::rfc8183; use crate::commons::util::file; use crate::commons::KrillResult; -use crate::constants::{REPOSITORY_NOTIFICATION_RETAIN_SECONDS, REPOSITORY_RRDP_DIR, REPOSITORY_RSYNC_DIR}; +use crate::constants::{ + test_mode_enabled, REPOSITORY_NOTIFICATION_RETAIN_SECONDS, REPOSITORY_RRDP_DIR, REPOSITORY_RSYNC_DIR, +}; use crate::pubd::events::RrdpSessionReset; use crate::pubd::publishers::Publisher; use crate::pubd::{Cmd, CmdDet, Evt, EvtDet, Ini, RrdpUpdate}; @@ -265,7 +267,7 @@ impl RrdpServer { let mut retain_secs = REPOSITORY_NOTIFICATION_RETAIN_SECONDS; - if cfg!(test) { + if test_mode_enabled() { retain_secs = 1; } diff --git a/src/test.rs b/src/test.rs index d024c4a2..bee84b8a 100644 --- a/src/test.rs +++ b/src/test.rs @@ -1,12 +1,12 @@ //! Helper functions for testing Krill. +use std::fs; use std::fs::File; use std::io::Write; use std::path::PathBuf; use std::str::FromStr; use std::sync::Arc; use std::time::Duration; -use std::{env, fs}; use bytes::Bytes; @@ -30,7 +30,6 @@ use crate::commons::crypto::SignSupport; use crate::commons::remote::rfc8183; use crate::commons::remote::rfc8183::{ChildRequest, RepositoryResponse}; use crate::commons::util::httpclient; -use crate::constants::{KRILL_ENV_TESTBED_ENABLED, KRILL_ENV_TEST_ANN, KRILL_ENV_TEST_UNIT_DATA}; use crate::daemon::ca::{ta_handle, ResourceTaggedAttestation, RtaContentRequest, RtaPrepareRequest}; use crate::daemon::http::server; @@ -75,10 +74,9 @@ pub async fn server_ready(uri: &str) -> bool { } fn test_config(dir: &PathBuf) -> Config { - env::set_var(KRILL_ENV_TEST_UNIT_DATA, dir.to_string_lossy().to_string()); - env::set_var(KRILL_ENV_TEST_ANN, "1"); - env::set_var(KRILL_ENV_TESTBED_ENABLED, "1"); - + crate::constants::enable_test_mode(); + crate::constants::enable_testbed(); + crate::constants::enable_test_announcements(); Config::test(dir) }