boatramp 0.2.7

boatramp — self-hosted, streaming-first static site publishing (server + CLI in one binary)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//! The `token` subcommand: manage control-plane tokens.
//!
//! Tokens are minted **server-side** (the issuing node holds the root private
//! key) and returned once; only their metadata (`authz/tokens/<id>`) is stored,
//! so `ls` reports the metadata and `rm` revokes by the metadata id.

use boatramp_core::authz::{GrantedRole, TokenMeta};
use clap::Subcommand;
use serde::Deserialize;

use crate::client;
use crate::config::ProjectConfig;

/// A failure in the `token` subcommand.
#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// Resolving the server failed.
    #[error(transparent)]
    Client(#[from] crate::client::ClientError),
    /// An HTTP request to the control plane failed.
    #[error(transparent)]
    Http(#[from] reqwest::Error),
    /// A key or credential for offline attenuation failed to parse / sign.
    #[error("delegation: {0}")]
    Delegation(String),
    /// `token bootstrap` was run without the bootstrap secret in the environment.
    #[error(
        "set BOATRAMP_BOOTSTRAP_SECRET to the secret configured on the server \
         (the value passed to `serve --bootstrap-secret`)"
    )]
    MissingBootstrapSecret,
    /// `token mint` could not resolve a signer (no env key, no `[serve.signer]` /
    /// `auth_root_private_key` in the config).
    #[error(
        "no signer for offline mint: set BOATRAMP_AUTH_ROOT_PRIVATE_KEY, or point \
         --config at a boatramp.cfg with [serve.signer] / auth_root_private_key"
    )]
    MissingSigner,
    /// Building / loading the offline-mint signer failed.
    #[error("signer: {0}")]
    Signer(String),
}

/// `token` module result; `Err` is [`Error`].
type Result<T> = std::result::Result<T, Error>;

/// Arguments for `boatramp token`.
#[derive(Debug, clap::Args)]
pub struct TokenArgs {
    /// boatramp server base URL (overrides [deploy].server).
    #[arg(long, env = "BOATRAMP_SERVER", global = true)]
    server: Option<String>,

    #[command(subcommand)]
    command: TokenCommand,
}

