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`) and `member` (`list`, `show`, `repair`,
29//! `cancel`, `suspend`, `resume`, `archive`, `remove`; see
30//! [`SuspendOptions`] and [`RemoveOptions`]).
31//! Every authenticated request is signed with
32//! [`NostrSecretKey::sign_request_with_nonce`](stratum_node_core::NostrSecretKey::sign_request_with_nonce)
33//! over the exact URL, method and body bytes sent, with a fresh random
34//! nonce so no two signings are the same event.
35//! - **Tenant commands.** `join`, `pod` (`ls`, `get`, `put`, `rm`, `acl get`,
36//! `acl put`), `export`, `profile init` and `profile show` take a
37//! [`GateTarget`]: the node's
38//! [`CanonicalOrigin`], which every request is signed for and named in
39//! `Host`, and a loopback [`ConnectAddress`] the request is sent to over
40//! plain HTTP. Every pod path is checked as a
41//! [`PodPath`](stratum_node_core::PodPath) before anything is sent.
42//! - **The public profile.** `join` and `profile init` write the card
43//! `/pods/<pk>/profile/card` and its public-read ACL with the exact texts
44//! of [`profile_card`](stratum_node_core::profile_card) and
45//! [`profile_acl`](stratum_node_core::profile_acl), reading first and
46//! creating only what is missing ([`ProfileInitOptions`]). `profile show`
47//! reads anyone's card with an unsigned request and needs no key.
48//! - **Exit codes.** Failures are [`CliError`]s, each mapped to a stable
49//! code in [`exit`]; [`CliError::hint`] adds advice for the codes that
50//! need it.
51//!
52//! The crate targets Unix: key files rely on Unix permission bits.
53//!
54//! # Example
55//!
56//! ```no_run
57//! use stratum_node_cli::{Output, OutputFormat, Session};
58//!
59//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
60//! let output = Output::new(OutputFormat::Human);
61//! let session = Session::new("http://127.0.0.1:7460".parse()?, Some("operator.nsec".into()), output);
62//! let code = session.status().unwrap_or_else(|err| {
63//! output.error(&err);
64//! err.exit_code()
65//! });
66//! std::process::exit(i32::from(code));
67//! # }
68//! ```
69
70#[cfg(not(unix))]
71compile_error!("stratum-node-cli supports Unix only: key files rely on Unix permission bits");
72
73mod admin;
74mod changes;
75mod client;
76mod error;
77pub mod exit;
78mod export;
79mod gate;
80mod join;
81mod key;
82mod lifecycle;
83mod listing;
84mod origin;
85mod output;
86mod pod;
87mod private_file;
88mod profile;
89mod redact;
90mod resume;
91mod session;
92mod setup;
93mod token;
94mod url;
95mod wire;
96
97pub use admin::{parse_duration, InviteOptions, MAX_INVITATION_LIFETIME};
98pub use changes::{parse_changes, ParseChangeError};
99pub use error::CliError;
100pub use export::{
101 ExportOptions, MANIFEST_FORMAT, MANIFEST_FORMAT_VERSION, MANIFEST_SUFFIX, MAX_EXPORT_BYTES,
102 MAX_EXPORT_ENTRIES,
103};
104pub use join::{JoinOptions, DEFAULT_WAIT};
105pub use key::{generate_key_file, load_key_file, KeyFileError};
106pub use lifecycle::{RemoveOptions, SuspendOptions, DEFAULT_ENFORCEMENT_WAIT};
107pub use origin::{
108 CanonicalOrigin, ConnectAddress, GateTarget, ParseConnectError, ParseOriginError, Scheme,
109};
110pub use output::{Output, OutputFormat};
111pub use pod::{PutOptions, WriteCondition};
112pub use private_file::PrivateFileError;
113pub use profile::ProfileInitOptions;
114pub use session::{ApplyOptions, Confirmation, Session, SetupOptions};
115pub use token::{BootstrapToken, TokenFileError};
116pub use url::{LoopbackUrl, ParseUrlError};
117
118/// The default node URL: the loopback address the controller listens on by
119/// default.
120pub const DEFAULT_URL: &str = "http://127.0.0.1:7460";
121
122/// Compiles the README's examples as doctests.
123#[cfg(doctest)]
124#[doc = include_str!("../README.md")]
125struct ReadmeDoctests;