vtc-service 0.11.22

Service for Verifiable Trust Communities
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
//! Policy read endpoints — list + show (M2.4.1).
//!
//! Spec §7.1 surfacing. Two admin-only GETs that operators (and the
//! M2.4-adjacent CLI verbs) reach for when inspecting the current
//! policy state. Neither endpoint mutates state.
//!
//! ## Filters on the list endpoint
//!
//! - `purpose` — exact-match on `PolicyPurpose` wire form
//!   (`"join"`, `"removal"`, `"crossCommunityRoles"`, …). Unknown
//!   values surface as 400 via serde.
//! - `status` — `"active"` returns only the row currently pointed
//!   at by `active_policies:<purpose>`; `"archived"` returns every
//!   row that is *not* the current active pointer for its purpose.
//!   Omitted returns every row.
//!
//! Filters are applied **after** pagination — the page boundary is
//! over the entire `policies:*` keyspace and rows that fail the
//! filter are silently dropped from the response. This mirrors the
//! members-list approach (M1.4.1) and trades a slightly noisier
//! cursor for a stable page-size invariant.

use std::collections::HashSet;

use axum::Json;
use axum::extract::{Path, Query, State};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use vti_common::error::AppError;
use vti_common::pagination::{Cursor, MAX_LIMIT};

use crate::auth::AdminAuth;
use crate::policy::{
    Policy, PolicyPurpose, get_active_policy_id, get_policy, list_policies_paginated,
};
use crate::server::AppState;

// ---------------------------------------------------------------------------
// Wire shape
// ---------------------------------------------------------------------------

/// Full Policy projection returned by both endpoints. Mirrors the
/// storage row except `sha256` ships as hex (the `hex32` serde
/// adapter is on the storage row already, so the wire shape is
/// already correct here — this struct just adds `isActive` so
/// callers don't need a second round-trip).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
#[derive(utoipa::ToSchema)]
pub struct PolicyResponse {
    #[serde(flatten)]
    pub policy: Policy,
    /// `true` when this row is the currently-active policy for its
    /// purpose. Computed against the `active_policies:<purpose>`
    /// keyspace at response time.
    pub is_active: bool,
}

/// Canonical `policy/list` response.
#[derive(Debug, Serialize, utoipa::ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct PolicyListResponse {
    pub policies: Vec<PolicyModuleResponse>,
    /// Canonical-required: more matching modules exist beyond this page.
    pub truncated: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cursor: Option<String>,
}

/// Canonical `policy/get` response.
#[derive(Debug, Serialize, utoipa::ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct PolicyGetResponse {
    pub policy: PolicyModuleResponse,
}

/// Reverse-DNS-namespaced `ext` member carrying this maintainer's
/// **intrinsic** policy purpose (SPEC.md §4.5.1).
///
/// Canonical models purpose as a property of the *activation binding*,
/// not of the module — a module is purpose-agnostic and reusable. VTC
/// cannot follow that: a purpose is baked into the module's own Rego
/// package name and validated at upload, because a module in the wrong
/// package compiles cleanly and then silently denies every request for
/// that ceremony. Inference is not a way out either — only 4 of the 10
/// purposes have an expected package, so 6 could never be derived from
/// the source.
///
/// So the purpose travels in `ext`, which is exactly what the framework
/// reserves for ecosystem-defined members. It is a documented
/// divergence rather than a hidden one: a consumer reading the module
/// can see that this maintainer binds purpose intrinsically.
pub const PURPOSE_EXT_KEY: &str = "org.openvtc.purpose";

/// Canonical `policy/_shared` **PolicyModule**.
///
/// Field mapping from VTC's storage row:
/// - `rego_source` → `module`
/// - `version` (monotone per-purpose revision counter) → `version`,
///   which canonical also uses as the optimistic-concurrency token.
/// - `updatedAt` is the activation time when the row has been
///   activated, else its creation time. A VTC revision is immutable, so
///   activation is the only thing that can change it after write.
/// - `sha256` / `authorDid` / the intrinsic `purpose` have no canonical
///   home and ride in `ext` (the canonical type is
///   `additionalProperties: false`).
///
/// `appliesTo` / `priority` / `enabled` are deliberately **not**
/// emitted: VTC does no `appliesTo`/`priority` selection, and emitting
/// empty values would imply a selection model that isn't there.
#[derive(Debug, Clone, Serialize, utoipa::ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct PolicyModuleResponse {
    pub id: Uuid,
    pub name: String,
    pub module: String,
    pub version: u32,
    pub created_at: String,
    pub updated_at: String,
    pub ext: serde_json::Value,
}

