From c2e555402d5f47d86d1a321fd99d2afb488d8a62 Mon Sep 17 00:00:00 2001 From: johntaiko Date: Wed, 22 May 2024 10:41:06 +0800 Subject: [PATCH] fix: let config_path in config_dir (#233) * fix: let config_path in config_dir * fix: save updated config in new path --- provers/sgx/setup/src/app_args.rs | 4 ++-- provers/sgx/setup/src/main.rs | 7 ++++++- provers/sgx/setup/src/setup_bootstrap.rs | 23 +++++++++++++---------- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/provers/sgx/setup/src/app_args.rs b/provers/sgx/setup/src/app_args.rs index 48a65ba60..fdcdfae1c 100644 --- a/provers/sgx/setup/src/app_args.rs +++ b/provers/sgx/setup/src/app_args.rs @@ -29,10 +29,10 @@ pub struct BootstrapArgs { pub l1_chain_id: u64, #[clap(long, default_value = "0x4826533B4897376654Bb4d4AD88B7faFD0C98528")] pub sgx_verifier_address: String, - #[clap(long, default_value = "/etc/raiko/config.sgx.json")] + #[clap(long, default_value = "config.sgx.json")] /// Path to a config file that includes sufficient json args to request /// a proof of specified type. Curl json-rpc overrides its contents - pub config_path: PathBuf, + pub config_filename: String, } fn get_default_raiko_user_config_path(subdir: &str) -> PathBuf { diff --git a/provers/sgx/setup/src/main.rs b/provers/sgx/setup/src/main.rs index ccf82d74d..008e5eab1 100644 --- a/provers/sgx/setup/src/main.rs +++ b/provers/sgx/setup/src/main.rs @@ -13,7 +13,12 @@ pub async fn main() -> Result<()> { match args.command { Command::Bootstrap(sgx_bootstrap_args) => { println!("Setup bootstrapping: {:?}", sgx_bootstrap_args); - setup_bootstrap(args.global_opts.secrets_dir, &sgx_bootstrap_args).await?; + setup_bootstrap( + args.global_opts.secrets_dir, + args.global_opts.config_dir, + &sgx_bootstrap_args, + ) + .await?; } } diff --git a/provers/sgx/setup/src/setup_bootstrap.rs b/provers/sgx/setup/src/setup_bootstrap.rs index cc1d49b75..6410c7285 100644 --- a/provers/sgx/setup/src/setup_bootstrap.rs +++ b/provers/sgx/setup/src/setup_bootstrap.rs @@ -19,6 +19,7 @@ use tracing::info; pub(crate) async fn setup_bootstrap( secret_dir: PathBuf, + config_dir: PathBuf, bootstrap_args: &BootstrapArgs, ) -> Result<()> { let cur_dir = env::current_exe() @@ -34,7 +35,7 @@ pub(crate) async fn setup_bootstrap( cmd }; - let mut instance_id = get_instance_id(&bootstrap_args.config_path).ok(); + let mut instance_id = get_instance_id(&config_dir).ok(); let need_init = check_bootstrap(secret_dir.clone(), gramine_cmd()) .await .is_err() @@ -43,7 +44,7 @@ pub(crate) async fn setup_bootstrap( if need_init { let bootstrap_proof = bootstrap(secret_dir, gramine_cmd()).await?; // clean check file - match remove_instance_id(&bootstrap_args.config_path) { + match remove_instance_id(&config_dir) { Ok(_) => Ok(()), Err(e) => { if e.kind() == std::io::ErrorKind::NotFound { @@ -63,25 +64,27 @@ pub(crate) async fn setup_bootstrap( .map_err(|e| anyhow::Error::msg(e.to_string()))?; info!("Saving instance id {}", register_id,); // set check file - set_instance_id(&bootstrap_args.config_path, register_id)?; + set_instance_id(&config_dir, register_id)?; instance_id = Some(register_id); } // Always reset the configuration with a persistent instance ID upon restart. - let file = File::open(&bootstrap_args.config_path)?; + let config_path = config_dir.join(&bootstrap_args.config_filename); + let file = File::open(&config_path)?; let reader = BufReader::new(file); let mut file_config: Value = serde_json::from_reader(reader)?; file_config["sgx"]["instance_id"] = Value::Number(Number::from(instance_id.unwrap())); //save to the same file - info!( - "Saving bootstrap data file {}", - bootstrap_args.config_path.display() - ); + info!("Saving bootstrap data file {}", config_path.display()); let json = serde_json::to_string_pretty(&file_config)?; - fs::write(&bootstrap_args.config_path, json).context(format!( + let new_config_path = config_path + .with_extension("") + .with_extension("new") + .with_extension("json"); + fs::write(&new_config_path, json).context(format!( "Saving bootstrap data file {} failed", - bootstrap_args.config_path.display() + new_config_path.display() ))?; Ok(()) }