1use kanidm_proto::{
2 constants::{
3 ATTR_ALLOW_PRIMARY_CRED_FALLBACK, ATTR_AUTH_PASSWORD_MINIMUM_LENGTH,
4 ATTR_AUTH_SESSION_EXPIRY, ATTR_CREDENTIAL_TYPE_MINIMUM, ATTR_GIDNUMBER,
5 ATTR_LIMIT_SEARCH_MAX_FILTER_TEST, ATTR_LIMIT_SEARCH_MAX_RESULTS, ATTR_PRIVILEGE_EXPIRY,
6 ATTR_WEBAUTHN_ATTESTATION_CA_LIST, ENTRYCLASS_ACCOUNT_POLICY,
7 },
8 v1::Entry,
9};
10use kaniop_k8s_util::types::get_first_cloned;
11use kaniop_operator::controller::kanidm::KanidmResource;
12use kaniop_operator::crd::KanidmRef;
13use kaniop_operator::kanidm::crd::Kanidm;
14
15use std::fmt;
16
17use k8s_openapi::apimachinery::pkg::apis::meta::v1::{Condition, LabelSelector};
18use kube::CustomResource;
19#[cfg(feature = "schemars")]
20use schemars::JsonSchema;
21use serde::{Deserialize, Serialize};
22
23#[derive(CustomResource, Serialize, Deserialize, Clone, Debug, Default, PartialEq)]
27#[cfg_attr(feature = "schemars", derive(JsonSchema))]
28#[kube(
29 category = "kaniop",
30 group = "kaniop.rs",
31 version = "v1beta1",
32 kind = "KanidmGroup",
33 plural = "kanidmgroups",
34 singular = "kanidmgroup",
35 shortname = "kg",
36 namespaced,
37 status = "KanidmGroupStatus",
38 doc = r#"The Kanidm group custom resource definition (CRD) defines a group in Kanidm."#,
39 printcolumn = r#"{"name":"Kanidm","type":"string","jsonPath":".status.kanidmRef"}"#,
40 printcolumn = r#"{"name":"ManagedBy","type":"string","jsonPath":".spec.entryManagedBy"}"#,
41 printcolumn = r#"{"name":"GID","type":"integer","jsonPath":".status.gid"}"#,
42 printcolumn = r#"{"name":"Ready","type":"boolean","jsonPath":".status.ready"}"#,
43 printcolumn = r#"{"name":"Age","type":"date","jsonPath":".metadata.creationTimestamp"}"#,
44 derive = "Default"
45)]
46#[serde(rename_all = "camelCase")]
47pub struct KanidmGroupSpec {
48 pub kanidm_ref: KanidmRef,
49
50 #[schemars(extend("x-kubernetes-validations" = [{"message": "kanidmName cannot be changed.", "rule": "self == oldSelf"}]))]
55 #[serde(skip_serializing_if = "Option::is_none")]
56 pub kanidm_name: Option<String>,
57
58 #[serde(skip_serializing_if = "Option::is_none")]
60 pub entry_managed_by: Option<String>,
61
62 #[serde(skip_serializing_if = "Option::is_none")]
66 pub mail: Option<Vec<String>>,
67
68 #[serde(skip_serializing_if = "Option::is_none")]
72 pub members: Option<Vec<String>>,
73
74 #[serde(skip_serializing_if = "Option::is_none")]
77 pub posix_attributes: Option<KanidmGroupPosixAttributes>,
78
79 #[serde(skip_serializing_if = "Option::is_none")]
90 pub account_policy: Option<KanidmGroupAccountPolicy>,
91}
92
93impl KanidmResource for KanidmGroup {
94 #[inline]
95 fn kanidm_ref_spec(&self) -> &KanidmRef {
96 &self.spec.kanidm_ref
97 }
98
99 #[inline]
100 fn get_namespace_selector(kanidm: &Kanidm) -> &Option<LabelSelector> {
101 &kanidm.spec.group_namespace_selector
102 }
103
104 #[inline]
105 fn kanidm_name_override(&self) -> Option<&str> {
106 self.spec.kanidm_name.as_deref()
107 }
108}
109
110#[derive(Serialize, Deserialize, Clone, Debug, Default)]
118#[cfg_attr(feature = "schemars", derive(JsonSchema))]
119#[serde(rename_all = "camelCase")]
120pub struct KanidmGroupPosixAttributes {
121 pub gidnumber: Option<u32>,
125}
126
127impl PartialEq for KanidmGroupPosixAttributes {
128 fn eq(&self, other: &Self) -> bool {
131 self.gidnumber.is_none() || self.gidnumber == other.gidnumber
132 }
133}
134
135impl From<Entry> for KanidmGroupPosixAttributes {
136 fn from(entry: Entry) -> Self {
137 KanidmGroupPosixAttributes {
138 gidnumber: get_first_cloned(&entry, ATTR_GIDNUMBER).and_then(|s| s.parse::<u32>().ok()),
139 }
140 }
141}
142
143#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq)]
149#[cfg_attr(feature = "schemars", derive(JsonSchema))]
150#[serde(rename_all = "snake_case")]
151pub enum CredentialTypeMinimum {
152 #[default]
154 Any,
155 Mfa,
157 Passkey,
159 AttestedPasskey,
161}
162
163impl fmt::Display for CredentialTypeMinimum {
164 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
165 match self {
166 CredentialTypeMinimum::Any => write!(f, "any"),
167 CredentialTypeMinimum::Mfa => write!(f, "mfa"),
168 CredentialTypeMinimum::Passkey => write!(f, "passkey"),
169 CredentialTypeMinimum::AttestedPasskey => write!(f, "attested_passkey"),
170 }
171 }
172}
173
174impl CredentialTypeMinimum {
175 pub fn from_kanidm_str(s: &str) -> Option<Self> {
177 match s.to_lowercase().as_str() {
178 "any" => Some(CredentialTypeMinimum::Any),
179 "mfa" => Some(CredentialTypeMinimum::Mfa),
180 "passkey" => Some(CredentialTypeMinimum::Passkey),
181 "attested_passkey" => Some(CredentialTypeMinimum::AttestedPasskey),
182 _ => None,
183 }
184 }
185}
186
187#[cfg(feature = "schemars")]
191fn credential_type_minimum_schema(
192 _generator: &mut schemars::generate::SchemaGenerator,
193) -> schemars::Schema {
194 schemars::json_schema!({
195 "description": "Minimum security strength of credentials that may be assigned to accounts affected by this policy. In order from weakest to strongest: any < mfa < passkey < attested_passkey.\n\nMore info: https://kanidm.github.io/kanidm/stable/accounts/account_policy.html#credential-type-minimum",
196 "type": "string",
197 "enum": ["any", "mfa", "passkey", "attested_passkey"],
198 "x-enum-descriptions": [
199 "Any credential type is allowed (weakest)",
200 "Multi-factor authentication required",
201 "Passkey (WebAuthn) required",
202 "Attested passkey required (strongest) - requires configuring WebAuthn attestation CA list"
203 ],
204 "nullable": true
205 })
206}
207
208#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq)]
220#[cfg_attr(feature = "schemars", derive(JsonSchema))]
221#[serde(rename_all = "camelCase")]
222pub struct KanidmGroupAccountPolicy {
223 #[serde(skip_serializing_if = "Option::is_none")]
230 pub auth_session_expiry: Option<u32>,
231
232 #[serde(skip_serializing_if = "Option::is_none")]
237 #[cfg_attr(
238 feature = "schemars",
239 schemars(schema_with = "credential_type_minimum_schema", default)
240 )]
241 pub credential_type_minimum: Option<CredentialTypeMinimum>,
242
243 #[serde(skip_serializing_if = "Option::is_none")]
245 pub password_minimum_length: Option<u32>,
246
247 #[cfg_attr(feature = "schemars", schemars(extend("x-kubernetes-validations" = [
252 {
253 "message": "privilegeExpiry must be at most 3600 seconds (1 hour)",
254 "rule": "self <= 3600"
255 }
256 ])))]
257 #[serde(skip_serializing_if = "Option::is_none")]
258 pub privilege_expiry: Option<u32>,
259
260 #[serde(skip_serializing_if = "Option::is_none")]
268 pub webauthn_attestation_ca_list: Option<String>,
269
270 #[serde(skip_serializing_if = "Option::is_none")]
274 pub allow_primary_cred_fallback: Option<bool>,
275
276 #[serde(skip_serializing_if = "Option::is_none")]
278 pub limit_search_max_results: Option<u32>,
279
280 #[serde(skip_serializing_if = "Option::is_none")]
282 pub limit_search_max_filter_test: Option<u32>,
283}
284
285#[derive(Clone, Debug, Default)]
287pub struct KanidmGroupAccountPolicyAttributes {
288 pub enabled: bool,
290 pub auth_session_expiry: Option<u32>,
291 pub credential_type_minimum: Option<CredentialTypeMinimum>,
292 pub password_minimum_length: Option<u32>,
293 pub privilege_expiry: Option<u32>,
294 pub webauthn_attestation_ca_list: Option<String>,
295 pub allow_primary_cred_fallback: Option<bool>,
296 pub limit_search_max_results: Option<u32>,
297 pub limit_search_max_filter_test: Option<u32>,
298}
299
300impl From<&Entry> for KanidmGroupAccountPolicyAttributes {
301 fn from(entry: &Entry) -> Self {
302 let enabled = entry
303 .attrs
304 .get("class")
305 .is_some_and(|classes| classes.iter().any(|c| c == ENTRYCLASS_ACCOUNT_POLICY));
306
307 KanidmGroupAccountPolicyAttributes {
308 enabled,
309 auth_session_expiry: get_first_cloned(entry, ATTR_AUTH_SESSION_EXPIRY)
310 .and_then(|s| s.parse::<u32>().ok()),
311 credential_type_minimum: get_first_cloned(entry, ATTR_CREDENTIAL_TYPE_MINIMUM)
312 .and_then(|s| CredentialTypeMinimum::from_kanidm_str(&s)),
313 password_minimum_length: get_first_cloned(entry, ATTR_AUTH_PASSWORD_MINIMUM_LENGTH)
314 .and_then(|s| s.parse::<u32>().ok()),
315 privilege_expiry: get_first_cloned(entry, ATTR_PRIVILEGE_EXPIRY)
316 .and_then(|s| s.parse::<u32>().ok()),
317 webauthn_attestation_ca_list: get_first_cloned(
318 entry,
319 ATTR_WEBAUTHN_ATTESTATION_CA_LIST,
320 ),
321 allow_primary_cred_fallback: get_first_cloned(entry, ATTR_ALLOW_PRIMARY_CRED_FALLBACK)
322 .and_then(|s| s.parse::<bool>().ok()),
323 limit_search_max_results: get_first_cloned(entry, ATTR_LIMIT_SEARCH_MAX_RESULTS)
324 .and_then(|s| s.parse::<u32>().ok()),
325 limit_search_max_filter_test: get_first_cloned(
326 entry,
327 ATTR_LIMIT_SEARCH_MAX_FILTER_TEST,
328 )
329 .and_then(|s| s.parse::<u32>().ok()),
330 }
331 }
332}
333
334#[derive(Serialize, Deserialize, Clone, Debug, Default)]
339#[cfg_attr(feature = "schemars", derive(JsonSchema))]
340#[serde(rename_all = "camelCase")]
341pub struct KanidmGroupStatus {
342 #[serde(default, skip_serializing_if = "Option::is_none")]
343 #[cfg_attr(
344 feature = "schemars",
345 schemars(extend("x-kubernetes-list-type" = "map", "x-kubernetes-list-map-keys" = ["type"]))
346 )]
347 pub conditions: Option<Vec<Condition>>,
348
349 pub ready: bool,
350
351 #[serde(default, skip_serializing_if = "Option::is_none")]
352 pub gid: Option<u32>,
353
354 pub kanidm_ref: String,
355}