kcode-k1-peering 0.2.0

Local signing boundary for Kennedy K1 subsystem transactions
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
use ed25519_dalek::{Signer, SigningKey};
use kcode_k1_txn_ordering::{K1TxnOrdering, SubsystemId, TxId};
use std::fs::{self, File, OpenOptions};
use std::io::{Read, Write};
use std::path::Path;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};

const IDENTITY_BYTES: usize = 64;
const PRIVATE_KEY_BYTES: usize = 32;
const PUBLIC_KEY_BYTES: usize = 32;

pub struct K1Peering {
    ordering: Arc<K1TxnOrdering>,
    signing_key: SigningKey,
    public_key: [u8; PUBLIC_KEY_BYTES],
}

impl K1Peering {
    pub fn open(root: &Path, ordering: Arc<K1TxnOrdering>) -> Result<Self, String> {
        prepare_root(root)?;
        let identity_path = root.join("identity.key");

        let (signing_key, public_key) = match fs::symlink_metadata(&identity_path) {
            Ok(metadata) => load_identity(&identity_path, metadata)?,
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
                if ordering.tip().is_some() {
                    return Err(
                        "identity.key is missing while canonical transaction history exists"
                            .to_owned(),
                    );
                }
                create_identity(&identity_path)?
            }
            Err(error) => return Err(format!("cannot inspect identity.key: {error}")),
        };

        Ok(Self {
            ordering,
            signing_key,
            public_key,
        })
    }

    pub fn submit_txn(&self, subsystem: SubsystemId, payload: &[u8]) -> Result<TxId, String> {
        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map_err(|_| "system clock is before the Unix epoch".to_owned())?
            .as_secs();

        self.ordering
            .submit_local_txn(
                timestamp,
                self.public_key,
                subsystem,
                payload,
                |bytes| Ok(self.signing_key.sign(bytes).to_bytes()),
                |_| Ok(()),
            )
            .map(|(id, _)| id)
    }
}

fn prepare_root(root: &Path) -> Result<(), String> {
    match fs::symlink_metadata(root) {
        Ok(metadata) if metadata.file_type().is_dir() => return Ok(()),
        Ok(_) => return Err("peering root is not a real directory".to_owned()),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
        Err(error) => return Err(format!("cannot inspect peering root: {error}")),
    }

    fs::create_dir_all(root).map_err(|error| format!("cannot create peering root: {error}"))?;

    let metadata = fs::symlink_metadata(root)
        .map_err(|error| format!("cannot inspect created peering root: {error}"))?;

    if !metadata.file_type().is_dir() {
        return Err("created peering root is not a real directory".to_owned());
    }

    Ok(())
}

fn load_identity(
    path: &Path,
    metadata: fs::Metadata,
) -> Result<(SigningKey, [u8; PUBLIC_KEY_BYTES]), String> {
    if !metadata.file_type().is_file() {
        return Err("identity.key is not a real regular file".to_owned());
    }

    if metadata.len() != IDENTITY_BYTES as u64 {
        return Err("identity.key must contain exactly 64 bytes".to_owned());
    }

    validate_identity_permissions(&metadata)?;

    let mut file =
        File::open(path).map_err(|error| format!("cannot open identity.key: {error}"))?;
    let mut bytes = [0_u8; IDENTITY_BYTES];
    file.read_exact(&mut bytes)
        .map_err(|error| format!("cannot read identity.key: {error}"))?;

    let private_seed: [u8; PRIVATE_KEY_BYTES] = bytes[..PRIVATE_KEY_BYTES]
        .try_into()
        .expect("fixed private-key range");
    let stored_public_key: [u8; PUBLIC_KEY_BYTES] = bytes[PRIVATE_KEY_BYTES..]
        .try_into()
        .expect("fixed public-key range");
    let signing_key = SigningKey::from_bytes(&private_seed);
    let derived_public_key = signing_key.verifying_key().to_bytes();

    if stored_public_key != derived_public_key {
        return Err("identity.key public key does not match its private seed".to_owned());
    }

    Ok((signing_key, derived_public_key))
}

