kanidm_cli/domain/
mod.rs
1use crate::common::OpType;
2use crate::{handle_client_error, DomainOpt};
3use anyhow::{Context, Error};
4use kanidm_proto::internal::ImageValue;
5use std::fs::read;
6
7impl DomainOpt {
8 pub fn debug(&self) -> bool {
9 match self {
10 DomainOpt::SetDisplayname(copt) => copt.copt.debug,
11 DomainOpt::SetLdapBasedn { copt, .. }
12 | DomainOpt::SetImage { copt, .. }
13 | DomainOpt::RemoveImage { copt }
14 | DomainOpt::SetLdapAllowUnixPasswordBind { copt, .. }
15 | DomainOpt::SetAllowEasterEggs { copt, .. }
16 | DomainOpt::RevokeKey { copt, .. }
17 | DomainOpt::Show(copt)
18 | DomainOpt::SetLdapMaxQueryableAttrs { copt, .. } => copt.debug,
19 }
20 }
21
22 pub async fn exec(&self) {
23 match self {
24 DomainOpt::SetDisplayname(opt) => {
25 eprintln!(
26 "Attempting to set the domain's display name to: {:?}",
27 opt.new_display_name
28 );
29 let client = opt.copt.to_client(OpType::Write).await;
30 match client
31 .idm_domain_set_display_name(&opt.new_display_name)
32 .await
33 {
34 Ok(_) => println!("Success"),
35 Err(e) => handle_client_error(e, opt.copt.output_mode),
36 }
37 }
38 DomainOpt::SetLdapMaxQueryableAttrs {
39 copt,
40 new_max_queryable_attrs,
41 } => {
42 eprintln!(
43 "Attempting to set the maximum number of queryable LDAP attributes to: {new_max_queryable_attrs:?}"
44 );
45 let client = copt.to_client(OpType::Write).await;
46 match client
47 .idm_domain_set_ldap_max_queryable_attrs(*new_max_queryable_attrs)
48 .await
49 {
50 Ok(_) => println!("Success"),
51 Err(e) => handle_client_error(e, copt.output_mode),
52 }
53 }
54 DomainOpt::SetLdapBasedn { copt, new_basedn } => {
55 eprintln!("Attempting to set the domain's ldap basedn to: {new_basedn:?}");
56 let client = copt.to_client(OpType::Write).await;
57 match client.idm_domain_set_ldap_basedn(new_basedn).await {
58 Ok(_) => println!("Success"),
59 Err(e) => handle_client_error(e, copt.output_mode),
60 }
61 }
62 DomainOpt::SetLdapAllowUnixPasswordBind { copt, enable } => {
63 let client = copt.to_client(OpType::Write).await;
64 match client.idm_set_ldap_allow_unix_password_bind(*enable).await {
65 Ok(_) => println!("Success"),
66 Err(e) => handle_client_error(e, copt.output_mode),
67 }
68 }
69 DomainOpt::SetAllowEasterEggs { copt, enable } => {
70 let client = copt.to_client(OpType::Write).await;
71 match client.idm_set_domain_allow_easter_eggs(*enable).await {
72 Ok(_) => {
73 if *enable {
74 println!("Success 🎉 🥚 🎉")
75 } else {
76 println!("Success")
77 }
78 }
79 Err(e) => handle_client_error(e, copt.output_mode),
80 }
81 }
82 DomainOpt::Show(copt) => {
83 let client = copt.to_client(OpType::Read).await;
84 match client.idm_domain_get().await {
85 Ok(e) => println!("{e}"),
86 Err(e) => handle_client_error(e, copt.output_mode),
87 }
88 }
89 DomainOpt::RevokeKey { copt, key_id } => {
90 let client = copt.to_client(OpType::Write).await;
91 match client.idm_domain_revoke_key(key_id).await {
92 Ok(_) => println!("Success"),
93 Err(e) => handle_client_error(e, copt.output_mode),
94 }
95 }
96 DomainOpt::SetImage {
97 copt,
98 path,
99 image_type,
100 } => {
101 let client = copt.to_client(OpType::Write).await;
102 let img_res: Result<ImageValue, Error> = (move || {
103 let file_name = path
104 .file_name()
105 .context("Please pass a file")?
106 .to_str()
107 .context("Path contains non utf-8")?
108 .to_string();
109
110 let image_type = match image_type {
111 Some(val) => val.clone(),
112 None => {
113 path
114 .extension().context("Path has no extension so we can't infer the imageType, or you could pass the optional imageType argument yourself.")?
115 .to_str().context("Path contains invalid utf-8")?
116 .try_into()
117 .map_err(Error::msg)?
118 }
119 };
120
121 let read_res = read(path);
122 match read_res {
123 Ok(data) => Ok(ImageValue::new(file_name, image_type, data)),
124 Err(err) => {
125 if copt.debug {
126 eprintln!(
127 "{}",
128 kanidm_lib_file_permissions::diagnose_path(path.as_ref())
129 );
130 }
131 Err(err).context(format!("Failed to read file at '{}'", path.display()))
132 }
133 }
134 })();
135
136 let img = match img_res {
137 Ok(img) => img,
138 Err(err) => {
139 eprintln!("{err}");
140 return;
141 }
142 };
143
144 match client.idm_domain_update_image(img).await {
145 Ok(_) => println!("Success"),
146 Err(e) => handle_client_error(e, copt.output_mode),
147 }
148 }
149 DomainOpt::RemoveImage { copt } => {
150 let client = copt.to_client(OpType::Write).await;
151
152 match client.idm_domain_delete_image().await {
153 Ok(_) => println!("Success"),
154 Err(e) => handle_client_error(e, copt.output_mode),
155 }
156 }
157 }
158 }
159}