Skip to content

Commit 3cdaad7

Browse files
committed
feat(backend-kubernetes): add Pod options and identity policy
Add optional structured pod_options (node selector, tolerations, workspace and ephemeral storage bounds, absolute deadline, and a downward-API setup Pod UID). Configured options join the create-intent fingerprint; omitted options keep the original Pod shape and intent. Add an optional identity_policy that checks the agent key, owner attestation, and launch owner before any cluster access, and a local --check-identity preflight that returns only the public Pod name or a sanitized refusal. Signed-off-by: Tee8z <[email protected]>
1 parent 8a6fd0f commit 3cdaad7

10 files changed

Lines changed: 522 additions & 19 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/buzz-backend-kubernetes/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ name = "buzz-backend-kubernetes"
1111
path = "src/main.rs"
1212

1313
[dependencies]
14+
buzz-sdk = { workspace = true }
1415
kube = { workspace = true }
1516
k8s-openapi = { workspace = true }
1617
nostr = { workspace = true }

‎crates/buzz-backend-kubernetes/src/config.rs‎

Lines changed: 111 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
//! `provider_config` parsing and the `info` config schema
22
//! (spec §`provider_config` v1 fields, `docs/remote-agents.md:1384-1389`).
33
//!
4-
//! Nine fields, all optional except `image` (required at parse time; the
5-
//! schema offers the published sprig image as a prefill default — §Image).
4+
//! Nine scalar fields and optional structured Pod controls. `image` is required
5+
//! at parse time (the schema offers the published sprig image as a prefill
6+
//! default — §Image).
67
//! No credential field exists, by I2: cluster auth comes from ambient
78
//! kubeconfig resolution and nothing else (`:196-198`).
89
910
use crate::image::{self, ImageRef};
11+
use k8s_openapi::api::core::v1::Toleration;
12+
use serde::{Deserialize, Serialize};
13+
use std::collections::BTreeMap;
1014

1115
/// Resource requests and limits (§Pod shape: 1cpu/2Gi → 2cpu/4Gi, all four
1216
/// configurable — `cargo build` in an agent workspace makes 500m/1Gi
@@ -61,7 +65,42 @@ pub const TERMINATION_GRACE_SECONDS: i64 = 60;
6165
/// the combination rather than shipping against an undefended convention.
6266
pub const RESTART_POLICY: &str = "Never";
6367

64-
#[derive(Debug, Clone, PartialEq, Eq)]
68+
/// Optional placement and resource bounds for a bare Pod. Defaults preserve
69+
/// the original Pod shape; configured fields participate in create intent.
70+
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
71+
#[serde(deny_unknown_fields)]
72+
pub struct PodOptions {
73+
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
74+
/// Exact node labels required for scheduling.
75+
pub node_selector: BTreeMap<String, String>,
76+
#[serde(default, skip_serializing_if = "Vec::is_empty")]
77+
/// Explicit taints this workload may tolerate.
78+
pub tolerations: Vec<Toleration>,
79+
#[serde(default, skip_serializing_if = "Option::is_none")]
80+
/// Optional emptyDir size limit, in Kubernetes quantity syntax.
81+
pub workspace_size_limit: Option<String>,
82+
#[serde(default, skip_serializing_if = "Option::is_none")]
83+
/// Scheduler reservation for writable storage and container logs.
84+
pub ephemeral_storage_request: Option<String>,
85+
#[serde(default, skip_serializing_if = "Option::is_none")]
86+
/// Eviction bound for aggregate container ephemeral storage.
87+
pub ephemeral_storage_limit: Option<String>,
88+
#[serde(default, skip_serializing_if = "Option::is_none")]
89+
/// Positive absolute Pod lifetime, including startup.
90+
pub active_deadline_seconds: Option<i64>,
91+
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
92+
/// Expose the server-assigned Pod UID to a bounded owner setup gate.
93+
pub setup_pending: bool,
94+
}
95+
96+
impl PodOptions {
97+
/// Preserve original intent serialization when no options are set.
98+
pub fn is_default(&self) -> bool {
99+
self == &Self::default()
100+
}
101+
}
102+
103+
#[derive(Debug, Clone, PartialEq)]
65104
pub struct ProviderConfig {
66105
/// kubeconfig context; `None` uses the current context.
67106
pub context: Option<String>,
@@ -71,6 +110,8 @@ pub struct ProviderConfig {
71110
/// `None` when `inactivity_seconds` was 0 — refused in v1, see [`parse`].
72111
pub inactivity_seconds: Option<u64>,
73112
pub service_account: Option<String>,
113+
/// Optional Pod placement, storage, and lifetime controls.
114+
pub pod_options: PodOptions,
74115
}
75116

76117
/// Read an optional non-empty string field. Rejects non-string scalars rather
@@ -165,13 +206,35 @@ pub fn parse(cfg: &serde_json::Value) -> Result<ProviderConfig, String> {
165206
Some(n) => Some(n),
166207
};
167208

209+
let pod_options: PodOptions = match cfg.get("pod_options") {
210+
None => PodOptions::default(),
211+
Some(value) => serde_json::from_value(value.clone())
212+
.map_err(|e| format!("provider_config.pod_options: {e}"))?,
213+
};
214+
if pod_options.active_deadline_seconds.is_some_and(|n| n <= 0) {
215+
return Err("provider_config.pod_options.active_deadline_seconds must be positive".into());
216+
}
217+
for value in [
218+
&pod_options.workspace_size_limit,
219+
&pod_options.ephemeral_storage_request,
220+
&pod_options.ephemeral_storage_limit,
221+
]
222+
.into_iter()
223+
.flatten()
224+
{
225+
if value.trim().is_empty() {
226+
return Err("provider_config.pod_options storage quantities must not be empty".into());
227+
}
228+
}
229+
168230
Ok(ProviderConfig {
169231
context: optional_string(cfg, "context")?,
170232
namespace,
171233
image,
172234
resources,
173235
inactivity_seconds,
174236
service_account: optional_string(cfg, "service_account")?,
237+
pod_options,
175238
})
176239
}
177240

