Files
start-os/appmgr/src/ssh.rs
Keagan McClelland 8eaf8f47e4 Feature/ssh (#382)
* wip ssh feature

* add and remove complete, list remains

* list half way done

* should finish the ssh module completely

* minor fixes

* formatting and offline queries

Co-authored-by: Aiden McClelland <me@drbonez.dev>
2021-08-09 14:05:21 -06:00

178 lines
5.2 KiB
Rust

use std::path::Path;
use anyhow::anyhow;
use clap::ArgMatches;
use rpc_toolkit::command;
use sqlx::{Pool, Sqlite};
use crate::context::EitherContext;
use crate::util::{display_none, display_serializable, IoFormat};
use crate::Error;
static SSH_AUTHORIZED_KEYS_FILE: &str = "~/.ssh/authorized_keys";
#[derive(serde::Deserialize, serde::Serialize)]
pub struct PubKey(
#[serde(serialize_with = "crate::util::serialize_display")]
#[serde(deserialize_with = "crate::util::deserialize_from_str")]
openssh_keys::PublicKey,
);
#[derive(serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct SshKeyResponse {
pub alg: String,
pub hash: String,
pub hostname: String,
pub created_at: String,
}
impl std::fmt::Display for SshKeyResponse {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
f,
"{} {} {} {}",
self.created_at, self.alg, self.hash, self.hostname
)
}
}
impl std::str::FromStr for PubKey {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
s.parse().map(|pk| PubKey(pk)).map_err(|e| Error {
source: e.into(),
kind: crate::ErrorKind::ParseSshKey,
revision: None,
})
}
}
#[command(subcommands(add, remove, list,))]
pub fn ssh(#[context] ctx: EitherContext) -> Result<EitherContext, Error> {
Ok(ctx)
}
#[command(display(display_none))]
pub async fn add(#[context] ctx: EitherContext, #[arg] key: PubKey) -> Result<String, Error> {
let pool = &ctx.as_rpc().unwrap().secret_store;
// check fingerprint for duplicates
let fp = key.0.fingerprint_md5();
if sqlx::query!("SELECT * FROM ssh_keys WHERE fingerprint = ?", fp)
.fetch_optional(pool)
.await?
.is_none()
{
// if no duplicates, insert into DB
let raw_key = format!("{}", key.0);
sqlx::query!(
"INSERT INTO ssh_keys (fingerprint, openssh_pubkey, created_at) VALUES (?, ?, datetime('now'))"
, fp, raw_key).execute(pool).await?;
// insert into live key file, for now we actually do a wholesale replacement of the keys file, for maximum
// consistency
sync_keys_from_db(pool, Path::new(SSH_AUTHORIZED_KEYS_FILE)).await?;
}
// return fingerprint
Ok(fp)
}
#[command(display(display_none))]
pub async fn remove(
#[context] ctx: EitherContext,
#[arg] fingerprint: String,
) -> Result<(), Error> {
let pool = &ctx.as_rpc().unwrap().secret_store;
// check if fingerprint is in DB
// if in DB, remove it from DB
let n = sqlx::query!("DELETE FROM ssh_keys WHERE fingerprint = ?", fingerprint)
.execute(pool)
.await?
.rows_affected();
// if not in DB, Err404
if n == 0 {
Err(Error {
source: anyhow::anyhow!("SSH Key Not Found"),
kind: crate::error::ErrorKind::NotFound,
revision: None,
})
} else {
// AND overlay key file
sync_keys_from_db(pool, Path::new(SSH_AUTHORIZED_KEYS_FILE)).await?;
Ok(())
}
}
fn display_all_ssh_keys(all: Vec<SshKeyResponse>, matches: &ArgMatches<'_>) {
use prettytable::*;
if matches.is_present("format") {
return display_serializable(all, matches);
}
let mut table = Table::new();
table.add_row(row![bc =>
"CREATED AT",
"ALGORITHM",
"HASH",
"HOSTNAME",
]);
for key in all {
let row = row![
&format!("{}", key.created_at),
&key.alg,
&key.hash,
&key.hostname,
];
table.add_row(row);
}
table.print_tty(false);
}
#[command(display(display_all_ssh_keys))]
pub async fn list(
#[context] ctx: EitherContext,
#[allow(unused_variables)]
#[arg(long = "format")]
format: Option<IoFormat>,
) -> Result<Vec<SshKeyResponse>, Error> {
let pool = &ctx.as_rpc().unwrap().secret_store;
// list keys in DB and return them
let entries = sqlx::query!("SELECT fingerprint, openssh_pubkey, created_at FROM ssh_keys")
.fetch_all(pool)
.await?;
Ok(entries
.into_iter()
.map(|r| {
let k = PubKey(r.openssh_pubkey.parse().unwrap()).0;
let alg = k.keytype().to_owned();
let hash = k.fingerprint();
let hostname = k.comment.unwrap_or("".to_owned());
let created_at = r.created_at;
SshKeyResponse {
alg,
hash,
hostname,
created_at,
}
})
.collect())
}
pub async fn sync_keys_from_db(pool: &Pool<Sqlite>, dest: &Path) -> Result<(), Error> {
let keys = sqlx::query!("SELECT openssh_pubkey FROM ssh_keys")
.fetch_all(pool)
.await?;
let contents: String = keys
.into_iter()
.map(|k| format!("{}\n", k.openssh_pubkey))
.collect();
let ssh_dir = dest.parent().ok_or_else(|| {
Error::new(
anyhow!("SSH Key File cannot be \"/\""),
crate::ErrorKind::Filesystem,
)
})?;
if tokio::fs::metadata(ssh_dir).await.is_err() {
tokio::fs::create_dir_all(ssh_dir).await?;
}
std::fs::write(dest, contents).map_err(|e| e.into())
}