opgpcard: Add a parameter '--key-only' to the ssh command.

This outputs only one line, containing the ssh public key string, which is useful in scripts (e.g. in CI).
This commit is contained in:
Heiko Schaefer 2023-03-13 17:50:36 +01:00
parent 8b9e921db7
commit c460904925
No known key found for this signature in database
GPG key ID: 4A849A1904CCBD7D
2 changed files with 22 additions and 8 deletions

View file

@ -22,6 +22,9 @@ pub struct SshCommand {
help = "Identifier of the card to use"
)]
pub ident: Option<String>,
#[clap(long, help = "Only print the ssh public key")]
pub key_only: bool,
}
pub fn print_ssh(
@ -31,6 +34,8 @@ pub fn print_ssh(
) -> Result<()> {
let mut output = output::Ssh::default();
output.key_only(command.key_only);
let ident = command.ident;
let backend = pick_card_for_reading(ident)?;

View file

@ -8,12 +8,17 @@ use crate::{OutputBuilder, OutputFormat, OutputVariant, OutputVersion};
#[derive(Debug, Default, Serialize)]
pub struct Ssh {
key_only: bool, // only print ssh public key, in text mode
ident: String,
authentication_key_fingerprint: Option<String>,
ssh_public_key: Option<String>,
}
impl Ssh {
pub fn key_only(&mut self, ssh_key_only: bool) {
self.key_only = ssh_key_only;
}
pub fn ident(&mut self, ident: String) {
self.ident = ident;
}
@ -27,16 +32,20 @@ impl Ssh {
}
fn text(&self) -> Result<String, OpgpCardError> {
let mut s = format!("OpenPGP card {}\n\n", self.ident);
if !self.key_only {
let mut s = format!("OpenPGP card {}\n\n", self.ident);
if let Some(fp) = &self.authentication_key_fingerprint {
s.push_str(&format!("Authentication key fingerprint:\n{fp}\n\n"));
}
if let Some(key) = &self.ssh_public_key {
s.push_str(&format!("SSH public key:\n{key}\n"));
}
if let Some(fp) = &self.authentication_key_fingerprint {
s.push_str(&format!("Authentication key fingerprint:\n{fp}\n\n"));
}
if let Some(key) = &self.ssh_public_key {
s.push_str(&format!("SSH public key:\n{key}\n"));
}
Ok(s)
Ok(s)
} else {
Ok(self.ssh_public_key.clone().unwrap_or("".to_string()))
}
}
fn v1(&self) -> Result<SshV0, OpgpCardError> {