stratum-node-cli 0.1.0-alpha.2

The `stratum` client for Stratum pod nodes: operator key files, bootstrap, config, backup, invitations and members over the loopback API; join, pod access and export for tenants through the gate
Documentation
//! Request and response bodies of the node's `/api/v1` loopback API and
//! of the tenant admission routes behind the gate.
//!
//! Request structs serialise their fields in a fixed order, and every
//! request body is serialised once and signed and sent as those bytes.
//! Response structs ignore members they do not name, so a node that adds
//! a member stays readable.

use serde::{Deserialize, Serialize};
use stratum_node_config::Change;
use stratum_node_core::{
    ChallengeId, DidNostr, InvitationId, MemberState, OperationId, Sha256Digest, UnixTime,
};

/// `POST /api/v1/config/preview`.
#[derive(Debug, Serialize)]
pub(crate) struct PreviewRequest<'a> {
    pub(crate) base_revision: u64,
    pub(crate) changes: &'a [Change],
}

/// `POST /api/v1/bootstrap/challenge`.
#[derive(Debug, Serialize)]
pub(crate) struct ChallengeRequest<'a> {
    pub(crate) did: DidNostr,
    pub(crate) base_revision: u64,
    pub(crate) changes: &'a [Change],
}

/// `POST /api/v1/config/apply`.
#[derive(Debug, Serialize)]
pub(crate) struct ApplyRequest<'a> {
    pub(crate) node: DidNostr,
    pub(crate) operation_id: &'a OperationId,
    pub(crate) base_revision: u64,
    pub(crate) changes: &'a [Change],
}

/// `POST /api/v1/backup`.
#[derive(Debug, Serialize)]
pub(crate) struct BackupRequest<'a> {
    pub(crate) node: DidNostr,
    pub(crate) operation_id: &'a OperationId,
}

/// `GET /readyz`.
#[derive(Debug, Deserialize)]
pub(crate) struct Readyz {
    pub(crate) status: String,
    pub(crate) version: String,
    pub(crate) head_revision: u64,
    pub(crate) active_revision: u64,
    pub(crate) public_config_hash: Option<String>,
    pub(crate) node_did: DidNostr,
    pub(crate) api_version: u64,
}

/// One entry of a preview's structured change list.
#[derive(Debug, Deserialize)]
pub(crate) struct ChangeEntry {
    pub(crate) path: String,
    pub(crate) apply_class: String,
    #[serde(default)]
    pub(crate) old: Option<serde_json::Value>,
    pub(crate) new: serde_json::Value,
}

/// One validation error, in a preview or an `invalid` error body.
#[derive(Debug, Deserialize)]
pub(crate) struct ValidationEntry {
    #[serde(default)]
    pub(crate) path: Option<String>,
    pub(crate) message: String,
}

/// `POST /api/v1/config/preview` response.
#[derive(Debug, Deserialize)]
pub(crate) struct PreviewResponse {
    pub(crate) base_revision: u64,
    pub(crate) text: String,
    pub(crate) diff: String,
    pub(crate) changes: Vec<ChangeEntry>,
    pub(crate) strictest_apply_class: Option<String>,
    pub(crate) errors: Vec<ValidationEntry>,
}

/// `POST /api/v1/bootstrap/challenge` response.
#[derive(Debug, Deserialize)]
pub(crate) struct ChallengeResponse {
    pub(crate) challenge_id: ChallengeId,
    pub(crate) claim_body: String,
    pub(crate) url: String,
    pub(crate) method: String,
    pub(crate) expires_at: UnixTime,
}

/// `POST /api/v1/bootstrap/claim` response.
#[derive(Debug, Deserialize)]
pub(crate) struct ClaimResponse {
    pub(crate) administrator: DidNostr,
    pub(crate) node_did: DidNostr,
    pub(crate) revision: u64,
    pub(crate) public_config_hash: Sha256Digest,
}

