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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
//! Private durable representation for KAP-0038 operations.
//!
//! This module owns SQLite schema, row decoding, capacity enforcement, and guarded transitions. It
//! is not a generic repository interface and is not exposed outside the effect-gateway crate.

use std::{
    fs::{self, File, TryLockError},
    io,
    os::unix::fs::{MetadataExt, PermissionsExt},
    path::{Path, PathBuf},
};

use rusqlite::{params, Connection, OptionalExtension};
use rustix::fs::{open, Mode, OFlags};

use crate::{
    authorization::VerifiedAuthorization,
    kubernetes_facts::{ApplyOutcome, ReceiverObservation, TargetIdentity},
    publication, validate_identity, FrozenReceipt, GatewayError, InputField, OperationResult,
    OperationState, ReceiptReference, ReceiptStatement, SetDeploymentImageRequest, TargetRejection,
};

pub const OPERATION_COUNT_MAX: i64 = 10_000;

pub struct Journal {
    pub connection: Connection,
    worker_lock: File,
}

pub struct WorkerLock<'a> {
    file: &'a File,
}

pub struct OperationSnapshot {
    pub state: OperationState,
    pub result: Option<OperationResult>,
    pub target_rejection: Option<TargetRejection>,
    pub receipt: Option<ReceiptReference>,
}

impl Drop for WorkerLock<'_> {
    fn drop(&mut self) {
        let _ = self.file.unlock();
    }
}

impl OperationState {
    fn as_sql(self) -> &'static str {
        match self {
            Self::Requested => "requested",
            Self::Authorized => "authorized",
            Self::NotAttempted => "not_attempted",
            Self::ApplyStarted => "apply_started",
            Self::ReceiverObserved => "receiver_observed",
            Self::ReceiptPrepared => "receipt_prepared",
            Self::ReceiptWritten => "receipt_written",
            Self::Finalized => "finalized",
        }
    }

    fn from_sql(value: &str) -> Result<Self, GatewayError> {
        match value {
            "requested" => Ok(Self::Requested),
            "authorized" => Ok(Self::Authorized),
            "not_attempted" => Ok(Self::NotAttempted),
            "apply_started" => Ok(Self::ApplyStarted),
            "receiver_observed" => Ok(Self::ReceiverObserved),
            "receipt_prepared" => Ok(Self::ReceiptPrepared),
            "receipt_written" => Ok(Self::ReceiptWritten),
            "finalized" => Ok(Self::Finalized),
            _ => Err(GatewayError::InvalidPersistedState),
        }
    }
}

impl TargetRejection {
    fn as_sql(self) -> &'static str {
        match self {
            Self::DeploymentNotFound => "deployment_not_found",
            Self::ContainerNotFound => "container_not_found",
            Self::InvalidTarget => "invalid_target",
        }
    }

    fn from_sql(value: &str) -> Result<Self, GatewayError> {
        match value {
            "deployment_not_found" => Ok(Self::DeploymentNotFound),
            "container_not_found" => Ok(Self::ContainerNotFound),
            "invalid_target" => Ok(Self::InvalidTarget),
            _ => Err(GatewayError::InvalidPersistedState),
        }
    }
}

impl OperationResult {
    fn as_sql(self) -> &'static str {
        match self {
            Self::Succeeded => "SUCCEEDED",
            Self::Failed => "FAILED",
            Self::Unknown => "UNKNOWN",
        }
    }

    fn from_sql(value: &str) -> Result<Self, GatewayError> {
        match value {
            "SUCCEEDED" => Ok(Self::Succeeded),
            "FAILED" => Ok(Self::Failed),
            "UNKNOWN" => Ok(Self::Unknown),
            _ => Err(GatewayError::InvalidPersistedState),
        }
    }
}

