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