/// `GET /api/v1/config` response.
#[derive(Debug, Deserialize)]
pub(crate) struct ConfigResponse {
    pub(crate) head_revision: u64,
    pub(crate) active_revision: u64,
    pub(crate) public_config_hash: Sha256Digest,
    pub(crate) text: String,
}

/// `POST /api/v1/config/apply` response.
#[derive(Debug, Deserialize)]
pub(crate) struct ApplyResponse {
    pub(crate) revision: u64,
    pub(crate) public_config_hash: Sha256Digest,
    pub(crate) strictest_apply_class: Option<String>,
    pub(crate) restart_required: bool,
}

/// `GET /api/v1/audit/checkpoint` response.
#[derive(Debug, Deserialize)]
pub(crate) struct CheckpointResponse {
    pub(crate) sequence: u64,
    pub(crate) tail_hash: Sha256Digest,
    pub(crate) checkpoint: String,
    pub(crate) node_did: DidNostr,
}

/// `POST /api/v1/backup` response.
#[derive(Debug, Deserialize)]
pub(crate) struct BackupResponse {
    pub(crate) directory: String,
    pub(crate) manifest: serde_json::Value,
    pub(crate) checkpoint: String,
}

/// The name in a backup manifest's `format` member.
pub(crate) const BACKUP_FORMAT: &str = "stratum-backup";

/// The backup manifest `format_version` of a node without pods.
pub(crate) const BACKUP_FORMAT_VERSION: u64 = 1;

/// The backup manifest `format_version` of a node with pods: the Step 1
/// files, then [`BACKUP_POD_ARCHIVE`], and the `pod_archive` member.
pub(crate) const BACKUP_FORMAT_VERSION_PODS: u64 = 2;

/// The files a backup manifest lists, in its fixed order.
pub(crate) const BACKUP_FILES: [&str; 3] = ["stratum.db", "stratum.toml", "node-key.age"];

/// The pod data archive a format 2 backup lists after [`BACKUP_FILES`].
pub(crate) const BACKUP_POD_ARCHIVE: &str = "pods.tar.zst.age";

/// The content of the signed backup manifest event: the controller's
/// format (`stratum-backup`, version 1, or version 2 with pods).
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct BackupManifest {
    pub(crate) format: String,
    pub(crate) format_version: u64,
    pub(crate) node_did: DidNostr,
    pub(crate) created_at: UnixTime,
    pub(crate) config_revision: u64,
    pub(crate) audit_checkpoint: String,
    pub(crate) files: Vec<ManifestFile>,
    /// Format 2 only: what the pod archive holds.
    #[serde(default)]
    pub(crate) pod_archive: Option<PodArchiveFacts>,
}

/// The `pod_archive` member of a format 2 backup manifest.
#[derive(Debug, Clone, Copy, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct PodArchiveFacts {
    /// Directories and regular files in the archive.
    pub(crate) entries: u64,
    /// Total bytes of the regular files.
    pub(crate) bytes: u64,
}

/// One file listed in a [`BackupManifest`].
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct ManifestFile {
    pub(crate) name: String,
    #[expect(
        dead_code,
        reason = "parsed to check the format; checked by stratumd restore"
    )]
    pub(crate) sha256: Sha256Digest,
    #[expect(
        dead_code,
        reason = "parsed to check the format; checked by stratumd restore"
    )]
    pub(crate) size: u64,
}

/// The error envelope every failing route returns.
///
/// The contract says an `invalid` error "adds `errors`" without saying at
/// which level, so the list is read from inside `error` and beside it.
#[derive(Debug, Deserialize)]
pub(crate) struct ErrorEnvelope {
    pub(crate) error: ErrorDetail,
    #[serde(default)]
    pub(crate) errors: Vec<ValidationEntry>,
}

/// The body of [`ErrorEnvelope`].
#[derive(Debug, Deserialize)]
pub(crate) struct ErrorDetail {
    pub(crate) code: String,
    pub(crate) message: String,
    #[serde(default)]
    pub(crate) errors: Vec<ValidationEntry>,
}

