@@ -23,6 +23,27 @@ pub const DEFAULT_AA_CONFIG_PATH: &str = "/etc/attestation-agent.conf";
2323
2424pub 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 ) ]
2748pub 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
3661impl 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) ]
103145mod 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" ) ;
0 commit comments