subc-daemon 0.54.4

Embeddable subc daemon: bootstrap, module supervision, and opaque-byte splice routing.
Documentation
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
use super::*;
use crate::config_gate::GateError;
use crate::policy_root::{refuse, Key, Move, Section};
use subc_control::config::ConfigControlResponse;
use subc_control::policy_root::{RECOVER_OP, SET_OP};
use subc_protocol::policy_root::{GET_OP, ROTATE_OP};

pub(super) const VAULT: &str = "claustrum";

pub(super) fn is_op(op: &str) -> bool {
    matches!(op, GET_OP | ROTATE_OP | SET_OP | RECOVER_OP)
}

impl ControlHandler {
    fn policy_root_operator(&self, connection: ConnectionId) -> bool {
        self.claims.binding(connection).is_none()
            && self
                .local_connections
                .lock()
                .unwrap_or_else(|p| p.into_inner())
                .contains(&connection)
            && self.config_owner_connection(connection)
            && self
                .registry
                .get_module_by_connection(connection)
                .is_ok_and(|registration| registration.is_none())
            && self.supervisor.registration_spawn(connection).is_none()
    }

    fn policy_root_vault(&self, connection: ConnectionId) -> bool {
        let nonce = self.supervisor.spawn_launch_nonce_for(VAULT);
        let launched = matches!(
            self.connection_is_current_launch(VAULT, connection, nonce.as_deref()),
            crate::scopes::LaunchStanding::Current
        );
        let reserved = if self.supervisor.registration_spawn(connection).is_some() {
            self.rescan.as_ref().is_some_and(|rescan| {
                rescan
                    .module_entries
                    .lock()
                    .is_ok_and(|entries| entries.reserved(VAULT))
            })
        } else {
            nonce.is_some()
                && self
                    .supervisor
                    .reserved_hello_verification(VAULT, nonce.as_deref())
                    .is_ok_and(|proof| proof.nonce_verified && proof.prefix_owner.is_none())
        };
        launched && reserved
    }

    pub(super) async fn handle_policy_root(
        &self,
        ctx: &RouteCtx,
        frame: Frame,
        op: &str,
    ) -> Result<Vec<Frame>, RouterError> {
        let connection = ctx.connection_id;
        let registration = self
            .registry
            .get_module_by_connection(connection)
            .map_err(|err| RouterError::backend(0, frame.header.corr, err.to_string()))?;
        let admitted = match op {
            GET_OP => registration.is_some() || self.policy_root_operator(connection),
            ROTATE_OP => registration
                .as_ref()
                .is_some_and(|module| module.manifest.module_id == VAULT),
            SET_OP | RECOVER_OP => self.policy_root_operator(connection),
            _ => false,
        };
        let result = if admitted {
            self.execute_policy_root(ctx, &frame, op).await
        } else {
            Err(refuse("policy_root_caller_not_permitted"))
        };
        Ok(vec![match result {
            Ok(response) => control_response_body_frame(&frame, &response, "policy root response")?,
            Err(error) => config_gate::config_error_frame(&frame, error)?,
        }])
    }

