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;
5
6use k8s_openapi::apimachinery::pkg::apis::meta::v1::Condition;
7use kube::{CustomResource, ResourceExt};
8#[cfg(feature = "schemars")]
9use schemars::JsonSchema;
10use serde::{Deserialize, Serialize};
11
12/// Groups are a collection of other entities that exist within Kanidm.
13/// More info:
14/// https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
15#[derive(CustomResource, Serialize, Deserialize, Clone, Debug, Default, PartialEq)]
16#[cfg_attr(feature = "schemars", derive(JsonSchema))]
17#[kube(
18    category = "kaniop",
19    group = "kaniop.rs",
20    version = "v1beta1",
21    kind = "KanidmGroup",
22    plural = "groups",
23    singular = "kanidmgroup",
24    shortname = "kg",
25    namespaced,
26    status = "KanidmGroupStatus",
27    doc = r#"The Kanidm group custom resource definition (CRD) defines a group in Kanidm.
28    This resource has to be in the same namespace as the Kanidm cluster."#,
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    derive = "Default"
34)]
35#[serde(rename_all = "camelCase")]
36pub struct KanidmGroupSpec {
37    pub kanidm_ref: KanidmRef,
38
39    /// Optional name/spn of a group that have entry manager rights over this group.
40    #[serde(skip_serializing_if = "Option::is_none")]
41    pub entry_managed_by: Option<String>,
42
43    /// Set the exact list of mail addresses that this group is associated with. The first mail
44    /// address in the list is the `primary` and the remainder are aliases. Setting an empty list
45    /// will clear the mail attribute.
46    #[serde(skip_serializing_if = "Option::is_none")]
47    pub mail: Option<Vec<String>>,
48
49    /// Name or SPN of group members. Set the exact list of members that this group should contain,
50    /// removing any not listed in the set operation.
51    /// If you want to manage members from the database, do not set them here.
52    #[serde(skip_serializing_if = "Option::is_none")]
53    pub members: Option<Vec<String>>,
54
55    /// POSIX attributes for the group account. When specified, the operator will activate them.
56    /// If omitted, the operator retains the attributes in the database but ceases to manage them.
57    #[serde(skip_serializing_if = "Option::is_none")]
58    pub posix_attributes: Option<KanidmGroupPosixAttributes>,
59}
60
61impl KanidmResource for KanidmGroup {
62    #[inline]
63    fn kanidm_name(&self) -> String {
64        self.spec.kanidm_ref.name.clone()
65    }
66
67    #[inline]
68    fn kanidm_namespace(&self) -> String {
69        // safe unwrap: group is namespaced scoped
70        self.namespace().unwrap()
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}