fn create_identity(path: &Path) -> Result<(SigningKey, [u8; PUBLIC_KEY_BYTES]), String> {
    let mut private_seed = [0_u8; PRIVATE_KEY_BYTES];
    getrandom::fill(&mut private_seed)
        .map_err(|error| format!("cannot generate peering identity: {error}"))?;

    let signing_key = SigningKey::from_bytes(&private_seed);
    let public_key = signing_key.verifying_key().to_bytes();
    let mut bytes = [0_u8; IDENTITY_BYTES];
    bytes[..PRIVATE_KEY_BYTES].copy_from_slice(&private_seed);
    bytes[PRIVATE_KEY_BYTES..].copy_from_slice(&public_key);

    let mut options = OpenOptions::new();
    options.write(true).create_new(true);
    set_owner_only_creation_mode(&mut options);

    let mut file = options
        .open(path)
        .map_err(|error| format!("cannot create identity.key: {error}"))?;
    file.write_all(&bytes)
        .map_err(|error| format!("cannot write identity.key: {error}"))?;
    file.sync_all()
        .map_err(|error| format!("cannot synchronize identity.key: {error}"))?;

    Ok((signing_key, public_key))
}

#[cfg(unix)]
fn set_owner_only_creation_mode(options: &mut OpenOptions) {
    use std::os::unix::fs::OpenOptionsExt;
    options.mode(0o600);
}

#[cfg(not(unix))]
fn set_owner_only_creation_mode(_: &mut OpenOptions) {}

#[cfg(unix)]
fn validate_identity_permissions(metadata: &fs::Metadata) -> Result<(), String> {
    use std::os::unix::fs::PermissionsExt;

    if metadata.permissions().mode() & 0o077 != 0 {
        return Err("identity.key grants group or other permissions".to_owned());
    }

    Ok(())
}

