osdns 0.2.0

Safe, transactional control of operating-system DNS configuration
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
//! Destructive race tests: external actors changing DNS state mid-transaction
//! or mid-lease, including the DHCP delete/create replacement pattern that
//! replaces files instead of writing them in place.

#![cfg(feature = "test-util")]

use rstest::rstest;

mod common;

use common::*;
use osdns::testing::{CrashOutcome, FakeState, FaultInjector, TxPoint};
use osdns::{Error, Lease, RecoveryOutcome};

#[test]
fn external_change_during_capture_window_fails_transaction() {
    let fixture = new_fixture("race-capture");
    let injector = FaultInjector::new();
    injector.fail_at(TxPoint::AfterCapture, "simulated read-back hiccup");
    fixture.manager.install_fault_injector(injector.clone());

    let error = fixture
        .manager
        .apply(&iface_config(1, "1.1.1.1"))
        .unwrap_err();
    injector.clear();
    assert!(matches!(error, Error::Platform { .. }));
    assert_eq!(
        fixture.fake.current_state(IFACE1).unwrap(),
        Some(FakeState::Empty)
    );
    assert!(journal_files(&fixture.dir).is_empty());
}

#[test]
fn unconditional_backend_rechecks_state_before_mutation() {
    let dir = temp_dir("race-unconditional-preflight");
    let fake = FakeDns::unconditional();
    let manager = manager_for_testing(
        "io.osdns.test",
        &dir,
        &fake,
        std::time::Duration::from_secs(30),
    )
    .unwrap();
    fake.inject_external_before_unconditional_readback(IFACE1, state_with("9.9.9.9"))
        .unwrap();

    assert!(matches!(
        manager.apply(&iface_config(1, "1.1.1.1")).unwrap_err(),
        Error::ExternalModification { .. }
    ));
    assert_eq!(
        fake.current_state(IFACE1).unwrap(),
        Some(state_with("9.9.9.9"))
    );
}

#[test]
fn external_change_between_apply_and_readback_is_rolled_back() {
    let fixture = new_fixture("race-apply");
    let injector = FaultInjector::new();
    injector.fail_at(TxPoint::AfterApply, "read-back unavailable");
    fixture.manager.install_fault_injector(injector.clone());

    let error = fixture
        .manager
        .apply(&iface_config(1, "1.1.1.1"))
        .unwrap_err();
    injector.clear();
    assert!(matches!(error, Error::Platform { .. }));
    assert_eq!(
        fixture.fake.current_state(IFACE1).unwrap(),
        Some(FakeState::Empty),
        "a failed transaction must roll back to the captured base"
    );
    assert!(journal_files(&fixture.dir).is_empty());
}

#[test]
fn delete_then_recreate_requires_a_fresh_lease() {
    let fixture = new_fixture("race-delete-recreate");
    let lease = fixture.manager.apply(&iface_config(1, "1.1.1.1")).unwrap();

    assert!(fixture.fake.external_remove(IFACE1).unwrap());
    assert!(matches!(
        lease.update(&iface_config(1, "8.8.8.8")).unwrap_err(),
        Error::ResourceGone { .. }
    ));

    let _ = fixture.fake.external_change(IFACE1, state_with("9.9.9.9"));
    lease.restore().unwrap();
    assert_eq!(
        fixture.fake.current_state(IFACE1).unwrap(),
        Some(state_with("9.9.9.9"))
    );
}

#[test]
fn incarnation_replaced_between_bound_observation_and_apply_is_not_mutated() {
    let fixture = new_fixture("race-incarnation-before-apply");
    fixture
        .fake
        .replace_before_next_apply(IFACE1, state_with("9.9.9.9"))
        .unwrap();
    assert!(fixture.manager.apply(&iface_config(1, "1.1.1.1")).is_err());
    assert_eq!(
        fixture.fake.current_state(IFACE1).unwrap(),
        Some(state_with("9.9.9.9"))
    );
}

