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    /// Only KanidmOAuth2Client can be cross-namespace. It is ignored for other resources.
36    #[serde(skip_serializing_if = "Option::is_none")]
37    pub namespace: Option<String>,
38}
39
40/// Kanidm has features that enable its accounts and groups to be consumed on POSIX-like machines,
41/// such as Linux, FreeBSD or others. Both service accounts and person accounts can be used on POSIX
42/// systems.
43///
44/// The attributes defined here are set by the operator. If you want to manage those attributes
45/// from the database, do not set them here.
46/// Additionally, if you unset them here, they will be kept in the database.
47#[derive(Serialize, Deserialize, Clone, Debug, Default)]
48#[cfg_attr(feature = "schemars", derive(JsonSchema))]
49#[serde(rename_all = "camelCase")]
50pub struct KanidmPersonPosixAttributes {
51    /// The group ID number (GID) for the person account. In Kanidm there is no difference between
52    /// a UID and a GID number.
53    ///
54    /// If omitted, Kanidm will generate it automatically.
55    ///
56    /// More info:
57    /// https://kanidm.github.io/kanidm/stable/accounts/posix_accounts_and_groups.html#uid-and-gid-numbers
58    pub gidnumber: Option<u32>,
59    /// The login shell for the person account.
60    ///
61    /// This sets the default shell that will be used when the user logs in via SSH or other
62    /// mechanisms that require a shell. Common values include /bin/bash, /bin/zsh, /bin/sh.
63    pub loginshell: Option<String>,
64}
65
66impl PartialEq for KanidmPersonPosixAttributes {
67    /// Compare attributes defined in the first object with the second object values.
68    /// If the second object has more attributes defined, they will be ignored.
69    fn eq(&self, other: &Self) -> bool {
70        (self.gidnumber.is_none() || self.gidnumber == other.gidnumber)
71            && (self.loginshell.is_none() || self.loginshell == other.loginshell)
72    }
73}
74
75impl From<Entry> for KanidmPersonPosixAttributes {
76    fn from(entry: Entry) -> Self {
77        KanidmPersonPosixAttributes {
78            gidnumber: get_first_cloned(&entry, ATTR_GIDNUMBER).and_then(|s| s.parse::<u32>().ok()),
79            loginshell: get_first_cloned(&entry, ATTR_LOGINSHELL),
80        }
81    }
82}