kapsel 0.1.0-alpha.1

Crash-recoverable gateway for one authorized Kubernetes Deployment image operation
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
//! Application-owned composition for the one KAP-0038 operation.
//!
//! This module separates request-only agent intent from operator-owned authorization, Kubernetes
//! authority, receipt signing material, and durable paths. It is not a configuration-file grammar
//! or command adapter.

use std::{
    error::Error,
    fmt, fs, io,
    os::unix::fs::{MetadataExt, PermissionsExt},
    path::{Component, Path, PathBuf},
};

use ed25519_dalek::SigningKey;

use crate::{
    authorization::verify_authorization_grant, publication, receipt, AuthorizationTrust,
    ExactAuthorization, Gateway, GatewayError, OperationResult, OperationState, ReceiptReference,
    ReceiptSettings, SetDeploymentImageRequest, SubmissionResult, TargetRejection,
};

/// Request-only caller input for the sole supported operation.
pub type AgentRequest = SetDeploymentImageRequest;

/// Inputs controlled by the operator before an application instance opens durable state.
///
/// The signed grant, trust, Kubernetes client, signing seed, and paths must come from application
/// composition rather than agent request fields. This type deliberately does not implement
/// `Debug`, preventing accidental diagnostics from printing its secret-bearing fields.
pub struct OperatorConfiguration {
    /// Journal location owned by the operator.
    pub journal_path: PathBuf,
    /// Pre-existing owner-private receipt output directory.
    pub receipt_output_directory: PathBuf,
    /// Out-of-band trust for the exact authorization-grant signer.
    pub authorization_trust: AuthorizationTrust,
    /// One owner-signed exact grant used for request submission.
    pub signed_authorization_grant: Vec<u8>,
    /// Kubernetes authority constructed outside agent input.
    pub kubernetes_client: kube::Client,
    /// Receipt-signing seed controlled by the operator.
    pub receipt_signing_seed: [u8; 32],
    /// Public identity for the receipt-signing key.
    pub receipt_signing_key_id: String,
}

/// Operator-only inputs for provisioning one exact authorization grant.
///
/// This type deliberately does not implement `Debug` because it contains signing material.
pub struct GrantProvisioning<'a> {
    /// Exact operation tuple the owner is authorizing.
    pub authorization: &'a ExactAuthorization,
    /// Owner-controlled Ed25519 signing seed.
    pub signing_seed: &'a [u8; 32],
    /// Public identity for the authorization signing key.
    pub signing_key_id: &'a str,
}

/// Produces the canonical fixed-purpose grant supplied later through operator configuration.
pub fn provision_exact_grant(
    provisioning: &GrantProvisioning<'_>,
) -> Result<Vec<u8>, ApplicationError> {
    crate::authorization::sign_authorization_grant(
        provisioning.authorization,
        provisioning.signing_seed,
        provisioning.signing_key_id,
    )
    .map_err(|_| ApplicationError::InvalidGrantProvisioning)
}

/// Application-level report shared by future local and MCP adapters.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct OperationReport {
    /// Stable operation identity fixed by the configured authorization grant.
    pub operation_id: String,
    /// Current durable lifecycle state.
    pub state: OperationState,
    /// Receiver result, present only after receiver observation.
    pub result: Option<OperationResult>,
    /// Pre-attempt target rejection, distinct from a receiver result.
    pub target_rejection: Option<TargetRejection>,
    /// Frozen receipt reference, present only after finalization.
    pub receipt: Option<ReceiptReference>,
}

/// Compile-time composition root for the evaluator application.
pub struct Application {
    gateway: Gateway,
    kubernetes_client: kube::Client,
    signed_authorization_grant: Vec<u8>,
    authorized_request: AgentRequest,
    receipt_signing_key: SigningKey,
    receipt_signing_key_id: String,
    receipt_output_directory: PathBuf,
}