impl Journal {
    pub(super) fn open(path: impl AsRef<Path>) -> Result<Self, GatewayError> {
        let path = path.as_ref();
        let database_file = open_private_file(path).map_err(GatewayError::JournalFile)?;
        let database_identity = database_file
            .metadata()
            .map_err(GatewayError::JournalFile)?;
        let mut connection = Connection::open(path).map_err(GatewayError::Database)?;
        require_named_identity(path, &database_identity).map_err(GatewayError::JournalFile)?;
        connection
            .pragma_update(None, "journal_mode", "DELETE")
            .map_err(GatewayError::Database)?;
        connection
            .pragma_update(None, "synchronous", "FULL")
            .map_err(GatewayError::Database)?;
        let journal_mode = connection
            .query_row("PRAGMA journal_mode", [], |row| row.get::<_, String>(0))
            .map_err(GatewayError::Database)?;
        let synchronous = connection
            .query_row("PRAGMA synchronous", [], |row| row.get::<_, i64>(0))
            .map_err(GatewayError::Database)?;
        if !journal_mode.eq_ignore_ascii_case("delete") || synchronous != 2 {
            return Err(GatewayError::InvalidPersistedState);
        }
        let worker_lock_path = worker_lock_path(path);
        let worker_lock = open_private_file(&worker_lock_path).map_err(GatewayError::WorkerLock)?;
        let worker_lock_identity = worker_lock.metadata().map_err(GatewayError::WorkerLock)?;
        require_named_identity(&worker_lock_path, &worker_lock_identity)
            .map_err(GatewayError::WorkerLock)?;
        connection
            .execute_batch(
                "CREATE TABLE IF NOT EXISTS kubernetes_image_operations (
                    operation_id TEXT PRIMARY KEY NOT NULL,
                    namespace TEXT NOT NULL,
                    deployment TEXT NOT NULL,
                    container TEXT NOT NULL,
                    immutable_image_digest TEXT NOT NULL,
                    authorization_id TEXT,
                    authorization_signer_key_id TEXT,
                    authorization_grant_digest TEXT,
                    state TEXT NOT NULL,
                    write_strategy TEXT,
                    target_rejection TEXT,
                    target_read_failures INTEGER NOT NULL DEFAULT 0,
                    apply_attempted INTEGER NOT NULL DEFAULT 0,
                    target_uid TEXT,
                    target_resource_version TEXT,
                    apply_accepted INTEGER,
                    requested_generation INTEGER,
                    apply_resource_version TEXT,
                    receiver_uid TEXT,
                    receiver_image TEXT,
                    receiver_operation_marker TEXT,
                    current_generation INTEGER,
                    observed_generation INTEGER,
                    receiver_resource_version TEXT,
                    desired_replicas INTEGER,
                    updated_replicas INTEGER,
                    available_replicas INTEGER,
                    unavailable_replicas INTEGER,
                    available_condition INTEGER,
                    progress_deadline_exceeded INTEGER,
                    result TEXT,
                    receipt_path TEXT,
                    receipt_digest TEXT,
                    receipt_bytes BLOB,
                    receipt_key_id TEXT,
                    rollout_condition_type TEXT,
                    rollout_condition_status TEXT,
                    rollout_condition_reason TEXT
                ) STRICT;",
            )
            .map_err(GatewayError::Database)?;
        migrate_receipt_schema(&mut connection)?;
        Ok(Self {
            connection,
            worker_lock,
        })
    }

    pub(super) fn try_lock_worker(&self) -> Result<Option<WorkerLock<'_>>, GatewayError> {
        match self.worker_lock.try_lock() {
            Ok(()) => Ok(Some(WorkerLock {
                file: &self.worker_lock,
            })),
            Err(TryLockError::WouldBlock) => Ok(None),
            Err(TryLockError::Error(error)) => Err(GatewayError::WorkerLock(error)),
        }
    }

    pub(super) fn existing_submission(
        &self,
        request: &SetDeploymentImageRequest,
        authorization: &VerifiedAuthorization,
    ) -> Result<Option<OperationState>, GatewayError> {
        let existing = self
            .connection
            .query_row(
                "SELECT namespace, deployment, container, immutable_image_digest,
                        authorization_id, authorization_signer_key_id,
                        authorization_grant_digest, state
                 FROM kubernetes_image_operations
                 WHERE operation_id = ?1",
                [&request.operation_id],
                |row| {
                    Ok((
                        row.get::<_, String>(0)?,
                        row.get::<_, String>(1)?,
                        row.get::<_, String>(2)?,
                        row.get::<_, String>(3)?,
                        row.get::<_, Option<String>>(4)?,
                        row.get::<_, Option<String>>(5)?,
                        row.get::<_, Option<String>>(6)?,
                        row.get::<_, String>(7)?,
                    ))
                },
            )
            .optional()
            .map_err(GatewayError::Database)?;
        let Some((
            namespace,
            deployment,
            container,
            image,
            authorization_id,
            authorization_signer_key_id,
            authorization_grant_digest,
            state,
        )) = existing
        else {
            return Ok(None);
        };
        if namespace != request.namespace
            || deployment != request.deployment
            || container != request.container
            || image != request.immutable_image_digest
        {
            return Err(GatewayError::OperationIdentityConflict);
        }
        let state = OperationState::from_sql(&state)?;
        if state != OperationState::Requested
            && (authorization_id.as_deref()
                != Some(authorization.authorization.authorization_id.as_str())
                || authorization_signer_key_id.as_deref()
                    != Some(authorization.signer_key_id.as_str())
                || authorization_grant_digest.as_deref()
                    != Some(authorization.grant_digest.as_str()))
        {
            return Err(GatewayError::OperationIdentityConflict);
        }
        Ok(Some(state))
    }

    pub(super) fn insert_requested(
        &self,
        request: &SetDeploymentImageRequest,
    ) -> Result<(), GatewayError> {
        let operation_count = self
            .connection
            .query_row(
                "SELECT COUNT(*) FROM kubernetes_image_operations",
                [],
                |row| row.get::<_, i64>(0),
            )
            .map_err(GatewayError::Database)?;
        if operation_count >= OPERATION_COUNT_MAX {
            return Err(GatewayError::JournalFull);
        }
        self.connection
            .execute(
                "INSERT INTO kubernetes_image_operations (
                    operation_id, namespace, deployment, container,
                    immutable_image_digest, state
                ) VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
                params![
                    request.operation_id,
                    request.namespace,
                    request.deployment,
                    request.container,
                    request.immutable_image_digest,
                    OperationState::Requested.as_sql(),
                ],
            )
            .map_err(GatewayError::Database)?;
        Ok(())
    }

    pub(super) fn mark_authorized(
        &self,
        operation_id: &str,
        authorization: &VerifiedAuthorization,
    ) -> Result<(), GatewayError> {
        let changed = self
            .connection
            .execute(
                "UPDATE kubernetes_image_operations
                 SET state = ?1, authorization_id = ?2,
                     authorization_signer_key_id = ?3, authorization_grant_digest = ?4
                 WHERE operation_id = ?5 AND state = ?6",
                params![
                    OperationState::Authorized.as_sql(),
                    authorization.authorization.authorization_id,
                    authorization.signer_key_id,
                    authorization.grant_digest,
                    operation_id,
                    OperationState::Requested.as_sql(),
                ],
            )
            .map_err(GatewayError::Database)?;
        changed_one(changed)
    }

    #[cfg(test)]
    pub(super) fn state(&self, operation_id: &str) -> Result<Option<OperationState>, GatewayError> {
        self.connection
            .query_row(
                "SELECT state FROM kubernetes_image_operations WHERE operation_id = ?1",
                [operation_id],
                |row| row.get::<_, String>(0),
            )
            .optional()
            .map_err(GatewayError::Database)?
            .map(|state| OperationState::from_sql(&state))
            .transpose()
    }

    pub(super) fn operation_snapshot(
        &self,
        operation_id: &str,
    ) -> Result<Option<OperationSnapshot>, GatewayError> {
        let row = self
            .connection
            .query_row(
                "SELECT state, result, target_rejection, receipt_path, receipt_digest
                 FROM kubernetes_image_operations
                 WHERE operation_id = ?1",
                [operation_id],
                |row| {
                    Ok((
                        row.get::<_, String>(0)?,
                        row.get::<_, Option<String>>(1)?,
                        row.get::<_, Option<String>>(2)?,
                        row.get::<_, Option<String>>(3)?,
                        row.get::<_, Option<String>>(4)?,
                    ))
                },
            )
            .optional()
            .map_err(GatewayError::Database)?;
        let Some((state, result, rejection, receipt_path, receipt_digest)) = row else {
            return Ok(None);
        };
        let state = OperationState::from_sql(&state)?;
        let result = result
            .map(|value| OperationResult::from_sql(&value))
            .transpose()?;
        let target_rejection = rejection
            .map(|value| TargetRejection::from_sql(&value))
            .transpose()?;
        let receipt = if state == OperationState::Finalized {
            match (receipt_path, receipt_digest) {
                (Some(path), Some(digest)) => Some(ReceiptReference {
                    path: PathBuf::from(path),
                    digest,
                }),
                _ => return Err(GatewayError::InvalidPersistedState),
            }
        } else {
            None
        };
        Ok(Some(OperationSnapshot {
            state,
            result,
            target_rejection,
            receipt,
        }))
    }

    #[cfg(test)]
    pub(super) fn target_rejection(
        &self,
        operation_id: &str,
    ) -> Result<Option<TargetRejection>, GatewayError> {
        self.connection
            .query_row(
                "SELECT target_rejection
                 FROM kubernetes_image_operations
                 WHERE operation_id = ?1 AND state = ?2",
                params![operation_id, OperationState::NotAttempted.as_sql()],
                |row| row.get::<_, String>(0),
            )
            .optional()
            .map_err(GatewayError::Database)?
            .map(|value| TargetRejection::from_sql(&value))
            .transpose()
    }

    #[cfg(test)]
    pub(super) fn result(
        &self,
        operation_id: &str,
    ) -> Result<Option<OperationResult>, GatewayError> {
        self.connection
            .query_row(
                "SELECT result FROM kubernetes_image_operations WHERE operation_id = ?1",
                [operation_id],
                |row| row.get::<_, Option<String>>(0),
            )
            .optional()
            .map_err(GatewayError::Database)?
            .flatten()
            .map(|result| OperationResult::from_sql(&result))
            .transpose()
    }

    pub(super) fn receipt_statement(
        &self,
        operation_id: &str,
    ) -> Result<Option<ReceiptStatement>, GatewayError> {
        self.connection
            .query_row(
                "SELECT operation_id, authorization_id, authorization_signer_key_id,
                        authorization_grant_digest, namespace, deployment, container,
                        immutable_image_digest, write_strategy, target_uid,
                        target_resource_version, receiver_uid, receiver_image,
                        receiver_operation_marker, current_generation, requested_generation,
                        observed_generation, receiver_resource_version, desired_replicas,
                        updated_replicas, available_replicas, unavailable_replicas,
                        rollout_condition_type, rollout_condition_status,
                        rollout_condition_reason, result
                 FROM kubernetes_image_operations
                 WHERE operation_id = ?1 AND state IN (?2, ?3, ?4)",
                params![
                    operation_id,
                    OperationState::ReceiverObserved.as_sql(),
                    OperationState::ReceiptWritten.as_sql(),
                    OperationState::Finalized.as_sql(),
                ],
                ReceiptRow::from_sql,
            )
            .optional()
            .map_err(GatewayError::Database)?
            .map(ReceiptRow::into_statement)
            .transpose()
    }

    pub(super) fn prepare_receipt(&self, receipt: &FrozenReceipt) -> Result<(), GatewayError> {
        let path = receipt
            .path
            .to_str()
            .ok_or(GatewayError::ReceiptPublication)?;
        let changed = self
            .connection
            .execute(
                "UPDATE kubernetes_image_operations
                 SET state = ?1, receipt_path = ?2, receipt_digest = ?3,
                     receipt_bytes = ?4, receipt_key_id = ?5
                 WHERE operation_id = ?6 AND state = ?7",
                params![
                    OperationState::ReceiptPrepared.as_sql(),
                    path,
                    receipt.digest,
                    receipt.bytes,
                    receipt.key_id,
                    receipt.operation_id,
                    OperationState::ReceiverObserved.as_sql(),
                ],
            )
            .map_err(GatewayError::Database)?;
        changed_one(changed)
    }

    pub(super) fn mark_receipt_written(&self, operation_id: &str) -> Result<(), GatewayError> {
        let changed = self
            .connection
            .execute(
                "UPDATE kubernetes_image_operations
                 SET state = ?1
                 WHERE operation_id = ?2 AND state = ?3
                       AND receipt_path IS NOT NULL AND receipt_digest IS NOT NULL
                       AND receipt_bytes IS NOT NULL AND receipt_key_id IS NOT NULL",
                params![
                    OperationState::ReceiptWritten.as_sql(),
                    operation_id,
                    OperationState::ReceiptPrepared.as_sql(),
                ],
            )
            .map_err(GatewayError::Database)?;
        changed_one(changed)
    }

    #[cfg(test)]
    pub(super) fn frozen_receipt(
        &self,
        state: OperationState,
    ) -> Result<Option<FrozenReceipt>, GatewayError> {
        if !matches!(
            state,
            OperationState::ReceiptPrepared | OperationState::ReceiptWritten
        ) {
            return Err(GatewayError::InvalidPersistedState);
        }
        let receipt = self
            .connection
            .query_row(
                "SELECT operation_id, receipt_path, receipt_digest, receipt_bytes,
                        receipt_key_id
                 FROM kubernetes_image_operations
                 WHERE state = ?1
                 ORDER BY operation_id
                 LIMIT 1",
                [state.as_sql()],
                |row| {
                    Ok(FrozenReceipt {
                        operation_id: row.get(0)?,
                        path: PathBuf::from(row.get::<_, String>(1)?),
                        digest: row.get(2)?,
                        bytes: row.get(3)?,
                        key_id: row.get(4)?,
                    })
                },
            )
            .optional()
            .map_err(GatewayError::Database)?;
        let Some(receipt) = receipt else {
            return Ok(None);
        };
        if receipt.bytes.len() > crate::receipt::RECEIPT_BYTES_MAX
            || publication::receipt_digest_hex(&receipt.bytes) != receipt.digest
        {
            return Err(GatewayError::ReceiptDigestMismatch);
        }
        validate_identity(InputField::AuthorizationId, &receipt.key_id)
            .map_err(|_| GatewayError::InvalidPersistedState)?;
        Ok(Some(receipt))
    }

    pub(super) fn frozen_receipt_for(
        &self,
        operation_id: &str,
        state: OperationState,
    ) -> Result<Option<FrozenReceipt>, GatewayError> {
        if !matches!(
            state,
            OperationState::ReceiptPrepared | OperationState::ReceiptWritten
        ) {
            return Err(GatewayError::InvalidPersistedState);
        }
        let receipt = self
            .connection
            .query_row(
                "SELECT operation_id, receipt_path, receipt_digest, receipt_bytes,
                        receipt_key_id
                 FROM kubernetes_image_operations
                 WHERE operation_id = ?1 AND state = ?2",
                params![operation_id, state.as_sql()],
                |row| {
                    Ok(FrozenReceipt {
                        operation_id: row.get(0)?,
                        path: PathBuf::from(row.get::<_, String>(1)?),
                        digest: row.get(2)?,
                        bytes: row.get(3)?,
                        key_id: row.get(4)?,
                    })
                },
            )
            .optional()
            .map_err(GatewayError::Database)?;
        let Some(receipt) = receipt else {
            return Ok(None);
        };
        if receipt.bytes.len() > crate::receipt::RECEIPT_BYTES_MAX
            || publication::receipt_digest_hex(&receipt.bytes) != receipt.digest
        {
            return Err(GatewayError::ReceiptDigestMismatch);
        }
        validate_identity(InputField::AuthorizationId, &receipt.key_id)
            .map_err(|_| GatewayError::InvalidPersistedState)?;
        Ok(Some(receipt))
    }

    pub(super) fn mark_finalized(&self, operation_id: &str) -> Result<(), GatewayError> {
        let changed = self
            .connection
            .execute(
                "UPDATE kubernetes_image_operations
                 SET state = ?1
                 WHERE operation_id = ?2 AND state = ?3
                       AND receipt_path IS NOT NULL AND receipt_digest IS NOT NULL
                       AND receipt_bytes IS NOT NULL AND receipt_key_id IS NOT NULL",
                params![
                    OperationState::Finalized.as_sql(),
                    operation_id,
                    OperationState::ReceiptWritten.as_sql(),
                ],
            )
            .map_err(GatewayError::Database)?;
        changed_one(changed)
    }

    #[cfg(test)]
    pub(super) fn receipt_reference(
        &self,
        operation_id: &str,
    ) -> Result<Option<ReceiptReference>, GatewayError> {
        self.connection
            .query_row(
                "SELECT receipt_path, receipt_digest
                 FROM kubernetes_image_operations
                 WHERE operation_id = ?1 AND state IN (?2, ?3)
                       AND receipt_path IS NOT NULL AND receipt_digest IS NOT NULL",
                params![
                    operation_id,
                    OperationState::ReceiptWritten.as_sql(),
                    OperationState::Finalized.as_sql(),
                ],
                |row| {
                    Ok(ReceiptReference {
                        path: PathBuf::from(row.get::<_, String>(0)?),
                        digest: row.get(1)?,
                    })
                },
            )
            .optional()
            .map_err(GatewayError::Database)
    }

    pub(super) fn next_request(
        &self,
        state: OperationState,
    ) -> Result<Option<SetDeploymentImageRequest>, GatewayError> {
        self.connection
            .query_row(
                "SELECT operation_id, namespace, deployment, container,
                        immutable_image_digest
                 FROM kubernetes_image_operations
                 WHERE state = ?1
                 ORDER BY CASE WHEN ?1 = 'authorized' THEN target_read_failures ELSE 0 END,
                          operation_id
                 LIMIT 1",
                [state.as_sql()],
                |row| {
                    Ok(SetDeploymentImageRequest {
                        operation_id: row.get(0)?,
                        namespace: row.get(1)?,
                        deployment: row.get(2)?,
                        container: row.get(3)?,
                        immutable_image_digest: row.get(4)?,
                    })
                },
            )
            .optional()
            .map_err(GatewayError::Database)
    }

    pub(super) fn request_in_state(
        &self,
        operation_id: &str,
        state: OperationState,
    ) -> Result<Option<SetDeploymentImageRequest>, GatewayError> {
        self.connection
            .query_row(
                "SELECT operation_id, namespace, deployment, container,
                        immutable_image_digest
                 FROM kubernetes_image_operations
                 WHERE operation_id = ?1 AND state = ?2",
                params![operation_id, state.as_sql()],
                |row| {
                    Ok(SetDeploymentImageRequest {
                        operation_id: row.get(0)?,
                        namespace: row.get(1)?,
                        deployment: row.get(2)?,
                        container: row.get(3)?,
                        immutable_image_digest: row.get(4)?,
                    })
                },
            )
            .optional()
            .map_err(GatewayError::Database)
    }

    pub(super) fn defer_target_retry(&self, operation_id: &str) -> Result<(), GatewayError> {
        let changed = self
            .connection
            .execute(
                "UPDATE kubernetes_image_operations
                 SET target_read_failures = target_read_failures + 1
                 WHERE operation_id = ?1 AND state = ?2
                       AND target_read_failures < 9223372036854775807",
                params![operation_id, OperationState::Authorized.as_sql()],
            )
            .map_err(GatewayError::Database)?;
        changed_one(changed)
    }

    pub(super) fn mark_not_attempted(
        &self,
        operation_id: &str,
        rejection: TargetRejection,
    ) -> Result<(), GatewayError> {
        let changed = self
            .connection
            .execute(
                "UPDATE kubernetes_image_operations
                 SET state = ?1, target_rejection = ?2, apply_attempted = 0
                 WHERE operation_id = ?3 AND state = ?4",
                params![
                    OperationState::NotAttempted.as_sql(),
                    rejection.as_sql(),
                    operation_id,
                    OperationState::Authorized.as_sql(),
                ],
            )
            .map_err(GatewayError::Database)?;
        changed_one(changed)
    }

    pub(super) fn mark_apply_started(
        &self,
        operation_id: &str,
        write_strategy: &str,
        target: &TargetIdentity,
    ) -> Result<(), GatewayError> {
        target.validate()?;
        let changed = self
            .connection
            .execute(
                "UPDATE kubernetes_image_operations
                 SET state = ?1, write_strategy = ?2, apply_attempted = 1,
                     target_uid = ?3, target_resource_version = ?4
                 WHERE operation_id = ?5 AND state = ?6",
                params![
                    OperationState::ApplyStarted.as_sql(),
                    write_strategy,
                    target.deployment_uid,
                    target.resource_version,
                    operation_id,
                    OperationState::Authorized.as_sql(),
                ],
            )
            .map_err(GatewayError::Database)?;
        changed_one(changed)
    }

    pub(super) fn record_apply_outcome(
        &self,
        operation_id: &str,
        outcome: &ApplyOutcome,
    ) -> Result<(), GatewayError> {
        outcome.validate()?;
        let target_uid = self
            .connection
            .query_row(
                "SELECT target_uid
                 FROM kubernetes_image_operations
                 WHERE operation_id = ?1 AND state = ?2 AND apply_attempted = 1",
                params![operation_id, OperationState::ApplyStarted.as_sql()],
                |row| row.get::<_, Option<String>>(0),
            )
            .map_err(GatewayError::Database)?;
        if target_uid.is_none()
            || outcome
                .deployment_uid
                .as_ref()
                .is_some_and(|uid| Some(uid) != target_uid.as_ref())
        {
            return Err(GatewayError::InvalidKubernetesFact);
        }
        let changed = self
            .connection
            .execute(
                "UPDATE kubernetes_image_operations
                 SET apply_accepted = ?1, requested_generation = ?2,
                     apply_resource_version = ?3
                 WHERE operation_id = ?4 AND state = ?5 AND apply_attempted = 1",
                params![
                    outcome.accepted,
                    outcome.requested_generation,
                    outcome.resource_version,
                    operation_id,
                    OperationState::ApplyStarted.as_sql(),
                ],
            )
            .map_err(GatewayError::Database)?;
        changed_one(changed)
    }

    pub(super) fn persisted_apply_outcome(
        &self,
        operation_id: &str,
    ) -> Result<ApplyOutcome, GatewayError> {
        self.connection
            .query_row(
                "SELECT apply_accepted, requested_generation, target_uid,
                        COALESCE(apply_resource_version, target_resource_version)
                 FROM kubernetes_image_operations
                 WHERE operation_id = ?1 AND state = ?2 AND apply_attempted = 1",
                params![operation_id, OperationState::ApplyStarted.as_sql()],
                |row| {
                    Ok(ApplyOutcome {
                        accepted: row.get::<_, Option<bool>>(0)?.unwrap_or(false),
                        requested_generation: row.get(1)?,
                        deployment_uid: row.get(2)?,
                        resource_version: row.get(3)?,
                    })
                },
            )
            .map_err(GatewayError::Database)
    }

    pub(super) fn freeze_observation(
        &self,
        request: &SetDeploymentImageRequest,
        outcome: &ApplyOutcome,
        observation: &ReceiverObservation,
    ) -> Result<(), GatewayError> {
        observation.validate()?;
        let result = observation.classify(request, outcome);
        let requested_generation = observation.requested_generation(request, outcome);
        let changed = self
            .connection
            .execute(
                "UPDATE kubernetes_image_operations
                 SET state = ?1, receiver_uid = ?2, receiver_image = ?3,
                     receiver_operation_marker = ?4, current_generation = ?5,
                     observed_generation = ?6, receiver_resource_version = ?7,
                     desired_replicas = ?8, updated_replicas = ?9,
                     available_replicas = ?10, unavailable_replicas = ?11,
                     result = ?12, requested_generation = ?13,
                     rollout_condition_type = ?14, rollout_condition_status = ?15,
                     rollout_condition_reason = ?16
                 WHERE operation_id = ?17 AND state = ?18",
                params![
                    OperationState::ReceiverObserved.as_sql(),
                    observation.deployment_uid,
                    observation.image,
                    observation.operation_marker,
                    observation.current_generation,
                    observation.observed_generation,
                    observation.resource_version,
                    observation.desired_replicas,
                    observation.updated_replicas,
                    observation.available_replicas,
                    observation.unavailable_replicas,
                    result.as_sql(),
                    requested_generation,
                    observation.rollout_condition_type,
                    observation.rollout_condition_status,
                    observation.rollout_condition_reason,
                    request.operation_id,
                    OperationState::ApplyStarted.as_sql(),
                ],
            )
            .map_err(GatewayError::Database)?;
        changed_one(changed)
    }
}

