Skip to main content

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