use std::path::PathBuf;
use clap::Parser;
use cosmian_kms_client::{
KmsClient,
kmip_2_1::kmip_types::UniqueIdentifier,
reexport::cosmian_kms_client_utils::configurable_kem_utils::{
KemAlgorithm, build_create_configurable_kem_keypair_request,
},
};
use cosmian_logger::debug;
use crate::{
actions::kms::console,
error::{
KmsCliError,
result::{KmsCliResult, KmsCliResultHelper},
},
};
#[derive(Parser)]
#[clap(verbatim_doc_comment)]
pub struct CreateKemKeyPairAction {
#[clap(long, short = 's')]
pub(crate) access_structure: Option<PathBuf>,
#[clap(long = "tag", short = 't', value_name = "TAG")]
pub(crate) tags: Vec<String>,
#[clap(long = "sensitive", default_value = "false")]
pub(crate) sensitive: bool,
#[clap(long = "kem", short = 'k', value_enum)]
pub(crate) kem_algorithm: KemAlgorithm,
#[clap(
long = "wrapping-key-id",
short = 'w',
required = false,
verbatim_doc_comment
)]
pub(crate) wrapping_key_id: Option<String>,
}
impl CreateKemKeyPairAction {
pub async fn run(
&self,
kms_rest_client: KmsClient,
) -> KmsCliResult<(UniqueIdentifier, UniqueIdentifier)> {
let access_structure = self
.access_structure
.as_ref()
.map(|path| {
let access_structure = std::fs::read_to_string(path)?;
debug!("access_structure: {access_structure:?}");
Ok::<_, KmsCliError>(access_structure)
})
.transpose()?;
let res = kms_rest_client
.create_key_pair(build_create_configurable_kem_keypair_request(
access_structure.as_deref(),
&self.tags,
self.kem_algorithm,
self.sensitive,
self.wrapping_key_id.as_ref(),
)?)
.await
.with_context(|| "failed creating a configurable-KEM key-pair")?;
let mut stdout =
console::Stdout::new("The configurable-KEM keypair has properly been generated.");
stdout.set_tags(Some(&self.tags));
stdout.set_key_pair_unique_identifier(
&res.private_key_unique_identifier,
&res.public_key_unique_identifier,
);
stdout.write()?;
Ok((
res.private_key_unique_identifier,
res.public_key_unique_identifier,
))
}
}