#[derive(Debug, Subcommand)]
enum TokenCommand {
    /// Mint a new token (printed once — not recoverable).
    Create {
        /// Human label for the token.
        label: String,
        /// Role (repeatable): `<role>` (global) or `<role>:<site>` (scoped) —
        /// e.g. `admin`, `publisher:blog`, `viewer:blog`.
        #[arg(long = "role", required = true)]
        roles: Vec<String>,
        /// Optional time-to-live in seconds (omit for no expiry).
        #[arg(long)]
        ttl_secs: Option<u64>,
        /// Make the token **delegatable**: embed this holder public key
        /// (`"<alg>:<hex>"`, from `boatramp auth init`) as the `cnf`, so the holder
        /// of the matching private key can `token attenuate` it offline.
        #[arg(long)]
        holder_pub: Option<String>,
        /// Make the token **PoP-bound** (DPoP): generate a fresh holder keypair,
        /// mint the token against its public half (`cnf`), and print the holder
        /// **private** key as `BOATRAMP_TOKEN_HOLDER_KEY` so the client can sign a
        /// per-request proof. A leaked token alone is then inert without this key.
        /// Also set `BOATRAMP_POP_ORIGIN` to the server's `[serve] pop_origin`.
        #[arg(long, conflicts_with = "holder_pub")]
        pop: bool,
    },
    /// Mint the FIRST control-plane token on a fresh deploy by presenting the
    /// single-use bootstrap secret (`BOATRAMP_BOOTSTRAP_SECRET`) — no admin token
    /// needed. The server mints via its own root key (nothing sensitive leaves it)
    /// and records the token (revocable). Use it to configure + mint scoped tokens,
    /// then unset the secret on the server.
    Bootstrap {
        /// Role for the bootstrap token (repeatable). Defaults to `admin`.
        #[arg(long = "role")]
        roles: Vec<String>,
        /// Optional time-to-live in seconds (default: 3600 = 1 h).
        #[arg(long)]
        ttl_secs: Option<u64>,
    },
    /// Mint a token **offline** — sign locally via the configured signer (local key
    /// or KMS/HSM), no server. The key-holder's recovery / air-gap path; prefer
    /// `token bootstrap` for the normal first-token flow. The token is unlisted (no
    /// server round-trip records it) but still revocable by its id.
    Mint {
        /// Role (repeatable): `<role>` (global) or `<role>:<site>` (scoped).
        #[arg(long = "role", required = true)]
        roles: Vec<String>,
        /// Optional time-to-live in seconds (omit for no expiry).
        #[arg(long)]
        ttl_secs: Option<u64>,
        /// Make it delegatable: embed this holder public key (`"<alg>:<hex>"`) as
        /// the `cnf` so it can be narrowed with `token attenuate`.
        #[arg(long)]
        holder_pub: Option<String>,
        /// `boatramp.cfg` supplying the signer (`[serve.signer]` /
        /// `auth_root_private_key`). Ignored when `BOATRAMP_AUTH_ROOT_PRIVATE_KEY`
        /// is set in the environment.
        #[arg(long, default_value = "boatramp.cfg")]
        config: std::path::PathBuf,
    },
    /// Narrow a (delegatable) token **offline** — no server, no root key — by
    /// signing a restrict-only delegation block with the holder key. The
    /// result is a longer credential; present it in place of the original.
    Attenuate {
        /// The token / credential to narrow.
        credential: String,
        /// The holder private key (`"<alg>:<hex>"`) that the parent block's `cnf`
        /// authorized.
        #[arg(long, env = "BOATRAMP_HOLDER_KEY")]
        holder_key: String,
        /// Restrict to a single site (denies every other target).
        #[arg(long)]
        only_site: Option<String>,
        /// Restrict to read-only operations.
        #[arg(long)]
        read_only: bool,
        /// Shorten the lifetime to this Unix second.
        #[arg(long)]
        not_after: Option<u64>,
        /// Permit one further attenuation by this holder public key
        /// (`"<alg>:<hex>"`); omit to make this the last block.
        #[arg(long)]
        next_holder_pub: Option<String>,
    },
    /// List issued tokens (short id, label, roles, expiry).
    Ls,
    /// Revoke a token by its id or a unique id prefix.
    Rm {
        /// Token id (or prefix).
        id: String,
    },
}

#[derive(serde::Serialize)]
struct CreateRequest {
    label: String,
    roles: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    ttl_secs: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    holder_pubkey: Option<String>,
}

#[derive(Deserialize)]
struct CreateResponse {
    token: String,
    id: String,
}

#[derive(serde::Serialize)]
struct BootstrapRequest {
    roles: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    ttl_secs: Option<u64>,
}

fn render_role(role: &GrantedRole) -> String {
    match &role.target {
        Some(t) => format!("{}:{}", role.name, t),
        None => role.name.clone(),
    }
}

