Add regen-key ability if can't read key

This commit is contained in:
asonix 2020-04-21 17:47:34 -05:00
parent b8336b441c
commit dea8c60042
4 changed files with 30 additions and 12 deletions

15
Cargo.lock generated
View file

@ -1670,9 +1670,9 @@ dependencies = [
[[package]] [[package]]
name = "parking_lot_core" name = "parking_lot_core"
version = "0.7.1" version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e136c1904604defe99ce5fd71a28d473fa60a12255d511aa78a9ddf11237aeb" checksum = "d58c7c768d4ba344e3e8d72518ac13e259d7c7ade24167003b8488e10b6740a3"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"cloudabi", "cloudabi",
@ -2049,9 +2049,9 @@ dependencies = [
[[package]] [[package]]
name = "rsa-magic-public-key" name = "rsa-magic-public-key"
version = "0.1.0" version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dfe89053790d2de7953814284a0d4b64edab2c9619103c6df7d133135fcfdafe" checksum = "11bc74e37efd50ace66f9f250bb917d1370bee373b9d974f3873cda073d8e5ac"
dependencies = [ dependencies = [
"base64 0.11.0", "base64 0.11.0",
"num-bigint-dig", "num-bigint-dig",
@ -2062,7 +2062,8 @@ dependencies = [
[[package]] [[package]]
name = "rsa-pem" name = "rsa-pem"
version = "0.1.0" version = "0.1.0"
source = "git+https://git.asonix.dog/Aardwolf/rsa-pem#8dc04bd060d7993058c120f5cbfa654890113614" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f5c8511d542ea4c96e1f7c4e8fb8010adceb741d3ff5d92673db44954b4abae"
dependencies = [ dependencies = [
"bit-vec", "bit-vec",
"log", "log",
@ -2335,9 +2336,9 @@ checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
[[package]] [[package]]
name = "standback" name = "standback"
version = "0.2.5" version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6389164ce46e8a68e1b373787efcca3b6b6620bb50b12d4e8d14380838db316f" checksum = "898341a519ec30272f8665dd05fb00156254310e99df3dc1e0cb096883d8656c"
[[package]] [[package]]
name = "static_assertions" name = "static_assertions"

View file

@ -38,8 +38,8 @@ num_cpus = "1.12"
pretty_env_logger = "0.4.0" pretty_env_logger = "0.4.0"
rand = "0.7" rand = "0.7"
rsa = "0.2" rsa = "0.2"
rsa-magic-public-key = "0.1.0" rsa-magic-public-key = "0.1.1"
rsa-pem = { version = "0.1.0", git = "https://git.asonix.dog/Aardwolf/rsa-pem" } rsa-pem = "0.1.0"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
sha2 = "0.8" sha2 = "0.8"

View file

@ -155,7 +155,7 @@ impl State {
let f3 = db.hydrate_listeners(); let f3 = db.hydrate_listeners();
let f4 = async move { let f4 = async move {
if let Some(key) = db.hydrate_private_key().await? { if let Ok(Some(key)) = db.hydrate_private_key().await {
Ok(key) Ok(key)
} else { } else {
info!("Generating new keys"); info!("Generating new keys");

View file

@ -165,8 +165,25 @@ impl Db {
pub async fn update_private_key(&self, private_key: &RSAPrivateKey) -> Result<(), MyError> { pub async fn update_private_key(&self, private_key: &RSAPrivateKey) -> Result<(), MyError> {
let pem_pkcs8 = private_key.to_pem_pkcs8()?; let pem_pkcs8 = private_key.to_pem_pkcs8()?;
info!("INSERT INTO settings (key, value, created_at) VALUES ('private_key', $1::TEXT, 'now');"); info!(
self.pool.get().await?.execute("INSERT INTO settings (key, value, created_at) VALUES ('private_key', $1::TEXT, 'now');", &[&pem_pkcs8]).await?; "INSERT INTO settings (key, value, created_at)
VALUES ('private_key', $1::TEXT, 'now')
ON CONFLICT (key)
DO UPDATE
SET value = $1::TEXT;"
);
self.pool
.get()
.await?
.execute(
"INSERT INTO settings (key, value, created_at)
VALUES ('private_key', $1::TEXT, 'now')
ON CONFLICT (key)
DO UPDATE
SET value = $1::TEXT;",
&[&pem_pkcs8],
)
.await?;
Ok(()) Ok(())
} }
} }