/// `POST /api/v1/invitations`.
#[derive(Debug, Serialize)]
pub(crate) struct InviteRequest<'a> {
    pub(crate) node: DidNostr,
    pub(crate) operation_id: &'a OperationId,
    pub(crate) subject: DidNostr,
    pub(crate) expires_in: u64,
}

/// `POST /api/v1/invitations` response.
#[derive(Debug, Deserialize)]
pub(crate) struct InviteResponse {
    pub(crate) invitation: InvitationId,
    pub(crate) subject: DidNostr,
    pub(crate) expires_at: UnixTime,
    pub(crate) origin: String,
    pub(crate) node: DidNostr,
}

/// `GET /api/v1/invitations` response.
#[derive(Debug, Deserialize)]
pub(crate) struct InvitationList {
    pub(crate) invitations: Vec<InvitationEntry>,
}

/// One invitation in [`InvitationList`].
#[derive(Debug, Deserialize)]
pub(crate) struct InvitationEntry {
    pub(crate) invitation: InvitationId,
    pub(crate) subject: DidNostr,
    pub(crate) state: String,
    pub(crate) issued_at: UnixTime,
    pub(crate) expires_at: UnixTime,
}

/// The body of every administrator action that names only the node and an
/// operation: revoke, repair and cancel.
#[derive(Debug, Serialize)]
pub(crate) struct OperationRequest<'a> {
    pub(crate) node: DidNostr,
    pub(crate) operation_id: &'a OperationId,
}

/// `POST /api/v1/invitations/{invitation}/revoke` response.
#[derive(Debug, Deserialize)]
pub(crate) struct RevokeResponse {
    pub(crate) invitation: InvitationId,
    pub(crate) state: String,
}

/// `GET /api/v1/members` response.
#[derive(Debug, Deserialize)]
pub(crate) struct MemberList {
    pub(crate) capacity: u64,
    pub(crate) used: u64,
    pub(crate) members: Vec<MemberEntry>,
}

/// One member in [`MemberList`].
#[derive(Debug, Deserialize)]
pub(crate) struct MemberEntry {
    pub(crate) did: DidNostr,
    pub(crate) state: MemberState,
    pub(crate) pod_url: String,
    pub(crate) operation_id: String,
    pub(crate) reserved_at: UnixTime,
    pub(crate) activated_at: Option<UnixTime>,
    pub(crate) last_error: Option<String>,
}

/// `POST /api/v1/members/{pk}/repair` and `…/cancel` response.
#[derive(Debug, Deserialize)]
pub(crate) struct MemberActionResponse {
    pub(crate) did: DidNostr,
    pub(crate) state: MemberState,
}

/// `GET /api/v1/admission/notice` response.
#[derive(Debug, Deserialize)]
pub(crate) struct NoticeResponse {
    pub(crate) node: DidNostr,
    pub(crate) origin: String,
    pub(crate) notice: String,
    pub(crate) notice_sha256: Sha256Digest,
}

/// `POST /api/v1/admission/redeem` and `GET /api/v1/admission/operation`
/// responses. The redeem response carries no `last_error`.
#[derive(Debug, Deserialize)]
pub(crate) struct OperationResponse {
    pub(crate) operation_id: OperationId,
    pub(crate) did: DidNostr,
    pub(crate) state: MemberState,
    pub(crate) pod_url: String,
    #[serde(default)]
    pub(crate) last_error: Option<String>,
}

/// `POST /api/v1/admission/confirm`.
#[derive(Debug, Serialize)]
pub(crate) struct ConfirmRequest<'a> {
    pub(crate) node: DidNostr,
    pub(crate) operation_id: &'a OperationId,
}

/// `POST /api/v1/admission/confirm` response.
#[derive(Debug, Deserialize)]
pub(crate) struct ConfirmResponse {
    pub(crate) did: DidNostr,
    pub(crate) state: MemberState,
    pub(crate) pod_url: String,
}