#[cfg(not(unix))]
fn validate_identity_permissions(_: &fs::Metadata) -> Result<(), String> {
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use ed25519_dalek::Signature;
    use kcode_k1_txn_ordering::{GENESIS_PARENT, Subsystem};
    use std::path::PathBuf;
    use std::sync::Mutex;
    use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering};

    static NEXT_ROOT: AtomicU64 = AtomicU64::new(0);

    struct TempRoots {
        base: PathBuf,
    }

    impl TempRoots {
        fn new(label: &str) -> Self {
            let sequence = NEXT_ROOT.fetch_add(1, Ordering::Relaxed);
            let base = std::env::temp_dir().join(format!(
                "kcode-k1-peering-{}-{}-{}",
                std::process::id(),
                sequence,
                label
            ));
            let _ = fs::remove_dir_all(&base);
            Self { base }
        }

        fn peering(&self) -> PathBuf {
            self.base.join("peering")
        }

        fn ordering(&self) -> PathBuf {
            self.base.join("ordering")
        }
    }

    impl Drop for TempRoots {
        fn drop(&mut self) {
            let _ = fs::remove_dir_all(&self.base);
        }
    }

    struct RecordingSubsystem {
        submissions: Mutex<Vec<(TxId, Vec<u8>)>>,
        fail: AtomicBool,
        reorgs: AtomicUsize,
    }

    impl RecordingSubsystem {
        fn new() -> Self {
            Self {
                submissions: Mutex::new(Vec::new()),
                fail: AtomicBool::new(false),
                reorgs: AtomicUsize::new(0),
            }
        }

        fn entries(&self) -> Vec<(TxId, Vec<u8>)> {
            self.submissions.lock().unwrap().clone()
        }
    }

    impl Subsystem for RecordingSubsystem {
        fn submit_txn(&self, id: TxId, payload: &[u8]) -> Result<(), String> {
            self.submissions
                .lock()
                .unwrap()
                .push((id, payload.to_vec()));

            if self.fail.load(Ordering::Relaxed) {
                Err("integration failed".to_owned())
            } else {
                Ok(())
            }
        }

        fn reorg(&self) -> Result<(), String> {
            self.reorgs.fetch_add(1, Ordering::Relaxed);
            Ok(())
        }
    }

    fn subsystem(value: u8) -> SubsystemId {
        SubsystemId::from_bytes([value; 20]).unwrap()
    }

    fn open_ordering(roots: &TempRoots) -> Arc<K1TxnOrdering> {
        Arc::new(K1TxnOrdering::open(&roots.ordering()).unwrap())
    }

    fn write_identity(path: &Path, bytes: &[u8]) {
        fs::create_dir_all(path.parent().unwrap()).unwrap();
        let mut options = OpenOptions::new();
        options.write(true).create_new(true);
        set_owner_only_creation_mode(&mut options);
        let mut file = options.open(path).unwrap();
        file.write_all(bytes).unwrap();
        file.sync_all().unwrap();
    }

    #[test]
    fn creates_owner_only_identity_and_reopens_stably() {
        let roots = TempRoots::new("identity");
        let ordering = open_ordering(&roots);
        let first = K1Peering::open(&roots.peering(), ordering.clone()).unwrap();
        let first_bytes = fs::read(roots.peering().join("identity.key")).unwrap();

        assert_eq!(first_bytes.len(), IDENTITY_BYTES);
        assert_eq!(&first_bytes[PRIVATE_KEY_BYTES..], &first.public_key);

        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let mode = fs::metadata(roots.peering().join("identity.key"))
                .unwrap()
                .permissions()
                .mode();
            assert_eq!(mode & 0o077, 0);
        }

        drop(first);
        let second = K1Peering::open(&roots.peering(), ordering).unwrap();
        let second_bytes = fs::read(roots.peering().join("identity.key")).unwrap();

        assert_eq!(second_bytes, first_bytes);
        assert_eq!(second.public_key, first_bytes[PRIVATE_KEY_BYTES..]);
    }

    #[test]
    fn submits_exact_signed_transaction_and_delivers_once() {
        let roots = TempRoots::new("signed");
        let ordering = open_ordering(&roots);
        let target = subsystem(b'a');
        let handler = Arc::new(RecordingSubsystem::new());
        ordering
            .register_subsystem(target, None, handler.clone())
            .unwrap();
        let peering = K1Peering::open(&roots.peering(), ordering.clone()).unwrap();

        let returned_id = peering.submit_txn(target, b"object update").unwrap();

        let entries = handler.entries();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].1, b"object update");
        assert_eq!(returned_id, entries[0].0);
        assert_eq!(ordering.tip(), Some(returned_id));

        let bytes = ordering.get_txn(returned_id).unwrap().unwrap();
        assert_eq!(TxId::for_transaction(&bytes), returned_id);
        assert_eq!(&bytes[..12], GENESIS_PARENT.as_bytes());
        assert_eq!(&bytes[20..52], &peering.public_key);
        assert_eq!(&bytes[52..72], target.as_bytes());
        assert_eq!(&bytes[72..bytes.len() - 64], b"object update");

        let signature_bytes: &[u8; 64] = bytes[bytes.len() - 64..].try_into().unwrap();
        let signature = Signature::from_bytes(signature_bytes);
        peering
            .signing_key
            .verifying_key()
            .verify_strict(&bytes[..bytes.len() - 64], &signature)
            .unwrap();
    }

    #[test]
    fn concurrent_submissions_form_one_linear_callback_sequence() {
        let roots = TempRoots::new("concurrent");
        let ordering = open_ordering(&roots);
        let target = subsystem(b'b');
        let handler = Arc::new(RecordingSubsystem::new());
        ordering
            .register_subsystem(target, None, handler.clone())
            .unwrap();
        let peering = Arc::new(K1Peering::open(&roots.peering(), ordering.clone()).unwrap());

        let threads: Vec<_> = (0_u8..12)
            .map(|value| {
                let peering = peering.clone();
                std::thread::spawn(move || peering.submit_txn(target, &[value]).unwrap())
            })
            .collect();

        for thread in threads {
            thread.join().unwrap();
        }

        let entries = handler.entries();
        assert_eq!(entries.len(), 12);

        let mut expected_parent = GENESIS_PARENT;
        for (id, _) in &entries {
            let bytes = ordering.get_txn(*id).unwrap().unwrap();
            assert_eq!(&bytes[..12], expected_parent.as_bytes());
            expected_parent = *id;
        }

        assert_eq!(ordering.tip(), Some(expected_parent));
    }

    #[test]
    fn rejects_unregistered_target_before_mutation() {
        let roots = TempRoots::new("unregistered");
        let ordering = open_ordering(&roots);
        let peering = K1Peering::open(&roots.peering(), ordering.clone()).unwrap();

        assert!(
            peering
                .submit_txn(subsystem(b'c'), b"not committed")
                .is_err()
        );
        assert_eq!(ordering.tip(), None);
    }

    #[test]
    fn callback_failure_is_reported_as_committed_without_retry() {
        let roots = TempRoots::new("callback-failure");
        let ordering = open_ordering(&roots);
        let target = subsystem(b'd');
        let handler = Arc::new(RecordingSubsystem::new());
        handler.fail.store(true, Ordering::Relaxed);
        ordering
            .register_subsystem(target, None, handler.clone())
            .unwrap();
        let peering = K1Peering::open(&roots.peering(), ordering.clone()).unwrap();

        let error = peering.submit_txn(target, b"committed once").unwrap_err();

        assert!(error.contains("committed"));
        assert!(ordering.tip().is_some());
        assert_eq!(handler.entries().len(), 1);
    }

    #[test]
    fn restart_and_checkpoint_replay_deliver_only_newer_updates() {
        let roots = TempRoots::new("replay");
        let ordering = open_ordering(&roots);
        let target = subsystem(b'e');
        let first_handler = Arc::new(RecordingSubsystem::new());
        ordering
            .register_subsystem(target, None, first_handler.clone())
            .unwrap();
        let peering = K1Peering::open(&roots.peering(), ordering.clone()).unwrap();

        peering.submit_txn(target, b"first").unwrap();
        peering.submit_txn(target, b"second").unwrap();
        let original = first_handler.entries();
        assert_eq!(original.len(), 2);

        drop(peering);
        drop(first_handler);
        drop(ordering);

        let reopened = open_ordering(&roots);
        let reopened_peering = K1Peering::open(&roots.peering(), reopened.clone()).unwrap();
        let replay_handler = Arc::new(RecordingSubsystem::new());
        reopened
            .register_subsystem(target, Some(original[0].0), replay_handler.clone())
            .unwrap();

        assert_eq!(
            replay_handler.entries(),
            vec![(original[1].0, b"second".to_vec())]
        );
        reopened_peering.submit_txn(target, b"third").unwrap();
        assert_eq!(replay_handler.entries().len(), 2);
    }

    #[test]
    fn rejects_truncated_and_mismatched_identity_material() {
        let truncated = TempRoots::new("truncated");
        let ordering = open_ordering(&truncated);
        write_identity(&truncated.peering().join("identity.key"), &[1_u8; 63]);
        assert!(K1Peering::open(&truncated.peering(), ordering).is_err());

        let mismatched = TempRoots::new("mismatched");
        let ordering = open_ordering(&mismatched);
        write_identity(
            &mismatched.peering().join("identity.key"),
            &[0_u8; IDENTITY_BYTES],
        );
        assert!(K1Peering::open(&mismatched.peering(), ordering).is_err());
    }

    #[cfg(unix)]
    #[test]
    fn rejects_over_permissive_and_symbolic_link_identity_files() {
        use std::os::unix::fs::{PermissionsExt, symlink};

        let permissive = TempRoots::new("permissive");
        let ordering = open_ordering(&permissive);
        let identity = permissive.peering().join("identity.key");
        let signing_key = SigningKey::from_bytes(&[7_u8; PRIVATE_KEY_BYTES]);
        let mut bytes = [0_u8; IDENTITY_BYTES];
        bytes[..PRIVATE_KEY_BYTES].fill(7);
        bytes[PRIVATE_KEY_BYTES..].copy_from_slice(&signing_key.verifying_key().to_bytes());
        write_identity(&identity, &bytes);
        fs::set_permissions(&identity, fs::Permissions::from_mode(0o644)).unwrap();
        assert!(K1Peering::open(&permissive.peering(), ordering).is_err());

        let linked = TempRoots::new("linked-identity");
        let ordering = open_ordering(&linked);
        fs::create_dir_all(linked.peering()).unwrap();
        let target = linked.base.join("identity-target");
        write_identity(&target, &bytes);
        symlink(&target, linked.peering().join("identity.key")).unwrap();
        assert!(K1Peering::open(&linked.peering(), ordering).is_err());
    }

    #[cfg(unix)]
    #[test]
    fn rejects_symbolic_link_peering_root() {
        use std::os::unix::fs::symlink;

        let roots = TempRoots::new("linked-root");
        let ordering = open_ordering(&roots);
        let actual = roots.base.join("actual-peering");
        fs::create_dir_all(&actual).unwrap();
        let linked = roots.base.join("linked-peering");
        symlink(&actual, &linked).unwrap();

        assert!(K1Peering::open(&linked, ordering).is_err());
    }

    #[test]
    fn missing_identity_with_canonical_history_fails_closed() {
        let roots = TempRoots::new("missing-with-history");
        let ordering = open_ordering(&roots);
        let target = subsystem(b'f');
        ordering
            .register_subsystem(target, None, Arc::new(RecordingSubsystem::new()))
            .unwrap();
        let peering = K1Peering::open(&roots.peering(), ordering.clone()).unwrap();
        peering.submit_txn(target, b"durable").unwrap();

        drop(peering);
        drop(ordering);
        fs::remove_file(roots.peering().join("identity.key")).unwrap();

        let reopened = open_ordering(&roots);
        assert!(K1Peering::open(&roots.peering(), reopened).is_err());
        assert!(!roots.peering().join("identity.key").exists());
    }
}