Skip to content

Commit 2e20b98

Browse files
committed
Add GlobalConfig with flag for logging errors
This commit adds a GlobalConfig structure with a `log_error_details` flag which controls if Rust error objects are logged along with the message. A `format_error` macro is also added to make the conditional logging easier. Signed-off-by: Ionut Mihalcea <ionut.mihalcea@arm.com>
1 parent 44fbaa0 commit 2e20b98

File tree

12 files changed

+85
-7
lines changed

12 files changed

+85
-7
lines changed

Cargo.lock

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
# Defaults to 1MB.
2222
#body_len_limit = 1048576
2323

24+
# Decide whether detailed information about errors occuring should be included in log messages.
25+
# WARNING: the details might include sensitive information about the keys used by Parsec clients,
26+
# such as key names or policies
27+
#log_error_details = false
28+
2429
# (Required) Configuration for the service IPC listener component.
2530
[listener]
2631
# (Required) Type of IPC that the service will support.

e2e_tests/provider_cfg/all/config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[core_settings]
22
# The CI already timestamps the logs
33
log_timestamp = false
4+
log_error_details = true
45

56
[listener]
67
listener_type = "DomainSocket"

e2e_tests/provider_cfg/mbed-crypto/config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[core_settings]
22
# The CI already timestamps the logs
33
log_timestamp = false
4+
log_error_details = true
45

56
[listener]
67
listener_type = "DomainSocket"

e2e_tests/provider_cfg/pkcs11/config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[core_settings]
22
# The CI already timestamps the logs
33
log_timestamp = false
4+
log_error_details = true
45

56
[listener]
67
listener_type = "DomainSocket"

e2e_tests/provider_cfg/tpm/config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[core_settings]
22
# The CI already timestamps the logs
33
log_timestamp = false
4+
log_error_details = true
45

56
[listener]
67
listener_type = "DomainSocket"

src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@
3737
// This one is hard to avoid.
3838
#![allow(clippy::multiple_crate_versions)]
3939

40+
#[allow(unused)]
41+
macro_rules! format_error {
42+
($message:expr, $error:expr) => {
43+
if crate::utils::GlobalConfig::log_error_details() {
44+
log::error!("{}; Error: {}", $message, $error)
45+
} else {
46+
log::error!("{};", $message)
47+
}
48+
};
49+
}
50+
4051
pub mod authenticators;
4152
pub mod back;
4253
pub mod front;

src/providers/pkcs11_provider/asym_sign.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl Pkcs11Provider {
9090
match self.backend.sign(session.session_handle(), &digest_info) {
9191
Ok(signature) => Ok(psa_sign_hash::Result { signature }),
9292
Err(e) => {
93-
error!("Failed to execute signing operation. Error: {}", e);
93+
format_error!("Failed to execute signing operation", e);
9494
Err(utils::to_response_status(e))
9595
}
9696
}

src/utils/global_config.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2020 Contributors to the Parsec project.
2+
// SPDX-License-Identifier: Apache-2.0
3+
use std::sync::atomic::AtomicBool;
4+
use std::sync::atomic::Ordering;
5+
6+
/// Configuration values that affect most or all the
7+
/// components of the service.
8+
#[derive(Default, Debug)]
9+
pub struct GlobalConfig {
10+
log_error_details: AtomicBool,
11+
}
12+
13+
impl GlobalConfig {
14+
const fn new() -> Self {
15+
GlobalConfig {
16+
log_error_details: AtomicBool::new(false),
17+
}
18+
}
19+
20+
/// Determine whether error logs should include detailed
21+
/// information about the error
22+
pub fn log_error_details() -> bool {
23+
GLOBAL_CONFIG.log_error_details.load(Ordering::Relaxed)
24+
}
25+
}
26+
27+
static GLOBAL_CONFIG: GlobalConfig = GlobalConfig::new();
28+
29+
pub(super) struct GlobalConfigBuilder {
30+
log_error_details: bool,
31+
}
32+
33+
impl GlobalConfigBuilder {
34+
pub fn new() -> Self {
35+
GlobalConfigBuilder {
36+
log_error_details: false,
37+
}
38+
}
39+
40+
pub fn with_log_error_details(mut self, log_error_details: bool) -> Self {
41+
self.log_error_details = log_error_details;
42+
43+
self
44+
}
45+
46+
pub fn build(self) {
47+
GLOBAL_CONFIG
48+
.log_error_details
49+
.store(self.log_error_details, Ordering::Relaxed);
50+
}
51+
}

src/utils/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright 2019 Contributors to the Parsec project.
22
// SPDX-License-Identifier: Apache-2.0
33
//! Service utilities
4+
mod global_config;
45
mod service_builder;
56

7+
pub use global_config::GlobalConfig;
68
pub use service_builder::{CoreSettings, ServiceBuilder, ServiceConfig};

0 commit comments

Comments
 (0)