2026-03-20 11:56:20 +00:00
|
|
|
//! Bitcoin RPC credential management.
|
|
|
|
|
//!
|
|
|
|
|
//! Uses `rpcauth` in bitcoin.conf (salted hash — no plaintext in config or CLI).
|
|
|
|
|
//! The actual password is stored in `/var/lib/archipelago/secrets/bitcoin-rpc-password`
|
|
|
|
|
//! and stays stable across reboots, restarts, and deploys.
|
2026-03-18 00:39:52 +00:00
|
|
|
|
|
|
|
|
use tokio::sync::OnceCell;
|
|
|
|
|
use tracing::debug;
|
|
|
|
|
|
|
|
|
|
const SECRETS_PATH: &str = "/var/lib/archipelago/secrets/bitcoin-rpc-password";
|
2026-03-20 11:56:20 +00:00
|
|
|
const RPC_USER: &str = "archipelago";
|
2026-03-18 00:39:52 +00:00
|
|
|
|
|
|
|
|
static CACHED_PASSWORD: OnceCell<String> = OnceCell::const_new();
|
|
|
|
|
|
2026-03-20 11:56:20 +00:00
|
|
|
/// Read the Bitcoin RPC password from the secrets file.
|
|
|
|
|
/// Falls back to env var (dev), then generates and persists a random password.
|
2026-03-18 00:39:52 +00:00
|
|
|
async fn read_password() -> String {
|
2026-03-20 11:56:20 +00:00
|
|
|
// 1. Secrets file (production)
|
2026-03-18 00:39:52 +00:00
|
|
|
if let Ok(pass) = tokio::fs::read_to_string(SECRETS_PATH).await {
|
|
|
|
|
let pass = pass.trim().to_string();
|
|
|
|
|
if !pass.is_empty() {
|
|
|
|
|
debug!("Bitcoin RPC password loaded from secrets file");
|
|
|
|
|
return pass;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-20 11:56:20 +00:00
|
|
|
// 2. Environment variable (dev)
|
2026-03-18 00:39:52 +00:00
|
|
|
if let Ok(pass) = std::env::var("BITCOIN_RPC_PASSWORD") {
|
|
|
|
|
if !pass.is_empty() {
|
|
|
|
|
debug!("Bitcoin RPC password loaded from env var");
|
|
|
|
|
return pass;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-20 11:56:20 +00:00
|
|
|
// 3. Generate and persist (first boot)
|
2026-03-18 22:41:23 +00:00
|
|
|
let random_pass = generate_random_password();
|
|
|
|
|
if let Some(parent) = std::path::Path::new(SECRETS_PATH).parent() {
|
|
|
|
|
let _ = tokio::fs::create_dir_all(parent).await;
|
|
|
|
|
}
|
|
|
|
|
match tokio::fs::write(SECRETS_PATH, &random_pass).await {
|
|
|
|
|
Ok(_) => {
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
|
{
|
|
|
|
|
use std::os::unix::fs::PermissionsExt;
|
2026-03-22 03:30:21 +00:00
|
|
|
let _ = tokio::fs::set_permissions(
|
2026-03-20 11:56:20 +00:00
|
|
|
SECRETS_PATH,
|
|
|
|
|
std::fs::Permissions::from_mode(0o600),
|
2026-03-22 03:30:21 +00:00
|
|
|
)
|
|
|
|
|
.await;
|
2026-03-18 22:41:23 +00:00
|
|
|
}
|
2026-03-20 11:56:20 +00:00
|
|
|
debug!("Bitcoin RPC password generated and saved");
|
2026-03-18 22:41:23 +00:00
|
|
|
}
|
|
|
|
|
Err(e) => {
|
2026-03-20 11:56:20 +00:00
|
|
|
tracing::warn!("Failed to save Bitcoin RPC password: {}", e);
|
2026-03-18 22:41:23 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
random_pass
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-20 11:56:20 +00:00
|
|
|
/// Generate a cryptographically random password (32 hex chars).
|
2026-08-02 16:27:34 -04:00
|
|
|
///
|
|
|
|
|
/// KEY-05: this is the node's Bitcoin RPC credential, so the source is named and
|
|
|
|
|
/// the 16-byte draw is guarded. It returns a bare `String` and its caller
|
|
|
|
|
/// (`read_password`) is a `OnceCell` initialiser that also returns a bare
|
|
|
|
|
/// `String`, so a degenerate draw aborts rather than propagating — the condition
|
|
|
|
|
/// means the kernel CSPRNG is broken, and a predictable Bitcoin RPC password on
|
|
|
|
|
/// a node that also serves LAN traffic is worse than a loud stop.
|
2026-03-18 22:41:23 +00:00
|
|
|
fn generate_random_password() -> String {
|
2026-08-02 16:27:34 -04:00
|
|
|
let mut bytes = [0u8; 16];
|
|
|
|
|
crate::entropy::draw_key_bytes(&mut rand::rngs::OsRng, &mut bytes).unwrap_or_else(|e| {
|
|
|
|
|
panic!("refusing to generate a Bitcoin RPC password from degenerate entropy: {e} (KEY-05)")
|
|
|
|
|
});
|
2026-03-18 22:41:23 +00:00
|
|
|
hex::encode(bytes)
|
2026-03-18 00:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get Bitcoin RPC credentials (user, password). Cached after first call.
|
|
|
|
|
pub async fn bitcoin_rpc_credentials() -> (String, String) {
|
|
|
|
|
let pass = CACHED_PASSWORD
|
|
|
|
|
.get_or_init(|| async { read_password().await })
|
|
|
|
|
.await;
|
2026-03-20 11:56:20 +00:00
|
|
|
(RPC_USER.to_string(), pass.clone())
|
2026-03-18 00:39:52 +00:00
|
|
|
}
|