vta-service 0.9.8

Service for Verifiable Trust Agents operating in Verifiable Trust Communities
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
//! `rollback_didcomm` operation — fail-forward dispatch.
//!
//! Spec: `docs/05-design-notes/runtime-service-management.md` §3.5a.
//!
//! Reads the per-kind snapshot store for `didcomm` and dispatches
//! into the equivalent forward operation that returns the VTA's
//! DIDComm service entry to the pre-mutation state. WebVH is
//! append-only; rollback never rewinds the chain — it appends a
//! new LogEntry plus, where the prior state involved an active
//! mediator, drains the just-departed mediator with the supplied
//! TTL.
//!
//! Dispatch table:
//!
//! | snapshot              | current state         | dispatched op            |
//! |-----------------------|-----------------------|--------------------------|
//! | `Disabled`            | enabled (mediator Y)  | `disable_didcomm`        |
//! | `Enabled { X }`       | disabled              | `enable_didcomm` with X  |
//! | `Enabled { X }`       | enabled (Y), X != Y   | `update_didcomm` X→Y     |
//! | `Disabled`            | disabled              | no-op (snapshot≡current) |
//! | `Enabled { X }`       | enabled (X)           | no-op (snapshot≡current) |
//!
//! The `update_didcomm` arm is the most common case in practice:
//! rolling back a `services didcomm update A→B` re-promotes A
//! and drains B. Spec §3.5a explicitly notes that drain is
//! re-fed via the same forward path, so the new drain entry uses
//! the operator-supplied `drain_ttl` (default 24h).
//!
//! Brick-prevention is enforced by the dispatched forward op
//! (the `disable_didcomm` arm goes through
//! `would_violate_last_service`). Rolling back a `services
//! didcomm enable` when REST is also off surfaces
//! `LastServiceRefused` verbatim per §7a.5.

use std::sync::Arc;
use std::time::Duration;

use thiserror::Error;
use tokio::sync::RwLock;
use tracing::info;

use crate::auth::AuthClaims;
use crate::config::AppConfig;
use crate::error::AppError;
use crate::messaging::handshake::{HandshakeError, ListenerProver};
use crate::operations::did_webvh::UpdateDidWebvhError;
use crate::operations::protocol::disable_didcomm::{
    DisableDidcommError, DisableDidcommParams, DisableTransport, disable_didcomm,
};
use crate::operations::protocol::document::{DocumentPatchError, current_didcomm_service};
use crate::operations::protocol::enable_didcomm::{
    EnableDidcommError, EnableDidcommParams, enable_didcomm,
};
use crate::operations::protocol::snapshot::{
    self, DidcommSnapshot, ServiceConfigSnapshot, ServiceKind,
};
use crate::operations::protocol::update_didcomm::{
    MigrateAuditKind, UpdateDidcommError, UpdateDidcommParams, update_didcomm,
};
use crate::operations::protocol::{OpContext, ServiceOpDeps};
use crate::store::KeyspaceHandle;

/// Handshake timeout when re-promoting a prior mediator. Matches
/// the default that the route layer applies for forward
/// operations.
const DEFAULT_ROLLBACK_HANDSHAKE_TIMEOUT: Duration = Duration::from_secs(10);