struct ReceiptRow {
    operation_id: String,
    authorization_id: Option<String>,
    authorization_signer_key_id: Option<String>,
    authorization_grant_digest: Option<String>,
    namespace: String,
    deployment: String,
    container: String,
    immutable_image_digest: String,
    write_strategy: Option<String>,
    target_uid: Option<String>,
    target_resource_version: Option<String>,
    receiver_uid: Option<String>,
    observed_image: Option<String>,
    observed_operation_marker: Option<String>,
    current_generation: Option<i64>,
    requested_generation: Option<i64>,
    observed_generation: Option<i64>,
    observed_resource_version: Option<String>,
    desired_replicas: Option<i32>,
    updated_replicas: Option<i32>,
    available_replicas: Option<i32>,
    unavailable_replicas: Option<i32>,
    rollout_condition_type: Option<String>,
    rollout_condition_status: Option<String>,
    rollout_condition_reason: Option<String>,
    result: String,
}

impl ReceiptRow {
    fn from_sql(row: &rusqlite::Row<'_>) -> rusqlite::Result<Self> {
        Ok(Self {
            operation_id: row.get(0)?,
            authorization_id: row.get(1)?,
            authorization_signer_key_id: row.get(2)?,
            authorization_grant_digest: row.get(3)?,
            namespace: row.get(4)?,
            deployment: row.get(5)?,
            container: row.get(6)?,
            immutable_image_digest: row.get(7)?,
            write_strategy: row.get(8)?,
            target_uid: row.get(9)?,
            target_resource_version: row.get(10)?,
            receiver_uid: row.get(11)?,
            observed_image: row.get(12)?,
            observed_operation_marker: row.get(13)?,
            current_generation: row.get(14)?,
            requested_generation: row.get(15)?,
            observed_generation: row.get(16)?,
            observed_resource_version: row.get(17)?,
            desired_replicas: row.get(18)?,
            updated_replicas: row.get(19)?,
            available_replicas: row.get(20)?,
            unavailable_replicas: row.get(21)?,
            rollout_condition_type: row.get(22)?,
            rollout_condition_status: row.get(23)?,
            rollout_condition_reason: row.get(24)?,
            result: row.get(25)?,
        })
    }

