kaniop_person/
crd.rs

1use kaniop_k8s_util::types::{get_first_cloned, parse_time};
2use kaniop_operator::controller::kanidm::KanidmResource;
3use kaniop_operator::crd::{KanidmPersonPosixAttributes, KanidmRef, is_default};
4
5use k8s_openapi::apimachinery::pkg::apis::meta::v1::{Condition, Time};
6use kanidm_proto::constants::{
7    ATTR_ACCOUNT_EXPIRE, ATTR_ACCOUNT_VALID_FROM, ATTR_DISPLAYNAME, ATTR_LEGALNAME, ATTR_MAIL,
8};
9use kanidm_proto::v1::Entry;
10use kube::{CustomResource, ResourceExt};
11#[cfg(feature = "schemars")]
12use schemars::JsonSchema;
13use serde::{Deserialize, Serialize};
14
15/// A person represents a human's account in Kanidm. The majority of your users will be a person who
16/// will use this account in their daily activities. These entries may contain personally
17/// identifying information that is considered by Kanidm to be sensitive. Because of this, there
18/// are default limits to who may access these data.
19/// More info:
20/// https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
21#[derive(CustomResource, Serialize, Deserialize, Clone, Debug, Default, PartialEq)]
22#[cfg_attr(feature = "schemars", derive(JsonSchema))]
23#[kube(
24    category = "kaniop",
25    group = "kaniop.rs",
26    version = "v1beta1",
27    kind = "KanidmPersonAccount",
28    plural = "kanidmpersonsaccounts",
29    singular = "kanidmpersonaccount",
30    shortname = "person",
31    namespaced,
32    status = "KanidmPersonAccountStatus",
33    doc = r#"The Kanidm person account custom resource definition (CRD) defines a person account in Kanidm.
34    This resource has to be in the same namespace as the Kanidm cluster."#,
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    derive = "Default"
40)]
41#[serde(rename_all = "camelCase")]
42pub struct KanidmPersonAccountSpec {
43    pub kanidm_ref: KanidmRef,
44    pub person_attributes: KanidmPersonAttributes,
45    /// POSIX attributes for the person account. When specified, the operator will activate them.
46    /// If omitted, the operator retains the attributes in the database but ceases to manage them.
47    #[serde(skip_serializing_if = "Option::is_none")]
48    pub posix_attributes: Option<KanidmPersonPosixAttributes>,
49
50    /// If credentials are not defined, Kaniop will generate a link accessible when describing
51    /// the person account. This link will be valid for the number of seconds defined here.
52    /// The default is 3600 seconds (1 hour).
53    #[serde(
54        default = "default_credentials_token_ttl",
55        skip_serializing_if = "is_default"
56    )]
57    pub credentials_token_ttl: u32,
58}
59
60impl KanidmResource for KanidmPersonAccount {
61    #[inline]
62    fn kanidm_name(&self) -> String {
63        self.spec.kanidm_ref.name.clone()
64    }
65    #[inline]
66    fn kanidm_namespace(&self) -> String {
67        // safe unwrap: person is namespaced scoped
68        self.namespace().unwrap()
69    }
70}
71
72/// Attributes that personally identify a person account.
73///
74/// The attributes defined here are set by the operator. If you want to manage those attributes
75/// from the database, do not set them here.
76/// Additionally, if you unset them here, they will be kept in the database.
77#[derive(Serialize, Deserialize, Clone, Debug, Default)]
78#[cfg_attr(feature = "schemars", derive(JsonSchema))]
79#[serde(rename_all = "camelCase")]
80pub struct KanidmPersonAttributes {
81    /// Set the display name for the person.
82    pub displayname: String,
83
84    /// Set the mail address, can be set multiple times for multiple addresses. The first listed
85    /// mail address is the 'primary'.
86    #[serde(skip_serializing_if = "Option::is_none")]
87    pub mail: Option<Vec<String>>,
88
89    /// Set the legal name for the person.
90    #[serde(skip_serializing_if = "Option::is_none")]
91    pub legalname: Option<String>,
92
93    /// Set an account valid from time.
94    ///
95    /// If omitted, the account will be valid from the time of creation.
96    #[serde(skip_serializing_if = "Option::is_none")]
97    pub account_valid_from: Option<Time>,
98
99    /// Set an accounts expiry time.
100    ///
101    /// If omitted, the account will not expire.
102    #[serde(skip_serializing_if = "Option::is_none")]
103    pub account_expire: Option<Time>,
104}
105
106impl PartialEq for KanidmPersonAttributes {
107    /// Compare attributes defined in the first object with the second object values.
108    /// If the second object has more attributes defined, they will be ignored.
109    fn eq(&self, other: &Self) -> bool {
110        self.displayname == other.displayname
111            && (self.mail.is_none() || self.mail == other.mail)
112            && (self.legalname.is_none() || self.legalname == other.legalname)
113            && (self.account_valid_from.is_none()
114                || self.account_valid_from == other.account_valid_from)
115            && (self.account_expire.is_none() || self.account_expire == other.account_expire)
116    }
117}
118
119impl From<Entry> for KanidmPersonAttributes {
120    fn from(entry: Entry) -> Self {
121        KanidmPersonAttributes {
122            displayname: get_first_cloned(&entry, ATTR_DISPLAYNAME).unwrap_or_default(),
123            mail: entry.attrs.get(ATTR_MAIL).cloned(),
124            legalname: get_first_cloned(&entry, ATTR_LEGALNAME),
125            account_valid_from: parse_time(&entry, ATTR_ACCOUNT_VALID_FROM),
126            account_expire: parse_time(&entry, ATTR_ACCOUNT_EXPIRE),
127        }
128    }
129}
130
131fn default_credentials_token_ttl() -> u32 {
132    3600
133}
134
135/// Most recent observed status of the Kanidm Person Account. Read-only.
136/// More info:
137/// https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
138#[derive(Serialize, Deserialize, Clone, Debug, Default)]
139#[cfg_attr(feature = "schemars", derive(JsonSchema))]
140#[serde(rename_all = "camelCase")]
141pub struct KanidmPersonAccountStatus {
142    #[serde(default, skip_serializing_if = "Option::is_none")]
143    pub conditions: Option<Vec<Condition>>,
144
145    pub ready: bool,
146
147    #[serde(default, skip_serializing_if = "Option::is_none")]
148    pub gid: Option<u32>,
149
150    pub kanidm_ref: String,
151}