/// Entry point for `boatramp token`.
pub async fn run(args: TokenArgs, config: &ProjectConfig) -> Result<()> {
    // Attenuation is fully offline — no server or root key needed. Handle it
    // before resolving the control-plane endpoint.
    if let TokenCommand::Attenuate {
        credential,
        holder_key,
        only_site,
        read_only,
        not_after,
        next_holder_pub,
    } = &args.command
    {
        return attenuate(
            credential,
            holder_key,
            only_site.clone(),
            *read_only,
            *not_after,
            next_holder_pub.as_deref(),
        )
        .await;
    }

    // Bootstrap authenticates with the single-use bootstrap secret (not the
    // config/admin token), so handle it before the shared token-bearing client.
    if let TokenCommand::Bootstrap { roles, ttl_secs } = &args.command {
        return bootstrap(args.server.clone(), config, roles, *ttl_secs).await;
    }

    // Offline mint signs locally via the configured signer — no server.
    if let TokenCommand::Mint {
        roles,
        ttl_secs,
        holder_pub,
        config: signer_config,
    } = &args.command
    {
        return mint_offline(roles, *ttl_secs, holder_pub.as_deref(), signer_config).await;
    }

    let server = client::resolve_server(args.server, config)?;
    let http = client::http_client(client::token(config).as_deref());

    match args.command {
        TokenCommand::Create {
            label,
            roles,
            ttl_secs,
            holder_pub,
            pop,
        } => {
            // `--pop`: generate a holder keypair and bind the token to its public
            // half; the printed private key is the per-request signing key.
            let pop_holder = pop.then(|| {
                boatramp_core::cose::LocalSigner::generate(boatramp_core::cose::TokenAlg::Es256)
            });
            let holder_pubkey = match &pop_holder {
                Some(holder) => {
                    use boatramp_core::cose::Signer;
                    Some(holder.public_key().to_hex())
                }
                None => holder_pub,
            };
            let response: CreateResponse = http
                .post(format!("{server}/api/tokens"))
                .json(&CreateRequest {
                    label,
                    roles,
                    ttl_secs,
                    holder_pubkey,
                })
                .send()
                .await?
                .error_for_status()?
                .json()
                .await?;
            if let Some(holder) = pop_holder {
                // Print both secrets as ready-to-export shell lines (matching
                // `auth init`), guidance to stderr.
                println!("BOATRAMP_TOKEN={}", response.token);
                println!("BOATRAMP_TOKEN_HOLDER_KEY={}", holder.private_hex());
                eprintln!("id: {}", response.id);
                eprintln!(
                    "PoP-bound token. Also set BOATRAMP_POP_ORIGIN to the server's \
                     [serve] pop_origin so the client binds the right origin."
                );
                eprintln!("store both secrets now — they cannot be recovered");
            } else {
                println!("{}", response.token);
                eprintln!("id: {}", response.id);
                eprintln!("store the token now — it cannot be recovered");
            }
        }
        TokenCommand::Attenuate { .. }
        | TokenCommand::Bootstrap { .. }
        | TokenCommand::Mint { .. } => {
            unreachable!("handled above")
        }
        TokenCommand::Ls => {
            let tokens: Vec<TokenMeta> = http
                .get(format!("{server}/api/tokens"))
                .send()
                .await?
                .error_for_status()?
                .json()
                .await?;
            if tokens.is_empty() {
                println!("no tokens");
                return Ok(());
            }
            for token in tokens {
                let roles = token
                    .roles
                    .iter()
                    .map(render_role)
                    .collect::<Vec<_>>()
                    .join(",");
                let short = &token.revocation_id[..token.revocation_id.len().min(12)];
                let expiry = match token.expires_at {
                    Some(t) => format!("  expires@{t}"),
                    None => String::new(),
                };
                println!("{short}  {}  [{roles}]{expiry}", token.label);
            }
        }
        TokenCommand::Rm { id } => {
            http.delete(format!("{server}/api/tokens/{id}"))
                .send()
                .await?
                .error_for_status()?;
            println!("revoked {id}");
        }
    }
    Ok(())
}

use boatramp_core::time::now_unix;

/// `token mint`: sign a control-plane token offline via the configured signer.
async fn mint_offline(
    roles: &[String],
    ttl_secs: Option<u64>,
    holder_pub: Option<&str>,
    signer_config: &std::path::Path,
) -> Result<()> {
    use boatramp_core::cose::{self, Claims};
    let signer = resolve_signer(signer_config).await?;
    let claims = Claims {
        roles: roles
            .iter()
            .map(|s| boatramp_core::authz::GrantedRole::parse(s))
            .collect(),
        kind: cose::KIND_ROLE.to_string(),
        ttl_secs,
        now_unix: now_unix(),
    };
    let token = match holder_pub {
        Some(hex) => {
            let holder = cose::TokenPublicKey::from_hex(hex)
                .map_err(|e| Error::Delegation(e.to_string()))?;
            cose::mint_delegatable(&claims, &holder, &*signer).await
        }
        None => cose::mint(&claims, &*signer).await,
    }
    .map_err(|e| Error::Delegation(e.to_string()))?;
    println!("{token}");
    eprintln!("minted offline — unlisted (no server record) but revocable by id");
    Ok(())
}

