From 5390a9389aed26e5e4f2142bd13d919b88dc09c9 Mon Sep 17 00:00:00 2001 From: TymanWasTaken Date: Fri, 30 Jun 2023 23:59:30 -0400 Subject: [PATCH] Handful of things - Update redb to fix regression when loading db multiple times (though I am likely just going to replace redb with sqlite) - Rename AspKeyType::EdDSA to AspKeyType::Ed25519 - Make `aspm keys list` look nicer --- .vscode/settings.json | 3 ++- Cargo.lock | 4 ++-- Cargo.toml | 2 +- crates/asp/src/keys/mod.rs | 18 +++++++++--------- src/commands/keys/generate.rs | 2 +- src/commands/keys/list.rs | 34 ++++++++++++++++++++++++++-------- src/db.rs | 4 ++-- 7 files changed, 43 insertions(+), 24 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index e6bd3b5..9154760 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -5,6 +5,7 @@ "Aspm", "josekit", "PKCS", - "Pkey" + "Pkey", + "printdoc" ] } \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 59638c8..091ce16 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1026,9 +1026,9 @@ dependencies = [ [[package]] name = "redb" -version = "1.0.2" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1770bc0931171df3ced2adc9fd72d59cb47a4dc693d184c73cd382067f6ff44e" +checksum = "f41f3c1c8851dafca590c6ccfaa254a694a8e002a754352e68fac81371988838" dependencies = [ "libc", "pyo3-build-config", diff --git a/Cargo.toml b/Cargo.toml index a6d18cf..865c72d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ thiserror = "1.0.40" asp = { path = "crates/asp" } indoc = "2.0.1" anstyle = "1.0.1" -redb = "1.0.2" +redb = "1.0.3" dialoguer = { version = "0.10.4", features = ["password"] } argon2 = { version = "0.5.0", features = ["std"] } data-encoding = "2.4.0" diff --git a/crates/asp/src/keys/mod.rs b/crates/asp/src/keys/mod.rs index 691ed9c..5013d60 100644 --- a/crates/asp/src/keys/mod.rs +++ b/crates/asp/src/keys/mod.rs @@ -15,7 +15,7 @@ use crate::utils::jwk::JwtExt; /// An enum representing the possible types of JWK for ASPs #[derive(Debug)] pub enum AspKeyType { - EdDSA, + Ed25519, ES256, } @@ -32,7 +32,7 @@ impl AspKey { match jwk.key_type() { "OKP" => match jwk.curve() { Some("Ed25519") => Ok(Self { - key_type: AspKeyType::EdDSA, + key_type: AspKeyType::Ed25519, fingerprint: jwk .get_fingerprint() .or(Err(AspKeyError::FingerprintError))?, @@ -67,7 +67,7 @@ impl AspKey { pub fn generate(key_type: AspKeyType) -> Result { let result: anyhow::Result = try { match key_type { - AspKeyType::EdDSA => { + AspKeyType::Ed25519 => { let jwk = Jwk::generate_ed_key(EdCurve::Ed25519)?; Self { key_type, @@ -90,14 +90,14 @@ impl AspKey { pub fn create_signer(&self) -> anyhow::Result> { Ok(match self.key_type { - AspKeyType::EdDSA => Box::new(Eddsa.signer_from_jwk(&self.jwk)?), + AspKeyType::Ed25519 => Box::new(Eddsa.signer_from_jwk(&self.jwk)?), AspKeyType::ES256 => Box::new(Es256.signer_from_jwk(&self.jwk)?), }) } pub fn create_verifier(&self) -> anyhow::Result> { Ok(match self.key_type { - AspKeyType::EdDSA => Box::new(Eddsa.verifier_from_jwk(&self.jwk)?), + AspKeyType::Ed25519 => Box::new(Eddsa.verifier_from_jwk(&self.jwk)?), AspKeyType::ES256 => Box::new(Es256.verifier_from_jwk(&self.jwk)?), }) } @@ -128,7 +128,7 @@ impl JwsHeaderExt for JwsHeader { fn set_asp_key(&mut self, key: &AspKey) -> anyhow::Result<()> { self.set_algorithm(match key.key_type { AspKeyType::ES256 => "ES256", - AspKeyType::EdDSA => "EdDSA", + AspKeyType::Ed25519 => "EdDSA", }); self.set_key_id(&key.fingerprint); self.set_jwk(key.jwk.to_public_key()?); @@ -159,7 +159,7 @@ mod tests { #[test] fn generate_eddsa() { - let key = AspKey::generate(AspKeyType::EdDSA); + let key = AspKey::generate(AspKeyType::Ed25519); assert!(key.is_ok(), "key should generate successfully"); let key = key.unwrap(); assert_eq!(key.jwk.key_type(), "OKP", "key should have type of OKP"); @@ -229,7 +229,7 @@ mod tests { fn export_encrypted() { let mut secret = [0u8; 32]; assert!(openssl::rand::rand_bytes(&mut secret).is_ok()); - let key = AspKey::generate(AspKeyType::EdDSA); + let key = AspKey::generate(AspKeyType::Ed25519); assert!(key.is_ok()); let jwe = key.unwrap().export_encrypted(&secret); assert!(jwe.is_ok()); @@ -239,7 +239,7 @@ mod tests { fn import_encrypted() { let mut secret = [0u8; 32]; assert!(openssl::rand::rand_bytes(&mut secret).is_ok()); - let key = AspKey::generate(AspKeyType::EdDSA).unwrap(); + let key = AspKey::generate(AspKeyType::Ed25519).unwrap(); let encrypted = key.export_encrypted(&secret); assert!(encrypted.is_ok()); let decrypted = AspKey::from_encrypted(&secret, &encrypted.unwrap()); diff --git a/src/commands/keys/generate.rs b/src/commands/keys/generate.rs index cfae183..16d8852 100644 --- a/src/commands/keys/generate.rs +++ b/src/commands/keys/generate.rs @@ -51,7 +51,7 @@ impl AspmSubcommand for KeysGenerateCommand { })?; let key = AspKey::generate(match self.key_type { - KeyGenerationType::Ed25519 => AspKeyType::EdDSA, + KeyGenerationType::Ed25519 => AspKeyType::Ed25519, KeyGenerationType::ES256 => AspKeyType::ES256, }) .context("Key generation failed for an unknown reason")?; diff --git a/src/commands/keys/list.rs b/src/commands/keys/list.rs index d86daf8..6cb4f5c 100644 --- a/src/commands/keys/list.rs +++ b/src/commands/keys/list.rs @@ -1,6 +1,7 @@ -use anstyle::{AnsiColor, Style as Anstyle}; +use anstyle::{AnsiColor, Reset, Style as Anstyle}; use anyhow::Context; use clap::Parser; +use indoc::printdoc; use redb::ReadableTable; use crate::{commands::AspmSubcommand, db::KEYS_TABLE}; @@ -19,25 +20,42 @@ impl AspmSubcommand for KeysListCommand { let iter = table.iter().context("Unable to read table entries")?; let entries: Vec<_> = iter.collect(); + // Construct styles + let reset = Reset::default().render(); let header_style = Anstyle::new() .bold() .underline() - .fg_color(Some(anstyle::Color::Ansi(AnsiColor::BrightGreen))); + .fg_color(Some(anstyle::Color::Ansi(AnsiColor::BrightMagenta))) + .render(); + let alias_style = Anstyle::new() + .underline() + .fg_color(Some(anstyle::Color::Ansi(AnsiColor::BrightCyan))) + .render(); + let key_style = Anstyle::new() + .fg_color(Some(anstyle::Color::Ansi(AnsiColor::BrightGreen))) + .render(); + let value_style = Anstyle::new() + .fg_color(Some(anstyle::Color::Ansi(AnsiColor::BrightYellow))) + .render(); + + // Print output println!( - "{style}Saved keys ({n} total):{reset}\n\n", - style = header_style.render(), + "{header_style}Saved keys ({n} total):{reset}\n", n = entries.len(), - reset = header_style.render_reset() ); for entry in entries.iter() { if let Ok((fingerprint, value)) = entry { let value = value.value(); - println!( - "{alias}: {fingerprint}/${key_type:?}", + printdoc! { + " + {alias_style}{alias}:{reset} + {key_style}Fingerprint{reset} {value_style}{fingerprint}{reset} + {key_style}Key Type{reset} {value_style}{key_type:?}{reset} + ", fingerprint = fingerprint.value(), key_type = value.key_type, alias = value.alias - ) + } } } diff --git a/src/db.rs b/src/db.rs index 99c3a17..ed7766e 100644 --- a/src/db.rs +++ b/src/db.rs @@ -92,7 +92,7 @@ impl RedbValue for KeysTableValue { .try_into() .unwrap(), key_type: match key_type_byte { - 0 => AspKeyType::EdDSA, + 0 => AspKeyType::Ed25519, 1 => AspKeyType::ES256, _ => panic!("parsing key table value failed: unknown key type byte found"), }, @@ -111,7 +111,7 @@ impl RedbValue for KeysTableValue { serialized.push(alias_bytes.len().try_into().unwrap()); // Add the first byte (alias length) serialized.extend_from_slice(alias_bytes.as_slice()); // Add the alias bytes serialized.push(match value.key_type { - AspKeyType::EdDSA => 0, + AspKeyType::Ed25519 => 0, AspKeyType::ES256 => 1, }); // Add the key type byte serialized.extend_from_slice(key_bytes); // Add the rest of the bytes, all of which are the key