#[test]
fn incarnation_replaced_between_identity_and_snapshot_is_rejected() {
    let fixture = new_fixture("race-incarnation-during-observe");
    fixture
        .fake
        .replace_during_next_observe(IFACE1, state_with("9.9.9.9"))
        .unwrap();
    assert!(fixture.manager.apply(&iface_config(1, "1.1.1.1")).is_err());
    assert_eq!(
        fixture.fake.current_state(IFACE1).unwrap(),
        Some(state_with("9.9.9.9"))
    );
    assert!(journal_files(&fixture.dir).is_empty());
}

#[test]
fn incarnation_replaced_during_update_is_not_mutated() {
    let fixture = new_fixture("race-incarnation-update");
    let lease = fixture.manager.apply(&iface_config(1, "1.1.1.1")).unwrap();
    fixture
        .fake
        .replace_before_next_apply(IFACE1, state_with("9.9.9.9"))
        .unwrap();
    assert!(lease.update(&iface_config(1, "8.8.8.8")).is_err());
    assert_eq!(
        fixture.fake.current_state(IFACE1).unwrap(),
        Some(state_with("9.9.9.9"))
    );
    lease.abandon().unwrap();
}

#[test]
fn external_write_between_two_leases_is_never_overwritten() {
    let fixture = new_fixture("race-two-leases");
    let first = fixture.manager.apply(&iface_config(1, "1.1.1.1")).unwrap();
    first.restore().unwrap();

    let second = fixture.manager.apply(&iface_config(1, "8.8.8.8")).unwrap();
    fixture
        .fake
        .external_change(IFACE1, state_with("9.9.9.9"))
        .unwrap();
    let failure = second.restore().unwrap_err();
    assert!(
        failure.error.is_external_modification(),
        "{:?}",
        failure.error
    );
    let second = failure.lease;

    let outcomes = fixture.manager.recover_stale().unwrap();
    assert!(
        outcomes
            .iter()
            .any(|o| matches!(o, RecoveryOutcome::Busy { .. }))
    );
    second.abandon().unwrap();
    assert_eq!(
        fixture.fake.current_state(IFACE1).unwrap(),
        Some(state_with("9.9.9.9"))
    );
}

/// Crash safety per phase: a crash before any mutation leaves a `Prepared`
/// record over untouched state (cleared); a crash once the mutation may
/// have run leaves ambiguous state (an external actor could independently
/// have produced the same desired state), so recovery reports a conflict
/// and mutates nothing instead of overwriting possibly-external state.
#[rstest]
#[case(TxPoint::AfterPrepared, true)]
#[case(TxPoint::AfterApply, false)]
#[case(TxPoint::AfterReadback, false)]
#[case(TxPoint::AfterVerify, false)]
fn crash_at_every_mutation_phase_leaves_recoverable_state(
    #[case] point: TxPoint,
    #[case] expect_cleared: bool,
) {
    use osdns::RecoveryOutcome;
    let fixture = new_fixture("race-crash-phases");
    let injector = FaultInjector::new();
    injector.crash_at(point);
    fixture.manager.install_fault_injector(injector.clone());

    let outcome =
        osdns::testing::catch_crash(|| fixture.manager.apply(&iface_config(1, "1.1.1.1")));
    injector.clear();
    assert!(matches!(outcome, CrashOutcome::Crashed));

    let outcomes = fixture.manager.recover_stale().unwrap();
    assert_eq!(outcomes.len(), 1, "{outcomes:?}");
    if expect_cleared {
        assert!(
            matches!(&outcomes[0], RecoveryOutcome::JournalCleared { .. }),
            "{outcomes:?}"
        );
        assert!(journal_files(&fixture.dir).is_empty(), "{outcomes:?}");
        assert_eq!(
            fixture.fake.current_state(IFACE1).unwrap(),
            Some(FakeState::Empty),
            "a pre-mutation crash must clear the journal without touching the system"
        );
    } else {
        // Ambiguous: the mutation may have run, but `current == desired`
        // alone never proves we applied it.
        assert!(
            matches!(&outcomes[0], RecoveryOutcome::ExternalConflict { .. }),
            "{outcomes:?}"
        );
        assert_eq!(
            journal_files(&fixture.dir).len(),
            1,
            "ambiguous journals are kept for forensics: {outcomes:?}"
        );
        assert_eq!(
            fixture.fake.current_state(IFACE1).unwrap(),
            Some(state_with("1.1.1.1")),
            "recovery must not overwrite ambiguous state"
        );
        // The claim can still be explicitly abandoned.
        fixture
            .manager
            .abandon_journal(&resource_id(IFACE1))
            .unwrap();
        assert!(journal_files(&fixture.dir).is_empty());
    }
}

