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
//! Explicit live-cluster proof for the KAP-0038 Deployment-image operation.

use std::{collections::BTreeMap, fs, os::unix::fs::PermissionsExt, path::PathBuf};

use ed25519_dalek::SigningKey;

use k8s_openapi::{
    api::{
        apps::v1::{Deployment, DeploymentSpec},
        core::v1::{Container, Namespace, PodSpec, PodTemplateSpec},
    },
    apimachinery::pkg::apis::meta::v1::{LabelSelector, ObjectMeta},
};
use kube::{
    api::{Api, DeleteParams, PostParams},
    Client,
};

use crate::{
    inspect_receipt, kubernetes_adapter::KubernetesDeploymentImageAdapter, DeploymentImageAdapter,
    ExactAuthorization, FaultPoint, Gateway, GatewayError, InspectionLimits, InspectionStatus,
    OperationResult, OperationState, ReceiptSettings, ReceiptTrust, SetDeploymentImageRequest,
};

const NAMESPACE: &str = "kapsel-kap0038";
const FAILED_NAMESPACE: &str = "kapsel-kap0038-failed";
const DEPLOYMENT: &str = "image-demo";
const FAILED_DEPLOYMENT: &str = "image-demo-failed";
const TARGET_IMAGE: &str = concat!(
    "registry.k8s.io/pause@sha256:",
    "278fb9dbcca9518083ad1e11276933a2e96f23de604a3a08cc3c80002767d24c"
);
const FAILED_IMAGE: &str = concat!(
    "registry.example.invalid/kapsel/unhealthy@sha256:",
    "1111111111111111111111111111111111111111111111111111111111111111"
);
const FIXTURE_IMAGE: &str = "registry.k8s.io/pause:3.10.1";

struct CountingAdapter {
    inner: KubernetesDeploymentImageAdapter,
    apply_calls: usize,
}

impl CountingAdapter {
    fn new(client: Client) -> Self {
        Self {
            inner: KubernetesDeploymentImageAdapter::new(client),
            apply_calls: 0,
        }
    }
}

impl DeploymentImageAdapter for CountingAdapter {
    async fn identify(
        &mut self,
        request: &SetDeploymentImageRequest,
    ) -> Result<crate::kubernetes_facts::TargetIdentity, crate::TargetReadError> {
        self.inner.identify(request).await
    }

    async fn apply(
        &mut self,
        request: &SetDeploymentImageRequest,
        target: &crate::kubernetes_facts::TargetIdentity,
    ) -> Result<crate::kubernetes_facts::ApplyOutcome, ()> {
        self.apply_calls += 1;
        self.inner.apply(request, target).await
    }

    async fn observe(
        &mut self,
        request: &SetDeploymentImageRequest,
    ) -> Result<crate::kubernetes_facts::ReceiverObservation, ()> {
        self.inner.observe(request).await
    }
}

#[tokio::test]
#[ignore = "requires scripts/test-kind-effect-gateway.sh"]
async fn kind_changes_exactly_one_container_through_the_gateway() {
    assert_eq!(std::env::var("KAPSEL_KIND_TEST").as_deref(), Ok("1"));
    let client = Client::try_default().await.unwrap();
    let namespaces: Api<Namespace> = Api::all(client.clone());
    tokio::time::timeout(
        std::time::Duration::from_secs(10),
        namespaces.create(
            &PostParams::default(),
            &Namespace {
                metadata: ObjectMeta {
                    name: Some(NAMESPACE.into()),
                    ..ObjectMeta::default()
                },
                ..Namespace::default()
            },
        ),
    )
    .await
    .unwrap()
    .unwrap();
    let proof = tokio::time::timeout(
        std::time::Duration::from_mins(1),
        run_gateway_proof(client.clone()),
    )
    .await
    .map_or_else(
        |_| Err("kind gateway proof exceeded 60 seconds".into()),
        |result| result.map_err(|error| error.to_string()),
    );
    let cleanup = tokio::time::timeout(
        std::time::Duration::from_secs(10),
        namespaces.delete(NAMESPACE, &DeleteParams::default()),
    )
    .await
    .map_or_else(
        |_| Err("kind cleanup exceeded 10 seconds".into()),
        |result| result.map(|_| ()).map_err(|error| error.to_string()),
    );
    assert!(cleanup.is_ok(), "kind fixture cleanup failed");
    proof.unwrap();
}

