Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# The build context of packaging/docker/Dockerfile is the repository root, because Cargo needs the manifests of
# every workspace member. Nothing here may reach the image: no history, no local data, no secrets.
.git
.github
target
**/target
/dist
/temp
/data
docs
packaging
**/*.md
.env
.env.*
**/.env
**/.env.*
*.sqlite
*.sqlite-wal
*.sqlite-shm
*~
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ target/
*.sqlite-shm
# a snapshot test's own render when it doesn't match
*.new.png
# the Docker deployment's settings and secrets (written by packaging/docker/install.sh), and a custom TLS certificate
/packaging/docker/.env
/packaging/docker/tls/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ that is signed in, and enter its code on the new one.
## Documentation

- [Installing the Craft Cloud app](docs/install.md) on Windows, macOS and Linux
- [Running the server](docs/server.md): reverse proxy, Postgres and S3, accounts, invites, backups
- [Running the server](docs/server.md): reverse proxy, Postgres and S3, accounts, invites, backups, Docker
- [The agent on the command line](docs/agent.md): `login`, `run`, `sync`, `join`, start at login
- [Development](docs/development.md): how it fits together, building, testing, the sync engine's rules
- [The app kit](docs/kit.md): how a Craft app makes its documents live
Expand Down
63 changes: 62 additions & 1 deletion apps/craft-cloud-server/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
//! appear in the process list or shell history: `printf '%s\n' "$PASSWORD" | craft-cloud-server user add …`.

use std::io::{BufRead, Read, Write};
use std::net::{IpAddr, SocketAddr};
use std::net::{IpAddr, SocketAddr, TcpStream};
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};

use clap::{Parser, Subcommand};
use craft_cloud_meta::{Meta, Role, valid_username};
Expand Down Expand Up @@ -85,6 +86,13 @@ pub enum Command {
#[command(subcommand)]
action: LiveAction,
},
/// Ask a running server whether it answers (`GET /api/info`). Prints nothing and exits 0 if it does; otherwise
/// exits 1 with one line on standard error. For a container's health check, which has no shell and no `curl`.
Healthcheck {
/// The address the server listens on, as seen from here.
#[arg(long, default_value = "127.0.0.1:8080")]
addr: String,
},
}

#[derive(Debug, Subcommand)]
Expand Down Expand Up @@ -179,6 +187,11 @@ pub async fn run(command: Command, input: &mut impl BufRead, out: &mut impl Writ
init(&dir, &admin, &listen, (behind_proxy, &trusted_proxies), input, out).await
}
Command::Serve { .. } => Err(CliError::Usage("serve is run by main".into())),
Command::Healthcheck { addr } => {
let addr: SocketAddr = addr.parse().map_err(|_| CliError::Usage(format!("bad address {addr}")))?;
// Blocking for at most `HEALTH_TIMEOUT`: this process does nothing else.
check_health(addr, HEALTH_TIMEOUT)
}
Command::User { config, action } => {
let config = Config::load(&config_path(config)?)?;
let (meta, _) = open(&config).await?;
Expand Down Expand Up @@ -259,6 +272,46 @@ pub async fn run(command: Command, input: &mut impl BufRead, out: &mut impl Writ
}
}

/// How long `healthcheck` waits for the whole exchange: connecting, sending and reading the status line.
pub const HEALTH_TIMEOUT: Duration = Duration::from_secs(3);

