Skip to content

Commit

Permalink
Add upgrade command to convert file to keychain
Browse files Browse the repository at this point in the history
  • Loading branch information
mxcl committed Nov 26, 2024
1 parent e5b9a31 commit adca322
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
18 changes: 12 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,20 @@ impl Config {
})
}

pub fn load(file: &mut impl Read) -> Result<Config, Error> {
let service: &str = "xyz.tea.BASE.bpb";
let account: &str = "example_account";
let str = get_keychain_item(service, account).unwrap();
Ok(toml::from_str(&str)?)
pub fn legacy_load(file: &mut impl Read) -> Result<Config, Error> {
let mut buf = vec![];
file.read_to_end(&mut buf)?;
Ok(toml::from_slice(&buf)?)
}

pub fn write(&self, file: &mut impl Write) -> Result<(), Error> {
pub fn load() -> Result<Config, Error> {
let service = "xyz.tea.BASE.bpb";
let account = "example_account";
let str = get_keychain_item(service, account)?;
Ok(toml::from_str::<Config>(&str)?)
}

pub fn write(&self) -> Result<(), Error> {
let secret = toml::to_string(self)?;
let service = "xyz.tea.BASE.bpb";
let account = "example_account"; //self.user_id();
Expand Down
4 changes: 2 additions & 2 deletions src/keychain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub fn add_keychain_item(service: &str, account: &str, secret: &str) -> Result<(
}
}

pub fn get_keychain_item(service: &str, account: &str) -> Result<String, String> {
pub fn get_keychain_item(service: &str, account: &str) -> Result<String, Error> {
let service = CString::new(service).unwrap();
let account = CString::new(account).unwrap();

Expand Down Expand Up @@ -119,7 +119,7 @@ pub fn get_keychain_item(service: &str, account: &str) -> Result<String, String>

Ok(secret)
} else {
Err(format!("SecItemCopyMatching failed with status: {}", status))
Err(failure::err_msg(format!("SecItemCopyMatching failed with status: {}", status)))
}
}
}
Expand Down
19 changes: 13 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ mod keychain;
mod tests;

use std::time::SystemTime;
use std::fs;

use ed25519_dalek as ed25519;
use failure::Error;
use keychain::add_keychain_item;
use rand::RngCore;

use crate::config::Config;
Expand All @@ -27,6 +29,7 @@ fn main() -> Result<(), Error> {
bail!("Must specify a userid argument, e.g.: `bpb init \"username <email>\"`")
}
}
Some("upgrade") => upgrade(),
Some("print") => print_public_key(),
Some("--help") => print_help_message(),
Some(arg) if gpg_sign_arg(arg) => verify_commit(),
Expand Down Expand Up @@ -73,15 +76,13 @@ fn generate_keypair(userid: String) -> Result<(), Error> {
let key_data = KeyData::create(keypair, userid, timestamp);
let config = Config::create(&key_data)?;

let mut file = std::fs::File::create(keys_file)?;
config.write(&mut file)?;
config.write()?;
println!("{}", key_data.public());
Ok(())
}

fn print_public_key() -> Result<(), Error> {
let mut file = std::fs::File::open(keys_file())?;
let config = Config::load(&mut file)?;
let config = Config::load()?;
let keypair = KeyData::load(&config)?;
println!("{}", keypair.public());
Ok(())
Expand All @@ -94,8 +95,7 @@ fn verify_commit() -> Result<(), Error> {
let mut stdin = std::io::stdin();
stdin.read_to_string(&mut commit)?;

let mut file = std::fs::File::open(keys_file())?;
let config = Config::load(&mut file)?;
let config = Config::load()?;
let keypair = KeyData::load(&config)?;

let sig = keypair.sign(commit.as_bytes())?;
Expand All @@ -114,6 +114,13 @@ fn delegate() -> ! {
process::exit(status)
}

fn upgrade() -> Result<(), Error> {
let mut file = std::fs::File::open(keys_file())?;
let config = Config::legacy_load(&mut file)?;
config.write()?;
fs::remove_file(keys_file()).map_err(|e| failure::err_msg(e.to_string()))
}

fn keys_file() -> String {
std::env::var("BPB_KEYS")
.unwrap_or_else(|_| format!("{}/.bpb_keys.toml", std::env::var("HOME").unwrap()))
Expand Down

0 comments on commit adca322

Please sign in to comment.