kanidm_cli/domain/
mod.rs

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