Skip to main content

orca/
kani.rs

1use kanidm_client::{KanidmClient, KanidmClientBuilder};
2
3use crate::{error::Error, profile::Profile};
4
5// This client contains our admin and idm_admin connections that are
6// pre-authenticated for use against the kanidm server. In addition,
7// new clients can be requested for our test actors.
8pub struct KanidmOrcaClient {
9    #[allow(dead_code)]
10    admin_client: KanidmClient,
11    idm_admin_client: KanidmClient,
12    // In future we probably need a way to connect to all the nodes?
13    // Or we just need all their uris.
14}
15
16impl KanidmOrcaClient {
17    pub async fn new(profile: &Profile) -> Result<Self, Error> {
18        let admin_client = KanidmClientBuilder::new()
19            .address(profile.control_uri().to_string())
20            .danger_accept_invalid_hostnames(true)
21            .danger_accept_invalid_certs(true)
22            .request_timeout(1200)
23            .build()
24            .map_err(|err| {
25                error!(?err, "Unable to create kanidm client");
26                Error::KanidmClient
27            })?;
28
29        admin_client
30            .auth_simple_password("admin", profile.admin_password())
31            .await
32            .map_err(|err| {
33                error!(?err, "Unable to authenticate as admin");
34                Error::KanidmClient
35            })?;
36
37        let idm_admin_client = admin_client.new_session().map_err(|err| {
38            error!(?err, "Unable to create new session");
39            Error::KanidmClient
40        })?;
41
42        idm_admin_client
43            .auth_simple_password("idm_admin", profile.idm_admin_password())
44            .await
45            .map_err(|err| {
46                error!(?err, "Unable to authenticate as idm_admin");
47                Error::KanidmClient
48            })?;
49
50        Ok(KanidmOrcaClient {
51            admin_client,
52            idm_admin_client,
53        })
54    }
55
56    pub async fn disable_mfa_requirement(&self) -> Result<(), Error> {
57        self.idm_admin_client
58            .group_account_policy_credential_type_minimum_set("idm_all_persons", "any")
59            .await
60            .map_err(|err| {
61                error!(?err, "Unable to modify idm_all_persons policy");
62                Error::KanidmClient
63            })
64    }
65
66    pub async fn extend_privilege_expiry(&self) -> Result<(), Error> {
67        self.idm_admin_client
68            .group_account_policy_privilege_expiry_set("idm_all_persons", 3600)
69            .await
70            .map_err(|err| {
71                error!(?err, "Unable to modify idm_all_persons policy");
72                Error::KanidmClient
73            })?;
74
75        self.idm_admin_client
76            .group_account_policy_privilege_expiry_set("idm_all_accounts", 3600)
77            .await
78            .map_err(|err| {
79                error!(?err, "Unable to modify idm_all_accounts policy");
80                Error::KanidmClient
81            })
82    }
83
84    pub async fn person_exists(&self, username: &str) -> Result<bool, Error> {
85        self.idm_admin_client
86            .idm_person_account_get(username)
87            .await
88            .map(|e| e.is_some())
89            .map_err(|err| {
90                error!(?err, ?username, "Unable to check person");
91                Error::KanidmClient
92            })
93    }
94
95    pub async fn person_create(&self, username: &str, display_name: &str) -> Result<(), Error> {
96        self.idm_admin_client
97            .idm_person_account_create(username, display_name)
98            .await
99            .map_err(|err| {
100                error!(?err, ?username, "Unable to create person");
101                Error::KanidmClient
102            })
103    }
104
105    pub async fn person_set_primary_password_only(
106        &self,
107        username: &str,
108        password: &str,
109    ) -> Result<(), Error> {
110        self.idm_admin_client
111            .idm_person_account_primary_credential_set_password(username, password)
112            .await
113            .map_err(|err| {
114                error!(?err, ?username, "Unable to set person password");
115                Error::KanidmClient
116            })
117    }
118
119    pub async fn group_set_members(&self, group_name: &str, members: &[&str]) -> Result<(), Error> {
120        self.idm_admin_client
121            .idm_group_set_members(group_name, members)
122            .await
123            .map_err(|err| {
124                error!(?err, ?group_name, "Unable to set group members");
125                Error::KanidmClient
126            })
127    }
128
129    pub async fn group_add_members(&self, group_name: &str, members: &[&str]) -> Result<(), Error> {
130        self.idm_admin_client
131            .idm_group_add_members(group_name, members)
132            .await
133            .map_err(|err| {
134                error!(?err, ?group_name, "Unable to add group members");
135                Error::KanidmClient
136            })
137    }
138
139    pub async fn group_exists(&self, group_name: &str) -> Result<bool, Error> {
140        self.idm_admin_client
141            .idm_group_get(group_name)
142            .await
143            .map(|e| e.is_some())
144            .map_err(|err| {
145                error!(?err, ?group_name, "Unable to check group");
146                Error::KanidmClient
147            })
148    }
149
150    pub async fn group_create(&self, group_name: &str) -> Result<(), Error> {
151        self.idm_admin_client
152            .idm_group_create(group_name, Some("idm_admins"))
153            .await
154            .map_err(|err| {
155                error!(?err, ?group_name, "Unable to create group");
156                Error::KanidmClient
157            })
158    }
159}