Port pem_to_public_dnskey to clap4 derive API.

This commit is contained in:
Axel Viala
2022-10-16 15:48:10 +02:00
committed by Benjamin Fry
parent f587ea2289
commit 09cf3b92f7

View File

@@ -22,53 +22,58 @@
use std::fs::{File, OpenOptions}; use std::fs::{File, OpenOptions};
use std::io::{BufReader, Read, Write}; use std::io::{BufReader, Read, Write};
use std::path::PathBuf;
use clap::{Arg, ArgMatches, Command}; use clap::Parser;
use openssl::pkey::PKey; use openssl::pkey::PKey;
use tracing::info; use tracing::info;
use trust_dns_client::rr::dnssec::{KeyPair, Public}; use trust_dns_client::rr::dnssec::{KeyPair, Public};
fn args() -> ArgMatches { /// Cli struct for all options managed with clap derive api.
Command::new("Trust-DNS pem-to-public-dnskey") #[derive(Debug, Parser)]
.version(trust_dns_client::version()) #[clap(
.author("Benjamin Fry <benjaminfry@me.com>") name = "Trust-DNS pem-to-public-dnskey",
.about( version,
"Converts a PEM formatted public key into a raw public dnskey (not the inverse of dnskey-to-pem). This can be used to create a dnskey in the TrustAnchor internal format in Trust-DNS.", about = "Converts a PEM formatted public key into a raw public dnskey (not the inverse of dnskey-to-pem). This can be used to create a dnskey in the TrustAnchor internal format in Trust-DNS.",
) author = "Benjamin Fry <benjaminfry@me.com>"
.arg( )]
Arg::new("key") struct Cli {
.value_name("PEM_KEY_FILE") /// Input PEM FILE from which to read the public key
.help("Input PEM FILE from which to read the public key") #[arg(
.required(true) long = "key",
.num_args(1) value_name = "PEM_KEY_FILE",
.index(1), value_hint=clap::ValueHint::FilePath,
) )]
.arg( pub(crate) key: PathBuf,
Arg::new("output")
.value_name("OUTPUT_FILE") /// Output FILE to write the dnskey defaults to `out.dnskey`
.long("output") #[arg(
.short('o') short = 'o',
.num_args(1) long = "output",
.required(false) default_value = "out.pem",
.help("Output FILE to write to") value_name = "OUTPUT_FILE",
.default_value("out.dnskey"), value_hint=clap::ValueHint::FilePath,
) )]
.get_matches() pub(crate) output: PathBuf,
} }
/// Run the pem_to_public_dnskey program /// Run the pem_to_public_dnskey program
pub fn main() { pub fn main() {
trust_dns_util::logger(env!("CARGO_BIN_NAME"), Some(tracing::Level::INFO)); trust_dns_util::logger(env!("CARGO_BIN_NAME"), Some(tracing::Level::INFO));
let matches = args(); let args = Cli::parse();
let key_path = args.key;
let output_path = args.output;
let key_path = matches.get_one::<String>("key").unwrap(); info!("Reading key from pem: {}", key_path.display());
let output_path = matches.get_one::<String>("output").unwrap();
info!("Reading key from pem: {}", key_path); let mut key_file = File::open(&key_path).unwrap_or_else(|_| {
panic!(
let mut key_file = File::open(key_path).expect("private key file could not be opened"); "private key file <{}> could not be opened",
key_path.display()
)
});
let pkey = read_pem(&mut key_file); let pkey = read_pem(&mut key_file);
let key_pair = into_key_pair(pkey); let key_pair = into_key_pair(pkey);
@@ -80,7 +85,7 @@ pub fn main() {
let mut public_key_file = OpenOptions::new() let mut public_key_file = OpenOptions::new()
.write(true) .write(true)
.create_new(true) .create_new(true)
.open(output_path) .open(&output_path)
.expect("could not open public_key file for writing"); .expect("could not open public_key file for writing");
public_key_file public_key_file