/// Sends `GET /api/info` to `addr` and succeeds only if the status line says 200. The whole exchange must finish
/// within `timeout`. The body is never read, and nothing the server sends is printed.
pub fn check_health(addr: SocketAddr, timeout: Duration) -> Result<(), CliError> {
let fail = |what: &str| CliError::Usage(format!("{addr} is not healthy: {what}"));
let deadline = Instant::now() + timeout;
let left = || deadline.checked_duration_since(Instant::now()).filter(|d| !d.is_zero());
let mut stream = TcpStream::connect_timeout(&addr, timeout).map_err(|e| fail(&format!("can't connect: {e}")))?;
let request = format!("GET /api/info HTTP/1.0\r\nHost: {addr}\r\nConnection: close\r\n\r\n");
stream.set_write_timeout(left()).map_err(|e| fail(&e.to_string()))?;
stream.write_all(request.as_bytes()).map_err(|e| fail(&format!("can't send the request: {e}")))?;
// The status line fits in 64 bytes. It counts only when it is complete (ends in CRLF): an answer that stops inside
// it, or goes on for 64 bytes without a line end, is not a healthy server.
let mut head = [0u8; 64];
let mut len = 0;
let line_end = loop {
if let Some(end) = head.get(..len).and_then(|read| read.windows(2).position(|pair| pair == b"\r\n")) {
break end;
}
let wait = left().ok_or_else(|| fail("no answer in time"))?;
stream.set_read_timeout(Some(wait)).map_err(|e| fail(&e.to_string()))?;
let Some(room) = head.get_mut(len..).filter(|room| !room.is_empty()) else { return Err(fail("not an HTTP answer")) };
match stream.read(room) {
Ok(0) => return Err(fail("the answer ended inside the status line")),
Ok(n) => len += n,
Err(e) if matches!(e.kind(), std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut) => return Err(fail("no answer in time")),
Err(e) => return Err(fail(&format!("can't read the answer: {e}"))),
}
};
let line = String::from_utf8_lossy(head.get(..line_end).unwrap_or_default());
let mut parts = line.split_whitespace();
match (parts.next(), parts.next()) {
(Some(version), Some("200")) if version.starts_with("HTTP/1.") => Ok(()),
(Some(version), Some(status)) if version.starts_with("HTTP/") => Err(fail(&format!("status {}", status.chars().take(3).collect::<String>()))),
_ => Err(fail("not an HTTP answer")),
}
}

/// `user invite`: makes the invite and prints how to use it. The code is printed only here, never logged.
async fn invite(meta: &Meta, config: &Config, username: &str, out: &mut impl Write) -> Result<(), CliError> {
let code = Code::generate(INVITE_CHARS).map_err(|e| CliError::Usage(format!("the system random source failed: {e}")))?;
Expand Down Expand Up @@ -513,6 +566,14 @@ mod tests {
}
}

#[test]
fn healthcheck_parses_with_and_without_an_address() {
let cli = Cli::try_parse_from(["craft-cloud-server", "healthcheck"]).unwrap();
assert!(matches!(&cli.command, Command::Healthcheck { addr } if addr == "127.0.0.1:8080"));
let cli = Cli::try_parse_from(["craft-cloud-server", "healthcheck", "--addr", "127.0.0.1:9000"]).unwrap();
assert!(matches!(&cli.command, Command::Healthcheck { addr } if addr == "127.0.0.1:9000"));
}