    async fn execute_policy_root(
        &self,
        ctx: &RouteCtx,
        frame: &Frame,
        op: &str,
    ) -> Result<serde_json::Value, GateError> {
        let malformed = |_| GateError::new("invalid_control_body", "invalid policy root request");
        let encode = |_| {
            GateError::new(
                "config_unavailable",
                "could not encode the policy root response",
            )
        };
        if op == GET_OP {
            let _: ClientControlRequest = serde_json::from_slice(&frame.body).map_err(malformed)?;
            let view = if let Some(owner) = &self.config_runtime {
                owner.on_blocking(|owner| owner.read_policy_root()).await?.0
            } else {
                subc_protocol::policy_root::PolicyRoot {
                    pin: None,
                    generation: 0,
                }
            };
            return serde_json::to_value(ClientControlResponse::PolicyRoot(view)).map_err(encode);
        }
        let owner = self
            .config_runtime
            .as_ref()
            .ok_or_else(|| GateError::new("config_gate_inactive", "no config store configured"))?;
        if op == ROTATE_OP {
            let request = serde_json::from_slice(&frame.body).map_err(malformed)?;
            let ModuleControlRequestFromModule::PolicyRootRotate(request) = request else {
                return Err(GateError::new(
                    "invalid_control_body",
                    "invalid policy root rotation",
                ));
            };
            let operation = self.supervisor.operation_lock();
            let _guard = operation.lock().await;
            if !self.policy_root_vault(ctx.connection_id) {
                return Err(refuse("policy_root_caller_not_permitted"));
            }
            let change = owner
                .on_blocking(move |owner| owner.plan_policy_root(Move::Rotate(request)))
                .await?;
            return serde_json::to_value(ModuleControlResponseToModule::PolicyRootRotate(change))
                .map_err(encode);
        }
        let request = serde_json::from_slice(&frame.body).map_err(malformed)?;
        let response = match request {
            ClientControlRequest::PolicyRootSet(key) => {
                let change = owner
                    .on_blocking(move |owner| {
                        owner.plan_policy_root(Move::Set(Key {
                            key_id: key.key_id,
                            public_key_hex: key.public_key_hex,
                        }))
                    })
                    .await?;
                ClientControlResponse::PolicyRootSet(change)
            }
            ClientControlRequest::PolicyRootRecover(request) => {
                let change = owner
                    .on_blocking(move |owner| owner.plan_policy_root(Move::Recover(request)))
                    .await?;
                ClientControlResponse::PolicyRootRecover(change)
            }
            _ => {
                return Err(GateError::new(
                    "invalid_control_body",
                    "invalid policy root move",
                ))
            }
        };
        serde_json::to_value(response).map_err(encode)
    }

    pub(super) async fn rescan_policy_root(&self, section: Section) {
        if let Some(owner) = &self.config_runtime {
            if let Err(error) = owner
                .on_blocking(move |owner| owner.rescan_policy_root(section))
                .await
            {
                tracing::warn!(%error, "agent policy trust root could not be reconciled");
            }
        }
    }

