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: {:?}",
44 new_max_queryable_attrs
45 );
46 let client = copt.to_client(OpType::Write).await;
47 match client
48 .idm_domain_set_ldap_max_queryable_attrs(*new_max_queryable_attrs)
49 .await
50 {
51 Ok(_) => println!("Success"),
52 Err(e) => handle_client_error(e, copt.output_mode),
53 }
54 }
55 DomainOpt::SetLdapBasedn { copt, new_basedn } => {
56 eprintln!(
57 "Attempting to set the domain's ldap basedn to: {:?}",
58 new_basedn
59 );
60 let client = copt.to_client(OpType::Write).await;
61 match client.idm_domain_set_ldap_basedn(new_basedn).await {
62 Ok(_) => println!("Success"),
63 Err(e) => handle_client_error(e, copt.output_mode),
64 }
65 }
66 DomainOpt::SetLdapAllowUnixPasswordBind { copt, enable } => {
67 let client = copt.to_client(OpType::Write).await;
68 match client.idm_set_ldap_allow_unix_password_bind(*enable).await {
69 Ok(_) => println!("Success"),
70 Err(e) => handle_client_error(e, copt.output_mode),
71 }
72 }
73 DomainOpt::SetAllowEasterEggs { copt, enable } => {
74 let client = copt.to_client(OpType::Write).await;
75 match client.idm_set_domain_allow_easter_eggs(*enable).await {
76 Ok(_) => {
77 if *enable {
78 println!("Success 🎉 🥚 🎉")
79 } else {
80 println!("Success")
81 }
82 }
83 Err(e) => handle_client_error(e, copt.output_mode),
84 }
85 }
86 DomainOpt::Show(copt) => {
87 let client = copt.to_client(OpType::Read).await;
88 match client.idm_domain_get().await {
89 Ok(e) => println!("{}", e),
90 Err(e) => handle_client_error(e, copt.output_mode),
91 }
92 }
93 DomainOpt::RevokeKey { copt, key_id } => {
94 let client = copt.to_client(OpType::Write).await;
95 match client.idm_domain_revoke_key(key_id).await {
96 Ok(_) => println!("Success"),
97 Err(e) => handle_client_error(e, copt.output_mode),
98 }
99 }
100 DomainOpt::SetImage {
101 copt,
102 path,
103 image_type,
104 } => {
105 let client = copt.to_client(OpType::Write).await;
106 let img_res: Result<ImageValue, Error> = (move || {
107 let file_name = path
108 .file_name()
109 .context("Please pass a file")?
110 .to_str()
111 .context("Path contains non utf-8")?
112 .to_string();
113
114 let image_type = match image_type {
115 Some(val) => val.clone(),
116 None => {
117 path
118 .extension().context("Path has no extension so we can't infer the imageType, or you could pass the optional imageType argument yourself.")?
119 .to_str().context("Path contains invalid utf-8")?
120 .try_into()
121 .map_err(Error::msg)?
122 }
123 };
124
125 let read_res = read(path);
126 match read_res {
127 Ok(data) => Ok(ImageValue::new(file_name, image_type, data)),
128 Err(err) => {
129 if copt.debug {
130 eprintln!(
131 "{}",
132 kanidm_lib_file_permissions::diagnose_path(path.as_ref())
133 );
134 }
135 Err(err).context(format!("Failed to read file at '{}'", path.display()))
136 }
137 }
138 })();
139
140 let img = match img_res {
141 Ok(img) => img,
142 Err(err) => {
143 eprintln!("{err}");
144 return;
145 }
146 };
147
148 match client.idm_domain_update_image(img).await {
149 Ok(_) => println!("Success"),
150 Err(e) => handle_client_error(e, copt.output_mode),
151 }
152 }
153 DomainOpt::RemoveImage { copt } => {
154 let client = copt.to_client(OpType::Write).await;
155
156 match client.idm_domain_delete_image().await {
157 Ok(_) => println!("Success"),
158 Err(e) => handle_client_error(e, copt.output_mode),
159 }
160 }
161 }
162 }
163}