impl Application {
    /// Validates operator configuration before opening or creating the journal.
    ///
    /// Grant trust, canonical grant bytes, receipt key identity, and output-directory safety are
    /// checked before durable state is opened. Constructing the Kubernetes client and protecting
    /// its credentials remain operator responsibilities.
    pub fn open(configuration: OperatorConfiguration) -> Result<Self, ApplicationError> {
        let verified = verify_authorization_grant(
            &configuration.signed_authorization_grant,
            &configuration.authorization_trust,
        )
        .map_err(|_| ApplicationError::InvalidAuthorizationConfiguration)?;
        receipt::validate_key_id(&configuration.receipt_signing_key_id)
            .map_err(|_| ApplicationError::InvalidReceiptConfiguration)?;
        publication::validate_private_directory(&configuration.receipt_output_directory)
            .map_err(|_| ApplicationError::InvalidReceiptOutputDirectory)?;
        validate_journal_path(&configuration.journal_path)?;

        let authorized_request = AgentRequest {
            operation_id: verified.authorization.operation_id,
            namespace: verified.authorization.namespace,
            deployment: verified.authorization.deployment,
            container: verified.authorization.container,
            immutable_image_digest: verified.authorization.immutable_image_digest,
        };
        let gateway = Gateway::open(
            &configuration.journal_path,
            configuration.authorization_trust,
        )
        .map_err(ApplicationError::Gateway)?;
        Ok(Self {
            gateway,
            kubernetes_client: configuration.kubernetes_client,
            signed_authorization_grant: configuration.signed_authorization_grant,
            authorized_request,
            receipt_signing_key: SigningKey::from_bytes(&configuration.receipt_signing_seed),
            receipt_signing_key_id: configuration.receipt_signing_key_id,
            receipt_output_directory: configuration.receipt_output_directory,
        })
    }

    /// Submits request-only intent under the operator-configured exact grant.
    pub fn submit(&self, request: &AgentRequest) -> Result<SubmissionResult, ApplicationError> {
        self.gateway
            .submit_authorized(request, &self.signed_authorization_grant)
            .map_err(ApplicationError::Gateway)
    }

    /// Submits request-only intent and owns all subsequent lifecycle sequencing.
    pub async fn execute(
        &mut self,
        request: &AgentRequest,
    ) -> Result<OperationReport, ApplicationError> {
        self.submit(request)?;
        self.reconcile()
            .await?
            .ok_or(ApplicationError::InvalidApplicationState)
    }

    /// Recovers and advances the configured operation to its next externally blocked or terminal
    /// report without allowing an adapter to sequence durable states.
    pub async fn reconcile(&mut self) -> Result<Option<OperationReport>, ApplicationError> {
        loop {
            let Some(report) = self.report()? else {
                return Ok(None);
            };
            match report.state {
                OperationState::Requested => {
                    self.gateway
                        .submit_authorized(
                            &self.authorized_request,
                            &self.signed_authorization_grant,
                        )
                        .map_err(ApplicationError::Gateway)?;
                },
                OperationState::Authorized | OperationState::ApplyStarted => {
                    if self
                        .gateway
                        .run_operation_once(
                            &self.authorized_request.operation_id,
                            self.kubernetes_client.clone(),
                        )
                        .await
                        .map_err(ApplicationError::Gateway)?
                        .is_none()
                    {
                        return self.report();
                    }
                },
                OperationState::ReceiverObserved
                | OperationState::ReceiptPrepared
                | OperationState::ReceiptWritten => {
                    if self
                        .gateway
                        .finalize_operation_receipt_once(
                            &self.authorized_request.operation_id,
                            &ReceiptSettings {
                                signing_seed: self.receipt_signing_key.as_bytes(),
                                key_id: &self.receipt_signing_key_id,
                                output_directory: &self.receipt_output_directory,
                            },
                        )
                        .map_err(ApplicationError::Gateway)?
                        .is_none()
                    {
                        return self.report();
                    }
                },
                OperationState::NotAttempted | OperationState::Finalized => {
                    return Ok(Some(report));
                },
            }
        }
    }

    /// Reports the configured operation without provider or network access.
    pub fn report(&self) -> Result<Option<OperationReport>, ApplicationError> {
        let operation_id = &self.authorized_request.operation_id;
        let Some(snapshot) = self
            .gateway
            .operation_snapshot(operation_id)
            .map_err(ApplicationError::Gateway)?
        else {
            return Ok(None);
        };
        Ok(Some(OperationReport {
            operation_id: operation_id.clone(),
            state: snapshot.state,
            result: snapshot.result,
            target_rejection: snapshot.target_rejection,
            receipt: snapshot.receipt,
        }))
    }
}

fn validate_journal_path(path: &Path) -> Result<(), ApplicationError> {
    if !path.is_absolute() || !matches!(path.components().next_back(), Some(Component::Normal(_))) {
        return Err(ApplicationError::InvalidJournalPath);
    }
    let parent = path.parent().ok_or(ApplicationError::InvalidJournalPath)?;
    publication::validate_private_directory(parent)
        .map_err(|_| ApplicationError::InvalidJournalPath)?;
    validate_private_file_or_missing(path)?;

    let mut worker_lock_path = path.as_os_str().to_os_string();
    worker_lock_path.push(".kap0038-worker.lock");
    validate_private_file_or_missing(Path::new(&worker_lock_path))
}