#[derive(Debug, Clone)]
pub struct RollbackDidcommParams {
    /// Drain window applied to the mediator being demoted by this
    /// rollback. Used by the `update_didcomm` arm (drains the
    /// currently-active mediator while promoting the prior one)
    /// and by the `disable_didcomm` arm (drains the
    /// currently-active mediator on the way to "DIDComm off").
    /// Spec §3.6 default: 24h.
    pub drain_ttl: Duration,
    /// Transport over which the rollback request was delivered.
    /// Drives the same `MIN_DRAIN_TTL_OVER_DIDCOMM` floor that
    /// applies to direct disable: a rollback that disables
    /// DIDComm over the DIDComm transport with `drain_ttl < 1h`
    /// is refused so the listener doesn't drop the response
    /// mid-flight.
    pub transport: DisableTransport,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RollbackKind {
    /// Snapshot was `Disabled`; rollback re-disabled DIDComm.
    Disabled,
    /// Snapshot was `Enabled { mediator: X }`; rollback re-enabled
    /// DIDComm with X (forward operation: enable_didcomm).
    Enabled,
    /// Snapshot was `Enabled { mediator: X }`; current was Y;
    /// rollback updated the mediator from Y back to X (forward
    /// operation: update_didcomm).
    Updated,
    /// Snapshot ≡ current state. No LogEntry was published.
    NoOp,
}

#[derive(Debug, Clone)]
pub struct RollbackDidcommResult {
    /// `Some(version_id)` when a LogEntry was published, `None`
    /// when the rollback was a no-op (snapshot ≡ current).
    pub new_version_id: Option<String>,
    pub kind: RollbackKind,
    /// Mediator being drained as part of this rollback (the
    /// previously-active one), or `None` for `Enabled` /
    /// `NoOp` arms where no drain is scheduled.
    pub draining_mediator: Option<String>,
    /// The VTA's own DID. Empty for no-op rollbacks. See
    /// [`super::enable_rest::EnableRestResult`].
    pub vta_did: String,
    /// True when the VTA's DID is self-hosted. `false` on no-op
    /// rollbacks.
    pub serverless: bool,
}

#[derive(Debug, Error)]
pub enum RollbackDidcommError {
    #[error(
        "no prior mutation for `services didcomm` to roll back from. \
         Use `services didcomm enable / update / disable` directly instead."
    )]
    NoPriorMutation,
    #[error("VTA DID is not configured — run `vta setup` first")]
    VtaDidNotConfigured,
    #[error("VTA DID `{0}` has no webvh record")]
    VtaDidRecordMissing(String),
    #[error("VTA DID `{0}` has no published log")]
    VtaDidLogMissing(String),
    #[error("VTA DID log is empty")]
    EmptyLog,

    #[error(transparent)]
    EnableForward(#[from] EnableDidcommError),
    #[error(transparent)]
    UpdateForward(#[from] UpdateDidcommError),
    #[error(transparent)]
    DisableForward(#[from] DisableDidcommError),

    #[error(transparent)]
    Handshake(#[from] HandshakeError),
    #[error("DID document patch failed: {0}")]
    DocumentPatch(#[from] DocumentPatchError),
    #[error("WebVH update failed: {0}")]
    WebVHUpdate(#[from] UpdateDidWebvhError),
    #[error("auth: {0}")]
    Auth(String),
    #[error("storage error: {0}")]
    Storage(String),
}

impl From<AppError> for RollbackDidcommError {
    fn from(value: AppError) -> Self {
        Self::Storage(value.to_string())
    }
}

impl From<crate::operations::protocol::preconditions::ProtocolPreconditionError>
    for RollbackDidcommError
{
    fn from(value: crate::operations::protocol::preconditions::ProtocolPreconditionError) -> Self {
        use crate::operations::protocol::preconditions::ProtocolPreconditionError as E;
        match value {
            E::VtaDidNotConfigured => Self::VtaDidNotConfigured,
            E::VtaDidRecordMissing(s) => Self::VtaDidRecordMissing(s),
            E::VtaDidLogMissing(s) => Self::VtaDidLogMissing(s),
            E::EmptyLog => Self::EmptyLog,
            E::Storage(s) | E::DocumentParse(s) => Self::Storage(s),
        }
    }
}

pub async fn rollback_didcomm(
    deps: &ServiceOpDeps<'_>,
    prover: &(dyn ListenerProver + Send + Sync),
    auth: &AuthClaims,
    params: RollbackDidcommParams,
    channel: &str,
) -> Result<RollbackDidcommResult, RollbackDidcommError> {
    auth.require_super_admin()
        .map_err(|e| RollbackDidcommError::Auth(e.to_string()))?;

    // PROTOCOL_LOCK is taken by the dispatched forward op —
    // holding it twice would deadlock. The snapshot read is
    // atomic via fjall.
    let snap = snapshot::read(deps.snapshot_ks, ServiceKind::Didcomm)
        .await
        .map_err(|e| RollbackDidcommError::Storage(format!("snapshot read: {e}")))?
        .ok_or(RollbackDidcommError::NoPriorMutation)?;
    let didcomm_snap = match snap {
        ServiceConfigSnapshot::Didcomm(s) => s,
        other => {
            return Err(RollbackDidcommError::Storage(format!(
                "snapshot kind mismatch: stored {other:?}, requested Didcomm",
            )));
        }
    };

    let current_mediator = read_current_didcomm_mediator(deps.config, deps.webvh_ks).await?;

    info!(
        channel,
        snapshot = ?didcomm_snap,
        current = ?current_mediator,
        "rollback_didcomm dispatching",
    );

    match (didcomm_snap, current_mediator.as_deref()) {
        // Snapshot says off, currently on → disable.
        (DidcommSnapshot::Disabled, Some(prior)) => {
            let result = disable_didcomm(
                deps,
                auth,
                DisableDidcommParams {
                    drain_ttl: params.drain_ttl,
                    transport: params.transport,
                },
                OpContext::Rollback,
                channel,
            )
            .await?;
            Ok(RollbackDidcommResult {
                new_version_id: Some(result.new_version_id),
                kind: RollbackKind::Disabled,
                draining_mediator: Some(prior.to_string()),
                vta_did: result.vta_did,
                serverless: result.serverless,
            })
        }

        // Snapshot says on with mediator X, currently off →
        // enable with X. No drain (nothing is currently active
        // to demote).
        (
            DidcommSnapshot::Enabled {
                mediator_did,
                routing_keys: _,
            },
            None,
        ) => {
            let result = enable_didcomm(
                deps,
                prover,
                auth,
                EnableDidcommParams {
                    mediator_did: mediator_did.clone(),
                    force: false,
                    handshake_timeout: DEFAULT_ROLLBACK_HANDSHAKE_TIMEOUT,
                },
                OpContext::Rollback,
                channel,
            )
            .await?;
            Ok(RollbackDidcommResult {
                new_version_id: Some(result.new_version_id),
                kind: RollbackKind::Enabled,
                draining_mediator: None,
                vta_did: result.vta_did,
                serverless: result.serverless,
            })
        }

        // Snapshot says on with mediator X, currently on with Y
        // where X != Y → update Y→X. Drains Y for the supplied
        // TTL.
        (
            DidcommSnapshot::Enabled {
                mediator_did,
                routing_keys: _,
            },
            Some(current),
        ) if mediator_did != current => {
            let result = update_didcomm(
                deps,
                prover,
                auth,
                UpdateDidcommParams {
                    new_mediator_did: mediator_did.clone(),
                    drain_ttl: params.drain_ttl,
                    force: false,
                    handshake_timeout: DEFAULT_ROLLBACK_HANDSHAKE_TIMEOUT,
                    audit_kind: MigrateAuditKind::Rollback,
                    transport: params.transport,
                },
                OpContext::Rollback,
                channel,
            )
            .await?;
            Ok(RollbackDidcommResult {
                new_version_id: Some(result.new_version_id),
                kind: RollbackKind::Updated,
                draining_mediator: Some(result.prior_mediator_did),
                vta_did: result.vta_did,
                serverless: result.serverless,
            })
        }

        // Snapshot ≡ current state — no-op.
        _ => {
            info!(
                channel,
                "rollback_didcomm: snapshot matches current state — no-op"
            );
            Ok(RollbackDidcommResult {
                new_version_id: None,
                kind: RollbackKind::NoOp,
                draining_mediator: None,
                // No LogEntry written — empty `vta_did` + `false`
                // are the "no follow-up needed" sentinel for the
                // CLI hint path.
                vta_did: String::new(),
                serverless: false,
            })
        }
    }
}

async fn read_current_didcomm_mediator(
    config: &Arc<RwLock<AppConfig>>,
    webvh_ks: &KeyspaceHandle,
) -> Result<Option<String>, RollbackDidcommError> {
    let state = super::preconditions::load_vta_doc_state(config, webvh_ks).await?;
    Ok(current_didcomm_service(&state.current_doc).map(|svc| svc.mediator_did))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::store::Store;
    use vti_common::config::StoreConfig as VtiStoreConfig;

    async fn empty_keyspace(name: &str) -> (tempfile::TempDir, KeyspaceHandle) {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::open(&VtiStoreConfig {
            data_dir: dir.path().into(),
        })
        .unwrap();
        let ks = store.keyspace(name).unwrap();
        (dir, ks)
    }

    /// Empty snapshot keyspace yields the typed
    /// NoPriorMutation error path. Pin the error message for
    /// the CLI's suggested-fix string.
    #[tokio::test]
    async fn no_prior_mutation_error_message_is_actionable() {
        let (_dir, ks) = empty_keyspace(snapshot::KEYSPACE_NAME).await;
        let snap = snapshot::read(&ks, ServiceKind::Didcomm).await.unwrap();
        assert!(snap.is_none());

        let err = RollbackDidcommError::NoPriorMutation;
        let msg = err.to_string();
        assert!(
            msg.contains("no prior mutation"),
            "error message must point operator at the right next step, got: {msg}",
        );
        assert!(msg.contains("services didcomm enable"));
    }

    #[tokio::test]
    async fn snapshot_disabled_round_trips() {
        let (_dir, ks) = empty_keyspace(snapshot::KEYSPACE_NAME).await;
        snapshot::write(
            &ks,
            ServiceConfigSnapshot::Didcomm(DidcommSnapshot::Disabled),
        )
        .await
        .unwrap();
        let read = snapshot::read(&ks, ServiceKind::Didcomm)
            .await
            .unwrap()
            .unwrap();
        match read {
            ServiceConfigSnapshot::Didcomm(DidcommSnapshot::Disabled) => {}
            other => panic!("expected Didcomm(Disabled), got {other:?}"),
        }
    }

    #[tokio::test]
    async fn snapshot_enabled_with_mediator_round_trips() {
        let (_dir, ks) = empty_keyspace(snapshot::KEYSPACE_NAME).await;
        snapshot::write(
            &ks,
            ServiceConfigSnapshot::Didcomm(DidcommSnapshot::Enabled {
                mediator_did: "did:peer:2.M".into(),
                routing_keys: vec![],
            }),
        )
        .await
        .unwrap();
        let read = snapshot::read(&ks, ServiceKind::Didcomm)
            .await
            .unwrap()
            .unwrap();
        match read {
            ServiceConfigSnapshot::Didcomm(DidcommSnapshot::Enabled { mediator_did, .. }) => {
                assert_eq!(mediator_did, "did:peer:2.M");
            }
            other => panic!("expected Didcomm(Enabled), got {other:?}"),
        }
    }

    #[test]
    fn rollback_kind_variants_are_distinct() {
        assert_ne!(RollbackKind::Disabled, RollbackKind::Enabled);
        assert_ne!(RollbackKind::Enabled, RollbackKind::Updated);
        assert_ne!(RollbackKind::Updated, RollbackKind::NoOp);
    }

    /// `RollbackDidcommParams` carries both drain_ttl and transport
    /// — the transport drives the MIN_DRAIN_TTL_OVER_DIDCOMM floor
    /// when the dispatched op is `disable_didcomm` over DIDComm
    /// transport.
    #[test]
    fn params_carry_transport_and_ttl() {
        let p = RollbackDidcommParams {
            drain_ttl: Duration::from_secs(86_400),
            transport: DisableTransport::Rest,
        };
        assert_eq!(p.drain_ttl.as_secs(), 86_400);
        assert_eq!(p.transport, DisableTransport::Rest);
    }
}