stratum_node_cli/lib.rs
1//! The library behind `stratum`, the client for a Stratum node: the
2//! administrator's commands on the loopback API, and a tenant's commands
3//! through the gate.
4//!
5//! **Pre-release:** this is `0.1.0-alpha`. The commands, output and API may
6//! change before 0.1.0, and nothing built on it has been deployed yet.
7//!
8//! A Stratum node serves an administrative API on loopback only. `stratum`
9//! talks to that API over plain HTTP and holds the operator's own Nostr key;
10//! it never sees the node key or the node's state volume, except that
11//! `stratum setup` reads the bootstrap token file the operator points it at.
12//! A tenant uses the same binary with their own key to join a node and to
13//! read, write and export their pod through the node's gate.
14//!
15//! - **URLs.** [`LoopbackUrl`] accepts `http://` URLs whose host is an
16//! IPv4 address in `127.0.0.0/8`, `[::1]` or `localhost`, and nothing
17//! else: Step 1 has no TLS. `localhost` is connected to `127.0.0.1`
18//! without a name lookup.
19//! - **Operator keys.** [`generate_key_file`] writes a fresh key as one
20//! `nsec1…` line to a file created exclusively with mode `0600`;
21//! [`load_key_file`] refuses symbolic links and modes wider than `0600`.
22//! The secret is never printed, logged or returned as text.
23//! - **Changes.** [`parse_changes`] reads `--set path=value` arguments typed
24//! by the schema in `stratum-node-config`.
25//! - **Commands.** [`Session`] runs each command against one node: `status`,
26//! `config` (`schema`, `show`, `preview`, `apply`), `setup`,
27//! `sign-challenge`, `audit checkpoint`, `backup`, `invite`
28//! (`create`, `list`, `revoke`), `member` (`list`, `show`, `repair`,
29//! `cancel`, `suspend`, `resume`, `archive`, `remove`; see
30//! [`SuspendOptions`] and [`RemoveOptions`]), `application` (`add`,
31//! `list`, `withdraw`, `approve`, `disable`; see [`ApplicationAction`])
32//! and `installations`.
33//! Every authenticated request is signed with
34//! [`NostrSecretKey::sign_request_with_nonce`](stratum_node_core::NostrSecretKey::sign_request_with_nonce)
35//! over the exact URL, method and body bytes sent, with a fresh random
36//! nonce so no two signings are the same event.
37//! - **Tenant commands.** `join`, `pod` (`ls`, `get`, `put`, `rm`, `acl get`,
38//! `acl put`), `export`, `profile init` and `profile show` take a
39//! [`GateTarget`]: the node's
40//! [`CanonicalOrigin`], which every request is signed for and named in
41//! `Host`, and a loopback [`ConnectAddress`] the request is sent to over
42//! plain HTTP. Every pod path is checked as a
43//! [`PodPath`](stratum_node_core::PodPath) before anything is sent.
44//! - **Applications.** `app` (`catalogue`, `install`, `list`, `show`,
45//! `jobs`, `run`, `withdraw`, `cleanup`, `resume`) installs and withdraws
46//! applications in the tenant's own pod. `install` ([`InstallOptions`])
47//! checks the offered grant by rendering it again with
48//! [`render_offered`](stratum_node_core::render_offered), reads the ACL
49//! that governs each granted container, builds its new ACL (the owner's
50//! control, existing rules kept, inherited `acl:default` rules
51//! materialised, the offered rule), refuses any statement it does not
52//! evaluate and any `Control` for the application, previews the diffs and
53//! [`OPERATOR_STATEMENT`], writes each ACL conditionally after rereading
54//! what governs it, reads it back, and consents only if every read-back
55//! matches. `withdraw` and `cleanup` edit the installation's
56//! authorisation out of each ACL with `If-Match` and report each
57//! container as it is cleaned. Turtle is parsed with `oxttl`. Every `app`
58//! command shows a withdrawn grant whose cleanup is not yet reported.
59//! - **The public profile.** `join` and `profile init` write the card
60//! `/pods/<pk>/profile/card` and its public-read ACL with the exact texts
61//! of [`profile_card`](stratum_node_core::profile_card) and
62//! [`profile_acl`](stratum_node_core::profile_acl), reading first and
63//! creating only what is missing ([`ProfileInitOptions`]). `profile show`
64//! reads anyone's card with an unsigned request and needs no key.
65//! - **Exit codes.** Failures are [`CliError`]s, each mapped to a stable
66//! code in [`exit`]; [`CliError::hint`] adds advice for the codes that
67//! need it.
68//!
69//! The crate targets Unix: key files rely on Unix permission bits.
70//!
71//! # Example
72//!
73//! ```no_run
74//! use stratum_node_cli::{Output, OutputFormat, Session};
75//!
76//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
77//! let output = Output::new(OutputFormat::Human);
78//! let session = Session::new("http://127.0.0.1:7460".parse()?, Some("operator.nsec".into()), output);
79//! let code = session.status().unwrap_or_else(|err| {
80//! output.error(&err);
81//! err.exit_code()
82//! });
83//! std::process::exit(i32::from(code));
84//! # }
85//! ```
86
87#[cfg(not(unix))]
88compile_error!("stratum-node-cli supports Unix only: key files rely on Unix permission bits");
89
90mod acl;
91mod admin;
92mod app;
93mod application;
94mod changes;
95mod client;
96mod error;
97pub mod exit;
98mod export;
99mod gate;
100mod join;
101mod key;
102mod lifecycle;
103mod listing;
104mod origin;
105mod output;
106mod pod;
107mod private_file;
108mod profile;
109mod redact;
110mod resume;
111mod session;
112mod setup;
113mod token;
114mod url;
115mod wire;
116
117pub use admin::{parse_duration, InviteOptions, MAX_INVITATION_LIFETIME};
118pub use app::{InstallOptions, OPERATOR_STATEMENT};
119pub use application::ApplicationAction;
120pub use changes::{parse_changes, ParseChangeError};
121pub use error::CliError;
122pub use export::{
123 ExportOptions, MANIFEST_FORMAT, MANIFEST_FORMAT_VERSION, MANIFEST_SUFFIX, MAX_EXPORT_BYTES,
124 MAX_EXPORT_ENTRIES,
125};
126pub use join::{JoinOptions, DEFAULT_WAIT};
127pub use key::{generate_key_file, load_key_file, KeyFileError};
128pub use lifecycle::{RemoveOptions, SuspendOptions, DEFAULT_ENFORCEMENT_WAIT};
129pub use origin::{
130 CanonicalOrigin, ConnectAddress, GateTarget, ParseConnectError, ParseOriginError, Scheme,
131};
132pub use output::{Output, OutputFormat};
133pub use pod::{PutOptions, WriteCondition};
134pub use private_file::PrivateFileError;
135pub use profile::ProfileInitOptions;
136pub use session::{ApplyOptions, Confirmation, Session, SetupOptions};
137pub use token::{BootstrapToken, TokenFileError};
138pub use url::{LoopbackUrl, ParseUrlError};
139
140/// The default node URL: the loopback address the controller listens on by
141/// default.
142pub const DEFAULT_URL: &str = "http://127.0.0.1:7460";
143
144/// Compiles the README's examples as doctests.
145#[cfg(doctest)]
146#[doc = include_str!("../README.md")]
147struct ReadmeDoctests;