    fn into_statement(self) -> Result<ReceiptStatement, GatewayError> {
        Ok(ReceiptStatement {
            operation_id: self.operation_id,
            authorization_id: self
                .authorization_id
                .ok_or(GatewayError::InvalidPersistedState)?,
            authorization_signer_key_id: self
                .authorization_signer_key_id
                .ok_or(GatewayError::InvalidPersistedState)?,
            authorization_grant_digest: self
                .authorization_grant_digest
                .ok_or(GatewayError::InvalidPersistedState)?,
            namespace: self.namespace,
            deployment: self.deployment,
            container: self.container,
            immutable_image_digest: self.immutable_image_digest,
            write_strategy: self
                .write_strategy
                .ok_or(GatewayError::InvalidPersistedState)?,
            target_uid: self.target_uid.ok_or(GatewayError::InvalidPersistedState)?,
            target_resource_version: self
                .target_resource_version
                .ok_or(GatewayError::InvalidPersistedState)?,
            receiver_uid: self.receiver_uid,
            observed_image: self.observed_image,
            observed_operation_marker: self.observed_operation_marker,
            current_generation: self.current_generation,
            requested_generation: self.requested_generation,
            observed_generation: self.observed_generation,
            observed_resource_version: self.observed_resource_version,
            desired_replicas: self.desired_replicas,
            updated_replicas: self.updated_replicas,
            available_replicas: self.available_replicas,
            unavailable_replicas: self.unavailable_replicas,
            rollout_condition_type: self.rollout_condition_type,
            rollout_condition_status: self.rollout_condition_status,
            rollout_condition_reason: self.rollout_condition_reason,
            result: OperationResult::from_sql(&self.result)?,
        })
    }
}

