kaniop_operator/crd.rs
1use kaniop_k8s_util::types::get_first_cloned;
2
3use kanidm_proto::{
4 constants::{ATTR_GIDNUMBER, ATTR_LOGINSHELL},
5 v1::Entry,
6};
7
8#[cfg(feature = "schemars")]
9use schemars::JsonSchema;
10use serde::{Deserialize, Serialize};
11
12/// Checks if a given value is equal to its type's default value.
13pub fn is_default<T: Default + PartialEq>(value: &T) -> bool {
14 #[cfg(feature = "examples-gen")]
15 {
16 // When generating examples, never skip fields so users can see all available options
17 let _ = value; // Suppress unused variable warning
18 false
19 }
20 #[cfg(not(feature = "examples-gen"))]
21 {
22 value == &T::default()
23 }
24}
25
26/// KanidmRef is a reference to a Kanidm object in the same cluster. It is used to specify where
27/// the object is stored.
28#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq)]
29#[cfg_attr(feature = "schemars", derive(JsonSchema))]
30#[schemars(extend("x-kubernetes-validations" = [{"message": "Value is immutable", "rule": "self == oldSelf"}]))]
31#[serde(rename_all = "camelCase")]
32pub struct KanidmRef {
33 pub name: String,
34
35 /// For cross-namespace resources. Reference Kanidm namespace. If omitted, the namespace of the
36 /// resource will be used.
37 #[serde(skip_serializing_if = "Option::is_none")]
38 pub namespace: Option<String>,
39}
40
41/// Kanidm has features that enable its accounts and groups to be consumed on POSIX-like machines,
42/// such as Linux, FreeBSD or others. Both service accounts and person accounts can be used on POSIX
43/// systems.
44///
45/// The attributes defined here are set by the operator. If you want to manage those attributes
46/// from the database, do not set them here.
47/// Additionally, if you unset them here, they will be kept in the database.
48#[derive(Serialize, Deserialize, Clone, Debug, Default)]
49#[cfg_attr(feature = "schemars", derive(JsonSchema))]
50#[serde(rename_all = "camelCase")]
51pub struct KanidmPersonPosixAttributes {
52 /// The group ID number (GID) for the person account. In Kanidm there is no difference between
53 /// a UID and a GID number.
54 ///
55 /// If omitted, Kanidm will generate it automatically.
56 ///
57 /// More info:
58 /// https://kanidm.github.io/kanidm/stable/accounts/posix_accounts_and_groups.html#uid-and-gid-numbers
59 pub gidnumber: Option<u32>,
60 /// The login shell for the person account.
61 ///
62 /// This sets the default shell that will be used when the user logs in via SSH or other
63 /// mechanisms that require a shell. Common values include /bin/bash, /bin/zsh, /bin/sh.
64 pub loginshell: Option<String>,
65}
66
67impl PartialEq for KanidmPersonPosixAttributes {
68 /// Compare attributes defined in the first object with the second object values.
69 /// If the second object has more attributes defined, they will be ignored.
70 fn eq(&self, other: &Self) -> bool {
71 (self.gidnumber.is_none() || self.gidnumber == other.gidnumber)
72 && (self.loginshell.is_none() || self.loginshell == other.loginshell)
73 }
74}
75
76impl From<Entry> for KanidmPersonPosixAttributes {
77 fn from(entry: Entry) -> Self {
78 KanidmPersonPosixAttributes {
79 gidnumber: get_first_cloned(&entry, ATTR_GIDNUMBER).and_then(|s| s.parse::<u32>().ok()),
80 loginshell: get_first_cloned(&entry, ATTR_LOGINSHELL),
81 }
82 }
83}