fn validate_private_file_or_missing(path: &Path) -> Result<(), ApplicationError> {
    match fs::symlink_metadata(path) {
        Ok(metadata)
            if metadata.file_type().is_file()
                && metadata.uid() == rustix::process::geteuid().as_raw()
                && metadata.permissions().mode().trailing_zeros() >= 6 =>
        {
            Ok(())
        },
        Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(()),
        Ok(_) | Err(_) => Err(ApplicationError::InvalidJournalPath),
    }
}

/// Bounded application composition or operation failure.
#[derive(Debug)]
pub enum ApplicationError {
    /// Operator grant-signing inputs were invalid.
    InvalidGrantProvisioning,
    /// Configured grant bytes or trust were invalid.
    InvalidAuthorizationConfiguration,
    /// Receipt signing-key identity was invalid.
    InvalidReceiptConfiguration,
    /// Journal path was relative, unsafe, symlinked, or outside an owner-private directory.
    InvalidJournalPath,
    /// Receipt output was absent, unsafe, or not owner-private.
    InvalidReceiptOutputDirectory,
    /// Application state did not contain the configured operation after submission.
    InvalidApplicationState,
    /// The deep gateway rejected configuration, intent, durable state, or provider interaction.
    Gateway(GatewayError),
}

impl fmt::Display for ApplicationError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        let class = match self {
            Self::InvalidGrantProvisioning => "invalid_grant_provisioning",
            Self::InvalidAuthorizationConfiguration => "invalid_authorization_configuration",
            Self::InvalidReceiptConfiguration => "invalid_receipt_configuration",
            Self::InvalidJournalPath => "invalid_journal_path",
            Self::InvalidReceiptOutputDirectory => "invalid_receipt_output_directory",
            Self::InvalidApplicationState => "invalid_application_state",
            Self::Gateway(_) => "gateway",
        };
        write!(formatter, "Kapsel application failure: {class}")
    }
}

