diff --git a/crates/berth-api/src/types.rs b/crates/berth-api/src/types.rs index 043f7f8..0faf409 100644 --- a/crates/berth-api/src/types.rs +++ b/crates/berth-api/src/types.rs @@ -1643,6 +1643,38 @@ pub struct Applied { pub plugins: Vec, #[serde(default, skip_serializing_if = "Vec::is_empty")] pub offers: Vec, + /// The host's device on the tailnet, as its Tailscale client saw it at + /// the end of the apply, when it is on one. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub tailnet: Option, +} + +/// A host's device on the tailnet, from `tailscale status --json`. It +/// holds public values only, which the control server's API finds it by. +#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, ToSchema)] +pub struct TailnetDevice { + /// The device's stable id: Tailscale's `nodeId`, or the number of + /// Headscale's node. + pub id: String, + /// Its MagicDNS name, such as `studio.example.ts.net`, without the final dot. + #[serde(default)] + pub dns_name: String, + /// The machine's own hostname, as the client reports it. + #[serde(default)] + pub host_name: String, + /// Its addresses on the tailnet. + #[serde(default)] + pub addresses: Vec, + /// The tailnet's name, when the client knows it. + #[serde(default, skip_serializing_if = "String::is_empty")] + pub tailnet: String, +} + +impl TailnetDevice { + /// The first label of its MagicDNS name: its name on the tailnet. + pub fn name(&self) -> &str { + self.dns_name.split('.').next().unwrap_or_default() + } } /// Provisioners a feature of a host offers, as its tasks found them: berth diff --git a/crates/berth-core/src/apply.rs b/crates/berth-core/src/apply.rs index 4e3607a..9634fa6 100644 --- a/crates/berth-core/src/apply.rs +++ b/crates/berth-core/src/apply.rs @@ -360,6 +360,9 @@ pub fn settle_with(paths: &Paths, host: &Host, out: &mut dyn Write) -> Result<() let _ = writeln!(out, "! {error}"); } } + for line in settle_tailnet(paths, host) { + let _ = writeln!(out, "{line}"); + } let Some(mut profile) = host.place.load(paths)? else { return Ok(()); }; @@ -385,6 +388,35 @@ pub fn settle_with(paths: &Paths, host: &Host, out: &mut dyn Write) -> Result<() Ok(()) } +/// After a successful apply of a host that joins the tailnet: record its +/// device, and rename it when a rename asked for that. Only with the API +/// key, as berth does nothing with the device without it; a host applied +/// before the key was set is looked up by its address or hostname instead. +fn settle_tailnet(paths: &Paths, host: &Host) -> Vec { + if !berth_api::profile::joins_tailnet(&host.config) + || crate::tailnet::Control::configured(paths) + .device_problem() + .is_some() + { + return vec![]; + } + let reported = match crate::node::applied(paths, host) { + Ok(applied) => applied.and_then(|applied| applied.tailnet), + Err(error) => { + return vec![format!( + "! {} did not say which device it is on the tailnet, so berth has not recorded it: {error}", + host.name + )]; + } + }; + crate::tailnet::after_apply(paths, host, reported).unwrap_or_else(|error| { + vec![format!( + "! berth did not record {}'s device on the tailnet: {error}", + host.name + )] + }) +} + /// After a successful apply of a node: register the provisioners its /// features offer, and forget those they no longer do. Says what it did, a /// line each. diff --git a/crates/berth-core/src/host_type.rs b/crates/berth-core/src/host_type.rs index 28426f3..5d419cd 100644 --- a/crates/berth-core/src/host_type.rs +++ b/crates/berth-core/src/host_type.rs @@ -294,7 +294,7 @@ pub fn rename_action() -> Action { berth_api::Argument { name: "hostname".into(), label: "Hostname too".into(), - help: "Make the new name the profile's hostname as well. The machine takes it at its next apply.".into(), + help: "Make the new name the profile's hostname as well. The machine takes it at its next apply, and so does its device on the tailnet.".into(), default: "no".into(), values: vec!["no".into(), "yes".into()], ..Default::default() diff --git a/crates/berth-core/src/node.rs b/crates/berth-core/src/node.rs index 91b915f..fe5ff22 100644 --- a/crates/berth-core/src/node.rs +++ b/crates/berth-core/src/node.rs @@ -652,6 +652,14 @@ pub fn rename(paths: &Paths, old: &str, new: &str) -> Result<()> { /// What the node `host` reported at the end of its last apply, asked of its /// controller until it answers: the provisioners its features offer. pub fn reported(paths: &Paths, host: &Host) -> Result> { + Ok(applied(paths, host)? + .map(|applied| applied.offers) + .unwrap_or_default()) +} + +/// The record the last apply left on `host`, asked of its controller until +/// it answers, as the controller may restart at the end of an apply. +pub fn applied(paths: &Paths, host: &Host) -> Result> { let password = host.kept_sudo_password(paths); let link = crate::link::Link::new(host.link(paths, password.as_deref())?); let runtime = crate::link::runtime()?; @@ -662,12 +670,7 @@ pub fn reported(paths: &Paths, host: &Host) -> Result> { tokio::time::timeout(std::time::Duration::from_secs(15), link.version()).await }); match asked { - Ok(Ok(version)) => { - return Ok(version - .applied - .map(|applied| applied.offers) - .unwrap_or_default()); - } + Ok(Ok(version)) => return Ok(version.applied), Ok(Err(error)) if started.elapsed() >= patience => { return Err(error.to_string().into()); } diff --git a/crates/berth-core/src/profiles.rs b/crates/berth-core/src/profiles.rs index da155d0..3956565 100644 --- a/crates/berth-core/src/profiles.rs +++ b/crates/berth-core/src/profiles.rs @@ -196,8 +196,9 @@ pub fn list(paths: &Paths, scope: &str) -> Result> { } /// Forget a host: its profile, its generated passwords, the record of a -/// machine of it that was never made, what berth knows of its SSH and the passwords it keeps for -/// logging in, in one change. A profile in a file is removed with the file. +/// machine of it that was never made, what berth knows of its SSH and of its +/// device on the tailnet, and the passwords it keeps for logging in, in one +/// change. The device stays on the tailnet. A profile in a file is removed with the file. pub fn remove(paths: &Paths, place: &Place) -> Result<()> { let (scope, name) = place.side(); // The provisioners a node offers go with it, unless they still hold resources. @@ -215,6 +216,7 @@ pub fn remove(paths: &Paths, place: &Place) -> Result<()> { let mut removals = vec![ (kind::CREDENTIALS, scope.as_str(), name.as_str()), (kind::ACCESS, scope.as_str(), name.as_str()), + (kind::TAILNET, scope.as_str(), name.as_str()), ]; if !machine.is_empty() { removals.push((kind::RESOURCE, scope.as_str(), machine.as_str())); @@ -255,10 +257,11 @@ pub fn hostname_allowed(name: &str, profile: &Value) -> Result<()> { /// Give a saved host another name in its scope: its profile, its generated /// passwords, what berth knows of its SSH, the passwords it keeps for logging -/// in and the resources that belong to it, in one change. A host of this -/// installation's own takes its messages and jobs along. The machine stays as -/// it is, and so does its hostname unless `hostname` asks for the profile's to -/// be the new name, which an apply then gives the machine. Returns the new place. +/// in, its device on the tailnet and the resources that belong to it, in one +/// change. A host of this installation's own takes its messages and jobs +/// along. The machine stays as it is, and so does its hostname unless +/// `hostname` asks for the profile's to be the new name, which an apply then +/// gives the machine and its device on the tailnet. Returns the new place. pub fn rename(paths: &Paths, from: &Place, to: &str, hostname: bool) -> Result { let Place::Stored { scope, name: old } = from else { return Err("A profile in a file is renamed by moving the file".into()); @@ -294,6 +297,7 @@ pub fn rename(paths: &Paths, from: &Place, to: &str, hostname: bool) -> Result

Result

self.headscale_key(url, &login, lifetime), + None => self.tailscale_key(&login, description, lifetime), + } + } + + /// How curl logs in to the API: a line of its configuration. An OAuth + /// client is exchanged for an access token first. + fn login(&self) -> Result { let credential = self.credential.as_ref().map_err(String::clone)?; match (&self.headscale, credential) { - (Some(url), Credential::Token(token)) => self.headscale_key(url, token, lifetime), - (None, Credential::Token(token)) => { - self.tailscale_key(&format!("user = \"{token}:\""), description, lifetime) + (Some(_), Credential::Token(token)) => { + Ok(format!("header = \"Authorization: Bearer {token}\"")) } + // The access token is the user name of HTTP basic authentication. + (None, Credential::Token(token)) => Ok(format!("user = \"{token}:\"")), (None, Credential::Client { id, secret }) => { let token = self.exchange(id, secret)?; - let login = format!("header = \"Authorization: Bearer {token}\""); - self.tailscale_key(&login, description, lifetime) + Ok(format!("header = \"Authorization: Bearer {token}\"")) } (Some(_), Credential::Client { .. }) => Err("Headscale has no OAuth clients".into()), } } + /// Why berth cannot change devices at this control server, if it cannot: + /// that needs the key alone, not the user or tags of new hosts. + pub fn device_problem(&self) -> Option { + match (&self.headscale, &self.credential) { + (_, Err(why)) => Some(why.clone()), + (Some(_), Ok(Credential::Client { .. })) => Some(format!( + "Headscale has no OAuth clients: the secret {TAILNET_API_KEY} is one of its API keys (headscale apikeys create)" + )), + _ => None, + } + } + + /// What the key needs to change devices, for a message after a refusal. + fn permission(&self) -> String { + match (&self.headscale, &self.credential) { + (Some(_), _) => "Every Headscale API key may do this, so check that the key has not expired: headscale apikeys list".into(), + (None, Ok(Credential::Client { .. })) => format!( + "The OAuth client in {TAILNET_API_KEY} needs the devices:core scope, besides auth_keys" + ), + (None, _) => format!( + "An API access token in {TAILNET_API_KEY} has the permissions of the user who made it, who has to be allowed to manage the tailnet's devices. An OAuth client with the scopes auth_keys and devices:core needs no user" + ), + } + } + + /// Remove the device with this id from the tailnet: `Ok(false)` when + /// there is no such device. + pub fn delete_device(&self, id: &str) -> Result { + let id = device_id(id)?; + let url = match &self.headscale { + Some(url) => format!("{url}/api/v1/node/{id}"), + None => format!("{}/api/v2/device/{id}", self.tailscale_api), + }; + let (status, reply) = request("DELETE", &url, &self.login()?, None)?; + self.done(status, &reply, "remove the device") + } + + /// Give the device with this id another name on the tailnet: `Ok(false)` + /// when there is no such device. + pub fn rename_device(&self, id: &str, name: &str) -> Result { + let id = device_id(id)?; + if !device_name(name) { + return Err(format!("{name} is no name for a device on the tailnet").into()); + } + let login = self.login()?; + let (status, reply) = match &self.headscale { + Some(url) => request( + "POST", + &format!("{url}/api/v1/node/{id}/rename/{name}"), + &login, + Some(Body::Json(json!({}))), + )?, + None => request( + "POST", + &format!("{}/api/v2/device/{id}/name", self.tailscale_api), + &login, + Some(Body::Json(json!({"name": name}))), + )?, + }; + self.done(status, &reply, "rename the device") + } + + /// Whether a change of a device was done, was of no device, or was refused. + fn done(&self, status: u16, reply: &Value, what: &str) -> Result { + let not_found = status == 404 + || reply["message"] + .as_str() + .is_some_and(|message| message.to_ascii_lowercase().contains("not found")); + match status { + 200..=299 => Ok(true), + _ if not_found => Ok(false), + 401 | 403 => Err(format!( + "{}. {}", + refused_to(&self.name(), what, status, reply), + self.permission() + ) + .into()), + _ => Err(refused_to(&self.name(), what, status, reply)), + } + } + + /// The devices on the tailnet, as berth compares them with a host. + pub fn devices(&self) -> Result> { + let login = self.login()?; + let (url, list, field) = match &self.headscale { + Some(url) => (format!("{url}/api/v1/node"), "nodes", "id"), + None => ( + format!("{}/api/v2/tailnet/-/devices", self.tailscale_api), + "devices", + "nodeId", + ), + }; + let (status, reply) = call(&url, &login, None)?; + if status != 200 { + let refused = refused_to(&self.name(), "list the devices", status, &reply); + return Err(match status { + 401 | 403 => format!("{refused}. {}", self.permission()).into(), + _ => refused, + }); + } + let text = |value: &Value| value.as_str().unwrap_or_default().to_owned(); + let found = reply[list].as_array().cloned().unwrap_or_default(); + Ok(found + .iter() + .map(|device| { + let (dns_name, host_name, addresses) = match self.headscale { + // Headscale's name for the node on the tailnet is its given name. + Some(_) => ( + text(&device["givenName"]), + text(&device["name"]), + &device["ipAddresses"], + ), + None => ( + text(&device["name"]).trim_end_matches('.').to_owned(), + text(&device["hostname"]), + &device["addresses"], + ), + }; + let id = match text(&device[field]) { + id if id.is_empty() => text(&device["id"]), + id => id, + }; + TailnetDevice { + id, + dns_name, + host_name, + addresses: addresses + .as_array() + .map(|all| all.iter().map(text).collect()) + .unwrap_or_default(), + tailnet: String::new(), + } + }) + .collect()) + } + /// An access token for an OAuth client, by the client credentials grant. fn exchange(&self, id: &str, secret: &str) -> Result { let form = format!( @@ -248,8 +406,7 @@ impl Control { } } - fn headscale_key(&self, url: &str, token: &str, lifetime: u64) -> Result { - let login = format!("header = \"Authorization: Bearer {token}\""); + fn headscale_key(&self, url: &str, login: &str, lifetime: u64) -> Result { let mut request = json!({ "reusable": false, "ephemeral": false, @@ -266,7 +423,7 @@ impl Control { } user => { let lookup = format!("{url}/api/v1/user?name={}", query(user)); - let (status, reply) = call(&lookup, &login, None)?; + let (status, reply) = call(&lookup, login, None)?; if status != 200 { return Err(refused("Headscale", status, &reply)); } @@ -280,7 +437,7 @@ impl Control { } let (status, reply) = call( &format!("{url}/api/v1/preauthkey"), - &login, + login, Some(Body::Json(request)), )?; match reply["preAuthKey"]["key"].as_str() { @@ -291,12 +448,35 @@ impl Control { } fn refused(who: &str, status: u16, reply: &Value) -> Box { + refused_to(who, "create a key", status, reply) +} + +fn refused_to(who: &str, what: &str, status: u16, reply: &Value) -> Box { let reason = match (reply["message"].as_str(), reply.as_str()) { (Some(message), _) => message.to_owned(), (None, Some(text)) if !text.trim().is_empty() => text.trim().chars().take(200).collect(), _ => "no reason given".into(), }; - format!("{who} refused to create a key (HTTP {status}): {reason}").into() + format!("{who} refused to {what} (HTTP {status}): {reason}").into() +} + +/// Whether a name can be a device's on the tailnet, and a part of a path: a +/// DNS label of lower-case letters, digits and `-`. +fn device_name(name: &str) -> bool { + (1..=63).contains(&name.len()) + && name + .bytes() + .all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'-') + && !name.starts_with('-') + && !name.ends_with('-') +} + +/// A device's id as it goes into a path: letters and digits only. +fn device_id(id: &str) -> Result<&str> { + match !id.is_empty() && id.bytes().all(|byte| byte.is_ascii_alphanumeric()) { + true => Ok(id), + false => Err(format!("{id:?} is not the id of a device on the tailnet").into()), + } } fn query(text: &str) -> String { @@ -320,6 +500,15 @@ enum Body { /// login is a line of curl's configuration, given on its standard input, and /// the body is a private file, so neither appears in the process list. fn call(url: &str, login: &str, body: Option) -> Result<(u16, Value)> { + let method = match body { + Some(_) => "POST", + None => "GET", + }; + request(method, url, login, body) +} + +/// [`call`] with another method than the one its body implies. +fn request(method: &str, url: &str, login: &str, body: Option) -> Result<(u16, Value)> { let mut file = tempfile::NamedTempFile::new()?; let mut command = Command::new("curl"); command.args([ @@ -345,11 +534,12 @@ fn call(url: &str, login: &str, body: Option) -> Result<(u16, Value)> { }; file.flush()?; command - .args(["--request", "POST", "--header"]) + .arg("--header") .arg(format!("Content-Type: {kind}")) .arg("--data-binary") .arg(format!("@{}", file.path().display())); } + command.args(["--request", method]); let mut child = command .arg(url) .stdin(Stdio::piped()) @@ -503,6 +693,261 @@ pub fn apply_vars(paths: &Paths, config: &Value, host: &str, berth: &Path) -> Va vars } +/// What berth keeps of a host's device on the tailnet, in the store with +/// the host: what the last apply found, and the name a rename with the +/// hostname gave it, which the next apply gives the device. +#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)] +pub struct Kept { + #[serde(default, skip_serializing_if = "Option::is_none")] + pub device: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub rename: Option, +} + +impl Kept { + /// What the store keeps of the host `name` in `scope`; nothing when it has nothing. + pub fn read(store: &store::Store, scope: &str, name: &str) -> Result { + Ok(match store.read_json(kind::TAILNET, scope, name)? { + Some(value) => serde_json::from_value(value) + .map_err(|error| format!("The record of {name} on the tailnet: {error}"))?, + None => Kept::default(), + }) + } + + /// What the store keeps of the host at `place`. + pub fn of(paths: &Paths, place: &Place) -> Result { + let (scope, name) = place.side(); + Kept::read(store::shared(paths)?.as_ref(), &scope, &name) + } + + fn save(&self, paths: &Paths, place: &Place) -> Result<()> { + let (scope, name) = place.side(); + store::shared(paths)?.write_json( + kind::TAILNET, + &scope, + &name, + &serde_json::to_value(self)?, + )?; + Ok(()) + } +} + +/// A device for people: its MagicDNS name, or its id. +fn label(device: &TailnetDevice) -> String { + match device.dns_name.as_str() { + "" => device.id.clone(), + name => name.to_owned(), + } +} + +/// The devices of `devices` that can be the host of this profile: those at +/// its address, or else those with its hostname, as its device's name or as +/// the machine's own. +fn matching<'a>( + devices: &'a [TailnetDevice], + config: &Value, + name: &str, +) -> Vec<&'a TailnetDevice> { + let same = |one: &str, other: &str| { + let (one, other) = (one.trim_end_matches('.'), other.trim_end_matches('.')); + !one.is_empty() && one.eq_ignore_ascii_case(other) + }; + let address = profile::Access::of(config).address; + let at: Vec<_> = devices + .iter() + .filter(|device| { + // Headscale lists a node's name without the tailnet's domain. + let short = !device.dns_name.contains('.') + && address.parse::().is_err() + && address + .split_once('.') + .is_some_and(|(first, _)| same(first, &device.dns_name)); + short + || same(&device.dns_name, &address) + || device.addresses.iter().any(|known| same(known, &address)) + }) + .collect(); + if !at.is_empty() { + return at; + } + let hostname = config["hostname"] + .as_str() + .filter(|hostname| !hostname.is_empty()) + .unwrap_or(name); + devices + .iter() + .filter(|device| same(device.name(), hostname) || same(&device.host_name, hostname)) + .collect() +} + +/// The device of a host berth has no record of, when exactly one device on +/// the tailnet can be it; otherwise the line that says why berth does nothing. +fn look_up( + control: &Control, + config: &Value, + name: &str, +) -> std::result::Result { + let devices = control.devices().map_err(|error| { + format!("! berth has no record of {name}'s device on the tailnet, and could not look for it: {error}. Remove it there if it is there") + })?; + match matching(&devices, config, name).as_slice() { + [device] => Ok((*device).clone()), + [] => Err(format!( + "berth has no record of {name}'s device on the tailnet, and none there has its address or hostname: nothing is removed" + )), + several => Err(format!( + "! berth has no record of {name}'s device on the tailnet, and {} devices there could be it: {}. It removes none of them: remove the right one there", + several.len(), + several + .iter() + .map(|device| label(device)) + .collect::>() + .join(", ") + )), + } +} + +/// After the machine of the host at `place` was destroyed: remove its +/// device from the tailnet, the one the last apply recorded or the one +/// that alone has the host's address or hostname. Says what it did, or why +/// it did not; it never fails, as the machine is gone either way. `None` +/// for a host that was never on the tailnet. +pub fn remove_device(paths: &Paths, place: &Place, config: &Value) -> Option { + remove_device_at(&Control::configured(paths), paths, place, config) +} + +fn remove_device_at( + control: &Control, + paths: &Paths, + place: &Place, + config: &Value, +) -> Option { + let name = place.title(); + let kept = match Kept::of(paths, place) { + Ok(kept) => kept, + Err(error) => return Some(format!("! {name}'s device on the tailnet stays: {error}")), + }; + if kept.device.is_none() && !profile::joins_tailnet(config) { + return None; + } + let at = control.name(); + if let Some(why) = control.device_problem() { + return Some(format!( + "! {name}'s device on the tailnet at {at} stays, as berth cannot remove it: {why}. Remove it there" + )); + } + let device = match kept.device { + Some(device) => device, + None => match look_up(control, config, &name) { + Ok(device) => device, + Err(line) => return Some(line), + }, + }; + let shown = label(&device); + Some(match control.delete_device(&device.id) { + Ok(true) => format!("✓ Removed {name}'s device {shown} from the tailnet at {at}"), + Ok(false) => format!("{name}'s device {shown} is not on the tailnet at {at} any more"), + Err(error) => { + format!("! {name}'s device {shown} stays on the tailnet: {error}. Remove it there") + } + }) +} + +/// A MagicDNS name with its first label replaced. +fn renamed(dns_name: &str, to: &str) -> String { + match dns_name.split_once('.') { + Some((_, rest)) => format!("{to}.{rest}"), + None => to.to_owned(), + } +} + +/// After an apply of `host` that joins the tailnet: keep the device it +/// reported, and give the device the name a rename with the hostname asked +/// for, now that the machine has that hostname. A profile that reaches the +/// host by the device's old name is given the new one. Says what it did, a +/// line each. +pub fn after_apply( + paths: &Paths, + host: &crate::host::Host, + reported: Option, +) -> Result> { + after_apply_at(&Control::configured(paths), paths, host, reported) +} + +fn after_apply_at( + control: &Control, + paths: &Paths, + host: &crate::host::Host, + reported: Option, +) -> Result> { + let name = &host.name; + let mut kept = Kept::of(paths, &host.place)?; + let Some(mut device) = reported else { + return Ok(match &kept.rename { + Some(to) => vec![format!( + "! {name} did not say which device it is on the tailnet, so its device is named {to} at a later apply" + )], + None => vec![], + }); + }; + let mut lines = vec![]; + if let Some(to) = kept.rename.clone() { + let old = label(&device); + if device.name() == to { + kept.rename = None; + } else if let Some(why) = control.device_problem() { + lines.push(format!( + "! {name}'s device on the tailnet is still {old}, as berth cannot rename it: {why}. berth renames it at the next apply" + )); + } else { + match control.rename_device(&device.id, &to) { + Ok(true) => { + let was = device.dns_name.clone(); + device.dns_name = renamed(&was, &to); + kept.rename = None; + lines.push(format!( + "✓ Renamed {name}'s device on the tailnet from {old} to {}", + label(&device) + )); + if let Some(line) = follow(paths, host, &was, &device.dns_name)? { + lines.push(line); + } + } + Ok(false) => lines.push(format!( + "! {name}'s device {old} is not on the tailnet at {} any more, so berth renames it at the next apply", + control.name() + )), + Err(error) => lines.push(format!( + "! {name}'s device on the tailnet is still {old}: {error}. berth renames it at the next apply" + )), + } + } + } + kept.device = Some(device); + kept.save(paths, &host.place)?; + Ok(lines) +} + +/// Reach the host by the device's new name when its profile named the old +/// one, the whole MagicDNS name or its first label. +fn follow(paths: &Paths, host: &crate::host::Host, was: &str, now: &str) -> Result> { + let Some(mut config) = host.place.load(paths)? else { + return Ok(None); + }; + let address = profile::Access::of(&config).address; + let short = |name: &str| name.split('.').next().unwrap_or_default().to_owned(); + let new = if !was.is_empty() && address.trim_end_matches('.').eq_ignore_ascii_case(was) { + now.to_owned() + } else if !was.is_empty() && address.eq_ignore_ascii_case(&short(was)) { + short(now) + } else { + return Ok(None); + }; + config["access"]["address"] = json!(new); + host.place.save(paths, &config)?; + Ok(Some(format!("{} is reached at {new} now", host.name))) +} + #[cfg(test)] mod tests { use super::*; @@ -976,4 +1421,342 @@ mod tests { file ); } + + fn device(id: &str, dns_name: &str, addresses: &[&str]) -> TailnetDevice { + TailnetDevice { + id: id.into(), + dns_name: dns_name.into(), + host_name: dns_name.split('.').next().unwrap().into(), + addresses: addresses + .iter() + .map(|address| address.to_string()) + .collect(), + tailnet: String::new(), + } + } + + /// The host `studio`, saved with this profile. + fn studio(paths: &Paths, config: Value) -> crate::host::Host { + let place = Place::own("studio").unwrap(); + place.save(paths, &config).unwrap(); + crate::host::Host { + name: "studio".into(), + place, + config, + } + } + + fn record(paths: &Paths, place: &Place, kept: &Kept) { + kept.save(paths, place).unwrap(); + } + + #[test] + fn a_destroyed_hosts_recorded_device_is_removed_from_tailscale_and_headscale() { + let (_home, paths) = paths(); + let host = studio(&paths, json!({"hostname": "studio"})); + let recorded = |id: &str| Kept { + device: Some(device(id, "studio.example.ts.net", &["100.64.0.7"])), + rename: None, + }; + record(&paths, &host.place, &recorded("nFAKE1CNTRL")); + let (url, seen) = fake(vec![(200, json!({}))]); + let said = + remove_device_at(&control(None, &url), &paths, &host.place, &host.config).unwrap(); + assert_eq!( + said, + "✓ Removed studio's device studio.example.ts.net from the tailnet at Tailscale" + ); + let asked = seen.recv().unwrap(); + assert_eq!(asked.line, "DELETE /api/v2/device/nFAKE1CNTRL HTTP/1.1"); + assert_eq!( + asked.authorization, + format!("Basic {}", crate::registry::base64(b"secret-token:")) + ); + + record(&paths, &host.place, &recorded("12")); + let (url, seen) = fake(vec![(200, json!({}))]); + let said = + remove_device_at(&control(Some(&url), ""), &paths, &host.place, &host.config).unwrap(); + assert!(said.starts_with("✓ Removed studio's device"), "{said}"); + let asked = seen.recv().unwrap(); + assert_eq!(asked.line, "DELETE /api/v1/node/12 HTTP/1.1"); + assert_eq!(asked.authorization, "Bearer secret-token"); + } + + #[test] + fn a_device_gone_already_or_one_berth_may_not_remove_is_said_and_never_fails() { + let (_home, paths) = paths(); + let host = studio(&paths, json!({"hostname": "studio"})); + let kept = Kept { + device: Some(device("nFAKE1CNTRL", "studio.example.ts.net", &[])), + rename: None, + }; + record(&paths, &host.place, &kept); + let (url, _seen) = fake(vec![ + (404, json!({"message": "not found"})), + ( + 403, + json!({"message": "calling actor does not have enough permissions to perform this function"}), + ), + ]); + let mut tailscale = control(None, &url); + let said = remove_device_at(&tailscale, &paths, &host.place, &host.config).unwrap(); + assert_eq!( + said, + "studio's device studio.example.ts.net is not on the tailnet at Tailscale any more" + ); + tailscale.credential = Ok(Credential::parse("kFAKE:tskey-client-kFAKE-0000").unwrap()); + // The client's token comes first; the fake answers it with the 403 of the next call. + let (url, _seen) = fake(vec![ + (200, json!({"access_token": "tskey-access-FAKE"})), + ( + 403, + json!({"message": "calling actor does not have enough permissions to perform this function"}), + ), + ]); + tailscale.tailscale_api = url; + let said = remove_device_at(&tailscale, &paths, &host.place, &host.config).unwrap(); + assert!(said.starts_with("! studio's device studio.example.ts.net stays on the tailnet: Tailscale refused to remove the device (HTTP 403)"), "{said}"); + assert!(said.contains("needs the devices:core scope"), "{said}"); + assert!(said.ends_with("Remove it there"), "{said}"); + // A refused Headscale key is named as such. + let (url, _seen) = fake(vec![(401, json!("Unauthorized"))]); + let said = + remove_device_at(&control(Some(&url), ""), &paths, &host.place, &host.config).unwrap(); + assert!( + said.contains("HTTP 401") && said.contains("headscale apikeys list"), + "{said}" + ); + } + + #[test] + fn without_the_key_or_the_tailnet_berth_removes_nothing() { + let (_home, paths) = paths(); + // A host that never joined, with no record, has nothing on the tailnet. + let off = studio( + &paths, + json!({"ansible_vars": {"devbox_install_tailscale": false}}), + ); + assert_eq!(remove_device(&paths, &off.place, &off.config), None); + let host = studio(&paths, json!({"hostname": "studio"})); + let said = remove_device(&paths, &host.place, &host.config).unwrap(); + assert!( + said.starts_with("! studio's device on the tailnet at Tailscale stays, as berth cannot remove it: berth has no API key"), + "{said}" + ); + assert!(said.contains("berth secret set tailnet_api_key"), "{said}"); + } + + #[test] + fn a_host_without_a_record_is_looked_up_and_its_device_removed_only_when_alone() { + let (_home, paths) = paths(); + let tailscale_list = |devices: Value| (200, json!({"devices": devices})); + let listed = json!([ + {"id": "111", "nodeId": "nSTUDIO", "name": "studio.example.ts.net", "hostname": "studio", "addresses": ["100.64.0.7"]}, + {"id": "222", "nodeId": "nSTUDIO1", "name": "studio-1.example.ts.net", "hostname": "studio", "addresses": ["100.64.0.9"]}, + {"id": "333", "nodeId": "nDOCK", "name": "dock.example.ts.net", "hostname": "dock", "addresses": ["100.64.0.8"]}, + ]); + + // At its address: that device alone, though two have its hostname. + let host = studio( + &paths, + json!({"hostname": "studio", "access": {"address": "100.64.0.9"}}), + ); + let (url, seen) = fake(vec![tailscale_list(listed.clone()), (200, json!({}))]); + let said = + remove_device_at(&control(None, &url), &paths, &host.place, &host.config).unwrap(); + assert_eq!( + seen.recv().unwrap().line, + "GET /api/v2/tailnet/-/devices HTTP/1.1" + ); + assert_eq!( + seen.recv().unwrap().line, + "DELETE /api/v2/device/nSTUDIO1 HTTP/1.1" + ); + assert!(said.contains("studio-1.example.ts.net"), "{said}"); + + // By its hostname, two devices could be it: berth removes neither. + let host = studio( + &paths, + json!({"hostname": "studio", "access": {"address": "192.0.2.10"}}), + ); + let (url, seen) = fake(vec![tailscale_list(listed.clone())]); + let said = + remove_device_at(&control(None, &url), &paths, &host.place, &host.config).unwrap(); + assert!( + said.contains( + "2 devices there could be it: studio.example.ts.net, studio-1.example.ts.net" + ), + "{said}" + ); + assert!(said.contains("removes none of them"), "{said}"); + seen.recv().unwrap(); + assert!( + seen.recv_timeout(std::time::Duration::from_millis(300)) + .is_err() + ); + + // None has its address or hostname: nothing to remove. + let host = studio( + &paths, + json!({"hostname": "pier", "access": {"address": "192.0.2.10"}}), + ); + let (url, _seen) = fake(vec![tailscale_list(listed)]); + let said = + remove_device_at(&control(None, &url), &paths, &host.place, &host.config).unwrap(); + assert!( + said.contains("none there has its address or hostname"), + "{said}" + ); + + // On Headscale, by its MagicDNS name, the node's given name. + let host = studio( + &paths, + json!({"hostname": "pier", "access": {"address": "studio.hs.example.org"}}), + ); + let (url, seen) = fake(vec![ + ( + 200, + json!({"nodes": [ + {"id": "12", "name": "studio", "givenName": "studio", "ipAddresses": ["100.64.0.7"]}, + {"id": "13", "name": "dock", "givenName": "dock", "ipAddresses": ["100.64.0.8"]}, + ]}), + ), + (200, json!({})), + ]); + let said = + remove_device_at(&control(Some(&url), ""), &paths, &host.place, &host.config).unwrap(); + assert_eq!(seen.recv().unwrap().line, "GET /api/v1/node HTTP/1.1"); + assert_eq!(seen.recv().unwrap().line, "DELETE /api/v1/node/12 HTTP/1.1"); + assert!(said.starts_with("✓ Removed studio's device"), "{said}"); + } + + #[test] + fn an_apply_records_the_device_and_renames_it_after_a_rename_with_the_hostname() { + let (_home, paths) = paths(); + let reported = device("nFAKE1CNTRL", "studio.example.ts.net", &["100.64.0.7"]); + let host = studio( + &paths, + json!({"hostname": "studio", "access": {"address": "studio.example.ts.net"}}), + ); + // Nothing to rename: the device is recorded and nothing is asked. + let lines = after_apply_at( + &control(None, "http://127.0.0.1:9"), + &paths, + &host, + Some(reported.clone()), + ) + .unwrap(); + assert!(lines.is_empty(), "{lines:?}"); + let kept = Kept::of(&paths, &host.place).unwrap(); + assert_eq!( + kept, + Kept { + device: Some(reported.clone()), + rename: None + } + ); + + // A rename with the hostname leaves the device for the apply. + let place = crate::profiles::rename(&paths, &host.place, "pier", true).unwrap(); + let kept = Kept::of(&paths, &place).unwrap(); + assert_eq!(kept.rename.as_deref(), Some("pier")); + assert_eq!(kept.device.as_ref(), Some(&reported)); + assert_eq!(Kept::of(&paths, &host.place).unwrap(), Kept::default()); + let pier = crate::host::Host::load_at(&paths, place.clone()).unwrap(); + + // A refusal keeps the rename for the next apply. + let (url, _seen) = fake(vec![(403, json!({"message": "not enough permissions"}))]); + let lines = + after_apply_at(&control(None, &url), &paths, &pier, Some(reported.clone())).unwrap(); + assert!(lines[0].starts_with("! pier's device on the tailnet is still studio.example.ts.net: Tailscale refused to rename the device (HTTP 403)"), "{lines:?}"); + assert_eq!( + Kept::of(&paths, &place).unwrap().rename.as_deref(), + Some("pier") + ); + + let (url, seen) = fake(vec![(200, json!({}))]); + let lines = + after_apply_at(&control(None, &url), &paths, &pier, Some(reported.clone())).unwrap(); + let asked = seen.recv().unwrap(); + assert_eq!(asked.line, "POST /api/v2/device/nFAKE1CNTRL/name HTTP/1.1"); + assert_eq!(asked.body, json!({"name": "pier"})); + assert_eq!( + lines, + [ + "✓ Renamed pier's device on the tailnet from studio.example.ts.net to pier.example.ts.net", + "pier is reached at pier.example.ts.net now", + ] + ); + let kept = Kept::of(&paths, &place).unwrap(); + assert_eq!(kept.rename, None); + assert_eq!(kept.device.unwrap().dns_name, "pier.example.ts.net"); + assert_eq!( + place.load(&paths).unwrap().unwrap()["access"]["address"], + "pier.example.ts.net" + ); + + // On Headscale the new name is in the path. + let place = crate::profiles::rename(&paths, &place, "attic", true).unwrap(); + let attic = crate::host::Host::load_at(&paths, place.clone()).unwrap(); + let (url, seen) = fake(vec![( + 200, + json!({"node": {"id": "12", "givenName": "attic"}}), + )]); + after_apply_at( + &control(Some(&url), ""), + &paths, + &attic, + Some(device("12", "pier.hs.example.org", &[])), + ) + .unwrap(); + assert_eq!( + seen.recv().unwrap().line, + "POST /api/v1/node/12/rename/attic HTTP/1.1" + ); + assert_eq!(Kept::of(&paths, &place).unwrap().rename, None); + } + + #[test] + fn a_rename_without_the_hostname_a_copy_and_a_forgotten_host_leave_the_tailnet_alone() { + let (_home, paths) = paths(); + let host = studio(&paths, json!({"hostname": "studio"})); + let reported = device("nFAKE1CNTRL", "studio.example.ts.net", &[]); + let kept = Kept { + device: Some(reported.clone()), + rename: None, + }; + record(&paths, &host.place, &kept); + let place = crate::profiles::rename(&paths, &host.place, "pier", false).unwrap(); + assert_eq!(Kept::of(&paths, &place).unwrap(), kept); + // Nothing is asked of the control server: its address goes nowhere. + let pier = crate::host::Host::load_at(&paths, place.clone()).unwrap(); + let lines = after_apply_at( + &control(None, "http://127.0.0.1:9"), + &paths, + &pier, + Some(reported), + ) + .unwrap(); + assert!(lines.is_empty(), "{lines:?}"); + // A copy starts with no device of its own. + let (copy, _) = crate::profiles::duplicate(&paths, &place, "lab").unwrap(); + assert_eq!(Kept::of(&paths, ©).unwrap(), Kept::default()); + // Forgetting the host forgets its record, and asks nothing of the tailnet. + crate::profiles::remove(&paths, &place).unwrap(); + assert_eq!(Kept::of(&paths, &place).unwrap(), Kept::default()); + } + + #[test] + fn names_and_ids_are_checked_before_they_go_into_a_path() { + let control = control(None, "http://127.0.0.1:9"); + assert!(control.delete_device("../keys").is_err()); + assert!(control.rename_device("nFAKE", "Pier/x").is_err()); + assert!(control.rename_device("nFAKE", "-pier").is_err()); + assert_eq!( + renamed("studio.example.ts.net", "pier"), + "pier.example.ts.net" + ); + assert_eq!(renamed("studio", "pier"), "pier"); + } } diff --git a/crates/berth/src/cli/args.rs b/crates/berth/src/cli/args.rs index cc46a3f..477d45f 100644 --- a/crates/berth/src/cli/args.rs +++ b/crates/berth/src/cli/args.rs @@ -230,7 +230,7 @@ pub enum Host { Rename { name: String, new: String, - /// Make NEW its profile's hostname as well, for the next apply. + /// Make NEW its profile's hostname as well, and its device's name on the tailnet, for the next apply. #[arg(long)] hostname: bool, }, @@ -238,7 +238,7 @@ pub enum Host { #[command(visible_alias = "rm")] Remove { name: String, - /// Destroy the machine a provisioner made for it as well, with its disk. + /// Destroy the machine a provisioner made for it as well, with its disk, and remove its device from the tailnet. #[arg(long)] destroy: bool, }, diff --git a/crates/berth/src/local/serve/actions.rs b/crates/berth/src/local/serve/actions.rs index 4ddec5f..e6cf307 100644 --- a/crates/berth/src/local/serve/actions.rs +++ b/crates/berth/src/local/serve/actions.rs @@ -1352,7 +1352,7 @@ fn remove_action(seen: &Seen) -> Action { let (help, confirm) = match made(seen.machine) { Some(machine) => ( format!( - "Forget {name}: its profile, the passwords berth generated and keeps for it, and its host keys. With destroy, {} also destroys its machine {} with its disk. Without destroy the machine is left as it is, and berth keeps it as a resource of {} with no host, which berth resource remove deletes later.", + "Forget {name}: its profile, the passwords berth generated and keeps for it, and its host keys. With destroy, {} also destroys its machine {} with its disk, and berth removes its device from the tailnet. Without destroy the machine and its device are left as they are, and berth keeps it as a resource of {} with no host, which berth resource remove deletes later.", machine.provisioner, machine.external_id, machine.provisioner ), format!( @@ -1369,7 +1369,7 @@ fn remove_action(seen: &Seen) -> Action { }; let destroy = match made(seen.machine) { Some(machine) => format!( - "Destroy the machine {} made, {}, with its disk. It cannot be undone.", + "Destroy the machine {} made, {}, with its disk, and remove its device from the tailnet. It cannot be undone.", machine.provisioner, machine.external_id ), None => "Destroy the machine a provisioner made for the host. This host has none, so yes is refused.".into(), @@ -1408,7 +1408,7 @@ fn remove(state: Arc, name: String, act: Act) -> Run { Box::pin(async move { let destroy = yes(&act, "destroy")?; let place = own_place(&name)?; - saved_profile(&state, &name)?; + let (_, profile) = saved_profile(&state, &name)?; let machine = machine_of(&state, &name); if destroy && let Some(why) = destroy_refused(&name, machine.as_ref()) { return Err(Fail::new(code::NOT_AVAILABLE, why)); @@ -1454,10 +1454,14 @@ fn remove(state: Arc, name: String, act: Act) -> Run { match acting.run(paths, &(serve.provision)(), out) { // What it says of the profile staying is not so here. Ok(_) => { - let _ = writeln!( - out, - "✓ {provisioner} destroyed {id}. A node of {name} on a tailnet stays: remove it there" - ); + let _ = writeln!(out, "✓ {provisioner} destroyed {id}"); + // The machine is gone, so its device on the tailnet goes too; a + // device berth cannot remove is named, and the host is forgotten all the same. + if let Some(line) = + berth_core::tailnet::remove_device(paths, &place, &profile) + { + let _ = writeln!(out, "{line}"); + } } Err(error) => { let _ = writeln!(out, "✗ {error}"); @@ -1996,6 +2000,7 @@ fn rename(state: Arc, name: String, act: Act) -> Run { return Err(bad(problem)); } let from = own_place(&name)?; + let joins = profile::joins_tailnet(&profile_of(&state, &name)); if hostname { berth_core::profiles::hostname_allowed(&name, &profile_of(&state, &name)) .map_err(|error| bad(error.to_string()))?; @@ -2026,9 +2031,13 @@ fn rename(state: Arc, name: String, act: Act) -> Run { Ok(_) => { let _ = writeln!(out, "✓ {name} is called {new} now"); if hostname { + let also = match joins { + true => ", and its device on the tailnet is renamed then", + false => "", + }; let _ = writeln!( out, - "Its profile's hostname is {new}; the machine takes it at its next apply" + "Its profile's hostname is {new}; the machine takes it at its next apply{also}" ); } true diff --git a/crates/berth/src/local/serve/tests.rs b/crates/berth/src/local/serve/tests.rs index 53e7893..6fb111d 100644 --- a/crates/berth/src/local/serve/tests.rs +++ b/crates/berth/src/local/serve/tests.rs @@ -5157,3 +5157,214 @@ async fn a_host_that_declares_its_controller_stops_it_asks_the_controller() { let (status, said) = send(client, "POST", "/v1/hosts/dock/actions/stop", Value::Null).await; assert_eq!(status, 501, "{said}"); } + +/// A control server that gives every request the same answer, and tells +/// the request lines it was sent. +fn fake_control(status: u16, answer: Value) -> (String, Arc>>) { + use std::io::{BufRead, BufReader, Read, Write}; + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let url = format!("http://{}", listener.local_addr().unwrap()); + let asked = Arc::new(std::sync::Mutex::new(vec![])); + let seen = asked.clone(); + std::thread::spawn(move || { + for stream in listener.incoming() { + let Ok(mut stream) = stream else { break }; + let mut reader = BufReader::new(stream.try_clone().unwrap()); + let mut line = String::new(); + reader.read_line(&mut line).unwrap(); + let mut length = 0; + loop { + let mut header = String::new(); + reader.read_line(&mut header).unwrap(); + let header = header.trim_end(); + if header.is_empty() { + break; + } + if let Some((name, value)) = header.split_once(':') + && name.eq_ignore_ascii_case("content-length") + { + length = value.trim().parse().unwrap(); + } + } + let mut body = vec![0; length]; + reader.read_exact(&mut body).unwrap(); + seen.lock().unwrap().push(line.trim_end().to_owned()); + let text = answer.to_string(); + let _ = write!( + stream, + "HTTP/1.1 {status} X\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{text}", + text.len() + ); + } + }); + (url, asked) +} + +/// Point berth at a Headscale server, with an API key. +fn on_headscale(paths: &berth_core::paths::Paths, url: &str) { + paths.set("tailnet_login_server", url).unwrap(); + paths.set("tailnet_user", "1").unwrap(); + berth_core::registry::Store::own(store::shared(paths).unwrap()) + .set_secret(berth_api::profile::TAILNET_API_KEY, Some("hskey-api-FAKE")) + .unwrap(); +} + +fn tailnet_device(id: &str, dns_name: &str) -> berth_api::TailnetDevice { + berth_api::TailnetDevice { + id: id.into(), + dns_name: dns_name.into(), + host_name: dns_name.split('.').next().unwrap().into(), + addresses: vec!["100.64.0.7".into()], + tailnet: String::new(), + } +} + +#[tokio::test(flavor = "multi_thread")] +async fn destroying_a_host_removes_its_device_from_the_tailnet_and_forgetting_it_does_not() { + let served = served_with(quick).await; + let fake = fake_provisioner(&served); + let mut client = served.client().await; + let client = &mut client; + register_fake(client).await; + send( + client, + "PUT", + "/v1/secrets/api_token", + json!({"value": "tok-s3cret"}), + ) + .await; + let paths = &served.state.paths; + paths.prepare().unwrap(); + // The key refuses: the destroy goes on, and says so. + let (url, asked) = fake_control(403, json!({"message": "Unauthorized"})); + on_headscale(paths, &url); + let done = async |client: &mut Client, acted: &Value| { + let id = acted["job"]["id"].as_str().unwrap(); + let job = job_done(client, id).await; + (job, job_ended(&served.socket, id).await.0) + }; + for (name, id, address) in [ + ("studio", "vm-1", "192.0.2.20"), + ("attic", "vm-2", "192.0.2.21"), + ] { + let place = berth_core::profiles::Place::own(name).unwrap(); + place + .save(paths, &json!({"hostname": name, "builder": {}, "access": {"user": "ops", "link": ["sh", "-c", "exit 1"]}})) + .unwrap(); + std::fs::write( + fake.join("answer.create"), + json!({"id": id, "address": address}).to_string(), + ) + .unwrap(); + let asked = json!({"provisioner": "vm", "host": name, "arguments": {"server": "https://192.0.2.10:8443"}}); + let (status, created) = send(client, "POST", "/v1/resources", asked).await; + assert_eq!(status, 202, "{created}"); + let (job, output) = done(client, &created).await; + assert_eq!(job["exit_code"], 0, "{output}"); + let device = tailnet_device( + if name == "studio" { "12" } else { "13" }, + &format!("{name}.hs.example.org"), + ); + store::shared(paths) + .unwrap() + .write_json(kind::TAILNET, store::OWN, name, &json!({"device": device})) + .unwrap(); + } + asked.lock().unwrap().clear(); + + // Forgotten, the host's device stays on the tailnet. + let (status, acted) = send( + client, + "POST", + "/v1/hosts/attic/actions/remove", + Value::Null, + ) + .await; + assert_eq!(status, 202, "{acted}"); + let (job, output) = done(client, &acted).await; + assert_eq!(job["exit_code"], 0, "{output}"); + assert!( + asked.lock().unwrap().is_empty(), + "{:?}", + asked.lock().unwrap() + ); + + // Destroyed, its device is removed, or berth says why not. + let (status, acted) = send( + client, + "POST", + "/v1/hosts/studio/actions/remove", + json!({"arguments": {"destroy": "yes"}}), + ) + .await; + assert_eq!(status, 202, "{acted}"); + let (job, output) = done(client, &acted).await; + assert_eq!(job["exit_code"], 0, "{output}"); + assert!(output.contains("vm destroyed vm-1"), "{output}"); + assert_eq!(*asked.lock().unwrap(), ["DELETE /api/v1/node/12 HTTP/1.1"]); + assert!( + output + .contains("! studio's device studio.hs.example.org stays on the tailnet: Headscale at") + && output.contains("refused to remove the device (HTTP 403)") + && output.contains("headscale apikeys list"), + "{output}" + ); + assert!(!output.contains("hskey-api-FAKE"), "{output}"); + assert_eq!( + send(client, "GET", "/v1/hosts/studio", Value::Null).await.0, + 404 + ); +} + +#[tokio::test(flavor = "multi_thread")] +async fn an_apply_records_the_device_its_host_reports_on_the_tailnet() { + let Some(host) = Berthd::start().await else { + return; + }; + std::fs::write( + host.dir.path().join("etc/applied.json"), + json!({"berth": "0.9.0", "plugins": [], "tailnet": { + "id": "12", "dns_name": "studio.hs.example.org", "host_name": "studio", + "addresses": ["100.64.0.7"], "tailnet": ""}}) + .to_string(), + ) + .unwrap(); + let served = served().await; + let paths = served.state.paths.clone(); + paths.prepare().unwrap(); + let link = host.link(); + let mut command = vec![link.program.clone()]; + command.extend(link.args.clone()); + let place = berth_core::profiles::Place::own("studio").unwrap(); + let config = + json!({"hostname": "studio", "builder": {}, "access": {"user": "ops", "link": command}}); + place.save(&paths, &config).unwrap(); + // A rename with the hostname waits for this apply. + let (url, asked) = fake_control(200, json!({"node": {"id": "12", "givenName": "pier"}})); + on_headscale(&paths, &url); + let place = berth_core::profiles::rename(&paths, &place, "pier", true).unwrap(); + let pier = berth_core::host::Host::load_at(&paths, place.clone()).unwrap(); + let mut out = vec![]; + let settled = { + let paths = paths.clone(); + tokio::task::spawn_blocking(move || { + berth_core::apply::settle_with(&paths, &pier, &mut out) + .map(|_| out) + .map_err(|error| error.to_string()) + }) + .await + .unwrap() + }; + let out = String::from_utf8(settled.unwrap()).unwrap(); + assert!( + out.contains("✓ Renamed pier's device on the tailnet from studio.hs.example.org to pier.hs.example.org"), + "{out}" + ); + assert_eq!( + *asked.lock().unwrap(), + ["POST /api/v1/node/12/rename/pier HTTP/1.1"] + ); + let kept = berth_core::tailnet::Kept::of(&paths, &place).unwrap(); + assert_eq!(kept.rename, None); + assert_eq!(kept.device.unwrap().dns_name, "pier.hs.example.org"); +} diff --git a/docs/content/docs/(guide)/reference/cli.mdx b/docs/content/docs/(guide)/reference/cli.mdx index 7dbf800..2cf7ac7 100644 --- a/docs/content/docs/(guide)/reference/cli.mdx +++ b/docs/content/docs/(guide)/reference/cli.mdx @@ -359,7 +359,7 @@ berth host rename NAME NEW [--hostname] | --- | --- | | `NAME` | Its name. | | `NEW` | Its new name. | -| `--hostname` | Make NEW its profile's hostname as well, for the next apply. | +| `--hostname` | Make NEW its profile's hostname as well, and its device's name on the tailnet, for the next apply. | ### berth host remove @@ -374,7 +374,7 @@ berth host remove NAME [--destroy] | Argument | What it is | | --- | --- | | `NAME` | Its name. | -| `--destroy` | Destroy the machine a provisioner made for it as well, with its disk. | +| `--destroy` | Destroy the machine a provisioner made for it as well, with its disk, and remove its device from the tailnet. | ### berth host duplicate diff --git a/docs/content/docs/(guide)/use/access.mdx b/docs/content/docs/(guide)/use/access.mdx index 23539c4..558aa5b 100644 --- a/docs/content/docs/(guide)/use/access.mdx +++ b/docs/content/docs/(guide)/use/access.mdx @@ -36,7 +36,7 @@ The secret is one of these: | Control server | The secret | Lifetime | | --- | --- | --- | -| Tailscale | An OAuth client with the `auth_keys` scope, as `CLIENT_ID:CLIENT_SECRET` | The client doesn't expire. berth trades it for an access token of an hour each time it makes a key | +| Tailscale | An OAuth client with the scopes `auth_keys` and `devices:core`, as `CLIENT_ID:CLIENT_SECRET` | The client doesn't expire. berth trades it for an access token of an hour each time it uses it | | Tailscale | An API access token from the admin console | 1 to 90 days, chosen when you make it | | Headscale | An API key from `headscale apikeys create` | 90 days unless you choose otherwise | @@ -55,6 +55,32 @@ When berth writes a seed or an installer, it says until when the key works. A ma A host that's on the tailnet already is left alone, so applying again goes without a key and leaves the host logged in. Once it has joined, it publishes the desktop and T3 there. That's `devbox_tailscale_serve`, on by default, and it needs HTTPS certificates enabled for the tailnet. Enable them if Tailscale asks, then run **Publish desktop and T3 on the tailnet**. That action also gives Cockpit the tailnet's trusted certificate in place of its self-signed one. +### The host's device + +The same key looks after the host's device on the tailnet: + +| When | What berth does | +| --- | --- | +| An apply | Records the device's id, its name and its addresses, from `tailscale status` on the host | +| `berth host remove HOST --destroy` | Removes the device once the provisioner has destroyed the machine | +| The next apply after `berth host rename OLD NEW --hostname` | Gives the device the name `NEW`, after the machine has taken the hostname | + +The rename waits for the apply so the name on the tailnet changes when the machine's hostname does. Until then the old MagicDNS name keeps working, so berth can still connect for the apply. When the profile's address is the old name, berth changes it to the new one. + +Removing a host without `--destroy` leaves its device alone, and so does a rename without `--hostname`. A copy made with [duplicate](/docs/use/lifecycle#duplicating-a-host) joins as a device of its own. + +A host set up by an older berth has no record until its next apply. Until then berth looks for its device among the tailnet's devices, by the host's address and then by its hostname. It acts only when exactly one device matches, and says so otherwise. + +The key needs these permissions: + +| The secret | To make keys | To list, remove and rename devices | +| --- | --- | --- | +| A Tailscale OAuth client | The scope `auth_keys` | The scope `devices:core`. Tailscale asks for tags when a client gets it | +| A Tailscale API access token | The permissions of the user who made it | The same | +| A Headscale API key | Any key | Any key | + +When the control server refuses, or berth has no key, the job says so, and the host is removed all the same. Remove the device in the admin console then. A rename that's refused waits for the next apply. + ### When berth has no API key A host whose profile wants the tailnet needs the secret. Without it, berth stops before anything runs and says what to do: set the secret, or switch **Tailscale** off for the host. A seed, an installer or a new machine isn't made, and the set-up tabs show the problem for them. An apply stops before its first change. A host that's on the tailnet already is applied as usual, as it doesn't need a key. diff --git a/docs/content/docs/(guide)/use/lifecycle.mdx b/docs/content/docs/(guide)/use/lifecycle.mdx index a65ac42..6b90cab 100644 --- a/docs/content/docs/(guide)/use/lifecycle.mdx +++ b/docs/content/docs/(guide)/use/lifecycle.mdx @@ -58,6 +58,7 @@ This saves a copy of studio's profile as the host `attic`. The copy leaves out w | The hashes of the passwords berth generated, and those passwords | No. A hash you typed stays | | The host keys berth recorded, the passwords it logs in with, its jobs and messages | No | | The record of what was applied to it | No. That stays on the machine | +| Its device on the tailnet | No. The copy joins as a device of its own | The disk isn't cloned. The new host is installed from its profile, as any other. @@ -79,6 +80,8 @@ When a provisioner made the host's machine, removing it without `--destroy` keep berth host remove studio --destroy ``` -With `--destroy`, or `destroy=yes`, the provisioner destroys the machine with its disk first, and then berth forgets the host. The question before it says which provisioner and which machine. For a host no provisioner made, `--destroy` is refused, as there is nothing for berth to destroy. If destroying fails, the host and its machine's record stay, so you can try again. A node of the host on a tailnet stays: remove it there. +With `--destroy`, or `destroy=yes`, the provisioner destroys the machine with its disk first, and then berth forgets the host. The question before it says which provisioner and which machine. For a host no provisioner made, `--destroy` is refused, as there is nothing for berth to destroy. If destroying fails, the host and its machine's record stay, so you can try again. + +Once the machine is destroyed, berth also removes its [device on the tailnet](/docs/use/access#the-hosts-device). When it can't, such as without the API key or with a key that may not remove devices, the job says why and berth forgets the host all the same. Remove the device in the tailnet's admin console then. Without `--destroy`, the device stays. On the home screen, `x` asks the same. For a host a provisioner made, `f` forgets it and leaves the machine, and `d` destroys the machine too, after asking once more. diff --git a/docs/content/docs/(guide)/use/moving.mdx b/docs/content/docs/(guide)/use/moving.mdx index c232a1c..49606e0 100644 --- a/docs/content/docs/(guide)/use/moving.mdx +++ b/docs/content/docs/(guide)/use/moving.mdx @@ -119,9 +119,9 @@ berth moves everything it keeps under the old name to the new one, in one change | The host keys berth recorded, and the passwords it logs in with | Yes | | The record of its machine and its other resources | Yes, under the same id | | Its messages, its jobs and where its channel was read up to | Yes | -| The machine, its hostname and its tailnet name | No | +| The machine, its hostname and its name on the tailnet | No, unless `--hostname` | | The provisioner's name for the machine, such as the Incus instance `berth-test` | No | -With `--hostname`, the new name becomes the profile's hostname as well. The machine takes it at its next apply, which also writes the profile the host keeps with the new name. The name has to suit a hostname then: lower-case letters, digits and `-`. +With `--hostname`, the new name becomes the profile's hostname as well. The machine takes it at its next apply, which also writes the profile the host keeps with the new name. On a tailnet, that apply renames the host's [device](/docs/use/access#the-hosts-device) too, when berth has the API key. The name has to suit a hostname then: lower-case letters, digits and `-`. berth refuses when a host has the new name already. A job that is creating the host's machine blocks the rename until that job ends. To keep the host and make another like it, [duplicate](/docs/use/lifecycle#duplicating-a-host) it instead. When a [berth server](/docs/use/server) keeps the host, the server renames it as a job. On its overview and over the server's API it's the host's action **Rename**, `rename`. diff --git a/docs/content/docs/(guide)/use/secrets.mdx b/docs/content/docs/(guide)/use/secrets.mdx index 9f98263..307b0c5 100644 --- a/docs/content/docs/(guide)/use/secrets.mdx +++ b/docs/content/docs/(guide)/use/secrets.mdx @@ -33,7 +33,7 @@ echo "$TOKEN" | berth secret set github Or use the **Secrets** tab of the home screen (`5`). Enter on **+ Add secret** asks for a name and the value, Enter on a secret replaces its value, `x` twice removes it. Values are never shown. -The secret `tailnet_api_key` is berth's own, for making the keys hosts join the tailnet with, and is never given to a host. See [joining the tailnet](/docs/use/access#joining-the-tailnet) for what it holds. +The secret `tailnet_api_key` is berth's own, for making the keys hosts join the tailnet with and for removing and renaming their devices there. It's never given to a host. See [joining the tailnet](/docs/use/access#joining-the-tailnet) for what it holds and the permissions it needs. diff --git a/docs/content/docs/(guide)/use/server.mdx b/docs/content/docs/(guide)/use/server.mdx index b3d3c8d..f797acf 100644 --- a/docs/content/docs/(guide)/use/server.mdx +++ b/docs/content/docs/(guide)/use/server.mdx @@ -141,7 +141,7 @@ With the role `admin` on the server, which its token has, berth sets the server - `berth host create HOST --provisioner NAME`, `berth host start` and `berth host stop` use the server's provisioners and secrets ([Provisioners](/docs/deploy/provisioners#with-a-berth-server)). - `berth resource` uses the server's resources. A deployment's specification is sent to the server and kept there, and it gets the server's secrets ([Deployments](/docs/deploy/deployments#with-a-berth-server)). - A secret's value is sent to the server and kept only there. -- The `tailnet_` settings and the secret `tailnet_api_key` are the server's, which creates the [keys its hosts join with](/docs/use/access#joining-the-tailnet). Settings and `berth config set` change them there. +- The `tailnet_` settings and the secret `tailnet_api_key` are the server's, which creates the [keys its hosts join with](/docs/use/access#joining-the-tailnet) and looks after their [devices](/docs/use/access#the-hosts-device). Settings and `berth config set` change them there. - A plugin is installed on the server, and its features show on the Features tab of the server's hosts. A `.tgz` or folder of yours is sent there first. - The server logs in to hosts with the keys of [its own key ring](/docs/use/accounts#keys), by default its key `berth`. `berth key` acts on the server's ring, and its private halves stay there. `berth key trust HOST` and `berth host verify HOST --accept` make first contact and accept a changed host key from the server. - `berth key rotate` replaces a key on the hosts that take it, as a job of the server ([Rekeying](/docs/use/accounts#rekeying)). diff --git a/docs/openapi/berth.openapi.json b/docs/openapi/berth.openapi.json index 2b2dfaf..0c4d2e2 100644 --- a/docs/openapi/berth.openapi.json +++ b/docs/openapi/berth.openapi.json @@ -209,6 +209,17 @@ "$ref": "#/components/schemas/AppliedPlugin" }, "type": "array" + }, + "tailnet": { + "oneOf": [ + { + "$ref": "#/components/schemas/TailnetDevice", + "description": "The host's device on the tailnet, as its Tailscale client saw it at\nthe end of the apply, when it is on one." + }, + { + "type": "null" + } + ] } }, "required": [ @@ -3353,6 +3364,38 @@ ], "type": "object" }, + "TailnetDevice": { + "description": "A host's device on the tailnet, from `tailscale status --json`. It\nholds public values only, which the control server's API finds it by.", + "properties": { + "addresses": { + "description": "Its addresses on the tailnet.", + "items": { + "type": "string" + }, + "type": "array" + }, + "dns_name": { + "description": "Its MagicDNS name, such as `studio.example.ts.net`, without the final dot.", + "type": "string" + }, + "host_name": { + "description": "The machine's own hostname, as the client reports it.", + "type": "string" + }, + "id": { + "description": "The device's stable id: Tailscale's `nodeId`, or the number of\nHeadscale's node.", + "type": "string" + }, + "tailnet": { + "description": "The tailnet's name, when the client knows it.", + "type": "string" + } + }, + "required": [ + "id" + ], + "type": "object" + }, "Topic": { "description": "An entry of `/etc/berth/channels.json`.", "properties": { diff --git a/docs/openapi/berth.operations.json b/docs/openapi/berth.operations.json index 0fefea4..74f6ee9 100644 --- a/docs/openapi/berth.operations.json +++ b/docs/openapi/berth.operations.json @@ -209,6 +209,17 @@ "$ref": "#/components/schemas/AppliedPlugin" }, "type": "array" + }, + "tailnet": { + "oneOf": [ + { + "$ref": "#/components/schemas/TailnetDevice", + "description": "The host's device on the tailnet, as its Tailscale client saw it at\nthe end of the apply, when it is on one." + }, + { + "type": "null" + } + ] } }, "required": [ @@ -2424,6 +2435,38 @@ ], "type": "object" }, + "TailnetDevice": { + "description": "A host's device on the tailnet, from `tailscale status --json`. It\nholds public values only, which the control server's API finds it by.", + "properties": { + "addresses": { + "description": "Its addresses on the tailnet.", + "items": { + "type": "string" + }, + "type": "array" + }, + "dns_name": { + "description": "Its MagicDNS name, such as `studio.example.ts.net`, without the final dot.", + "type": "string" + }, + "host_name": { + "description": "The machine's own hostname, as the client reports it.", + "type": "string" + }, + "id": { + "description": "The device's stable id: Tailscale's `nodeId`, or the number of\nHeadscale's node.", + "type": "string" + }, + "tailnet": { + "description": "The tailnet's name, when the client knows it.", + "type": "string" + } + }, + "required": [ + "id" + ], + "type": "object" + }, "Version": { "properties": { "api": { diff --git a/docs/openapi/berthd.openapi.json b/docs/openapi/berthd.openapi.json index 77b4e37..1f125f6 100644 --- a/docs/openapi/berthd.openapi.json +++ b/docs/openapi/berthd.openapi.json @@ -18,6 +18,17 @@ "$ref": "#/components/schemas/AppliedPlugin" }, "type": "array" + }, + "tailnet": { + "oneOf": [ + { + "$ref": "#/components/schemas/TailnetDevice", + "description": "The host's device on the tailnet, as its Tailscale client saw it at\nthe end of the apply, when it is on one." + }, + { + "type": "null" + } + ] } }, "required": [ @@ -1380,6 +1391,38 @@ ], "type": "object" }, + "TailnetDevice": { + "description": "A host's device on the tailnet, from `tailscale status --json`. It\nholds public values only, which the control server's API finds it by.", + "properties": { + "addresses": { + "description": "Its addresses on the tailnet.", + "items": { + "type": "string" + }, + "type": "array" + }, + "dns_name": { + "description": "Its MagicDNS name, such as `studio.example.ts.net`, without the final dot.", + "type": "string" + }, + "host_name": { + "description": "The machine's own hostname, as the client reports it.", + "type": "string" + }, + "id": { + "description": "The device's stable id: Tailscale's `nodeId`, or the number of\nHeadscale's node.", + "type": "string" + }, + "tailnet": { + "description": "The tailnet's name, when the client knows it.", + "type": "string" + } + }, + "required": [ + "id" + ], + "type": "object" + }, "Topic": { "description": "An entry of `/etc/berth/channels.json`.", "properties": { diff --git a/docs/openapi/berthd.operations.json b/docs/openapi/berthd.operations.json index 6203557..21f4709 100644 --- a/docs/openapi/berthd.operations.json +++ b/docs/openapi/berthd.operations.json @@ -18,6 +18,17 @@ "$ref": "#/components/schemas/AppliedPlugin" }, "type": "array" + }, + "tailnet": { + "oneOf": [ + { + "$ref": "#/components/schemas/TailnetDevice", + "description": "The host's device on the tailnet, as its Tailscale client saw it at\nthe end of the apply, when it is on one." + }, + { + "type": "null" + } + ] } }, "required": [ @@ -1380,6 +1391,38 @@ ], "type": "object" }, + "TailnetDevice": { + "description": "A host's device on the tailnet, from `tailscale status --json`. It\nholds public values only, which the control server's API finds it by.", + "properties": { + "addresses": { + "description": "Its addresses on the tailnet.", + "items": { + "type": "string" + }, + "type": "array" + }, + "dns_name": { + "description": "Its MagicDNS name, such as `studio.example.ts.net`, without the final dot.", + "type": "string" + }, + "host_name": { + "description": "The machine's own hostname, as the client reports it.", + "type": "string" + }, + "id": { + "description": "The device's stable id: Tailscale's `nodeId`, or the number of\nHeadscale's node.", + "type": "string" + }, + "tailnet": { + "description": "The tailnet's name, when the client knows it.", + "type": "string" + } + }, + "required": [ + "id" + ], + "type": "object" + }, "Topic": { "description": "An entry of `/etc/berth/channels.json`.", "properties": { diff --git a/provision/ansible/roles/devbox/tasks/applied.yml b/provision/ansible/roles/devbox/tasks/applied.yml index 1df428c..db0d39e 100644 --- a/provision/ansible/roles/devbox/tasks/applied.yml +++ b/provision/ansible/roles/devbox/tasks/applied.yml @@ -1,14 +1,40 @@ --- +# The host's device on the tailnet, so that berth can remove it when the +# machine is destroyed and rename it with the host: the id the control +# server's API knows it by, and what berth finds it by otherwise. +- name: Inspect the host's device on the tailnet + ansible.builtin.command: tailscale status --json + register: devbox_tailnet_self + changed_when: false + failed_when: false + check_mode: false + when: devbox_berth_applied is defined +- name: Tell which device the host is on the tailnet + vars: + devbox_tailnet_said: "{{ devbox_tailnet_self.stdout | default('', true) | from_json }}" + ansible.builtin.set_fact: + devbox_tailnet_device: + id: "{{ devbox_tailnet_said.Self.ID | string }}" + dns_name: "{{ devbox_tailnet_said.Self.DNSName | default('', true) | regex_replace('[.]$', '') }}" + host_name: "{{ devbox_tailnet_said.Self.HostName | default('', true) }}" + addresses: "{{ devbox_tailnet_said.Self.TailscaleIPs | default([], true) }}" + tailnet: "{{ (devbox_tailnet_said.CurrentTailnet | default({}, true)).Name | default('', true) }}" + when: + - devbox_berth_applied is defined + - devbox_tailnet_self.rc | default(1) == 0 + - devbox_tailnet_self.stdout is search('"BackendState":\\s*"Running"') + - (devbox_tailnet_said.Self | default({}, true)).ID | default('', true) | length > 0 # Which berth and plugins this configuration came from, for berthd to report: # berth compares it with what it would apply now to tell whether the host is # behind. Only the plugins whose features the host has are recorded. A host # with the controller alone says so, and so does a node, with the # provisioners its features offer; a whole profile is recorded as before. +# The device on the tailnet goes in too, when the host is on one. - name: Record which berth and plugins this configuration came from ansible.builtin.copy: dest: /etc/berth/applied.json mode: '0644' - content: "{{ ({'berth': devbox_berth_applied.berth, 'plugins': devbox_berth_applied.plugins | selectattr('id', 'in', devbox_plugin_features | map('regex_replace', '/.*$', '') | list) | list} | combine({'level': devbox_level} if devbox_level != 'workstation' else {}) | combine({'offers': devbox_plugin_offers} if devbox_plugin_offers | default([]) | length > 0 else {})) | to_nice_json }}\n" + content: "{{ ({'berth': devbox_berth_applied.berth, 'plugins': devbox_berth_applied.plugins | selectattr('id', 'in', devbox_plugin_features | map('regex_replace', '/.*$', '') | list) | list} | combine({'level': devbox_level} if devbox_level != 'workstation' else {}) | combine({'offers': devbox_plugin_offers} if devbox_plugin_offers | default([]) | length > 0 else {}) | combine({'tailnet': devbox_tailnet_device} if devbox_tailnet_device is defined else {})) | to_nice_json }}\n" when: devbox_berth_applied is defined # The profile itself, as berth holds it, so that a berth without it can take # the host over: berth host adopt reads it over SSH, and berthd serves it to an