#[test]
fn lease_keeps_working_after_recover_attempt_on_live_resource() {
    let fixture = new_fixture("race-live-recover");
    let lease: Lease = fixture.manager.apply(&iface_config(1, "1.1.1.1")).unwrap();
    let outcomes = fixture.manager.recover_stale().unwrap();
    assert!(matches!(&outcomes[0], RecoveryOutcome::Busy { .. }));
    lease.restore().unwrap();
    assert_eq!(
        fixture.fake.current_state(IFACE1).unwrap(),
        Some(FakeState::Empty)
    );
}

#[test]
fn restore_refuses_concurrent_external_change() {
    let fixture = new_fixture("race-restore-guard");
    let lease = fixture.manager.apply(&iface_config(1, "1.1.1.1")).unwrap();

    fixture
        .fake
        .external_change(IFACE1, state_with("9.9.9.9"))
        .unwrap();
    let failure = lease.restore().unwrap_err();
    assert!(failure.error.is_external_modification());
    assert_eq!(
        fixture.fake.current_state(IFACE1).unwrap(),
        Some(state_with("9.9.9.9")),
        "the concurrent change must survive"
    );
    failure.lease.abandon().unwrap();
}

#[test]
fn update_refuses_concurrent_external_change() {
    let fixture = new_fixture("race-update-guard");
    let lease = fixture.manager.apply(&iface_config(1, "1.1.1.1")).unwrap();
    fixture
        .fake
        .external_change(IFACE1, state_with("9.9.9.9"))
        .unwrap();
    let err = lease.update(&iface_config(1, "8.8.8.8")).unwrap_err();
    assert!(err.is_external_modification(), "{err:?}");
    assert_eq!(
        fixture.fake.current_state(IFACE1).unwrap(),
        Some(state_with("9.9.9.9")),
        "the concurrent change must survive the refused update"
    );
    lease.abandon().unwrap();
}

#[test]
fn cas_rejection_does_not_rollback_over_external_state() {
    let fixture = new_fixture("race-cas-reject");
    fixture
        .fake
        .inject_external_before_guarded(IFACE1, state_with("9.9.9.9"))
        .unwrap();
    let err = fixture
        .manager
        .apply(&iface_config(1, "1.1.1.1"))
        .unwrap_err();
    assert!(err.is_external_modification(), "{err:?}");
    assert_eq!(
        fixture.fake.current_state(IFACE1).unwrap(),
        Some(state_with("9.9.9.9")),
        "a CAS rejection must leave the external state untouched"
    );
}

#[test]
fn partial_mutation_then_external_change_is_not_rolled_over() {
    let fixture = new_fixture("race-partial-then-external");
    fixture.fake.inject_partial_apply_failure(1);
    fixture
        .fake
        .inject_external_after_guarded_mutation(IFACE1, state_with("9.9.9.9"))
        .unwrap();
    let err = fixture
        .manager
        .apply(&iface_config(1, "1.1.1.1"))
        .unwrap_err();
    assert!(matches!(err, Error::Platform { .. }), "{err:?}");
    assert_eq!(
        fixture.fake.current_state(IFACE1).unwrap(),
        Some(state_with("9.9.9.9")),
        "rollback must not overwrite an external write that landed after our partial mutation"
    );
}

