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
//! The `email` subcommand: manage a project's SMTP delivery profiles.
//!
//! A profile is the connection config (host / port / security / AUTH) for one SMTP
//! relay plus a default sender. The **password is sealed server-side** with the
//! operator's `[secrets]` key envelope and stored per-project; it never leaves the
//! store over the API — `ls`/`show` return only the redacted config. A guest
//! function/handler *uses* a profile by importing the `email` capability and calling
//! `send`; it can neither read nor reconfigure a profile (that is this command's
//! job, gated by a boatramp token).
//!
//! Scoping follows the uniform project rule: the global `--project` /
//! `BOATRAMP_PROJECT` flag (falling back to `[publish].project`, else `default`)
//! selects the tenant, exactly like `function` / `compute` / `secrets`.
use boatramp_core::email_config::EmailProfileInfo;
use clap::Subcommand;
use crate::client;
use crate::config::ProjectConfig;
/// A failure in the `email` subcommand.
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Resolving the server / building the client failed.
#[error(transparent)]
Client(#[from] crate::client::ClientError),
/// An HTTP request to the control plane failed.
#[error(transparent)]
Http(#[from] reqwest::Error),
/// Reading the password from stdin failed.
#[error("reading SMTP password: {0}")]
Read(#[source] std::io::Error),
/// The server refused the request; carries the status + its (password-free) body
/// so the operator sees the reason (the no-envelope `501`, an invalid config…).
#[error("server returned HTTP {status}: {body}")]
Server { status: u16, body: String },
}
/// `email` module result; `Err` is [`Error`].
type Result<T> = std::result::Result<T, Error>;
/// Arguments for `boatramp email`.
#[derive(Debug, clap::Args)]
pub struct EmailArgs {
/// boatramp server base URL (overrides [publish].server).
#[arg(long, env = "BOATRAMP_SERVER", global = true)]
server: Option<String>,
#[command(subcommand)]
command: EmailCommand,
}
/// How to supply the SMTP AUTH password (mutually exclusive; omit both for an
/// unauthenticated relay). `--password-stdin` is preferred — nothing hits argv.
#[derive(Debug, clap::Args)]
struct PasswordSource {
/// The SMTP AUTH password inline. Convenient, but it lands in your shell history
/// and the process table — prefer `--password-stdin` for anything sensitive.
#[arg(long, group = "password_source", value_name = "PASSWORD")]
password: Option<String>,
/// Read the SMTP AUTH password from standard input (preferred).
#[arg(long, group = "password_source")]
password_stdin: bool,
}
#[derive(Debug, Subcommand)]
enum EmailCommand {
/// Create or **update** an SMTP profile. Fields you pass overwrite; fields you omit keep
/// their stored value — so you can change one parameter (e.g. `--from`) without re-sending
/// the rest, and the sealed password is preserved unless you pass a new one. On create,
/// `--host` and `--from` are required.
Set {
/// The profile name (a guest selects it via the message's `profile`; omit in
/// the guest to use `default`).
name: String,
/// SMTP relay hostname (required on create; omit to keep on update).
#[arg(long)]
host: Option<String>,
/// SMTP relay port; omit to keep (on create, the conventional port for `--security`:
/// 587 starttls / 465 tls / 25 plaintext).
#[arg(long)]
port: Option<u16>,
/// Transport security: `starttls` (587), `tls` (implicit, 465), or `plaintext` (a
/// trusted local relay only). Omit to keep (defaults to `starttls` on create).
#[arg(long)]
security: Option<String>,
/// SMTP AUTH username. Omit to keep; use `--no-auth` to drop it.
#[arg(long)]
username: Option<String>,
#[command(flatten)]
password: PasswordSource,
/// Drop the username + password (an unauthenticated relay).
#[arg(long)]
no_auth: bool,
/// The default (and only permitted) `From` address (required on create; omit to keep).
#[arg(long)]
from: Option<String>,
/// Default sends through this profile to the durable spool (`--durable` = on,
/// `--durable false` = off); omit to keep. A guest can still opt in/out per message.
#[arg(long, num_args = 0..=1, default_missing_value = "true")]
durable: Option<bool>,
},
/// List the project's SMTP profiles (redacted — never the password).
Ls,
/// Show one profile's redacted config.
Show {
/// The profile name.
name: String,
},
/// Remove a profile by name.
Rm {
/// The profile name.
name: String,
},
}
/// Entry point for `boatramp email`.
pub async fn run(args: EmailArgs, config: &ProjectConfig) -> Result<()> {
let server = client::resolve_server(args.server, config)?;
let http = client::http_client(client::token(config).as_deref());
// The project-scoped collection segment (`email` for the default project, else
// `projects/<proj>/email`) — the same `--project` routing as secrets/function.
let seg = client::project_seg(&client::resolve_project(config), "email");
match args.command {
EmailCommand::Set {
name,
host,
port,
security,
username,
password,
no_auth,
from,
durable,
} => {
let password = read_password(password)?;
let resp = http
.put(format!("{server}/api/{seg}/profiles/{name}"))
.json(&SetProfileRequest {
host: host.as_deref(),
port,
security: security.as_deref(),
username: username.as_deref(),
password,
from: from.as_deref(),
durable,
clear_auth: no_auth,
})
.send()
.await?;
let info: EmailProfileInfo = parse_json(resp).await?;
// Never echo the password; confirm by redacted config only.
println!(
"set email profile {} ({} {}:{} from {})",
info.name, info.security, info.host, info.port, info.from
);
}
EmailCommand::Ls => {
let resp = http
.get(format!("{server}/api/{seg}/profiles"))
.send()
.await?;
let profiles: Vec<EmailProfileInfo> = parse_json(resp).await?;
if profiles.is_empty() {
println!("no email profiles");
return Ok(());
}
println!(
"{:<20} {:<28} {:<10} {:<28} DURABLE",
"NAME", "HOST", "SECURITY", "FROM"
);
for p in profiles {
println!(
"{:<20} {:<28} {:<10} {:<28} {}",
p.name,
format!("{}:{}", p.host, p.port),
p.security,
p.from,
p.durable
);
}
}
EmailCommand::Show { name } => {
let resp = http
.get(format!("{server}/api/{seg}/profiles/{name}"))
.send()
.await?;
let p: EmailProfileInfo = parse_json(resp).await?;
println!("name: {}", p.name);
println!("host: {}:{}", p.host, p.port);
println!("security: {}", p.security);
println!(
"username: {}",
p.username.as_deref().unwrap_or("(none — unauthenticated)")
);
println!(
"password: {}",
if p.has_password {
"(set, sealed)"
} else {
"(none)"
}
);
println!("from: {}", p.from);
println!("durable: {}", p.durable);
}
EmailCommand::Rm { name } => {
let resp = http
.delete(format!("{server}/api/{seg}/profiles/{name}"))
.send()
.await?;
check_no_content(resp).await?;
println!("removed email profile {name}");
}
}
Ok(())
}
/// The `set` request body — mirrors the server's `SetEmailProfileRequest`. The
/// server seals `password` and stores the profile under the path `name`.
#[derive(serde::Serialize)]
struct SetProfileRequest<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
host: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
port: Option<u16>,
#[serde(skip_serializing_if = "Option::is_none")]
security: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
username: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
password: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
from: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
durable: Option<bool>,
#[serde(skip_serializing_if = "std::ops::Not::not")]
clear_auth: bool,
}
/// Read the password from stdin / inline / neither (an unauthenticated relay).
fn read_password(source: PasswordSource) -> Result<Option<String>> {
use std::io::Read as _;
if source.password_stdin {
let mut buf = String::new();
std::io::stdin()
.read_to_string(&mut buf)
.map_err(Error::Read)?;
Ok(Some(buf.trim_end_matches('\n').to_string()))
} else if let Some(pw) = source.password {
Ok(Some(pw))
} else {
Ok(None)
}
}
/// Deserialize a JSON success body, mapping a non-2xx status to a legible
/// [`Error::Server`] carrying the server's (password-free) message.
async fn parse_json<T: serde::de::DeserializeOwned>(resp: reqwest::Response) -> Result<T> {
let status = resp.status();
let bytes = resp.bytes().await?;
if !status.is_success() {
return Err(Error::Server {
status: status.as_u16(),
body: String::from_utf8_lossy(&bytes).trim().to_string(),
});
}
serde_json::from_slice(&bytes).map_err(|e| Error::Server {
status: status.as_u16(),
body: format!("could not parse response: {e}"),
})
}
/// Expect a `204 No Content` (or any 2xx); surface a non-success status + body.
async fn check_no_content(resp: reqwest::Response) -> Result<()> {
let status = resp.status();
if status.is_success() {
return Ok(());
}
let bytes = resp.bytes().await?;
Err(Error::Server {
status: status.as_u16(),
body: String::from_utf8_lossy(&bytes).trim().to_string(),
})
}
#[cfg(test)]
mod tests {
use super::*;
use clap::Parser;
#[derive(Parser)]
struct Cli {
#[arg(long, global = true, env = "BOATRAMP_PROJECT")]
project: Option<String>,
#[command(subcommand)]
cmd: Cmd,
}
#[derive(Subcommand)]
enum Cmd {
Email(EmailArgs),
}
fn parse(argv: &[&str]) -> std::result::Result<Cli, clap::Error> {
Cli::try_parse_from(std::iter::once("boatramp").chain(argv.iter().copied()))
}
#[test]
fn set_parses_with_and_without_a_password_source() {
// No password → an unauthenticated relay.
assert!(parse(&[
"email",
"set",
"default",
"--host",
"smtp.example.com",
"--from",
"a@b.com",
])
.is_ok());
// Inline or stdin password parse.
assert!(parse(&[
"email",
"set",
"default",
"--host",
"h",
"--from",
"a@b.com",
"--password",
"pw",
])
.is_ok());
assert!(parse(&[
"email",
"set",
"default",
"--host",
"h",
"--from",
"a@b.com",
"--password-stdin",
])
.is_ok());
// Two password sources at once are mutually exclusive → a parse error.
assert!(parse(&[
"email",
"set",
"default",
"--host",
"h",
"--from",
"a@b.com",
"--password",
"pw",
"--password-stdin",
])
.is_err());
// Partial update: a single field, no host/from (they're optional now — kept on update).
assert!(parse(&["email", "set", "default", "--from", "new@b.com"]).is_ok());
// `--no-auth` (drop credentials) and the tri-state `--durable` (bare = on) parse.
assert!(parse(&["email", "set", "default", "--no-auth"]).is_ok());
assert!(parse(&["email", "set", "default", "--durable"]).is_ok());
assert!(parse(&["email", "set", "default", "--durable", "false"]).is_ok());
}
#[test]
fn ls_show_rm_parse_and_project_flag_reaches_the_subcommand() {
assert!(parse(&["email", "ls"]).is_ok());
assert!(parse(&["email", "show", "default"]).is_ok());
assert!(parse(&["email", "rm", "default"]).is_ok());
let cli = parse(&["email", "--project", "acme", "ls"]).expect("parses");
assert_eq!(cli.project.as_deref(), Some("acme"));
}
}