@@ -232,10 +295,35 @@ pub fn config_schema() -> serde_json::Value {
232295
"description": "The agent exits after this long with no work, and can be started again at any time.",
233296
"default": DEFAULT_INACTIVITY_SECONDS
234297
},
298+
"pod_options": {
299+
"type": "object",
300+
"title": "Pod placement and bounds",
301+
"description": "Optional structured provider configuration; omit to preserve the original Pod shape.",
302+
"additionalProperties": false,
303+
"properties": {
304+
"node_selector": {"type": "object", "additionalProperties": {"type": "string"}},
305+
"tolerations": {"type": "array", "items": {"type": "object"}},
306+
"workspace_size_limit": {"type": "string", "minLength": 1},
307+
"ephemeral_storage_request": {"type": "string", "minLength": 1},
308+
"ephemeral_storage_limit": {"type": "string", "minLength": 1},
309+
"active_deadline_seconds": {"type": "integer", "minimum": 1},
310+
"setup_pending": {"type": "boolean", "default": false}
311+
}
312+
},
235313
"service_account": {
236314
"type": "string",
237315
"title": "Service account",
238316
"description": "Scheduling/RBAC identity only. No API token is mounted."
317+
},
318+
"identity_policy": {
319+
"type": "object",
320+
"description": "Optional approved agent and owner public keys, cryptographically checked before cluster access. Also enables local --check-identity preflight.",
321+
"additionalProperties": false,
322+
"required": ["agent_pubkey", "owner_pubkey"],
323+
"properties": {
324+
"agent_pubkey": {"type": "string", "pattern": "^[0-9a-f]{64}$"},
325+
"owner_pubkey": {"type": "string", "pattern": "^[0-9a-f]{64}$"}
326+
}
239327
}
240328
},
241329
"required": ["namespace", "image"]
@@ -254,6 +342,22 @@ mod tests {
254342
serde_json::json!({"namespace": "buzz-agents-abc123", "image": digest_ref()})
255343
}
256344

345+
#[test]
346+
fn refuses_invalid_pod_options() {
347+
for bad in [
348+
serde_json::json!({"active_deadline_seconds": 0}),
349+
serde_json::json!({"active_deadline_seconds": -1}),
350+
serde_json::json!({"workspace_size_limit": " "}),
351+
serde_json::json!({"unknown": true}),
352+
serde_json::json!({"node_selector": "amd64"}),
353+
] {
354+
let mut cfg = minimal();
355+
cfg["pod_options"] = bad;
356+
assert!(parse(&cfg).unwrap_err().contains("pod_options"));
357+
}
358+
assert!(parse(&minimal()).unwrap().pod_options.is_default());
359+
}
360+
257361
#[test]
258362
fn applies_spec_defaults() {
259363
let c = parse(&minimal()).unwrap();
@@ -423,10 +527,10 @@ mod tests {
423527
);
424528
}
425529

