Skip to main content

kaniop_person/
crd.rs

1use kaniop_k8s_util::types::{get_first_cloned, parse_time};
2use kaniop_operator::controller::kanidm::KanidmResource;
3use kaniop_operator::crd::{is_default, KanidmAccountPosixAttributes, KanidmRef};
4use kaniop_operator::kanidm::crd::Kanidm;
5
6use k8s_openapi::apimachinery::pkg::apis::meta::v1::{Condition, LabelSelector, Time};
7use kanidm_proto::constants::{
8    ATTR_ACCOUNT_EXPIRE, ATTR_ACCOUNT_VALID_FROM, ATTR_DISPLAYNAME, ATTR_LEGALNAME, ATTR_MAIL,
9};
10use kanidm_proto::v1::Entry;
11use kube::CustomResource;
12#[cfg(feature = "schemars")]
13use schemars::JsonSchema;
14use serde::{Deserialize, Serialize};
15
16/// A person represents a human's account in Kanidm. The majority of your users will be a person who
17/// will use this account in their daily activities. These entries may contain personally
18/// identifying information that is considered by Kanidm to be sensitive. Because of this, there
19/// are default limits to who may access these data.
20/// More info:
21/// https://kanidm.github.io/kanidm/master/accounts/people_accounts.html
22#[derive(CustomResource, Serialize, Deserialize, Clone, Debug, Default, PartialEq)]
23#[cfg_attr(feature = "schemars", derive(JsonSchema))]
24#[kube(
25    category = "kaniop",
26    group = "kaniop.rs",
27    version = "v1beta1",
28    kind = "KanidmPersonAccount",
29    plural = "kanidmpersonsaccounts",
30    singular = "kanidmpersonaccount",
31    shortname = "person",
32    namespaced,
33    status = "KanidmPersonAccountStatus",
34    doc = r#"The Kanidm person account custom resource definition (CRD) defines a person account in Kanidm."#,
35    printcolumn = r#"{"name":"Kanidm","type":"string","jsonPath":".status.kanidmRef"}"#,
36    printcolumn = r#"{"name":"GID","type":"integer","jsonPath":".status.gid"}"#,
37    printcolumn = r#"{"name":"Valid","type":"string","jsonPath":".status.conditions[?(@.type == 'Valid')].status"}"#,
38    printcolumn = r#"{"name":"Ready","type":"boolean","jsonPath":".status.ready"}"#,
39    printcolumn = r#"{"name":"Age","type":"date","jsonPath":".metadata.creationTimestamp"}"#,
40    derive = "Default"
41)]
42#[serde(rename_all = "camelCase")]
43pub struct KanidmPersonAccountSpec {
44    pub kanidm_ref: KanidmRef,
45
46    /// The name of the entity in Kanidm. If not specified, the Kubernetes resource name is used.
47    /// Use this field to manage Kanidm entities with names that don't conform to Kubernetes naming rules
48    /// (e.g., entities with underscores like `idm_admin` or `idm_all_persons`).
49    /// This field is immutable and cannot be changed after creation.
50    #[schemars(extend("x-kubernetes-validations" = [{"message": "kanidmName cannot be changed.", "rule": "self == oldSelf"}]))]
51    #[serde(skip_serializing_if = "Option::is_none")]
52    pub kanidm_name: Option<String>,
53
54    pub person_attributes: KanidmPersonAttributes,
55    /// POSIX attributes for the person 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<KanidmAccountPosixAttributes>,
59
60    /// If credentials are not defined, Kaniop will generate a link accessible when describing
61    /// the person account. This link will be valid for the number of seconds defined here.
62    /// The default is 3600 seconds (1 hour).
63    #[serde(
64        default = "default_credentials_token_ttl",
65        skip_serializing_if = "is_default"
66    )]
67    pub credentials_token_ttl: u32,
68}
69
70impl KanidmResource for KanidmPersonAccount {
71    #[inline]
72    fn kanidm_ref_spec(&self) -> &KanidmRef {
73        &self.spec.kanidm_ref
74    }
75
76    #[inline]
77    fn get_namespace_selector(kanidm: &Kanidm) -> &Option<LabelSelector> {
78        &kanidm.spec.person_namespace_selector
79    }
80
81    #[inline]
82    fn kanidm_name_override(&self) -> Option<&str> {
83        self.spec.kanidm_name.as_deref()
84    }
85}
86
87/// Attributes that personally identify a person account.
88///
89/// The attributes defined here are set by the operator. If you want to manage those attributes
90/// from the database, do not set them here.
91/// Additionally, if you unset them here, they will be kept in the database.
92#[derive(Serialize, Deserialize, Clone, Debug, Default)]
93#[cfg_attr(feature = "schemars", derive(JsonSchema))]
94#[serde(rename_all = "camelCase")]
95pub struct KanidmPersonAttributes {
96    /// Set the display name for the person.
97    pub displayname: String,
98
99    /// Set the mail address, can be set multiple times for multiple addresses. The first listed
100    /// mail address is the 'primary'.
101    #[serde(skip_serializing_if = "Option::is_none")]
102    pub mail: Option<Vec<String>>,
103
104    /// Set the legal name for the person.
105    #[serde(skip_serializing_if = "Option::is_none")]
106    pub legalname: Option<String>,
107
108    /// Set an account valid from time.
109    ///
110    /// If omitted, the account will be valid from the time of creation.
111    #[serde(skip_serializing_if = "Option::is_none")]
112    pub account_valid_from: Option<Time>,
113
114    /// Set an accounts expiry time.
115    ///
116    /// If omitted, the account will not expire.
117    #[serde(skip_serializing_if = "Option::is_none")]
118    pub account_expire: Option<Time>,
119}
120
121impl PartialEq for KanidmPersonAttributes {
122    /// Compare attributes defined in the first object with the second object values.
123    /// If the second object has more attributes defined, they will be ignored.
124    fn eq(&self, other: &Self) -> bool {
125        self.displayname == other.displayname
126            // comparison is ordered because first mail is primary
127            && (self.mail.is_none() || self.mail == other.mail)
128            && (self.legalname.is_none() || self.legalname == other.legalname)
129            && (self.account_valid_from.is_none()
130                || self.account_valid_from == other.account_valid_from)
131            && (self.account_expire.is_none() || self.account_expire == other.account_expire)
132    }
133}
134
135impl From<Entry> for KanidmPersonAttributes {
136    fn from(entry: Entry) -> Self {
137        KanidmPersonAttributes {
138            displayname: get_first_cloned(&entry, ATTR_DISPLAYNAME).unwrap_or_default(),
139            mail: entry.attrs.get(ATTR_MAIL).cloned(),
140            legalname: get_first_cloned(&entry, ATTR_LEGALNAME),
141            account_valid_from: parse_time(&entry, ATTR_ACCOUNT_VALID_FROM),
142            account_expire: parse_time(&entry, ATTR_ACCOUNT_EXPIRE),
143        }
144    }
145}
146
147fn default_credentials_token_ttl() -> u32 {
148    3600
149}
150
151/// Most recent observed status of the Kanidm Person Account. Read-only.
152/// More info:
153/// https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
154#[derive(Serialize, Deserialize, Clone, Debug, Default)]
155#[cfg_attr(feature = "schemars", derive(JsonSchema))]
156#[serde(rename_all = "camelCase")]
157pub struct KanidmPersonAccountStatus {
158    #[serde(default, skip_serializing_if = "Option::is_none")]
159    pub conditions: Option<Vec<Condition>>,
160
161    pub ready: bool,
162
163    #[serde(default, skip_serializing_if = "Option::is_none")]
164    pub gid: Option<u32>,
165
166    pub kanidm_ref: String,
167}