#[tokio::test]
#[ignore = "requires scripts/test-kind-effect-gateway.sh"]
async fn kind_failed_rollout_recovers_and_inspects_classifier_complete_receipt() {
    assert_eq!(std::env::var("KAPSEL_KIND_TEST").as_deref(), Ok("1"));
    let client = Client::try_default().await.unwrap();
    let namespaces: Api<Namespace> = Api::all(client.clone());
    tokio::time::timeout(
        std::time::Duration::from_secs(10),
        namespaces.create(
            &PostParams::default(),
            &Namespace {
                metadata: ObjectMeta {
                    name: Some(FAILED_NAMESPACE.into()),
                    ..ObjectMeta::default()
                },
                ..Namespace::default()
            },
        ),
    )
    .await
    .unwrap()
    .unwrap();
    let proof = tokio::time::timeout(
        std::time::Duration::from_mins(1),
        run_failed_rollout_proof(client.clone()),
    )
    .await
    .map_or_else(
        |_| Err("kind failed-rollout proof exceeded 60 seconds".into()),
        |result| result.map_err(|error| error.to_string()),
    );
    let cleanup = tokio::time::timeout(
        std::time::Duration::from_secs(10),
        namespaces.delete(FAILED_NAMESPACE, &DeleteParams::default()),
    )
    .await
    .map_or_else(
        |_| Err("kind failed-rollout cleanup exceeded 10 seconds".into()),
        |result| result.map(|_| ()).map_err(|error| error.to_string()),
    );
    assert!(
        cleanup.is_ok(),
        "kind failed-rollout fixture cleanup failed"
    );
    proof.unwrap();
}

async fn run_gateway_proof(client: Client) -> Result<(), Box<dyn std::error::Error>> {
    let deployments: Api<Deployment> = Api::namespaced(client.clone(), NAMESPACE);
    tokio::time::timeout(
        std::time::Duration::from_secs(10),
        deployments.create(
            &PostParams::default(),
            &fixture_deployment_for(NAMESPACE, DEPLOYMENT),
        ),
    )
    .await??;
    wait_for_deployment_rollout(&deployments, DEPLOYMENT).await?;
    let request = request();
    let authorization = ExactAuthorization {
        authorization_id: "kind-auth-001".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(),
    };
    let directory = private_test_directory_for("success");
    let database = directory.join("journal.sqlite3");
    let mut gateway = Gateway::open_for_test(&database)?;
    gateway.submit_exact_for_test(&request, &authorization)?;
    let mut adapter = KubernetesDeploymentImageAdapter::new(client.clone());
    match gateway
        .run_once_with_adapter(&mut adapter, Some(FaultPoint::ApplyReturned))
        .await
    {
        Err(GatewayError::InjectedFault) => {},
        Err(error) => return Err(error.into()),
        Ok(_) => return Err("kind fault injection did not stop after the patch".into()),
    }
    assert_eq!(
        gateway.get(&request.operation_id)?,
        Some(OperationState::ApplyStarted)
    );
    drop(gateway);
    let mut gateway = Gateway::open_for_test(&database)?;

    let state = gateway.run_once(client).await?;

    assert_eq!(state, Some(OperationState::ReceiverObserved));
    assert_eq!(
        gateway.result(&request.operation_id)?,
        Some(OperationResult::Succeeded)
    );
    let observed = tokio::time::timeout(
        std::time::Duration::from_secs(10),
        deployments.get(DEPLOYMENT),
    )
    .await??;
    let containers = &observed
        .spec
        .as_ref()
        .and_then(|spec| spec.template.spec.as_ref())
        .ok_or("missing fixture pod spec")?
        .containers;
    assert_eq!(containers.len(), 2);
    assert_eq!(
        containers
            .iter()
            .find(|container| container.name == "target")
            .and_then(|container| container.image.as_deref()),
        Some(TARGET_IMAGE)
    );
    assert_eq!(
        containers
            .iter()
            .find(|container| container.name == "untouched")
            .and_then(|container| container.image.as_deref()),
        Some(FIXTURE_IMAGE)
    );
    drop(gateway);
    fs::remove_dir_all(directory)?;
    Ok(())
}