impl Error for ApplicationError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            Self::Gateway(error) => Some(error),
            Self::InvalidGrantProvisioning
            | Self::InvalidAuthorizationConfiguration
            | Self::InvalidReceiptConfiguration
            | Self::InvalidJournalPath
            | Self::InvalidReceiptOutputDirectory
            | Self::InvalidApplicationState => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use std::{fs, os::unix::fs::PermissionsExt, path::Path};

    use ed25519_dalek::SigningKey;
    use tower_test::mock;

    use super::*;

    fn request() -> AgentRequest {
        AgentRequest {
            operation_id: "application-op-1".into(),
            namespace: "demo".into(),
            deployment: "agent-api".into(),
            container: "api".into(),
            immutable_image_digest: concat!(
                "registry.example/agent-api@sha256:",
                "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
            )
            .into(),
        }
    }

    fn authorization(request: &AgentRequest) -> ExactAuthorization {
        ExactAuthorization {
            authorization_id: "application-auth-1".into(),
            operation_id: request.operation_id.clone(),
            namespace: request.namespace.clone(),
            deployment: request.deployment.clone(),
            container: request.container.clone(),
            immutable_image_digest: request.immutable_image_digest.clone(),
        }
    }

    fn private_directory(path: &Path) {
        fs::create_dir(path).unwrap();
        fs::set_permissions(path, fs::Permissions::from_mode(0o700)).unwrap();
    }

    type KubernetesHandle =
        mock::Handle<http::Request<kube::client::Body>, http::Response<kube::client::Body>>;

    fn configuration(root: &Path) -> OperatorConfiguration {
        configuration_and_handle(root).0
    }

    fn configuration_and_handle(root: &Path) -> (OperatorConfiguration, KubernetesHandle) {
        let request = request();
        let authorization_seed = [41_u8; 32];
        let authorization_key = SigningKey::from_bytes(&authorization_seed);
        let signed_authorization_grant = provision_exact_grant(&GrantProvisioning {
            authorization: &authorization(&request),
            signing_seed: &authorization_seed,
            signing_key_id: "application-authorization-key",
        })
        .unwrap();
        let output = root.join("receipts");
        private_directory(&output);
        let output = fs::canonicalize(output).unwrap();
        let (service, handle) =
            mock::pair::<http::Request<kube::client::Body>, http::Response<kube::client::Body>>();
        let configuration = OperatorConfiguration {
            journal_path: fs::canonicalize(root).unwrap().join("journal.sqlite3"),
            receipt_output_directory: output,
            authorization_trust: AuthorizationTrust {
                key_id: "application-authorization-key".into(),
                public_key: authorization_key.verifying_key().to_bytes(),
            },
            signed_authorization_grant,
            kubernetes_client: kube::Client::new(service, "demo"),
            receipt_signing_seed: [42_u8; 32],
            receipt_signing_key_id: "application-receipt-key".into(),
        };
        (configuration, handle)
    }

    #[tokio::test]
    async fn execute_owns_target_rejection_lifecycle() {
        let root =
            std::env::temp_dir().join(format!("kapsel-application-execute-{}", std::process::id()));
        let _ = fs::remove_dir_all(&root);
        private_directory(&root);
        let (configuration, mut handle) = configuration_and_handle(&root);
        let mut application = Application::open(configuration).unwrap();
        let responder = tokio::spawn(async move {
            let (_, send) = handle.next_request().await.unwrap();
            send.send_response(
                http::Response::builder()
                    .status(http::StatusCode::NOT_FOUND)
                    .body(kube::client::Body::from(
                        serde_json::to_vec(&serde_json::json!({
                            "apiVersion": "v1",
                            "kind": "Status",
                            "status": "Failure",
                            "message": "deployments.apps agent-api not found",
                            "reason": "NotFound",
                            "details": {
                                "name": "agent-api",
                                "group": "apps",
                                "kind": "deployments"
                            },
                            "code": 404
                        }))
                        .unwrap(),
                    ))
                    .unwrap(),
            );
        });

        let report = application.execute(&request()).await.unwrap();

        assert_eq!(report.state, OperationState::NotAttempted);
        assert_eq!(
            report.target_rejection,
            Some(TargetRejection::DeploymentNotFound)
        );
        assert_eq!(report.result, None);
        assert_eq!(report.receipt, None);
        responder.await.unwrap();
        drop(application);
        fs::remove_dir_all(root).unwrap();
    }

    #[tokio::test]
    async fn request_only_submission_uses_operator_configured_grant() {
        let root =
            std::env::temp_dir().join(format!("kapsel-application-submit-{}", std::process::id()));
        let _ = fs::remove_dir_all(&root);
        private_directory(&root);
        let application = Application::open(configuration(&root)).unwrap();
        let request = request();

        assert_eq!(
            application.submit(&request).unwrap(),
            SubmissionResult::Created
        );
        assert_eq!(
            application.report().unwrap(),
            Some(OperationReport {
                operation_id: request.operation_id,
                state: OperationState::Authorized,
                result: None,
                target_rejection: None,
                receipt: None,
            })
        );
        assert_eq!(
            fs::metadata(root.join("journal.sqlite3"))
                .unwrap()
                .permissions()
                .mode()
                & 0o777,
            0o600
        );
        drop(application);
        fs::remove_dir_all(root).unwrap();
    }

    #[tokio::test]
    async fn invalid_operator_configuration_precedes_journal_creation() {
        let root = std::env::temp_dir().join(format!(
            "kapsel-application-configuration-{}",
            std::process::id()
        ));
        let _ = fs::remove_dir_all(&root);
        private_directory(&root);
        let mut configuration = configuration(&root);
        configuration.signed_authorization_grant = b"self-appointed".to_vec();
        let journal = configuration.journal_path.clone();

        assert!(matches!(
            Application::open(configuration),
            Err(ApplicationError::InvalidAuthorizationConfiguration)
        ));
        assert!(!journal.exists());
        fs::remove_dir_all(root).unwrap();
    }

    #[tokio::test]
    async fn unsafe_journal_path_is_rejected_before_creation() {
        let root = std::env::temp_dir().join(format!(
            "kapsel-application-journal-path-{}",
            std::process::id()
        ));
        let _ = fs::remove_dir_all(&root);
        private_directory(&root);
        let mut configuration = configuration(&root);
        configuration.journal_path = PathBuf::from("relative-journal.sqlite3");

        assert!(matches!(
            Application::open(configuration),
            Err(ApplicationError::InvalidJournalPath)
        ));
        assert!(!Path::new("relative-journal.sqlite3").exists());
        fs::remove_dir_all(root).unwrap();
    }

    #[tokio::test]
    async fn mismatched_intent_does_not_create_an_operation() {
        let root = std::env::temp_dir().join(format!(
            "kapsel-application-mismatch-{}",
            std::process::id()
        ));
        let _ = fs::remove_dir_all(&root);
        private_directory(&root);
        let application = Application::open(configuration(&root)).unwrap();
        let mut mismatched = request();
        mismatched.container = "other".into();

        assert!(matches!(
            application.submit(&mismatched),
            Err(ApplicationError::Gateway(
                GatewayError::AuthorizationMismatch
            ))
        ));
        assert_eq!(application.report().unwrap(), None);
        drop(application);
        fs::remove_dir_all(root).unwrap();
    }
}