fn migrate_receipt_schema(connection: &mut Connection) -> Result<(), GatewayError> {
    let transaction = connection.transaction().map_err(GatewayError::Database)?;
    let columns = {
        let mut statement = transaction
            .prepare("PRAGMA table_info(kubernetes_image_operations)")
            .map_err(GatewayError::Database)?;
        let rows = statement
            .query_map([], |row| row.get::<_, String>(1))
            .map_err(GatewayError::Database)?;
        rows.collect::<Result<Vec<_>, _>>()
            .map_err(GatewayError::Database)?
    };
    for (name, declaration) in [
        (
            "authorization_signer_key_id",
            "authorization_signer_key_id TEXT",
        ),
        (
            "authorization_grant_digest",
            "authorization_grant_digest TEXT",
        ),
        ("target_rejection", "target_rejection TEXT"),
        (
            "target_read_failures",
            "target_read_failures INTEGER NOT NULL DEFAULT 0",
        ),
        ("receipt_path", "receipt_path TEXT"),
        ("receipt_digest", "receipt_digest TEXT"),
        ("receipt_bytes", "receipt_bytes BLOB"),
        ("receipt_key_id", "receipt_key_id TEXT"),
        ("rollout_condition_type", "rollout_condition_type TEXT"),
        ("rollout_condition_status", "rollout_condition_status TEXT"),
        ("rollout_condition_reason", "rollout_condition_reason TEXT"),
    ] {
        if !columns.iter().any(|column| column == name) {
            transaction
                .execute(
                    &format!("ALTER TABLE kubernetes_image_operations ADD COLUMN {declaration}"),
                    [],
                )
                .map_err(GatewayError::Database)?;
        }
    }
    transaction
        .execute(
            "UPDATE kubernetes_image_operations
             SET requested_generation = current_generation
             WHERE requested_generation IS NULL
                   AND result IN ('SUCCEEDED', 'FAILED')
                   AND target_uid IS NOT NULL
                   AND receiver_uid = target_uid
                   AND receiver_image = immutable_image_digest
                   AND receiver_operation_marker = operation_id
                   AND current_generation IS NOT NULL
                   AND observed_generation >= current_generation",
            [],
        )
        .map_err(GatewayError::Database)?;
    transaction
        .execute(
            "UPDATE kubernetes_image_operations
             SET rollout_condition_type = CASE
                    WHEN progress_deadline_exceeded = 1 THEN 'Progressing'
                    WHEN available_condition = 1 THEN 'Available'
                    ELSE NULL
                 END,
                 rollout_condition_status = CASE
                    WHEN progress_deadline_exceeded = 1 THEN 'False'
                    WHEN available_condition = 1 THEN 'True'
                    ELSE NULL
                 END,
                 rollout_condition_reason = CASE
                    WHEN progress_deadline_exceeded = 1 THEN 'ProgressDeadlineExceeded'
                    ELSE NULL
                 END
             WHERE rollout_condition_type IS NULL
                   AND (progress_deadline_exceeded = 1 OR available_condition = 1)",
            [],
        )
        .map_err(GatewayError::Database)?;
    transaction.commit().map_err(GatewayError::Database)
}

