Skip to content

Commit 1003a2b

Browse files
Xynnn007mythi
authored andcommitted
AA: add log to configuration
Related to confidential-containers#1324 Signed-off-by: Xynnn007 <xynnn@linux.alibaba.com>
1 parent ed3474a commit 1003a2b

10 files changed

Lines changed: 100 additions & 50 deletions

File tree

attestation-agent/attestation-agent/config.example.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,8 @@
1111
"eventlog_config": {
1212
"init_pcr": 17,
1313
"enable_eventlog": false
14+
},
15+
"log": {
16+
"level": "info"
1417
}
1518
}

attestation-agent/attestation-agent/config.example.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,6 @@ M9QaC1mzQ/OStg==
3434

3535
init_pcr = 17
3636
enable_eventlog = false
37+
38+
[log]
39+
level = "info"

attestation-agent/attestation-agent/src/bin/grpc-aa/main.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
mod server;
77

88
use anyhow::*;
9-
use attestation_agent::{initdata::Initdata, AttestationAPIs, AttestationAgent};
9+
use attestation_agent::{config::Config, initdata::Initdata, AttestationAPIs, AttestationAgent};
1010
use base64::Engine;
1111
use clap::Parser;
1212
use shadow_rs::shadow;
@@ -69,9 +69,13 @@ struct Cli {
6969

7070
#[tokio::main]
7171
pub async fn main() -> Result<()> {
72+
let cli = Cli::parse();
73+
let (config, config_log) = Config::from_file(cli.config_file)?;
74+
7275
let env_filter = match std::env::var_os("RUST_LOG") {
73-
Some(_) => EnvFilter::try_from_default_env().expect("RUST_LOG is present but invalid"),
74-
None => EnvFilter::new("info"),
76+
Some(_) => EnvFilter::try_from_default_env().context("RUST_LOG is present but invalid")?,
77+
None => EnvFilter::try_new(&config.log.level)
78+
.context(format!("Invalid log level: {}", config.log.level))?,
7579
};
7680

7781
let version = format!(
@@ -98,11 +102,12 @@ rpc: grpc
98102
Subscriber::builder().with_env_filter(env_filter).init();
99103

100104
info!("Welcome to Confidential Containers Attestation Agent (gRPC version)!\n\n{version}");
101-
let cli = Cli::parse();
105+
info!("{config_log}");
106+
debug!(config = ?config, "Using config");
102107

103108
let attestation_socket = cli.attestation_sock.parse::<SocketAddr>()?;
104109

105-
let mut aa = AttestationAgent::new(cli.config_file.as_deref()).context("start AA")?;
110+
let mut aa = AttestationAgent::new(config).context("start AA")?;
106111

107112
let mut initdata_digest = None;
108113
if let Some(initdata_toml_path) = cli.initdata_toml {

attestation-agent/attestation-agent/src/bin/ttrpc-aa.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//
55

66
use anyhow::*;
7-
use attestation_agent::{initdata::Initdata, AttestationAPIs, AttestationAgent};
7+
use attestation_agent::{config::Config, initdata::Initdata, AttestationAPIs, AttestationAgent};
88
use base64::Engine;
99
use clap::Parser;
1010
use const_format::concatcp;
@@ -87,9 +87,14 @@ pub fn start_ttrpc_service(aa: AttestationAgent) -> Result<HashMap<String, Servi
8787

8888
#[tokio::main]
8989
pub async fn main() -> Result<()> {
90+
let cli = Cli::parse();
91+
92+
let (config, config_log) = Config::from_file(cli.config_file)?;
93+
9094
let env_filter = match std::env::var_os("RUST_LOG") {
91-
Some(_) => EnvFilter::try_from_default_env().expect("RUST_LOG is present but invalid"),
92-
None => EnvFilter::new("info"),
95+
Some(_) => EnvFilter::try_from_default_env().context("RUST_LOG is present but invalid")?,
96+
None => EnvFilter::try_new(&config.log.level)
97+
.context(format!("Invalid log level: {}", config.log.level))?,
9398
};
9499

95100
let version = format!(
@@ -116,7 +121,9 @@ rpc: ttrpc
116121
Subscriber::builder().with_env_filter(env_filter).init();
117122

118123
info!("Welcome to Confidential Containers Attestation Agent (ttRPC version)!\n\n{version}");
119-
let cli = Cli::parse();
124+
125+
info!("{config_log}");
126+
debug!(config = ?config, "Using config");
120127

121128
if !Path::new(DEFAULT_UNIX_SOCKET_DIR).exists() {
122129
std::fs::create_dir_all(DEFAULT_UNIX_SOCKET_DIR).expect("Create unix socket dir failed");
@@ -125,7 +132,7 @@ rpc: ttrpc
125132
clean_previous_sock_file(&cli.attestation_sock)
126133
.context("clean previous attestation socket file")?;
127134

128-
let mut aa = AttestationAgent::new(cli.config_file.as_deref()).context("start AA")?;
135+
let mut aa = AttestationAgent::new(config).context("start AA")?;
129136

130137
let mut initdata_digest = None;
131138
if let Some(initdata_toml_path) = cli.initdata_toml {

attestation-agent/attestation-agent/src/config/mod.rs

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,27 @@ pub const DEFAULT_AA_CONFIG_PATH: &str = "/etc/attestation-agent.conf";
2323

2424
pub const DEFAULT_EVENTLOG_HASH: &str = "sha384";
2525

26+
pub const DEFAULT_LOG_LEVEL: &str = "info";
27+
28+
fn default_log_level() -> String {
29+
DEFAULT_LOG_LEVEL.to_string()
30+
}
31+
32+
#[derive(Clone, Debug, Deserialize, PartialEq)]
33+
pub struct LogConfig {
34+
/// log level
35+
#[serde(default = "default_log_level")]
36+
pub level: String,
37+
}
38+
39+
impl Default for LogConfig {
40+
fn default() -> Self {
41+
Self {
42+
level: DEFAULT_LOG_LEVEL.to_string(),
43+
}
44+
}
45+
}
46+
2647
#[derive(Clone, Debug, Deserialize, PartialEq, Default)]
2748
pub struct Config {
2849
/// configs about token
@@ -31,15 +52,36 @@ pub struct Config {
3152

3253
/// configs about eventlog
3354
pub eventlog_config: EventlogConfig,
55+
56+
/// log configuration
57+
#[serde(default)]
58+
pub log: LogConfig,
3459
}
3560

3661
impl Config {
3762
pub fn default_with_kernel_cmdline() -> Self {
3863
Config {
3964
token_configs: TokenConfigs::from_kernel_cmdline(),
4065
eventlog_config: EventlogConfig::default(),
66+
log: LogConfig::default(),
4167
}
4268
}
69+
70+
pub fn from_file(config_path: Option<String>) -> Result<(Self, String)> {
71+
let (config, config_log) = match config_path {
72+
Some(config_path) => {
73+
let config = Self::try_from(&config_path[..])?;
74+
let log = format!("Using config file: {config_path}");
75+
(config, log)
76+
}
77+
None => {
78+
let config = Self::default_with_kernel_cmdline();
79+
let log = "No AA config file specified. Using default configuration and the kbs address will be read from kernel cmdline.".to_string();
80+
(config, log)
81+
}
82+
};
83+
Ok((config, config_log))
84+
}
4385
}
4486

4587
#[derive(Clone, Debug, Deserialize, PartialEq)]
@@ -101,7 +143,7 @@ impl TryFrom<&str> for Config {
101143

102144
#[cfg(test)]
103145
mod tests {
104-
use crate::config::{EventlogConfig, TokenConfigs};
146+
use crate::config::{EventlogConfig, LogConfig, TokenConfigs};
105147

106148
use super::Config;
107149

@@ -144,7 +186,8 @@ M9QaC1mzQ/OStg==
144186
eventlog_config: EventlogConfig {
145187
init_pcr: 17,
146188
enable_eventlog: false,
147-
}
189+
},
190+
log: LogConfig::default(),
148191
})]
149192
#[case("config.example.json",
150193
Config {
@@ -184,7 +227,8 @@ M9QaC1mzQ/OStg==
184227
eventlog_config: EventlogConfig {
185228
init_pcr: 17,
186229
enable_eventlog: false,
187-
}
230+
},
231+
log: LogConfig::default(),
188232
})]
189233
#[case(
190234
"test/config1.toml",
@@ -203,7 +247,8 @@ M9QaC1mzQ/OStg==
203247
eventlog_config: EventlogConfig {
204248
init_pcr: 17,
205249
enable_eventlog: false,
206-
}
250+
},
251+
log: LogConfig { level: "warn".to_string() },
207252
})]
208253
#[case(
209254
"test/config2.toml",
@@ -220,26 +265,8 @@ M9QaC1mzQ/OStg==
220265
eventlog_config: EventlogConfig {
221266
init_pcr: 17,
222267
enable_eventlog: false,
223-
}
224-
})]
225-
#[case(
226-
"test/config3.toml",
227-
Config {
228-
token_configs: TokenConfigs {
229-
#[cfg(feature = "coco_as")]
230-
coco_as: Some(crate::config::coco_as::CoCoASConfig {
231-
url: "http://127.0.0.1:8000".to_string(),
232-
}),
233-
#[cfg(feature = "kbs")]
234-
kbs: Some(crate::config::kbs::KbsConfig {
235-
url: "https://127.0.0.1:8080".to_string(),
236-
cert: None,
237-
})
238268
},
239-
eventlog_config: EventlogConfig {
240-
init_pcr: 17,
241-
enable_eventlog: false,
242-
}
269+
log: LogConfig { level: "warn".to_string() },
243270
})]
244271
#[case(
245272
"test/config4.toml",
@@ -253,7 +280,8 @@ M9QaC1mzQ/OStg==
253280
eventlog_config: EventlogConfig {
254281
init_pcr: 17,
255282
enable_eventlog: false,
256-
}
283+
},
284+
log: LogConfig { level: "warn".to_string() },
257285
})]
258286
#[case(
259287
"test/config5.toml",
@@ -267,7 +295,8 @@ M9QaC1mzQ/OStg==
267295
eventlog_config: EventlogConfig {
268296
init_pcr: 17,
269297
enable_eventlog: false,
270-
}
298+
},
299+
log: LogConfig::default(),
271300
})]
272301
#[case(
273302
"test/config6.toml",
@@ -281,7 +310,8 @@ M9QaC1mzQ/OStg==
281310
eventlog_config: EventlogConfig {
282311
init_pcr: 17,
283312
enable_eventlog: false,
284-
}
313+
},
314+
log: LogConfig::default(),
285315
})]
286316
fn parse_configs(#[case] config: &str, #[case] expected: Config) {
287317
let _config = Config::try_from(config).expect("failed to parse config file");

attestation-agent/attestation-agent/src/lib.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub mod token;
2121

2222
use eventlog::EventLog;
2323
use token::*;
24-
use tracing::{debug, info, warn};
24+
use tracing::{debug, info};
2525

2626
use crate::{config::Config, eventlog::Event};
2727

@@ -55,9 +55,10 @@ pub enum RuntimeMeasurement {
5555
/// ```no_run
5656
/// use attestation_agent::AttestationAgent;
5757
/// use attestation_agent::AttestationAPIs;
58+
/// use attestation_agent::config::Config;
5859
///
5960
/// // initialize with empty config
60-
/// let mut aa = AttestationAgent::new(None).unwrap();
61+
/// let mut aa = AttestationAgent::new(Config::default()).unwrap();
6162
///
6263
/// let _quote = aa.get_evidence(&[0;64]);
6364
/// ```
@@ -123,18 +124,7 @@ impl AttestationAgent {
123124
}
124125

125126
/// Create a new instance of [AttestationAgent].
126-
pub fn new(config_path: Option<&str>) -> Result<Self> {
127-
let config = match config_path {
128-
Some(config_path) => {
129-
info!("Using AA config file: {config_path}");
130-
Config::try_from(config_path)?
131-
}
132-
None => {
133-
warn!("No AA config file specified. Using a default configuration and the kbs address will be read from kernel cmdline.");
134-
Config::default_with_kernel_cmdline()
135-
}
136-
};
137-
debug!("Using config: {config:#?}");
127+
pub fn new(config: Config) -> Result<Self> {
138128
let config = RwLock::new(config);
139129

140130
let primary_tee = detect_tee_type();

attestation-agent/attestation-agent/test/config1.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
[log]
2+
level = "warn"
3+
14
[token_configs]
25
[token_configs.coco_as]
36
url = "http://127.0.0.1:8000"

attestation-agent/attestation-agent/test/config2.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
[log]
2+
level = "warn"
3+
14
[token_configs]
25
[token_configs.kbs]
36
url = "https://127.0.0.1:8080"

attestation-agent/attestation-agent/test/config3.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
[log]
2+
level = "warn"
3+
14
[token_configs]
25
[token_configs.coco_as]
36
url = "http://127.0.0.1:8000"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
[log]
2+
level = "warn"
3+
14
[token_configs]
25

36
[eventlog_config]

0 commit comments

Comments
 (0)