kaniop_group/
crd.rs

1use kanidm_proto::{constants::ATTR_GIDNUMBER, v1::Entry};
2use kaniop_k8s_util::types::get_first_cloned;
3use kaniop_operator::controller::kanidm::KanidmResource;
4use kaniop_operator::crd::KanidmRef;
5use kaniop_operator::kanidm::crd::Kanidm;
6
7use k8s_openapi::apimachinery::pkg::apis::meta::v1::{Condition, LabelSelector};
8use kube::CustomResource;
9#[cfg(feature = "schemars")]
10use schemars::JsonSchema;
11use serde::{Deserialize, Serialize};
12
13/// Groups are a collection of other entities that exist within Kanidm.
14/// More info:
15/// https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
16#[derive(CustomResource, Serialize, Deserialize, Clone, Debug, Default, PartialEq)]
17#[cfg_attr(feature = "schemars", derive(JsonSchema))]
18#[kube(
19    category = "kaniop",
20    group = "kaniop.rs",
21    version = "v1beta1",
22    kind = "KanidmGroup",
23    plural = "kanidmgroups",
24    singular = "kanidmgroup",
25    shortname = "kg",
26    namespaced,
27    status = "KanidmGroupStatus",
28    doc = r#"The Kanidm group custom resource definition (CRD) defines a group in Kanidm."#,
29    printcolumn = r#"{"name":"Kanidm","type":"string","jsonPath":".status.kanidmRef"}"#,
30    printcolumn = r#"{"name":"ManagedBy","type":"string","jsonPath":".spec.entryManagedBy"}"#,
31    printcolumn = r#"{"name":"GID","type":"integer","jsonPath":".status.gid"}"#,
32    printcolumn = r#"{"name":"Ready","type":"boolean","jsonPath":".status.ready"}"#,
33    printcolumn = r#"{"name":"Age","type":"date","jsonPath":".metadata.creationTimestamp"}"#,
34    derive = "Default"
35)]
36#[serde(rename_all = "camelCase")]
37pub struct KanidmGroupSpec {
38    pub kanidm_ref: KanidmRef,
39
40    /// Optional name/spn of a group or account that have entry manager rights over this group.
41    #[serde(skip_serializing_if = "Option::is_none")]
42    pub entry_managed_by: Option<String>,
43
44    /// Set the exact list of mail addresses that this group is associated with. The first mail
45    /// address in the list is the `primary` and the remainder are aliases. Setting an empty list
46    /// will clear the mail attribute.
47    #[serde(skip_serializing_if = "Option::is_none")]
48    pub mail: Option<Vec<String>>,
49
50    /// Name or SPN of group members. Set the exact list of members that this group should contain,
51    /// removing any not listed in the set operation.
52    /// If you want to manage members from the database, do not set them here.
53    #[serde(skip_serializing_if = "Option::is_none")]
54    pub members: Option<Vec<String>>,
55
56    /// POSIX attributes for the group account. When specified, the operator will activate them.
57    /// If omitted, the operator retains the attributes in the database but ceases to manage them.
58    #[serde(skip_serializing_if = "Option::is_none")]
59    pub posix_attributes: Option<KanidmGroupPosixAttributes>,
60}
61
62impl KanidmResource for KanidmGroup {
63    #[inline]
64    fn kanidm_ref_spec(&self) -> &KanidmRef {
65        &self.spec.kanidm_ref
66    }
67
68    #[inline]
69    fn get_namespace_selector(kanidm: &Kanidm) -> &Option<LabelSelector> {
70        &kanidm.spec.group_namespace_selector
71    }
72}
73
74/// Kanidm has features that enable its accounts and groups to be consumed on POSIX-like machines,
75/// such as Linux, FreeBSD or others. Both service accounts and person accounts can be used on POSIX
76/// systems.
77///
78/// The attributes defined here are set by the operator. If you want to manage those attributes
79/// from the database, do not set them here.
80/// Additionally, if you unset them here, they will be kept in the database.
81#[derive(Serialize, Deserialize, Clone, Debug, Default)]
82#[cfg_attr(feature = "schemars", derive(JsonSchema))]
83#[serde(rename_all = "camelCase")]
84pub struct KanidmGroupPosixAttributes {
85    /// The group ID number (GID) for the group account.
86    ///
87    /// If omitted, Kanidm will generate it automatically.
88    pub gidnumber: Option<u32>,
89}
90
91impl PartialEq for KanidmGroupPosixAttributes {
92    /// Compare attributes defined in the first object with the second object values.
93    /// If the second object has more attributes defined, they will be ignored.
94    fn eq(&self, other: &Self) -> bool {
95        self.gidnumber.is_none() || self.gidnumber == other.gidnumber
96    }
97}
98
99impl From<Entry> for KanidmGroupPosixAttributes {
100    fn from(entry: Entry) -> Self {
101        KanidmGroupPosixAttributes {
102            gidnumber: get_first_cloned(&entry, ATTR_GIDNUMBER).and_then(|s| s.parse::<u32>().ok()),
103        }
104    }
105}
106
107/// Most recent observed status of the Kanidm Group. Read-only.
108///
109/// More info:
110/// https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
111#[derive(Serialize, Deserialize, Clone, Debug, Default)]
112#[cfg_attr(feature = "schemars", derive(JsonSchema))]
113#[serde(rename_all = "camelCase")]
114pub struct KanidmGroupStatus {
115    #[serde(default, skip_serializing_if = "Option::is_none")]
116    pub conditions: Option<Vec<Condition>>,
117
118    pub ready: bool,
119
120    #[serde(default, skip_serializing_if = "Option::is_none")]
121    pub gid: Option<u32>,
122
123    pub kanidm_ref: String,
124}