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.
50///
51/// Used as a `skip_serializing_if` predicate. With the `examples-gen` feature it always returns
52/// `false`, so every field is serialized and the generated examples document all options. Because
53/// cargo unifies that feature into every crate of a workspace-wide build, tests must not assume a
54/// default-valued field is omitted; assert against this function instead.
55pub fn is_default<T: Default + PartialEq>(value: &T) -> bool {
56 #[cfg(feature = "examples-gen")]
57 {
58 // When generating examples, never skip fields so users can see all available options
59 let _ = value; // Suppress unused variable warning
60 false
61 }
62 #[cfg(not(feature = "examples-gen"))]
63 {
64 value == &T::default()
65 }
66}
67
68/// Template for metadata that may be attached to objects managed by the operator.
69/// Inspired by cert-manager's Certificate `secretTemplate` field.
70///
71/// Allows attaching custom annotations and labels to operator-managed objects. The operator's own
72/// labels and annotations take precedence over any conflicting keys in the template. Changes to
73/// this template are enforced on the next reconciliation, overwriting any manual modifications
74/// made to the object.
75#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq)]
76#[cfg_attr(feature = "schemars", derive(JsonSchema))]
77#[serde(rename_all = "camelCase")]
78pub struct MetadataTemplate {
79 /// Annotations to add to the object. The operator's own annotations take precedence over
80 /// any keys specified here.
81 #[serde(skip_serializing_if = "Option::is_none")]
82 pub annotations: Option<BTreeMap<String, String>>,
83
84 /// Labels to add to the object. The operator's own labels take precedence over any keys
85 /// specified here.
86 #[serde(skip_serializing_if = "Option::is_none")]
87 pub labels: Option<BTreeMap<String, String>>,
88}
89
90/// KanidmRef is a reference to a Kanidm object in the same cluster. It is used to specify where
91/// the object is stored.
92#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq)]
93#[cfg_attr(feature = "schemars", derive(JsonSchema))]
94#[schemars(extend("x-kubernetes-validations" = [{"message": "Value is immutable", "rule": "self == oldSelf"}]))]
95#[serde(rename_all = "camelCase")]
96pub struct KanidmRef {
97 pub name: String,
98
99 /// For cross-namespace resources. Reference Kanidm namespace. If omitted, the namespace of the
100 /// resource will be used.
101 #[serde(skip_serializing_if = "Option::is_none")]
102 pub namespace: Option<String>,
103}
104
105/// Kanidm has features that enable its accounts and groups to be consumed on POSIX-like machines,
106/// such as Linux, FreeBSD or others. Both service accounts and person accounts can be used on POSIX
107/// systems.
108///
109/// The attributes defined here are set by the operator. If you want to manage those attributes
110/// from the database, do not set them here.
111/// Additionally, if you unset them here, they will be kept in the database.
112#[derive(Serialize, Deserialize, Clone, Debug, Default)]
113#[cfg_attr(feature = "schemars", derive(JsonSchema))]
114#[serde(rename_all = "camelCase")]
115pub struct KanidmAccountPosixAttributes {
116 /// The group ID number (GID) for the person account. In Kanidm there is no difference between
117 /// a UID and a GID number.
118 ///
119 /// If omitted, Kanidm will generate it automatically.
120 ///
121 /// More info:
122 /// https://kanidm.github.io/kanidm/stable/accounts/posix_accounts_and_groups.html#uid-and-gid-numbers
123 pub gidnumber: Option<u32>,
124 /// The login shell for the person account.
125 ///
126 /// This sets the default shell that will be used when the user logs in via SSH or other
127 /// mechanisms that require a shell. Common values include /bin/bash, /bin/zsh, /bin/sh.
128 pub loginshell: Option<String>,
129}
130
131impl PartialEq for KanidmAccountPosixAttributes {
132 /// Compare attributes defined in the first object with the second object values.
133 /// If the second object has more attributes defined, they will be ignored.
134 fn eq(&self, other: &Self) -> bool {
135 (self.gidnumber.is_none() || self.gidnumber == other.gidnumber)
136 && (self.loginshell.is_none() || self.loginshell == other.loginshell)
137 }
138}
139
140impl From<Entry> for KanidmAccountPosixAttributes {
141 fn from(entry: Entry) -> Self {
142 KanidmAccountPosixAttributes {
143 gidnumber: get_first_cloned(&entry, ATTR_GIDNUMBER).and_then(|s| s.parse::<u32>().ok()),
144 loginshell: get_first_cloned(&entry, ATTR_LOGINSHELL),
145 }
146 }
147}