fn open_private_file(path: &Path) -> io::Result<File> {
    let file = File::from(open(
        path,
        OFlags::CREATE | OFlags::RDWR | OFlags::CLOEXEC | OFlags::NOFOLLOW,
        Mode::RUSR | Mode::WUSR,
    )?);
    file.set_permissions(fs::Permissions::from_mode(0o600))?;
    let metadata = file.metadata()?;
    if !metadata.is_file()
        || metadata.uid() != rustix::process::geteuid().as_raw()
        || metadata.mode().trailing_zeros() < 6
    {
        return Err(io::Error::new(
            io::ErrorKind::PermissionDenied,
            "journal file is not owner-private",
        ));
    }
    Ok(file)
}

fn require_named_identity(path: &Path, expected: &fs::Metadata) -> io::Result<()> {
    let actual = fs::symlink_metadata(path)?;
    if !actual.is_file()
        || actual.dev() != expected.dev()
        || actual.ino() != expected.ino()
        || actual.uid() != expected.uid()
    {
        return Err(io::Error::new(
            io::ErrorKind::PermissionDenied,
            "journal file identity changed",
        ));
    }
    Ok(())
}

fn worker_lock_path(database_path: &Path) -> PathBuf {
    let mut lock_path = database_path.as_os_str().to_os_string();
    lock_path.push(".kap0038-worker.lock");
    PathBuf::from(lock_path)
}

fn changed_one(changed: usize) -> Result<(), GatewayError> {
    if changed == 1 {
        Ok(())
    } else {
        Err(GatewayError::InvalidTransition)
    }
}