Skip to content

Commit

Permalink
fix(dgw): write new JRL into a temporary file, and swap on success (#857
Browse files Browse the repository at this point in the history
)

It’s preferable to proceed like this to avoid losing current JRL file
if the file is truncated without being rewritten succesfully immediately.

Issue: DGW-104
  • Loading branch information
CBenoit authored May 15, 2024
1 parent fbbcbbe commit d91f1cf
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions devolutions-gateway/src/api/jrl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,15 @@ async fn update_jrl(
let jrl_json = serde_json::to_string_pretty(&claims)
.map_err(HttpError::internal().with_msg("failed to serialize JRL").err())?;

let jrl_file = conf.jrl_file.as_path();
let jrl_tmp_path = conf.jrl_file.with_extension("tmp");

info!(path = %jrl_file, "Writing JRL file to disk");

// FIXME(DGW-104): use a temporary file to write the new JRL, and swap on success
debug!(path = %jrl_tmp_path, "Writing JRL file to disk");

let mut file = tokio::fs::OpenOptions::new()
.write(true)
.truncate(true)
.create(true)
.open(jrl_file)
.open(&jrl_tmp_path)
.await
.map_err(HttpError::internal().err())?
.pipe(BufWriter::new);
Expand All @@ -61,6 +59,14 @@ async fn update_jrl(

file.flush().await.map_err(HttpError::internal().err())?;

let jrl_path = conf.jrl_file.as_path();

debug!(tmp_path = %jrl_tmp_path, path = %jrl_path, "Swapping temporary JRL file");

tokio::fs::rename(jrl_tmp_path, jrl_path)
.await
.map_err(HttpError::internal().err())?;

*jrl.lock() = claims;

info!("Current JRL updated!");
Expand Down

0 comments on commit d91f1cf

Please sign in to comment.