    pub(super) async fn apply_unstamped_policy_root(
        &self,
        ctx: &RouteCtx,
        frame: &Frame,
        owner: &Arc<crate::config_gate::runtime::RuntimeOwner>,
        digest: &subc_config::Digest,
    ) -> Result<Option<ConfigControlResponse>, GateError> {
        let query = digest.clone();
        let Some(review) = owner
            .on_blocking(move |owner| owner.unstamped_policy_root_review(&query))
            .await?
        else {
            return Ok(None);
        };
        if !self.policy_root_operator(ctx.connection_id) {
            return Err(refuse("policy_root_caller_not_permitted"));
        }
        if !self.forwarding.operator_confirms().config_gate_ready() {
            return Err(GateError::new(
                "approval_surface_unavailable",
                "policy root changes require interactive operator presence",
            ));
        }
        let request = owner.claim_approval_request(ctx.connection_id, frame.header.corr)?;
        let caller = self.resolve_caller(ctx.connection_id).await;
        request.request.ensure_live()?;
        let key = crate::operator_confirm::DeclineKey::for_caller(&caller, ctx.connection_id);
        let decision = self
            .forwarding
            .operator_confirms()
            .approve_config_gate(ctx, frame, review, caller.description(), key)
            .await;
        use crate::config_gate::PresenceDecision;
        match PresenceDecision::from_operator_outcome(&decision) {
            PresenceDecision::InteractiveApproval => {}
            PresenceDecision::Declined => {
                return Err(GateError::new(
                    "approval_declined",
                    "the operator declined the policy root change",
                ))
            }
            _ => {
                return Err(GateError::new(
                    "approval_surface_unavailable",
                    "interactive operator presence is unavailable",
                ))
            }
        }
        // A rescan that already read the old file must finish before this
        // apply publishes the new pin and its unstamped reader view.
        let operation = self.supervisor.operation_lock().lock_owned().await;
        let digest = digest.clone();
        let cancellation = request.request.clone();
        let generation = owner
            .on_blocking(move |owner| {
                let _operation = operation;
                owner.apply_unstamped_edits_after_presence(
                    &digest,
                    true,
                    config_gate::now_ms(),
                    || cancellation.claim_publication(),
                )
            })
            .await?
            .ok_or_else(|| {
                GateError::new("plan_stale", "the policy root plan is no longer pending")
            })?;
        Ok(Some(ConfigControlResponse::Applied {
            generation,
            restart_required: false,
        }))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn owner_runtime(
        root: &cortexkit_test_support::ScratchDir,
    ) -> Arc<crate::config_gate::runtime::RuntimeOwner> {
        use crate::config_gate::capture::{ConfigCapturer, OfflineProjectRegistry};
        use crate::config_store::{ConfigStore, StoreRoot};

        let settings = root.join("subc.jsonc");
        std::fs::write(&settings, br#"{"version":1}"#).unwrap();
        std::fs::create_dir(root.join("store")).unwrap();
        Arc::new(
            crate::config_gate::runtime::RuntimeOwner::new(
                "policy-root-caller-tests".into(),
                ConfigStore::open_digest_only_for_tests(
                    StoreRoot::from_service_definition(root.join("store")).unwrap(),
                ),
                ConfigCapturer {
                    daemon_path: settings,
                    config_home: root.to_path_buf(),
                    projects: Arc::new(OfflineProjectRegistry),
                },
            )
            .with_policy_root_data(std::fs::canonicalize(root).unwrap()),
        )
    }

    fn context(
        handler: &ControlHandler,
        local: bool,
        module: Option<&str>,
    ) -> (
        RouteCtx,
        tokio::sync::mpsc::Receiver<crate::router::OutboundFrame>,
    ) {
        let connection_id = ConnectionId::new(700);
        if local {
            handler.record_local_connection(connection_id);
        }
        #[cfg(unix)]
        let peer = ObservedPeer::observed_uid(subc_os::effective_uid());
        #[cfg(windows)]
        let peer =
            ObservedPeer::observed_sid(subc_os::named_pipe::current_user_sid_string().unwrap());
        #[cfg(not(any(unix, windows)))]
        let peer = ObservedPeer::unobserved();
        handler
            .observed_peers
            .lock()
            .unwrap()
            .insert(connection_id, peer);
        if let Some(module) = module {
            handler
                .registry
                .register_with_control_ops(
                    subc_protocol::manifest::ModuleManifest::builder(module, "1.0.0").build(),
                    PROTOCOL_VERSION,
                    connection_id,
                    vec![],
                )
                .unwrap();
        }
        let (send, recv) = tokio::sync::mpsc::channel(8);
        (
            RouteCtx {
                connection_id,
                egress: crate::FrameSink::new(send),
            },
            recv,
        )
    }

    async fn refusal(
        handler: &ControlHandler,
        ctx: &RouteCtx,
        body: serde_json::Value,
    ) -> serde_json::Value {
        let request = Frame::build(
            FrameType::Request,
            control_flags(),
            0,
            0,
            1,
            serde_json::to_vec(&body).unwrap(),
        )
        .unwrap();
        let replies = handler
            .handle_control_frame_ingress(ctx, request, None, false)
            .await
            .unwrap();
        assert_eq!(replies[0].header.ty, FrameType::Error);
        serde_json::from_slice::<serde_json::Value>(&replies[0].body).unwrap()["code"].clone()
    }

    #[tokio::test]
    async fn policy_root_review_rotate_requires_vault_id_and_a_verified_launch() {
        let root = cortexkit_test_support::ScratchDir::new("policy-root-rotate-callers");
        let runtime = owner_runtime(&root);
        for (module, body) in [
            ("sample-module", json!({"op":ROTATE_OP})),
            (
                VAULT,
                json!({"op":ROTATE_OP,"statement_b64":"e30=","signature_hex":"00"}),
            ),
        ] {
            let handler = ControlHandler::new(Arc::new(Registry::default()))
                .with_config_runtime(runtime.clone());
            let (ctx, _recv) = context(&handler, true, Some(module));
            assert_eq!(
                refusal(&handler, &ctx, body).await,
                "policy_root_caller_not_permitted"
            );
        }
    }

    #[tokio::test]
    async fn policy_root_review_set_recover_and_unstamped_apply_require_local_operator() {
        let root = cortexkit_test_support::ScratchDir::new("policy-root-operator-callers");
        let runtime = owner_runtime(&root);
        let raw = ed25519_dalek::SigningKey::from_bytes(&[23; 32])
            .verifying_key()
            .to_bytes();
        let key = Key {
            key_id: subc_protocol::policy_root::key_id(&raw),
            public_key_hex: raw.iter().map(|byte| format!("{byte:02x}")).collect(),
        };
        let change = runtime.plan_policy_root(Move::Set(key.clone())).unwrap();
        for (local, module) in [(true, Some("sample-module")), (false, None)] {
            let handler = ControlHandler::new(Arc::new(Registry::default()))
                .with_config_runtime(runtime.clone());
            let (ctx, _recv) = context(&handler, local, module);
            for request in [
                json!({"op":SET_OP,"key_id":key.key_id,"public_key_hex":key.public_key_hex}),
                json!({"op":RECOVER_OP,"body_b64":"e30="}),
                json!({"op":"config.apply","digest":change.digest}),
            ] {
                assert_eq!(
                    refusal(&handler, &ctx, request).await,
                    "policy_root_caller_not_permitted"
                );
            }
        }
        assert!(runtime.read_policy_root().unwrap().0.pin.is_none());
        assert!(!root.join("policy-root").exists());
    }

    #[tokio::test]
    async fn policy_root_local_get_returns_null_and_client_cannot_rotate() {
        let handler = ControlHandler::new(Arc::new(Registry::default()));
        let connection_id = ConnectionId::new(700);
        handler.record_local_connection(connection_id);
        #[cfg(unix)]
        let peer = ObservedPeer::observed_uid(subc_os::effective_uid());
        #[cfg(windows)]
        let peer =
            ObservedPeer::observed_sid(subc_os::named_pipe::current_user_sid_string().unwrap());
        #[cfg(not(any(unix, windows)))]
        let peer = ObservedPeer::unobserved();
        handler
            .observed_peers
            .lock()
            .unwrap()
            .insert(connection_id, peer);
        let (send, _recv) = tokio::sync::mpsc::channel(8);
        let ctx = RouteCtx {
            connection_id,
            egress: crate::FrameSink::new(send),
        };
        let call = |body: serde_json::Value| {
            Frame::build(
                FrameType::Request,
                control_flags(),
                0,
                0,
                1,
                serde_json::to_vec(&body).unwrap(),
            )
            .unwrap()
        };
        let reply = handler
            .handle_control_frame_ingress(&ctx, call(json!({"op":GET_OP})), None, false)
            .await
            .unwrap();
        assert_eq!(reply[0].header.ty, FrameType::Response);
        assert_eq!(
            serde_json::from_slice::<serde_json::Value>(&reply[0].body).unwrap(),
            json!({"op":GET_OP,"pin":null,"generation":0})
        );
        let denied = handler
            .handle_control_frame_ingress(
                &ctx,
                call(json!({"op":ROTATE_OP,"statement_b64":"e30=","signature_hex":"00"})),
                None,
                false,
            )
            .await
            .unwrap();
        assert_eq!(denied[0].header.ty, FrameType::Error);
        assert_eq!(
            serde_json::from_slice::<serde_json::Value>(&denied[0].body).unwrap()["code"],
            "policy_root_caller_not_permitted"
        );
    }
}