-
-
Notifications
You must be signed in to change notification settings - Fork 637
/
Copy pathrekey.rs
57 lines (49 loc) · 2.01 KB
/
rekey.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use clap::Args;
use eyre::{Result, bail};
use tokio::{fs::File, io::AsyncWriteExt};
use atuin_client::{
encryption::{Key, decode_key, encode_key, generate_encoded_key, load_key},
record::sqlite_store::SqliteStore,
record::store::Store,
settings::Settings,
};
#[derive(Args, Debug)]
pub struct Rekey {
/// The new key to use for encryption. Omit for a randomly-generated key
key: Option<String>,
}
impl Rekey {
pub async fn run(&self, settings: &Settings, store: SqliteStore) -> Result<()> {
let key = if let Some(key) = self.key.clone() {
println!("Re-encrypting store with specified key");
match bip39::Mnemonic::from_phrase(&key, bip39::Language::English) {
Ok(mnemonic) => encode_key(Key::from_slice(mnemonic.entropy()))?,
Err(err) => {
match err {
// assume they copied in the base64 key
bip39::ErrorKind::InvalidWord(_) => key,
bip39::ErrorKind::InvalidChecksum => {
bail!("key mnemonic was not valid")
}
bip39::ErrorKind::InvalidKeysize(_)
| bip39::ErrorKind::InvalidWordLength(_)
| bip39::ErrorKind::InvalidEntropyLength(_, _) => {
bail!("key was not the correct length")
}
}
}
}
} else {
println!("Re-encrypting store with freshly-generated key");
let (_, encoded) = generate_encoded_key()?;
encoded
};
let current_key: [u8; 32] = load_key(settings)?.into();
let new_key: [u8; 32] = decode_key(key.clone())?.into();
store.re_encrypt(¤t_key, &new_key).await?;
println!("Store rewritten. Saving new key");
let mut file = File::create(settings.key_path.clone()).await?;
file.write_all(key.as_bytes()).await?;
Ok(())
}
}