426-
/// Nine fields exactly (§`provider_config` v1 fields). The cap is 20; the
530+
/// Eleven fields (§`provider_config` v1 fields). The cap is 20; the
427531
/// count is pinned so a field added without a spec change is caught here.
428532
#[test]
429-
fn schema_declares_exactly_the_nine_v1_fields() {
533+
fn schema_declares_scalar_fields_and_optional_pod_controls() {
430534
let schema = config_schema();
431535
let props = schema["properties"].as_object().unwrap();
432536
let mut keys: Vec<&str> = props.keys().map(String::as_str).collect();
@@ -437,11 +541,13 @@ mod tests {
437541
"context",
438542
"cpu_limit",
439543
"cpu_request",
544+
"identity_policy",
440545
"image",
441546
"inactivity_seconds",
442547
"memory_limit",
443548
"memory_request",
444549
"namespace",
550+
"pod_options",
445551
"service_account"
446552
]
447553
);
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
//! Optional owner-approved identity policy, checked before cluster access.
2+
3+
use crate::{naming::AgentIdentity, wire::DeployRequest};
4+
use serde::Deserialize;
5+
6+
#[derive(Deserialize)]
7+
#[serde(deny_unknown_fields)]
8+
struct IdentityPolicy {
9+
agent_pubkey: String,
10+
owner_pubkey: String,
11+
}
12+
13+
/// Validate an optional public identity policy without exposing key material.
14+
/// A local preflight requires the policy; ordinary deploys retain compatibility.
15+
pub fn check(request: &DeployRequest, required: bool) -> Result<AgentIdentity, String> {
16+
let identity = AgentIdentity::from_nsec(&request.agent.private_key_nsec)?;
17+
let Some(value) = request.provider_config.get("identity_policy") else {
18+
return if required {
19+
Err("identity preflight requires an approved identity_policy".into())
20+
} else {
21+
Ok(identity)
22+
};
23+
};
24+
let policy: IdentityPolicy =
25+
serde_json::from_value(value.clone()).map_err(|_| "invalid identity_policy".to_string())?;
26+
let approved_agent = nostr::PublicKey::from_hex(&policy.agent_pubkey)
27+
.map_err(|_| "invalid approved agent public key".to_string())?;
28+
let approved_owner = nostr::PublicKey::from_hex(&policy.owner_pubkey)
29+
.map_err(|_| "invalid approved owner public key".to_string())?;
30+
if identity.pubkey_hex() == approved_owner.to_hex() {
31+
return Err("owner identity cannot be deployed as the agent".into());
32+
}
33+
if identity.pubkey_hex() != approved_agent.to_hex() {
34+
return Err("agent key does not match the approved agent identity".into());
35+
}
36+
let tag = request
37+
.agent
38+
.auth_tag
39+
.as_deref()
40+
.ok_or_else(|| "owner attestation is required by identity_policy".to_string())?;
41+
let now = std::time::SystemTime::now()
42+
.duration_since(std::time::UNIX_EPOCH)
43+
.map_err(|_| "could not establish identity verification time".to_string())?
44+
.as_secs();
45+
let owner = buzz_sdk::nip_oa::verify_auth_tag_for_auth_event(tag, &approved_agent, now)
46+
.map_err(|_| "owner attestation verification failed".to_string())?;
47+
if owner != approved_owner {
48+
return Err("attestation owner does not match the approved owner".into());
49+
}
50+
let descriptor_owner = request
51+
.agent
52+
.launch
53+
.as_ref()
54+
.and_then(|launch| launch.owner_pubkey.as_deref());
55+
if descriptor_owner != Some(approved_owner.to_hex().as_str()) {
56+
return Err("launch owner does not match the verified owner".into());
57+
}
58+
Ok(identity)
59+
}
60+
61+
#[cfg(test)]
62+
mod tests {
63+
use super::*;
64+
use nostr::nips::nip19::ToBech32;
65+
66+
fn request(agent: &nostr::Keys, owner: &nostr::Keys) -> DeployRequest {
67+
serde_json::from_value(serde_json::json!({
68+
"agent": {
69+
"relay_url": "wss://example.invalid",
70+
"private_key_nsec": agent.secret_key().to_bech32().unwrap(),
71+
"auth_tag": buzz_sdk::nip_oa::compute_auth_tag(owner, &agent.public_key(), "").unwrap(),
72+
"launch": {"owner_pubkey": owner.public_key().to_hex()}
73+
},
74+
"provider_config": {"identity_policy": {
75+
"agent_pubkey": agent.public_key().to_hex(),
76+
"owner_pubkey": owner.public_key().to_hex()
77+
}}
78+
})).unwrap()
79+
}
80+
81+
#[test]
82+
fn binds_agent_signature_owner_and_descriptor() {
83+
let agent = nostr::Keys::generate();
84+
let owner = nostr::Keys::generate();
85+
let mut request = request(&agent, &owner);
86+
assert_eq!(
87+
check(&request, true).unwrap().pubkey_hex(),
88+
agent.public_key().to_hex()
89+
);
90+
request.agent.launch.as_mut().unwrap().owner_pubkey = Some(agent.public_key().to_hex());
91+
assert!(check(&request, true).unwrap_err().contains("launch owner"));
92+
}
93+
94+
#[test]
95+
fn rejects_wrong_key_human_key_and_forged_or_wrong_owner_tag() {
96+
let agent = nostr::Keys::generate();
97+
let owner = nostr::Keys::generate();
98+
let stranger = nostr::Keys::generate();
99+
let mut wrong = request(&agent, &owner);
100+
wrong.agent.private_key_nsec = stranger.secret_key().to_bech32().unwrap();
101+
assert!(check(&wrong, true).unwrap_err().contains("approved agent"));
102+
wrong.agent.private_key_nsec = owner.secret_key().to_bech32().unwrap();
103+
assert!(check(&wrong, true).unwrap_err().contains("owner identity"));
104+
let mut forged = request(&agent, &owner);
105+
forged.agent.auth_tag = Some("synthetic-secret-not-a-tag".into());
106+
let error = check(&forged, true).unwrap_err();
107+
assert!(error.contains("verification failed"));
108+
assert!(!error.contains("synthetic-secret"));
109+
forged.agent.auth_tag =
110+
Some(buzz_sdk::nip_oa::compute_auth_tag(&stranger, &agent.public_key(), "").unwrap());
111+
assert!(check(&forged, true).unwrap_err().contains("approved owner"));
112+
forged.agent.auth_tag = Some(
113+
buzz_sdk::nip_oa::compute_auth_tag(&owner, &agent.public_key(), "created_at<1")
114+
.unwrap(),
115+
);
116+
assert!(check(&forged, true)
117+
.unwrap_err()
118+
.contains("verification failed"));
119+
}
120+
121+
#[test]
122+
fn requires_policy_only_for_preflight_and_refuses_unknown_policy_fields() {
123+
let mut request = request(&nostr::Keys::generate(), &nostr::Keys::generate());
124+
request.provider_config["identity_policy"]["extra"] = serde_json::json!(true);
125+
assert!(check(&request, true).is_err());
126+
request.provider_config = serde_json::json!({});
127+
assert!(check(&request, true).is_err());
128+
assert!(check(&request, false).is_ok());
129+
}
130+
}

‎crates/buzz-backend-kubernetes/src/intent.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl std::fmt::Display for Fingerprint {
6060
/// The non-secret, pre-binding description of the pod this deploy would
6161
/// create. Every field is provider-controlled and scheduling-relevant; there
6262
/// is deliberately no field for env values, Secret data, or the generation.
63-
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
63+
#[derive(Debug, Clone, PartialEq, Serialize)]
6464
pub struct IntentTemplate {
6565
/// Schema version of the template itself. Bumping it re-fingerprints every
6666
/// pod, which is the intended way to roll out a pod-shape change.
@@ -83,6 +83,8 @@ pub struct IntentTemplate {
8383
pub workspace_mount_path: String,
8484
pub run_as_user: i64,
8585
pub run_as_group: i64,
86+
#[serde(skip_serializing_if = "crate::config::PodOptions::is_default")]
87+
pub pod_options: crate::config::PodOptions,
8688
}
8789

8890
/// Current template schema version.
@@ -130,6 +132,7 @@ impl IntentTemplate {
130132
workspace_mount_path: crate::config::WORKSPACE_PATH.to_string(),
131133
run_as_user: crate::config::RUN_AS_UID,
132134
run_as_group: crate::config::RUN_AS_GID,
135+
pod_options: crate::config::PodOptions::default(),
133136
}
134137
}
135138
}

0 commit comments

Comments
 (0)