#[test]
fn reset_epoch_takes_a_username_or_all_but_not_both_or_neither() {
assert!(Cli::try_parse_from(["craft-cloud-server", "reset-epoch", "--config", "c.toml", "--all"]).is_ok());
Expand Down
121 changes: 121 additions & 0 deletions apps/craft-cloud-server/tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,124 @@ fn the_binary_serves_what_init_created() {
assert!(body.starts_with("HTTP/1.1 200"), "{body}");
assert!(body.contains("\"chunker_params\":[\"fastcdc-v1\"]"), "{body}");
}

/// A listener on a free loopback port that answers every connection with `reply` (or, with `None`, reads the
/// request and then stays silent until `hold` has passed), for `healthcheck` to ask.
fn fake_server(reply: Option<&'static str>, hold: Duration) -> std::net::SocketAddr {
use std::io::{Read, Write};
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let addr = listener.local_addr().unwrap();
std::thread::spawn(move || {
for conn in listener.incoming() {
let Ok(mut conn) = conn else { return };
let mut request = [0u8; 512];
let _ = conn.read(&mut request);
match reply {
Some(text) => {
let _ = conn.write_all(text.as_bytes());
}
None => std::thread::sleep(hold),
}
}
});
addr
}

fn healthcheck(addr: std::net::SocketAddr) -> std::process::Output {
Process::new(env!("CARGO_BIN_EXE_craft-cloud-server")).args(["healthcheck", "--addr", &addr.to_string()]).output().unwrap()
}

#[test]
fn healthcheck_succeeds_only_on_a_200() {
let ok = fake_server(Some("HTTP/1.1 200 OK\r\ncontent-length: 2\r\n\r\n{}"), Duration::ZERO);
let out = healthcheck(ok);
assert!(out.status.success());
assert!(out.stdout.is_empty() && out.stderr.is_empty());

let broken = fake_server(Some("HTTP/1.1 500 Internal Server Error\r\ncontent-length: 0\r\n\r\n"), Duration::ZERO);
let out = healthcheck(broken);
assert_eq!(out.status.code(), Some(1));
assert!(out.stdout.is_empty());
let stderr = String::from_utf8(out.stderr).unwrap();
assert_eq!(stderr.lines().count(), 1, "{stderr}");
assert!(stderr.contains("status 500") && !stderr.contains("panicked"), "{stderr}");

let not_http = fake_server(Some("hello there\r\n"), Duration::ZERO);
let out = healthcheck(not_http);
assert_eq!(out.status.code(), Some(1));
assert!(String::from_utf8(out.stderr).unwrap().contains("not an HTTP answer"));
}

#[test]
fn healthcheck_needs_a_complete_status_line() {
// Stops inside the status line, with and without closing the connection later.
let truncated = fake_server(Some("HTTP/1.1 200"), Duration::ZERO);
let out = healthcheck(truncated);
assert_eq!(out.status.code(), Some(1));
assert!(String::from_utf8(out.stderr).unwrap().contains("ended inside the status line"));

// More than the buffer holds without a line end.
let endless = fake_server(Some("HTTP/1.1 200 OK xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"), Duration::ZERO);
let out = healthcheck(endless);
assert_eq!(out.status.code(), Some(1));

// A bare line feed is not a status line end.
let bare = fake_server(Some("HTTP/1.1 200 OK\n"), Duration::ZERO);
assert_eq!(healthcheck(bare).status.code(), Some(1));
}

#[test]
fn healthcheck_fails_with_nobody_listening_and_with_a_bad_address() {
let port = std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port();
let out = healthcheck(std::net::SocketAddr::from(([127, 0, 0, 1], port)));
assert_eq!(out.status.code(), Some(1));
let stderr = String::from_utf8(out.stderr).unwrap();
assert!(stderr.contains("can't connect") && !stderr.contains("panicked"), "{stderr}");

let out = Process::new(env!("CARGO_BIN_EXE_craft-cloud-server")).args(["healthcheck", "--addr", "not an address"]).output().unwrap();
assert_eq!(out.status.code(), Some(1));
assert!(String::from_utf8(out.stderr).unwrap().contains("bad address"));
}

#[test]
fn healthcheck_gives_up_on_an_answer_that_never_comes() {
// Called in-process with a short timeout so the test stays quick; the binary uses `HEALTH_TIMEOUT`.
let silent = fake_server(None, Duration::from_secs(5));
let started = Instant::now();
let result = craft_cloud_server::cli::check_health(silent, Duration::from_millis(300));
assert!(matches!(result, Err(CliError::Usage(m)) if m.contains("no answer in time")));
assert!(started.elapsed() < Duration::from_secs(3));

let started = Instant::now();
let out = healthcheck(silent);
assert_eq!(out.status.code(), Some(1));
assert!(started.elapsed() < Duration::from_secs(4), "{:?}", started.elapsed());
}

#[test]
fn healthcheck_is_ok_against_the_real_server() {
let parent = tempfile::tempdir().unwrap();
let dir = parent.path().join("data");
let port = std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port();
let exe = env!("CARGO_BIN_EXE_craft-cloud-server");
let mut init = Process::new(exe)
.args(["init", "--dir", dir.to_str().unwrap(), "--admin", "ana", "--listen", &format!("127.0.0.1:{port}")])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.unwrap();
use std::io::Write;
init.stdin.take().unwrap().write_all(b"admin password 1\n").unwrap();
assert!(init.wait().unwrap().success());
let _server =
Server(Process::new(exe).args(["serve", "--config", dir.join("config.toml").to_str().unwrap()]).stderr(Stdio::null()).spawn().unwrap());
let addr = std::net::SocketAddr::from(([127, 0, 0, 1], port));
let deadline = Instant::now() + Duration::from_secs(20);
loop {
if healthcheck(addr).status.success() {
return;
}
assert!(Instant::now() < deadline, "the server never became healthy");
std::thread::sleep(Duration::from_millis(100));
}
}
Loading