Skip to content

Commit

Permalink
fix: let config_path in config_dir (#233)
Browse files Browse the repository at this point in the history
* fix: let config_path in config_dir

* fix: save updated config in new path
  • Loading branch information
johntaiko authored May 22, 2024
1 parent aaf05a7 commit 78a5844
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
4 changes: 2 additions & 2 deletions provers/sgx/setup/src/app_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion provers/sgx/setup/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
}
}

Expand Down
23 changes: 13 additions & 10 deletions provers/sgx/setup/src/setup_bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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 {
Expand All @@ -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(())
}

0 comments on commit 78a5844

Please sign in to comment.