stratum-node-cli 0.1.0-alpha.4

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
//! The library behind `stratum`, the client for a Stratum node: the
//! administrator's commands on the loopback API, and a tenant's commands
//! through the gate.
//!
//! **Pre-release:** this is `0.1.0-alpha`. The commands, output and API may
//! change before 0.1.0, and nothing built on it has been deployed yet.
//!
//! A Stratum node serves an administrative API on loopback only. `stratum`
//! talks to that API over plain HTTP and holds the operator's own Nostr key;
//! it never sees the node key or the node's state volume, except that
//! `stratum setup` reads the bootstrap token file the operator points it at.
//! A tenant uses the same binary with their own key to join a node and to
//! read, write and export their pod through the node's gate.
//!
//! - **URLs.** [`LoopbackUrl`] accepts `http://` URLs whose host is an
//!   IPv4 address in `127.0.0.0/8`, `[::1]` or `localhost`, and nothing
//!   else: Step 1 has no TLS. `localhost` is connected to `127.0.0.1`
//!   without a name lookup.
//! - **Operator keys.** [`generate_key_file`] writes a fresh key as one
//!   `nsec1…` line to a file created exclusively with mode `0600`;
//!   [`load_key_file`] refuses symbolic links and modes wider than `0600`.
//!   The secret is never printed, logged or returned as text.
//! - **Changes.** [`parse_changes`] reads `--set path=value` arguments typed
//!   by the schema in `stratum-node-config`.
//! - **Commands.** [`Session`] runs each command against one node: `status`,
//!   `config` (`schema`, `show`, `preview`, `apply`), `setup`,
//!   `sign-challenge`, `audit checkpoint`, `backup`, `invite`
//!   (`create`, `list`, `revoke`), `member` (`list`, `show`, `repair`,
//!   `cancel`, `suspend`, `resume`, `archive`, `remove`; see
//!   [`SuspendOptions`] and [`RemoveOptions`]), `application` (`add`,
//!   `list`, `withdraw`, `approve`, `disable`; see [`ApplicationAction`])
//!   and `installations`.
//!   Every authenticated request is signed with
//!   [`NostrSecretKey::sign_request_with_nonce`](stratum_node_core::NostrSecretKey::sign_request_with_nonce)
//!   over the exact URL, method and body bytes sent, with a fresh random
//!   nonce so no two signings are the same event.
//! - **Tenant commands.** `join`, `pod` (`ls`, `get`, `put`, `rm`, `acl get`,
//!   `acl put`), `export`, `profile init` and `profile show` take a
//!   [`GateTarget`]: the node's
//!   [`CanonicalOrigin`], which every request is signed for and named in
//!   `Host`, and a loopback [`ConnectAddress`] the request is sent to over
//!   plain HTTP. Every pod path is checked as a
//!   [`PodPath`](stratum_node_core::PodPath) before anything is sent.
//! - **Applications.** `app` (`catalogue`, `install`, `list`, `show`,
//!   `jobs`, `run`, `withdraw`, `cleanup`, `resume`) installs and withdraws
//!   applications in the tenant's own pod. `install` ([`InstallOptions`])
//!   checks the offered grant by rendering it again with
//!   [`render_offered`](stratum_node_core::render_offered), reads the ACL
//!   that governs each granted container, builds its new ACL (the owner's
//!   control, existing rules kept, inherited `acl:default` rules
//!   materialised, the offered rule), refuses any statement it does not
//!   evaluate and any `Control` for the application, previews the diffs and
//!   [`OPERATOR_STATEMENT`], writes each ACL conditionally after rereading
//!   what governs it, reads it back, and consents only if every read-back
//!   matches. `withdraw` and `cleanup` edit the installation's
//!   authorisation out of each ACL with `If-Match` and report each
//!   container as it is cleaned. Turtle is parsed with `oxttl`. Every `app`
//!   command shows a withdrawn grant whose cleanup is not yet reported.
//! - **The public profile.** `join` and `profile init` write the card
//!   `/pods/<pk>/profile/card` and its public-read ACL with the exact texts
//!   of [`profile_card`](stratum_node_core::profile_card) and
//!   [`profile_acl`](stratum_node_core::profile_acl), reading first and
//!   creating only what is missing ([`ProfileInitOptions`]). `profile show`
//!   reads anyone's card with an unsigned request and needs no key.
//! - **Exit codes.** Failures are [`CliError`]s, each mapped to a stable
//!   code in [`exit`]; [`CliError::hint`] adds advice for the codes that
//!   need it.
//!
//! The crate targets Unix: key files rely on Unix permission bits.
//!
//! # Example
//!
//! ```no_run
//! use stratum_node_cli::{Output, OutputFormat, Session};
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let output = Output::new(OutputFormat::Human);
//! let session = Session::new("http://127.0.0.1:7460".parse()?, Some("operator.nsec".into()), output);
//! let code = session.status().unwrap_or_else(|err| {
//!     output.error(&err);
//!     err.exit_code()
//! });
//! std::process::exit(i32::from(code));
//! # }
//! ```

#[cfg(not(unix))]
compile_error!("stratum-node-cli supports Unix only: key files rely on Unix permission bits");

mod acl;
mod admin;
mod app;
mod application;
mod changes;
mod client;
mod error;
pub mod exit;
mod export;
mod gate;
mod join;
mod key;
mod lifecycle;
mod listing;
mod origin;
mod output;
mod pod;
mod private_file;
mod profile;
mod redact;
mod resume;
mod session;
mod setup;
mod token;
mod url;
mod wire;

pub use admin::{parse_duration, InviteOptions, MAX_INVITATION_LIFETIME};
pub use app::{InstallOptions, OPERATOR_STATEMENT};
pub use application::ApplicationAction;
pub use changes::{parse_changes, ParseChangeError};
pub use error::CliError;
pub use export::{
    ExportOptions, MANIFEST_FORMAT, MANIFEST_FORMAT_VERSION, MANIFEST_SUFFIX, MAX_EXPORT_BYTES,
    MAX_EXPORT_ENTRIES,
};
pub use join::{JoinOptions, DEFAULT_WAIT};
pub use key::{generate_key_file, load_key_file, KeyFileError};
pub use lifecycle::{RemoveOptions, SuspendOptions, DEFAULT_ENFORCEMENT_WAIT};
pub use origin::{
    CanonicalOrigin, ConnectAddress, GateTarget, ParseConnectError, ParseOriginError, Scheme,
};
pub use output::{Output, OutputFormat};
pub use pod::{PutOptions, WriteCondition};
pub use private_file::PrivateFileError;
pub use profile::ProfileInitOptions;
pub use session::{ApplyOptions, Confirmation, Session, SetupOptions};
pub use token::{BootstrapToken, TokenFileError};
pub use url::{LoopbackUrl, ParseUrlError};

/// The default node URL: the loopback address the controller listens on by
/// default.
pub const DEFAULT_URL: &str = "http://127.0.0.1:7460";

/// Compiles the README's examples as doctests.
#[cfg(doctest)]
#[doc = include_str!("../README.md")]
struct ReadmeDoctests;