impl From<&Policy> for PolicyModuleResponse {
    fn from(p: &Policy) -> Self {
        Self {
            id: p.id,
            // Rows written before `name` existed fall back to the
            // purpose — the only stable human-meaningful identifier
            // such a row has, and canonical requires `name`.
            name: p
                .name
                .clone()
                .unwrap_or_else(|| p.purpose.as_str().to_owned()),
            module: p.rego_source.clone(),
            version: p.version,
            created_at: p.created_at.to_rfc3339(),
            updated_at: p.activated_at.unwrap_or(p.created_at).to_rfc3339(),
            ext: serde_json::json!({
                PURPOSE_EXT_KEY: p.purpose.as_str(),
                "org.openvtc.sha256": hex::encode(p.sha256),
                "org.openvtc.authorDid": p.author_did,
            }),
        }
    }
}

impl PolicyResponse {
    fn from_policy(policy: Policy, is_active: bool) -> Self {
        Self { policy, is_active }
    }
}

// ---------------------------------------------------------------------------
// GET /v1/policies
// ---------------------------------------------------------------------------

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
#[derive(utoipa::ToSchema, utoipa::IntoParams)]
pub struct ListPoliciesQuery {
    /// Filter by purpose (wire-form camelCase). A VTC extension —
    /// canonical has no purpose filter because purpose is not a
    /// property of a module there.
    pub purpose: Option<PolicyPurpose>,
    /// Canonical `policy/list` parameters this maintainer does not
    /// implement. Accepting them silently would tell a caller their
    /// query was narrowed when it was not, so they are refused.
    pub context_id: Option<String>,
    pub enabled_only: Option<bool>,
    /// Canonical page-size name.
    pub page_size: Option<usize>,
    /// `"active"` — only the row pointed at by each
    /// `active_policies:<purpose>`. `"archived"` — every row that
    /// is *not* the current active pointer. Omitted → all rows.
    pub status: Option<PolicyStatusFilter>,
    /// Pagination cursor (returned by a previous call).
    pub cursor: Option<String>,
    /// Page size. Clamped to `1..=200`.
    pub limit: Option<usize>,
}

#[derive(Debug, Clone, Copy, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
#[derive(utoipa::ToSchema)]
pub enum PolicyStatusFilter {
    Active,
    Archived,
}

#[utoipa::path(
    get, path = "/policies", tag = "policies",
    security(("bearer_jwt" = [])),
    params(ListPoliciesQuery),
    responses(
        (status = 200, description = "Paginated list of policies", body = PolicyListResponse),
        (status = 401, description = "Missing or invalid bearer token"),
        (status = 403, description = "Caller is not an admin"),
    ),
)]
pub async fn list_policies(
    _auth: AdminAuth,
    State(state): State<AppState>,
    Query(query): Query<ListPoliciesQuery>,
) -> Result<Json<PolicyListResponse>, AppError> {
    let mut unsupported = Vec::new();
    if query.context_id.is_some() {
        unsupported.push("contextId");
    }
    if query.enabled_only.is_some() {
        unsupported.push("enabledOnly");
    }
    if !unsupported.is_empty() {
        return Err(AppError::Validation(format!(
            "this maintainer does not implement the {} filter(s): its policy log \
             is not context-partitioned and modules carry no enabled flag. \
             Refusing rather than returning an unfiltered list.",
            unsupported.join(", "),
        )));
    }

    // `status=active` is resolved directly from the per-purpose active
    // pointers, not the paginated keyspace scan. The purpose/status
    // filters below run *after* pagination, so a small `limit` (the
    // simulator's active-policy lookup sends `limit=1`) would drop the
    // active row for every purpose whose row isn't in the first page —
    // surfacing the active policy for one arbitrary purpose only. The
    // active set is at most one row per `PolicyPurpose`, so resolve it
    // deterministically and ignore limit/cursor.
    if matches!(query.status, Some(PolicyStatusFilter::Active)) {
        let mut items = Vec::new();
        for purpose in PolicyPurpose::ALL {
            if query.purpose.is_some_and(|f| f != purpose) {
                continue;
            }
            if let Some(id) = get_active_policy_id(&state.active_policies_ks, purpose).await?
                && let Some(p) = get_policy(&state.policies_ks, id).await?
            {
                items.push(PolicyResponse::from_policy(p, true));
            }
        }
        return Ok(Json(PolicyListResponse {
            policies: items.iter().map(|r| (&r.policy).into()).collect(),
            truncated: false,
            cursor: None,
        }));
    }

    let limit = query
        .page_size
        .or(query.limit)
        .unwrap_or(50)
        .clamp(1, MAX_LIMIT);

    let audit_writer = state
        .audit_writer
        .as_ref()
        .ok_or_else(|| AppError::Internal("audit_writer not initialised".into()))?;
    let audit_key = audit_writer.active_key().await?;

    let decoded_cursor = match &query.cursor {
        Some(s) => Some(Cursor::decode(s, &audit_key.key)?),
        None => None,
    };

    let page = list_policies_paginated(
        &state.policies_ks,
        &audit_key,
        decoded_cursor.as_ref(),
        limit,
    )
    .await?;

    // Resolve the set of currently-active policy ids once per page.
    // Cheap: at most 9 entries (one per PolicyPurpose).
    let mut active_ids: HashSet<Uuid> = HashSet::new();
    for purpose in PolicyPurpose::ALL {
        if let Some(id) = get_active_policy_id(&state.active_policies_ks, purpose).await? {
            active_ids.insert(id);
        }
    }

    let purpose_filter = query.purpose;
    let status_filter = query.status;

    let items: Vec<PolicyResponse> = page
        .items
        .into_iter()
        .filter(|p| purpose_filter.is_none_or(|f| p.purpose == f))
        .filter_map(|p| {
            let is_active = active_ids.contains(&p.id);
            match status_filter {
                Some(PolicyStatusFilter::Active) if !is_active => return None,
                Some(PolicyStatusFilter::Archived) if is_active => return None,
                _ => {}
            }
            Some(PolicyResponse::from_policy(p, is_active))
        })
        .collect();

    Ok(Json(PolicyListResponse {
        policies: items.iter().map(|r| (&r.policy).into()).collect(),
        truncated: page.next_cursor.is_some(),
        cursor: page.next_cursor,
    }))
}

