1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// Copyright 2026 Paolo Vella
// SPDX-License-Identifier: MPL-2.0
//! Kubernetes-native deployment types (Phase 27).
//!
//! Leaf types for leader election status, service discovery endpoints,
//! and deployment info. No internal dependencies.
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
/// Leader election status for a Vellaveto instance.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "status", rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
pub enum LeaderStatus {
/// This instance currently holds the leader lease.
Leader {
/// ISO 8601 timestamp when leadership was acquired.
since: String,
},
/// This instance is a follower.
Follower {
/// Instance ID of the current leader, if known.
#[serde(skip_serializing_if = "Option::is_none")]
leader_id: Option<String>,
},
/// Leadership status is unknown (e.g., during startup or partition).
#[default]
Unknown,
}
impl LeaderStatus {
/// Maximum length for the `since` timestamp field.
const MAX_SINCE_LEN: usize = 64;
/// Maximum length for the `leader_id` field.
const MAX_LEADER_ID_LEN: usize = 256;
/// Validate structural bounds on string fields within each variant.
///
/// SECURITY (FIND-R158-006): LeaderStatus variants carry user-facing strings
/// (`since`, `leader_id`) that were not validated, allowing control/format
/// character injection into deployment info responses and audit logs.
pub fn validate(&self) -> Result<(), String> {
match self {
LeaderStatus::Leader { since } => {
if since.len() > Self::MAX_SINCE_LEN {
return Err(format!(
"LeaderStatus::Leader since length {} exceeds max {}",
since.len(),
Self::MAX_SINCE_LEN,
));
}
if crate::core::has_dangerous_chars(since) {
return Err(
"LeaderStatus::Leader since contains control or format characters"
.to_string(),
);
}
}
LeaderStatus::Follower { leader_id } => {
if let Some(ref id) = leader_id {
if id.len() > Self::MAX_LEADER_ID_LEN {
return Err(format!(
"LeaderStatus::Follower leader_id length {} exceeds max {}",
id.len(),
Self::MAX_LEADER_ID_LEN,
));
}
if crate::core::has_dangerous_chars(id) {
return Err(
"LeaderStatus::Follower leader_id contains control or format characters"
.to_string(),
);
}
}
}
LeaderStatus::Unknown => {}
}
Ok(())
}
}
/// A discovered service endpoint in the cluster.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ServiceEndpoint {
/// Unique identifier for this endpoint (e.g., pod name).
pub id: String,
/// URL to reach this endpoint (e.g., `http://vellaveto-0.vellaveto:3000`).
pub url: String,
/// Labels/metadata associated with this endpoint.
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
pub labels: HashMap<String, String>,
/// Whether this endpoint is currently healthy.
pub healthy: bool,
}
impl ServiceEndpoint {
/// Maximum length for endpoint `id`.
const MAX_ID_LEN: usize = 256;
/// Maximum length for endpoint `url`.
const MAX_URL_LEN: usize = 2048;
/// Maximum number of labels per endpoint.
const MAX_LABELS: usize = 100;
/// Maximum length for a label key.
///
/// SECURITY (FIND-R113-011): Bound individual label key lengths.
const MAX_LABEL_KEY_LEN: usize = 256;
/// Maximum length for a label value.
///
/// SECURITY (FIND-R113-011): Bound individual label value lengths.
const MAX_LABEL_VALUE_LEN: usize = 1024;
/// Validate structural bounds on deserialized data.
pub fn validate(&self) -> Result<(), String> {
if self.id.len() > Self::MAX_ID_LEN {
return Err(format!(
"ServiceEndpoint id length {} exceeds max {}",
self.id.len(),
Self::MAX_ID_LEN,
));
}
// SECURITY (FIND-R113-011): Validate control/format chars on id.
if crate::core::has_dangerous_chars(&self.id) {
return Err("ServiceEndpoint id contains control or format characters".to_string());
}
if self.url.len() > Self::MAX_URL_LEN {
return Err(format!(
"ServiceEndpoint '{}' url length {} exceeds max {}",
self.id,
self.url.len(),
Self::MAX_URL_LEN,
));
}
// SECURITY (FIND-R113-011): Validate control/format chars on url.
if crate::core::has_dangerous_chars(&self.url) {
return Err("ServiceEndpoint url contains control or format characters".to_string());
}
if self.labels.len() > Self::MAX_LABELS {
return Err(format!(
"ServiceEndpoint '{}' labels count {} exceeds max {}",
self.id,
self.labels.len(),
Self::MAX_LABELS,
));
}
// SECURITY (FIND-R113-011): Validate label key/value lengths and control chars.
for (key, value) in &self.labels {
if key.len() > Self::MAX_LABEL_KEY_LEN {
return Err(format!(
"ServiceEndpoint '{}' label key length {} exceeds max {}",
self.id,
key.len(),
Self::MAX_LABEL_KEY_LEN,
));
}
if value.len() > Self::MAX_LABEL_VALUE_LEN {
return Err(format!(
"ServiceEndpoint '{}' label value length {} exceeds max {}",
self.id,
value.len(),
Self::MAX_LABEL_VALUE_LEN,
));
}
if key
.chars()
.any(|c| c.is_control() || crate::core::is_unicode_format_char(c))
{
return Err(format!(
"ServiceEndpoint '{}' label key contains control or format characters",
self.id,
));
}
if value
.chars()
.any(|c| c.is_control() || crate::core::is_unicode_format_char(c))
{
return Err(format!(
"ServiceEndpoint '{}' label value contains control or format characters",
self.id,
));
}
}
Ok(())
}
}
/// Events emitted by service discovery watchers.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "event", rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
pub enum DiscoveryEvent {
/// A new endpoint was discovered.
Added(ServiceEndpoint),
/// An endpoint was removed.
Removed {
/// ID of the removed endpoint.
id: String,
},
/// An existing endpoint was updated (e.g., health status changed).
Updated(ServiceEndpoint),
}
impl DiscoveryEvent {
/// Maximum length for the `id` in `Removed` events.
///
/// SECURITY (IMP-R224-007): Bound the Removed id to prevent OOM and log
/// injection from attacker-crafted discovery events.
const MAX_REMOVED_ID_LEN: usize = 256;
/// Validate structural bounds on all variants.
pub fn validate(&self) -> Result<(), String> {
match self {
DiscoveryEvent::Added(ep) => ep.validate(),
DiscoveryEvent::Updated(ep) => ep.validate(),
DiscoveryEvent::Removed { id } => {
if id.len() > Self::MAX_REMOVED_ID_LEN {
return Err(format!(
"DiscoveryEvent::Removed id length {} exceeds max {}",
id.len(),
Self::MAX_REMOVED_ID_LEN,
));
}
if crate::core::has_dangerous_chars(id) {
return Err(
"DiscoveryEvent::Removed id contains control or format characters"
.to_string(),
);
}
Ok(())
}
}
}
}
/// Summary of deployment status for the `/api/deployment/info` endpoint.
///
/// SECURITY (FIND-R44-015): In anonymous mode (no api_key configured), sensitive
/// topology fields (`instance_id`, `leader_status`, `discovered_endpoints`) are
/// set to `None` and omitted from the JSON response. Only `mode` and `uptime_secs`
/// are included.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DeploymentInfo {
/// Instance ID of this Vellaveto node (e.g., pod name).
/// None when redacted in anonymous mode.
#[serde(skip_serializing_if = "Option::is_none")]
pub instance_id: Option<String>,
/// Current leader election status.
/// None when redacted in anonymous mode.
#[serde(skip_serializing_if = "Option::is_none")]
pub leader_status: Option<LeaderStatus>,
/// Number of discovered service endpoints.
/// None when redacted in anonymous mode.
#[serde(skip_serializing_if = "Option::is_none")]
pub discovered_endpoints: Option<usize>,
/// Uptime in seconds since process start.
pub uptime_secs: u64,
/// Deployment mode (standalone, clustered, kubernetes).
pub mode: String,
}
impl DeploymentInfo {
/// Maximum length for `instance_id`.
const MAX_INSTANCE_ID_LEN: usize = 256;
/// Maximum length for `mode`.
const MAX_MODE_LEN: usize = 64;
/// Validate structural bounds on deserialized data.
pub fn validate(&self) -> Result<(), String> {
// SECURITY (FIND-R158-006): Validate nested LeaderStatus.
if let Some(ref status) = self.leader_status {
status.validate()?;
}
if let Some(ref id) = self.instance_id {
if id.len() > Self::MAX_INSTANCE_ID_LEN {
return Err(format!(
"DeploymentInfo instance_id length {} exceeds max {}",
id.len(),
Self::MAX_INSTANCE_ID_LEN,
));
}
// SECURITY (FIND-R115-005): Reject control/format chars in instance_id.
if id
.chars()
.any(|c| c.is_control() || crate::core::is_unicode_format_char(c))
{
return Err(
"DeploymentInfo instance_id contains control or format characters".to_string(),
);
}
}
if self.mode.len() > Self::MAX_MODE_LEN {
return Err(format!(
"DeploymentInfo mode length {} exceeds max {}",
self.mode.len(),
Self::MAX_MODE_LEN,
));
}
// SECURITY (FIND-R115-005): Reject control/format chars in mode.
if crate::core::has_dangerous_chars(&self.mode) {
return Err("DeploymentInfo mode contains control or format characters".to_string());
}
Ok(())
}
}