feat(renew-cmd): added

This commit is contained in:
2023-07-09 20:03:20 +02:00
parent 591858ef05
commit cbb99138a9
7 changed files with 109 additions and 42 deletions

View File

@@ -1,9 +1,10 @@
use anyhow::bail;
use anyhow::{bail, Context};
use axum_extra::routing::TypedPath;
use clap::{Parser, Subcommand};
use reqwest::{Client, StatusCode};
use ssh_key::Certificate;
use std::path::PathBuf;
use std::process;
use std::time::{Duration, SystemTime};
use tokio::fs;
use tokio::io::{stdin, AsyncBufReadExt, BufReader};
@@ -45,6 +46,19 @@ pub struct UploadArgs {
files: Vec<PathBuf>,
}
#[derive(Parser)]
pub struct RenewCommandArgs {
/// Execute the renew command
#[clap(short = 'x')]
execute: bool,
/// Path to the CA private key
#[clap(long="ca", env = env_key!("CA_KEY"))]
ca_key: Option<PathBuf>,
/// Certificates to generate commands for
#[clap(env = env_key!("FILES"))]
files: Vec<PathBuf>,
}
#[derive(Parser)]
pub struct ClientCommand {
#[clap(subcommand)]
@@ -55,12 +69,14 @@ pub struct ClientCommand {
pub enum ClientCommands {
Fetch(FetchArgs),
Upload(UploadArgs),
RenewCommand(RenewCommandArgs),
}
pub async fn run(ClientCommand { cmd }: ClientCommand) -> anyhow::Result<()> {
match cmd {
ClientCommands::Fetch(args) => fetch(args).await,
ClientCommands::Upload(args) => upload(args).await,
ClientCommands::RenewCommand(args) => renew(args).await,
}
}
@@ -170,6 +186,40 @@ async fn fetch(
Ok(())
}
async fn renew(
RenewCommandArgs {
files,
ca_key,
execute,
}: RenewCommandArgs,
) -> anyhow::Result<()> {
for file in files.iter() {
let cert = load_cert(&file).await?;
if let Some(cert) = cert {
let command = renew_command(
&cert,
ca_key
.as_deref()
.map(|path| path.to_str())
.flatten()
.unwrap_or("ca"),
file.to_str(),
);
println!("{}", command);
if execute {
process::Command::new("sh")
.arg("-c")
.arg(&command)
.spawn()
.with_context(|| format!("{command}"))?;
}
} else {
bail!("{file:?} doesn't exist");
}
}
Ok(())
}
#[instrument(skip(client, current))]
async fn fetch_cert(
client: Client,