|
| 1 | +use clap::Parser; |
| 2 | +use derive_more::Deref; |
| 3 | + |
| 4 | +use crate::{ |
| 5 | + cli::command::{self, CommandExecutor}, |
| 6 | + errors::RvError, |
| 7 | +}; |
| 8 | + |
| 9 | +#[derive(Parser, Deref)] |
| 10 | +#[command(author, version, about = r#"Lists the names of the policies that are installed on the RustyVault server."#)] |
| 11 | +pub struct List { |
| 12 | + #[deref] |
| 13 | + #[command(flatten, next_help_heading = "HTTP Options")] |
| 14 | + http_options: command::HttpOptions, |
| 15 | + |
| 16 | + #[command(flatten, next_help_heading = "Output Options")] |
| 17 | + output: command::OutputOptions, |
| 18 | +} |
| 19 | + |
| 20 | +impl CommandExecutor for List { |
| 21 | + #[inline] |
| 22 | + fn main(&self) -> Result<(), RvError> { |
| 23 | + let client = self.client()?; |
| 24 | + let sys = client.sys(); |
| 25 | + |
| 26 | + match sys.list_policy() { |
| 27 | + Ok(ret) => { |
| 28 | + if ret.response_status == 200 { |
| 29 | + let value = ret.response_data.as_ref().unwrap(); |
| 30 | + let keys = &value["keys"]; |
| 31 | + if *keys == serde_json::from_str::<serde_json::Value>("[]").unwrap() { |
| 32 | + ret.print_debug_info(); |
| 33 | + println!("No policy"); |
| 34 | + return Err(RvError::ErrRequestNoData); |
| 35 | + } |
| 36 | + self.output.print_value(keys, false)?; |
| 37 | + } else if ret.response_status == 404 { |
| 38 | + ret.print_debug_info(); |
| 39 | + println!("No policy"); |
| 40 | + return Err(RvError::ErrRequestNoData); |
| 41 | + } else { |
| 42 | + ret.print_debug_info(); |
| 43 | + } |
| 44 | + } |
| 45 | + Err(e) => eprintln!("{}", e), |
| 46 | + } |
| 47 | + |
| 48 | + Ok(()) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +#[cfg(test)] |
| 53 | +mod test { |
| 54 | + use crate::test_utils::TestHttpServer; |
| 55 | + |
| 56 | + #[test] |
| 57 | + fn test_cli_policy_list() { |
| 58 | + let mut test_http_server = TestHttpServer::new("test_cli_policy_list", true); |
| 59 | + test_http_server.token = test_http_server.root_token.clone(); |
| 60 | + |
| 61 | + // list policy |
| 62 | + let ret = test_http_server.cli(&["policy", "list"], &[]); |
| 63 | + assert!(ret.is_ok()); |
| 64 | + #[cfg(windows)] |
| 65 | + assert_eq!(ret.unwrap(), "default \r\nroot \r\n"); |
| 66 | + #[cfg(not(windows))] |
| 67 | + assert_eq!(ret.unwrap(), "default \nroot \n"); |
| 68 | + |
| 69 | + // write a test policy |
| 70 | + let test_policy = r#"path "secret/" {}"#; |
| 71 | + let client = test_http_server.client().unwrap(); |
| 72 | + let sys = client.sys(); |
| 73 | + assert!(sys.write_policy("my-policy", test_policy).is_ok()); |
| 74 | + |
| 75 | + // list policy again |
| 76 | + let ret = test_http_server.cli(&["policy", "list"], &[]); |
| 77 | + assert!(ret.is_ok()); |
| 78 | + #[cfg(windows)] |
| 79 | + assert_eq!(ret.unwrap(), "default \r\nmy-policy \r\nroot \r\n"); |
| 80 | + #[cfg(not(windows))] |
| 81 | + assert_eq!(ret.unwrap(), "default \nmy-policy \nroot \n"); |
| 82 | + |
| 83 | + // list policy with table format |
| 84 | + let ret = test_http_server.cli(&["policy", "list"], &["--format=table"]); |
| 85 | + assert!(ret.is_ok()); |
| 86 | + #[cfg(windows)] |
| 87 | + assert_eq!(ret.unwrap(), "default \r\nmy-policy \r\nroot \r\n"); |
| 88 | + #[cfg(not(windows))] |
| 89 | + assert_eq!(ret.unwrap(), "default \nmy-policy \nroot \n"); |
| 90 | + |
| 91 | + // list policy with json format |
| 92 | + let ret = test_http_server.cli(&["policy", "list"], &["--format=json"]); |
| 93 | + assert!(ret.is_ok()); |
| 94 | + assert_eq!(ret.unwrap(), "[\n \"default\",\n \"my-policy\",\n \"root\"\n]\n"); |
| 95 | + |
| 96 | + // list policy with yaml format |
| 97 | + let ret = test_http_server.cli(&["policy", "list"], &["--format=yaml"]); |
| 98 | + assert!(ret.is_ok()); |
| 99 | + assert_eq!(ret.unwrap(), "- default\n- my-policy\n- root\n"); |
| 100 | + } |
| 101 | +} |
0 commit comments