/// Resolve the offline-mint signer: `BOATRAMP_AUTH_ROOT_PRIVATE_KEY` (local) first,
/// else `[serve.signer]` / `auth_root_private_key` from `boatramp.cfg` — so KMS/HSM
/// works identically to the server.
async fn resolve_signer(
    config_path: &std::path::Path,
) -> Result<std::sync::Arc<dyn boatramp_core::cose::Signer>> {
    use boatramp_core::cose::{LocalSigner, Signer};
    use std::sync::Arc;
    if let Ok(hex) = std::env::var("BOATRAMP_AUTH_ROOT_PRIVATE_KEY") {
        if !hex.is_empty() {
            let signer =
                LocalSigner::from_private_hex(&hex).map_err(|e| Error::Signer(e.to_string()))?;
            return Ok(Arc::new(signer) as Arc<dyn Signer>);
        }
    }
    let serve = crate::config::ServerConfig::load(config_path)
        .map_err(|e| Error::Signer(e.to_string()))?
        .serve
        .unwrap_or_default();
    if let Some(sig) = serve.signer {
        return boatramp_server::signer::build_signer(&sig.to_signer_config())
            .await
            .map_err(|e| Error::Signer(e.to_string()));
    }
    if let Some(hex) = serve.auth_root_private_key {
        let signer =
            LocalSigner::from_private_hex(&hex).map_err(|e| Error::Signer(e.to_string()))?;
        return Ok(Arc::new(signer) as Arc<dyn Signer>);
    }
    Err(Error::MissingSigner)
}

/// `token bootstrap`: mint the first control-plane token by presenting the
/// single-use bootstrap secret (`BOATRAMP_BOOTSTRAP_SECRET`) as the bearer to the
/// RBAC-exempt `/api/tokens/bootstrap` route (the handler verifies the secret).
async fn bootstrap(
    server: Option<String>,
    config: &ProjectConfig,
    roles: &[String],
    ttl_secs: Option<u64>,
) -> Result<()> {
    let server = client::resolve_server(server, config)?;
    let secret = std::env::var("BOATRAMP_BOOTSTRAP_SECRET")
        .ok()
        .filter(|s| !s.is_empty())
        .ok_or(Error::MissingBootstrapSecret)?;
    let http = client::http_client(Some(secret.as_str()));
    let response: CreateResponse = http
        .post(format!("{server}/api/tokens/bootstrap"))
        .json(&BootstrapRequest {
            roles: roles.to_vec(),
            ttl_secs,
        })
        .send()
        .await?
        .error_for_status()?
        .json()
        .await?;
    println!("{}", response.token);
    eprintln!("id: {}", response.id);
    eprintln!(
        "store the token now — it cannot be recovered; then unset the bootstrap \
         secret on the server"
    );
    Ok(())
}

/// `token attenuate`: sign a restrict-only delegation block offline and print the
/// narrowed credential.
async fn attenuate(
    credential: &str,
    holder_key: &str,
    only_site: Option<String>,
    read_only: bool,
    not_after: Option<u64>,
    next_holder_pub: Option<&str>,
) -> Result<()> {
    use boatramp_core::cose::{self, Caveats, LocalSigner, TokenPublicKey};

    let signer = LocalSigner::from_private_hex(holder_key)
        .map_err(|e| Error::Delegation(format!("holder key: {e}")))?;
    let next = next_holder_pub
        .map(TokenPublicKey::from_hex)
        .transpose()
        .map_err(|e| Error::Delegation(format!("next holder key: {e}")))?;
    let caveats = Caveats::restrict(only_site, read_only, not_after);
    if caveats.is_empty() {
        return Err(Error::Delegation(
            "an attenuation must narrow something (--only-site / --read-only / --not-after)".into(),
        ));
    }
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0);
    let narrowed = cose::attenuate(credential, &signer, &caveats, next.as_ref(), now)
        .await
        .map_err(|e| Error::Delegation(e.to_string()))?;
    println!("{narrowed}");
    Ok(())
}