kaniop_operator/crd.rs
1use std::collections::BTreeMap;
2
3use kaniop_k8s_util::types::get_first_cloned;
4
5use kanidm_proto::{
6 constants::{ATTR_GIDNUMBER, ATTR_LOGINSHELL},
7 v1::Entry,
8};
9
10#[cfg(feature = "schemars")]
11use schemars::JsonSchema;
12use serde::{Deserialize, Serialize};
13
14/// Configuration for automatic secret rotation.
15///
16/// When enabled, the operator will automatically rotate secrets based on the configured period.
17/// This is useful for security compliance and reducing the impact of credential leakage.
18#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
19#[cfg_attr(feature = "schemars", derive(JsonSchema))]
20#[serde(rename_all = "camelCase")]
21pub struct SecretRotation {
22 /// Enable automatic secret rotation. Defaults to false (opt-in).
23 #[serde(default)]
24 pub enabled: bool,
25
26 /// Rotation period in days. Secrets will be rotated when they are older than this period.
27 /// Defaults to 90 days.
28 #[serde(default = "default_rotation_period_days")]
29 pub period_days: u32,
30}
31
32impl Default for SecretRotation {
33 fn default() -> Self {
34 Self {
35 enabled: false,
36 period_days: default_rotation_period_days(),
37 }
38 }
39}
40
41/// Default rotation period of 90 days.
42///
43/// This value aligns with common security best practices and compliance frameworks
44/// (e.g., PCI-DSS, SOC 2) that recommend rotating credentials every 90 days.
45fn default_rotation_period_days() -> u32 {
46 90
47}
48
49/// Checks if a given value is equal to its type's default value.
50pub fn is_default<T: Default + PartialEq>(value: &T) -> bool {
51 #[cfg(feature = "examples-gen")]
52 {
53 // When generating examples, never skip fields so users can see all available options
54 let _ = value; // Suppress unused variable warning
55 false
56 }
57 #[cfg(not(feature = "examples-gen"))]
58 {
59 value == &T::default()
60 }
61}
62
63/// Template for metadata that may be attached to objects managed by the operator.
64/// Inspired by cert-manager's Certificate `secretTemplate` field.
65///
66/// Allows attaching custom annotations and labels to operator-managed objects. The operator's own
67/// labels and annotations take precedence over any conflicting keys in the template. Changes to
68/// this template are enforced on the next reconciliation, overwriting any manual modifications
69/// made to the object.
70#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq)]
71#[cfg_attr(feature = "schemars", derive(JsonSchema))]
72#[serde(rename_all = "camelCase")]
73pub struct MetadataTemplate {
74 /// Annotations to add to the object. The operator's own annotations take precedence over
75 /// any keys specified here.
76 #[serde(skip_serializing_if = "Option::is_none")]
77 pub annotations: Option<BTreeMap<String, String>>,
78
79 /// Labels to add to the object. The operator's own labels take precedence over any keys
80 /// specified here.
81 #[serde(skip_serializing_if = "Option::is_none")]
82 pub labels: Option<BTreeMap<String, String>>,
83}
84
85/// KanidmRef is a reference to a Kanidm object in the same cluster. It is used to specify where
86/// the object is stored.
87#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq)]
88#[cfg_attr(feature = "schemars", derive(JsonSchema))]
89#[schemars(extend("x-kubernetes-validations" = [{"message": "Value is immutable", "rule": "self == oldSelf"}]))]
90#[serde(rename_all = "camelCase")]
91pub struct KanidmRef {
92 pub name: String,
93
94 /// For cross-namespace resources. Reference Kanidm namespace. If omitted, the namespace of the
95 /// resource will be used.
96 #[serde(skip_serializing_if = "Option::is_none")]
97 pub namespace: Option<String>,
98}
99
100/// Kanidm has features that enable its accounts and groups to be consumed on POSIX-like machines,
101/// such as Linux, FreeBSD or others. Both service accounts and person accounts can be used on POSIX
102/// systems.
103///
104/// The attributes defined here are set by the operator. If you want to manage those attributes
105/// from the database, do not set them here.
106/// Additionally, if you unset them here, they will be kept in the database.
107#[derive(Serialize, Deserialize, Clone, Debug, Default)]
108#[cfg_attr(feature = "schemars", derive(JsonSchema))]
109#[serde(rename_all = "camelCase")]
110pub struct KanidmAccountPosixAttributes {
111 /// The group ID number (GID) for the person account. In Kanidm there is no difference between
112 /// a UID and a GID number.
113 ///
114 /// If omitted, Kanidm will generate it automatically.
115 ///
116 /// More info:
117 /// https://kanidm.github.io/kanidm/stable/accounts/posix_accounts_and_groups.html#uid-and-gid-numbers
118 pub gidnumber: Option<u32>,
119 /// The login shell for the person account.
120 ///
121 /// This sets the default shell that will be used when the user logs in via SSH or other
122 /// mechanisms that require a shell. Common values include /bin/bash, /bin/zsh, /bin/sh.
123 pub loginshell: Option<String>,
124}
125
126impl PartialEq for KanidmAccountPosixAttributes {
127 /// Compare attributes defined in the first object with the second object values.
128 /// If the second object has more attributes defined, they will be ignored.
129 fn eq(&self, other: &Self) -> bool {
130 (self.gidnumber.is_none() || self.gidnumber == other.gidnumber)
131 && (self.loginshell.is_none() || self.loginshell == other.loginshell)
132 }
133}
134
135impl From<Entry> for KanidmAccountPosixAttributes {
136 fn from(entry: Entry) -> Self {
137 KanidmAccountPosixAttributes {
138 gidnumber: get_first_cloned(&entry, ATTR_GIDNUMBER).and_then(|s| s.parse::<u32>().ok()),
139 loginshell: get_first_cloned(&entry, ATTR_LOGINSHELL),
140 }
141 }
142}