From 77bdb9d76935fba06f03c5678258f4e738cdfb0d Mon Sep 17 00:00:00 2001 From: Hugues de Valon Date: Fri, 6 Dec 2019 14:46:04 +0000 Subject: [PATCH] Make sure Cargo features work Makes sure that Parsec builds with commands like: cargo build --no-default-features --features "mbed-crypto-provider" cargo build --no-default-features --features "tpm-provider" Also renames the "mbed" features to "mbed-crypto-provider" for consistency. Signed-off-by: Hugues de Valon --- Cargo.toml | 4 +- build.rs | 2 +- src/bin/main.rs | 11 ++++-- src/providers/mod.rs | 2 +- src/utils/service_builder.rs | 76 ++++++++++++++++++++++++------------ 5 files changed, 63 insertions(+), 32 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b60f13d5..980b2323 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,6 +44,6 @@ serde = { version = "1.0", features = ["derive"] } mbed-crypto-version = "mbedcrypto-2.0.0" [features] -default = ["mbed", "pkcs11-provider"] -mbed = [] +default = ["mbed-crypto-provider", "pkcs11-provider"] +mbed-crypto-provider = [] pkcs11-provider = ["pkcs11", "serde_asn1_der"] diff --git a/build.rs b/build.rs index aed5c903..fa1ad3a8 100644 --- a/build.rs +++ b/build.rs @@ -165,7 +165,7 @@ fn main() { .expect("Cargo.toml does not contain package metadata."); let parsec_config = get_value_from_table(&metadata, CONFIG_TABLE_NAME); - if cfg!(feature = "mbed") { + if cfg!(feature = "mbed-crypto-provider") { let mbed_config = config.mbed_config.expect(&format!( "Could not find mbed_config table in the {} file.", BUILD_CONFIG_FILE_PATH diff --git a/src/bin/main.rs b/src/bin/main.rs index 223e025b..6d452e8b 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -15,7 +15,7 @@ use log::info; use parsec::utils::{ServiceBuilder, ServiceConfig}; use signal_hook::{flag, SIGHUP, SIGTERM}; -use std::io::Error; +use std::io::{Error, ErrorKind}; use std::sync::{ atomic::{AtomicBool, Ordering}, Arc, @@ -42,10 +42,12 @@ fn main() -> Result<(), Error> { info!("Parsec started. Configuring the service..."); + let front_end_handler = ServiceBuilder::build_service(&config) + .ok_or_else(|| Error::new(ErrorKind::Other, "Parsec can not be configured."))?; // Multiple threads can not just have a reference of the front end handler because they could // outlive the run function. It is needed to give them all ownership of the front end handler // through an Arc. - let mut front_end_handler = Arc::from(ServiceBuilder::build_service(&config)); + let mut front_end_handler = Arc::from(front_end_handler); let mut listener = ServiceBuilder::start_listener(&config.listener); let mut threadpool = ServiceBuilder::build_threadpool(config.core_settings.thread_pool_size); @@ -71,7 +73,10 @@ fn main() -> Result<(), Error> { config_file = ::std::fs::read_to_string(CONFIG_FILE_PATH) .expect("Failed to read configuration file"); config = toml::from_str(&config_file).expect("Failed to parse service configuration"); - front_end_handler = Arc::from(ServiceBuilder::build_service(&config)); + front_end_handler = + Arc::from(ServiceBuilder::build_service(&config).ok_or_else(|| { + Error::new(ErrorKind::Other, "Parsec can not be configured.") + })?); listener = ServiceBuilder::start_listener(&config.listener); threadpool = ServiceBuilder::build_threadpool(config.core_settings.thread_pool_size); diff --git a/src/providers/mod.rs b/src/providers/mod.rs index 0e37d38b..173243f8 100644 --- a/src/providers/mod.rs +++ b/src/providers/mod.rs @@ -20,7 +20,7 @@ pub mod core_provider; #[cfg(feature = "pkcs11-provider")] pub mod pkcs11_provider; -#[cfg(feature = "mbed")] +#[cfg(feature = "mbed-crypto-provider")] pub mod mbed_provider; #[derive(Deserialize, Debug)] diff --git a/src/utils/service_builder.rs b/src/utils/service_builder.rs index 1b9854ce..22c9f965 100644 --- a/src/utils/service_builder.rs +++ b/src/utils/service_builder.rs @@ -24,11 +24,8 @@ use crate::front::{ }; use crate::key_id_managers::on_disk_manager::{OnDiskKeyIDManagerBuilder, DEFAULT_MAPPINGS_PATH}; use crate::key_id_managers::{KeyIdManagerConfig, KeyIdManagerType, ManageKeyIDs}; -use crate::providers::{ - core_provider::CoreProviderBuilder, mbed_provider::MbedProviderBuilder, - pkcs11_provider::Pkcs11ProviderBuilder, Provide, ProviderConfig, ProviderType, -}; -use log::{info, LevelFilter}; +use crate::providers::{core_provider::CoreProviderBuilder, Provide, ProviderConfig}; +use log::{error, LevelFilter}; use parsec_interface::operations_protobuf::ProtobufConverter; use parsec_interface::requests::AuthType; use parsec_interface::requests::{BodyType, ProviderID}; @@ -40,6 +37,15 @@ use std::sync::RwLock; use std::time::Duration; use threadpool::{Builder as ThreadPoolBuilder, ThreadPool}; +#[cfg(feature = "mbed-crypto-provider")] +use crate::providers::mbed_provider::MbedProviderBuilder; +#[cfg(feature = "pkcs11-provider")] +use crate::providers::pkcs11_provider::Pkcs11ProviderBuilder; +#[cfg(not(all(feature = "mbed-crypto-provider", feature = "pkcs11-provider")))] +use log::warn; +#[cfg(any(feature = "mbed-crypto-provider", feature = "pkcs11-provider"))] +use {crate::providers::ProviderType, log::info}; + const VERSION_MINOR: u8 = 0; const VERSION_MAJOR: u8 = 1; @@ -65,11 +71,16 @@ pub struct ServiceConfig { pub struct ServiceBuilder; impl ServiceBuilder { - pub fn build_service(config: &ServiceConfig) -> FrontEndHandler { + pub fn build_service(config: &ServiceConfig) -> Option { let key_id_managers = build_key_id_managers(&config.key_manager); let providers = build_providers(&config.provider, key_id_managers); + if providers.is_empty() { + error!("Parsec needs at least one provider to start. No valid provider could be created from the configuration."); + return None; + } + let backend_handlers = build_backend_handlers(providers); let dispatcher = DispatcherBuilder::new() @@ -78,10 +89,12 @@ impl ServiceBuilder { let simple_authenticator = Box::from(SimpleAuthenticator {}); - FrontEndHandlerBuilder::new() - .with_dispatcher(dispatcher) - .with_authenticator(AuthType::Simple, simple_authenticator) - .build() + Some( + FrontEndHandlerBuilder::new() + .with_dispatcher(dispatcher) + .with_authenticator(AuthType::Simple, simple_authenticator) + .build(), + ) } pub fn start_listener(config: &ListenerConfig) -> Box { @@ -145,36 +158,41 @@ fn build_providers( ) -> HashMap { let mut map = HashMap::new(); for config in configs { - let key_id_manager = key_id_managers - .get(&config.key_id_manager) - .unwrap_or_else(|| { - panic!( + let key_id_manager = match key_id_managers.get(&config.key_id_manager) { + Some(key_id_manager) => key_id_manager, + None => { + error!( "Key ID manager with specified name was not found ({})", config.key_id_manager - ) - }); - map.insert( - config.provider_type.to_provider_id(), - get_provider(config, key_id_manager.clone()), - ); + ); + continue; + } + }; + let provider = match get_provider(config, key_id_manager.clone()) { + Some(provider) => provider, + None => continue, + }; + map.insert(config.provider_type.to_provider_id(), provider); } map } -fn get_provider(config: &ProviderConfig, key_id_manager: KeyIdManager) -> Provider { +fn get_provider(config: &ProviderConfig, key_id_manager: KeyIdManager) -> Option { match config.provider_type { + #[cfg(feature = "mbed-crypto-provider")] ProviderType::MbedProvider => { info!("Creating a Mbed Crypto Provider."); - Box::from( + Some(Box::from( MbedProviderBuilder::new() .with_key_id_store(key_id_manager) .build(), - ) + )) } + #[cfg(feature = "pkcs11-provider")] ProviderType::Pkcs11Provider => { info!("Creating a PKCS 11 Provider."); - Box::from( + Some(Box::from( Pkcs11ProviderBuilder::new() .with_key_id_store(key_id_manager) .with_pkcs11_library_path(config.library_path.clone().expect( @@ -185,7 +203,15 @@ fn get_provider(config: &ProviderConfig, key_id_manager: KeyIdManager) -> Provid )) .with_user_pin(config.user_pin.clone()) .build() - ) + )) + } + #[cfg(not(all(feature = "mbed-crypto-provider", feature = "pkcs11-provider")))] + _ => { + warn!( + "Provider \"{:?}\" chosen in the configuration was not compiled in Parsec binary.", + config.provider_type + ); + None } } }