|
| 1 | +use std::{collections::HashSet, process::Command}; |
| 2 | + |
| 3 | +use assert_cmd::prelude::*; |
| 4 | +use cosmian_kms_cli::reexport::test_kms_server::start_default_test_kms_server; |
| 5 | + |
| 6 | +use crate::{ |
| 7 | + config::COSMIAN_CLI_CONF_ENV, |
| 8 | + error::{CosmianError, result::CosmianResult}, |
| 9 | + tests::{ |
| 10 | + PROG_NAME, |
| 11 | + kms::{ |
| 12 | + KMS_SUBCOMMAND, |
| 13 | + utils::{extract_uids::extract_unique_identifier, recover_cmd_logs}, |
| 14 | + }, |
| 15 | + save_kms_cli_config, |
| 16 | + }, |
| 17 | +}; |
| 18 | + |
| 19 | +#[derive(Default)] |
| 20 | +pub(crate) struct SecretDataOptions { |
| 21 | + pub(crate) tags: HashSet<String>, |
| 22 | + pub(crate) sensitive: bool, |
| 23 | + pub(crate) key_id: Option<String>, |
| 24 | +} |
| 25 | + |
| 26 | +pub(crate) fn create_secret_data( |
| 27 | + cli_conf_path: &str, |
| 28 | + options: &SecretDataOptions, |
| 29 | +) -> CosmianResult<String> { |
| 30 | + let mut cmd = Command::cargo_bin(PROG_NAME)?; |
| 31 | + cmd.env(COSMIAN_CLI_CONF_ENV, cli_conf_path); |
| 32 | + |
| 33 | + let mut args = vec!["secret-data", "create"]; |
| 34 | + |
| 35 | + // add tags |
| 36 | + for tag in &options.tags { |
| 37 | + args.push("--tag"); |
| 38 | + args.push(tag); |
| 39 | + } |
| 40 | + if options.sensitive { |
| 41 | + args.push("--sensitive"); |
| 42 | + } |
| 43 | + if let Some(key_id) = options.key_id.as_ref() { |
| 44 | + args.push(key_id); |
| 45 | + } |
| 46 | + cmd.arg(KMS_SUBCOMMAND).args(args); |
| 47 | + |
| 48 | + let output = recover_cmd_logs(&mut cmd); |
| 49 | + if output.status.success() { |
| 50 | + let secret_data_output = std::str::from_utf8(&output.stdout)?; |
| 51 | + assert!(secret_data_output.contains("The secret data was successfully generated.")); |
| 52 | + let secret_data_id = extract_unique_identifier(secret_data_output) |
| 53 | + .ok_or_else(|| CosmianError::Default("failed extracting the private key".to_owned()))? |
| 54 | + .to_owned(); |
| 55 | + return Ok(secret_data_id) |
| 56 | + } |
| 57 | + |
| 58 | + Err(CosmianError::Default( |
| 59 | + std::str::from_utf8(&output.stderr)?.to_owned(), |
| 60 | + )) |
| 61 | +} |
| 62 | + |
| 63 | +#[tokio::test] |
| 64 | +pub(crate) async fn test_secret_data() -> CosmianResult<()> { |
| 65 | + // from specs |
| 66 | + let ctx = start_default_test_kms_server().await; |
| 67 | + let (owner_client_conf_path, _) = save_kms_cli_config(ctx); |
| 68 | + create_secret_data( |
| 69 | + &owner_client_conf_path, |
| 70 | + &SecretDataOptions { |
| 71 | + tags: HashSet::from_iter(vec!["tag1".to_owned(), "tag2".to_owned()]), |
| 72 | + ..Default::default() |
| 73 | + }, |
| 74 | + )?; |
| 75 | + |
| 76 | + let created_id = create_secret_data( |
| 77 | + &owner_client_conf_path, |
| 78 | + &SecretDataOptions { |
| 79 | + key_id: Some("secret_id".to_owned()), |
| 80 | + tags: HashSet::from_iter(vec!["tag1".to_owned(), "tag2".to_owned()]), |
| 81 | + ..Default::default() |
| 82 | + }, |
| 83 | + )?; |
| 84 | + assert_eq!(created_id, "secret_id".to_owned()); |
| 85 | + Ok(()) |
| 86 | +} |
0 commit comments