// ---------------------------------------------------------------------------
// GET /v1/policies/{id}
// ---------------------------------------------------------------------------

#[utoipa::path(
    get, path = "/policies/{id}", tag = "policies",
    security(("bearer_jwt" = [])),
    params(("id" = String, Path, description = "Policy id")),
    responses(
        (status = 200, description = "Policy", body = PolicyGetResponse),
        (status = 401, description = "Missing or invalid bearer token"),
        (status = 403, description = "Caller is not an admin"),
        (status = 404, description = "Policy not found"),
    ),
)]
pub async fn show_policy(
    _auth: AdminAuth,
    State(state): State<AppState>,
    Path(id): Path<Uuid>,
) -> Result<Json<PolicyGetResponse>, AppError> {
    let policy = get_policy(&state.policies_ks, id)
        .await?
        .ok_or_else(|| AppError::NotFound(format!("policy not found: {id}")))?;
    Ok(Json(PolicyGetResponse {
        policy: (&policy).into(),
    }))
}

// ---------------------------------------------------------------------------
// GET /v1/policies/active — canonical `policy/active`
// ---------------------------------------------------------------------------

/// Canonical `policy/active` response: the `(contextId, purpose) →
/// module` bindings currently in force.
#[derive(Debug, Serialize, utoipa::ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct ActiveBindingsResponse {
    pub bindings: Vec<ActiveBinding>,
}

/// One activation binding. `contextId` is omitted throughout: a VTC is
/// a single community and does not partition its policy set per trust
/// context, so every binding is community-wide.
#[derive(Debug, Serialize, utoipa::ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct ActiveBinding {
    pub purpose: String,
    pub policy: PolicyModuleResponse,
}

#[derive(Debug, Deserialize, utoipa::ToSchema, utoipa::IntoParams)]
#[serde(rename_all = "camelCase")]
#[into_params(parameter_in = Query)]
pub struct ActiveQuery {
    /// Narrow to a single decision slot.
    pub purpose: Option<PolicyPurpose>,
    /// Not implemented — a VTC is not context-partitioned. Refused
    /// rather than ignored, so a caller never reads a community-wide
    /// binding as one scoped to their context.
    pub context_id: Option<String>,
}

#[utoipa::path(
    get, path = "/policies/active", tag = "policies",
    security(("bearer_jwt" = [])),
    params(ActiveQuery),
    responses(
        (status = 200, description = "Active policy bindings", body = ActiveBindingsResponse),
        (status = 401, description = "Missing or invalid bearer token"),
        (status = 403, description = "Caller is not an admin"),
    ),
)]
pub async fn active_policies(
    _auth: AdminAuth,
    State(state): State<AppState>,
    Query(query): Query<ActiveQuery>,
) -> Result<Json<ActiveBindingsResponse>, AppError> {
    if query.context_id.is_some() {
        return Err(AppError::Validation(
            "this maintainer does not implement the contextId filter: a VTC is a \
             single community and its policy bindings are community-wide."
                .into(),
        ));
    }

    let mut bindings = Vec::new();
    for purpose in PolicyPurpose::ALL {
        if query.purpose.is_some_and(|f| f != purpose) {
            continue;
        }
        if let Some(id) = get_active_policy_id(&state.active_policies_ks, purpose).await?
            && let Some(p) = get_policy(&state.policies_ks, id).await?
        {
            bindings.push(ActiveBinding {
                purpose: purpose.as_str().to_owned(),
                policy: (&p).into(),
            });
        }
    }
    Ok(Json(ActiveBindingsResponse { bindings }))
}