async fn run_failed_rollout_proof(client: Client) -> Result<(), Box<dyn std::error::Error>> {
    let deployments: Api<Deployment> = Api::namespaced(client.clone(), FAILED_NAMESPACE);
    tokio::time::timeout(
        std::time::Duration::from_secs(10),
        deployments.create(
            &PostParams::default(),
            &fixture_deployment_for(FAILED_NAMESPACE, FAILED_DEPLOYMENT),
        ),
    )
    .await??;
    wait_for_deployment_rollout(&deployments, FAILED_DEPLOYMENT).await?;
    let request = failed_request();
    let authorization = ExactAuthorization {
        authorization_id: "kind-failed-auth-001".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(),
    };
    let directory = private_test_directory_for("failed");
    let receipt_directory = directory.join("receipts");
    fs::create_dir(&receipt_directory)?;
    fs::set_permissions(&receipt_directory, fs::Permissions::from_mode(0o700))?;
    let database = directory.join("journal.sqlite3");
    let mut gateway = Gateway::open_for_test(&database)?;
    gateway.submit_exact_for_test(&request, &authorization)?;
    let mut first_adapter = CountingAdapter::new(client.clone());
    match gateway
        .run_once_with_adapter(&mut first_adapter, Some(FaultPoint::ApplyReturned))
        .await
    {
        Err(GatewayError::InjectedFault) => {},
        Err(error) => return Err(error.into()),
        Ok(_) => return Err("kind failed-rollout fault did not stop after patch".into()),
    }
    assert_eq!(first_adapter.apply_calls, 1);
    drop(gateway);

    let mut gateway = Gateway::open_for_test(&database)?;
    let mut recovery_adapter = CountingAdapter::new(client);
    assert_eq!(
        gateway
            .run_once_with_adapter(&mut recovery_adapter, None)
            .await?,
        Some(OperationState::ReceiverObserved)
    );
    assert_eq!(recovery_adapter.apply_calls, 0);
    assert_eq!(
        gateway.result(&request.operation_id)?,
        Some(OperationResult::Failed)
    );

    let receipt_seed = [41_u8; 32];
    assert_eq!(
        gateway.finalize_receipt_once(&ReceiptSettings {
            signing_seed: &receipt_seed,
            key_id: "kind-failed-receipt-key",
            output_directory: &receipt_directory,
        })?,
        Some(OperationState::Finalized)
    );
    let reference = gateway
        .receipt_reference(&request.operation_id)?
        .ok_or("missing failed-rollout receipt reference")?;
    let receipt_bytes = fs::read(reference.path)?;
    let trust = ReceiptTrust {
        key_id: "kind-failed-receipt-key".into(),
        public_key: SigningKey::from_bytes(&receipt_seed)
            .verifying_key()
            .to_bytes(),
        accepted_purpose: "kapsel.kap0038.kubernetes-effect-receipt.v2".into(),
        not_before_unix_s: 100,
        not_after_unix_s: 200,
    }
    .encode()?;
    let report = inspect_receipt(&receipt_bytes, &trust, 150, InspectionLimits::default());
    assert_eq!(report.status(), InspectionStatus::Inspected);
    let statement = report.statement().ok_or("missing inspected statement")?;
    assert_eq!(statement.result(), OperationResult::Failed);
    assert_eq!(
        statement.rollout_condition_reason(),
        Some("ProgressDeadlineExceeded")
    );
    assert_eq!(statement.observed_image(), Some(FAILED_IMAGE));
    assert_eq!(
        statement.observed_operation_marker(),
        Some("kind-failed-op-001")
    );
    drop(gateway);
    fs::remove_dir_all(directory)?;
    Ok(())
}

#[allow(clippy::print_stdout)]
async fn wait_for_deployment_rollout(
    deployments: &Api<Deployment>,
    deployment_name: &str,
) -> Result<(), Box<dyn std::error::Error>> {
    tokio::time::timeout(std::time::Duration::from_secs(30), async {
        loop {
            let deployment = deployments.get(deployment_name).await?;
            let generation = deployment.metadata.generation;
            let ready = deployment.status.as_ref().is_some_and(|status| {
                status.observed_generation == generation
                    && status.available_replicas == Some(1)
                    && status.updated_replicas == Some(1)
            });
            if ready {
                return Ok::<(), kube::Error>(());
            }
            println!("waiting for the disposable kind fixture rollout");
            tokio::time::sleep(std::time::Duration::from_secs(1)).await;
        }
    })
    .await
    .map_err(|_| "fixture rollout exceeded 30 seconds")??;
    Ok(())
}

fn request() -> SetDeploymentImageRequest {
    SetDeploymentImageRequest {
        operation_id: "kind-op-001".into(),
        namespace: NAMESPACE.into(),
        deployment: DEPLOYMENT.into(),
        container: "target".into(),
        immutable_image_digest: TARGET_IMAGE.into(),
    }
}

fn failed_request() -> SetDeploymentImageRequest {
    SetDeploymentImageRequest {
        operation_id: "kind-failed-op-001".into(),
        namespace: FAILED_NAMESPACE.into(),
        deployment: FAILED_DEPLOYMENT.into(),
        container: "target".into(),
        immutable_image_digest: FAILED_IMAGE.into(),
    }
}

fn fixture_deployment_for(namespace: &str, deployment: &str) -> Deployment {
    let labels = BTreeMap::from([("app".into(), deployment.into())]);
    Deployment {
        metadata: ObjectMeta {
            name: Some(deployment.into()),
            namespace: Some(namespace.into()),
            ..ObjectMeta::default()
        },
        spec: Some(DeploymentSpec {
            replicas: Some(1),
            selector: LabelSelector {
                match_labels: Some(labels.clone()),
                ..LabelSelector::default()
            },
            template: PodTemplateSpec {
                metadata: Some(ObjectMeta {
                    labels: Some(labels),
                    ..ObjectMeta::default()
                }),
                spec: Some(PodSpec {
                    containers: vec![
                        Container {
                            name: "target".into(),
                            image: Some(FIXTURE_IMAGE.into()),
                            ..Container::default()
                        },
                        Container {
                            name: "untouched".into(),
                            image: Some(FIXTURE_IMAGE.into()),
                            ..Container::default()
                        },
                    ],
                    ..PodSpec::default()
                }),
            },
            progress_deadline_seconds: Some(15),
            ..DeploymentSpec::default()
        }),
        ..Deployment::default()
    }
}

fn private_test_directory_for(scenario: &str) -> PathBuf {
    let path = std::env::temp_dir().join(format!(
        "kapsel-kind-proof-{}-{scenario}",
        std::process::id()
    ));
    let _ = fs::remove_dir_all(&path);
    fs::create_dir(&path).unwrap();
    fs::set_permissions(&path, fs::Permissions::from_mode(0o700)).unwrap();
    fs::canonicalize(path).unwrap()
}