kaniop_person/
crd.rs

1use kaniop_k8s_util::types::{get_first_cloned, parse_time};
2use kaniop_operator::controller::kanidm::KanidmResource;
3use kaniop_operator::crd::{KanidmAccountPosixAttributes, KanidmRef, is_default};
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    pub person_attributes: KanidmPersonAttributes,
46    /// POSIX attributes for the person account. When specified, the operator will activate them.
47    /// If omitted, the operator retains the attributes in the database but ceases to manage them.
48    #[serde(skip_serializing_if = "Option::is_none")]
49    pub posix_attributes: Option<KanidmAccountPosixAttributes>,
50
51    /// If credentials are not defined, Kaniop will generate a link accessible when describing
52    /// the person account. This link will be valid for the number of seconds defined here.
53    /// The default is 3600 seconds (1 hour).
54    #[serde(
55        default = "default_credentials_token_ttl",
56        skip_serializing_if = "is_default"
57    )]
58    pub credentials_token_ttl: u32,
59}
60
61impl KanidmResource for KanidmPersonAccount {
62    #[inline]
63    fn kanidm_ref_spec(&self) -> &KanidmRef {
64        &self.spec.kanidm_ref
65    }
66
67    #[inline]
68    fn get_namespace_selector(kanidm: &Kanidm) -> &Option<LabelSelector> {
69        &kanidm.spec.person_namespace_selector
70    }
71}
72
73/// Attributes that personally identify a person account.
74///
75/// The attributes defined here are set by the operator. If you want to manage those attributes
76/// from the database, do not set them here.
77/// Additionally, if you unset them here, they will be kept in the database.
78#[derive(Serialize, Deserialize, Clone, Debug, Default)]
79#[cfg_attr(feature = "schemars", derive(JsonSchema))]
80#[serde(rename_all = "camelCase")]
81pub struct KanidmPersonAttributes {
82    /// Set the display name for the person.
83    pub displayname: String,
84
85    /// Set the mail address, can be set multiple times for multiple addresses. The first listed
86    /// mail address is the 'primary'.
87    #[serde(skip_serializing_if = "Option::is_none")]
88    pub mail: Option<Vec<String>>,
89
90    /// Set the legal name for the person.
91    #[serde(skip_serializing_if = "Option::is_none")]
92    pub legalname: Option<String>,
93
94    /// Set an account valid from time.
95    ///
96    /// If omitted, the account will be valid from the time of creation.
97    #[serde(skip_serializing_if = "Option::is_none")]
98    pub account_valid_from: Option<Time>,
99
100    /// Set an accounts expiry time.
101    ///
102    /// If omitted, the account will not expire.
103    #[serde(skip_serializing_if = "Option::is_none")]
104    pub account_expire: Option<Time>,
105}
106
107impl PartialEq for KanidmPersonAttributes {
108    /// Compare attributes defined in the first object with the second object values.
109    /// If the second object has more attributes defined, they will be ignored.
110    fn eq(&self, other: &Self) -> bool {
111        self.displayname == other.displayname
112            // comparison is ordered because first mail is primary
113            && (self.mail.is_none() || self.mail == other.mail)
114            && (self.legalname.is_none() || self.legalname == other.legalname)
115            && (self.account_valid_from.is_none()
116                || self.account_valid_from == other.account_valid_from)
117            && (self.account_expire.is_none() || self.account_expire == other.account_expire)
118    }
119}
120
121impl From<Entry> for KanidmPersonAttributes {
122    fn from(entry: Entry) -> Self {
123        KanidmPersonAttributes {
124            displayname: get_first_cloned(&entry, ATTR_DISPLAYNAME).unwrap_or_default(),
125            mail: entry.attrs.get(ATTR_MAIL).cloned(),
126            legalname: get_first_cloned(&entry, ATTR_LEGALNAME),
127            account_valid_from: parse_time(&entry, ATTR_ACCOUNT_VALID_FROM),
128            account_expire: parse_time(&entry, ATTR_ACCOUNT_EXPIRE),
129        }
130    }
131}
132
133fn default_credentials_token_ttl() -> u32 {
134    3600
135}
136
137/// Most recent observed status of the Kanidm Person Account. Read-only.
138/// More info:
139/// https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
140#[derive(Serialize, Deserialize, Clone, Debug, Default)]
141#[cfg_attr(feature = "schemars", derive(JsonSchema))]
142#[serde(rename_all = "camelCase")]
143pub struct KanidmPersonAccountStatus {
144    #[serde(default, skip_serializing_if = "Option::is_none")]
145    pub conditions: Option<Vec<Condition>>,
146
147    pub ready: bool,
148
149    #[serde(default, skip_serializing_if = "Option::is_none")]
150    pub gid: Option<u32>,
151
152    pub kanidm_ref: String,
153}