#[test]
fn indeterminate_apply_without_proof_does_not_rollback() {
    let fixture = new_fixture("race-indeterminate-no-proof");
    fixture.fake.inject_backend_failure(
        osdns::testing::FakeOp::Apply,
        1,
        "apply failed before mutating",
    );
    let err = fixture
        .manager
        .apply(&iface_config(1, "1.1.1.1"))
        .unwrap_err();
    assert!(matches!(err, Error::Platform { .. }), "{err:?}");
    assert_eq!(
        fixture.fake.current_state(IFACE1).unwrap(),
        Some(FakeState::Empty)
    );
}

#[test]
fn same_dns_generation_bump_is_not_ours_on_update_or_restore() {
    let fixture = new_fixture("race-same-dns-gen");
    let lease = fixture.manager.apply(&iface_config(1, "1.1.1.1")).unwrap();
    let ours = fixture.fake.generation(IFACE1).unwrap().unwrap();
    fixture
        .fake
        .external_change(IFACE1, state_with("1.1.1.1"))
        .unwrap();
    let external = fixture.fake.generation(IFACE1).unwrap().unwrap();
    assert!(external > ours);

    let err = lease.update(&iface_config(1, "8.8.8.8")).unwrap_err();
    assert!(err.is_external_modification(), "{err:?}");
    assert_eq!(fixture.fake.generation(IFACE1).unwrap(), Some(external));
    assert_eq!(
        journal_record_json(&fixture.dir)["applied"]["data"]["generation"],
        serde_json::json!(ours)
    );

    let failure = lease.restore().unwrap_err();
    assert!(
        failure.error.is_external_modification(),
        "{:?}",
        failure.error
    );
    assert_eq!(fixture.fake.generation(IFACE1).unwrap(), Some(external));
    failure.lease.abandon().unwrap();
}

#[test]
fn mutation_proof_is_not_replaced_by_equivalent_readback() {
    let fixture = new_fixture("race-readback-identity");
    fixture
        .fake
        .inject_external_after_guarded_mutation(IFACE1, state_with("1.1.1.1"))
        .unwrap();
    let err = fixture
        .manager
        .apply(&iface_config(1, "1.1.1.1"))
        .unwrap_err();
    assert!(err.is_external_modification(), "{err:?}");
    assert_eq!(
        fixture.fake.current_state(IFACE1).unwrap(),
        Some(state_with("1.1.1.1"))
    );
    if journal_files(&fixture.dir).is_empty() {
        return;
    }
    let record = journal_record_json(&fixture.dir);
    assert!(
        record["applied"].is_null()
            || record["applied"]["data"]["generation"]
                != serde_json::json!(fixture.fake.generation(IFACE1).unwrap().unwrap())
    );
    fixture
        .manager
        .abandon_journal(&resource_id(IFACE1))
        .unwrap();
}

#[test]
fn same_dns_rewrite_blocks_rollback_of_earlier_resource() {
    let fixture = new_multi_fixture("race-rollback-same-dns");
    let lease = fixture
        .manager
        .apply(&routing_config(&["corp.example"]))
        .unwrap();
    fixture
        .fake
        .inject_external_before_nth_guarded(1, IFACE1, state_with("8.8.8.8"))
        .unwrap();
    fixture.fake.inject_backend_failure_after(
        osdns::testing::FakeOp::Apply,
        1,
        1,
        "later resource apply failed",
    );
    let updated = osdns::DnsConfig::builder(iface_scope(1))
        .nameserver(ip("8.8.8.8"))
        .routing_domain("corp.example")
        .build()
        .unwrap();
    let err = lease.update(&updated).unwrap_err();
    assert!(
        matches!(err, Error::Platform { .. }) || err.is_external_modification(),
        "{err:?}"
    );
    let generation = fixture.fake.generation(IFACE1).unwrap().unwrap();
    assert_eq!(
        fixture.fake.current_state(IFACE1).unwrap(),
        Some(state_with("8.8.8.8"))
    );
    let retry = lease.update(&updated);
    assert!(retry.is_err(), "{retry:?}");
    assert_eq!(fixture.fake.generation(IFACE1).unwrap(), Some(generation));
    lease.abandon().unwrap();
}