diff --git a/Cargo.lock b/Cargo.lock index 06f0ea7..52407af 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -277,6 +277,7 @@ dependencies = [ "serde_json", "tempfile", "tokio", + "tower", "tui-input", "utoipa", "utoipa-axum", diff --git a/crates/berth-api/src/ids.rs b/crates/berth-api/src/ids.rs new file mode 100644 index 0000000..95f9480 --- /dev/null +++ b/crates/berth-api/src/ids.rs @@ -0,0 +1,209 @@ +//! The ids of what berth keeps: a UUID of version 7 as 32 lowercase hex +//! characters. Wherever an id is taken, it may carry the prefix of its kind, +//! such as `prov_`, which is dropped; replies give it bare. + +/// The kinds of things with an id, each with the prefix its ids may carry. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Kind { + Host, + Provisioner, + Resource, + Registry, + Secret, + Key, +} + +impl Kind { + pub const ALL: [Kind; 6] = [ + Kind::Host, + Kind::Provisioner, + Kind::Resource, + Kind::Registry, + Kind::Secret, + Kind::Key, + ]; + + /// The prefix an id of this kind may be given with, `_` included. + pub fn prefix(self) -> &'static str { + match self { + Kind::Host => "host_", + Kind::Provisioner => "prov_", + Kind::Resource => "res_", + Kind::Registry => "reg_", + Kind::Secret => "sec_", + Kind::Key => "key_", + } + } + + /// What people call one, for messages. + pub fn noun(self) -> &'static str { + match self { + Kind::Host => "host", + Kind::Provisioner => "provisioner", + Kind::Resource => "resource", + Kind::Registry => "registry", + Kind::Secret => "secret", + Kind::Key => "key", + } + } +} + +/// How long an id is. +pub const LENGTH: usize = 32; + +/// Whether a text is an id in its canonical form. +pub fn is_id(text: &str) -> bool { + text.len() == LENGTH + && text + .bytes() + .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)) +} + +/// An id in its canonical form, from the bare id in either case or the +/// hyphenated form of a UUID. +fn canonical(text: &str) -> Option { + let bare: String = match text.len() { + 36 => { + let bytes = text.as_bytes(); + if [8, 13, 18, 23].iter().any(|&at| bytes[at] != b'-') { + return None; + } + text.chars().filter(|&ch| ch != '-').collect() + } + _ => text.to_owned(), + }; + let bare = bare.to_ascii_lowercase(); + is_id(&bare).then_some(bare) +} + +/// Whether a name looks like an id, with or without a prefix, so that it +/// cannot be given as one. +pub fn looks_like_id(name: &str) -> bool { + let hex = |text: &str| text.len() == LENGTH && text.bytes().all(|b| b.is_ascii_hexdigit()); + hex(name) + || Kind::ALL.iter().any(|kind| { + name.get(..kind.prefix().len()) + .is_some_and(|start| start.eq_ignore_ascii_case(kind.prefix())) + && hex(&name[kind.prefix().len()..]) + }) +} + +/// What a value given for a thing of `kind` is. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Given { + /// An id with the prefix of `kind`: only that id, never a name. + Prefixed(String), + /// A bare id: that id, if there is a thing with it, else a name. + Bare(String), + /// A name. + Name(String), +} + +/// Read a value given for a thing of `kind`. A prefix of another kind is refused. +pub fn read(kind: Kind, given: &str) -> Result { + for other in Kind::ALL { + let Some(rest) = given + .get(..other.prefix().len()) + .filter(|start| start.eq_ignore_ascii_case(other.prefix())) + .map(|_| &given[other.prefix().len()..]) + else { + continue; + }; + let Some(id) = canonical(rest) else { + continue; + }; + if other != kind { + return Err(format!( + "{given} is the id of a {}, and a {} was asked for", + other.noun(), + kind.noun() + )); + } + return Ok(Given::Prefixed(id)); + } + Ok(match canonical(given) { + Some(id) => Given::Bare(id), + None => Given::Name(given.to_owned()), + }) +} + +/// The id a value gives when it is one, bare or with the prefix of `kind`. +pub fn id_in(kind: Kind, given: &str) -> Option { + match read(kind, given) { + Ok(Given::Prefixed(id) | Given::Bare(id)) => Some(id), + _ => None, + } +} + +/// A new id from the time in milliseconds and 10 random bytes: a UUID of +/// version 7, which sorts in the order the ids were made. +pub fn from_parts(millis: u64, random: [u8; 10]) -> String { + let mut bytes = [0u8; 16]; + bytes[..6].copy_from_slice(&millis.to_be_bytes()[2..]); + bytes[6..].copy_from_slice(&random); + bytes[6] = 0x70 | (bytes[6] & 0x0f); + bytes[8] = 0x80 | (bytes[8] & 0x3f); + bytes.iter().map(|byte| format!("{byte:02x}")).collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + const ID: &str = "01972c4e8f3a7b219d4e5a6b7c8d9e0f"; + + #[test] + fn an_id_is_32_lowercase_hex_characters_of_a_version_7_uuid() { + let id = from_parts(0x0197_2c4e_8f3a, [0xff; 10]); + assert!(is_id(&id), "{id}"); + assert_eq!(&id[..12], "01972c4e8f3a"); + assert_eq!(&id[12..13], "7"); + assert!("89ab".contains(&id[16..17])); + assert!(!is_id(&ID.to_uppercase()) && !is_id("r-k3x9p2qa")); + } + + #[test] + fn a_value_is_an_id_with_its_prefix_a_bare_id_or_a_name() { + let prefixed = format!("prov_{ID}"); + assert_eq!( + read(Kind::Provisioner, &prefixed), + Ok(Given::Prefixed(ID.into())) + ); + assert_eq!( + read(Kind::Provisioner, &format!("PROV_{}", ID.to_uppercase())), + Ok(Given::Prefixed(ID.into())) + ); + assert_eq!(read(Kind::Host, ID), Ok(Given::Bare(ID.into()))); + assert_eq!( + read(Kind::Host, "01972c4e-8f3a-7b21-9d4e-5a6b7c8d9e0f"), + Ok(Given::Bare(ID.into())) + ); + assert_eq!(read(Kind::Host, "studio"), Ok(Given::Name("studio".into()))); + // A prefix of another kind is refused. + let error = read(Kind::Host, &prefixed).unwrap_err(); + assert!(error.contains("provisioner"), "{error}"); + // A prefix without an id after it is a name. + assert_eq!( + read(Kind::Host, "host_studio"), + Ok(Given::Name("host_studio".into())) + ); + assert_eq!(id_in(Kind::Secret, &format!("sec_{ID}")), Some(ID.into())); + assert_eq!(id_in(Kind::Secret, "github_token"), None); + } + + #[test] + fn names_in_the_shape_of_an_id_are_told_apart() { + assert!(looks_like_id(ID)); + assert!(looks_like_id(&ID.to_uppercase())); + assert!(looks_like_id(&format!("prov_{ID}"))); + assert!(looks_like_id(&format!("Host_{ID}"))); + for name in [ + "studio", + "incus-vm", + &ID[1..], + "abcdefabcdefabcdefabcdefabcdefag", + ] { + assert!(!looks_like_id(name), "{name}"); + } + } +} diff --git a/crates/berth-api/src/lib.rs b/crates/berth-api/src/lib.rs index b365743..9560405 100644 --- a/crates/berth-api/src/lib.rs +++ b/crates/berth-api/src/lib.rs @@ -5,6 +5,7 @@ pub mod client; pub mod compat; pub mod escape; +pub mod ids; pub mod ops; #[cfg(feature = "client")] pub mod portal; diff --git a/crates/berth-api/src/ops.rs b/crates/berth-api/src/ops.rs index a6def3a..3649fea 100644 --- a/crates/berth-api/src/ops.rs +++ b/crates/berth-api/src/ops.rs @@ -289,6 +289,7 @@ pub static SERVE: &[Op] = &[ admin(call("secrets.list", "GET", "/v1/secrets")), admin(call("secrets.set", "PUT", "/v1/secrets/{name}")), admin(call("secrets.remove", "DELETE", "/v1/secrets/{name}")), + admin(call("secrets.rename", "POST", "/v1/secrets/{name}/rename")), admin(call("keys.list", "GET", "/v1/keys")), admin(bytes(call("keys.add", "PUT", "/v1/keys/{name}"))), admin(call("keys.remove", "DELETE", "/v1/keys/{name}")), @@ -296,6 +297,11 @@ pub static SERVE: &[Op] = &[ admin(call("registries.list", "GET", "/v1/registries")), admin(call("registries.save", "PUT", "/v1/registries/{name}")), admin(call("registries.remove", "DELETE", "/v1/registries/{name}")), + admin(call( + "registries.rename", + "POST", + "/v1/registries/{name}/rename", + )), admin(call( "registries.check", "POST", diff --git a/crates/berth-api/src/types.rs b/crates/berth-api/src/types.rs index f361ae2..0ee41eb 100644 --- a/crates/berth-api/src/types.rs +++ b/crates/berth-api/src/types.rs @@ -820,16 +820,24 @@ pub struct Acted { #[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, ToSchema)] pub struct JobInfo { pub id: String, - /// What it was done to: `host:NAME`, `provisioner:NAME`, + /// What it was done to, by id: `host:ID`, `provisioner:ID`, /// `resource:ID` or `installation`. Empty for a job from before 0.5. #[serde(default)] pub subject: String, + /// What it was done to, by name as it is called now: `host:NAME`, + /// `provisioner:NAME`, `resource:ID` or `installation`; by id for one + /// that is gone. + #[serde(default)] + pub subject_name: String, /// The action it runs, such as `apply`, `rollout` or `provision`. #[serde(default)] pub action: String, /// The hosts it is about, for a reader: a name, or several with commas. #[serde(default)] pub host: String, + /// The ids of those hosts, as `host` lists them; a host in a file by its path. + #[serde(default, skip_serializing_if = "String::is_empty")] + pub host_id: String, pub started: String, pub ended: Option, pub exit_code: Option, @@ -1246,14 +1254,32 @@ pub struct PluginUpdateSpec { pub id: Option, } +/// A thing berth keeps, by its id and its name. +#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, ToSchema)] +pub struct Named { + /// Its id, which stays when it is renamed. + pub id: String, + pub name: String, +} + +/// A new name for a thing. +#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, ToSchema)] +pub struct Rename { + pub name: String, +} + // ── Provisioners ──────────────────────────────────────────────────────────── /// Something a plugin creates machines with, for a saved host's profile. #[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, ToSchema)] pub struct ProvisionerInfo { + /// Its id, which stays when it is renamed. In the list of types, the + /// type itself. + pub id: String, /// The name it is registered under, such as `ovh-incus`. In the list of /// types, the type itself. - pub id: String, + #[serde(default)] + pub name: String, /// The plugin's provisioner it is, as `PLUGIN/ID`, such as `incus/vm`. #[serde(rename = "type", default)] pub kind: String, @@ -1284,9 +1310,12 @@ pub struct ProvisionerInfo { /// The values of its own arguments that are kept, by name. #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] pub settings: BTreeMap, - /// The node it lives on, when a feature of that host offered it. + /// The node it lives on, when a feature of that host offered it, by name. #[serde(default, skip_serializing_if = "String::is_empty")] pub node: String, + /// The id of that node. + #[serde(default, skip_serializing_if = "String::is_empty")] + pub node_id: String, /// What it can make: `machine`, and kinds its plugin names. #[serde(default, skip_serializing_if = "Vec::is_empty")] pub kinds: Vec, @@ -1303,6 +1332,17 @@ pub struct ProvisionerInfo { pub problem: String, } +impl ProvisionerInfo { + /// What it is called: its name, or for a server from before ids, which + /// said none, the name it gave as its id. + pub fn named(&self) -> &str { + match self.name.is_empty() { + true => &self.id, + false => &self.name, + } + } +} + /// A provisioner to register, or the type and settings of one that is. #[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, ToSchema)] pub struct SavedProvisioner { @@ -1616,6 +1656,9 @@ pub struct Resource { pub id: String, /// The name of the provisioner that made it. pub provisioner: String, + /// The id of that provisioner, when it is registered. + #[serde(default, skip_serializing_if = "String::is_empty")] + pub provisioner_id: String, /// `machine`, or a kind the plugin declares. pub kind: String, /// Unique per provisioner and kind. @@ -1627,9 +1670,13 @@ pub struct Resource { /// The provisioner's id of it; empty until `create` said it. #[serde(default)] pub external_id: String, - /// The host a machine belongs to, if any. + /// The host a machine belongs to, if any, by name; a profile in a file + /// by its path. #[serde(default, skip_serializing_if = "Option::is_none")] pub host: Option, + /// The id of that host, when it is a saved host. + #[serde(default, skip_serializing_if = "String::is_empty")] + pub host_id: String, #[serde(default, skip_serializing_if = "String::is_empty")] pub address: String, /// Where it can be reached, as its provisioner last said: when it was @@ -1662,7 +1709,7 @@ pub struct Resource { /// What to create a resource with. #[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, ToSchema)] pub struct NewResource { - /// The provisioner's name. + /// The provisioner, by name or id. pub provisioner: String, /// `machine` when empty. #[serde(default)] diff --git a/crates/berth-core/src/apply.rs b/crates/berth-core/src/apply.rs index 9634fa6..fd48f2b 100644 --- a/crates/berth-core/src/apply.rs +++ b/crates/berth-core/src/apply.rs @@ -616,7 +616,7 @@ mod tests { paths.set("source", source.to_str().unwrap()).unwrap(); let host = Host { name: "box".into(), - place: Place::own("box").unwrap(), + place: Place::own(&paths, "box").unwrap(), config: json!({"target": "cloud", "access": {"address": "box", "user": "ops", "sudo": "password"}}), }; let prepared = prepare(&paths, &host, Some("hunter2")).unwrap(); @@ -677,7 +677,7 @@ mod tests { }; paths.prepare().unwrap(); paths.give_own_key(); - let place = Place::own("studio").unwrap(); + let place = Place::own(&paths, "studio").unwrap(); let host = |config: Value| { place.save(&paths, &config).unwrap(); Host { @@ -687,6 +687,7 @@ mod tests { } }; let saved = || place.load(&paths).unwrap().unwrap(); + place.save(&paths, &json!({})).unwrap(); let secrets = crate::registry::Store::own(crate::store::shared(&paths).unwrap()); secrets.set_secret("sudo/studio", Some("hunter2")).unwrap(); secrets.set_secret("ssh/studio", Some("hunter2")).unwrap(); diff --git a/crates/berth-core/src/host.rs b/crates/berth-core/src/host.rs index 06e7039..fd15da5 100644 --- a/crates/berth-core/src/host.rs +++ b/crates/berth-core/src/host.rs @@ -6,7 +6,8 @@ use serde_json::Value; use std::process::Command; pub struct Host { - /// The name it is saved under, or the path of its file. + /// The name it is saved under, or the path of its file. Its id is the + /// place's. pub name: String, /// Where its profile is. pub place: Place, @@ -37,12 +38,10 @@ impl Host { crate::profiles::list(paths, &scope) .unwrap_or_default() .into_iter() - .filter_map(|(name, config)| { - Some(Host { - place: Place::stored(&scope, &name).ok()?, - name, - config, - }) + .map(|(place, config)| Host { + name: place.argument(), + place, + config, }) .filter(|host| host.access().reachable()) .collect() diff --git a/crates/berth-core/src/import.rs b/crates/berth-core/src/import.rs index cc0cef5..37747ed 100644 --- a/crates/berth-core/src/import.rs +++ b/crates/berth-core/src/import.rs @@ -53,6 +53,10 @@ pub const NAMES_MARKER: &str = "import:names"; /// older berths kept of joining the tailnet without a key berth makes. pub const TAILNET_MARKER: &str = "import:tailnet"; +/// The marker of the ninth, which gives everything berth keeps under a name +/// an id, and has what refers to it, and what belongs to it, use the id. +pub const IDS_MARKER: &str = "import:ids"; + /// Every import's marker: a store that lacks one is taken in by this berth. pub const MARKERS: &[&str] = &[ MARKER, @@ -63,6 +67,7 @@ pub const MARKERS: &[&str] = &[ PROVISIONERS_MARKER, NAMES_MARKER, TAILNET_MARKER, + IDS_MARKER, ]; /// What a row of the import is. @@ -96,6 +101,7 @@ pub fn run(paths: &Paths, installation: &Installation, store: &Store) -> Result< && marked(store, PROVISIONERS_MARKER)? && marked(store, NAMES_MARKER)? && marked(store, TAILNET_MARKER)? + && marked(store, IDS_MARKER)? { return Ok(()); } @@ -190,9 +196,458 @@ pub fn run(paths: &Paths, installation: &Installation, store: &Store) -> Result< eprintln!("berth: {renamed} {noun} {verb} type for level and target for profile now"); } } + // Last, as every import before keeps things by name. + if !marked(store, IDS_MARKER)? { + let plugins = crate::provision::found(paths); + let taken = take_in_ids(store, &plugins)?; + for said in taken.said() { + eprintln!("berth: {said}"); + } + } Ok(()) } +/// What the import of ids did. +#[derive(Debug, Default, PartialEq)] +pub(crate) struct TakenIds { + /// How many things were given an id. + pub given: usize, + /// The provisioners that were given a copy of a secret that all shared, + /// as `PROVISIONER: SECRET`. + pub copied: Vec, + /// The secrets all shared that went once each provisioner had its own. + pub dropped: Vec, +} + +impl TakenIds { + fn said(&self) -> Vec { + let mut said = vec![]; + if self.given > 0 { + said.push(format!( + "gave {} hosts, provisioners, resources, registries, secrets and keys ids of their own; their names can change now", + self.given + )); + } + if !self.copied.is_empty() { + said.push(format!( + "each provisioner has its own copy of the secrets it took from those all share: {}", + self.copied.join(", ") + )); + } + if !self.dropped.is_empty() { + said.push(format!( + "removed the secrets {} that all shared and nothing else uses", + self.dropped.join(", ") + )); + } + said + } +} + +/// The ninth import, in one change with its marker. Each host, provisioner, +/// resource, registry, global secret and key gets an id, and its name +/// becomes its label. What belongs to one is kept by its id: a host's +/// generated passwords, what berth knows of its SSH, its passwords for +/// logging in, a registry's login, a provisioner's secrets, a resource's +/// specification and a key's successor. Records, profiles, jobs, messages +/// and cursors refer to them by id. A provisioner that took a secret of the +/// name it declares from those all share gets its own copy, and a shared one +/// that nothing else uses then goes. A row that has a label already was +/// converted before and is left as it is. +pub(crate) fn take_in_ids(store: &Store, plugins: &[crate::plugins::Plugin]) -> Result { + use crate::store::{Batch, Rows, Table, new_id}; + use std::collections::{BTreeMap, BTreeSet}; + let rows = match store.export(Table::State)? { + Rows::State(rows) => rows, + _ => vec![], + }; + // A row by name: one without a label, which no thing owns and which is + // no resource of an id of the new shape. + let converted = |name: &str| { + berth_api::ids::is_id(name) + || name + .split_once('/') + .is_some_and(|(owner, _)| berth_api::ids::is_id(owner)) + }; + let legacy = |kind: &str| -> Vec<(String, String)> { + rows.iter() + .filter(|row| row.kind == kind && row.label.is_none() && !converted(&row.name)) + .map(|row| (row.scope.clone(), row.name.clone())) + .collect() + }; + let labelled = |kind: &str| -> Vec<(String, String, String)> { + rows.iter() + .filter(|row| row.kind == kind) + .filter_map(|row| Some((row.scope.clone(), row.name.clone(), row.label.clone()?))) + .collect() + }; + let mut batch = Batch::default(); + let mut taken = TakenIds::default(); + // (kind, scope, name) → id, for those converted now and before. + let mut ids: BTreeMap<(&str, String, String), String> = BTreeMap::new(); + for (scope, id, label) in labelled(kind::PROFILE) { + ids.insert((kind::PROFILE, scope, label), id); + } + for kind in [ + kind::PROVISIONER_ENTRY, + kind::REGISTRY, + kind::KEY, + kind::SECRET, + ] { + for (scope, id, label) in labelled(kind) { + ids.insert((kind, scope, label), id); + } + } + // The things themselves. + let mut values: Vec<(&str, String, String, String, Vec)> = vec![]; + for kind in [kind::PROFILE, kind::PROVISIONER_ENTRY, kind::REGISTRY] { + for (scope, name) in legacy(kind) { + let Some(value) = store.read(kind, &scope, &name)? else { + continue; + }; + let id = new_id(); + ids.insert((kind, scope.clone(), name.clone()), id.clone()); + batch.remove(kind, &scope, &name); + values.push((kind, scope, id, name, value)); + } + } + for (scope, name) in legacy(kind::KEY) { + if name.ends_with(crate::keys::ring::PENDING) { + continue; + } + let Some(value) = store.read(kind::KEY, &scope, &name)? else { + continue; + }; + let id = new_id(); + ids.insert((kind::KEY, scope.clone(), name.clone()), id.clone()); + batch.remove(kind::KEY, &scope, &name); + values.push((kind::KEY, scope, id, name, value)); + } + for (scope, name) in legacy(kind::KEY) { + let Some(key) = name.strip_suffix(crate::keys::ring::PENDING) else { + continue; + }; + batch.remove(kind::KEY, &scope, &name); + if let (Some(id), Some(value)) = ( + ids.get(&(kind::KEY, scope.clone(), key.to_owned())), + store.read(kind::KEY, &scope, &name)?, + ) { + batch.write(kind::KEY, &scope, &format!("{id}/pending"), value); + } + } + let id_of = + |ids: &BTreeMap<(&str, String, String), String>, + kind: &'static str, + scope: &str, + name: &str| { ids.get(&(kind, scope.to_owned(), name.to_owned())).cloned() }; + // Secrets: what belongs to a registry, a host or a provisioner goes + // under its owner; one whose owner is gone goes with it. + for (scope, name) in legacy(kind::SECRET) { + let owner_row = + |owner: Option, label: &str| owner.map(|id| format!("{id}/{label}")); + let moved = if let Some(registry) = name.strip_prefix("registry/") { + Some(owner_row( + id_of(&ids, kind::REGISTRY, &scope, registry), + "login", + )) + } else if let Some((label, host)) = name + .strip_prefix("ssh/") + .map(|host| ("ssh", host)) + .or_else(|| name.strip_prefix("sudo/").map(|host| ("sudo", host))) + { + // A profile in a file keeps its passwords by its path. + match host.contains('/') || host.ends_with(".json") { + true => continue, + false => Some(owner_row(id_of(&ids, kind::PROFILE, &scope, host), label)), + } + } else if let Some((provisioner, secret)) = name + .strip_prefix("provisioner/") + .and_then(|rest| rest.split_once('/')) + { + Some(owner_row( + id_of(&ids, kind::PROVISIONER_ENTRY, &scope, provisioner), + secret, + )) + } else { + None + }; + let Some(value) = store.read(kind::SECRET, &scope, &name)? else { + continue; + }; + batch.remove(kind::SECRET, &scope, &name); + match moved { + Some(Some(row)) => { + batch.write(kind::SECRET, &scope, &row, value); + } + Some(None) => {} + None => { + let id = new_id(); + ids.insert((kind::SECRET, scope.clone(), name.clone()), id.clone()); + values.push((kind::SECRET, scope, id, name, value)); + } + } + } + taken.given = values.len(); + // What hangs off a host by its name: by its id now. + for kind in [kind::CREDENTIALS, kind::ACCESS, kind::TAILNET] { + for (scope, name) in legacy(kind) { + let Some(id) = id_of(&ids, kind::PROFILE, &scope, &name) else { + continue; + }; + if let Some(value) = store.read(kind, &scope, &name)? { + batch + .remove(kind, &scope, &name) + .write(kind, &scope, &id, value); + } + } + } + // Resources: an id of the new shape, their provisioner and host by id, + // and their specification beside them. + let mut resources: BTreeMap = BTreeMap::new(); + // Every record, as the imports before make ids of the new shape too. + let records: Vec<(String, String)> = rows + .iter() + .filter(|row| row.kind == kind::RESOURCE) + .map(|row| (row.scope.clone(), row.name.clone())) + .collect(); + for (scope, old) in records { + let Some(value) = store.read(kind::RESOURCE, &scope, &old)? else { + continue; + }; + let Ok(mut record) = serde_json::from_slice::(&value) else { + continue; + }; + let before = record.clone(); + let id = match berth_api::ids::is_id(&old) { + true => old.clone(), + false => new_id(), + }; + record.id = id.clone(); + if let Some(provisioner) = id_of(&ids, kind::PROVISIONER_ENTRY, OWN, &record.provisioner) { + record.provisioner = provisioner; + } + if scope != crate::profiles::FILE_SCOPE + && let Some(host) = id_of(&ids, kind::PROFILE, &scope, &record.host) + { + record.host = host; + } + if record == before && id == old { + continue; + } + batch.remove(kind::RESOURCE, &scope, &old); + if let Some(spec) = store.read(kind::SPEC, &scope, &old)? { + batch + .remove(kind::SPEC, &scope, &old) + .write(kind::SPEC, &scope, &id, spec); + } + resources.insert(old, id.clone()); + values.push(( + kind::RESOURCE, + scope, + id, + String::new(), + serde_json::to_vec(&record)?, + )); + taken.given += 1; + } + // A provisioner's node by its id, and the secrets of the names its type + // declares that it took from those all share: its own copies now. + let mut copied_from: BTreeSet = BTreeSet::new(); + let owned_secrets: BTreeSet = rows + .iter() + .filter(|row| row.kind == kind::SECRET) + .map(|row| row.name.clone()) + .collect(); + let global = |ids: &BTreeMap<(&str, String, String), String>, name: &str| { + id_of(ids, kind::SECRET, OWN, name) + }; + let mut provisioner_values = vec![]; + for (kind, scope, id, name, value) in values.iter_mut() { + if *kind != kind::PROVISIONER_ENTRY { + continue; + } + let Ok(mut entry) = serde_json::from_slice::(value) else { + continue; + }; + if let Some(node) = id_of(&ids, kind::PROFILE, scope, &entry.node) { + entry.node = node; + } + *value = serde_json::to_vec(&entry)?; + provisioner_values.push((id.clone(), name.clone(), entry.kind.clone(), scope.clone())); + } + for (_, id, label) in labelled(kind::PROVISIONER_ENTRY) { + if let Some(value) = store.read(kind::PROVISIONER_ENTRY, OWN, &id)? + && let Ok(entry) = serde_json::from_slice::(&value) + { + provisioner_values.push((id, label, entry.kind, OWN.to_owned())); + } + } + for (id, name, kind_of, scope) in &provisioner_values { + let Ok((_, declared)) = crate::provision::find(plugins, kind_of) else { + continue; + }; + for secret in &declared.secrets { + let own = format!("{id}/{}", secret.name); + let had = owned_secrets.contains(&format!("provisioner/{name}/{}", secret.name)) + || owned_secrets.contains(&own); + if had { + continue; + } + let Some(value) = store.read(kind::SECRET, scope, &secret.name)?.or( + match global(&ids, &secret.name) { + Some(global) => store.read(kind::SECRET, scope, &global)?, + None => None, + }, + ) else { + continue; + }; + batch.write(kind::SECRET, scope, &own, value); + taken.copied.push(format!("{name}: {}", secret.name)); + copied_from.insert(secret.name.clone()); + } + } + // Profiles refer to registries, secrets and keys by id. + let mut profiles: Vec<(String, String, Value)> = vec![]; + for (kind, scope, id, _, value) in &values { + if *kind == kind::PROFILE + && let Ok(profile) = serde_json::from_slice::(value) + { + profiles.push((scope.clone(), id.clone(), profile)); + } + } + for (scope, id, _) in labelled(kind::PROFILE) { + if let Some(profile) = store.read_json(kind::PROFILE, &scope, &id)? { + profiles.push((scope, id, profile)); + } + } + let mut handed: BTreeSet = BTreeSet::new(); + let mut converted_profiles: BTreeMap<(String, String), Value> = BTreeMap::new(); + for (scope, id, mut profile) in profiles { + for entry in profile["secrets"].as_array().into_iter().flatten() { + if let Some(name) = entry["name"].as_str() { + handed.insert(name.to_owned()); + } + } + let mut changed = false; + if let Some(secrets) = profile.get_mut("secrets").and_then(Value::as_array_mut) { + for entry in secrets { + if let Some(name) = entry["name"].as_str() + && let Some(found) = id_of(&ids, kind::SECRET, OWN, name) + { + entry["name"] = json!(found); + changed = true; + } + } + } + if let Some(registries) = profile.get_mut("registries").and_then(Value::as_array_mut) { + for name in registries { + if let Some(found) = name + .as_str() + .and_then(|name| id_of(&ids, kind::REGISTRY, OWN, name)) + { + *name = json!(found); + changed = true; + } + } + } + if let Some(auth) = profile.pointer_mut("/access/auth") + && let Some(key) = auth.as_str().and_then(|text| text.strip_prefix("key:")) + && let Some(found) = id_of(&ids, kind::KEY, OWN, key) + { + *auth = json!(format!("key:{found}")); + changed = true; + } + if changed { + converted_profiles.insert((scope, id), profile); + } + } + for (kind, scope, id, _, value) in values.iter_mut() { + if *kind == kind::PROFILE + && let Some(profile) = converted_profiles.remove(&(scope.clone(), id.clone())) + { + *value = profile.to_string().into_bytes(); + } + } + for ((scope, id), profile) in converted_profiles { + batch.write(kind::PROFILE, &scope, &id, profile.to_string().into_bytes()); + } + // A shared secret a provisioner had through the name it declares goes + // when nothing else uses it: no profile hands it to a host, and no + // feature makes nodes trust it. + let trusted: BTreeSet = plugins + .iter() + .flat_map(crate::plugins::hosting) + .filter_map(|hosting| hosting.trusts) + .flat_map(|(cert, key)| [cert, key]) + .collect(); + for name in &copied_from { + if handed.contains(name) || trusted.contains(name) { + continue; + } + if let Some(index) = values.iter().position(|(kind, scope, _, label, _)| { + *kind == kind::SECRET && scope == OWN && label == name + }) { + values.remove(index); + taken.given -= 1; + } else if let Some(id) = global(&ids, name) { + batch.remove(kind::SECRET, OWN, &id); + } + taken.dropped.push(name.clone()); + } + for (kind, scope, id, label, value) in values { + match label.is_empty() { + true => batch.write(kind, &scope, &id, value), + false => batch.write_labelled(kind, &scope, &id, &label, value), + }; + } + // Jobs, messages and cursors, by the ids of what they are about. + let host_id = |name: &str| id_of(&ids, kind::PROFILE, OWN, name); + if let Rows::Jobs(jobs) = store.export(Table::Jobs)? { + for job in jobs { + let subject = match crate::runner::Subject::parse(&job.subject) { + Some(crate::runner::Subject::Host(name)) => { + host_id(&name).map(|id| format!("host:{id}")) + } + Some(crate::runner::Subject::Provisioner(name)) => { + id_of(&ids, kind::PROVISIONER_ENTRY, OWN, &name) + .map(|id| format!("provisioner:{id}")) + } + Some(crate::runner::Subject::Resource(old)) => { + resources.get(&old).map(|id| format!("resource:{id}")) + } + _ => None, + }; + let hosts: Vec = job + .host + .split(", ") + .filter(|host| !host.is_empty()) + .map(|host| host_id(host).unwrap_or_else(|| host.to_owned())) + .collect(); + let hosts = hosts.join(", "); + if subject.is_some() || hosts != job.host { + batch.set_job(&job.id, &hosts, &subject.unwrap_or(job.subject)); + } + } + } + let mut seen_hosts = BTreeSet::new(); + if let Rows::Messages(messages) = store.export(Table::Messages)? { + seen_hosts.extend(messages.into_iter().map(|message| message.host)); + } + if let Rows::Cursors(cursors) = store.export(Table::Cursors)? { + seen_hosts.extend(cursors.into_iter().map(|cursor| cursor.host)); + } + for host in seen_hosts { + if let Some(id) = host_id(&host) { + batch.move_host(&host, &id); + } + } + let marker = + json!({"version": berth_api::VERSION, "at": berth_api::rfc3339(berth_api::unix_now())}); + batch.write(kind::META, OWN, IDS_MARKER, marker.to_string().into_bytes()); + store.apply(&batch)?; + Ok(taken) +} + /// The seventh import, in one change with its marker: every stored profile, /// of every scope, takes the names of now ([`berth_api::profile::take_old_names`]). /// The values may be sealed, so this is done here rather than by a migration @@ -1279,8 +1734,21 @@ mod tests { store } + /// A value by the name of what it is: a thing by its name, which the + /// store keeps as its label; a secret also by the address of one that + /// belongs to something, such as `registry/NAME`. fn read(store: &Store, kind: &str, name: &str) -> Option { - let value = store.read(kind, OWN, name).unwrap()?; + // A row by name, as before the import of ids, is read as it is. + if kind == kind::SECRET + && let Some(value) = crate::registry::read_secret_in(store, OWN, name).unwrap() + { + return Some(value); + } + let row = store + .id_of(kind, OWN, name) + .unwrap() + .unwrap_or(name.to_owned()); + let value = store.read(kind, OWN, &row).unwrap()?; Some(String::from_utf8(value).unwrap()) } @@ -1532,15 +2000,24 @@ mod tests { json_row(&store, kind::PROFILE, "studio").unwrap(), json!({"hostname": "studio", "target": "laptop", "builder": {}}) ); - // A row an older berth writes later is read with the names of now. + // A row a berth server of an older version writes later is read with the names of now. let late = json!({"level": "controller", "access": {"address": "192.0.2.5"}}); store - .write(kind::PROFILE, OWN, "late", late.to_string().as_bytes()) + .write_labelled( + kind::PROFILE, + OWN, + &crate::store::new_id(), + "late", + late.to_string().as_bytes(), + ) .unwrap(); - let place = crate::profiles::Place::own("late").unwrap(); + let place = crate::profiles::Place::own(&paths, "late").unwrap(); assert_eq!(place.load(&paths).unwrap().unwrap()["type"], "managed"); let listed = crate::profiles::list(&paths, OWN).unwrap(); - let (_, listed) = listed.iter().find(|(name, _)| name == "late").unwrap(); + let (_, listed) = listed + .iter() + .find(|(place, _)| place.argument() == "late") + .unwrap(); assert!(listed.get("level").is_none()); } @@ -1681,7 +2158,7 @@ mod tests { let (_dir, paths) = scratch(); let store = open(&paths); let report = store.seal_report().unwrap(); - assert_eq!((report.sealed, report.plain), (0, 8)); + assert_eq!((report.sealed, report.plain), (0, 9)); assert!(read(&store, kind::META, MARKER).is_some()); assert!(read(&store, kind::META, PROVISIONERS_MARKER).is_some()); assert!(read(&store, kind::META, PROFILES_MARKER).is_some()); @@ -1703,8 +2180,8 @@ mod tests { let store = open(&paths); assert!(store.encrypted()); let report = store.seal_report().unwrap(); - // Two secrets, a registry, two settings and the eight markers. - assert_eq!((report.sealed, report.plain), (13, 0)); + // Two secrets, a registry, two settings and the nine markers. + assert_eq!((report.sealed, report.plain), (14, 0)); assert_eq!(read(&store, kind::SECRET, "github").unwrap(), "ghp-made-up"); } @@ -1803,12 +2280,25 @@ mod tests { assert!(read(&store, kind::META, PROVISIONERS_MARKER).is_some()); } + /// A row by the name of what it is, or of the host it belongs to. fn json_row(store: &Store, kind: &str, name: &str) -> Option { - store.read_json(kind, OWN, name).unwrap() + let of = match kind { + kind::CREDENTIALS | kind::ACCESS => kind::PROFILE, + kind => kind, + }; + let row = store + .id_of(of, OWN, name) + .unwrap() + .unwrap_or(name.to_owned()); + store.read_json(kind, OWN, &row).unwrap() } /// The resource that is the machine of a host of this installation. fn machine_of(store: &Store, host: &str) -> Option { + let host = store + .id_of(kind::PROFILE, OWN, host) + .unwrap() + .unwrap_or(host.to_owned()); store .entries(kind::RESOURCE, OWN) .unwrap() @@ -1841,7 +2331,7 @@ mod tests { record.external_id.as_str(), record.token.as_str(), record.kind.as_str(), - record.provisioner.as_str(), + crate::provisioner::label(&paths, &record.provisioner).as_str(), record.name.as_str() ), ("vm-1", "t0k", "machine", "cloud-vm", "studio") @@ -1853,10 +2343,13 @@ mod tests { ); assert!(crate::resource::valid_id(&record.id), "{}", record.id); assert!(json_row(&store, kind::INSTANCE, "studio").is_none()); - assert_eq!( - store.names(kind::PROFILE, OWN).unwrap(), - ["attic", "studio"] - ); + let hosts: Vec = store + .labels(kind::PROFILE, OWN) + .unwrap() + .into_iter() + .map(|found| found.name) + .collect(); + assert_eq!(hosts, ["attic", "studio"]); // The folder went to migrated/ whole, the lock with it; the mirror's is gone. let moved = paths.config_dir.join("migrated/hosts"); assert!(!paths.config_dir.join("hosts").exists()); @@ -2005,10 +2498,14 @@ mod tests { // Mended, it goes through. fs::write(&broken, "{}").unwrap(); run(&paths, &found, &store).unwrap(); - assert_eq!( - store.names(kind::CREDENTIALS, OWN).unwrap(), - ["attic", "studio"] - ); + let mut hosts: Vec = store + .names(kind::CREDENTIALS, OWN) + .unwrap() + .iter() + .map(|id| store.label_of(kind::PROFILE, OWN, id).unwrap().unwrap()) + .collect(); + hosts.sort(); + assert_eq!(hosts, ["attic", "studio"]); } #[test] @@ -2188,17 +2685,17 @@ mod tests { notices.iter().any(|notice| notice.contains("/nowhere/key")), "{notices:?}" ); - // The key file is a key of the ring, once; berth's key moved. - let keys = crate::keys::ring::names(&store).unwrap(); + // The key file is a key of the ring, once; berth's key moved. The + // rows are by name until the import of ids. + let keys = store.names(kind::KEY, OWN).unwrap(); assert_eq!(keys, ["berth", "deploy_key"]); - let berth = crate::keys::ring::read(&store, "berth", false) - .unwrap() - .unwrap(); + let berth: crate::keys::ring::Halves = + serde_json::from_slice(&store.read(kind::KEY, OWN, "berth").unwrap().unwrap()).unwrap(); assert_eq!(berth.private, "made-up private"); assert!(store.read(kind::KEY, OWN, "ssh.pub").unwrap().is_none()); let record = crate::keys::known::Record::load( &store, - &crate::profiles::Place::own("studio").unwrap(), + &crate::profiles::Place::at(OWN, "studio", "studio"), ) .unwrap(); assert_eq!(record.host_keys, ["ssh-ed25519 AAAAhostkey"]); @@ -2225,7 +2722,7 @@ mod tests { .map(|(scope, name, _)| access(scope, name)) .collect(); assert_eq!(before, after); - assert_eq!(crate::keys::ring::names(&store).unwrap().len(), 2); + assert_eq!(store.names(kind::KEY, OWN).unwrap().len(), 2); } #[test] diff --git a/crates/berth-core/src/kept_profile.rs b/crates/berth-core/src/kept_profile.rs index 116fef1..6de5457 100644 --- a/crates/berth-core/src/kept_profile.rs +++ b/crates/berth-core/src/kept_profile.rs @@ -16,12 +16,12 @@ use std::{io::Write, path::PathBuf, process::Stdio}; /// Where the role writes it, readable by root only. pub const PATH: &str = "/etc/berth/profile.json"; -/// The format of the document: 3 has the profile with the fields `type` -/// and `target`. Format 2 named them `level` and `profile`, and format 1, of +/// The format of the document: 4 has the host's id beside its name, and 3 +/// the profile with the fields `type` and `target`. Format 2 named them `level` and `profile`, and format 1, of /// berth 0.4.1, had the layout before `access`; both are converted when they /// are read. A berth refuses one newer than it knows, so a berth of format 2 /// asks to be updated rather than read `type` as a workstation. -pub const FORMAT: u64 = 3; +pub const FORMAT: u64 = 4; /// What a profile's `builder` held of how one berth reached the host, or of /// that berth's own machine, in the layout before `access`, and what still @@ -67,16 +67,20 @@ pub fn stored(config: &Value) -> Value { } /// The document the role writes, without the time of the write, which the -/// role adds. `host` is the name the host has in this berth. Only the record +/// role adds. `host` is the name the host has in this berth, and `id` its +/// id, which a berth that adopts the host gives it again when it can. Only the record /// of a machine that exists travels: one without the provisioner's id names /// nothing yet. What says when berth last looked at it stays here. -pub fn document(host: &str, config: &Value, machine: Option<&resource::Record>) -> Value { +pub fn document(host: &str, id: &str, config: &Value, machine: Option<&resource::Record>) -> Value { let mut document = json!({ "format": FORMAT, "berth": berth_api::VERSION, "host": host, "profile": stored(config), }); + if berth_api::ids::is_id(id) { + document["id"] = json!(id); + } if let Some(record) = machine.filter(|record| !record.external_id.is_empty()) { let mut kept = json!(record); if let Some(kept) = kept.as_object_mut() { @@ -96,7 +100,8 @@ pub fn for_host(paths: &Paths, host: &crate::host::Host) -> Value { .ok() .flatten() .map(|kept| kept.record); - let mut document = document(&host.place.title(), &host.config, machine.as_ref()); + let id = host.place.id().unwrap_or(""); + let mut document = document(&host.place.title(), id, &host.config, machine.as_ref()); // Another berth knows the provisioner by its type, not by this berth's name for it. if document["machine"].is_object() && let Some(record) = &machine @@ -109,8 +114,8 @@ pub fn for_host(paths: &Paths, host: &crate::host::Host) -> Value { /// Add the document to a profile handed to the seed builder, which puts it /// where the first boot finds it. `saved` is the profile as berth holds it. -pub fn for_seed(config: &mut Value, host: &str, saved: &Value) { - config["berth_profile"] = document(host, saved, None); +pub fn for_seed(config: &mut Value, host: &str, id: &str, saved: &Value) { + config["berth_profile"] = document(host, id, saved, None); } /// How the adopting berth reaches the host. @@ -182,6 +187,8 @@ pub struct Adopted { /// The type of the provisioner that made the machine, as `PLUGIN/ID`, /// when the document says it. pub machine_type: String, + /// The host's id where it was written, from format 4 on; empty before. + pub id: String, /// The name and berth of the write, and when it was. pub host: String, pub berth: String, @@ -282,6 +289,9 @@ pub fn adopted(paths: &Paths, title: &str, kept: &Value, access: &Access) -> Res profile, machine, machine_type, + id: Some(text("id")) + .filter(|id| berth_api::ids::is_id(id)) + .unwrap_or_default(), host: text("host"), berth: text("berth"), written: text("written"), @@ -400,8 +410,33 @@ pub fn read( } /// Save an adopted host at `place`, with the record of its machine in place -/// of one kept there before. -pub fn save(paths: &Paths, place: &Place, adopted: &Adopted) -> Result<()> { +/// of one kept there before. A host saved here for the first time takes the +/// id it kept, unless another host here has it; what berth learned of its +/// SSH on the way in goes with it. Returns where it was saved. +pub fn save(paths: &Paths, place: &Place, adopted: &Adopted) -> Result { + let place = &match place { + Place::Stored { scope, id, name } + if !adopted.id.is_empty() + && *id != adopted.id + && place.load(paths)?.is_none() + && crate::store::shared(paths)? + .label_of(crate::store::kind::PROFILE, scope, &adopted.id)? + .is_none() => + { + let store = crate::store::shared(paths)?; + let mut batch = crate::store::Batch::default(); + for kind in [crate::store::kind::ACCESS, crate::store::kind::CREDENTIALS] { + if let Some(value) = store.read(kind, scope, id)? { + batch + .write(kind, scope, &adopted.id, value) + .remove(kind, scope, id); + } + } + store.apply(&batch)?; + Place::at(scope, &adopted.id, name) + } + place => place.clone(), + }; place.save(paths, &adopted.profile)?; if let Some(old) = resource::of_place(paths, place)? { old.forget(paths)?; @@ -412,7 +447,7 @@ pub fn save(paths: &Paths, place: &Place, adopted: &Adopted) -> Result<()> { record.host = host; record.name = place.title(); record.provisioner = - crate::provisioner::adopted_name(paths, &record.provisioner, &adopted.machine_type)?; + crate::provisioner::adopted(paths, &record.provisioner, &adopted.machine_type)?; // An id this berth has already, or none of its making, is not taken over. let taken = resource::list(paths)? .iter() @@ -422,7 +457,7 @@ pub fn save(paths: &Paths, place: &Place, adopted: &Adopted) -> Result<()> { } resource::Kept { scope, record }.save(paths)?; } - Ok(()) + Ok(place.clone()) } #[cfg(test)] @@ -499,7 +534,7 @@ mod tests { #[test] fn only_the_record_of_a_machine_that_exists_travels() { let mut record = resource::Record { - id: "r-abcdefgh".into(), + id: "01972c4e8f3a7b219d4e5a6b7c8d9e0f".into(), provisioner: "incus/vm".into(), kind: "machine".into(), host: "studio".into(), @@ -507,10 +542,10 @@ mod tests { state: "running".into(), ..resource::Record::default() }; - let none = document("studio", &profile(), Some(&record)); + let none = document("studio", "", &profile(), Some(&record)); assert!(none.get("machine").is_none()); record.external_id = "berth-studio".into(); - let made = document("studio", &profile(), Some(&record)); + let made = document("studio", "", &profile(), Some(&record)); assert_eq!(made["machine"]["external_id"], "berth-studio"); assert!(made["machine"].get("host").is_none() && made["machine"].get("state").is_none()); assert_eq!( @@ -524,7 +559,7 @@ mod tests { fn a_kept_profile_read_back_applies_as_the_original() { let (_home, paths) = scratch(); let original = profile(); - let mut kept = document("studio", &original, None); + let mut kept = document("studio", "", &original, None); kept["written"] = json!("2026-11-13T10:00:00Z"); let back = adopted(&paths, "studio", &kept, &reached()).unwrap(); let dirs = json!({"demo": "/p/demo"}); @@ -555,8 +590,8 @@ mod tests { assert_eq!(back.registries(), ["nexus"]); // Kept again, it is the same document. assert_eq!( - document("studio", &back.profile, None), - document("studio", &original, None) + document("studio", "", &back.profile, None), + document("studio", "", &original, None) ); } @@ -612,8 +647,8 @@ mod tests { let back = adopted(&paths, "studio", &old("controller"), &reached()).unwrap(); assert_eq!(back.profile["type"], "managed"); // Kept again, it is written in the format of now. - let again = document("studio", &back.profile, None); - assert_eq!(again["format"], 3); + let again = document("studio", "", &back.profile, None); + assert_eq!(again["format"], 4); assert_eq!( (&again["profile"]["type"], &again["profile"]["target"]), (&json!("managed"), &json!("laptop")) @@ -646,34 +681,34 @@ mod tests { fn an_adopted_host_is_saved_with_its_machine_in_place_of_the_old_one() { let (_home, paths) = scratch(); let record = resource::Record { - id: "r-abcdefgh".into(), + id: "01972c4e8f3a7b219d4e5a6b7c8d9e1f".into(), provisioner: "incus/vm".into(), kind: "machine".into(), external_id: "berth-studio".into(), token: "0123abcd".into(), ..resource::Record::default() }; - let kept = document("old", &profile(), Some(&record)); + let kept = document("old", "", &profile(), Some(&record)); let back = adopted(&paths, "studio", &kept, &reached()).unwrap(); - let place = Place::own("studio").unwrap(); + let place = Place::own(&paths, "studio").unwrap(); save(&paths, &place, &back).unwrap(); assert_eq!(place.load(&paths).unwrap().unwrap(), back.profile); let machine = resource::of_place(&paths, &place).unwrap().unwrap(); - assert_eq!(machine.record.id, "r-abcdefgh"); + assert_eq!(machine.record.id, record.id); assert_eq!( (machine.record.host.as_str(), machine.record.name.as_str()), - ("studio", "studio") + (place.key().as_str(), "studio") ); // Saved again under another name, the id this berth has already is not taken twice. - let attic = Place::own("attic").unwrap(); + let attic = Place::own(&paths, "attic").unwrap(); save(&paths, &attic, &back).unwrap(); let other = resource::of_place(&paths, &attic).unwrap().unwrap(); - assert_ne!(other.record.id, "r-abcdefgh"); + assert_ne!(other.record.id, record.id); // Adopted again without a machine, the old record goes. let bare = adopted( &paths, "studio", - &document("old", &profile(), None), + &document("old", "", &profile(), None), &reached(), ) .unwrap(); diff --git a/crates/berth-core/src/keys/askpass.rs b/crates/berth-core/src/keys/askpass.rs index 83b1839..233018a 100644 --- a/crates/berth-core/src/keys/askpass.rs +++ b/crates/berth-core/src/keys/askpass.rs @@ -62,6 +62,10 @@ mod tests { data_dir: dir.path().join("data"), }; let store = crate::store::shared(&paths).unwrap(); + crate::profiles::Place::own(&paths, "studio") + .unwrap() + .save(&paths, &serde_json::json!({})) + .unwrap(); crate::registry::Store::own(store) .set_secret("ssh/studio", Some("kept-pw")) .unwrap(); diff --git a/crates/berth-core/src/keys/contact.rs b/crates/berth-core/src/keys/contact.rs index 20d7f61..f210752 100644 --- a/crates/berth-core/src/keys/contact.rs +++ b/crates/berth-core/src/keys/contact.rs @@ -11,7 +11,7 @@ use crate::Result; use crate::host::Host; use crate::paths::Paths; use crate::profiles::Place; -use crate::store::{OWN, Store, kind}; +use crate::store::{OWN, Store}; use berth_api::profile::{Access, Auth, BERTH_KEY}; use serde_json::Value; use std::io::Write; @@ -234,13 +234,8 @@ pub struct Rotation { /// that berth knows nothing of and does not log in with it, is left out. fn rotated(store: &Store, name: &str, old: &str, new: &str) -> Result> { let mut hosts = vec![]; - for (host, value) in store.entries(kind::PROFILE, OWN)? { - let Ok(config) = serde_json::from_slice::(&value) else { - continue; - }; - let Ok(place) = Place::own(&host) else { - continue; - }; + for (host, config) in crate::profiles::stored_entries(store, OWN)? { + let place = Place::at(OWN, &host.id, &host.name); let record = known::Record::load(store, &place)?; if wanted(&record, &config, name, old, new) { hosts.push((place, config)); @@ -375,7 +370,7 @@ mod tests { config_dir: dir.path().join("config"), data_dir: dir.path().join("data"), }; - let place = crate::profiles::Place::own("studio").unwrap(); + let place = crate::profiles::Place::own(&paths, "studio").unwrap(); let host = Host { name: "studio".into(), place: place.clone(), diff --git a/crates/berth-core/src/keys/known.rs b/crates/berth-core/src/keys/known.rs index 28e031d..4dac2fb 100644 --- a/crates/berth-core/src/keys/known.rs +++ b/crates/berth-core/src/keys/known.rs @@ -59,10 +59,14 @@ impl Record { } /// The name ssh files the host's key under: berth's own for the host, the -/// same whatever its address or port. +/// same whatever its address or port. A saved host's is its name, which +/// people see in what ssh says; a rename writes the file anew. pub fn alias(place: &Place) -> String { use sha2::{Digest, Sha256}; - let (scope, name) = place.side(); + let (scope, name) = match place { + Place::Stored { scope, name, .. } => (scope.clone(), name.clone()), + Place::File(_) => place.side(), + }; let short = |text: &str| -> String { Sha256::digest(text.as_bytes()) .iter() @@ -284,7 +288,7 @@ mod tests { data_dir: dir.path().join("data"), }; let store = Store::open(&dir.path().join("berth.db")).unwrap(); - let place = Place::own("studio").unwrap(); + let place = Place::own(&paths, "studio").unwrap(); let known_hosts = dir.path().join("known_hosts"); std::fs::write( &known_hosts, @@ -305,7 +309,7 @@ mod tests { .is_empty() ); // A port of its own is looked up as ssh writes it. - let dock = Place::own("dock").unwrap(); + let dock = Place::own(&paths, "dock").unwrap(); let config = serde_json::json!({"access": {"address": "192.0.2.11", "port": "2222"}}); std::fs::write(&known_hosts, "[192.0.2.11]:2222 ssh-ed25519 AAAAdock\n").unwrap(); let taken = take_from_user(&paths, &store, &dock, &config, &known_hosts).unwrap(); @@ -342,8 +346,12 @@ mod tests { #[test] fn aliases_are_one_word_per_place() { - let own = Place::own("studio").unwrap(); - let served = Place::stored("served:http://192.0.2.7:7777", "studio").unwrap(); + let own = Place::at("", "0197000000007000800000000000000a", "studio"); + let served = Place::at( + "served:http://192.0.2.7:7777", + "0197000000007000800000000000000b", + "studio", + ); let file = Place::File("/profiles/studio.json".into()); let names = [alias(&own), alias(&served), alias(&file)]; assert_eq!(names[0], "berth.studio"); @@ -359,7 +367,7 @@ mod tests { data_dir: dir.path().join("data"), }; let store = Store::open(&dir.path().join("berth.db")).unwrap(); - let place = Place::own("studio").unwrap(); + let place = Place::own(&paths, "studio").unwrap(); let path = prepare(&paths, &store, &place).unwrap(); assert!(!path.exists() && path.parent().unwrap().is_dir()); // What ssh writes there at first contact. diff --git a/crates/berth-core/src/keys/mod.rs b/crates/berth-core/src/keys/mod.rs index 92dec1e..6746e92 100644 --- a/crates/berth-core/src/keys/mod.rs +++ b/crates/berth-core/src/keys/mod.rs @@ -25,7 +25,7 @@ use crate::Result; use crate::link::LinkSpec; use crate::paths::Paths; use crate::profiles::Place; -use crate::store::{OWN, Store, kind}; +use crate::store::{OWN, Store}; use berth_api::profile::{Access, Auth, BERTH_KEY}; use serde_json::{Value, json}; use std::path::{Path, PathBuf}; @@ -162,7 +162,7 @@ pub fn login(paths: &Paths, store: &Store, place: &Place, config: &Value) -> Res } Auth::Password => { let secret = password_secret(place); - if store.names(kind::SECRET, OWN)?.contains(&secret) { + if crate::registry::read_secret_in(store, OWN, &secret)?.is_some() { login.askpass = Some(format!("secret:{secret}")); } } @@ -467,12 +467,10 @@ const PASSWORD_LINK: &str = r#"sh -c 'IFS= read -r p; if sudo -n true 2>/dev/nul /// The saved hosts of this installation that log in with a key of the ring. pub fn users(store: &Store, key: &str) -> Result> { - Ok(store - .entries(kind::PROFILE, OWN)? + Ok(crate::profiles::stored_entries(store, OWN)? .into_iter() - .filter_map(|(name, value)| { - let config: Value = serde_json::from_slice(&value).ok()?; - (Access::of(&config).auth.key() == Some(key)).then_some(name) + .filter_map(|(host, config)| { + (Access::of(&config).auth.key() == Some(key)).then_some(host.name) }) .collect()) } diff --git a/crates/berth-core/src/keys/refusal.rs b/crates/berth-core/src/keys/refusal.rs index 67e50f8..26f501d 100644 --- a/crates/berth-core/src/keys/refusal.rs +++ b/crates/berth-core/src/keys/refusal.rs @@ -229,7 +229,7 @@ mod tests { #[test] fn a_refused_login_waits_for_the_back_off_and_another_login_does_not() { let (_dir, paths) = scratch(); - let place = Place::own("studio").unwrap(); + let place = Place::own(&paths, "studio").unwrap(); let guard = Guard { paths: paths.clone(), place: place.clone(), @@ -273,9 +273,10 @@ mod tests { #[test] fn a_penalty_is_explained_and_waits_a_short_while() { let (_dir, paths) = scratch(); + let place = Place::own(&paths, "studio").unwrap(); let guard = Guard { paths, - place: Place::own("studio").unwrap(), + place, login: "one".into(), name: "studio".into(), }; diff --git a/crates/berth-core/src/keys/ring.rs b/crates/berth-core/src/keys/ring.rs index 35902e0..f67b70c 100644 --- a/crates/berth-core/src/keys/ring.rs +++ b/crates/berth-core/src/keys/ring.rs @@ -79,9 +79,22 @@ pub fn blob(public: &str) -> String { .join(" ") } +/// The row the store keeps a key in, or its pending successor: the key's +/// id, or `ID/pending`. `None` for a key there is not. +fn stored_row(store: &Store, name: &str, pending: bool) -> Result> { + row(name, pending)?; + Ok(store.id_of(kind::KEY, OWN, name)?.map(|id| match pending { + true => format!("{id}/pending"), + false => id, + })) +} + /// The halves of a key, or of its pending successor. pub fn read(store: &Store, name: &str, pending: bool) -> Result> { - let Some(value) = store.read(kind::KEY, OWN, &row(name, pending)?)? else { + let Some(stored) = stored_row(store, name, pending)? else { + return Ok(None); + }; + let Some(value) = store.read(kind::KEY, OWN, &stored)? else { return Ok(None); }; serde_json::from_slice(&value) @@ -89,13 +102,23 @@ pub fn read(store: &Store, name: &str, pending: bool) -> Result> .map_err(|_| format!("The key {name} in berth's store is not readable").into()) } +/// Keep a key under its name, with an id of its own when it is new; or the +/// pending successor of a key there is. fn write(store: &Store, name: &str, pending: bool, halves: &Halves) -> Result<()> { - Ok(store.write( - kind::KEY, - OWN, - &row(name, pending)?, - &serde_json::to_vec(halves)?, - )?) + let value = serde_json::to_vec(halves)?; + match (stored_row(store, name, pending)?, pending) { + (Some(stored), true) => Ok(store.write(kind::KEY, OWN, &stored, &value)?), + (None, true) => Err(no_key(name)), + (Some(id), false) => Ok(store.write_labelled(kind::KEY, OWN, &id, name, &value)?), + (None, false) => { + Ok(store.write_labelled(kind::KEY, OWN, &crate::store::new_id(), name, &value)?) + } + } +} + +/// The keys' names and ids, by name. +pub fn ids(store: &Store) -> Result> { + Ok(store.labels(kind::KEY, OWN)?) } /// Put a key that is not a real one under `name`, for a test that needs a @@ -114,9 +137,10 @@ pub fn give_test_key(store: &Store, name: &str) { /// The names of the keys, without the pending ones. Nothing is unsealed. pub fn names(store: &Store) -> Result> { Ok(store - .names(kind::KEY, OWN)? + .labels(kind::KEY, OWN)? .into_iter() - .filter(|name| !name.ends_with(PENDING) && valid_key_name(name)) + .map(|found| found.name) + .filter(|name| valid_key_name(name)) .collect()) } @@ -270,11 +294,10 @@ pub(crate) fn scratch_dir(paths: &Paths) -> Result { /// Add a key: a new one when `private` is `None`, else that private key. pub fn add(paths: &Paths, store: &Store, name: &str, private: Option<&[u8]>) -> Result { row(name, false)?; - if store - .names(kind::KEY, OWN)? - .iter() - .any(|known| known == name) - { + if berth_api::ids::looks_like_id(name) { + return Err(format!("{name} is in the shape of an id, which no key's name may be").into()); + } + if store.id_of(kind::KEY, OWN, name)?.is_some() { return Err(format!( "There is a key {name} already. Remove it first, or rotate it: berth key rotate {name}" ) @@ -311,7 +334,11 @@ pub fn remove(paths: &Paths, store: &Store, name: &str) -> Result<()> { ) .into()); } - store.change(&[], &[(kind::KEY, OWN, name)])?; + if let Some(id) = stored_row(store, name, false)? { + let mut batch = crate::store::Batch::default(); + batch.remove(kind::KEY, OWN, &id).forget_owned(OWN, &id); + store.apply(&batch)?; + } for file in [ file_path(paths, name, false)?, file_path(paths, name, true)?, @@ -382,9 +409,14 @@ pub fn finish_rotation(paths: &Paths, store: &Store, name: &str) -> Result<()> { let Some(next) = read(store, name, true)? else { return Ok(()); }; - let pending = row(name, true)?; + let (Some(id), Some(pending)) = ( + stored_row(store, name, false)?, + stored_row(store, name, true)?, + ) else { + return Ok(()); + }; store.change( - &[(kind::KEY, OWN, name, serde_json::to_vec(&next)?)], + &[(kind::KEY, OWN, id.as_str(), serde_json::to_vec(&next)?)], &[(kind::KEY, OWN, pending.as_str())], )?; let old = file_path(paths, name, false)?; diff --git a/crates/berth-core/src/keys/tests.rs b/crates/berth-core/src/keys/tests.rs index a0ed5a7..6661172 100644 --- a/crates/berth-core/src/keys/tests.rs +++ b/crates/berth-core/src/keys/tests.rs @@ -18,9 +18,12 @@ fn option<'a>(args: &'a [String], name: &str) -> Vec<&'a str> { .collect() } +/// The login of the saved host studio, whose profile has `access`. fn login_for(paths: &Paths, store: &Store, access: Value) -> Login { - let place = Place::own("studio").unwrap(); - login(paths, store, &place, &json!({"access": access})).unwrap() + let place = Place::own(paths, "studio").unwrap(); + let config = json!({"access": access}); + place.save(paths, &config).unwrap(); + login(paths, store, &place, &config).unwrap() } #[test] @@ -67,7 +70,7 @@ fn berth_and_a_named_key_offer_that_key_alone() { [paths.data_dir.join("keys/deploy").to_str().unwrap()] ); // A key that is not in the ring is an error, not ssh's defaults. - let place = Place::own("studio").unwrap(); + let place = Place::own(&paths, "studio").unwrap(); let missing = json!({"access": {"address": "192.0.2.10", "auth": "key:gone"}}); assert!(login(&paths, &store, &place, &missing).is_err()); } @@ -177,7 +180,7 @@ fn the_link_and_the_inventory_take_the_same_options() { ))); } let bad = |access: Value| { - let place = Place::own("studio").unwrap(); + let place = Place::own(&paths, "studio").unwrap(); login(&paths, &store, &place, &json!({"access": access})) .and_then(|login| login.inventory()) .is_err() @@ -228,7 +231,7 @@ fn the_ring_takes_openssh_keys_without_a_passphrase() { assert!(!serde_json::to_string(&listed).unwrap().contains("PRIVATE")); // A key a host logs in with stays. - Place::own("studio") + Place::own(&paths, "studio") .unwrap() .save( &paths, @@ -240,7 +243,7 @@ fn the_ring_takes_openssh_keys_without_a_passphrase() { .to_string(); assert!(said.contains("studio"), "{said}"); assert!(ring::remove(&paths, &store, BERTH_KEY).is_err()); - Place::own("studio") + Place::own(&paths, "studio") .unwrap() .save(&paths, &json!({"access": {"address": "192.0.2.10"}})) .unwrap(); @@ -258,9 +261,18 @@ fn a_key_name_that_leaves_the_folder_of_keys_is_refused_everywhere() { std::fs::write(&outside, "kept").unwrap(); let halves = serde_json::to_vec(&json!({"private": "x", "public": "ssh-ed25519 AAAA"})).unwrap(); - for name in ["../../outside", "../../outside.pending"] { - store.write(kind::KEY, OWN, name, &halves).unwrap(); - } + let id = crate::store::new_id(); + store + .write_labelled(crate::store::kind::KEY, OWN, &id, "../../outside", &halves) + .unwrap(); + store + .write( + crate::store::kind::KEY, + OWN, + &format!("{id}/pending"), + &halves, + ) + .unwrap(); let name = "../../outside"; assert!(ring::read(&store, name, false).is_err()); assert!(ring::file(&paths, &store, name, false).is_err()); @@ -307,6 +319,10 @@ fn child_env(command: &Command) -> std::collections::HashMap { fn no_child_ssh_can_reach_a_password_dialog_of_the_desktop() { let (_dir, paths, store) = scratch(); let helper = std::env::current_exe().unwrap().display().to_string(); + Place::own(&paths, "studio") + .unwrap() + .save(&paths, &json!({})) + .unwrap(); // A password berth has: ssh asks berth, and nothing else. crate::registry::Store::own(store.clone()) .set_secret("ssh/studio", Some("made-up-pw")) diff --git a/crates/berth-core/src/lib.rs b/crates/berth-core/src/lib.rs index 080c175..0847015 100644 --- a/crates/berth-core/src/lib.rs +++ b/crates/berth-core/src/lib.rs @@ -51,5 +51,9 @@ impl std::error::Error for Refused {} /// Whether `error` is a refusal before anything began: [`Refused`], or a /// host of a type that cannot do it. pub fn is_refused(error: &(dyn std::error::Error + 'static)) -> bool { - error.is::() || error.is::() + error.is::() + || error.is::() + || error.downcast_ref::().is_some_and(|error| { + matches!(error, store::Error::Taken { .. } | store::Error::Given(_)) + }) } diff --git a/crates/berth-core/src/node.rs b/crates/berth-core/src/node.rs index fe5ff22..43fd69d 100644 --- a/crates/berth-core/src/node.rs +++ b/crates/berth-core/src/node.rs @@ -11,8 +11,10 @@ use crate::{ host::Host, paths::Paths, plugins::{self, Plugin}, + profiles::Place, provisioner::{self, Entry}, registry::Store, + store::kind, }; use berth_api::{HostType, Offer}; use serde_json::{Value, json}; @@ -282,13 +284,7 @@ fn reaches_node( /// Why the node says the provisioner `entry` reaches the server it offers, /// which berth cannot check: it pins the certificate the node reports, which /// is public, or names a server the node gives as its own. -fn said_same( - store: &Store, - name: &str, - entry: &Entry, - offer: &Offer, - host: &Host, -) -> Option<&'static str> { +fn said_same(store: &Store, entry: &Entry, offer: &Offer, host: &Host) -> Option<&'static str> { let pins = |secret: &str, value: &str| { store .secret(secret) @@ -296,15 +292,10 @@ fn said_same( .flatten() .is_some_and(|kept| !kept.trim().is_empty() && kept.trim() == value.trim()) }; - let pinned = offer.secrets.iter().any(|(secret, value)| { - pins(&provisioner::secret_name(name, secret), value) - || (store - .secret(&provisioner::secret_name(name, secret)) - .ok() - .flatten() - .is_none() - && pins(secret, value)) - }); + let pinned = offer + .secrets + .iter() + .any(|(secret, value)| pins(&provisioner::secret_name(&entry.id, secret), value)); if pinned { return Some("it pins the certificate the node reports"); } @@ -361,6 +352,7 @@ fn register_by( ) -> Result> { let plugins = provision_plugins(paths); let wanted = features(&host.config); + let node = host.place.key(); let mut said = vec![]; let mut kept: Vec = vec![]; for offer in offers { @@ -412,7 +404,7 @@ fn register_by( let registered = provisioner::entries(paths)?; let ours = registered .iter() - .find(|(_, entry)| entry.node == host.name && entry.kind == kind); + .find(|(_, entry)| entry.node == node && entry.kind == kind); let by_hand = || { registered.iter().find(|(_, entry)| { entry.node.is_empty() @@ -424,17 +416,16 @@ fn register_by( if found.is_none() { // One the node only says is the same is left to its owner, and no second one is made beside it. let doubt = registered.iter().find_map(|(name, entry)| { - (entry.kind == kind && entry.node != host.name) - .then(|| { - said_same(store, name, entry, offer, host).map(|why| (name, entry, why)) - }) + (entry.kind == kind && entry.node != node) + .then(|| said_same(store, entry, offer, host).map(|why| (name, entry, why))) .flatten() }); match doubt { Some((name, entry, _)) if !entry.node.is_empty() => { said.push(format!( "! {} offers {kind}, and says it is the server of {name}, which is {}'s; both are left as they are", - host.name, entry.node + host.name, + node_name(paths, &entry.node) )); continue; } @@ -465,15 +456,26 @@ fn register_by( let entry = Entry { kind: kind.clone(), settings: values, - node: host.name.clone(), + node: node.clone(), feature: offer.feature.clone(), + ..before.clone().unwrap_or_default() }; - provisioner::save_entry(paths, &name, &entry)?; - for (secret, value) in &offer.secrets { + let id = provisioner::save_entry(paths, &name, &entry)?; + // The provisioner is given its own secrets alone: those the node + // reports, and the client certificate its feature makes the node trust. + let mut given: Vec<(String, String)> = offer.secrets.clone().into_iter().collect(); + if let Some((cert, key)) = &hosting.trusts { + for secret in [cert, key] { + if let Some(value) = store.secret(secret)? { + given.push((secret.clone(), value)); + } + } + } + for (secret, value) in &given { if !declared.secrets.iter().any(|known| &known.name == secret) { continue; } - let own = provisioner::secret_name(&name, secret); + let own = provisioner::secret_name(&id, secret); if store.secret(&own)?.as_deref() != Some(value.as_str()) { store.set_secret(&own, Some(value))?; } @@ -503,7 +505,7 @@ fn register_by( } } for (name, entry) in provisioner::entries(paths)? { - if entry.node != host.name || kept.contains(&name) { + if entry.node != node || kept.contains(&name) { continue; } match provisioner::remove_entry(paths, store, &name) { @@ -526,13 +528,21 @@ fn register_by( pub fn adopt(paths: &Paths, name: &str, node: &str) -> Result { let mut entry = provisioner::entry(paths, name)? .ok_or_else(|| format!("No provisioner {name} is registered"))?; + let name = entry.name.clone(); if !entry.node.is_empty() { - return Err(format!("{name} is the node {}'s already", entry.node).into()); + return Err(format!( + "{name} is the node {}'s already", + node_name(paths, &entry.node) + ) + .into()); } + let found = Place::find(paths, crate::store::OWN, node)?; let host = Host::all(paths) .into_iter() - .find(|host| host.name == node) + .find(|host| Some(&host.place) == found.as_ref()) .ok_or_else(|| format!("No host {node}"))?; + let node = host.name.clone(); + let node_id = host.place.key(); if crate::host_type::of(&host.config) != HostType::Node { return Err(format!("{node} is not a node").into()); } @@ -557,7 +567,7 @@ pub fn adopt(paths: &Paths, name: &str, node: &str) -> Result { }; if let Some((other, _)) = provisioner::entries(paths)? .into_iter() - .find(|(_, known)| known.node == node && known.kind == entry.kind) + .find(|(_, known)| known.node == node_id && known.kind == entry.kind) { return Err(format!( "{node} has the provisioner {other} of the type {} already. Remove it first: berth provisioner run {other} remove", @@ -565,9 +575,9 @@ pub fn adopt(paths: &Paths, name: &str, node: &str) -> Result { ) .into()); } - entry.node = node.to_owned(); + entry.node = node_id; entry.feature = offering; - provisioner::save_entry(paths, name, &entry)?; + provisioner::save_entry(paths, &entry.id.clone(), &entry)?; Ok(format!( "{name} is {node}'s now. The next apply of {node} brings its settings and the certificate it pins up to date: berth apply {node}" )) @@ -584,10 +594,10 @@ pub fn check_removal(paths: &Paths, host: &Host) -> Result<()> { let wanted = features(&host.config); let applied = crate::host_type::Capability::Plugins.allowed(crate::host_type::of(&host.config)); for (name, entry) in provisioner::entries(paths)? { - if entry.node != host.name || (applied && wanted.contains(&entry.feature)) { + if entry.node != host.place.key() || (applied && wanted.contains(&entry.feature)) { continue; } - let held = holds(paths, &name)?; + let held = holds(paths, &entry.id)?; if !held.is_empty() { return Err(format!( "{} offers the provisioner {name} through {}, which still holds {}. Remove {} first (berth resource ID remove), or keep {} among the features of {}", @@ -604,28 +614,31 @@ pub fn check_removal(paths: &Paths, host: &Host) -> Result<()> { Ok(()) } -/// The resources a registered provisioner holds, by id. -fn holds(paths: &Paths, name: &str) -> Result> { +/// The resources the registered provisioner with the id holds, by id. +fn holds(paths: &Paths, id: &str) -> Result> { Ok(crate::resource::list(paths)? .into_iter() - .filter(|kept| kept.record.provisioner == name) + .filter(|kept| kept.record.provisioner == id) .map(|kept| kept.record.id) .collect()) } -/// Forget the provisioners of the node `name` before the host goes: refused -/// while one still holds resources, as these would have no provisioner. -pub fn forget(paths: &Paths, store: &Store, name: &str) -> Result<()> { - let ours: Vec = provisioner::entries(paths)? +/// Forget the provisioners of the node with the id `node` before the host +/// goes: refused while one still holds resources, as these would have no +/// provisioner. +pub fn forget(paths: &Paths, store: &Store, node: &str) -> Result<()> { + let ours: Vec = provisioner::entries(paths)? .into_iter() - .filter(|(_, entry)| entry.node == name) - .map(|(provisioner, _)| provisioner) + .filter(|(_, entry)| entry.node == node) + .map(|(_, entry)| entry) .collect(); for provisioner in &ours { - let held = holds(paths, provisioner)?; + let held = holds(paths, &provisioner.id)?; if !held.is_empty() { return Err(format!( - "{name} is the node of the provisioner {provisioner}, which still holds {}. Remove {} first: berth resource ID remove", + "{} is the node of the provisioner {}, which still holds {}. Remove {} first: berth resource ID remove", + node_name(paths, node), + provisioner.name, held.join(", "), if held.len() == 1 { "it" } else { "them" } ) @@ -633,20 +646,22 @@ pub fn forget(paths: &Paths, store: &Store, name: &str) -> Result<()> { } } for provisioner in ours { - provisioner::remove_entry(paths, store, &provisioner)?; + provisioner::remove_entry(paths, store, &provisioner.id)?; } Ok(()) } -/// Give the provisioners of the node `old` its new name. -pub fn rename(paths: &Paths, old: &str, new: &str) -> Result<()> { - for (name, mut entry) in provisioner::entries(paths)? { - if entry.node == old { - entry.node = new.to_owned(); - provisioner::save_entry(paths, &name, &entry)?; - } - } - Ok(()) +/// The name of the host with the id, or the id when there is none. +pub fn node_name(paths: &Paths, id: &str) -> String { + crate::store::shared(paths) + .ok() + .and_then(|store| { + store + .label_of(kind::PROFILE, crate::store::OWN, id) + .ok() + .flatten() + }) + .unwrap_or_else(|| id.to_owned()) } /// What the node `host` reported at the end of its last apply, asked of its @@ -735,7 +750,7 @@ mod tests { let config = json!({"type": "node", "access": {"address": "192.0.2.20", "user": "root"}, "ansible_vars": {"devbox_plugin_features": features}}); - let place = Place::own("big").unwrap(); + let place = Place::own(&self.paths, "big").unwrap(); place.save(&self.paths, &config).unwrap(); Host { name: "big".into(), @@ -753,7 +768,7 @@ mod tests { scope: crate::store::OWN.into(), record: crate::resource::Record { id: "r1".into(), - provisioner: provisioner.into(), + provisioner: provisioner::reference(&self.paths, provisioner).unwrap(), kind: "machine".into(), name: "studio-2".into(), ..Default::default() @@ -791,8 +806,9 @@ mod tests { .unwrap(); assert_eq!( (entry.node.as_str(), entry.feature.as_str()), - ("big", "fake/engine") + (host.place.key().as_str(), "fake/engine") ); + assert_eq!(node_name(&world.paths, &entry.node), "big"); assert_eq!(entry.settings["server"], "https://192.0.2.20:8444"); assert_eq!( world @@ -855,6 +871,7 @@ mod tests { let host = world.host(&["fake/engine"]); // As the recipe registered it: its own name and setting, the shared certificate. hand(&world, "incus-vm", "https://incus-host:8444"); + // A secret all share of the name is never the provisioner's. world.store.set_secret("server_cert", Some("CERT")).unwrap(); hand(&world, "elsewhere", "https://192.0.2.99:8444"); let said = register_by(&world.paths, &world.store, &host, &[offer()], &lookup).unwrap(); @@ -869,7 +886,7 @@ mod tests { .unwrap(); assert_eq!( (taken.node.as_str(), taken.feature.as_str()), - ("big", "fake/engine") + (host.place.key().as_str(), "fake/engine") ); // Its setting reaches the server already, and stays. assert_eq!(taken.settings["server"], "https://incus-host:8444"); @@ -894,7 +911,7 @@ mod tests { .unwrap() .unwrap() .node, - "big" + host.place.key() ); // Another port of the same machine is another server. @@ -912,6 +929,7 @@ mod tests { let mut host = world.host(&["fake/engine"]); host.config["access"]["address"] = json!("big.example.org"); hand(&world, "incus-vm", "https://incus-host:8444"); + // A secret all share of the name is never the provisioner's. world.store.set_secret("server_cert", Some("CERT")).unwrap(); let mut aliased = offer(); aliased @@ -927,7 +945,7 @@ mod tests { .unwrap(); assert!( said.iter().any(|line| line - == "! big offers fake/vm, and incus-vm may reach the same server (it pins the certificate the node reports), which berth cannot check; both are left as they are. To make incus-vm big's: berth provisioner run incus-vm adopt node=big"), + == "! big offers fake/vm, and incus-vm may reach the same server (it names a server the node gives as its own), which berth cannot check; both are left as they are. To make incus-vm big's: berth provisioner run incus-vm adopt node=big"), "{said:?}" ); // Nothing changed, and there is no second one beside it. @@ -952,7 +970,7 @@ mod tests { .unwrap(); assert_eq!( (adopted.node.as_str(), adopted.feature.as_str()), - ("big", "fake/engine") + (host.place.key().as_str(), "fake/engine") ); let said = register_by(&world.paths, &world.store, &host, &[aliased], &lookup).unwrap(); assert!( @@ -1085,6 +1103,7 @@ mod tests { settings: [("server".into(), "https://192.0.2.40:8444".into())].into(), node: "dock".into(), feature: "fake/engine".into(), + ..Entry::default() }, ) .unwrap(); @@ -1271,13 +1290,12 @@ mod tests { "{refused}" ); let renamed = crate::profiles::rename(&world.paths, &host.place, "huge", false).unwrap(); - assert_eq!( - provisioner::entry(&world.paths, "big-fake-vm") - .unwrap() - .unwrap() - .node, - "huge" - ); + let node = provisioner::entry(&world.paths, "big-fake-vm") + .unwrap() + .unwrap() + .node; + assert_eq!(node, host.place.key()); + assert_eq!(node_name(&world.paths, &node), "huge"); crate::profiles::rename(&world.paths, &renamed, "big", false).unwrap(); // Once the machine is gone, so is the provisioner. crate::store::shared(&world.paths) @@ -1378,7 +1396,7 @@ mod tests { fn offered_values_name_where_berth_reaches_the_node() { let host = |access: Value| Host { name: "big".into(), - place: Place::own("big").unwrap(), + place: Place::at("", "0197000000007000800000000000000c", "big"), config: json!({"type": "node", "access": access}), }; let root = host(json!({"address": "192.0.2.20", "user": "root"})); diff --git a/crates/berth-core/src/paths.rs b/crates/berth-core/src/paths.rs index 576a316..8ccd369 100644 --- a/crates/berth-core/src/paths.rs +++ b/crates/berth-core/src/paths.rs @@ -458,7 +458,12 @@ impl Paths { crate::profiles::list(self, &crate::profiles::scope(self)) .unwrap_or_default() .into_iter() - .map(|(name, profile)| (name, berth_api::profile::Access::of(&profile).address)) + .map(|(place, profile)| { + ( + place.argument(), + berth_api::profile::Access::of(&profile).address, + ) + }) .collect() } } @@ -512,11 +517,11 @@ mod tests { data_dir: home.path().join("data"), }; paths.prepare().unwrap(); - crate::profiles::Place::own("laptop") + crate::profiles::Place::own(&paths, "laptop") .unwrap() .save(&paths, &json!({"access": {"address": "100.64.0.1"}})) .unwrap(); - crate::profiles::Place::own("attic") + crate::profiles::Place::own(&paths, "attic") .unwrap() .save(&paths, &json!({})) .unwrap(); diff --git a/crates/berth-core/src/plugins.rs b/crates/berth-core/src/plugins.rs index 110853e..d0d74e9 100644 --- a/crates/berth-core/src/plugins.rs +++ b/crates/berth-core/src/plugins.rs @@ -198,6 +198,7 @@ impl Provisioner { pub fn info(&self, plugin: &str, missing_secrets: Vec) -> ProvisionerInfo { ProvisionerInfo { id: format!("{plugin}/{}", self.id), + name: format!("{plugin}/{}", self.id), kind: format!("{plugin}/{}", self.id), label: self.label.clone(), description: self.description.clone(), @@ -213,6 +214,7 @@ impl Provisioner { own: self.own.clone(), settings: Default::default(), node: String::new(), + node_id: String::new(), kinds: self.kinds.clone(), actions: self.actions.clone(), collections: self.collections.clone(), diff --git a/crates/berth-core/src/profiles.rs b/crates/berth-core/src/profiles.rs index 3956565..ffdfb10 100644 --- a/crates/berth-core/src/profiles.rs +++ b/crates/berth-core/src/profiles.rs @@ -19,8 +19,13 @@ pub const FILE_SCOPE: &str = "file"; /// Where a host's profile is. #[derive(Debug, Clone, PartialEq, Eq)] pub enum Place { - /// A saved host of this installation or of the berth server it uses. - Stored { scope: String, name: String }, + /// A saved host of this installation or of the berth server it uses: + /// its id and its name. A host not saved yet has the id it will get. + Stored { + scope: String, + id: String, + name: String, + }, /// A profile in a file the user named; berth reads and writes that file. File(PathBuf), } @@ -32,7 +37,7 @@ pub fn scope(_: &Paths) -> String { } /// Whether a name can be a saved host's: it is a file name of its own on -/// every machine, and no option of a command. +/// every machine, no option of a command and not in the shape of an id. pub fn valid_name(name: &str) -> bool { !name.is_empty() && !name.starts_with(['-', '.']) @@ -40,6 +45,7 @@ pub fn valid_name(name: &str) -> bool { && name .chars() .all(|ch| ch.is_ascii_alphanumeric() || "-_.".contains(ch)) + && !berth_api::ids::looks_like_id(name) } /// A short digest of a text, as a part of a file name. @@ -54,32 +60,57 @@ fn digest(text: &str, length: usize) -> String { impl Place { /// What a host given on a command line is: anything that looks like a - /// path is a file, and a name is a saved host of [`scope`]. + /// path is a file, and a name or an id is a saved host of [`scope`]. pub fn resolve(paths: &Paths, host: &str) -> Result { if host.contains('/') || host.ends_with(".json") { return Ok(Place::File(std::path::absolute(host)?)); } - Place::stored(&scope(paths), host) + Place::stored(paths, &scope(paths), host) } - /// A saved host of a scope, by a name that is checked. - pub fn stored(scope: &str, name: &str) -> Result { + /// The saved host of a scope that a name or id gives, if there is one. + pub fn find(paths: &Paths, scope: &str, given: &str) -> Result> { + let found = store::shared(paths)?.find(kind::PROFILE, scope, given)?; + Ok(found.map(|found| Place::at(scope, &found.id, &found.name))) + } + + /// The saved host of a scope that a name or id gives, or a host not + /// saved yet under a name that is checked, with a new id. + pub fn stored(paths: &Paths, scope: &str, given: &str) -> Result { + if let Some(place) = Place::find(paths, scope, given)? { + return Ok(place); + } + if berth_api::ids::id_in(berth_api::ids::Kind::Host, given).is_some() { + return Err(format!("No host {given}").into()); + } + Place::new(scope, given) + } + + /// A host of a scope not saved yet, under a name that is checked, with a + /// new id. + pub fn new(scope: &str, name: &str) -> Result { if !valid_name(name) { return Err(format!("Not a host name: {name}").into()); } - Ok(Place::Stored { - scope: scope.to_owned(), - name: name.to_owned(), - }) + Ok(Place::at(scope, &store::new_id(), name)) + } + + /// A saved host of this installation's own, by its name or id. + pub fn own(paths: &Paths, given: &str) -> Result { + Place::stored(paths, OWN, given) } - /// A saved host of this installation's own. - pub fn own(name: &str) -> Result { - Place::stored(OWN, name) + /// A saved host by what the store keeps of it. + pub fn at(scope: &str, id: &str, name: &str) -> Place { + Place::Stored { + scope: scope.to_owned(), + id: id.to_owned(), + name: name.to_owned(), + } } - /// What names the host to another berth command: the saved name, or the - /// file's absolute path. + /// What names the host to another berth command: the saved name, or + /// the file's absolute path. pub fn argument(&self) -> String { match self { Place::Stored { name, .. } => name.clone(), @@ -87,6 +118,23 @@ impl Place { } } + /// What the host is known by in records and jobs: the saved host's id, + /// or the file's absolute path. + pub fn key(&self) -> String { + match self { + Place::Stored { id, .. } => id.clone(), + Place::File(path) => path.to_string_lossy().into_owned(), + } + } + + /// The saved host's id. + pub fn id(&self) -> Option<&str> { + match self { + Place::Stored { id, .. } => Some(id), + Place::File(_) => None, + } + } + /// The host's name for people: the saved name, or the file's name /// without `.json`. pub fn title(&self) -> String { @@ -107,10 +155,11 @@ impl Place { } } - /// The scope and name of what is kept beside the profile. + /// The scope and name of what is kept beside the profile: the saved + /// host's id, or the file's path. pub fn side(&self) -> (String, String) { match self { - Place::Stored { scope, name } => (scope.clone(), name.clone()), + Place::Stored { scope, id, .. } => (scope.clone(), id.clone()), Place::File(path) => (FILE_SCOPE.to_owned(), path.to_string_lossy().into_owned()), } } @@ -127,16 +176,18 @@ impl Place { } } - /// The profile, or `None` when there is none. + /// The profile, or `None` when there is none. It names its registries + /// and secrets by their names, as people read it. pub fn load(&self, paths: &Paths) -> Result> { match self { - Place::Stored { scope, name } => { - let mut profile: Option = - store::shared(paths)?.read_json(kind::PROFILE, scope, name)?; + Place::Stored { scope, id, .. } => { + let store = store::shared(paths)?; + let mut profile: Option = store.read_json(kind::PROFILE, scope, id)?; // A row an older berth or berth server wrote after the store's // names were converted is read with the names of now. if let Some(profile) = &mut profile { berth_api::profile::take_old_names(profile); + refs::to_names(&store, scope, profile)?; } Ok(profile) } @@ -147,6 +198,8 @@ impl Place { // A profile of an older berth is read in the layout of now; // the file keeps the old one until the user saves. crate::import::convert_profile(paths, &self.title(), &mut profile); + let store = store::shared(paths)?; + refs::to_names(&store, OWN, &mut profile)?; Ok(Some(profile)) } Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(None), @@ -155,15 +208,27 @@ impl Place { } } - /// Save the profile in place of the one there was. A file is written - /// privately and in one step. + /// Save the profile in place of the one there was. The store keeps the + /// ids of the registries and secrets it names, by name or id; a file + /// keeps their names, and is written privately and in one step. pub fn save(&self, paths: &Paths, profile: &Value) -> Result<()> { + let store = store::shared(paths)?; match self { - Place::Stored { scope, name } => { - Ok(store::shared(paths)?.write_json(kind::PROFILE, scope, name, profile)?) + Place::Stored { scope, id, name } => { + let mut profile = profile.clone(); + refs::to_ids(&store, scope, &mut profile)?; + Ok(store.write_labelled( + kind::PROFILE, + scope, + id, + name, + profile.to_string().as_bytes(), + )?) } Place::File(path) => { - let text = serde_json::to_string_pretty(profile)? + "\n"; + let mut profile = profile.clone(); + refs::to_names(&store, OWN, &mut profile)?; + let text = serde_json::to_string_pretty(&profile)? + "\n"; crate::paths::write_private(path, text.as_bytes()) } } @@ -174,62 +239,204 @@ impl Place { /// a provisioning takes, which a row of the store cannot be. pub fn lock_file(&self, paths: &Paths) -> PathBuf { let name = match self { - Place::Stored { scope, name } if scope == OWN => format!("{name}.lock"), - Place::Stored { scope, name } => format!("{}.{name}.lock", digest(scope, 8)), + Place::Stored { scope, id, .. } if scope == OWN => format!("{id}.lock"), + Place::Stored { scope, id, .. } => format!("{}.{id}.lock", digest(scope, 8)), Place::File(path) => format!("file.{}.lock", digest(&path.to_string_lossy(), 16)), }; paths.data_dir.join("locks").join(name) } } -/// The saved hosts of a scope with their profiles, by name. -pub fn list(paths: &Paths, scope: &str) -> Result> { - let entries = store::shared(paths)?.entries(kind::PROFILE, scope)?; - Ok(entries - .into_iter() - .filter_map(|(name, value)| { - let mut profile = serde_json::from_slice(&value).ok()?; - berth_api::profile::take_old_names(&mut profile); - Some((name, profile)) +/// How a profile refers to registries and secrets: by name where people +/// read it, by id in the store. A name that has no thing yet stays a name, +/// and is taken for the thing's id when one is made with it. +pub mod refs { + use crate::{ + Result, + store::{Store, kind}, + }; + use serde_json::Value; + + /// The places in a profile that refer to things: the kind and each + /// reference, as a value to read or change. + fn each( + profile: &mut Value, + mut visit: impl FnMut(&str, &mut Value) -> Result<()>, + ) -> Result<()> { + if let Some(secrets) = profile.get_mut("secrets").and_then(Value::as_array_mut) { + for entry in secrets { + if let Some(name) = entry.get_mut("name") { + visit(kind::SECRET, name)?; + } + } + } + if let Some(registries) = profile.get_mut("registries").and_then(Value::as_array_mut) { + for name in registries { + visit(kind::REGISTRY, name)?; + } + } + // A key of the ring is `key:KEY`. + if let Some(auth) = profile.pointer_mut("/access/auth") + && let Some(key) = auth.as_str().and_then(|text| text.strip_prefix("key:")) + { + let mut key = Value::String(key.to_owned()); + visit(kind::KEY, &mut key)?; + *auth = Value::String(format!("key:{}", key.as_str().unwrap_or(""))); + } + Ok(()) + } + + /// The things a profile refers to, as the store keeps them: their ids. + pub fn to_ids(store: &Store, scope: &str, profile: &mut Value) -> Result<()> { + each(profile, |kind, value| { + if let Some(given) = value.as_str() + && let Some(found) = store.find(kind, scope, given)? + { + *value = Value::String(found.id); + } + Ok(()) + }) + } + + /// The things a profile refers to, by name. + pub fn to_names(store: &Store, scope: &str, profile: &mut Value) -> Result<()> { + each(profile, |kind, value| { + if let Some(id) = value.as_str().filter(|text| berth_api::ids::is_id(text)) + && let Some(name) = store.label_of(kind, scope, id)? + { + *value = Value::String(name); + } + Ok(()) }) - .collect()) + } + + /// Whether a profile as the store keeps it refers to the thing. + pub fn refers_to(profile: &Value, kind: &str, id: &str, name: &str) -> bool { + let mut profile = profile.clone(); + let mut found = false; + let _ = each(&mut profile, |of, value| { + found |= of == kind && (value == id || value == name); + Ok(()) + }); + found + } + + /// A profile as the store keeps it with the references to a thing + /// changed: its name taken for its id, or its id for its name. + pub fn swap(profile: &mut Value, kind: &str, from: &str, to: &str) -> bool { + let mut changed = false; + let _ = each(profile, |of, value| { + if of == kind && value == from { + *value = Value::String(to.to_owned()); + changed = true; + } + Ok(()) + }); + changed + } +} + +/// The saved hosts of a scope with their profiles as people read them, from +/// a store that is open. +pub fn stored_entries(store: &store::Store, scope: &str) -> Result> { + let mut found = vec![]; + for (host, value) in store.labelled_entries(kind::PROFILE, scope)? { + let Ok(mut profile) = serde_json::from_slice::(&value) else { + continue; + }; + refs::to_names(store, scope, &mut profile)?; + found.push((host, profile)); + } + Ok(found) } -/// 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 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. +/// In a batch, change the references to a thing in every saved host's +/// profile of a scope, from `from` to `to`: a name to the id of a thing +/// made under it, or the id of a thing removed to its name. +pub fn swap_references( + store: &store::Store, + batch: &mut store::Batch, + scope: &str, + kind: &str, + from: &str, + to: &str, +) -> Result<()> { + for (found, value) in store.labelled_entries(kind::PROFILE, scope)? { + let Ok(mut profile) = serde_json::from_slice::(&value) else { + continue; + }; + if refs::swap(&mut profile, kind, from, to) { + batch.write( + kind::PROFILE, + scope, + &found.id, + serde_json::to_vec(&profile)?, + ); + } + } + Ok(()) +} + +/// The saved hosts of a scope with their profiles, by name. +pub fn list(paths: &Paths, scope: &str) -> Result> { + let store = store::shared(paths)?; + let entries = store.labelled_entries(kind::PROFILE, scope)?; + let mut found = vec![]; + for (host, value) in entries { + let Ok(mut profile) = serde_json::from_slice(&value) else { + continue; + }; + berth_api::profile::take_old_names(&mut profile); + refs::to_names(&store, scope, &mut profile)?; + found.push((Place::at(scope, &host.id, &host.name), profile)); + } + Ok(found) +} + +/// Forget a host: its profile and everything that belongs to it, its +/// generated passwords, the record of a 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. - if scope == OWN { + if let Place::Stored { scope, id, .. } = place + && scope == OWN + { let secrets = crate::registry::Store::own(store::shared(paths)?); - crate::node::forget(paths, &secrets, &name)?; + crate::node::forget(paths, &secrets, id)?; } - let password = crate::keys::password_secret(place); - let sudo = crate::keys::sudo_secret(place); // The record of a machine that was never made goes with the host; a // made one is refused before this. let machine = crate::resource::of_place(paths, place)? - .map(|kept| kept.record.id) - .unwrap_or_default(); - 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())); - removals.push((kind::SPEC, scope.as_str(), machine.as_str())); - } - if scope == OWN || matches!(place, Place::File(_)) { - removals.push((kind::SECRET, OWN, password.as_str())); - removals.push((kind::SECRET, OWN, sudo.as_str())); - } - if let Place::Stored { .. } = place { - removals.push((kind::PROFILE, scope.as_str(), name.as_str())); - } - store::shared(paths)?.change(&[], &removals)?; + .map(|kept| (kept.scope, kept.record.id)) + .filter(|(_, id)| !id.is_empty()); + let mut batch = store::Batch::default(); + if let Some((scope, id)) = &machine { + batch + .remove(kind::RESOURCE, scope, id) + .forget_owned(scope, id); + } + match place { + Place::Stored { scope, id, .. } => { + batch + .remove(kind::PROFILE, scope, id) + .forget_owned(scope, id); + if scope != OWN { + batch.forget_owned(OWN, id); + } + } + Place::File(_) => { + batch + .remove(kind::CREDENTIALS, &scope, &name) + .remove(kind::ACCESS, &scope, &name) + .remove(kind::TAILNET, &scope, &name) + .remove(kind::SECRET, OWN, &crate::keys::password_secret(place)) + .remove(kind::SECRET, OWN, &crate::keys::sudo_secret(place)); + } + } + store::shared(paths)?.apply(&batch)?; if let Place::File(path) = place { match fs::remove_file(path) { Err(error) if error.kind() != std::io::ErrorKind::NotFound => { @@ -255,115 +462,62 @@ 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, 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. +/// Give a saved host another name in its scope. Everything that belongs to +/// it, its generated passwords, what berth knows of its SSH, its passwords +/// for logging in, its device on the tailnet, its resources, messages and +/// jobs, stays with it, as it is kept by the host's id. 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 { + let Place::Stored { + scope, + id, + name: old, + } = from + else { return Err("A profile in a file is renamed by moving the file".into()); }; - let place = Place::stored(scope, to)?; + if !valid_name(to) { + return Err(format!("Not a host name: {to}").into()); + } if old == to { return Err(format!("{old} has that name already").into()); } - // Nothing is done to either name meanwhile, such as creating its machine. - fn busy(name: &str) -> impl FnOnce(&str) -> String + '_ { - move |holder| { - format!("Something else is being done to {name}{holder}. Try again when it has ended") - } - } - let held = crate::runner::lock_at(&from.lock_file(paths), "rename the host", busy(old))?; - let _new = crate::runner::lock_at(&place.lock_file(paths), "rename the host", busy(to))?; - let mut profile = from.load(paths)?.ok_or_else(|| format!("No host {old}"))?; - if place.load(paths)?.is_some() { + // Nothing is done to the host meanwhile, such as creating its machine. + let held = crate::runner::lock_at(&from.lock_file(paths), "rename the host", |holder| { + format!("Something else is being done to {old}{holder}. Try again when it has ended") + })?; + let store = store::shared(paths)?; + let mut profile: Value = store + .read_json(kind::PROFILE, scope, id)? + .ok_or_else(|| format!("No host {old}"))?; + if Place::find(paths, scope, to)?.is_some() { return Err(format!("There is a host {to} already").into()); } if hostname { hostname_allowed(old, &profile)?; profile["hostname"] = serde_json::json!(to); } - let store = store::shared(paths)?; - let mut writes = vec![( - kind::PROFILE, - scope.as_str(), - to, - serde_json::to_vec(&profile)?, - )]; - let mut removals = vec![(kind::PROFILE, scope.as_str(), old.as_str())]; - let mut moved = vec![ - (kind::CREDENTIALS, scope.clone(), old.clone(), to.to_owned()), - (kind::ACCESS, scope.clone(), old.clone(), to.to_owned()), - (kind::TAILNET, scope.clone(), old.clone(), to.to_owned()), - ]; - if scope == OWN { - for secret in [crate::keys::password_secret, crate::keys::sudo_secret] { - moved.push((kind::SECRET, OWN.into(), secret(from), secret(&place))); - } - } - for (kind, scope, old, new) in &moved { - if let Some(value) = store.read(kind, scope, old)? { - writes.push((kind, scope.as_str(), new.as_str(), value)); - removals.push((kind, scope.as_str(), old.as_str())); - } - } + let mut batch = store::Batch::default(); + batch.write_labelled(kind::PROFILE, scope, id, to, serde_json::to_vec(&profile)?); // The device on the tailnet takes the new hostname at the next apply, // when the machine has it too. if hostname && berth_api::profile::joins_tailnet(&profile) { - let mut kept = crate::tailnet::Kept::read(&store, scope, old)?; + let mut kept = crate::tailnet::Kept::read(&store, scope, id)?; kept.rename = Some(to.to_owned()); - writes.retain(|(kind, ..)| *kind != kind::TAILNET); - writes.push(( - kind::TAILNET, - scope.as_str(), - to, - serde_json::to_vec(&kept)?, - )); - } - // A resource keeps its id and its name at the provisioner; only the host - // it belongs to is named anew. - let mut resources = vec![]; - for (id, value) in store.entries(kind::RESOURCE, scope)? { - let mut record: crate::resource::Record = - serde_json::from_slice(&value).map_err(|error| { - format!("The record of the resource {id} in berth's store: {error}") - })?; - if record.host == *old { - record.host = to.to_owned(); - resources.push((id, serde_json::to_vec(&record)?)); - } + batch.write(kind::TAILNET, scope, id, serde_json::to_vec(&kept)?); } - for (id, value) in &resources { - writes.push((kind::RESOURCE, scope.as_str(), id.as_str(), value.clone())); - } - // The provisioners a node offers name it, and go on naming it. - let mut offered = vec![]; - if scope == OWN { - for (name, value) in store.entries(kind::PROVISIONER_ENTRY, OWN)? { - let Ok(mut entry) = serde_json::from_slice::(&value) else { - continue; - }; - if entry.node == *old { - entry.node = to.to_owned(); - offered.push((name, serde_json::to_vec(&entry)?)); - } + match store.apply(&batch) { + Err(store::Error::Taken { .. }) => { + return Err(format!("There is a host {to} already").into()); } - } - for (name, value) in &offered { - writes.push((kind::PROVISIONER_ENTRY, OWN, name.as_str(), value.clone())); - } - match scope == OWN { - true => store.rename_host(old, to, &writes, &removals)?, - false => store.change(&writes, &removals)?, + result => result?, } // ssh's file for the host is written from its row under the new alias. let _ = fs::remove_file(crate::keys::known::file(paths, from)); drop(held); - let _ = fs::remove_file(from.lock_file(paths)); - Ok(place) + Ok(Place::at(scope, id, to)) } /// What a copy of `profile` under the name `to` keeps: everything but what @@ -406,20 +560,23 @@ pub fn duplicate( from: &Place, to: &str, ) -> Result<(Place, Option)> { - let Place::Stored { scope, name: old } = from else { + let Place::Stored { + scope, name: old, .. + } = from + else { return Err("A profile in a file is copied by copying the file".into()); }; - let place = Place::stored(scope, to)?; if old == to { return Err(format!("{old} has that name already").into()); } + if Place::find(paths, scope, to)?.is_some() { + return Err(format!("There is a host {to} already").into()); + } + let place = Place::new(scope, to)?; let _new = crate::runner::lock_at(&place.lock_file(paths), "duplicate the host", |holder| { format!("Something else is being done to {to}{holder}. Try again when it has ended") })?; let profile = from.load(paths)?.ok_or_else(|| format!("No host {old}"))?; - if place.load(paths)?.is_some() { - return Err(format!("There is a host {to} already").into()); - } let generated: Vec = crate::credentials::load(paths, from)? .keys() .cloned() @@ -456,19 +613,28 @@ mod tests { #[test] fn a_name_is_a_saved_host_and_a_path_a_file() { let (home, paths) = scratch(); - assert_eq!( - Place::resolve(&paths, "studio").unwrap(), - Place::Stored { - scope: OWN.into(), - name: "studio".into() - } + let resolved = Place::resolve(&paths, "studio").unwrap(); + assert!( + matches!(&resolved, Place::Stored { scope, id, name } + if scope == OWN && name == "studio" && berth_api::ids::is_id(id)), + "{resolved:?}" ); let file = Place::resolve(&paths, "../studio.json").unwrap(); assert!(matches!(&file, Place::File(path) if path.is_absolute())); assert_eq!(file.title(), "studio"); - for bad in ["-x", ".hidden", "a b", ""] { + for bad in [ + "-x", + ".hidden", + "a b", + "", + "01972c4e8f3a7b219d4e5a6b7c8d9e0f", + "host_01972c4e8f3a7b219d4e5a6b7c8d9e0f", + ] { assert!(Place::resolve(&paths, bad).is_err(), "{bad}"); } + // The id of another kind is refused. + let error = Place::resolve(&paths, "prov_01972c4e8f3a7b219d4e5a6b7c8d9e0f").unwrap_err(); + assert!(error.to_string().contains("provisioner"), "{error}"); // A berth server set for the commands leaves the saved hosts its own. paths.set("server", "http://192.0.2.7:7777").unwrap(); let own = Place::resolve(&paths, "studio").unwrap(); @@ -483,8 +649,8 @@ mod tests { #[test] fn profiles_are_loaded_saved_listed_and_removed_in_their_place() { let (home, paths) = scratch(); - let own = Place::own("studio").unwrap(); - let served = Place::stored("served:http://192.0.2.7:7777", "studio").unwrap(); + let own = Place::own(&paths, "studio").unwrap(); + let served = Place::stored(&paths, "served:http://192.0.2.7:7777", "studio").unwrap(); let file = Place::File(home.path().join("elsewhere/attic.json")); for (place, address) in [ (&own, "192.0.2.10"), @@ -503,7 +669,7 @@ mod tests { list(&paths, scope) .unwrap() .into_iter() - .map(|(name, _)| name) + .map(|(place, _)| place.argument()) .collect() }; assert_eq!(names(OWN), ["studio"]); @@ -547,9 +713,10 @@ mod tests { } #[test] - fn a_renamed_host_takes_what_berth_keeps_of_it() { + fn a_renamed_host_keeps_its_id_and_what_belongs_to_it() { let (_home, paths) = scratch(); - let test = Place::own("test").unwrap(); + let test = Place::own(&paths, "test").unwrap(); + let id = test.key(); let profile = json!({"hostname": "test", "access": {"address": "192.0.2.10"}}); test.save(&paths, &profile).unwrap(); crate::credentials::save(&paths, &test, "ops", "made-up-1").unwrap(); @@ -558,29 +725,30 @@ mod tests { .write_json( kind::ACCESS, OWN, - "test", + &id, &json!({"host_keys": ["ssh-ed25519 AAAA"]}), ) .unwrap(); - store - .write(kind::SECRET, OWN, "ssh/test", b"made-up-2") - .unwrap(); - store - .write(kind::SECRET, OWN, "ssh/lab", b"made-up-3") + let secrets = crate::registry::Store::own(store.clone()); + secrets.set_secret("ssh/test", Some("made-up-2")).unwrap(); + Place::own(&paths, "lab") + .unwrap() + .save(&paths, &json!({"access": {"address": "192.0.2.11"}})) .unwrap(); + secrets.set_secret("ssh/lab", Some("made-up-3")).unwrap(); let machine = crate::resource::Record { - id: "r-test".into(), + id: crate::store::new_id(), provisioner: "incus".into(), kind: crate::plugins::MACHINE.into(), name: "berth-test".into(), - host: "test".into(), + host: id.clone(), ..Default::default() }; store .write_json( kind::RESOURCE, OWN, - "r-test", + &machine.id, &serde_json::to_value(&machine).unwrap(), ) .unwrap(); @@ -589,41 +757,52 @@ mod tests { fs::write(&known, "berth.test ssh-ed25519 AAAA\n").unwrap(); let studio = rename(&paths, &test, "studio", false).unwrap(); - assert_eq!(studio, Place::own("studio").unwrap()); - assert_eq!(test.load(&paths).unwrap(), None); + assert_eq!(studio, Place::own(&paths, "studio").unwrap()); + assert_eq!(studio.key(), id); + // The name is free, and the id finds the host by its new name. + assert_eq!(Place::find(&paths, OWN, "test").unwrap(), None); + assert_eq!(Place::find(&paths, OWN, &id).unwrap(), Some(studio.clone())); + assert_eq!( + Place::find(&paths, OWN, &format!("host_{id}")).unwrap(), + Some(studio.clone()) + ); // The hostname stays unless asked for. assert_eq!(studio.load(&paths).unwrap().unwrap(), profile); assert_eq!( crate::credentials::load(&paths, &studio).unwrap()["ops"], "made-up-1" ); - assert!(crate::credentials::load(&paths, &test).unwrap().is_empty()); let record = crate::keys::known::Record::load(&store, &studio).unwrap(); assert_eq!(record.host_keys, ["ssh-ed25519 AAAA"]); - assert_eq!(store.read(kind::ACCESS, OWN, "test").unwrap(), None); assert_eq!( - store - .read(kind::SECRET, OWN, "ssh/studio") - .unwrap() - .unwrap(), - b"made-up-2" + secrets.secret("ssh/studio").unwrap().as_deref(), + Some("made-up-2") ); - assert_eq!(store.read(kind::SECRET, OWN, "ssh/test").unwrap(), None); + assert_eq!(secrets.secret("ssh/test").unwrap(), None); assert_eq!( - store.read(kind::SECRET, OWN, "ssh/lab").unwrap().unwrap(), - b"made-up-3" + secrets.secret("ssh/lab").unwrap().as_deref(), + Some("made-up-3") ); let kept = crate::resource::of_place(&paths, &studio).unwrap().unwrap(); assert_eq!( (kept.record.id.as_str(), kept.record.name.as_str()), - ("r-test", "berth-test") + (machine.id.as_str(), "berth-test") ); - assert!(crate::resource::of_place(&paths, &test).unwrap().is_none()); assert!(!known.exists()); // With hostname, the profile's is the new name. - let lab = rename(&paths, &studio, "lab", true).unwrap(); - assert_eq!(lab.load(&paths).unwrap().unwrap()["hostname"], "lab"); + let lab = rename(&paths, &studio, "pier", true).unwrap(); + assert_eq!(lab.load(&paths).unwrap().unwrap()["hostname"], "pier"); + + // Removing it removes what belongs to it, and leaves the other host's. + remove(&paths, &lab).unwrap(); + assert!(crate::credentials::load(&paths, &lab).unwrap().is_empty()); + assert_eq!(secrets.secret("ssh/pier").unwrap(), None); + assert_eq!(store.read(kind::ACCESS, OWN, &id).unwrap(), None); + assert_eq!( + secrets.secret("ssh/lab").unwrap().as_deref(), + Some("made-up-3") + ); } #[test] @@ -656,7 +835,7 @@ mod tests { #[test] fn a_duplicate_is_a_new_host_with_its_machine_planned() { let (_home, paths) = scratch(); - let studio = Place::own("studio").unwrap(); + let studio = Place::own(&paths, "studio").unwrap(); studio .save( &paths, @@ -669,16 +848,16 @@ mod tests { .write_json( kind::ACCESS, OWN, - "studio", + &studio.key(), &json!({"host_keys": ["ssh-ed25519 AAAA"]}), ) .unwrap(); let machine = crate::resource::Record { - id: "r-studio".into(), + id: "01972c4e8f3a7b219d4e5a6b7c8d9e0f".into(), provisioner: "incus".into(), kind: crate::plugins::MACHINE.into(), name: "studio".into(), - host: "studio".into(), + host: studio.key(), external_id: "berth-studio".into(), arguments: [("cpus".to_owned(), "4".to_owned())].into(), state: "running".into(), @@ -688,13 +867,13 @@ mod tests { .write_json( kind::RESOURCE, OWN, - "r-studio", + &machine.id, &serde_json::to_value(&machine).unwrap(), ) .unwrap(); let (lab, planned) = duplicate(&paths, &studio, "lab").unwrap(); - assert_eq!(lab, Place::own("lab").unwrap()); + assert_eq!(lab, Place::own(&paths, "lab").unwrap()); assert_eq!( lab.load(&paths).unwrap().unwrap(), json!({"access": {"user": "ops"}}) @@ -702,7 +881,7 @@ mod tests { // The original keeps all it had. assert_eq!(studio.load(&paths).unwrap().unwrap()["hostname"], "studio"); assert!(crate::credentials::load(&paths, &lab).unwrap().is_empty()); - assert_eq!(store.read(kind::ACCESS, OWN, "lab").unwrap(), None); + assert_eq!(store.read(kind::ACCESS, OWN, &lab.key()).unwrap(), None); let planned = planned.unwrap(); let kept = crate::resource::of_place(&paths, &lab).unwrap().unwrap(); assert_eq!(kept, planned); @@ -716,7 +895,7 @@ mod tests { ); assert!(kept.record.external_id.is_empty()); assert_eq!(kept.record.arguments["cpus"], "4"); - assert_ne!(kept.record.id, "r-studio"); + assert_ne!(kept.record.id, machine.id); assert_eq!( crate::resource::of_place(&paths, &studio) .unwrap() @@ -729,14 +908,14 @@ mod tests { let taken = duplicate(&paths, &studio, "lab").unwrap_err(); assert_eq!(taken.to_string(), "There is a host lab already"); assert!(duplicate(&paths, &studio, "studio").is_err()); - assert!(duplicate(&paths, &Place::own("none").unwrap(), "other").is_err()); + assert!(duplicate(&paths, &Place::own(&paths, "none").unwrap(), "other").is_err()); } #[test] fn a_rename_needs_a_host_a_free_name_and_a_saved_host() { let (home, paths) = scratch(); - let test = Place::own("test").unwrap(); - let studio = Place::own("studio").unwrap(); + let test = Place::own(&paths, "test").unwrap(); + let studio = Place::own(&paths, "studio").unwrap(); let error = |result: Result| result.unwrap_err().to_string(); assert_eq!( error(rename(&paths, &test, "studio", false)), @@ -760,7 +939,7 @@ mod tests { "test has that name already" ); // Only a workstation is given a hostname by berth. - let dock = Place::own("dock").unwrap(); + let dock = Place::own(&paths, "dock").unwrap(); dock.save( &paths, &json!({"type": "ssh", "access": {"address": "192.0.2.30"}}), @@ -788,9 +967,9 @@ mod tests { fn each_place_has_a_lock_file_of_its_own() { let (home, paths) = scratch(); let files = [ - Place::own("studio").unwrap(), - Place::stored("served:http://192.0.2.7:7777", "studio").unwrap(), - Place::stored("served:http://192.0.2.8:7777", "studio").unwrap(), + Place::own(&paths, "studio").unwrap(), + Place::stored(&paths, "served:http://192.0.2.7:7777", "studio").unwrap(), + Place::stored(&paths, "served:http://192.0.2.8:7777", "studio").unwrap(), Place::File(home.path().join("studio.json")), ] .map(|place| place.lock_file(&paths)); @@ -798,6 +977,11 @@ mod tests { assert_eq!(file.parent().unwrap(), paths.data_dir.join("locks")); assert!(!files[at + 1..].contains(file), "{file:?}"); } - assert!(files[0].ends_with("studio.lock")); + // A saved host's is by its id, which a rename leaves as it is. + let own = files[0].file_name().unwrap().to_string_lossy().into_owned(); + assert!( + berth_api::ids::is_id(own.trim_end_matches(".lock")), + "{own}" + ); } } diff --git a/crates/berth-core/src/provision.rs b/crates/berth-core/src/provision.rs index 5b32a22..3d6637a 100644 --- a/crates/berth-core/src/provision.rs +++ b/crates/berth-core/src/provision.rs @@ -162,7 +162,7 @@ fn secret(store: &Store, name: &str) -> Option { /// The secrets of the provisioner registered as `name`, under the names its /// plugin declares, and the required ones without a value. Each entry keeps -/// its own, as `provisioner/NAME/SECRET`. +/// its own, and is given no secret of another. pub(crate) fn secrets( store: &Store, name: &str, @@ -170,9 +170,7 @@ pub(crate) fn secrets( ) -> (Map, Vec) { let (mut given, mut missing) = (Map::new(), vec![]); for wanted in &provisioner.secrets { - match secret(store, &format!("provisioner/{name}/{}", wanted.name)) - .or_else(|| secret(store, &wanted.name)) - { + match secret(store, &crate::provisioner::secret_name(name, &wanted.name)) { Some(value) => { given.insert(wanted.name.clone(), json!(value)); } @@ -187,9 +185,14 @@ pub(crate) fn needs_secrets(name: &str, missing: &[String]) -> Result<()> { match missing { [] => Ok(()), missing => Err(crate::Refused(format!( - "{name} needs the secret{} {}: berth secret set NAME", + "{name} needs the secret{} {}: berth provisioner run {name} secrets {}", if missing.len() == 1 { "" } else { "s" }, - missing.join(", ") + missing.join(", "), + missing + .iter() + .map(|secret| format!("{secret}=VALUE")) + .collect::>() + .join(" ") )) .into()), } @@ -286,7 +289,8 @@ pub fn list(paths: &Paths, store: &Store) -> Vec { } // Its plugin was removed: it stays listed, to add the plugin again or remove it. Err(_) => ProvisionerInfo { - id: name, + id: entry.id.clone(), + name, label: entry.kind.clone(), problem: format!( "No plugin brings {} any more: add its plugin again", @@ -485,23 +489,28 @@ pub fn prepare(paths: &Paths, store: &Store, host: &str, asked: &NewResource) -> let profile = Host::load(paths, host).map_err(|_| format!("No host {host}"))?; let lock = lock(paths, host)?; let kept = resource::of_place(paths, &profile.place)?; + let reference = crate::provisioner::for_use(paths, &asked.provisioner)?; if let Some(kept) = kept .as_ref() .filter(|kept| !kept.record.external_id.is_empty()) { return Err(format!( "{host} has a machine already, {} of {}. See it with: berth resource show {}; delete it with: berth resource remove {}", - kept.record.external_id, kept.record.provisioner, kept.record.id, kept.record.id + kept.record.external_id, + crate::provisioner::label(paths, &kept.record.provisioner), + kept.record.id, + kept.record.id ) .into()); } if let Some(kept) = kept .as_ref() - .filter(|kept| kept.record.provisioner != asked.provisioner) + .filter(|kept| kept.record.provisioner != reference) { return Err(format!( "A machine for {host} was asked of {}, which did not say it made one. Forget it with: berth resource remove {}", - kept.record.provisioner, kept.record.id + crate::provisioner::label(paths, &kept.record.provisioner), + kept.record.id ) .into()); } @@ -512,20 +521,16 @@ pub fn prepare(paths: &Paths, store: &Store, host: &str, asked: &NewResource) -> return Err("A machine is given no secrets of its own; its profile names them".into()); } let plugins = found(paths); - let (plugin, provisioner) = resolve(paths, &plugins, &asked.provisioner)?; + let (plugin, provisioner) = resolve(paths, &plugins, &reference)?; runnable(provisioner)?; + let name = crate::provisioner::label(paths, &reference); if provisioner.machine().is_none() { - return Err(format!("{} makes no machines for hosts", asked.provisioner).into()); - } - let settings = crate::provisioner::settings(paths, &asked.provisioner)?; - let arguments = values_of( - &asked.provisioner, - &provisioner.arguments, - &asked.arguments, - &settings, - )?; - let (secrets, missing) = secrets(store, &asked.provisioner, provisioner); - needs_secrets(&asked.provisioner, &missing)?; + return Err(format!("{name} makes no machines for hosts").into()); + } + let settings = crate::provisioner::settings(paths, &reference)?; + let arguments = values_of(&name, &provisioner.arguments, &asked.arguments, &settings)?; + let (secrets, missing) = secrets(store, &reference, provisioner); + needs_secrets(&name, &missing)?; if provisioner.setup == Setup::Apply && profile.access().user.is_empty() { return Err(format!( "{} creates the machine with an account berth reaches over SSH: the profile of {host} needs access.user", @@ -552,7 +557,7 @@ pub fn prepare(paths: &Paths, store: &Store, host: &str, asked: &NewResource) -> scope, record: resource::Record { id: resource::new_id()?, - provisioner: asked.provisioner.clone(), + provisioner: reference, kind: crate::plugins::MACHINE.into(), name: profile.place.title(), host: side, @@ -568,7 +573,7 @@ pub fn prepare(paths: &Paths, store: &Store, host: &str, asked: &NewResource) -> kept.save(paths)?; Ok(Prepared { host: host.to_owned(), - name: asked.provisioner.clone(), + name, dir: plugin.dir.clone(), provisioner: provisioner.clone(), hidden: hidden(&secrets), @@ -592,7 +597,7 @@ pub fn plan( scope, record: resource::Record { id: resource::new_id()?, - provisioner: provisioner.to_owned(), + provisioner: crate::provisioner::reference(paths, provisioner)?, kind: crate::plugins::MACHINE.into(), name: place.title(), host: side, @@ -1372,7 +1377,7 @@ print(out) } fn profile(&self, name: &str, profile: Value) { - Place::own(name) + Place::own(&self.paths, name) .unwrap() .save(&self.paths, &profile) .unwrap(); @@ -1382,6 +1387,11 @@ print(out) Store::new(&self.paths).unwrap() } + /// The id of the host studio. + fn studio(&self) -> String { + Place::own(&self.paths, "studio").unwrap().key() + } + fn file(&self, name: &str, text: &str) { fs::write(self.plugin.join(name), text).unwrap(); } @@ -1485,10 +1495,13 @@ print(out) .unwrap(); } + /// The provisioner fake-vm, which a machine of the type fake/vm is made + /// with, with its secret: a provisioner is given its own secrets alone. fn with_secret(world: &World) { + register(world, "fake-vm", "fake/vm"); world .store() - .set_secret("api_token", Some("tok-s3cret-value")) + .set_secret("provisioner/fake-vm/api_token", Some("tok-s3cret-value")) .unwrap(); } @@ -1505,7 +1518,7 @@ print(out) assert!(kept.external_id.is_empty() && kept.token.len() == 32); assert_eq!( (kept.kind.as_str(), kept.host.as_str()), - ("machine", "studio") + ("machine", world.studio().as_str()) ); // The record is a resource in the store, and no file is beside anything. let store = store::shared(&world.paths).unwrap(); @@ -1589,17 +1602,26 @@ print(out) said.contains("version 1") && said.contains("\"protocol\": 2"), "{said}" ); - assert!(refused("fake/vm", &[("server", "x")]).contains("api_token")); // Only registered ones are listed, by their names. assert!(list(&world.paths, &world.store()).is_empty()); register(&world, "vm", "fake/vm"); + // The type names the one provisioner of it, which is given its own secrets alone. + world + .store() + .set_secret("api_token", Some("shared")) + .unwrap(); + assert!(refused("fake/vm", &[("server", "x")]).contains("vm needs the secret api_token")); let listed = list(&world.paths, &world.store()); assert_eq!( - (listed[0].id.as_str(), listed[0].kind.as_str()), + (listed[0].name.as_str(), listed[0].kind.as_str()), ("vm", "fake/vm") ); + assert!(berth_api::ids::is_id(&listed[0].id)); assert_eq!(listed[0].missing_secrets, ["api_token"]); - with_secret(&world); + world + .store() + .set_secret("provisioner/vm/api_token", Some("tok-made-up")) + .unwrap(); assert!( list(&world.paths, &world.store())[0] .missing_secrets @@ -1687,9 +1709,10 @@ print(out) #[test] fn a_host_that_joins_the_tailnet_gets_no_machine_without_berths_api_key() { let world = World::new(); + register(&world, "fake-vm", "fake/vm"); world .store() - .set_secret("api_token", Some("tok-made-up")) + .set_secret("provisioner/fake-vm/api_token", Some("tok-made-up")) .unwrap(); world.file("answer.create", r#"{"id": "vm-1"}"#); world.profile( @@ -1704,7 +1727,7 @@ print(out) assert!(said.contains("berth secret set tailnet_api_key"), "{said}"); // Nothing was asked of the provisioner, and no record was kept. assert!(world.requests().iter().all(|(verb, _)| verb != "create")); - let place = crate::profiles::Place::own("studio").unwrap(); + let place = crate::profiles::Place::own(&world.paths, "studio").unwrap(); assert!( crate::resource::of_place(&world.paths, &place) .unwrap() @@ -1715,13 +1738,17 @@ print(out) #[test] fn secrets_go_on_standard_input_and_never_into_the_output() { let world = World::new(); + register(&world, "fake-vm", "fake/vm"); world .store() - .set_secret("api_token", Some("tok-s3cret-value\nsecond-line-of-it")) + .set_secret( + "provisioner/fake-vm/api_token", + Some("tok-s3cret-value\nsecond-line-of-it"), + ) .unwrap(); world .store() - .set_secret("extra", Some("optional-one")) + .set_secret("provisioner/fake-vm/extra", Some("optional-one")) .unwrap(); world.file("leak", ""); world.file("say.create", "using tok-s3cret-value\n"); @@ -1891,7 +1918,7 @@ print(out) .unwrap_err() .to_string(); assert!( - said.contains("gone") && said.contains("berth resource remove r-"), + said.contains("gone") && said.contains("berth resource remove "), "{said}" ); assert_eq!(world.record().unwrap().external_id, "seeded-1"); @@ -2047,7 +2074,7 @@ print(out) let said = done.unwrap_err().to_string(); assert!(started.elapsed() < Duration::from_secs(20)); assert!(said.contains("studio's first boot failed"), "{said}"); - assert!(said.contains("berth resource remove r-"), "{said}"); + assert!(said.contains("berth resource remove "), "{said}"); let end = out .split_once("! studio: its first boot failed. The end of its log:\n") .unwrap() @@ -2172,7 +2199,7 @@ print(out) }; for place in [ Place::File(home.path().join("studio.json")), - Place::own("studio").unwrap(), + Place::own(&paths, "studio").unwrap(), ] { let taken = |profile: Value| { place.save(&paths, &profile).unwrap(); @@ -2229,7 +2256,7 @@ print(out) .unwrap() .unwrap(); assert_eq!(found.record.token, "t"); - assert_eq!(found.title(), "studio's machine"); + assert_eq!(found.title(&world.paths), "studio's machine"); } #[test] @@ -2278,7 +2305,7 @@ print(out) r#"{"id": "vm-1", "address": "192.0.2.20"}"#, ); register(&world, "lab", "fake/vm"); - // A secret of its own wins over the one all share. + // It is given its own secret. world .store() .set_secret("provisioner/lab/api_token", Some("tok-lab")) @@ -2293,15 +2320,21 @@ print(out) let (_, create) = world.requests().pop().unwrap(); assert_eq!(create["arguments"]["server"], "https://192.0.2.10:8443"); assert_eq!(create["secrets"]["api_token"], "tok-lab"); - assert_eq!(world.record().unwrap().provisioner, "lab"); + assert_eq!( + world.record().unwrap().provisioner, + crate::provisioner::id_of(&world.paths, "lab") + .unwrap() + .unwrap() + ); // The record keeps where it was made, whatever the setting says later. assert_eq!( world.record().unwrap().arguments["server"], "https://192.0.2.10:8443" ); let listed = list(&world.paths, &world.store()); - assert_eq!(listed[0].settings["server"], "https://192.0.2.10:8443"); - assert!(listed[0].problem.is_empty(), "{}", listed[0].problem); + let lab = listed.iter().position(|info| info.name == "lab").unwrap(); + assert_eq!(listed[lab].settings["server"], "https://192.0.2.10:8443"); + assert!(listed[lab].problem.is_empty(), "{}", listed[lab].problem); // An empty value is forgotten. crate::provisioner::save_settings( &world.paths, @@ -2310,13 +2343,13 @@ print(out) ) .unwrap(); let listed = list(&world.paths, &world.store()); - assert!(listed[0].settings.is_empty()); + assert!(listed[lab].settings.is_empty()); assert!( - listed[0] + listed[lab] .problem .contains("berth provisioner set lab server=VALUE"), "{}", - listed[0].problem + listed[lab].problem ); } diff --git a/crates/berth-core/src/provisioner.rs b/crates/berth-core/src/provisioner.rs index f62e77e..5a1989e 100644 --- a/crates/berth-core/src/provisioner.rs +++ b/crates/berth-core/src/provisioner.rs @@ -48,12 +48,18 @@ const FORGET: &str = "-"; /// plugin's provisioner it is, and the values of its own arguments. #[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)] pub struct Entry { + /// Its id, which the store keeps it by. + #[serde(skip)] + pub id: String, + /// Its name, which the store keeps beside it. + #[serde(skip)] + pub name: String, #[serde(rename = "type")] pub kind: String, #[serde(default)] pub settings: BTreeMap, - /// The node it lives on, when a feature of that host offered it: its - /// name among this berth's hosts. + /// The node it lives on, when a feature of that host offered it: the + /// id of that host among this berth's. #[serde(default, skip_serializing_if = "String::is_empty")] pub node: String, /// The node's feature that offered it, as `PLUGIN/FEATURE`. @@ -61,41 +67,148 @@ pub struct Entry { pub feature: String, } -/// The entry registered under a provisioner's name, if there is one. -pub fn entry(paths: &Paths, name: &str) -> Result> { - Ok(store::shared(paths)? - .read_json(kind::PROVISIONER_ENTRY, OWN, name)? - .and_then(|entry| serde_json::from_value(entry).ok())) +/// The registered provisioner a name or id gives in a store that is open. +/// A type, such as `incus/vm`, gives the one provisioner registered of it, +/// and nothing when there are none or several. +pub fn find_in(store: &store::Store, given: &str) -> Result> { + if let Some(found) = store.find(kind::PROVISIONER_ENTRY, OWN, given)? { + return Ok(Some(found)); + } + if !given.contains('/') { + return Ok(None); + } + let mut of_type = vec![]; + for (found, value) in store.labelled_entries(kind::PROVISIONER_ENTRY, OWN)? { + if serde_json::from_slice::(&value).is_ok_and(|entry| entry.kind == given) { + of_type.push(found); + } + } + Ok(match of_type.len() { + 1 => of_type.pop(), + _ => None, + }) +} + +/// The provisioner registered under a name or with an id, if there is one, +/// or the one of a type as [`find_in`] says. +pub fn entry(paths: &Paths, given: &str) -> Result> { + let store = store::shared(paths)?; + let Some(found) = find_in(&store, given)? else { + return Ok(None); + }; + Ok(store + .read_json(kind::PROVISIONER_ENTRY, OWN, &found.id)? + .and_then(|entry| serde_json::from_value::(entry).ok()) + .map(|entry| Entry { + id: found.id, + name: found.name, + ..entry + })) } -/// The node a registered provisioner lives on, or nothing. -pub fn node_of(paths: &Paths, name: &str) -> String { - entry(paths, name) +/// The id of the provisioner registered under a name or with an id. +pub fn id_of(paths: &Paths, given: &str) -> Result> { + let shared = store::shared(paths)?; + Ok(find_in(&shared, given)?.map(|found| found.id)) +} + +/// What a provisioner is called for people: the name of the registered one +/// with the id, or what was given, such as a type that is not registered. +pub fn label(paths: &Paths, given: &str) -> String { + store::shared(paths) .ok() - .flatten() - .map(|entry| entry.node) - .unwrap_or_default() + .and_then(|store| find_in(&store, given).ok().flatten()) + .map_or_else(|| given.to_owned(), |found| found.name) } -/// Register a provisioner under `name`, of the type `entry.kind`. -pub fn save_entry(paths: &Paths, name: &str, entry: &Entry) -> Result<()> { - if !plugins::valid_name(name) { +/// What a record keeps of the provisioner a name or id gives: the id of the +/// registered one, or what was given, such as a type no provisioner is +/// registered for. +pub fn reference(paths: &Paths, given: &str) -> Result { + Ok(id_of(paths, given)?.unwrap_or_else(|| given.to_owned())) +} + +/// What a record keeps of the provisioner a name, id or type gives, to +/// make something with: the id of a registered one, or of the one +/// registered of a type; a type no provisioner is registered of stays the +/// type, which is given no secrets as it has none of its own. +pub fn for_use(paths: &Paths, given: &str) -> Result { + if let Some(id) = id_of(paths, given)? { + return Ok(id); + } + if !given.contains('/') { + return Err(format!("No provisioner {given}. See: berth provisioner list").into()); + } + let several: Vec = entries(paths)? + .into_iter() + .filter(|(_, entry)| entry.kind == given) + .map(|(name, _)| name) + .collect(); + if !several.is_empty() { return Err(format!( - "{name} is not a name for a provisioner: lowercase letters, digits, - and _, starting with a letter" + "Several provisioners are of the type {given}: name one ({})", + several.join(", ") ) .into()); } - store::shared(paths)?.write_json(kind::PROVISIONER_ENTRY, OWN, name, &json!(entry))?; - Ok(()) + Ok(given.to_owned()) +} + +/// The name of the node a registered provisioner lives on, or nothing: what +/// its plugin is told, and people. +pub fn node_of(paths: &Paths, given: &str) -> String { + entry(paths, given) + .ok() + .flatten() + .filter(|entry| !entry.node.is_empty()) + .map(|entry| crate::node::node_name(paths, &entry.node)) + .unwrap_or_default() +} + +/// Why a provisioner cannot be called `name`, if it cannot. +fn name_problem(name: &str) -> Option { + (!plugins::valid_name(name) || berth_api::ids::looks_like_id(name)).then(|| { + format!( + "{name} is not a name for a provisioner: lowercase letters, digits, - and _, starting with a letter" + ) + }) +} + +/// Save the provisioner registered under a name or with an id, or register +/// a new one of the type `entry.kind` under the name. Returns its id. +pub fn save_entry(paths: &Paths, given: &str, entry: &Entry) -> Result { + let store = store::shared(paths)?; + let (id, name) = match find_in(&store, given)? { + Some(found) => (found.id, found.name), + None => { + if let Some(problem) = name_problem(given) { + return Err(problem.into()); + } + (store::new_id(), given.to_owned()) + } + }; + store.write_labelled( + kind::PROVISIONER_ENTRY, + OWN, + &id, + &name, + json!(entry).to_string().as_bytes(), + )?; + Ok(id) } /// The registered provisioners, by name. pub fn entries(paths: &Paths) -> Result> { let shared = store::shared(paths)?; let mut found = vec![]; - for name in shared.names(kind::PROVISIONER_ENTRY, OWN)? { - if let Some(entry) = entry(paths, &name)? { - found.push((name, entry)); + for (thing, value) in shared.labelled_entries(kind::PROVISIONER_ENTRY, OWN)? { + if let Ok(entry) = serde_json::from_slice::(&value) { + let entry = Entry { + id: thing.id, + name: thing.name.clone(), + ..entry + }; + found.push((thing.name, entry)); } } Ok(found) @@ -107,25 +220,30 @@ pub fn default_name(kind: &str) -> String { kind.replace('/', "-") } -/// The berth secret a registered provisioner keeps as its own `secret`. One -/// without it is given the secret of that name that all share. -pub fn secret_name(name: &str, secret: &str) -> String { - format!("provisioner/{name}/{secret}") +/// The address of the secret `secret` the registered provisioner of a name +/// or id keeps as its own. A provisioner is given its own secrets alone. +pub fn secret_name(provisioner: &str, secret: &str) -> String { + format!("provisioner/{provisioner}/{secret}") } -/// Whether `name` is the name of a secret a provisioner keeps of its own. +/// Whether `name` is the address of a secret a provisioner keeps of its own. pub fn is_secret_name(name: &str) -> bool { name.strip_prefix("provisioner/") .and_then(|rest| rest.split_once('/')) .is_some_and(|(provisioner, secret)| { - plugins::valid_name(provisioner) && plugins::valid_name(secret) + (plugins::valid_name(provisioner) || berth_api::ids::is_id(provisioner)) + && plugins::valid_name(secret) }) } /// Register a provisioner under `name`, or change the settings of the one /// there is. A registered one keeps its type; settings left out stay. -pub fn register(paths: &Paths, name: &str, saved: &berth_api::SavedProvisioner) -> Result { - let existing = entry(paths, name)?; +pub fn register(paths: &Paths, given: &str, saved: &berth_api::SavedProvisioner) -> Result { + let existing = entry(paths, given)?; + let name = existing + .as_ref() + .map_or_else(|| given.to_owned(), |existing| existing.name.clone()); + let name = name.as_str(); let wanted = saved.kind.trim(); let kind = match (&existing, wanted) { (Some(existing), "") => existing.kind.clone(), @@ -187,24 +305,25 @@ fn kept_values( .collect()) } -/// The name of this berth's provisioner for a machine another berth made +/// The id of this berth's provisioner for a machine another berth made /// with its provisioner `name` of the type `kind`: one registered here of -/// that type, or one registered for it now. Without a type, the name stays. -pub fn adopted_name(paths: &Paths, name: &str, kind: &str) -> Result { +/// that type, or one registered for it now. Without a type, a provisioner +/// registered under the name, or the name as it is. +pub fn adopted(paths: &Paths, name: &str, kind: &str) -> Result { if kind.is_empty() { - return Ok(name.to_owned()); + return Ok(id_of(paths, name)?.unwrap_or_else(|| name.to_owned())); } let registered = entries(paths)?; - if let Some((found, _)) = registered + if let Some((_, found)) = registered .iter() .find(|(known, entry)| known == name && entry.kind == kind) .or_else(|| registered.iter().find(|(_, entry)| entry.kind == kind)) { - return Ok(found.clone()); + return Ok(found.id.clone()); } - let stem = match plugins::valid_name(name) { - true => name.to_owned(), - false => default_name(kind), + let stem = match name_problem(name) { + None => name.to_owned(), + Some(_) => default_name(kind), }; let mut free = stem.clone(); let mut number = 2; @@ -219,144 +338,74 @@ pub fn adopted_name(paths: &Paths, name: &str, kind: &str) -> Result { kind: kind.to_owned(), ..Entry::default() }, - )?; - Ok(free) + ) } -/// Forget a registered provisioner and the secrets it keeps. One that still -/// holds resources is kept: they would have no provisioner. -pub fn remove_entry(paths: &Paths, store: &Store, name: &str) -> Result<()> { - if entry(paths, name)?.is_none() { - return Err(format!("No provisioner {name}. See: berth provisioner list").into()); - } +/// Forget a registered provisioner and everything that belongs to it, such +/// as the secrets it keeps, in one change. One that still holds resources is +/// kept: they would have no provisioner. +pub fn remove_entry(paths: &Paths, _store: &Store, given: &str) -> Result<()> { + let Some(entry) = entry(paths, given)? else { + return Err(format!("No provisioner {given}. See: berth provisioner list").into()); + }; let held: Vec = crate::resource::list(paths)? .into_iter() - .filter(|kept| kept.record.provisioner == name) + .filter(|kept| kept.record.provisioner == entry.id) .map(|kept| kept.record.id) .collect(); if !held.is_empty() { return Err(format!( - "{name} still holds {}. Remove {} first: berth resource remove ID", + "{} still holds {}. Remove {} first: berth resource remove ID", + entry.name, held.join(", "), if held.len() == 1 { "it" } else { "them" } ) .into()); } - let prefix = secret_name(name, ""); - for secret in store.all_secret_names()? { - if secret.starts_with(&prefix) { - store.set_secret(&secret, None)?; - } - } - store::shared(paths)?.forget(kind::PROVISIONER_ENTRY, OWN, name)?; + let mut batch = store::Batch::default(); + batch + .remove(kind::PROVISIONER_ENTRY, OWN, &entry.id) + .forget_owned(OWN, &entry.id); + store::shared(paths)?.apply(&batch)?; Ok(()) } /// Why the registered provisioner `old` cannot be called `new`, if it cannot. pub fn rename_problem(paths: &Paths, old: &str, new: &str) -> Result> { - if entry(paths, old)?.is_none() { + let Some(entry) = entry(paths, old)? else { return Err( format!("No provisioner {old} is registered. See: berth provisioner list").into(), ); - } - Ok(if !plugins::valid_name(new) { - Some(format!( - "{new} is not a name for a provisioner: lowercase letters, digits, - and _, starting with a letter" - )) - } else if new == old { - Some(format!("{old} has that name already")) - } else if entry(paths, new)?.is_some() { + }; + Ok(if let Some(problem) = name_problem(new) { + Some(problem) + } else if new == entry.name { + Some(format!("{} has that name already", entry.name)) + } else if store::shared(paths)? + .id_of(kind::PROVISIONER_ENTRY, OWN, new)? + .is_some() + { Some(format!("There is a provisioner {new} already")) } else { None }) } -/// Give the registered provisioner `old` the name `new`, in one change: its -/// entry with its settings and the node it is of, the secrets it keeps of its -/// own, the records of what it made and of the machines planned with it, and -/// its jobs. Each resource keeps its id, and the machines and what runs on -/// them are not touched. The caller holds the lock of `old`, as its job does; -/// the lock of `new` and those of what it made are taken here, so that no -/// work on one of them saves its record with the old name meanwhile. -pub fn rename(paths: &Paths, store: &Store, old: &str, new: &str) -> Result<()> { +/// Give the registered provisioner `old` the name `new`. Everything that +/// belongs to it or refers to it, its settings, node, secrets, resources and +/// jobs, does so by its id, and stays with it. +pub fn rename(paths: &Paths, _store: &Store, old: &str, new: &str) -> Result<()> { if let Some(problem) = rename_problem(paths, old, new)? { return Err(problem.into()); } - let _new = crate::runner::lock( - paths, - &crate::runner::Subject::Provisioner(new.to_owned()), - "rename the provisioner", - )?; - let _held = crate::resource::list(paths)? - .iter() - .filter(|kept| kept.record.provisioner == old) - .map(|kept| kept.lock(paths)) - .collect::>>()?; - let shared = store::shared(paths)?; - let mut writes: Vec<(&str, String, String, Vec)> = vec![]; - let mut removals: Vec<(&str, String, String)> = vec![]; - let mut moved = |kind: &'static str, scope: &str, from: String, to: String, value: Vec| { - writes.push((kind, scope.to_owned(), to, value)); - removals.push((kind, scope.to_owned(), from)); - }; - for kind in [kind::PROVISIONER_ENTRY, kind::PROVISIONER] { - if let Some(value) = shared.read(kind, OWN, old)? { - moved(kind, OWN, old.to_owned(), new.to_owned(), value); + let id = id_of(paths, old)?.unwrap_or_default(); + match store::shared(paths)?.relabel(kind::PROVISIONER_ENTRY, OWN, &id, new) { + Ok(_) => Ok(()), + Err(store::Error::Taken { .. }) => { + Err(format!("There is a provisioner {new} already").into()) } + Err(error) => Err(error.into()), } - let (from, to) = (secret_name(old, ""), secret_name(new, "")); - let mut stale = vec![]; - for secret in store.all_secret_names()? { - if let Some(own) = secret.strip_prefix(&from) { - if let Some(value) = shared.read(kind::SECRET, OWN, &secret)? { - moved( - kind::SECRET, - OWN, - secret.clone(), - format!("{to}{own}"), - value, - ); - } - } else if secret.starts_with(&to) { - // Left by a provisioner of that name before; the renamed one has its own. - stale.push(secret); - } - } - for scope in [OWN, crate::profiles::FILE_SCOPE] { - for (id, value) in shared.entries(kind::RESOURCE, scope)? { - let mut record: crate::resource::Record = - serde_json::from_slice(&value).map_err(|error| { - format!("The record of the resource {id} in berth's store: {error}") - })?; - if record.provisioner == old { - record.provisioner = new.to_owned(); - writes.push(( - kind::RESOURCE, - scope.to_owned(), - id, - serde_json::to_vec(&record)?, - )); - } - } - } - let written: Vec<(&str, &str, &str, Vec)> = writes - .iter() - .map(|(kind, scope, name, value)| (*kind, scope.as_str(), name.as_str(), value.clone())) - .collect(); - let mut removed: Vec<(&str, &str, &str)> = removals - .iter() - .map(|(kind, scope, name)| (*kind, scope.as_str(), name.as_str())) - .collect(); - removed.extend( - stale - .iter() - .filter(|secret| !written.iter().any(|(_, _, name, _)| name == secret)) - .map(|secret| (kind::SECRET, OWN, secret.as_str())), - ); - let subject = |name: &str| crate::runner::Subject::Provisioner(name.to_owned()).key(); - shared.rename_subject(&subject(old), &subject(new), &written, &removed)?; - Ok(()) } /// The values of a provisioner's own arguments that berth keeps, by name. @@ -381,7 +430,8 @@ pub fn save_settings(paths: &Paths, name: &str, values: &BTreeMap ProvisionerInfo { let missing = secrets(store, name, provisioner).1; let mut info = provisioner.info(plugin, missing.clone()); - info.id = name.to_owned(); + if let Ok(Some(entry)) = entry(paths, name) { + info.id = entry.id; + info.name = entry.name; + info.node = crate::node::node_name(paths, &entry.node); + info.node_id = entry.node; + } else { + info.id = name.to_owned(); + info.name = name.to_owned(); + } info.settings = settings(paths, name).unwrap_or_default(); - info.node = node_of(paths, name); if info.problem.is_empty() { - info.problem = problem(&info.id, provisioner, &missing, &info.settings); + info.problem = problem(&info.name, provisioner, &missing, &info.settings); } info } @@ -583,7 +640,7 @@ pub fn state( Ok(own) => own, Err(error) => { state.message = error.to_string(); - state.resources = crate::resource::merge(&records, name, vec![]); + state.resources = crate::resource::merge(paths, &records, name, vec![]); return Ok(state); } }; @@ -636,7 +693,7 @@ pub fn state( listed = crate::resource::listed_of(&answer); } } - state.resources = crate::resource::merge(&records, name, listed); + state.resources = crate::resource::merge(paths, &records, name, listed); Ok(state) } @@ -802,15 +859,13 @@ fn new_action( } } -/// berth's own action that keeps the secrets of one registered provisioner, -/// in place of those all of its type share. +/// berth's own action that keeps the secrets of one registered provisioner. fn secrets_action(name: &str, provisioner: &Provisioner) -> Action { Action { name: SECRETS.into(), label: "Secrets".into(), help: format!( - "The secrets {name} uses of its own, such as the certificate of its server. Each is kept in berth's store as {}, and one it does not have is taken from the secret of that name that all share. An empty value keeps what it has, and {FORGET} forgets it.", - secret_name(name, "NAME") + "The secrets {name} uses, such as the certificate of its server. Each belongs to {name}, goes with it when it is renamed and is removed with it; {name} is given no other secret. An empty value keeps what it has, and {FORGET} forgets it." ), group: "Set-Up".into(), arguments: provisioner @@ -857,7 +912,7 @@ fn rename_action(name: &str) -> Action { name: RENAME.into(), label: "Rename".into(), help: format!( - "Give {name} another name in berth. Its settings, the secrets it keeps of its own, the resources and machines it made or plans, and its jobs go with it. What it made is not touched, and a node's provisioner stays the node's." + "Give {name} another name in berth. Its id stays, and with it its settings, its secrets, the resources and machines it made or plans, and its jobs. What it made is not touched, and a node's provisioner stays the node's." ), group: "Set-Up".into(), arguments: vec![Argument { @@ -1206,7 +1261,7 @@ impl Acting { "" => continue, FORGET => { store.set_secret(&kept, None)?; - "forgotten: the one all share is used" + "forgotten" } value => { store.set_secret(&kept, Some(value))?; @@ -1250,7 +1305,7 @@ impl Acting { } Doing::Create(creating, store) => { let id = creating.kept().record.id.clone(); - let title = creating.kept().title(); + let title = creating.kept().title(paths); creating.run(paths, &store, options, out)?; Ok(Answered { message: format!("{title} is made. Its page: berth resource show {id}"), diff --git a/crates/berth-core/src/registry.rs b/crates/berth-core/src/registry.rs index d92bfb0..1c8b278 100644 --- a/crates/berth-core/src/registry.rs +++ b/crates/berth-core/src/registry.rs @@ -32,6 +32,80 @@ pub fn type_label(kind: &str) -> &'static str { } pub const FORMATS: &[&str] = &["npm", "raw", "docker"]; +/// Where a secret is kept. +enum Located { + /// A global secret: its id when it is there, and its name. + Global(Option, String), + /// A secret that belongs to something, by the name of its row. + Owned(String), + /// One that would belong to something there is not, and why. + NoOwner(String), +} + +/// The row a secret is kept in, as given by its name or id, or by the +/// address of a secret that belongs to something: `registry/REGISTRY` +/// for a registry's login, `ssh/HOST` and `sudo/HOST` for the passwords +/// berth logs in to a host with, and `provisioner/PROVISIONER/SECRET` +/// for a provisioner's own. +fn locate(store: &store::Store, scope: &str, given: &str) -> Result { + let owned = |kind: &str, owner: &str, label: &str| -> Result { + Ok(match store.find(kind, scope, owner)? { + Some(found) => Located::Owned(format!("{}/{label}", found.id)), + None => Located::NoOwner(format!( + "No {} {owner}, which the secret {given} would belong to", + store::noun(kind) + )), + }) + }; + if let Some(registry) = given.strip_prefix("registry/") { + return owned(kind::REGISTRY, registry, "login"); + } + for (prefix, label) in [("ssh/", "ssh"), ("sudo/", "sudo")] { + if let Some(host) = given.strip_prefix(prefix) { + // A profile in a file keeps its passwords by its path. + if host.contains('/') || host.ends_with(".json") { + return Ok(Located::Owned(given.to_owned())); + } + return owned(kind::PROFILE, host, label); + } + } + // A provisioner is named by its name, its id or its type. + if let Some((provisioner, secret)) = given + .strip_prefix("provisioner/") + .and_then(|rest| rest.rsplit_once('/')) + { + return Ok(match crate::provisioner::find_in(store, provisioner)? { + Some(found) => Located::Owned(format!("{}/{secret}", found.id)), + None => Located::NoOwner(format!( + "No provisioner {provisioner}, which the secret {given} would belong to" + )), + }); + } + Ok(match store.find(kind::SECRET, scope, given)? { + Some(found) => Located::Global(Some(found.id), found.name), + None => Located::Global(None, given.to_owned()), + }) +} + +/// The value of a secret in a store that is open, by its name or id, or by +/// the address of one that belongs to something, such as `registry/NAME`. +pub fn read_secret_in(store: &store::Store, scope: &str, given: &str) -> Result> { + let row = match locate(store, scope, given)? { + Located::Global(Some(id), _) => id, + Located::Owned(row) => row, + Located::Global(None, _) | Located::NoOwner(_) => return Ok(None), + }; + Ok(store + .read(kind::SECRET, scope, &row)? + .map(|value| String::from_utf8_lossy(&value).into_owned())) +} + +/// Whether a name can be a global secret's: no address of one that +/// belongs to something, and not in the shape of an id. +pub fn valid_secret_name(name: &str) -> bool { + !name.trim().is_empty() && !name.contains('/') && !berth_api::ids::looks_like_id(name) +} + /// The registries and secrets of this installation. #[derive(Clone)] pub struct Store { @@ -116,101 +190,186 @@ impl Store { pub fn registries(&self) -> Result> { self.store - .entries(kind::REGISTRY, &self.scope)? + .labelled_entries(kind::REGISTRY, &self.scope)? .into_iter() - .map(|(name, value)| { + .map(|(found, value)| { let registry = serde_json::from_slice(&value) - .map_err(|error| format!("Registry {name} in the store: {error}"))?; - Ok((name, registry)) + .map_err(|error| format!("Registry {} in the store: {error}", found.name))?; + Ok((found.name, registry)) }) .collect() } - /// The rows of the registries, for one change of the store. - fn registry_rows(registries: &Map) -> Vec<(&str, &str, Vec)> { - registries - .iter() - .map(|(name, registry)| { - ( - kind::REGISTRY, - name.as_str(), - registry.to_string().into_bytes(), - ) - }) - .collect() + /// The registries' names and ids, by name. + pub fn registry_ids(&self) -> Result> { + Ok(self.store.labels(kind::REGISTRY, &self.scope)?) + } + + /// The registry a name or id gives. + pub fn find_registry(&self, given: &str) -> Result> { + Ok(self.store.find(kind::REGISTRY, &self.scope, given)?) } - /// Make these the registries, in one change of the store. + /// Make these the registries, in one change of the store: one of a name + /// kept keeps its id and login, a new one gets an id, and one left out + /// goes with its login. Profiles that name a registry refer to its id. pub fn save_registries(&self, registries: Map) -> Result<()> { - let rows = Self::registry_rows(®istries); - Ok(self.store.replace(&self.scope, &[kind::REGISTRY], &rows)?) + let before = self.registry_ids()?; + let mut batch = store::Batch::default(); + for (name, registry) in ®istries { + let mut registry = registry.clone(); + if let Some(fields) = registry.as_object_mut() { + fields.remove("id"); + } + let id = match before.iter().find(|found| &found.name == name) { + Some(found) => found.id.clone(), + None => { + let id = store::new_id(); + crate::profiles::swap_references( + &self.store, + &mut batch, + &self.scope, + kind::REGISTRY, + name, + &id, + )?; + id + } + }; + batch.write_labelled( + kind::REGISTRY, + &self.scope, + &id, + name, + registry.to_string().into_bytes(), + ); + } + for gone in before + .iter() + .filter(|found| !registries.contains_key(&found.name)) + { + batch + .remove(kind::REGISTRY, &self.scope, &gone.id) + .forget_owned(&self.scope, &gone.id); + crate::profiles::swap_references( + &self.store, + &mut batch, + &self.scope, + kind::REGISTRY, + &gone.id, + &gone.name, + )?; + } + Ok(self.store.apply(&batch)?) } - /// Give a registry another name, with its stored login, in one change of - /// the store. Returns whether there was a login to go with the name. + /// Give a registry another name. Its login and the hosts that use it + /// stay with it, as both refer to its id. Returns whether it has a login. pub fn rename_registry(&self, old: &str, new: &str) -> Result { - let mut registries = self.registries()?; - let registry = registries - .remove(old) + let found = self + .find_registry(old)? .ok_or_else(|| format!("No registry {old}"))?; - if registries.contains_key(new) { - return Err(format!("There is a registry {new} already").into()); - } - registries.insert(new.to_owned(), registry.clone()); - let (old_login, new_login) = (format!("registry/{old}"), format!("registry/{new}")); - let login = self.store.read(kind::SECRET, &self.scope, &old_login)?; - let mut writes = vec![( - kind::REGISTRY, - self.scope.as_str(), - new, - registry.to_string().into_bytes(), - )]; - if let Some(login) = &login { - writes.push((kind::SECRET, &self.scope, &new_login, login.clone())); + if found.name == new { + return Err(format!("{new} has that name already").into()); } - self.store.change( - &writes, - &[ - (kind::REGISTRY, &self.scope, old), - (kind::SECRET, &self.scope, &old_login), - ], - )?; - Ok(login.is_some()) + match self + .store + .relabel(kind::REGISTRY, &self.scope, &found.id, new) + { + Err(store::Error::Taken { .. }) => { + return Err(format!("There is a registry {new} already").into()); + } + result => result?, + }; + self.has_secret(&format!("registry/{}", found.id)) } fn registry(&self, name: &str) -> Result { + let found = self + .find_registry(name)? + .ok_or_else(|| format!("No registry {name}. See: berth registry"))?; self.store - .read_json(kind::REGISTRY, &self.scope, name)? + .read_json(kind::REGISTRY, &self.scope, &found.id)? .ok_or_else(|| format!("No registry {name}. See: berth registry").into()) } - /// The names of every secret, with the logins of registries, in order. - /// Nothing is unsealed. + fn locate(&self, given: &str) -> Result { + locate(&self.store, &self.scope, given) + } + + /// The global secrets, without those that belong to something, by name. + pub fn secret_ids(&self) -> Result> { + Ok(self.store.labels(kind::SECRET, &self.scope)?) + } + + /// The names of the global secrets, and the addresses of the secrets + /// that belong to registries, hosts and provisioners, in order. Nothing + /// is unsealed. pub fn all_secret_names(&self) -> Result> { - Ok(self.store.names(kind::SECRET, &self.scope)?) + let mut names: Vec = self + .secret_ids()? + .into_iter() + .map(|found| found.name) + .collect(); + let owners = [ + (kind::REGISTRY, "registry"), + (kind::PROFILE, "host"), + (kind::PROVISIONER_ENTRY, "provisioner"), + ]; + let mut labels = std::collections::HashMap::new(); + for (kind, of) in owners { + for found in self.store.labels(kind, &self.scope)? { + labels.insert(found.id, (of, found.name)); + } + } + for row in self.store.names(kind::SECRET, &self.scope)? { + match row.split_once('/') { + Some((owner, label)) if berth_api::ids::is_id(owner) => { + match (labels.get(owner), label) { + (Some(("registry", name)), _) => names.push(format!("registry/{name}")), + (Some(("host", name)), label) => names.push(format!("{label}/{name}")), + (Some((_, name)), label) => { + names.push(format!("provisioner/{name}/{label}")) + } + (None, _) => {} + } + } + // A file's passwords, by its path. + Some(_) => names.push(row), + None => {} + } + } + names.sort(); + names.dedup(); + Ok(names) } /// The secrets the user named, without the passwords of registries and /// those berth logs in to hosts with. pub fn secret_names(&self) -> Vec { - self.all_secret_names() + self.secret_ids() .unwrap_or_default() .into_iter() - .filter(|name| { - !["registry/", "ssh/", "sudo/", "provisioner/"] - .iter() - .any(|kept| name.starts_with(kept)) - }) + .map(|found| found.name) .collect() } /// Whether a secret has a value. Nothing is unsealed. pub fn has_secret(&self, name: &str) -> Result { - Ok(self.all_secret_names()?.iter().any(|known| known == name)) + Ok(match self.locate(name)? { + Located::Global(id, _) => id.is_some(), + Located::Owned(row) => self.store.names(kind::SECRET, &self.scope)?.contains(&row), + Located::NoOwner(_) => false, + }) } pub fn secret(&self, name: &str) -> Result> { - match self.store.read(kind::SECRET, &self.scope, name)? { + let row = match self.locate(name)? { + Located::Global(Some(id), _) => id, + Located::Owned(row) => row, + Located::Global(None, _) | Located::NoOwner(_) => return Ok(None), + }; + match self.store.read(kind::SECRET, &self.scope, &row)? { Some(value) => Ok(Some( String::from_utf8(value).map_err(|_| format!("Secret {name} is not text"))?, )), @@ -218,16 +377,86 @@ impl Store { } } + /// Set a secret, or forget it with `None`. A new global secret gets an + /// id, and profiles that name it refer to that; one forgotten is named + /// by them again. pub fn set_secret(&self, name: &str, value: Option<&str>) -> Result<()> { - match value { - Some(value) => self - .store - .write(kind::SECRET, &self.scope, name, value.as_bytes())?, - None => { - self.store.forget(kind::SECRET, &self.scope, name)?; + let mut batch = store::Batch::default(); + match (self.locate(name)?, value) { + (Located::NoOwner(why), Some(_)) => return Err(why.into()), + (Located::NoOwner(_), None) | (Located::Global(None, _), None) => return Ok(()), + (Located::Owned(row), Some(value)) => { + batch.write(kind::SECRET, &self.scope, &row, value.as_bytes().to_vec()); + } + (Located::Owned(row), None) => { + batch.remove(kind::SECRET, &self.scope, &row); + } + (Located::Global(Some(id), label), Some(value)) => { + batch.write_labelled( + kind::SECRET, + &self.scope, + &id, + &label, + value.as_bytes().to_vec(), + ); + } + (Located::Global(None, label), Some(value)) => { + if !valid_secret_name(&label) { + return Err(format!("Not a name for a secret: {label}").into()); + } + let id = store::new_id(); + batch.write_labelled( + kind::SECRET, + &self.scope, + &id, + &label, + value.as_bytes().to_vec(), + ); + crate::profiles::swap_references( + &self.store, + &mut batch, + &self.scope, + kind::SECRET, + &label, + &id, + )?; + } + (Located::Global(Some(id), label), None) => { + batch.remove(kind::SECRET, &self.scope, &id); + crate::profiles::swap_references( + &self.store, + &mut batch, + &self.scope, + kind::SECRET, + &id, + &label, + )?; + } + } + Ok(self.store.apply(&batch)?) + } + + /// Give a global secret another name. The profiles that hand it to + /// hosts refer to its id, and go on doing so. + pub fn rename_secret(&self, old: &str, new: &str) -> Result<()> { + let Located::Global(Some(id), label) = self.locate(old)? else { + return Err(format!( + "No secret {old}. A secret that belongs to a registry, host or provisioner keeps its name" + ) + .into()); + }; + if label == new { + return Err(format!("{new} has that name already").into()); + } + if !valid_secret_name(new) { + return Err(format!("Not a name for a secret: {new}").into()); + } + match self.store.relabel(kind::SECRET, &self.scope, &id, new) { + Err(store::Error::Taken { .. }) => { + Err(format!("There is a secret {new} already").into()) } + result => Ok(result.map(|_| ())?), } - Ok(()) } /// `user:password` of a registry in base64, when its login is known. @@ -753,9 +982,11 @@ mod tests { store.set_secret("registry/verdaccio", Some("tok")).unwrap(); store.set_secret("github", Some("token")).unwrap(); store.set_secret("cert", Some("PEM")).unwrap(); + // The login belongs to the registry, by its id. + let nexus = store.find_registry("nexus").unwrap().unwrap().id; assert_eq!( opened - .read(kind::SECRET, store::OWN, "registry/nexus") + .read(kind::SECRET, store::OWN, &format!("{nexus}/login")) .unwrap() .unwrap(), b"pw" diff --git a/crates/berth-core/src/resource.rs b/crates/berth-core/src/resource.rs index 685aa9e..9e4e3de 100644 --- a/crates/berth-core/src/resource.rs +++ b/crates/berth-core/src/resource.rs @@ -94,26 +94,12 @@ fn scopes(paths: &Paths) -> Vec { /// A new id for a resource. pub fn new_id() -> Result { - let mut bytes = [0u8; 8]; - use std::io::Read; - std::fs::File::open("/dev/urandom")?.read_exact(&mut bytes)?; - let letters = b"abcdefghijklmnopqrstuvwxyz234567"; - Ok(format!( - "r-{}", - bytes - .iter() - .map(|byte| letters[(byte & 31) as usize] as char) - .collect::() - )) + Ok(store::new_id()) } -/// Whether a text can be a resource's id: one word of berth's own making. +/// Whether a text can be a resource's id: one of berth's own making. pub fn valid_id(id: &str) -> bool { - id.starts_with("r-") - && id.len() <= 40 - && id[2..] - .chars() - .all(|ch| ch.is_ascii_alphanumeric() || ch == '-') + berth_api::ids::is_id(id) } /// Every resource berth keeps here. @@ -141,11 +127,20 @@ pub fn list(paths: &Paths) -> Result> { Ok(kept) } -/// A resource by its id, or by its name when only one has it. +/// A resource by its id, bare or as `res_ID`, or by its name when only one +/// has it. pub fn find_by(paths: &Paths, given: &str) -> Result { let all = list(paths)?; - if let Some(found) = all.iter().find(|kept| kept.record.id == given) { - return Ok(found.clone()); + match berth_api::ids::read(berth_api::ids::Kind::Resource, given) + .map_err(store::Error::Given)? + { + berth_api::ids::Given::Prefixed(id) => return find(paths, &id), + berth_api::ids::Given::Bare(id) => { + if let Some(found) = all.iter().find(|kept| kept.record.id == id) { + return Ok(found.clone()); + } + } + berth_api::ids::Given::Name(_) => {} } let named: Vec<&Kept> = all .iter() @@ -200,13 +195,19 @@ impl Kept { } /// The place of the host it belongs to. - pub fn place(&self) -> Option { + pub fn place(&self, paths: &Paths) -> Option { if self.record.host.is_empty() { return None; } match self.scope.as_str() { FILE_SCOPE => Some(Place::File(PathBuf::from(&self.record.host))), - scope => Place::stored(scope, &self.record.host).ok(), + scope => { + let store = store::shared(paths).ok()?; + let name = store + .label_of(kind::PROFILE, scope, &self.record.host) + .ok()??; + Some(Place::at(scope, &self.record.host, &name)) + } } } @@ -282,9 +283,9 @@ impl Kept { /// host's lock, so that applying and changing the machine exclude each /// other. pub fn lock(&self, paths: &Paths) -> Result { - match self.place() { + match self.place(paths) { Some(place) if self.record.kind == plugins::MACHINE => { - let host = place.argument(); + let host = place.title(); crate::runner::lock_at(&place.lock_file(paths), "work on the machine", |holder| { format!( "Something else is being done to {host}'s machine{holder}. Try again when it has ended" @@ -301,8 +302,8 @@ impl Kept { } /// What it is called for people: its name, and its host's for a machine. - pub fn title(&self) -> String { - match self.place() { + pub fn title(&self, paths: &Paths) -> String { + match self.place(paths) { Some(place) => format!("{}'s machine", place.title()), None => self.record.name.clone(), } @@ -367,14 +368,28 @@ pub fn info(paths: &Paths, kept: &Kept, plugins: &[Plugin], busy: bool) -> Resou (false, _) if record.state.is_empty() => ("unknown".to_owned(), String::new()), (false, _) => (record.state.clone(), String::new()), }; + let provisioner_id = crate::provisioner::id_of(paths, &record.provisioner) + .ok() + .flatten() + .filter(|id| *id == record.provisioner) + .unwrap_or_default(); + let (host, host_id) = match kept.place(paths) { + Some(place @ Place::Stored { .. }) => (Some(place.title()), place.key()), + _ => ( + (!record.host.is_empty()).then(|| record.host.clone()), + String::new(), + ), + }; Resource { id: record.id.clone(), - provisioner: record.provisioner.clone(), + provisioner: crate::provisioner::label(paths, &record.provisioner), + provisioner_id, kind: record.kind.clone(), name: record.name.clone(), state, external_id: record.external_id.clone(), - host: (!record.host.is_empty()).then(|| record.host.clone()), + host, + host_id, address: record.address.clone(), endpoints: record.endpoints.clone(), arguments: record.arguments.clone(), @@ -396,11 +411,30 @@ pub fn listed( host: Option<&str>, ) -> Result> { let plugins = found(paths); + // Records keep both by id; either is given by name or id. + let provisioner = provisioner + .map(|given| crate::provisioner::reference(paths, given)) + .transpose()?; + let host = match host { + Some(given) if given.contains('/') || given.ends_with(".json") => Some(given.to_owned()), + Some(given) => Some( + Place::find(paths, &crate::profiles::scope(paths), given)? + .map_or_else(|| given.to_owned(), |place| place.key()), + ), + None => None, + }; Ok(list(paths)? .into_iter() - .filter(|kept| provisioner.is_none_or(|wanted| kept.record.provisioner == wanted)) + .filter(|kept| { + provisioner + .as_ref() + .is_none_or(|wanted| kept.record.provisioner == *wanted) + }) .filter(|kept| kind.is_none_or(|wanted| kept.record.kind == wanted)) - .filter(|kept| host.is_none_or(|wanted| kept.record.host == wanted)) + .filter(|kept| { + host.as_ref() + .is_none_or(|wanted| kept.record.host == *wanted) + }) .map(|kept| { let busy = kept.record.external_id.is_empty() && kept.busy(paths); info(paths, &kept, &plugins, busy) @@ -427,13 +461,16 @@ impl Called { .map_err(|_| { format!( "No provisioner {} any more, which made {}: add its plugin again", - kept.record.provisioner, - kept.title() + crate::provisioner::label(paths, &kept.record.provisioner), + kept.title(paths) ) })?; runnable(provisioner)?; let (secrets, missing) = secrets(store, &kept.record.provisioner, provisioner); - needs_secrets(&kept.record.provisioner, &missing)?; + needs_secrets( + &crate::provisioner::label(paths, &kept.record.provisioner), + &missing, + )?; Ok(Called { dir: plugin.dir.clone(), provisioner: provisioner.clone(), @@ -582,16 +619,16 @@ fn take_endpoints(record: &mut Record, answer: &Value, out: &mut dyn Write) { Ok(endpoint) if endpoints.iter().any(|known| known.name == endpoint.name) => say( out, &format!( - "! {} reported the endpoint {} twice; the first stands", - record.provisioner, endpoint.name + "! The provisioner of {} reported the endpoint {} twice; the first stands", + record.name, endpoint.name ), ), Ok(endpoint) => endpoints.push(endpoint), Err(why) => say( out, &format!( - "! {} reported an endpoint berth leaves out: {why}", - record.provisioner + "! The provisioner of {} reported an endpoint berth leaves out: {why}", + record.name ), ), } @@ -643,7 +680,12 @@ pub fn status(paths: &Paths, store: &Store, id: &str, options: &Options) -> Resu kept.save(paths)?; } } - None => problem = format!("{} status said no state", kept.record.provisioner), + None => { + problem = format!( + "{} status said no state", + crate::provisioner::label(paths, &kept.record.provisioner) + ) + } }, Err(error) => problem = error.to_string(), } @@ -691,10 +733,11 @@ pub fn prepare(paths: &Paths, store: &Store, asked: &berth_api::NewResource) -> "" => plugins::MACHINE.to_owned(), kind => kind.to_owned(), }; + let reference = crate::provisioner::reference(paths, &asked.provisioner)?; if let Some(token) = asked.token.as_deref().filter(|token| !token.is_empty()) && let Some(again) = list(paths)?.into_iter().find(|kept| { kept.record.asked == token - && kept.record.provisioner == asked.provisioner + && kept.record.provisioner == reference && kept.record.kind == kind }) { @@ -733,8 +776,10 @@ pub fn prepare(paths: &Paths, store: &Store, asked: &berth_api::NewResource) -> ) .into()); } + let reference = crate::provisioner::for_use(paths, &asked.provisioner)?; + let label = crate::provisioner::label(paths, &reference); let plugins = found(paths); - let (plugin, provisioner) = provisioner_of(paths, &plugins, &asked.provisioner)?; + let (plugin, provisioner) = provisioner_of(paths, &plugins, &reference)?; runnable(provisioner)?; let declared_kind = kind_of(provisioner, &kind).ok_or_else(|| { let kinds: Vec<&str> = provisioner @@ -743,8 +788,7 @@ pub fn prepare(paths: &Paths, store: &Store, asked: &berth_api::NewResource) -> .map(|kind| kind.id.as_str()) .collect(); format!( - "{} makes no {kind}. It makes: {}", - asked.provisioner, + "{label} makes no {kind}. It makes: {}", match kinds.is_empty() { true => "nothing".to_owned(), false => kinds.join(", "), @@ -769,29 +813,27 @@ pub fn prepare(paths: &Paths, store: &Store, asked: &berth_api::NewResource) -> } check_secrets(&asked.secrets)?; let existing = list(paths)?.into_iter().find(|kept| { - kept.record.provisioner == asked.provisioner - && kept.record.kind == kind - && kept.record.name == name + kept.record.provisioner == reference && kept.record.kind == kind && kept.record.name == name }); if let Some(existing) = existing .as_ref() .filter(|kept| !kept.record.external_id.is_empty()) { return Err(format!( - "There is a {kind} {name} of {} already, {}. Change it with: berth resource set {}, or remove it first", - asked.provisioner, existing.record.id, existing.record.id + "There is a {kind} {name} of {label} already, {}. Change it with: berth resource set {}, or remove it first", + existing.record.id, existing.record.id ) .into()); } - let settings = crate::provisioner::settings(paths, &asked.provisioner)?; + let settings = crate::provisioner::settings(paths, &reference)?; let arguments = values_of( - &asked.provisioner, + &label, &declared(provisioner, &kind), &asked.arguments, &settings, )?; - let (secrets, missing) = secrets(store, &asked.provisioner, provisioner); - needs_secrets(&asked.provisioner, &missing)?; + let (secrets, missing) = secrets(store, &reference, provisioner); + needs_secrets(&label, &missing)?; let environment = environment(store, &asked.secrets)?; // Asked again after a failure, the provisioner finds it by its token. let mut kept = match existing { @@ -805,7 +847,7 @@ pub fn prepare(paths: &Paths, store: &Store, asked: &berth_api::NewResource) -> scope: OWN.to_owned(), record: Record { id: new_id()?, - provisioner: asked.provisioner.clone(), + provisioner: reference.clone(), kind: kind.clone(), name: name.to_owned(), token: provision::new_token()?, @@ -824,7 +866,7 @@ pub fn prepare(paths: &Paths, store: &Store, asked: &berth_api::NewResource) -> &kept.record, &kept.record.arguments, &secrets, - &crate::provisioner::node_of(paths, &asked.provisioner), + &crate::provisioner::node_of(paths, &reference), ); request["token"] = json!(kept.record.token); if let Some(spec) = spec { @@ -892,7 +934,9 @@ impl Creating { out, &format!( "▸ Creating the {} {} with {}", - kept.record.kind, kept.record.name, kept.record.provisioner + kept.record.kind, + kept.record.name, + crate::provisioner::label(paths, &kept.record.provisioner) ), ); let answer = call( @@ -907,7 +951,7 @@ impl Creating { if external.is_empty() { return Err(format!( "{} create said no id. Asking again finds the same one", - kept.record.provisioner + crate::provisioner::label(paths, &kept.record.provisioner) ) .into()); } @@ -923,7 +967,8 @@ impl Creating { out, &format!( "✓ {} created {external}: {}", - kept.record.provisioner, kept.record.state + crate::provisioner::label(paths, &kept.record.provisioner), + kept.record.state ), ); Ok(()) @@ -982,7 +1027,7 @@ pub fn actions(paths: &Paths, store: &Store, kept: &Kept) -> Vec { let why = match provisioner { None => Some(format!( "No provisioner {} any more: add its plugin again", - record.provisioner + crate::provisioner::label(paths, &record.provisioner) )), Some(found) => runnable(found) .and_then(|()| { @@ -993,7 +1038,7 @@ pub fn actions(paths: &Paths, store: &Store, kept: &Kept) -> Vec { }) .err() .map(|error| error.to_string()) - .or_else(|| (!made).then(|| format!("{} is not made yet", kept.title()))), + .or_else(|| (!made).then(|| format!("{} is not made yet", kept.title(paths)))), }; let limit = |mut action: Action| { if let Some(why) = &why { @@ -1013,7 +1058,7 @@ pub fn actions(paths: &Paths, store: &Store, kept: &Kept) -> Vec { if can("stop") { actions.push(limit(Action { group: "Power".into(), - confirm: Some(format!("Stop {}?", kept.title())), + confirm: Some(format!("Stop {}?", kept.title(paths))), ..own( "stop", "Stop", @@ -1101,8 +1146,10 @@ pub fn actions(paths: &Paths, store: &Store, kept: &Kept) -> Vec { ), }; let remove = Action { - confirm: Some(match kept.place() { - Some(_) if machine => format!("Delete {}? The host's profile stays.", kept.title()), + confirm: Some(match kept.place(paths) { + Some(_) if machine => { + format!("Delete {}? The host's profile stays.", kept.title(paths)) + } _ => format!( "Remove the {} {} with what it holds?", record.kind, record.name @@ -1450,8 +1497,8 @@ impl Acting { kept.forget(paths)?; Ok(done(format!( "Forgot {}, whose making did not end. One {} made anyway is not removed", - kept.title(), - kept.record.provisioner + kept.title(paths), + crate::provisioner::label(paths, &kept.record.provisioner) ))) } Doing::Remove(called) => { @@ -1459,9 +1506,9 @@ impl Acting { out, &format!( "▸ Removing {} ({}) with {}", - kept.title(), + kept.title(paths), kept.record.external_id, - kept.record.provisioner + crate::provisioner::label(paths, &kept.record.provisioner) ), ); let request = called.request("destroy", &called.arguments()); @@ -1492,10 +1539,10 @@ impl Acting { } } kept.forget(paths)?; - Ok(done(match kept.place() { + Ok(done(match kept.place(paths) { Some(place) if kept.record.kind == plugins::MACHINE => format!( "Deleted {}'s machine. The profile stays, and so does its node on the tailnet: remove that there", - place.argument() + place.title() ), _ => format!("Removed the {} {}", kept.record.kind, kept.record.name), })) @@ -1534,9 +1581,9 @@ impl Acting { None => kept.save(paths)?, } Ok(done(match verb.as_str() { - "start" => format!("Started {}", kept.title()), - "stop" => format!("Stopped {}", kept.title()), - _ => format!("Changed {}: {}", kept.title(), kept.record.state), + "start" => format!("Started {}", kept.title(paths)), + "stop" => format!("Stopped {}", kept.title(paths)), + _ => format!("Changed {}: {}", kept.title(paths), kept.record.state), })) } Doing::Exec { @@ -1557,7 +1604,10 @@ impl Acting { .as_i64() .and_then(|code| i32::try_from(code).ok()) .ok_or_else(|| { - format!("{} exec said no exit status", kept.record.provisioner) + format!( + "{} exec said no exit status", + crate::provisioner::label(paths, &kept.record.provisioner) + ) })?; Ok(crate::provisioner::Answered { message: format!("{} exited with {code}", command[0]), @@ -1629,13 +1679,14 @@ pub fn logs( ) -> Result<()> { let kept = find(paths, id)?; if kept.record.external_id.is_empty() { - return Err(format!("{} is not made yet", kept.title()).into()); + return Err(format!("{} is not made yet", kept.title(paths)).into()); } let called = Called::load(paths, store, kept)?; if !called.can("logs") { return Err(format!( "{} gives no logs of a {}", - called.kept.record.provisioner, called.kept.record.kind + crate::provisioner::label(paths, &called.kept.record.provisioner), + called.kept.record.kind ) .into()); } @@ -1657,10 +1708,12 @@ pub fn logs( /// What a provisioner's own list says of what it holds, joined with /// berth's records of it. pub(crate) fn merge( + paths: &Paths, records: &[Kept], provisioner: &str, listed: Vec, ) -> Vec { + let provisioner = crate::provisioner::reference(paths, provisioner).unwrap_or_default(); let mut joined: Vec = records .iter() .filter(|kept| kept.record.provisioner == provisioner) @@ -1668,7 +1721,11 @@ pub(crate) fn merge( id: kept.record.external_id.clone(), kind: kept.record.kind.clone(), name: kept.record.name.clone(), - host: kept.record.host.clone(), + // A host by its name, as the provisioner's own list says it. + host: match kept.place(paths) { + Some(place) => place.title(), + None => kept.record.host.clone(), + }, state: String::new(), resource: kept.record.id.clone(), }) diff --git a/crates/berth-core/src/runner.rs b/crates/berth-core/src/runner.rs index 72e09c0..9020a37 100644 --- a/crates/berth-core/src/runner.rs +++ b/crates/berth-core/src/runner.rs @@ -175,6 +175,42 @@ impl Subject { } } + /// The subject with the id of the host, provisioner or resource it + /// names by name or id. A host in a file stays its path, and a + /// provisioner of a type no provisioner is registered for its type. + pub fn by_id(&self, paths: &Paths) -> Subject { + match self { + Subject::Host(given) => Subject::Host(host_key(paths, given)), + Subject::Provisioner(given) => Subject::Provisioner( + crate::provisioner::reference(paths, given).unwrap_or_else(|_| given.clone()), + ), + Subject::Resource(given) => Subject::Resource( + berth_api::ids::id_in(berth_api::ids::Kind::Resource, given) + .unwrap_or_else(|| given.clone()), + ), + Subject::Installation => Subject::Installation, + } + } + + /// The subject as people call it: the name of its host or provisioner + /// now, or its id when it has none any more. + pub fn named(&self, paths: &Paths) -> Subject { + let store = crate::store::shared(paths).ok(); + let label = |kind: &str, id: &str| { + store + .as_ref() + .and_then(|store| store.label_of(kind, crate::store::OWN, id).ok().flatten()) + .unwrap_or_else(|| id.to_owned()) + }; + match self { + Subject::Host(id) => Subject::Host(label(crate::store::kind::PROFILE, id)), + Subject::Provisioner(id) => { + Subject::Provisioner(label(crate::store::kind::PROVISIONER_ENTRY, id)) + } + other => other.clone(), + } + } + /// The file whose lock is held while something changes it. A host's is /// the one its machine's provisioning holds, so that the two exclude /// each other too. @@ -220,6 +256,50 @@ impl std::fmt::Display for Subject { } } +/// A job's hosts, as `studio, lab`, by their ids. +pub fn hosts_by_id(paths: &Paths, hosts: &str) -> String { + each_host(hosts, |host| host_key(paths, host)) +} + +/// The id of a saved host a name or id gives, or what was given: the path +/// of a profile in a file, or a host that is not saved. +fn host_key(paths: &Paths, given: &str) -> String { + if given.contains('/') || given.ends_with(".json") { + return Place::resolve(paths, given).map_or_else(|_| given.to_owned(), |place| place.key()); + } + Place::find(paths, &crate::profiles::scope(paths), given) + .ok() + .flatten() + .map_or_else(|| given.to_owned(), |place| place.key()) +} + +/// A job's hosts, kept by their ids, by their names now. +pub fn hosts_named(paths: &Paths, hosts: &str) -> String { + let store = crate::store::shared(paths).ok(); + each_host(hosts, |host| { + store + .as_ref() + .and_then(|store| { + store + .label_of(crate::store::kind::PROFILE, crate::store::OWN, host) + .ok() + .flatten() + }) + .unwrap_or_else(|| host.to_owned()) + }) +} + +fn each_host(hosts: &str, map: impl Fn(&str) -> String) -> String { + if hosts.trim().is_empty() { + return String::new(); + } + hosts + .split(", ") + .map(|host| map(host.trim())) + .collect::>() + .join(", ") +} + // ── Locks ─────────────────────────────────────────────────────────────────── /// Held while something changes a subject. A second caller is refused rather @@ -275,7 +355,8 @@ pub fn lock_at(path: &Path, doing: &str, refused: impl FnOnce(&str) -> String) - /// Take the subject's lock for what `doing` names, or say what else changes /// it now. pub fn lock(paths: &Paths, subject: &Subject, doing: &str) -> Result { - lock_at(&subject.lock_file(paths)?, doing, |holder| { + // By id, so that a rename does not give the subject a second lock. + lock_at(&subject.by_id(paths).lock_file(paths)?, doing, |holder| { format!("Something else is being done to {subject}{holder}. Try again when it has ended") }) } @@ -729,6 +810,9 @@ impl Runner { /// callers with the same token one gets the ticket to run it; the others /// find that job. pub fn reserve(&self, mut request: Request) -> Result { + // A job keeps its subject and its hosts by their ids, which a rename leaves as they are. + request.subject = request.subject.by_id(&self.paths); + request.host = hosts_by_id(&self.paths, &request.host); let (subject, action) = (request.subject.key(), request.action.clone()); if let Some(token) = &request.token && let Some(job) = self.store.job_by_token(&subject, &action, token)? diff --git a/crates/berth-core/src/seed.rs b/crates/berth-core/src/seed.rs index 97c9ff4..7d7a007 100644 --- a/crates/berth-core/src/seed.rs +++ b/crates/berth-core/src/seed.rs @@ -65,7 +65,12 @@ pub fn cloud(paths: &Paths, store: &Store, name: &str, delivered: bool) -> Resul .ok_or_else(|| format!("No host {name}"))?; let key = tailnet::seed_key(paths, &saved, name)?; let mut config = named(saved.clone(), name); - crate::kept_profile::for_seed(&mut config, &host.place.title(), &saved); + crate::kept_profile::for_seed( + &mut config, + &host.place.title(), + host.place.id().unwrap_or(""), + &saved, + ); let seeded = tailnet::profile_file(&config)?; // The machine's operator takes these keys, so that berth can always reach it. let own = crate::keys::operator_keys(paths, &saved)?; @@ -198,7 +203,12 @@ pub fn installer(paths: &Paths, name: &str, ubuntu: &std::path::Path) -> Result< let saved = host.config.clone(); let key = tailnet::seed_key(paths, &saved, name)?; let mut config = named(saved.clone(), name); - crate::kept_profile::for_seed(&mut config, &host.place.title(), &saved); + crate::kept_profile::for_seed( + &mut config, + &host.place.title(), + host.place.id().unwrap_or(""), + &saved, + ); let hostname = config["hostname"].as_str().unwrap_or(name).to_owned(); let profile = tailnet::profile_file(&config)?; let own = crate::keys::operator_keys(paths, &saved)?; diff --git a/crates/berth-core/src/simulate.rs b/crates/berth-core/src/simulate.rs index a815ece..3ea7b54 100644 --- a/crates/berth-core/src/simulate.rs +++ b/crates/berth-core/src/simulate.rs @@ -138,7 +138,7 @@ impl Seed { return Err("token has at least 32 characters and no spaces".into()); } for (name, host) in &seed.hosts { - Place::own(name).map_err(|error| format!("hosts.{name}: {error}"))?; + checked(name).map_err(|error| format!("hosts.{name}: {error}"))?; if host .profile .as_ref() @@ -211,7 +211,7 @@ impl Seed { .map_err(|error| format!("provisioners.{name}: {error}"))?; } for (name, profile, applied) in self.profiles() { - let place = Place::own(&name)?; + let place = Place::own(paths, &name)?; place.save(paths, &profile)?; if applied { let host = Host::load_at(paths, place)?; @@ -224,6 +224,14 @@ impl Seed { } } +/// A host's name, checked: it names a folder of the simulation. +fn checked(name: &str) -> Result<()> { + match crate::profiles::valid_name(name) { + true => Ok(()), + false => Err(format!("Not a host name: {name}").into()), + } +} + fn write_private(file: &Path, text: &str) -> Result<()> { use std::os::unix::fs::OpenOptionsExt; let mut opened = fs::OpenOptions::new() @@ -295,7 +303,7 @@ impl Simulation { } fn dir(&self, name: &str) -> Result { - Place::own(name)?; + checked(name)?; Ok(self.root.join("hosts").join(name)) } @@ -743,7 +751,7 @@ pub fn provision(verb: &str, input: &str, progress: &mut dyn Write) -> Result &str { + match id_kind(kind) { + Some(kind) => kind.noun(), + None => kind, + } +} + +/// The kind of id the things of a kind of `state` have, if they have one. +pub fn id_kind(kind: &str) -> Option { + Some(match kind { + kind::PROFILE => ids::Kind::Host, + kind::PROVISIONER_ENTRY => ids::Kind::Provisioner, + kind::RESOURCE => ids::Kind::Resource, + kind::REGISTRY => ids::Kind::Registry, + kind::SECRET => ids::Kind::Secret, + kind::KEY => ids::Kind::Key, + _ => return None, + }) +} + +/// A new id: a UUID of version 7 as [`berth_api::ids`] writes it. +pub fn new_id() -> String { + use std::io::Read; + let mut random = [0u8; 10]; + // The kernel's random source does not fail once the system has booted. + std::fs::File::open("/dev/urandom") + .and_then(|mut file| file.read_exact(&mut random)) + .expect("reading /dev/urandom"); + let millis = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|elapsed| elapsed.as_millis() as u64) + .unwrap_or_default(); + ids::from_parts(millis, random) +} + +/// Whether a text may be the label of a thing: not empty, and not in the +/// shape of an id. +pub fn check_label(kind: &str, label: &str) -> Result<()> { + if label.trim().is_empty() { + return Err(Error::Given(format!("A {} needs a name", noun(kind)))); + } + if ids::looks_like_id(label) { + return Err(Error::Given(format!( + "{label} is in the shape of an id, which no name of a {} may be", + noun(kind) + ))); + } + Ok(()) +} + +/// A thing found by its name or id. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Found { + pub id: String, + pub name: String, +} + /// The scope of this installation's own state; a mirror of a berth server uses another. pub const OWN: &str = ""; @@ -76,6 +134,14 @@ pub enum Error { }, /// A sealed value that does not open with its key, or a key that cannot be used. Sealing(String), + /// Another thing of the kind has the label already. + Taken { + kind: String, + label: String, + }, + /// A value given for a thing that cannot be one, such as the id of + /// another kind. + Given(String), } impl std::fmt::Display for Error { @@ -100,6 +166,8 @@ impl std::fmt::Display for Error { } } Error::Sealing(message) => f.write_str(message), + Error::Taken { kind, label } => write!(f, "There is a {} {label} already", noun(kind)), + Error::Given(message) => f.write_str(message), } } } @@ -229,6 +297,35 @@ pub struct StateRow { pub value: Vec, pub sealed: bool, pub updated: String, + /// The name of a thing with an id, which `name` holds; none for any + /// other row. Unique within its kind and scope. + pub label: Option, +} + +/// A change of `state` and of the hosts and subjects other tables name, +/// made in one transaction in this order: the removals, the rows of owners, +/// the writes, then the moves of messages, cursors and jobs. +#[derive(Debug, Clone, Default)] +pub struct Change { + /// (kind, scope, name) + pub removals: Vec<(String, String, String)>, + /// (scope, owner id): the rows that belong to the owner, as + /// [`owned_by`] tells them. + pub owners: Vec<(String, String)>, + pub writes: Vec, + /// (old, new) host of messages and cursors. + pub hosts: Vec<(String, String)>, + /// (job id, host, subject) to set. + pub jobs: Vec<(String, String, String)>, +} + +/// Whether a row without a label belongs to the thing with the id `owner`: +/// its name is the id, or starts with the id and a `/`. +pub fn owned_by(row_name: &str, owner: &str) -> bool { + row_name == owner + || row_name + .strip_prefix(owner) + .is_some_and(|rest| rest.starts_with('/')) } /// The tables of the store, in the order a copy writes them. @@ -429,35 +526,19 @@ pub trait Backend: Send { fn replace_states(&mut self, scope: &str, kinds: &[&str], rows: &[StateRow]) -> Result<()>; /// Every row of the table. fn all_states(&mut self) -> Result>; - /// In one transaction: insert or replace each of `writes`, and delete - /// the rows with the keys (kind, scope, name) in `removals`. - fn change_states( - &mut self, - writes: &[StateRow], - removals: &[(String, String, String)], - ) -> Result<()>; - /// In one transaction: the change of [`Backend::change_states`], and the - /// messages, channel cursor and jobs of the host `old` given to `new`. - /// What a host of the name `new` left before, which nothing would tell - /// from the renamed host's, goes: its messages, cursor and job tokens. - fn rename_host( + /// The row of a kind and scope with this label. + fn labelled(&mut self, kind: &str, scope: &str, label: &str) -> Result>; + /// Give a row another label. False when there is no such row. + fn relabel( &mut self, - old: &str, - new: &str, - writes: &[StateRow], - removals: &[(String, String, String)], - ) -> Result<()>; - /// In one transaction: the change of [`Backend::change_states`], and the - /// jobs of the subject `old`, such as `provisioner:NAME`, given to `new`. - /// The tokens of jobs `new` had before go, so that asking again does not - /// find them. - fn rename_subject( - &mut self, - old: &str, - new: &str, - writes: &[StateRow], - removals: &[(String, String, String)], - ) -> Result<()>; + kind: &str, + scope: &str, + name: &str, + label: &str, + updated: &str, + ) -> Result; + /// Make a change in one transaction, in the order [`Change`] lists. + fn change_states(&mut self, change: &Change) -> Result<()>; /// In one transaction, while nobody else writes to the table: give each /// row the value and `sealed` of its new version, leaving `updated` as /// it is. False, with nothing changed, when the table no longer holds @@ -725,6 +806,7 @@ impl Store { value, sealed, updated: updated.to_owned(), + label: None, }) } @@ -821,66 +903,143 @@ impl Store { writes: &[(&str, &str, &str, Vec)], removals: &[(&str, &str, &str)], ) -> Result<()> { + let mut batch = Batch::default(); + for (kind, scope, name, value) in writes { + batch.write(kind, scope, name, value.clone()); + } + for (kind, scope, name) in removals { + batch.remove(kind, scope, name); + } + self.apply(&batch) + } + + /// Make the changes of a batch in one transaction. + pub fn apply(&self, batch: &Batch) -> Result<()> { let updated = now(); - let rows = writes - .iter() - .map(|(kind, scope, name, value)| self.row(kind, scope, name, value, &updated)) - .collect::>>()?; - let removals: Vec<_> = removals - .iter() - .map(|(kind, scope, name)| { - ((*kind).to_owned(), (*scope).to_owned(), (*name).to_owned()) + let mut change = Change { + removals: batch.removals.clone(), + owners: batch.owners.clone(), + hosts: batch.hosts.clone(), + jobs: batch.jobs.clone(), + ..Change::default() + }; + for (kind, scope, name, label, value) in &batch.writes { + if let Some(label) = label { + check_label(kind, label)?; + } + let mut row = self.row(kind, scope, name, value, &updated)?; + row.label = label.clone(); + change.writes.push(row); + } + let result = self.backend().change_states(&change); + result.map_err(|error| taken(error, &batch.writes)) + } + + // ── Things with ids ───────────────────────────────────────────────────── + + /// The id of the thing of a kind with this name. + pub fn id_of(&self, kind: &str, scope: &str, label: &str) -> Result> { + Ok(self + .backend() + .labelled(kind, scope, label)? + .map(|row| row.name)) + } + + /// The name of the thing of a kind with this id. Nothing is unsealed. + pub fn label_of(&self, kind: &str, scope: &str, id: &str) -> Result> { + Ok(self + .backend() + .state(kind, scope, id)? + .and_then(|row| row.label)) + } + + /// The thing of a kind that a name or id gives: an id with the prefix + /// of its kind is that id alone; a bare id is that id when a thing has + /// it; anything else is a name. A prefix of another kind is refused. + pub fn find(&self, kind: &str, scope: &str, given: &str) -> Result> { + let found = |row: StateRow| { + row.label.clone().map(|name| Found { + id: row.name.clone(), + name, }) - .collect(); - self.backend().change_states(&rows, &removals) + }; + let Some(id_kind) = id_kind(kind) else { + return Ok(self.backend().labelled(kind, scope, given)?.and_then(found)); + }; + match ids::read(id_kind, given).map_err(Error::Given)? { + ids::Given::Prefixed(id) => Ok(self.backend().state(kind, scope, &id)?.and_then(found)), + ids::Given::Bare(id) => { + // The lock is let go before the name is looked up. + let row = self.backend().state(kind, scope, &id)?; + match row { + Some(row) if row.label.is_some() => Ok(found(row)), + _ => Ok(self.backend().labelled(kind, scope, given)?.and_then(found)), + } + } + ids::Given::Name(name) => { + Ok(self.backend().labelled(kind, scope, &name)?.and_then(found)) + } + } } - /// A change as [`Store::change`] makes it, in the same transaction as - /// moving the messages, channel cursor and jobs of the host `old` to - /// `new`: what renames a host of this installation's own. - pub fn rename_host( - &self, - old: &str, - new: &str, - writes: &[(&str, &str, &str, Vec)], - removals: &[(&str, &str, &str)], - ) -> Result<()> { - let updated = now(); - let rows = writes - .iter() - .map(|(kind, scope, name, value)| self.row(kind, scope, name, value, &updated)) - .collect::>>()?; - let removals: Vec<_> = removals - .iter() - .map(|(kind, scope, name)| { - ((*kind).to_owned(), (*scope).to_owned(), (*name).to_owned()) + /// The things of a kind in a scope, by name. + pub fn labels(&self, kind: &str, scope: &str) -> Result> { + let mut found: Vec = self + .backend() + .states(kind, scope)? + .into_iter() + .filter_map(|row| { + Some(Found { + name: row.label?, + id: row.name, + }) }) .collect(); - self.backend().rename_host(old, new, &rows, &removals) + found.sort_by(|one, other| one.name.cmp(&other.name)); + Ok(found) + } + + /// The things of a kind in a scope with their values, unsealed, by name. + pub fn labelled_entries(&self, kind: &str, scope: &str) -> Result)>> { + let mut found = vec![]; + for row in self.backend().states(kind, scope)? { + let Some(name) = row.label.clone() else { + continue; + }; + let id = row.name.clone(); + found.push((Found { id, name }, self.opened(row)?)); + } + found.sort_by(|(one, _), (other, _)| one.name.cmp(&other.name)); + Ok(found) } - /// A change as [`Store::change`] makes it, in the same transaction as - /// giving the jobs of the subject `old` to `new`, both as - /// [`crate::runner::Subject::key`] says them. - pub fn rename_subject( + /// Insert or replace the value of a thing with an id, under its name. + pub fn write_labelled( &self, - old: &str, - new: &str, - writes: &[(&str, &str, &str, Vec)], - removals: &[(&str, &str, &str)], + kind: &str, + scope: &str, + id: &str, + label: &str, + value: &[u8], ) -> Result<()> { - let updated = now(); - let rows = writes - .iter() - .map(|(kind, scope, name, value)| self.row(kind, scope, name, value, &updated)) - .collect::>>()?; - let removals: Vec<_> = removals - .iter() - .map(|(kind, scope, name)| { - ((*kind).to_owned(), (*scope).to_owned(), (*name).to_owned()) + let mut batch = Batch::default(); + batch.write_labelled(kind, scope, id, label, value.to_vec()); + self.apply(&batch) + } + + /// Give a thing another name, which no other thing of its kind has. + /// False when there is no thing with the id. + pub fn relabel(&self, kind: &str, scope: &str, id: &str, label: &str) -> Result { + check_label(kind, label)?; + self.backend() + .relabel(kind, scope, id, label, &now()) + .map_err(|error| match is_taken(&error) { + true => Error::Taken { + kind: kind.to_owned(), + label: label.to_owned(), + }, + false => error, }) - .collect(); - self.backend().rename_subject(old, new, &rows, &removals) } /// The ids of the keys the sealed rows of `state` were made with. @@ -959,6 +1118,96 @@ impl Store { } } +/// A write of a batch: (kind, scope, name, label, plain value). +type Write = (String, String, String, Option, Vec); + +/// Several changes to `state` to make in one transaction, with +/// [`Store::apply`]. +#[derive(Debug, Clone, Default)] +pub struct Batch { + writes: Vec, + removals: Vec<(String, String, String)>, + owners: Vec<(String, String)>, + hosts: Vec<(String, String)>, + jobs: Vec<(String, String, String)>, +} + +impl Batch { + /// Insert or replace a value; a row with a label keeps it. + pub fn write(&mut self, kind: &str, scope: &str, name: &str, value: Vec) -> &mut Self { + let key = (kind.to_owned(), scope.to_owned(), name.to_owned()); + self.writes.push((key.0, key.1, key.2, None, value)); + self + } + + /// Insert or replace the value of a thing with an id, under its name. + pub fn write_labelled( + &mut self, + kind: &str, + scope: &str, + id: &str, + label: &str, + value: Vec, + ) -> &mut Self { + let key = (kind.to_owned(), scope.to_owned(), id.to_owned()); + self.writes + .push((key.0, key.1, key.2, Some(label.to_owned()), value)); + self + } + + pub fn remove(&mut self, kind: &str, scope: &str, name: &str) -> &mut Self { + self.removals + .push((kind.to_owned(), scope.to_owned(), name.to_owned())); + self + } + + /// Remove every row that belongs to the thing with the id `owner`. + pub fn forget_owned(&mut self, scope: &str, owner: &str) -> &mut Self { + self.owners.push((scope.to_owned(), owner.to_owned())); + self + } + + /// Give the messages and the channel cursor of the host `old` to `new`. + pub fn move_host(&mut self, old: &str, new: &str) -> &mut Self { + self.hosts.push((old.to_owned(), new.to_owned())); + self + } + + /// Set the hosts and the subject of a job. + pub fn set_job(&mut self, id: &str, host: &str, subject: &str) -> &mut Self { + self.jobs + .push((id.to_owned(), host.to_owned(), subject.to_owned())); + self + } + + pub fn is_empty(&self) -> bool { + self.writes.is_empty() + && self.removals.is_empty() + && self.owners.is_empty() + && self.hosts.is_empty() + && self.jobs.is_empty() + } +} + +/// Whether an error of a backend is a label taken by another row. +fn is_taken(error: &Error) -> bool { + matches!(error, Error::Database(message) if message.contains("state_label") || message.contains("state.label")) +} + +/// The error of a change, with a label that was taken said as such. +fn taken(error: Error, writes: &[Write]) -> Error { + if !is_taken(&error) { + return error; + } + match writes.iter().find(|write| write.3.is_some()) { + Some((kind, _, _, Some(label), _)) => Error::Taken { + kind: kind.clone(), + label: label.clone(), + }, + _ => error, + } +} + /// What a copy did with one table. #[derive(Debug, Clone, PartialEq, Eq)] pub struct Copied { diff --git a/crates/berth-core/src/store/postgres.rs b/crates/berth-core/src/store/postgres.rs index fa74c9e..19d38d0 100644 --- a/crates/berth-core/src/store/postgres.rs +++ b/crates/berth-core/src/store/postgres.rs @@ -6,8 +6,8 @@ //! answer. use super::{ - Backend, CursorRow, Entry, Error, Filter, Job, JobFilter, KvRow, MessageRow, Result, Rows, - StateRow, Stored, Table, When, json_value, keep_from, keep_name, + Backend, Change, CursorRow, Entry, Error, Filter, Job, JobFilter, KvRow, MessageRow, Result, + Rows, StateRow, Stored, Table, When, json_value, keep_from, keep_name, }; use berth_api::{From, Keep, Level, Message}; use postgres::config::SslMode; @@ -51,6 +51,8 @@ pub(super) const MIGRATIONS: &[&str] = &[ CREATE UNIQUE INDEX jobs_token ON jobs(subject, action, token) WHERE token IS NOT NULL;", "ALTER TABLE jobs ADD COLUMN stopped_by TEXT;", "ALTER TABLE jobs ADD COLUMN arguments TEXT;", + "ALTER TABLE state ADD COLUMN label TEXT COLLATE \"C\"; + CREATE UNIQUE INDEX state_label ON state(kind, scope, label) WHERE label IS NOT NULL;", ]; /// The advisory lock that keeps two berths from migrating at once. @@ -58,7 +60,19 @@ const MIGRATION_LOCK: i64 = 0x6265_7274_6800; impl std::convert::From for Error { fn from(error: PgError) -> Self { - Error::Database(error.to_string()) + Error::Database(described(&error)) + } +} + +/// What the server said of an error, with the constraint it names: the +/// client's own text says only "db error". +fn described(error: &PgError) -> String { + match error.as_db_error() { + Some(db) => match db.constraint() { + Some(constraint) => format!("{} ({constraint})", db.message()), + None => db.message().to_owned(), + }, + None => error.to_string(), } } @@ -112,9 +126,9 @@ impl Link { Err(error) if !fresh && (closed(&error) || client.is_closed()) => { self.client = None; let (client, _) = self.client()?; - work(client).map_err(|error| self.error(error)) + work(client).map_err(|error| self.error(described(&error))) } - Err(error) => Err(self.error(error)), + Err(error) => Err(self.error(described(&error))), } } } @@ -263,7 +277,7 @@ fn insert_job(client: &mut impl postgres::GenericClient, job: &Job) -> PgResult< ) } -const STATE_COLUMNS: &str = "kind, scope, name, value, sealed, updated"; +const STATE_COLUMNS: &str = "kind, scope, name, value, sealed, updated, label"; fn state_row(row: &Row) -> StateRow { StateRow { @@ -273,13 +287,16 @@ fn state_row(row: &Row) -> StateRow { value: row.get(3), sealed: row.get(4), updated: row.get(5), + label: row.get(6), } } -const PUT_STATE: &str = "INSERT INTO state(kind, scope, name, value, sealed, updated) - VALUES ($1, $2, $3, $4, $5, $6) +// A write without a label keeps the one the row has. +const PUT_STATE: &str = "INSERT INTO state(kind, scope, name, value, sealed, updated, label) + VALUES ($1, $2, $3, $4, $5, $6, $7) ON CONFLICT (kind, scope, name) DO UPDATE - SET value = excluded.value, sealed = excluded.sealed, updated = excluded.updated"; + SET value = excluded.value, sealed = excluded.sealed, updated = excluded.updated, + label = COALESCE(excluded.label, state.label)"; fn put_state(client: &mut impl postgres::GenericClient, row: &StateRow) -> PgResult<()> { client.execute( @@ -291,11 +308,26 @@ fn put_state(client: &mut impl postgres::GenericClient, row: &StateRow) -> PgRes &row.value, &row.sealed, &row.updated, + &row.label, ], )?; Ok(()) } +/// Delete the rows that belong to `owner` in `scope`: those without a label +/// whose name is the owner's id or starts with it and a `/`. +fn forget_owned( + client: &mut impl postgres::GenericClient, + scope: &str, + owner: &str, +) -> PgResult<()> { + client.execute( + "DELETE FROM state WHERE scope = $1 AND label IS NULL AND (name = $2 OR left(name, length($2) + 1) = $2 || '/')", + &[&scope, &owner], + )?; + Ok(()) +} + fn revision( client: &mut impl postgres::GenericClient, namespace: &str, @@ -804,6 +836,38 @@ impl Backend for PostgresBackend { }) } + fn labelled(&mut self, kind: &str, scope: &str, label: &str) -> Result> { + let (kind, scope, label) = (kind.to_owned(), scope.to_owned(), label.to_owned()); + self.call(move |client| { + let row = client.query_opt( + &format!( + "SELECT {STATE_COLUMNS} FROM state WHERE kind = $1 AND scope = $2 AND label = $3" + ), + &[&kind, &scope, &label], + )?; + Ok(row.as_ref().map(state_row)) + }) + } + + fn relabel( + &mut self, + kind: &str, + scope: &str, + name: &str, + label: &str, + updated: &str, + ) -> Result { + let given = [kind, scope, name, label, updated].map(str::to_owned); + self.call(move |client| { + let [kind, scope, name, label, updated] = &given; + let changed = client.execute( + "UPDATE state SET label = $4, updated = $5 WHERE kind = $1 AND scope = $2 AND name = $3", + &[kind, scope, name, label, updated], + )?; + Ok(changed == 1) + }) + } + fn put_state(&mut self, row: &StateRow) -> Result<()> { let row = row.clone(); self.call(move |client| put_state(client, &row)) @@ -847,96 +911,33 @@ impl Backend for PostgresBackend { }) } - fn change_states( - &mut self, - writes: &[StateRow], - removals: &[(String, String, String)], - ) -> Result<()> { - let (writes, removals) = (writes.to_vec(), removals.to_vec()); + fn change_states(&mut self, change: &Change) -> Result<()> { + let change = change.clone(); self.call(move |client| { let mut transaction = client.transaction()?; - for row in &writes { - put_state(&mut transaction, row)?; - } - for (kind, scope, name) in &removals { + for (kind, scope, name) in &change.removals { transaction.execute( "DELETE FROM state WHERE kind = $1 AND scope = $2 AND name = $3", &[kind, scope, name], )?; } - transaction.commit() - }) - } - - fn rename_host( - &mut self, - old: &str, - new: &str, - writes: &[StateRow], - removals: &[(String, String, String)], - ) -> Result<()> { - let (writes, removals) = (writes.to_vec(), removals.to_vec()); - let (old, new) = (old.to_owned(), new.to_owned()); - self.call(move |client| { - let mut transaction = client.transaction()?; - for row in &writes { - put_state(&mut transaction, row)?; - } - for (kind, scope, name) in &removals { - transaction.execute( - "DELETE FROM state WHERE kind = $1 AND scope = $2 AND name = $3", - &[kind, scope, name], - )?; + for (scope, owner) in &change.owners { + forget_owned(&mut transaction, scope, owner)?; } - let (old_subject, new_subject) = (format!("host:{old}"), format!("host:{new}")); - transaction.execute("DELETE FROM messages WHERE host = $1", &[&new])?; - transaction.execute("DELETE FROM cursors WHERE host = $1", &[&new])?; - transaction.execute( - "UPDATE messages SET host = $2 WHERE host = $1", - &[&old, &new], - )?; - transaction.execute( - "UPDATE cursors SET host = $2 WHERE host = $1", - &[&old, &new], - )?; - transaction.execute("UPDATE jobs SET host = $2 WHERE host = $1", &[&old, &new])?; - transaction.execute( - "UPDATE jobs SET token = NULL WHERE subject = $1", - &[&new_subject], - )?; - transaction.execute( - "UPDATE jobs SET subject = $2 WHERE subject = $1", - &[&old_subject, &new_subject], - )?; - transaction.commit() - }) - } - - fn rename_subject( - &mut self, - old: &str, - new: &str, - writes: &[StateRow], - removals: &[(String, String, String)], - ) -> Result<()> { - let (writes, removals) = (writes.to_vec(), removals.to_vec()); - let (old, new) = (old.to_owned(), new.to_owned()); - self.call(move |client| { - let mut transaction = client.transaction()?; - for row in &writes { + for row in &change.writes { put_state(&mut transaction, row)?; } - for (kind, scope, name) in &removals { + for (old, new) in &change.hosts { + transaction + .execute("UPDATE messages SET host = $2 WHERE host = $1", &[old, new])?; + transaction.execute("UPDATE cursors SET host = $2 WHERE host = $1", &[old, new])?; + } + for (id, host, subject) in &change.jobs { transaction.execute( - "DELETE FROM state WHERE kind = $1 AND scope = $2 AND name = $3", - &[kind, scope, name], + "UPDATE jobs SET host = $2, subject = $3 WHERE id = $1", + &[id, host, subject], )?; } - transaction.execute("UPDATE jobs SET token = NULL WHERE subject = $1", &[&new])?; - transaction.execute( - "UPDATE jobs SET subject = $2 WHERE subject = $1", - &[&old, &new], - )?; transaction.commit() }) } @@ -1058,8 +1059,8 @@ impl Backend for PostgresBackend { match &rows { Rows::State(rows) => { let statement = transaction.prepare( - "INSERT INTO state(kind, scope, name, value, sealed, updated) - VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT DO NOTHING", + "INSERT INTO state(kind, scope, name, value, sealed, updated, label) + VALUES ($1, $2, $3, $4, $5, $6, $7) ON CONFLICT DO NOTHING", )?; for row in rows { added += transaction.execute( @@ -1071,6 +1072,7 @@ impl Backend for PostgresBackend { &row.value, &row.sealed, &row.updated, + &row.label, ], )?; } diff --git a/crates/berth-core/src/store/sqlite.rs b/crates/berth-core/src/store/sqlite.rs index 173801b..e46df44 100644 --- a/crates/berth-core/src/store/sqlite.rs +++ b/crates/berth-core/src/store/sqlite.rs @@ -1,8 +1,8 @@ //! The store in a SQLite file. use super::{ - Backend, CursorRow, Entry, Error, Filter, Job, JobFilter, KvRow, MessageRow, Result, Rows, - StateRow, Stored, Table, When, json_value, keep_from, keep_name, + Backend, Change, CursorRow, Entry, Error, Filter, Job, JobFilter, KvRow, MessageRow, Result, + Rows, StateRow, Stored, Table, When, json_value, keep_from, keep_name, }; use berth_api::{From, Level, Message}; use rusqlite::{Connection, OptionalExtension, params}; @@ -35,6 +35,8 @@ pub(super) const MIGRATIONS: &[&str] = &[ CREATE UNIQUE INDEX jobs_token ON jobs(subject, action, token) WHERE token IS NOT NULL;", "ALTER TABLE jobs ADD COLUMN stopped_by TEXT;", "ALTER TABLE jobs ADD COLUMN arguments TEXT;", + "ALTER TABLE state ADD COLUMN label TEXT; + CREATE UNIQUE INDEX state_label ON state(kind, scope, label) WHERE label IS NOT NULL;", ]; impl std::convert::From for Error { @@ -147,7 +149,7 @@ fn entry(row: &rusqlite::Row<'_>) -> rusqlite::Result { }) } -const STATE_COLUMNS: &str = "kind, scope, name, value, sealed, updated"; +const STATE_COLUMNS: &str = "kind, scope, name, value, sealed, updated, label"; fn state_row(row: &rusqlite::Row<'_>) -> rusqlite::Result { Ok(StateRow { @@ -157,13 +159,16 @@ fn state_row(row: &rusqlite::Row<'_>) -> rusqlite::Result { value: row.get(3)?, sealed: row.get(4)?, updated: row.get(5)?, + label: row.get(6)?, }) } -const PUT_STATE: &str = "INSERT INTO state(kind, scope, name, value, sealed, updated) - VALUES (?1, ?2, ?3, ?4, ?5, ?6) +// A write without a label keeps the one the row has. +const PUT_STATE: &str = "INSERT INTO state(kind, scope, name, value, sealed, updated, label) + VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7) ON CONFLICT (kind, scope, name) DO UPDATE - SET value = excluded.value, sealed = excluded.sealed, updated = excluded.updated"; + SET value = excluded.value, sealed = excluded.sealed, updated = excluded.updated, + label = COALESCE(excluded.label, state.label)"; fn put_state(db: &Connection, row: &StateRow) -> rusqlite::Result<()> { db.execute( @@ -174,12 +179,23 @@ fn put_state(db: &Connection, row: &StateRow) -> rusqlite::Result<()> { row.name, row.value, row.sealed, - row.updated + row.updated, + row.label ], )?; Ok(()) } +/// Delete the rows that belong to `owner` in `scope`: those without a label +/// whose name is the owner's id or starts with it and a `/`. +fn forget_owned(db: &Connection, scope: &str, owner: &str) -> rusqlite::Result<()> { + db.execute( + "DELETE FROM state WHERE scope = ?1 AND label IS NULL AND (name = ?2 OR substr(name, 1, length(?2) + 1) = ?2 || '/')", + [scope, owner], + )?; + Ok(()) +} + impl SqliteBackend { /// Open the file, creating it and bringing its schema up to date. pub fn open(path: &Path) -> Result { @@ -537,6 +553,34 @@ impl Backend for SqliteBackend { Ok(rows.collect::>>()?) } + fn labelled(&mut self, kind: &str, scope: &str, label: &str) -> Result> { + Ok(self + .db + .query_row( + &format!( + "SELECT {STATE_COLUMNS} FROM state WHERE kind = ?1 AND scope = ?2 AND label = ?3" + ), + [kind, scope, label], + state_row, + ) + .optional()?) + } + + fn relabel( + &mut self, + kind: &str, + scope: &str, + name: &str, + label: &str, + updated: &str, + ) -> Result { + let changed = self.db.execute( + "UPDATE state SET label = ?4, updated = ?5 WHERE kind = ?1 AND scope = ?2 AND name = ?3", + [kind, scope, name, label, updated], + )?; + Ok(changed == 1) + } + fn put_state(&mut self, row: &StateRow) -> Result<()> { Ok(put_state(&self.db, row)?) } @@ -574,88 +618,32 @@ impl Backend for SqliteBackend { Ok(rows.collect::>>()?) } - fn change_states( - &mut self, - writes: &[StateRow], - removals: &[(String, String, String)], - ) -> Result<()> { + fn change_states(&mut self, change: &Change) -> Result<()> { let transaction = self .db .transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?; - for row in writes { - put_state(&transaction, row)?; - } - for (kind, scope, name) in removals { + for (kind, scope, name) in &change.removals { transaction.execute( "DELETE FROM state WHERE kind = ?1 AND scope = ?2 AND name = ?3", [kind, scope, name], )?; } - transaction.commit()?; - Ok(()) - } - - fn rename_host( - &mut self, - old: &str, - new: &str, - writes: &[StateRow], - removals: &[(String, String, String)], - ) -> Result<()> { - let transaction = self - .db - .transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?; - for row in writes { - put_state(&transaction, row)?; - } - for (kind, scope, name) in removals { - transaction.execute( - "DELETE FROM state WHERE kind = ?1 AND scope = ?2 AND name = ?3", - [kind, scope, name], - )?; + for (scope, owner) in &change.owners { + forget_owned(&transaction, scope, owner)?; } - let (old_subject, new_subject) = (format!("host:{old}"), format!("host:{new}")); - transaction.execute("DELETE FROM messages WHERE host = ?1", [new])?; - transaction.execute("DELETE FROM cursors WHERE host = ?1", [new])?; - transaction.execute("UPDATE messages SET host = ?2 WHERE host = ?1", [old, new])?; - transaction.execute("UPDATE cursors SET host = ?2 WHERE host = ?1", [old, new])?; - transaction.execute("UPDATE jobs SET host = ?2 WHERE host = ?1", [old, new])?; - transaction.execute( - "UPDATE jobs SET token = NULL WHERE subject = ?1", - [&new_subject], - )?; - transaction.execute( - "UPDATE jobs SET subject = ?2 WHERE subject = ?1", - [&old_subject, &new_subject], - )?; - transaction.commit()?; - Ok(()) - } - - fn rename_subject( - &mut self, - old: &str, - new: &str, - writes: &[StateRow], - removals: &[(String, String, String)], - ) -> Result<()> { - let transaction = self - .db - .transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?; - for row in writes { + for row in &change.writes { put_state(&transaction, row)?; } - for (kind, scope, name) in removals { + for (old, new) in &change.hosts { + transaction.execute("UPDATE messages SET host = ?2 WHERE host = ?1", [old, new])?; + transaction.execute("UPDATE cursors SET host = ?2 WHERE host = ?1", [old, new])?; + } + for (id, host, subject) in &change.jobs { transaction.execute( - "DELETE FROM state WHERE kind = ?1 AND scope = ?2 AND name = ?3", - [kind, scope, name], + "UPDATE jobs SET host = ?2, subject = ?3 WHERE id = ?1", + [id, host, subject], )?; } - transaction.execute("UPDATE jobs SET token = NULL WHERE subject = ?1", [new])?; - transaction.execute( - "UPDATE jobs SET subject = ?2 WHERE subject = ?1", - [old, new], - )?; transaction.commit()?; Ok(()) } @@ -761,8 +749,8 @@ impl Backend for SqliteBackend { match rows { Rows::State(rows) => { let mut statement = transaction.prepare( - "INSERT INTO state(kind, scope, name, value, sealed, updated) - VALUES (?1, ?2, ?3, ?4, ?5, ?6) ON CONFLICT DO NOTHING", + "INSERT INTO state(kind, scope, name, value, sealed, updated, label) + VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7) ON CONFLICT DO NOTHING", )?; for row in rows { added += statement.execute(params![ @@ -771,7 +759,8 @@ impl Backend for SqliteBackend { row.name, row.value, row.sealed, - row.updated + row.updated, + row.label ])?; } } diff --git a/crates/berth-core/src/store/suite.rs b/crates/berth-core/src/store/suite.rs index 7702c6c..a85508b 100644 --- a/crates/berth-core/src/store/suite.rs +++ b/crates/berth-core/src/store/suite.rs @@ -92,8 +92,12 @@ on_each_backend!( state_rows_are_kept_listed_and_removed, replacing_states_leaves_other_scopes_and_kinds_alone, changes_are_all_or_nothing, - a_renamed_host_takes_its_rows_messages_and_jobs, - a_renamed_subject_takes_its_rows_and_jobs, + things_are_kept_by_id_and_found_by_name_or_id, + a_rename_changes_the_label_alone, + what_belongs_to_a_thing_goes_with_it, + a_change_moves_messages_cursors_and_jobs, + a_store_of_names_takes_ids_once, + renames_keep_what_belongs_and_refers, sealed_values_round_trip, a_sealed_store_without_its_key_says_so, plain_rows_are_read_with_a_key_loaded, @@ -814,51 +818,236 @@ fn changes_are_all_or_nothing(place: &Place) { assert_eq!(all_names(&store), before); } -fn a_renamed_host_takes_its_rows_messages_and_jobs(place: &Place) { +const STUDIO: &str = "01972c4e8f3a7b219d4e5a6b7c8d9e0f"; +const LAB: &str = "01972c4e8f3a7b219d4e5a6b7c8d9e1f"; + +fn things_are_kept_by_id_and_found_by_name_or_id(place: &Place) { let store = place.open().with_keys(Some(keys()), None); - store.write("profile", OWN, "test", b"p").unwrap(); - store.write("profile", OWN, "lab", b"l").unwrap(); - store.take_in("test", &message(1, "a", Keep::Log)).unwrap(); - store.take_in("test", &message(2, "b", Keep::Log)).unwrap(); - store.take_in("lab", &message(1, "a", Keep::Log)).unwrap(); - // A removed host of the new name left a message and a cursor. store - .take_in("studio", &message(1, "old", Keep::Log)) + .write_labelled(kind::PROFILE, OWN, STUDIO, "studio", b"p") .unwrap(); - let job = |id: &str, host: &str, subject: &str, token: Option<&str>| Job { - id: id.into(), - host: host.into(), - what: "apply".into(), - started: rfc3339(unix_now()), - output_path: format!("/tmp/{id}.log"), - subject: subject.into(), - action: "apply".into(), - token: token.map(str::to_owned), - ..Job::default() - }; store - .add_job(&job("j1", "test", "host:test", Some("t"))) + .write_labelled( + kind::PROFILE, + "served:http://192.0.2.7:7777", + LAB, + "studio", + b"s", + ) .unwrap(); + let found = |given: &str| store.find(kind::PROFILE, OWN, given).unwrap(); + let studio = Some(Found { + id: STUDIO.into(), + name: "studio".into(), + }); + assert_eq!(found("studio"), studio); + assert_eq!(found(STUDIO), studio); + assert_eq!(found(&format!("host_{STUDIO}")), studio); + assert_eq!(found(&format!("HOST_{}", STUDIO.to_uppercase())), studio); + assert_eq!(found("01972c4e-8f3a-7b21-9d4e-5a6b7c8d9e0f"), studio); + // An id with its prefix is that id alone; a bare one no thing has is a name. + assert_eq!(found(&format!("host_{LAB}")), None); + assert_eq!(found(LAB), None); + // A prefix of another kind is refused. + let wrong = store + .find(kind::PROFILE, OWN, &format!("prov_{STUDIO}")) + .unwrap_err(); + assert!(matches!(wrong, Error::Given(_)), "{wrong}"); + assert_eq!( + store.read(kind::PROFILE, OWN, STUDIO).unwrap().unwrap(), + b"p" + ); + assert_eq!( + store + .id_of(kind::PROFILE, OWN, "studio") + .unwrap() + .as_deref(), + Some(STUDIO) + ); + assert_eq!( + store + .label_of(kind::PROFILE, OWN, STUDIO) + .unwrap() + .as_deref(), + Some("studio") + ); + assert_eq!( + store.labels(kind::PROFILE, OWN).unwrap(), + vec![studio.clone().unwrap()] + ); + // A write without a label keeps the one the row has. + store.write(kind::PROFILE, OWN, STUDIO, b"q").unwrap(); + assert_eq!(found("studio").unwrap().id, STUDIO); + let (thing, value) = store + .labelled_entries(kind::PROFILE, OWN) + .unwrap() + .remove(0); + assert_eq!( + (thing.name.as_str(), value.as_slice()), + ("studio", &b"q"[..]) + ); + // A name is unique in its kind and scope, and never in the shape of an id. + let taken = store + .write_labelled(kind::PROFILE, OWN, LAB, "studio", b"x") + .unwrap_err(); + assert!( + matches!(&taken, Error::Taken { label, .. } if label == "studio"), + "{taken}" + ); + assert_eq!(taken.to_string(), "There is a host studio already"); + for shaped in [LAB.to_owned(), format!("reg_{LAB}")] { + let refused = store + .write_labelled(kind::REGISTRY, OWN, LAB, &shaped, b"x") + .unwrap_err(); + assert!(matches!(refused, Error::Given(_)), "{refused}"); + } + assert_eq!(store.labels(kind::PROFILE, OWN).unwrap().len(), 1); + // A copy of the store keeps the labels. + let Rows::State(rows) = store.export(Table::State).unwrap() else { + panic!("state rows") + }; + assert!( + rows.iter() + .all(|row| row.label.as_deref() == Some("studio")) + ); +} + +fn a_rename_changes_the_label_alone(place: &Place) { + let store = place.open().with_keys(Some(keys()), None); store - .add_job(&job("j2", "studio", "host:studio", Some("t"))) + .write_labelled(kind::PROVISIONER_ENTRY, OWN, STUDIO, "vm", b"e") .unwrap(); store - .add_job(&job("j3", "test, lab", "installation", None)) + .write_labelled(kind::PROVISIONER_ENTRY, OWN, LAB, "lab", b"l") .unwrap(); + let before = store + .backend() + .state(kind::PROVISIONER_ENTRY, OWN, STUDIO) + .unwrap() + .unwrap(); + assert!( + store + .relabel(kind::PROVISIONER_ENTRY, OWN, STUDIO, "big") + .unwrap() + ); + let after = store + .backend() + .state(kind::PROVISIONER_ENTRY, OWN, STUDIO) + .unwrap() + .unwrap(); + // The sealed value is the one it had, and opens under the id as before. + assert_eq!( + (after.value, after.label.as_deref()), + (before.value, Some("big")) + ); + assert_eq!( + store + .read(kind::PROVISIONER_ENTRY, OWN, STUDIO) + .unwrap() + .unwrap(), + b"e" + ); + assert_eq!( + store.id_of(kind::PROVISIONER_ENTRY, OWN, "vm").unwrap(), + None + ); + let taken = store + .relabel(kind::PROVISIONER_ENTRY, OWN, STUDIO, "lab") + .unwrap_err(); + assert_eq!(taken.to_string(), "There is a provisioner lab already"); + assert!(matches!( + store.relabel(kind::PROVISIONER_ENTRY, OWN, STUDIO, LAB), + Err(Error::Given(_)) + )); + assert!( + !store + .relabel( + kind::PROVISIONER_ENTRY, + OWN, + "01972c4e8f3a7b219d4e5a6b7c8d9eff", + "x" + ) + .unwrap() + ); +} +fn what_belongs_to_a_thing_goes_with_it(place: &Place) { + let store = place.open().with_keys(Some(keys()), None); store - .rename_host( - "test", - "studio", - &[("profile", OWN, "studio", b"p".to_vec())], - &[("profile", OWN, "test")], - ) + .write_labelled(kind::PROVISIONER_ENTRY, OWN, STUDIO, "vm", b"e") .unwrap(); + store + .write_labelled(kind::PROVISIONER_ENTRY, OWN, LAB, "lab", b"l") + .unwrap(); + for (owner, label) in [ + (STUDIO, "server_cert"), + (STUDIO, "client_key"), + (LAB, "server_cert"), + ] { + store + .write( + kind::SECRET, + OWN, + &format!("{owner}/{label}"), + label.as_bytes(), + ) + .unwrap(); + } + store.write(kind::ACCESS, OWN, STUDIO, b"a").unwrap(); + // A global secret whose name starts as the id does belongs to nothing. + store + .write_labelled(kind::SECRET, OWN, LAB, &format!("{}x", &STUDIO[..8]), b"g") + .unwrap(); + let mut batch = Batch::default(); + batch + .remove(kind::PROVISIONER_ENTRY, OWN, STUDIO) + .forget_owned(OWN, STUDIO); + store.apply(&batch).unwrap(); + let mut left: Vec = all_names(&store) + .into_iter() + .map(|(kind, _, name)| format!("{kind} {name}")) + .collect(); + left.sort(); assert_eq!( - all_names(&store), - owned(&[("profile", "", "lab"), ("profile", "", "studio")]) + left, + [ + format!("provisioner-entry {LAB}"), + format!("secret {LAB}"), + format!("secret {LAB}/server_cert"), + ] ); - assert_eq!(store.read("profile", OWN, "studio").unwrap().unwrap(), b"p"); + assert!(owned_by(&format!("{STUDIO}/x"), STUDIO) && owned_by(STUDIO, STUDIO)); + assert!(!owned_by(&format!("{STUDIO}x"), STUDIO)); +} + +fn a_change_moves_messages_cursors_and_jobs(place: &Place) { + let store = place.open(); + store + .take_in("studio", &message(1, "a", Keep::Log)) + .unwrap(); + store + .take_in("studio", &message(2, "b", Keep::Log)) + .unwrap(); + store.take_in("lab", &message(1, "a", Keep::Log)).unwrap(); + let job = Job { + id: "j1".into(), + host: "studio, lab".into(), + what: "apply".into(), + started: rfc3339(unix_now()), + output_path: "/tmp/j1.log".into(), + subject: "host:studio".into(), + action: "apply".into(), + token: Some("t".into()), + ..Job::default() + }; + store.add_job(&job).unwrap(); + let mut batch = Batch::default(); + batch.move_host("studio", STUDIO).set_job( + "j1", + &format!("{STUDIO}, lab"), + &format!("host:{STUDIO}"), + ); + store.apply(&batch).unwrap(); let topics = |host: &str| -> Vec { all(&store) .into_iter() @@ -866,84 +1055,382 @@ fn a_renamed_host_takes_its_rows_messages_and_jobs(place: &Place) { .map(|stored| stored.message.topic) .collect() }; - assert_eq!(topics("studio"), ["b", "a"]); - assert!(topics("test").is_empty()); + assert_eq!(topics(STUDIO), ["b", "a"]); + assert!(topics("studio").is_empty()); assert_eq!(topics("lab"), ["a"]); - assert_eq!(store.cursor("studio").unwrap(), 2); - assert_eq!(store.cursor("test").unwrap(), 0); + assert_eq!(store.cursor(STUDIO).unwrap(), 2); // The cursor goes on from where it was: nothing is taken in twice. assert!( store - .take_in("studio", &message(2, "b", Keep::Log)) + .take_in(STUDIO, &message(2, "b", Keep::Log)) .unwrap() .is_none() ); - - let renamed = store.job("j1").unwrap().unwrap(); + let moved = store.job("j1").unwrap().unwrap(); assert_eq!( - (renamed.host.as_str(), renamed.subject.as_str()), - ("studio", "host:studio") + (moved.host.as_str(), moved.subject.as_str()), + ( + format!("{STUDIO}, lab").as_str(), + format!("host:{STUDIO}").as_str() + ) ); - let found = store.job_by_token("host:studio", "apply", "t").unwrap(); + let found = store + .job_by_token(&format!("host:{STUDIO}"), "apply", "t") + .unwrap(); assert_eq!(found.map(|job| job.id), Some("j1".into())); - assert_eq!(store.job("j3").unwrap().unwrap().host, "test, lab"); } -fn a_renamed_subject_takes_its_rows_and_jobs(place: &Place) { - let store = place.open().with_keys(Some(keys()), None); - store.write("provisioner-entry", OWN, "vm", b"e").unwrap(); - let job = |id: &str, subject: &str, token: Option<&str>| Job { - id: id.into(), - what: "settings".into(), - started: rfc3339(unix_now()), - output_path: format!("/tmp/{id}.log"), - subject: subject.into(), - action: "settings".into(), - token: token.map(str::to_owned), - ..Job::default() - }; +/// A store as berth kept it before ids: a host with its passwords and its +/// machine, a node's provisioner with a certificate of its own, a registry +/// with its login, secrets all share, a job and a message. +fn store_of_names(store: &Store) { + let json = |value: Value| value.to_string().into_bytes(); + let rows: Vec<(&str, &str, Vec)> = vec![ + ( + kind::PROFILE, + "studio", + json( + json!({"access": {"address": "192.0.2.10", "auth": "key:deploy"}, + "registries": ["nexus"], "secrets": [{"name": "github", "env": "GH_TOKEN"}]}), + ), + ), + ( + kind::PROFILE, + "dock", + json(json!({"type": "node", "access": {"address": "192.0.2.20"}})), + ), + (kind::CREDENTIALS, "studio", json(json!({"ops": "made-up"}))), + ( + kind::PROVISIONER_ENTRY, + "lab", + json(json!({"type": "fake/vm", "node": "dock"})), + ), + ( + kind::REGISTRY, + "nexus", + json(json!({"type": "nexus", "url": "https://nexus.example"})), + ), + ( + kind::KEY, + "deploy", + json(json!({"private": "x", "public": "ssh-ed25519 AAAA"})), + ), + ( + kind::KEY, + "deploy.pending", + json(json!({"private": "y", "public": "ssh-ed25519 BBBB"})), + ), + (kind::SECRET, "github", b"ghp".to_vec()), + (kind::SECRET, "api_token", b"shared".to_vec()), + (kind::SECRET, "registry/nexus", b"pw".to_vec()), + (kind::SECRET, "sudo/studio", b"sudo".to_vec()), + ( + kind::SECRET, + "provisioner/lab/server_cert", + b"lab-cert".to_vec(), + ), + (kind::SECRET, "registry/gone", b"orphan".to_vec()), + ( + kind::RESOURCE, + "r-k3x9p2qa", + json(json!({"id": "r-k3x9p2qa", "provisioner": "lab", + "kind": "machine", "name": "studio", "host": "studio", "token": "t"})), + ), + (kind::SPEC, "r-k3x9p2qa", b"spec".to_vec()), + ]; + for (kind, name, value) in rows { + store.write(kind, OWN, name, &value).unwrap(); + } store - .add_job(&job("j1", "provisioner:vm", Some("t"))) + .add_job(&Job { + id: "j1".into(), + host: "studio, dock".into(), + what: "settings".into(), + started: rfc3339(unix_now()), + output_path: "/tmp/j1.log".into(), + subject: "provisioner:lab".into(), + action: "settings".into(), + ..Job::default() + }) .unwrap(); - // A provisioner of the new name that was removed left a job. store - .add_job(&job("j2", "provisioner:lab", Some("t"))) + .take_in("studio", &message(1, "a", Keep::Log)) .unwrap(); +} + +fn a_store_of_names_takes_ids_once(place: &Place) { + let store = place.open().with_keys(Some(keys()), None); + store_of_names(&store); + // The plugin's provisioner takes api_token, which lab had from those all share. + let dir = tempfile::tempdir().unwrap(); + std::fs::create_dir(dir.path().join("fake")).unwrap(); + std::fs::write( + dir.path().join("fake/package.json"), + r#"{"name": "berth-plugin-fake", "berth": {"provisioners": [ + {"id": "vm", "label": "Fake VM", "command": "bin/provision", "protocol": 2, + "secrets": ["api_token", "server_cert"], + "kinds": [{"id": "machine", "setup": "apply"}]}]}}"#, + ) + .unwrap(); + std::fs::create_dir(dir.path().join("fake/bin")).unwrap(); + std::fs::write(dir.path().join("fake/bin/provision"), "").unwrap(); + let plugins = crate::plugins::load(&[dir.path().to_owned()]); + assert_eq!(plugins.len(), 1); + let taken = crate::import::take_in_ids(&store, &plugins).unwrap(); + assert_eq!(taken.copied, ["lab: api_token"]); + assert_eq!(taken.dropped, ["api_token"]); + let id = |kind: &str, name: &str| store.id_of(kind, OWN, name).unwrap().unwrap(); + let (studio, dock, lab) = ( + id(kind::PROFILE, "studio"), + id(kind::PROFILE, "dock"), + id(kind::PROVISIONER_ENTRY, "lab"), + ); + let (nexus, github, deploy) = ( + id(kind::REGISTRY, "nexus"), + id(kind::SECRET, "github"), + id(kind::KEY, "deploy"), + ); + let read = |kind: &str, name: &str| store.read(kind, OWN, name).unwrap(); + let json = |kind: &str, name: &str| store.read_json(kind, OWN, name).unwrap().unwrap(); + let profile = json(kind::PROFILE, &studio); + assert_eq!(profile["registries"], json!([nexus])); + assert_eq!(profile["secrets"][0]["name"], github.as_str()); + assert_eq!(profile["access"]["auth"], format!("key:{deploy}")); + assert_eq!(json(kind::PROVISIONER_ENTRY, &lab)["node"], dock.as_str()); + assert_eq!(json(kind::CREDENTIALS, &studio)["ops"], "made-up"); + // Owned secrets are under their owners; the one all shared went once lab had a copy. + assert_eq!( + read(kind::SECRET, &format!("{nexus}/login")).unwrap(), + b"pw" + ); + assert_eq!( + read(kind::SECRET, &format!("{studio}/sudo")).unwrap(), + b"sudo" + ); + assert_eq!( + read(kind::SECRET, &format!("{lab}/server_cert")).unwrap(), + b"lab-cert" + ); + assert_eq!( + read(kind::SECRET, &format!("{lab}/api_token")).unwrap(), + b"shared" + ); + assert_eq!(store.id_of(kind::SECRET, OWN, "api_token").unwrap(), None); + assert_eq!( + read(kind::KEY, &format!("{deploy}/pending")).unwrap(), + br#"{"private":"y","public":"ssh-ed25519 BBBB"}"# + ); + let labels: Vec = store + .labels(kind::SECRET, OWN) + .unwrap() + .into_iter() + .map(|found| found.name) + .collect(); + assert_eq!(labels, ["github"]); + // The resource has an id of the new shape and refers by id. + let resources = store.entries(kind::RESOURCE, OWN).unwrap(); + let (resource, value) = &resources[0]; + assert!(ids::is_id(resource)); + let record: Value = serde_json::from_slice(value).unwrap(); + assert_eq!( + (record["provisioner"].as_str(), record["host"].as_str()), + (Some(lab.as_str()), Some(studio.as_str())) + ); + assert_eq!(read(kind::SPEC, resource).unwrap(), b"spec"); + let job = store.job("j1").unwrap().unwrap(); + assert_eq!(job.subject, format!("provisioner:{lab}")); + assert_eq!(job.host, format!("{studio}, {dock}")); + assert_eq!(all(&store)[0].host, studio); + // Nothing is left under a name, and every row of a thing has its label. + let left: Vec<_> = all_names(&store) + .into_iter() + .filter(|(kind, _, name)| { + kind != "meta" + && ["studio", "dock", "lab", "nexus", "github", "deploy"].contains(&name.as_str()) + }) + .collect(); + assert!(left.is_empty(), "{left:?}"); + + // Again, with its marker gone, it changes nothing. + let before = store.export(Table::State).unwrap(); store - .add_job(&job("j3", "provisioner:other", None)) + .forget(kind::META, OWN, crate::import::IDS_MARKER) .unwrap(); - store - .rename_subject( - "provisioner:vm", - "provisioner:lab", - &[("provisioner-entry", OWN, "lab", b"e".to_vec())], - &[("provisioner-entry", OWN, "vm")], + let again = crate::import::take_in_ids(&store, &plugins).unwrap(); + assert_eq!((again.given, again.copied.len()), (0, 0)); + let Rows::State(mut after) = store.export(Table::State).unwrap() else { + panic!() + }; + let Rows::State(mut before) = before else { + panic!() + }; + for rows in [&mut before, &mut after] { + rows.retain(|row| row.kind != "meta"); + for row in rows.iter_mut() { + row.updated.clear(); + } + } + assert_eq!(after, before); +} + +fn renames_keep_what_belongs_and_refers(place: &Place) { + let home = tempfile::tempdir().unwrap(); + let paths = crate::paths::Paths { + config_dir: home.path().join("config"), + data_dir: home.path().join("data"), + }; + let store_setting = match place.target() { + Target::Sqlite(file) => file.to_string_lossy().into_owned(), + Target::Postgres(url) => url, + }; + paths.set("store", &store_setting).unwrap(); + let store = shared(&paths).unwrap(); + let secrets = crate::registry::Store::own(store.clone()); + // A registry with its login, and a host that uses it and a secret. + let mut registries = serde_json::Map::new(); + registries.insert( + "nexus".into(), + json!({"type": "nexus", "url": "https://nexus.example"}), + ); + secrets.save_registries(registries).unwrap(); + secrets.set_secret("registry/nexus", Some("pw")).unwrap(); + secrets.set_secret("github", Some("ghp")).unwrap(); + let studio = crate::profiles::Place::own(&paths, "studio").unwrap(); + studio + .save( + &paths, + &json!({"access": {"address": "192.0.2.10"}, "registries": ["nexus"], + "secrets": [{"name": "github", "env": "GH_TOKEN"}]}), ) .unwrap(); + // The registry and the secret go by other names; the host goes on naming them. + let nexus = secrets.find_registry("nexus").unwrap().unwrap().id; + assert!(secrets.rename_registry("nexus", "mirror").unwrap()); + secrets.rename_secret("github", "gh").unwrap(); + let profile = studio.load(&paths).unwrap().unwrap(); + assert_eq!(profile["registries"], json!(["mirror"])); + assert_eq!(profile["secrets"][0]["name"], "gh"); assert_eq!( - all_names(&store), - owned(&[("provisioner-entry", "", "lab")]) + secrets.secret("registry/mirror").unwrap().as_deref(), + Some("pw") ); assert_eq!( - store - .read("provisioner-entry", OWN, "lab") + secrets + .secret(&format!("registry/reg_{nexus}")) .unwrap() - .unwrap(), - b"e" + .as_deref(), + Some("pw") ); - let subject = |id: &str| store.job(id).unwrap().unwrap().subject; - assert_eq!( - (subject("j1"), subject("j2"), subject("j3")), - ( - "provisioner:lab".to_owned(), - "provisioner:lab".to_owned(), - "provisioner:other".to_owned() + assert_eq!(secrets.secret("gh").unwrap().as_deref(), Some("ghp")); + let payload = secrets.host_payload(&profile).unwrap(); + assert_eq!(payload["env"]["GH_TOKEN"], "ghp"); + // A secret named in a profile before it is set is taken by its id once it is. + studio + .save( + &paths, + &json!({"secrets": [{"name": "later", "env": "LATER"}]}), ) + .unwrap(); + secrets.set_secret("later", Some("now")).unwrap(); + secrets.rename_secret("later", "sooner").unwrap(); + assert_eq!( + studio.load(&paths).unwrap().unwrap()["secrets"][0]["name"], + "sooner" ); - let found = store - .job_by_token("provisioner:lab", "settings", "t") + // A secret that is removed is named by the profiles again. + secrets.set_secret("sooner", None).unwrap(); + assert_eq!( + studio.load(&paths).unwrap().unwrap()["secrets"][0]["name"], + "sooner" + ); + // Names that are taken or in the shape of an id are refused. + assert!(secrets.rename_secret("gh", STUDIO).is_err()); + secrets.set_secret("other", Some("x")).unwrap(); + assert_eq!( + secrets + .rename_secret("gh", "other") + .unwrap_err() + .to_string(), + "There is a secret other already" + ); + + // A provisioner of a node, with its secret and a resource. + crate::profiles::Place::own(&paths, "dock") + .unwrap() + .save( + &paths, + &json!({"type": "node", "access": {"address": "192.0.2.20"}}), + ) .unwrap(); - assert_eq!(found.map(|job| job.id), Some("j1".into())); + let dock = crate::profiles::Place::own(&paths, "dock").unwrap(); + let entry = crate::provisioner::Entry { + kind: "fake/vm".into(), + node: dock.key(), + ..Default::default() + }; + let lab = crate::provisioner::save_entry(&paths, "lab", &entry).unwrap(); + secrets + .set_secret("provisioner/lab/server_cert", Some("cert")) + .unwrap(); + crate::resource::Kept { + scope: OWN.into(), + record: crate::resource::Record { + id: new_id(), + provisioner: lab.clone(), + kind: "deployment".into(), + name: "web".into(), + ..Default::default() + }, + } + .save(&paths) + .unwrap(); + crate::provisioner::rename(&paths, &secrets, "lab", "big").unwrap(); + assert_eq!( + crate::provisioner::id_of(&paths, "big").unwrap().as_deref(), + Some(lab.as_str()) + ); + assert_eq!( + secrets + .secret("provisioner/big/server_cert") + .unwrap() + .as_deref(), + Some("cert") + ); + assert_eq!( + crate::provisioner::entry(&paths, &format!("prov_{lab}")) + .unwrap() + .unwrap() + .node, + dock.key() + ); + let held = crate::provisioner::remove_entry(&paths, &secrets, "big").unwrap_err(); + assert!(held.to_string().contains("still holds"), "{held}"); + // A host renamed keeps being the node. + let pier = crate::profiles::rename(&paths, &dock, "pier", false).unwrap(); + assert_eq!(crate::provisioner::node_of(&paths, "big"), "pier"); + assert_eq!(pier.key(), dock.key()); + + // Removed, the provisioner takes what belongs to it along. + for kept in crate::resource::list(&paths).unwrap() { + kept.forget(&paths).unwrap(); + } + crate::provisioner::remove_entry(&paths, &secrets, "big").unwrap(); + assert!( + !all_names(&store) + .iter() + .any(|(_, _, name)| name.starts_with(&lab)) + ); + // So does a registry, with its login. + secrets.save_registries(serde_json::Map::new()).unwrap(); + assert!( + !all_names(&store) + .iter() + .any(|(_, _, name)| name.starts_with(&nexus)) + ); + assert_eq!( + studio.load(&paths).unwrap().unwrap()["registries"], + Value::Null + ); } fn sealed_values_round_trip(place: &Place) { @@ -1380,11 +1867,19 @@ fn the_old_files_go_into_an_empty_store(place: &Place) { }; let store = place.open(); crate::import::run(&paths, &installation, &store).unwrap(); + // Each has an id of its own, and its name as its label. + let github = store.id_of(kind::SECRET, OWN, "github").unwrap().unwrap(); assert_eq!( - store.read(kind::SECRET, OWN, "github").unwrap().unwrap(), + store.read(kind::SECRET, OWN, &github).unwrap().unwrap(), b"ghp-made-up" ); - assert_eq!(store.names(kind::REGISTRY, OWN).unwrap(), ["hub"]); + let registries: Vec = store + .labels(kind::REGISTRY, OWN) + .unwrap() + .into_iter() + .map(|found| found.name) + .collect(); + assert_eq!(registries, ["hub"]); assert_eq!( store .read(kind::SETTING, OWN, "default_host") diff --git a/crates/berth-core/src/tailnet.rs b/crates/berth-core/src/tailnet.rs index a21db81..56b35bb 100644 --- a/crates/berth-core/src/tailnet.rs +++ b/crates/berth-core/src/tailnet.rs @@ -1412,7 +1412,7 @@ mod tests { paths.set("tailnet_user", "5").unwrap(); set_secret(&paths, Some("hskey-api-x")); let config = json!({"hostname": "studio"}); - let stored = crate::profiles::Place::own("studio").unwrap(); + let stored = crate::profiles::Place::own(&paths, "studio").unwrap(); let file = crate::profiles::Place::File(home.path().join("attic.json")); let mut keys = vec![]; for place in [&stored, &file] { @@ -1450,7 +1450,7 @@ mod tests { /// The host `studio`, saved with this profile. fn studio(paths: &Paths, config: Value) -> crate::host::Host { - let place = Place::own("studio").unwrap(); + let place = Place::own(paths, "studio").unwrap(); place.save(paths, &config).unwrap(); crate::host::Host { name: "studio".into(), @@ -1675,7 +1675,6 @@ mod tests { 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. diff --git a/crates/berth-core/tests/fixtures/store-0.6.0-rc.4.db b/crates/berth-core/tests/fixtures/store-0.6.0-rc.4.db new file mode 100644 index 0000000..60024b4 Binary files /dev/null and b/crates/berth-core/tests/fixtures/store-0.6.0-rc.4.db differ diff --git a/crates/berth-core/tests/sshd.rs b/crates/berth-core/tests/sshd.rs index 5a3ae47..1ed3cc6 100644 --- a/crates/berth-core/tests/sshd.rs +++ b/crates/berth-core/tests/sshd.rs @@ -119,7 +119,7 @@ fn host(paths: &Paths, name: &str, port: u16) -> Host { .unwrap() .trim() .to_owned(); - let place = Place::own(name).unwrap(); + let place = Place::own(paths, name).unwrap(); let config = json!({"access": {"address": "127.0.0.1", "port": port.to_string(), "user": user}}); place.save(paths, &config).unwrap(); diff --git a/crates/berth-core/tests/upgrade.rs b/crates/berth-core/tests/upgrade.rs index feebfc5..f62235d 100644 --- a/crates/berth-core/tests/upgrade.rs +++ b/crates/berth-core/tests/upgrade.rs @@ -1,14 +1,24 @@ -//! A store that berth 0.5.2 wrote, with made-up hosts and fake keys, opened by -//! this berth: it is copied as it was, converted once, and the next start -//! changes nothing. +//! Stores that berth 0.5.2 and 0.6.0-rc.4 wrote, with made-up hosts and fake +//! keys, opened by this berth: each is copied as it was, converted once, and +//! the next start changes nothing. The store of 0.6.0-rc.4 was made with that +//! berth's commands, and the rows its commands make only with a machine or a +//! server, such as resources, jobs and messages, were added in its layout. use berth_core::paths::Paths; use berth_core::store::{self, kind}; use std::os::unix::fs::PermissionsExt; const FIXTURE: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/fixtures/store-0.5.2.db"); +const FIXTURE_RC4: &str = concat!( + env!("CARGO_MANIFEST_DIR"), + "/tests/fixtures/store-0.6.0-rc.4.db" +); fn installation() -> (tempfile::TempDir, Paths) { + installation_of(FIXTURE) +} + +fn installation_of(fixture: &str) -> (tempfile::TempDir, Paths) { let dir = tempfile::tempdir().unwrap(); let paths = Paths { config_dir: dir.path().join("config"), @@ -16,7 +26,7 @@ fn installation() -> (tempfile::TempDir, Paths) { }; std::fs::create_dir_all(&paths.data_dir).unwrap(); let file = paths.data_dir.join("berth.db"); - std::fs::copy(FIXTURE, &file).unwrap(); + std::fs::copy(fixture, &file).unwrap(); std::fs::set_permissions(&file, std::fs::Permissions::from_mode(0o600)).unwrap(); (dir, paths) } @@ -59,28 +69,30 @@ fn a_store_of_berth_0_5_2_is_copied_then_converted_once() { // The store itself is converted: names of now, no pasted key, every marker. assert!(store.schema_version().unwrap() > 4); - let studio = store - .read_json(kind::PROFILE, store::OWN, "studio") - .unwrap() - .unwrap(); + // Hosts are kept by their ids, with their names beside. + let profile = |name: &str| { + let id = store + .id_of(kind::PROFILE, store::OWN, name) + .unwrap() + .unwrap(); + store + .read_json(kind::PROFILE, store::OWN, &id) + .unwrap() + .unwrap() + }; + let studio = profile("studio"); assert_eq!( berth_api::profile::host_type(&studio), berth_api::HostType::Workstation ); assert!(studio.get("level").is_none() && studio.get("tailscale_auth_key").is_none()); - let pier = store - .read_json(kind::PROFILE, store::OWN, "pier") - .unwrap() - .unwrap(); + let pier = profile("pier"); assert_eq!( berth_api::profile::host_type(&pier), berth_api::HostType::Managed ); // dock had berth 0.5 make its key, so it joins as before; pier never joined. - let dock = store - .read_json(kind::PROFILE, store::OWN, "dock") - .unwrap() - .unwrap(); + let dock = profile("dock"); assert!( dock["ansible_vars"] .get("devbox_tailnet_optional") @@ -92,8 +104,7 @@ fn a_store_of_berth_0_5_2_is_copied_then_converted_once() { for marker in berth_core::import::MARKERS { assert!(names.iter().any(|name| name == marker), "{marker}"); } - let secrets = store.names(kind::SECRET, store::OWN).unwrap(); - assert!(secrets.iter().any(|name| name == "tailnet_api_key")); + ids_are_given(&paths, &store); let converted = rows(&store); drop(store); @@ -110,6 +121,82 @@ fn a_store_of_berth_0_5_2_is_copied_then_converted_once() { assert!(left.is_empty(), "{left:?}"); } +/// Everything kept under a name has an id, and what refers to it or +/// belongs to it uses the id. +fn ids_are_given(paths: &Paths, store: &store::Store) { + use berth_core::profiles::Place; + let id = |kind: &str, scope: &str, name: &str| { + let id = store.id_of(kind, scope, name).unwrap().unwrap(); + assert!(berth_api::ids::is_id(&id), "{kind} {name}: {id}"); + id + }; + for host in ["studio", "dock", "pier"] { + id(kind::PROFILE, store::OWN, host); + } + for host in ["harbor", "wharf"] { + id(kind::PROFILE, "served:http://127.0.0.1:17777", host); + } + let nexus = id(kind::REGISTRY, store::OWN, "nexus"); + id(kind::REGISTRY, store::OWN, "hub"); + let npm = id(kind::SECRET, store::OWN, "npm_token"); + let deploy = id(kind::KEY, store::OWN, "deploy"); + id(kind::KEY, store::OWN, "berth"); + id(kind::SECRET, store::OWN, "tailnet_api_key"); + // The store refers to the registry, the secrets and the key by id; a + // profile read says their names. + let studio = id(kind::PROFILE, store::OWN, "studio"); + let kept = store + .read_json(kind::PROFILE, store::OWN, &studio) + .unwrap() + .unwrap(); + assert_eq!(kept["registries"], serde_json::json!([nexus])); + assert_eq!(kept["secrets"][0]["name"], npm.as_str()); + let read = Place::own(paths, "studio") + .unwrap() + .load(paths) + .unwrap() + .unwrap(); + assert_eq!(read["registries"], serde_json::json!(["nexus"])); + assert_eq!(read["secrets"][0]["name"], "npm_token"); + let dock = id(kind::PROFILE, store::OWN, "dock"); + let kept = store + .read_json(kind::PROFILE, store::OWN, &dock) + .unwrap() + .unwrap(); + assert_eq!(kept["access"]["auth"], format!("key:{deploy}")); + let read = Place::own(paths, "dock") + .unwrap() + .load(paths) + .unwrap() + .unwrap(); + assert_eq!(read["access"]["auth"], "key:deploy"); + // The registry's login belongs to it. + let secrets = berth_core::registry::Store::new(paths).unwrap(); + assert_eq!( + store + .read(kind::SECRET, store::OWN, &format!("{nexus}/login")) + .unwrap() + .is_some(), + secrets.has_secret("registry/nexus").unwrap() + ); + assert!(secrets.has_secret("registry/nexus").unwrap()); + // Owned secrets are not among the global ones. + assert!( + secrets + .secret_names() + .iter() + .all(|name| !name.contains('/')) + ); + // Jobs are about the hosts by id. + let jobs = store.jobs(10).unwrap(); + assert!( + jobs.iter() + .any(|job| job.subject == format!("host:{studio}") && job.host == studio), + "{jobs:?}" + ); + assert!(jobs.iter().all(|job| !job.subject.ends_with(":studio"))); +} + #[test] fn only_the_latest_copy_is_kept() { let (_dir, paths) = installation(); @@ -136,3 +223,154 @@ fn only_the_latest_copy_is_kept() { None ); } + +#[test] +fn a_store_of_berth_0_6_0_rc_4_is_converted_to_ids_once() { + use berth_core::{profiles::Place, provisioner, registry, resource}; + let (_dir, paths) = installation_of(FIXTURE_RC4); + // The plugins of this source, which say what each provisioner and feature takes. + paths + .set("source", concat!(env!("CARGO_MANIFEST_DIR"), "/../..")) + .unwrap(); + let file = paths.data_dir.join("berth.db"); + assert_eq!(schema(&file), 6); + let store = store::open_converting(&paths).unwrap(); + assert!( + paths + .data_dir + .join("berth.db.berth-0.6.0-rc.4-schema-6") + .is_file() + ); + let host = |name: &str| Place::find(&paths, store::OWN, name).unwrap().unwrap(); + let (studio, dock) = (host("studio"), host("dock")); + assert!(berth_api::ids::is_id(&studio.key()) && berth_api::ids::is_id(&dock.key())); + let lab = provisioner::entry(&paths, "lab").unwrap().unwrap(); + let node = provisioner::entry(&paths, "dock-incus-vm") + .unwrap() + .unwrap(); + assert!(berth_api::ids::is_id(&lab.id)); + // A node's provisioner refers to the node by id. + assert_eq!(node.node, dock.key()); + assert_eq!(node.feature, "incus/server"); + + // Each provisioner keeps its own server certificate, and has its own copy + // of the client certificate it took from those all share. + let secrets = registry::Store::new(&paths).unwrap(); + let secret = |name: &str| secrets.secret(name).unwrap(); + let client = secret("incus_client_cert").unwrap(); + for provisioner in ["lab", "dock-incus-vm"] { + assert_eq!( + secret(&format!("provisioner/{provisioner}/incus_client_cert")).as_deref(), + Some(client.as_str()) + ); + assert!(secret(&format!("provisioner/{provisioner}/incus_client_key")).is_some()); + } + assert!( + secret("provisioner/lab/incus_server_cert") + .unwrap() + .contains("made-up-lab") + ); + assert!( + secret("provisioner/dock-incus-vm/incus_server_cert") + .unwrap() + .contains("made-up-dock") + ); + // The client certificate stays as well: the feature of the node trusts it. + assert_eq!( + secrets.secret_names(), + [ + "github_token", + "incus_client_cert", + "incus_client_key", + "incus_server_cert", + "tailnet_api_key" + ] + ); + // What belongs to a host or a registry is kept by its id. + assert_eq!(secret("sudo/studio").as_deref(), Some("sudo-pw-made-up")); + assert_eq!( + store + .read(kind::SECRET, store::OWN, &format!("{}/sudo", studio.key())) + .unwrap() + .as_deref(), + Some(&b"sudo-pw-made-up"[..]) + ); + assert_eq!( + secret("registry/nexus").as_deref(), + Some("nexus-pw-made-up") + ); + // A profile in a file keeps its password by its path. + assert_eq!( + secret("ssh//srv/made-up/attic.json").as_deref(), + Some("attic-pw-made-up") + ); + assert_eq!( + berth_core::credentials::load(&paths, &studio).unwrap()["ops"], + "made-up-ops-pw" + ); + let known = berth_core::keys::known::Record::load(&store, &studio).unwrap(); + assert_eq!(known.host_keys, ["ssh-ed25519 AAAAmadeuphostkey"]); + let profile = studio.load(&paths).unwrap().unwrap(); + assert_eq!(profile["registries"], serde_json::json!(["nexus"])); + assert_eq!(profile["secrets"][0]["name"], "github_token"); + + // Resources have ids of the new shape, and refer to their provisioner and host by id. + let machine = resource::of_place(&paths, &studio).unwrap().unwrap(); + assert!(resource::valid_id(&machine.record.id)); + assert_eq!(machine.record.provisioner, lab.id); + assert_eq!(machine.record.external_id, "berth-studio"); + let web = resource::list(&paths) + .unwrap() + .into_iter() + .find(|kept| kept.record.name == "web") + .unwrap(); + assert_eq!(web.record.provisioner, node.id); + assert_eq!(web.spec(&paths).unwrap().as_deref(), Some("services: {}")); + + // Jobs and messages are about them by id. + let job = |id: &str| store.job(id).unwrap().unwrap(); + assert_eq!( + job("jstudio000001").subject, + format!("host:{}", studio.key()) + ); + assert_eq!( + job("jlab000000002").subject, + format!("provisioner:{}", lab.id) + ); + assert_eq!( + job("jres000000003").subject, + format!("resource:{}", machine.record.id) + ); + assert_eq!( + job("jroll00000004").host, + format!("{}, {}", studio.key(), dock.key()) + ); + let (messages, _) = store + .inbox(&store::Filter { + limit: 10, + ..Default::default() + }) + .unwrap(); + assert_eq!(messages[0].host, studio.key()); + assert_eq!(store.cursor(&studio.key()).unwrap(), 1); + let converted = rows(&store); + drop(store); + + // Opened again, nothing changes. + let store = store::open_converting(&paths).unwrap(); + assert_eq!(rows(&store), converted); + + // A rename changes the name alone. + provisioner::rename(&paths, &secrets, "lab", "lab2").unwrap(); + assert_eq!( + secret("provisioner/lab2/incus_server_cert"), + secret(&format!("provisioner/{}/incus_server_cert", lab.id)) + ); + assert!(secret("provisioner/lab2/incus_server_cert").is_some()); + let machine = resource::of_place(&paths, &studio).unwrap().unwrap(); + assert_eq!(machine.record.provisioner, lab.id); + assert_eq!( + provisioner::label(&paths, &machine.record.provisioner), + "lab2" + ); +} diff --git a/crates/berth-tui/src/flow.rs b/crates/berth-tui/src/flow.rs index e6d0c40..707d367 100644 --- a/crates/berth-tui/src/flow.rs +++ b/crates/berth-tui/src/flow.rs @@ -586,7 +586,7 @@ impl Flow { .catalogue .provisioners .iter() - .any(|known| &known.id == name) + .any(|known| known.named() == name || &known.id == name) { return Err(Stop::Refused(format!( "No provisioner {name}. berth provisioner list names them" diff --git a/crates/berth-tui/src/machine.rs b/crates/berth-tui/src/machine.rs index 585e185..170b278 100644 --- a/crates/berth-tui/src/machine.rs +++ b/crates/berth-tui/src/machine.rs @@ -182,7 +182,7 @@ impl Machine { /// The provisioner chosen to create the machine with. pub fn provisioner(&self) -> Option<&ProvisionerInfo> { let chosen = self.chosen.as_ref()?; - self.all().iter().find(|known| &known.id == chosen) + self.all().iter().find(|known| known.named() == chosen) } /// The machine the host has, once that is known. @@ -199,7 +199,7 @@ impl Machine { let Some(provisioner) = self.all().get(index) else { return; }; - if self.chosen.as_ref() == Some(&provisioner.id) { + if self.chosen.as_ref() == Some(&provisioner.named().to_owned()) { return; } let values = provisioner @@ -213,7 +213,7 @@ impl Machine { (argument.name.clone(), value) }) .collect(); - self.chosen = Some(provisioner.id.clone()); + self.chosen = Some(provisioner.named().to_owned()); self.values = values; } @@ -224,11 +224,11 @@ impl Machine { .iter() .map(|known| { // Two provisioners of one type differ by their names alone. - let label = match known.kind.is_empty() || known.kind == known.id { + let label = match known.kind.is_empty() || known.kind == known.named() { true => known.label.clone(), - false => format!("{} · {}", known.id, known.label), + false => format!("{} · {}", known.named(), known.label), }; - (label, known.id.clone()) + (label, known.named().to_owned()) }) .collect() } @@ -260,7 +260,7 @@ impl Machine { /// Choose the provisioner with this name. pub fn choose_id(&mut self, id: &str) { - if let Some(index) = self.all().iter().position(|known| known.id == id) { + if let Some(index) = self.all().iter().position(|known| known.named() == id) { self.choose(index); } } @@ -275,7 +275,7 @@ impl Machine { Some(chosen) => self .all() .iter() - .position(|known| &known.id == chosen) + .position(|known| known.named() == chosen) .map_or(0, |index| (index + 1) % count), None => 0, }; @@ -373,7 +373,7 @@ impl Machine { }) .collect(); Ok(NewResource { - provisioner: provisioner.id.clone(), + provisioner: provisioner.named().to_owned(), kind: "machine".into(), arguments, ..NewResource::default() diff --git a/crates/berth-tui/src/provisioners.rs b/crates/berth-tui/src/provisioners.rs index 4f234d7..5aaec69 100644 --- a/crates/berth-tui/src/provisioners.rs +++ b/crates/berth-tui/src/provisioners.rs @@ -251,7 +251,7 @@ pub fn rows(provisioners: &[ProvisionerInfo]) -> Vec> { let (said, colour) = usable(provisioner); Line::from(vec![ Span::styled( - format!("{:<20}", provisioner.id), + format!("{:<20}", provisioner.named()), Style::new().fg(theme::TEXT), ), Span::styled(format!("{:<28}", provisioner.label), muted), @@ -359,7 +359,7 @@ pub fn help(provisioner: Option<&ProvisionerInfo>, place: &str) -> String { } text + &format!( "Enter opens its page, which asks it how it is.\n\nThe provisioners of {place}. From the command line: `berth provisioner show {}`", - provisioner.id + provisioner.named() ) } @@ -499,7 +499,7 @@ impl Page { false => format!("{mark} {} · {}", state.state, state.message), }; let mut lines = vec![ - (format!("{} · {}", info.label, info.id), Tone::Plain), + (format!("{} · {}", info.label, info.named()), Tone::Plain), (said, tone), ]; // The settings row is one of them, in the same columns. @@ -1401,7 +1401,7 @@ impl Page { self.state = Some(match (state, &actions) { (Ok(state), Err(error)) => Err(format!( "{} answered, but not with its actions: {error}", - state.provisioner.id + state.provisioner.named() )), (state, _) => state, }); @@ -1452,7 +1452,7 @@ impl Tab { let makers: Vec<(String, String)> = list .iter() .filter(|provisioner| provisioner.kinds.iter().any(|known| known.id == kind)) - .map(|provisioner| (provisioner.id.clone(), provisioner.label.clone())) + .map(|provisioner| (provisioner.named().to_owned(), provisioner.label.clone())) .collect(); let Some((first, _)) = makers.first().cloned() else { return Asked::Nothing(Some(format!( @@ -1887,7 +1887,7 @@ impl Tab { } Code::Enter => { if let Some(provisioner) = self.selected(list) { - let id = provisioner.id.clone(); + let id = provisioner.named().to_owned(); self.page = Some(Page::new(&id)); return Asked::Open(id); } diff --git a/crates/berth/Cargo.toml b/crates/berth/Cargo.toml index 80cd42f..d0edbbe 100644 --- a/crates/berth/Cargo.toml +++ b/crates/berth/Cargo.toml @@ -27,6 +27,7 @@ futures-util.workspace = true # set up from. [target.'cfg(target_os = "linux")'.dependencies] berth-core.workspace = true +tower = { version = "0.5", features = ["util"] } # For its API document, which berth serve's includes. berthd = { path = "../berthd" } axum.workspace = true diff --git a/crates/berth/src/cli/args.rs b/crates/berth/src/cli/args.rs index d915477..c9407c0 100644 --- a/crates/berth/src/cli/args.rs +++ b/crates/berth/src/cli/args.rs @@ -740,6 +740,13 @@ pub enum Secret { #[arg(long, value_name = "FILE")] value_file: Option, }, + /// Give a secret another name. The profiles that hand it to hosts go on doing so. + #[command(visible_alias = "mv")] + Rename { + /// The secret, by its name or its id. + name: String, + new: String, + }, /// Remove a secret. #[command(visible_alias = "rm")] Remove { name: String }, @@ -781,6 +788,13 @@ pub enum Registry { }, /// Whether a registry answers and takes its login. Check { name: Option }, + /// Give a registry another name. Its login and the hosts that use it stay with it. + #[command(visible_alias = "mv")] + Rename { + /// The registry, by its name or its id. + name: String, + new: String, + }, /// Remove a registry with its login. #[command(visible_alias = "rm")] Remove { name: String }, diff --git a/crates/berth/src/cli/pages.rs b/crates/berth/src/cli/pages.rs index 07462c7..b3d592b 100644 --- a/crates/berth/src/cli/pages.rs +++ b/crates/berth/src/cli/pages.rs @@ -122,7 +122,10 @@ pub fn provisioner( given: &BTreeMap, ) -> String { let info = &state.provisioner; - let mut text = format!("{} {}\n", info.id, info.label); + let mut text = format!("{} {}\n", info.named(), info.label); + if info.id != info.named() { + text += &format!("id {}\n", info.id); + } if !info.description.is_empty() { text += &format!("{}\n", info.description); } @@ -172,7 +175,7 @@ pub fn provisioner( if info.own.iter().any(|name| given.contains_key(name)) { text += &format!( " To keep them: berth provisioner set {} NAME=VALUE\n", - info.id + info.named() ); } } @@ -241,7 +244,10 @@ pub fn provisioner( } } if !groups.is_empty() { - text += &format!("\nActions (berth provisioner run {} ACTION)\n", info.id); + text += &format!( + "\nActions (berth provisioner run {} ACTION)\n", + info.named() + ); } for (title, list) in groups { text += &format!(" {title}\n"); diff --git a/crates/berth/src/cli/setup.rs b/crates/berth/src/cli/setup.rs index e8cdf2c..b02259c 100644 --- a/crates/berth/src/cli/setup.rs +++ b/crates/berth/src/cli/setup.rs @@ -521,6 +521,15 @@ pub fn secret(client: &Client, command: Secret) -> Result<()> { out::done(format!("Set the secret {name}")); Ok(()) } + Secret::Rename { name, new } => { + client.call( + "secrets.rename", + json!({"name": name}), + Some(json!({"name": new})), + )?; + out::done(format!("{name} is called {new} now")); + Ok(()) + } Secret::Remove { name } => { ask::confirm(&format!("This removes the secret {name}."))?; client.call("secrets.remove", json!({"name": name}), None)?; @@ -677,6 +686,15 @@ pub fn registry(global: &Global, command: Registry) -> Result<()> { false => Ok(()), } } + Registry::Rename { name, new } => { + client.call( + "registries.rename", + json!({"name": name}), + Some(json!({"name": new})), + )?; + out::done(format!("{name} is called {new} now, with its login")); + Ok(()) + } Registry::Remove { name } => { ask::confirm(&format!("This removes the registry {name} with its login."))?; client.call("registries.remove", json!({"name": name}), None)?; diff --git a/crates/berth/src/cli/things.rs b/crates/berth/src/cli/things.rs index 3f53200..a417a6d 100644 --- a/crates/berth/src/cli/things.rs +++ b/crates/berth/src/cli/things.rs @@ -628,7 +628,7 @@ pub fn provisioner(client: &Client, command: Provisioner) -> Result<()> { problem => styled(&format!("no: {problem}"), Style::Dim), }; table.row(vec![ - text("id"), + text("name"), text("type"), text("node"), usable, diff --git a/crates/berth/src/local/serve/actions.rs b/crates/berth/src/local/serve/actions.rs index 4001f9b..58cd7b4 100644 --- a/crates/berth/src/local/serve/actions.rs +++ b/crates/berth/src/local/serve/actions.rs @@ -25,7 +25,7 @@ use berth_core::{ apply, host::Host, host_type::{self, Capability, HostType, Powered}, - keys::{self, Once, contact, ring}, + keys::{Once, contact, ring}, profiles::Place, registry::Store, rollout, @@ -323,7 +323,7 @@ fn profile_of(state: &Serve, name: &str) -> Value { /// The machine a provisioner made for the saved host `name`, or one planned /// for it, as berth's record has it. pub(super) fn machine_of(state: &Serve, name: &str) -> Option { - let place = Place::own(name).ok()?; + let place = Place::own(&state.paths, name).ok()?; let kept = berth_core::resource::of_place(&state.paths, &place).ok()??; let plugins = berth_core::provision::list_plugins(&state.paths); Some(berth_core::resource::info( @@ -371,7 +371,7 @@ fn no_action(subject: &str, action: &str, actions: &[Action]) -> Fail { } #[utoipa::path(get, path = "/v1/hosts/{name}/actions", summary = "What can be done to a host: the actions its controller lists, and berth's own.", tag = "hosts", - params(("name" = String, Path, description = "A saved host")), + params(("name" = String, Path, description = "A saved host, by its name or its id (bare or as `host_ID`)")), responses((status = 200, description = "The actions, each saying whether it can run now", body = Vec), (status = 404, description = "No such host", body = ErrorBody)))] pub async fn host_actions( @@ -413,7 +413,7 @@ pub async fn host_actions( } #[utoipa::path(post, path = "/v1/hosts/{name}/actions/{action}", summary = "Run one of a host's actions. One of berth's that changes something is a job; an action of the host's controller is a session there, which a job follows unless it needs a terminal.", tag = "hosts", - params(("name" = String, Path, description = "A saved host"), + params(("name" = String, Path, description = "A saved host, by its name or its id (bare or as `host_ID`)"), ("action" = String, Path, description = "The action, as hosts.actions names it")), request_body(content = Option, description = "Its arguments, and a token that makes asking again safe"), responses((status = 202, description = "Started as a job; an action of its controller also says its session", body = Acted), @@ -706,8 +706,7 @@ fn logs_in_alone(seen: &Seen, action: Action) -> Action { return action; } let wants = profile::Access::of(seen.profile).auth == Auth::Password; - let kept = Place::own(seen.name) - .is_ok_and(|place| seen.secrets.contains(&keys::password_secret(&place))); + let kept = seen.secrets.contains(&format!("ssh/{}", seen.name)); unavailable( action, (wants && !kept).then(|| { @@ -735,8 +734,8 @@ fn install_controller(state: Arc, name: String, act: Act) -> Run { Box::pin(async move { let remote = remote(&state, &name)?; let bad = |error: Box| turned_down(error); - let host = - own_place(&name).and_then(|place| Host::load_at(&state.paths, place).map_err(bad))?; + let host = own_place(&state.paths, &name) + .and_then(|place| Host::load_at(&state.paths, place).map_err(bad))?; let password = act .sudo_password .or_else(|| remote.password.clone()) @@ -774,8 +773,8 @@ fn apply(state: Arc, name: String, act: Act) -> Run { Box::pin(async move { let remote = remote(&state, &name)?; let bad = |error: Box| turned_down(error); - let host = - own_place(&name).and_then(|place| Host::load_at(&state.paths, place).map_err(bad))?; + let host = own_place(&state.paths, &name) + .and_then(|place| Host::load_at(&state.paths, place).map_err(bad))?; let password = act .sudo_password .or_else(|| remote.password.clone()) @@ -1097,7 +1096,7 @@ fn secrets(state: Arc, name: String, act: Act) -> Run { Box::pin(async move { let remote = remote(&state, &name)?; let bad = |error: Box| turned_down(error); - let host = Host::load_at(&state.paths, own_place(&name)?).map_err(bad)?; + let host = Host::load_at(&state.paths, own_place(&state.paths, &name)?).map_err(bad)?; let payload = Store::own(state.store.clone()) .host_payload(&host.config) .map_err(bad)?; @@ -1463,7 +1462,7 @@ fn destroy_refused(name: &str, machine: Option<&Resource>) -> Option { fn remove(state: Arc, name: String, act: Act) -> Run { Box::pin(async move { let destroy = yes(&act, "destroy")?; - let place = own_place(&name)?; + let place = own_place(&state.paths, &name)?; let (_, profile) = saved_profile(&state, &name)?; let machine = machine_of(&state, &name); if destroy && let Some(why) = destroy_refused(&name, machine.as_ref()) { @@ -1504,7 +1503,7 @@ fn remove(state: Arc, name: String, act: Act) -> Run { let paths = &serve.paths; if let Some(acting) = acting { let (provisioner, id) = ( - acting.kept().record.provisioner.clone(), + berth_core::provisioner::label(paths, &acting.kept().record.provisioner), acting.kept().record.external_id.clone(), ); match acting.run(paths, &(serve.provision)(), out) { @@ -1613,8 +1612,8 @@ fn duplicate(state: Arc, name: String, act: Act) -> Run { Box::pin(async move { let new = given(&act, "name").unwrap_or_default().to_owned(); let create = yes(&act, "create")?; - let to = own_place(&new)?; - let from = own_place(&name)?; + let to = own_place(&state.paths, &new)?; + let from = own_place(&state.paths, &name)?; saved_profile(&state, &name)?; if to.load(&state.paths).map_err(internal)?.is_some() { return Err(Fail::new( @@ -1796,7 +1795,7 @@ fn rollout(state: Arc, _: String, act: Act) -> Run { let mut problem = None; let mut skipped = vec![]; for (name, password) in names.iter().zip(passwords) { - let host = berth_core::profiles::Place::own(name) + let host = berth_core::profiles::Place::own(&paths, name) .and_then(|place| Host::load_at(&paths, place)); // Only a node or a workstation is applied; the others are said and left. let host = match host { @@ -1874,7 +1873,7 @@ fn take_known_host_key(state: &Serve, name: &str) { let Some(known_hosts) = state.known_hosts.lock().unwrap().clone() else { return; }; - let Ok(place) = own_place(name) else { + let Ok(place) = own_place(&state.paths, name) else { return; }; let Ok(host) = Host::load_at(&state.paths, place.clone()) else { @@ -1939,7 +1938,7 @@ fn trust_key_action(seen: &Seen) -> Action { fn trust_key(state: Arc, name: String, act: Act) -> Run { Box::pin(async move { let bad = |error: String| Fail::new(code::BAD_REQUEST, error); - let host = Host::load_at(&state.paths, own_place(&name)?) + let host = Host::load_at(&state.paths, own_place(&state.paths, &name)?) .map_err(|error| bad(error.to_string()))?; let keep = yes(&act, "keep_password")?; let password = given(&act, "password").map(str::to_owned); @@ -2013,7 +2012,7 @@ fn host_key_action(seen: &Seen) -> Action { fn host_key(state: Arc, name: String, act: Act) -> Run { Box::pin(async move { let accept = yes(&act, "accept")?; - let host = Host::load_at(&state.paths, own_place(&name)?) + let host = Host::load_at(&state.paths, own_place(&state.paths, &name)?) .map_err(|error| Fail::new(code::BAD_REQUEST, error.to_string()))?; let paths = state.paths.clone(); // It may log in to the host, and waits for that. @@ -2053,11 +2052,11 @@ fn rename(state: Arc, name: String, act: Act) -> Run { let bad = |error: String| Fail::new(code::BAD_REQUEST, error); let new = given(&act, "name").unwrap_or_default().to_owned(); let hostname = yes(&act, "hostname")?; - let to = own_place(&new)?; + let to = own_place(&state.paths, &new)?; if hostname && let Some(problem) = berth_tui::setup::hostname_problem(&new) { return Err(bad(problem)); } - let from = own_place(&name)?; + let from = own_place(&state.paths, &name)?; let joins = profile::joins_tailnet(&profile_of(&state, &name)); if hostname { berth_core::profiles::hostname_allowed(&name, &profile_of(&state, &name)) @@ -2184,7 +2183,7 @@ fn adopt_reach( let address = given(act, "address").unwrap_or(""); let port = given(act, "port").unwrap_or(""); let key = given(act, "key"); - let place = Place::own(name).map_err(|error| error.to_string())?; + let place = Place::own(paths, name).map_err(|error| error.to_string())?; let saved = place.load(paths).map_err(|error| error.to_string())?; let reach = match (&saved, address) { (None, "") => { diff --git a/crates/berth/src/local/serve/api.rs b/crates/berth/src/local/serve/api.rs index d61f666..8abd380 100644 --- a/crates/berth/src/local/serve/api.rs +++ b/crates/berth/src/local/serve/api.rs @@ -21,7 +21,7 @@ use axum::{ }; use berth_api::{ Applied, Behind, Done, ErrorBody, ErrorDetail, HostBehind, HostType, JobInfo, Message, Output, - Power, PowerState, Version, b64, client, code, + Power, PowerState, Version, b64, client, code, ids, ops::{self, Role}, streams, }; @@ -88,6 +88,8 @@ impl From for Fail { let kind = match error { store::Error::Conflict(_) => code::PRECONDITION, store::Error::TooLarge => code::TOO_LARGE, + store::Error::Taken { .. } => code::CONFLICT, + store::Error::Given(_) => code::BAD_REQUEST, store::Error::Database(_) | store::Error::Encrypted { .. } | store::Error::Sealing(_) => code::INTERNAL, @@ -156,6 +158,9 @@ pub(super) fn turned_down(said: impl Into) -> Fail { #[derive(Serialize, ToSchema)] pub struct HostInfo { + /// Its id, which stays when it is renamed; empty for a host that is not saved. + #[serde(default, skip_serializing_if = "String::is_empty")] + pub id: String, pub name: String, /// Its SSH address. pub address: String, @@ -223,7 +228,11 @@ pub(super) struct Known { pub struct InboxMessage { #[serde(flatten)] pub message: Message, + /// The host's name. pub host: String, + /// The host's id, when it is a saved host. + #[serde(skip_serializing_if = "String::is_empty")] + pub host_id: String, pub done: bool, /// The order it was taken in; what `before` counts in. pub seq: i64, @@ -265,16 +274,20 @@ pub(super) fn job_info(paths: &berth_core::paths::Paths, job: store::Job) -> Job let report = berth_core::runner::output_file(paths, &job.output_path) .and_then(|output| std::fs::read(report_path(&output)).ok()) .and_then(|data| serde_json::from_slice(&data).ok()); + let subject_name = berth_core::runner::Subject::parse(&job.subject) + .map_or_else(|| job.subject.clone(), |subject| subject.named(paths).key()); JobInfo { report, id: job.id, subject: job.subject, + subject_name, // Rows from before actions were kept say it as what they did. action: match job.action.is_empty() { true => job.what, false => job.action, }, - host: job.host, + host: berth_core::runner::hosts_named(paths, &job.host), + host_id: job.host, started: job.started, ended: job.ended, exit_code: job.exit_code, @@ -319,12 +332,14 @@ fn guarded_routes() -> OpenApiRouter> { .routes(routes!(actions::act)) .routes(routes!(setup::secrets)) .routes(routes!(setup::set_secret, setup::remove_secret)) + .routes(routes!(setup::rename_secret)) .routes(routes!(keys::list)) .routes(routes!(keys::add, keys::remove)) .routes(routes!(keys::rotate)) .routes(routes!(setup::registries)) .routes(routes!(setup::save_registry, setup::remove_registry)) .routes(routes!(setup::check_registry)) + .routes(routes!(setup::rename_registry)) .routes(routes!(setup::settings)) .routes(routes!(setup::set_setting)) .routes(routes!(setup::plugins, setup::add_plugin)) @@ -407,9 +422,88 @@ pub fn router(state: Arc, guarded: bool) -> Router { response })); } - router + let inner = router .layer(DefaultBodyLimit::max(64 * 1024 * 1024)) - .with_state(state) + .with_state(state.clone()); + // Before the routes are matched, so every handler gets the name. + let paths = state.paths.clone(); + Router::new().fallback_service(tower::service_fn(move |request: Request| { + let (inner, paths) = (inner.clone(), paths.clone()); + async move { + use tower::ServiceExt; + match by_name(&paths, request) { + Ok(request) => inner.oneshot(request).await, + Err(fail) => Ok(fail.into_response()), + } + } + })) +} + +/// The things a path names by id, as the parts of the paths of hosts, +/// provisioners, registries, secrets, keys, resources and the inbox: their +/// kind of row, and the kind of id. +const NAMED: &[(&str, &str, ids::Kind)] = &[ + ("hosts", store::kind::PROFILE, ids::Kind::Host), + ("inbox", store::kind::PROFILE, ids::Kind::Host), + ( + "provisioners", + store::kind::PROVISIONER_ENTRY, + ids::Kind::Provisioner, + ), + ("registries", store::kind::REGISTRY, ids::Kind::Registry), + ("secrets", store::kind::SECRET, ids::Kind::Secret), + ("keys", store::kind::KEY, ids::Kind::Key), + ("resources", store::kind::RESOURCE, ids::Kind::Resource), +]; + +/// A request with the thing its path names by id, bare or with the prefix +/// of its kind, named by its name instead: every handler then finds it as +/// it always has. A resource is named by its id, which loses its prefix. An +/// id with a prefix that no thing has is not found, and a prefix of another +/// kind is refused. +fn by_name(paths: &berth_core::paths::Paths, mut request: Request) -> Result { + let path = request.uri().path().to_owned(); + let parts: Vec<&str> = path.split('/').collect(); + let (Some(&""), Some(&"v1"), Some(collection), Some(given)) = + (parts.first(), parts.get(1), parts.get(2), parts.get(3)) + else { + return Ok(request); + }; + let Some((_, kind, id_kind)) = NAMED.iter().find(|(known, ..)| known == collection) else { + return Ok(request); + }; + let read = ids::read(*id_kind, given).map_err(|why| Fail::new(code::BAD_REQUEST, why))?; + let (id, prefixed) = match read { + ids::Given::Name(_) => return Ok(request), + ids::Given::Prefixed(id) => (id, true), + ids::Given::Bare(id) => (id, false), + }; + let name = match *id_kind { + ids::Kind::Resource => Some(id), + _ => store::shared(paths) + .map_err(|error| Fail::new(code::INTERNAL, error.to_string()))? + .label_of(kind, store::OWN, &id)?, + }; + let name = match (name, prefixed) { + (Some(name), _) => client::encode(&name), + (None, true) => { + return Err(Fail::new( + code::NOT_FOUND, + format!("No {} {given}", id_kind.noun()), + )); + } + (None, false) => return Ok(request), + }; + let mut rewritten: Vec<&str> = parts.clone(); + rewritten[3] = &name; + let mut uri = rewritten.join("/"); + if let Some(query) = request.uri().query() { + uri = format!("{uri}?{query}"); + } + *request.uri_mut() = uri + .parse() + .map_err(|_| Fail::new(code::BAD_REQUEST, format!("Not a path: {uri}")))?; + Ok(request) } /// What a browser finds at the server's address. It says nothing of the @@ -579,7 +673,7 @@ pub(super) fn known(state: &Serve, name: &str) -> Known { .as_ref() .map_or(HostType::Workstation, berth_api::profile::host_type); let rotating = profile.is_ok_and(|profile| { - let place = berth_core::profiles::Place::own(name); + let place = berth_core::profiles::Place::own(&state.paths, name); let store = berth_core::store::shared(&state.paths); match (place, store) { (Ok(place), Ok(store)) => berth_core::keys::contact::waiting(&store, &place, &profile) @@ -612,6 +706,7 @@ async fn info(remote: Arc, compare: Compare, known: Known) -> HostInfo { ); let reachable = seen.is_ok(); HostInfo { + id: remote.id.clone(), name: remote.name.clone(), address: remote.address.clone(), reachable, @@ -691,7 +786,7 @@ async fn hosts(State(state): App) -> Json> { } #[utoipa::path(get, path = "/v1/hosts/{name}", summary = "One host: its address, and whether it answers.", tag = "hosts", - params(("name" = String, Path, description = "A saved host")), + params(("name" = String, Path, description = "A saved host, by its name or its id (bare or as `host_ID`)")), responses((status = 200, description = "One host", body = HostInfo), (status = 404, description = "No such host", body = ErrorBody)))] async fn host(State(state): App, Path(name): Path) -> Answer> { @@ -707,9 +802,9 @@ async fn host(State(state): App, Path(name): Path) -> Answer, - /// Only the jobs about this host, alone or among others, as a roll-out is. + /// Only the jobs about this host, by name or id, alone or among others, as a roll-out is. host: Option, /// Only the jobs that have not ended, or with `false` only those that have. running: Option, @@ -726,9 +821,20 @@ async fn jobs(State(state): App, Query(query): Query) -> Answer, } -fn inbox_message(stored: store::Stored) -> InboxMessage { +fn inbox_message(paths: &berth_core::paths::Paths, stored: store::Stored) -> InboxMessage { + let (host, host_id) = host_named(paths, &stored.host); InboxMessage { message: stored.message, - host: stored.host, + host, + host_id, done: stored.done, seq: stored.seq, } } +/// What the store keeps of a host a name or id gives: its id, or what was +/// given when no saved host has it. +pub(super) fn host_key(paths: &berth_core::paths::Paths, given: &str) -> String { + berth_core::profiles::Place::find(paths, store::OWN, given) + .ok() + .flatten() + .map_or_else(|| given.to_owned(), |place| place.key()) +} + +/// A host the store keeps by its id: its name now and the id, or what is +/// kept and no id when no saved host has it. +pub(super) fn host_named(paths: &berth_core::paths::Paths, kept: &str) -> (String, String) { + let name = store::shared(paths).ok().and_then(|store| { + store + .label_of(store::kind::PROFILE, store::OWN, kept) + .ok() + .flatten() + }); + match name { + Some(name) => (name, kept.to_owned()), + None => (kept.to_owned(), String::new()), + } +} + #[utoipa::path(get, path = "/v1/inbox", summary = "Messages taken in from the hosts' channels.", tag = "inbox", params(InboxQuery), responses((status = 200, description = "Stored messages, newest first", body = Inbox), (status = 101, description = "As a WebSocket: messages as they are taken in")))] async fn inbox(State(state): App, Query(query): Query, ws: Ws) -> Answer { + let host = query + .host + .as_deref() + .map(|host| host_key(&state.paths, host)); if let Ok(ws) = ws { let events = state.events.subscribe(); + let paths = state.paths.clone(); return Ok(ws.on_upgrade(move |socket| { forward(socket, events, move |event| match event { Event::Inbox(stored) - if query.host.as_ref().is_none_or(|host| *host == stored.host) + if host.as_ref().is_none_or(|host| *host == stored.host) && stored .message .topic .starts_with(query.topic.as_deref().unwrap_or("")) => { - Some(stored.json()) + serde_json::to_value(inbox_message(&paths, stored)).ok() } _ => None, }) })); } let filter = Filter { - host: query.host.as_deref(), + host: host.as_deref(), topic: query.topic.as_deref(), before: query.before, done: query.done, @@ -1017,7 +1154,10 @@ async fn inbox(State(state): App, Query(query): Query, ws: Ws) -> An }; let (messages, next) = state.store.inbox(&filter)?; Ok(Json(Inbox { - messages: messages.into_iter().map(inbox_message).collect(), + messages: messages + .into_iter() + .map(|stored| inbox_message(&state.paths, stored)) + .collect(), next, }) .into_response()) @@ -1051,7 +1191,10 @@ async fn forward( } fn mark(state: &Serve, host: &str, id: u64, done: bool) -> Answer> { - if !state.store.set_done(host, id, done)? { + if !state + .store + .set_done(&host_key(&state.paths, host), id, done)? + { return Err(Fail::new( code::NOT_FOUND, format!("No message {id} from {host}"), @@ -1061,7 +1204,7 @@ fn mark(state: &Serve, host: &str, id: u64, done: bool) -> Answer> { } #[utoipa::path(post, path = "/v1/inbox/{host}/{id}/done", summary = "Mark a message as done.", tag = "inbox", - params(("host" = String, Path, description = "The host it came from"), ("id" = u64, Path, description = "The message's id on that host")), + params(("host" = String, Path, description = "The host it came from, by its name or its id"), ("id" = u64, Path, description = "The message's id on that host")), responses((status = 200, description = "Marked done", body = Done), (status = 404, description = "No such message", body = ErrorBody)))] async fn mark_done(State(state): App, Path((host, id)): Path<(String, u64)>) -> Answer> { @@ -1069,7 +1212,7 @@ async fn mark_done(State(state): App, Path((host, id)): Path<(String, u64)>) -> } #[utoipa::path(delete, path = "/v1/inbox/{host}/{id}/done", summary = "Mark a message as open again.", tag = "inbox", - params(("host" = String, Path, description = "The host it came from"), ("id" = u64, Path, description = "The message's id on that host")), + params(("host" = String, Path, description = "The host it came from, by its name or its id"), ("id" = u64, Path, description = "The message's id on that host")), responses((status = 200, description = "No longer marked done", body = Done), (status = 404, description = "No such message", body = ErrorBody)))] async fn mark_open(State(state): App, Path((host, id)): Path<(String, u64)>) -> Answer> { @@ -1266,8 +1409,12 @@ async fn events(State(state): App, ws: WebSocketUpgrade) -> Response { } Some(frame) } - Event::Resource { id, provisioner, kind, change } if follow.changes.contains(&Changes::Resources) => { - Some(json!({"source": "resource", "resource": id, "provisioner": provisioner, "kind": kind, "change": change})) + Event::Resource { id, provisioner, provisioner_name, kind, change } if follow.changes.contains(&Changes::Resources) => { + let mut frame = json!({"source": "resource", "resource": id, "provisioner": provisioner_name, "kind": kind, "change": change}); + if berth_api::ids::is_id(&provisioner) { + frame["provisioner_id"] = json!(provisioner); + } + Some(frame) } _ => None, }) diff --git a/crates/berth/src/local/serve/connect.rs b/crates/berth/src/local/serve/connect.rs index 137662e..631ec4c 100644 --- a/crates/berth/src/local/serve/connect.rs +++ b/crates/berth/src/local/serve/connect.rs @@ -42,9 +42,10 @@ pub enum Dialler { impl Dialler { /// The dialler for the provisioner that made a resource. pub fn of(paths: &Paths, kept: &Kept) -> Dialler { - match berth_core::provisioner::entry(paths, &kept.record.provisioner) { - Ok(Some(entry)) if !entry.node.is_empty() => Dialler::Node(entry.node), - _ => Dialler::Server, + // The server's hosts are known by name. + match berth_core::provisioner::node_of(paths, &kept.record.provisioner) { + node if node.is_empty() => Dialler::Server, + node => Dialler::Node(node), } } @@ -326,10 +327,10 @@ pub async fn connect( Fail::new( code::NOT_FOUND, match names.is_empty() { - true => format!("{} reports no endpoints", kept.title()), + true => format!("{} reports no endpoints", kept.title(&state.paths)), false => format!( "{} has no endpoint {given}. Its endpoints: {}", - kept.title(), + kept.title(&state.paths), names.join(", ") ), }, @@ -350,7 +351,7 @@ pub async fn connect( format!( "berth serve --simulate dials no endpoint: {} of {} leads nowhere", endpoint.name, - kept.title() + kept.title(&state.paths) ), )); } diff --git a/crates/berth/src/local/serve/here.rs b/crates/berth/src/local/serve/here.rs index 728b387..87c730b 100644 --- a/crates/berth/src/local/serve/here.rs +++ b/crates/berth/src/local/serve/here.rs @@ -195,7 +195,7 @@ mod tests { fn a_client_in_the_process_calls_streams_and_runs_a_job_to_its_end() { let (_dir, paths) = scratch(); let profile = json!({"hostname": "dock", "builder": {}, "access": {"address": "192.0.2.7", "link": ["sh", "-c", "exit 1"]}}); - berth_core::profiles::Place::own("dock") + berth_core::profiles::Place::own(&paths, "dock") .unwrap() .save(&paths, &profile) .unwrap(); @@ -264,7 +264,7 @@ mod tests { here.wait_for_jobs(|_| {}); assert!(here.running().is_empty()); assert!( - berth_core::profiles::Place::own("studio") + berth_core::profiles::Place::own(&paths, "studio") .unwrap() .load(&paths) .unwrap() @@ -292,7 +292,7 @@ mod tests { let (dir, paths) = scratch(); for (name, address) in [("studio", "192.0.2.10"), ("dock", "192.0.2.11")] { let profile = json!({"hostname": name, "builder": {}, "access": {"address": address, "link": ["sh", "-c", "exit 1"]}}); - berth_core::profiles::Place::own(name) + berth_core::profiles::Place::own(&paths, name) .unwrap() .save(&paths, &profile) .unwrap(); diff --git a/crates/berth/src/local/serve/keys.rs b/crates/berth/src/local/serve/keys.rs index bb430cf..339ea89 100644 --- a/crates/berth/src/local/serve/keys.rs +++ b/crates/berth/src/local/serve/keys.rs @@ -21,6 +21,9 @@ use utoipa::ToSchema; /// A key of the ring, by its public half. #[derive(Debug, Clone, Serialize, Deserialize, ToSchema)] pub struct KeyInfo { + /// Its id; empty for the key that replaces another while it rotates. + #[serde(default, skip_serializing_if = "String::is_empty")] + pub id: String, pub name: String, /// The public half, a line of `authorized_keys`. pub public: String, @@ -40,13 +43,15 @@ pub struct KeyList { pub keys: Vec, } -fn info(key: ring::Public, hosts: Vec) -> KeyInfo { +fn info(id: String, key: ring::Public, hosts: Vec) -> KeyInfo { KeyInfo { + id, name: key.name, public: key.public, fingerprint: key.fingerprint, pending: key.pending.map(|next| { Box::new(KeyInfo { + id: String::new(), name: next.name, public: next.public, fingerprint: next.fingerprint, @@ -64,7 +69,10 @@ pub fn listing(paths: &berth_core::paths::Paths) -> berth_core::Result let mut keys = vec![]; for key in ring::list(paths, &store)? { let hosts = keys::users(&store, &key.name)?; - keys.push(info(key, hosts)); + let id = store + .id_of(store::kind::KEY, store::OWN, &key.name)? + .unwrap_or_default(); + keys.push(info(id, key, hosts)); } Ok(KeyList { keys }) } @@ -103,7 +111,7 @@ pub async fn list(State(state): App) -> Answer> { } #[utoipa::path(put, path = "/v1/keys/{name}", summary = "Add a key to the ring: a new ed25519 key without a body, or the OpenSSH private key in the body, which may have no passphrase. The answer has its public half only.", tag = "set-up", - params(("name" = String, Path, description = "The key's name: letters, digits, - and _")), + params(("name" = String, Path, description = "The key's name: letters, digits, - and _, and not in the shape of an id")), request_body(content = Vec, content_type = "application/octet-stream", description = "An OpenSSH private key, or nothing"), responses((status = 200, description = "Added", body = KeyInfo), (status = 400, description = "Not a name, not a private key, or a key with a passphrase", body = ErrorBody), @@ -118,14 +126,17 @@ pub async fn add( let store = store::shared(&paths)?; let private = (!body.is_empty()).then_some(&body[..]); let key = ring::add(&paths, &store, &name, private)?; - Ok(info(key, vec![])) + let id = store + .id_of(store::kind::KEY, store::OWN, &key.name)? + .unwrap_or_default(); + Ok(info(id, key, vec![])) }) .await .map(Json) } #[utoipa::path(delete, path = "/v1/keys/{name}", summary = "Remove a key from the ring. Refused for the key berth, while a host logs in with the key, and while it rotates.", tag = "set-up", - params(("name" = String, Path, description = "The key")), + params(("name" = String, Path, description = "The key, by its name or its id (bare or as `key_ID`)")), responses((status = 200, description = "Removed", body = Done), (status = 404, description = "No such key", body = ErrorBody), (status = 409, description = "It is berth's, in use or rotating", body = ErrorBody)))] @@ -140,7 +151,7 @@ pub async fn remove(State(state): App, Path(name): Path) -> Answer Option> { berth_core::profiles::list(&self.paths, store::OWN) .ok() - .map(|saved| saved.into_iter().collect()) + .map(|saved| { + saved + .into_iter() + .map(|(place, profile)| (place.argument(), profile)) + .collect() + }) } fn resources(&self) -> Option> { @@ -155,9 +163,11 @@ impl Watch { "added" => "created", other => other, }; + let provisioner = record.map(|r| r.provisioner.clone()).unwrap_or_default(); let _ = self.events.send(Event::Resource { id: id.to_owned(), - provisioner: record.map(|r| r.provisioner.clone()).unwrap_or_default(), + provisioner_name: berth_core::provisioner::label(&self.paths, &provisioner), + provisioner, kind: record.map(|r| r.kind.clone()).unwrap_or_default(), change, }); @@ -186,6 +196,9 @@ impl Watch { /// A host as `berth serve` knows it. pub struct Remote { pub name: String, + /// The saved host's id, which its messages are kept by; empty for one + /// that is not saved. + pub id: String, pub address: String, pub link: Option, /// The SSH user's sudo password, for a host that asks for one. @@ -199,9 +212,19 @@ pub struct Remote { } impl Remote { + /// What its messages and channel cursor are kept by: its id, or its + /// name when it has none. + pub fn key(&self) -> &str { + match self.id.is_empty() { + true => &self.name, + false => &self.id, + } + } + pub fn new(name: &str, address: &str, spec: LinkSpec) -> Remote { Remote { name: name.to_owned(), + id: String::new(), address: address.to_owned(), link: Some(Link::new(spec)), password: None, @@ -388,9 +411,9 @@ impl Serve { .values() .filter(|remote| { remote.profile.as_ref().is_some_and(|taken| { - !saved - .iter() - .any(|(name, config)| *name == remote.name && config == taken) + !saved.iter().any(|(place, config)| { + place.argument() == remote.name && config == taken + }) }) }) .map(|remote| remote.name.clone()) @@ -399,13 +422,11 @@ impl Serve { for name in stale { self.forget(&name); } - for (name, config) in saved { + for (place, config) in saved { + let name = place.argument(); if self.host(&name).is_some() { continue; } - let Ok(place) = berth_core::profiles::Place::own(&name) else { - continue; - }; let host = Host { name, place, @@ -452,6 +473,7 @@ impl Serve { Ok(spec) => Remote::new(&host.name, &host.address(), spec), Err(problem) => Remote { name: host.name.clone(), + id: String::new(), address: host.address(), link: None, password: None, @@ -461,6 +483,7 @@ impl Serve { }, }; self.add(Remote { + id: host.place.key(), password: password.ok().flatten(), profile: Some(host.config.clone()), ..remote @@ -468,9 +491,11 @@ impl Serve { } } - /// A message from a host: store what is kept and tell whoever listens. + /// A message from a host: store what is kept, by the host's id, and tell + /// whoever listens. fn accept(&self, host: &str, message: Message) { - let taken = self.store.take_in(host, &message); + let key = api::host_key(&self.paths, host); + let taken = self.store.take_in(&key, &message); let _ = self.events.send(Event::Channel { host: host.to_owned(), message, @@ -519,7 +544,7 @@ async fn take_in(state: Arc, remote: Arc) { tokio::time::sleep(Duration::from_secs(60)).await; continue; } - let since = state.store.cursor(&remote.name).unwrap_or(0); + let since = state.store.cursor(remote.key()).unwrap_or(0); if let Ok(mut stream) = link.stream(&format!("/v1/channel?since={since}")).await { wait = 1; while let Some(Ok(frame)) = stream.next().await { diff --git a/crates/berth/src/local/serve/provisioners.rs b/crates/berth/src/local/serve/provisioners.rs index 17df650..89287f3 100644 --- a/crates/berth/src/local/serve/provisioners.rs +++ b/crates/berth/src/local/serve/provisioners.rs @@ -51,7 +51,7 @@ async fn blocking( fn known(state: &Serve, store: &Store, name: &str) -> Answer { provision::list(&state.paths, store) .into_iter() - .find(|known| known.id == name) + .find(|known| known.name == name || known.id == name) .ok_or_else(|| { missing(format!( "No provisioner {name}. See: berth provisioner list" @@ -72,7 +72,7 @@ pub async fn types(State(state): App) -> Answer>> { } #[utoipa::path(put, path = "/v1/provisioners/{name}", summary = "Register a provisioner under a name of its own, of a type a plugin brings, or change the settings of one that is registered. A registered one keeps its type.", tag = "set-up", - params(("name" = String, Path, description = "Its name: lowercase letters, digits, - and _, starting with a letter")), + params(("name" = String, Path, description = "Its name: lowercase letters, digits, - and _, starting with a letter, and not in the shape of an id; or the id of a registered one")), request_body = SavedProvisioner, responses((status = 200, description = "Registered, as it is listed", body = ProvisionerInfo), (status = 400, description = "The name, the type or a setting does not fit; the message says which", body = ErrorBody)))] @@ -88,7 +88,7 @@ pub async fn save( } #[utoipa::path(delete, path = "/v1/provisioners/{name}", summary = "Forget a registered provisioner, with its settings and the secrets it keeps of its own. It is kept while it holds resources.", tag = "set-up", - params(("name" = String, Path, description = "Its name")), + params(("name" = String, Path, description = "Its name or its id (bare or as `prov_ID`)")), responses((status = 204, description = "Forgotten"), (status = 404, description = "No such provisioner", body = ErrorBody), (status = 409, description = "It still holds resources; the message names them", body = ErrorBody)))] @@ -101,7 +101,7 @@ pub async fn remove(State(state): App, Path(name): Path) -> Answer>, Query, style = Form, explode, description = "Values of the provisioner's own arguments as `NAME=VALUE` for this once, such as another server to ask; the others are its settings")), responses((status = 200, description = "What it said. A state of `unreachable` or `unusable` comes with why as its message", body = ProvisionerState), (status = 404, description = "No such provisioner", body = ErrorBody)))] @@ -120,7 +120,7 @@ pub async fn provisioner( } #[utoipa::path(get, path = "/v1/provisioners/{name}/actions", summary = "What can be done to a provisioner: its plugin's actions, and berth's own settings of it.", tag = "set-up", - params(("name" = String, Path, description = "The provisioner's name")), + params(("name" = String, Path, description = "The provisioner, by its name or its id (bare or as `prov_ID`)")), responses((status = 200, description = "The actions, each saying whether it can run here now", body = Vec), (status = 404, description = "No such provisioner", body = ErrorBody)))] pub async fn provisioner_actions( @@ -135,7 +135,7 @@ pub async fn provisioner_actions( } #[utoipa::path(post, path = "/v1/provisioners/{name}/actions/{action}", summary = "Run one of a provisioner's actions. One that changes something is a job, which holds the provisioner's lock; one that only reads answers at once.", tag = "set-up", - params(("name" = String, Path, description = "The provisioner's name"), + params(("name" = String, Path, description = "The provisioner, by its name or its id (bare or as `prov_ID`)"), ("action" = String, Path, description = "The action, as provisioners.actions names it")), request_body(content = Option, description = "Its arguments, which may also give the provisioner's own for this once, and a token that makes asking again safe"), responses((status = 202, description = "Started as a job", body = Acted), diff --git a/crates/berth/src/local/serve/resources.rs b/crates/berth/src/local/serve/resources.rs index 8c7d32e..1f5cbdb 100644 --- a/crates/berth/src/local/serve/resources.rs +++ b/crates/berth/src/local/serve/resources.rs @@ -81,7 +81,7 @@ fn described(state: &Serve, kept: &resource::Kept) -> Resource { /// How a host is reached, to tell whether making its machine changed it. fn reached(paths: &Paths, host: &str) -> Value { - berth_core::profiles::Place::own(host) + berth_core::profiles::Place::own(paths, host) .and_then(|place| Host::load_at(paths, place)) .map(|host| host.config["access"].clone()) .unwrap_or_default() @@ -113,7 +113,7 @@ pub async fn list(State(state): App, Query(filter): Query) -> Answer) -> Answer> { @@ -208,7 +208,7 @@ pub async fn create(State(state): App, body: Bytes) -> Answer<(StatusCode, Json< } #[utoipa::path(get, path = "/v1/resources/{id}/actions", summary = "What can be done to a resource: start, stop and change as its kind can, its plugin's actions of the kind, and removing it.", tag = "resources", - params(("id" = String, Path, description = "berth's id of the resource")), + params(("id" = String, Path, description = "berth's id of the resource, bare or as `res_ID`")), responses((status = 200, description = "The actions, each saying whether it can run now", body = Vec), (status = 404, description = "No such resource", body = ErrorBody)))] pub async fn actions(State(state): App, Path(id): Path) -> Answer>> { @@ -320,7 +320,7 @@ fn admitted(state: &Serve, access: &Access, id: &str, action: &str, act: &Act) - } #[utoipa::path(post, path = "/v1/resources/{id}/actions/{action}", summary = "Run one of a resource's actions. One that changes something is a job of the resource, which holds its lock; one that only reads answers at once.", tag = "resources", - params(("id" = String, Path, description = "berth's id of the resource"), + params(("id" = String, Path, description = "berth's id of the resource, bare or as `res_ID`"), ("action" = String, Path, description = "The action, as resources.actions names it")), request_body(content = Option, description = "Its arguments, and a token that makes asking again safe"), responses((status = 202, description = "Started as a job", body = Acted), @@ -349,7 +349,7 @@ pub async fn act( } #[utoipa::path(put, path = "/v1/resources/{id}", summary = "Change a resource, as a job: other values of its kind's arguments, a new specification, other secrets. What is not given stays.", tag = "resources", - params(("id" = String, Path, description = "berth's id of the resource")), + params(("id" = String, Path, description = "berth's id of the resource, bare or as `res_ID`")), request_body(content = ResourceChange, description = "What changes, and a token that makes asking again safe"), responses((status = 202, description = "Started as a job", body = Acted), (status = 400, description = "An argument, a secret or the specification does not fit", body = ErrorBody), @@ -383,7 +383,7 @@ pub struct Token { } #[utoipa::path(delete, path = "/v1/resources/{id}", summary = "Remove a resource with its provisioner, as a job, and forget it. A host's profile stays.", tag = "resources", - params(("id" = String, Path, description = "berth's id of the resource"), Token), + params(("id" = String, Path, description = "berth's id of the resource, bare or as `res_ID`"), Token), responses((status = 202, description = "Started as a job", body = Acted), (status = 404, description = "No such resource", body = ErrorBody), (status = 409, description = "Something else is being done to it", body = ErrorBody), @@ -426,7 +426,7 @@ const FOLLOW_FOR: Duration = Duration::from_secs(60 * 60); const READ_FOR: Duration = Duration::from_secs(60); #[utoipa::path(get, path = "/v1/resources/{id}/logs", summary = "What a resource wrote, as its provisioner gives it, with secrets masked: the last lines as text, or as a WebSocket those and what follows.", tag = "resources", - params(("id" = String, Path, description = "berth's id of the resource"), LogQuery), + params(("id" = String, Path, description = "berth's id of the resource, bare or as `res_ID`"), LogQuery), responses((status = 200, description = "The last lines", content_type = "text/plain", body = String), (status = 101, description = "As a WebSocket: the lines as they come"), (status = 404, description = "No such resource", body = ErrorBody), diff --git a/crates/berth/src/local/serve/setup.rs b/crates/berth/src/local/serve/setup.rs index 523a8ae..b949ea9 100644 --- a/crates/berth/src/local/serve/setup.rs +++ b/crates/berth/src/local/serve/setup.rs @@ -45,16 +45,16 @@ fn store(state: &Serve) -> Store { /// Where the profile of a host of this name is: a host of the server's own, /// whatever its settings say, by a name that is never a path. -pub(super) fn own_place(name: &str) -> Answer { +pub(super) fn own_place(paths: &berth_core::paths::Paths, name: &str) -> Answer { if !safe(name) { return Err(bad(format!("Not a host name: {name}"))); } - Place::own(name).map_err(bad) + Place::own(paths, name).map_err(bad) } /// A saved host's profile, or a 404. pub(super) fn saved_profile(state: &Serve, name: &str) -> Answer<(Place, Value)> { - let place = own_place(name)?; + let place = own_place(&state.paths, name)?; match place.load(&state.paths).map_err(internal)? { Some(profile) => Ok((place, profile)), None => Err(missing(format!("No host {name}"))), @@ -62,7 +62,7 @@ pub(super) fn saved_profile(state: &Serve, name: &str) -> Answer<(Place, Value)> } #[utoipa::path(get, path = "/v1/hosts/{name}/profile", summary = "A host's profile, as it is saved.", tag = "set-up", - params(("name" = String, Path, description = "A saved host")), + params(("name" = String, Path, description = "A saved host, by its name or its id (bare or as `host_ID`)")), responses((status = 200, description = "The profile, with the passwords and keys in it", body = Value), (status = 404, description = "No such host", body = ErrorBody)))] pub async fn profile(State(state): App, Path(name): Path) -> Answer> { @@ -70,7 +70,7 @@ pub async fn profile(State(state): App, Path(name): Path) -> Answer Answer> { // A caller on the socket or in berth itself is the server's own user. let own_user = access.is_none(); - let place = own_place(&name)?; + let place = own_place(&state.paths, &name)?; if !profile.is_object() { return Err(bad("A profile is an object")); } @@ -138,7 +138,7 @@ pub async fn save_profile( } #[utoipa::path(delete, path = "/v1/hosts/{name}", summary = "Remove a host's profile. The host itself is left as it is.", tag = "set-up", - params(("name" = String, Path, description = "A saved host")), + params(("name" = String, Path, description = "A saved host, by its name or its id (bare or as `host_ID`)")), responses((status = 200, description = "Removed", body = Done), (status = 404, description = "No such host", body = ErrorBody), (status = 409, description = "It has a machine berth created, which is deleted first, or one is being created", body = ErrorBody)))] @@ -179,7 +179,7 @@ pub struct Credentials { } #[utoipa::path(get, path = "/v1/hosts/{name}/credentials", summary = "The passwords the server generated for a host's accounts.", tag = "set-up", - params(("name" = String, Path, description = "A saved host")), + params(("name" = String, Path, description = "A saved host, by its name or its id (bare or as `host_ID`)")), responses((status = 200, description = "The passwords by account; none when none were generated", body = Credentials), (status = 404, description = "No such host", body = ErrorBody)))] pub async fn credentials(State(state): App, Path(name): Path) -> Answer> { @@ -194,8 +194,13 @@ pub async fn credentials(State(state): App, Path(name): Path) -> Answer< /// except to a host. #[derive(Serialize, ToSchema)] pub struct SecretNames { - /// A registry's password or token is `registry/NAME`. + /// The global secrets' names, and the addresses of those that belong to + /// something: a registry's password or token is `registry/NAME`, the + /// passwords berth logs in to a host with `ssh/HOST` and `sudo/HOST`, + /// and a provisioner's own `provisioner/NAME/SECRET`. pub names: Vec, + /// The global secrets, by name, with their ids. + pub secrets: Vec, } #[derive(Deserialize, ToSchema)] @@ -204,12 +209,13 @@ pub struct SecretValue { } fn secret_name(name: &str) -> Answer<&str> { - // A host's SSH or sudo password is kept under the host's name. + use berth_api::ids::{self, Kind}; + // A host's SSH or sudo password belongs to the host, by its name or id. if let Some(host) = name .strip_prefix("ssh/") .or_else(|| name.strip_prefix("sudo/")) { - return match profiles::valid_name(host) { + return match profiles::valid_name(host) || ids::id_in(Kind::Host, host).is_some() { true => Ok(name), false => Err(bad(format!("Not a host's name: {host}"))), }; @@ -217,11 +223,16 @@ fn secret_name(name: &str) -> Answer<&str> { if berth_core::provisioner::is_secret_name(name) { return Ok(name); } + if let Some(registry) = name.strip_prefix("registry/") + && ids::id_in(Kind::Registry, registry).is_some() + { + return Ok(name); + } let plain = name.strip_prefix("registry/").unwrap_or(name); - match plugins::valid_name(plain) { + match plugins::valid_name(plain) && !ids::looks_like_id(plain) { true => Ok(name), false => Err(bad(format!( - "Not a secret's name: {name} (lower-case letters, digits, - and _)" + "Not a secret's name: {name} (lower-case letters, digits, - and _, and not in the shape of an id)" ))), } } @@ -229,12 +240,75 @@ fn secret_name(name: &str) -> Answer<&str> { #[utoipa::path(get, path = "/v1/secrets", summary = "The names of the secrets.", tag = "set-up", responses((status = 200, description = "The names", body = SecretNames)))] pub async fn secrets(State(state): App) -> Answer> { - let names = store(&state).all_secret_names().map_err(internal)?; - Ok(Json(SecretNames { names })) + let store = store(&state); + let names = store.all_secret_names().map_err(internal)?; + let secrets = store + .secret_ids() + .map_err(internal)? + .into_iter() + .map(named) + .collect(); + Ok(Json(SecretNames { names, secrets })) +} + +pub(super) fn named(found: berth_core::store::Found) -> berth_api::Named { + berth_api::Named { + id: found.id, + name: found.name, + } +} + +/// A rename's error as an answer: a name that is taken or cannot be one is +/// refused, a thing that is not there not found. +fn renamed(result: berth_core::Result<()>) -> Answer> { + match result { + Ok(()) => Ok(Json(Done { ok: true })), + Err(error) if error.to_string().starts_with("No ") => Err(missing(error.to_string())), + Err(error) if error.to_string().starts_with("There is ") => { + Err(Fail::new(code::CONFLICT, error.to_string())) + } + Err(error) => Err(bad(error.to_string())), + } +} + +#[utoipa::path(post, path = "/v1/secrets/{name}/rename", summary = "Give a global secret another name.", tag = "set-up", + params(("name" = String, Path, description = "The secret, by its name or its id (bare or as `sec_ID`)")), + request_body(content = berth_api::Rename, description = "The new name"), + responses((status = 200, description = "Renamed: the profiles that hand it to hosts refer to it still", body = Done), + (status = 400, description = "Not a name for a secret, or one that belongs to something", body = ErrorBody), + (status = 404, description = "No such secret", body = ErrorBody), + (status = 409, description = "The name is taken", body = ErrorBody)))] +pub async fn rename_secret( + State(state): App, + Path(name): Path, + Json(to): Json, +) -> Answer> { + renamed(store(&state).rename_secret(&name, to.name.trim())) +} + +#[utoipa::path(post, path = "/v1/registries/{name}/rename", summary = "Give a registry another name.", tag = "set-up", + params(("name" = String, Path, description = "The registry, by its name or its id (bare or as `reg_ID`)")), + request_body(content = berth_api::Rename, description = "The new name"), + responses((status = 200, description = "Renamed, with its login: the hosts that use it refer to it still", body = Done), + (status = 400, description = "Not a name for a registry", body = ErrorBody), + (status = 404, description = "No such registry", body = ErrorBody), + (status = 409, description = "The name is taken", body = ErrorBody)))] +pub async fn rename_registry( + State(state): App, + Path(name): Path, + Json(to): Json, +) -> Answer> { + let new = to.name.trim(); + if !plugins::valid_name(new) || berth_api::ids::looks_like_id(new) { + return Err(bad(format!( + "A registry has a name of lower-case letters, digits, - and _, and not in the shape of an id: {new}" + ))); + } + renamed(store(&state).rename_registry(&name, new).map(|_| ())) } #[utoipa::path(put, path = "/v1/secrets/{name}", summary = "Set a secret's value.", tag = "set-up", - params(("name" = String, Path, description = "The secret; `registry/NAME` is a registry's login")), + params(("name" = String, Path, description = "The secret, by its name or its id (bare or as `sec_ID`); `registry/REGISTRY` is a registry's login, `ssh/HOST` and `sudo/HOST` a host's passwords and `provisioner/PROVISIONER/SECRET` a provisioner's own")), request_body(content = SecretValue, description = "The value"), responses((status = 200, description = "Kept", body = Done), (status = 400, description = "Not a name", body = ErrorBody)))] @@ -243,14 +317,16 @@ pub async fn set_secret( Path(name): Path, Json(secret): Json, ) -> Answer> { - store(&state) - .set_secret(secret_name(&name)?, Some(&secret.value)) - .map_err(internal)?; - Ok(Json(Done { ok: true })) + match store(&state).set_secret(secret_name(&name)?, Some(&secret.value)) { + Ok(()) => Ok(Json(Done { ok: true })), + // One that would belong to a registry, host or provisioner there is not. + Err(error) if error.to_string().starts_with("No ") => Err(missing(error.to_string())), + Err(error) => Err(bad(error.to_string())), + } } #[utoipa::path(delete, path = "/v1/secrets/{name}", summary = "Remove a secret.", tag = "set-up", - params(("name" = String, Path, description = "The secret")), + params(("name" = String, Path, description = "The secret, by its name or its id (bare or as `sec_ID`), or the address of one that belongs to something")), responses((status = 200, description = "Removed", body = Done), (status = 404, description = "No such secret", body = ErrorBody)))] pub async fn remove_secret(State(state): App, Path(name): Path) -> Answer> { @@ -266,8 +342,9 @@ pub async fn remove_secret(State(state): App, Path(name): Path) -> Answe #[derive(Serialize, ToSchema)] pub struct Registries { - /// By name: `type` (nexus, npm, raw or docker), `url`, `username` and, for - /// a Nexus server, its `repositories`. + /// By name: its `id`, `type` (nexus, npm, raw or docker), `url`, + /// `username` and, for a Nexus server, its `repositories`. The id is + /// berth's, and is left out of what is saved. pub registries: Map, } @@ -280,12 +357,21 @@ pub struct Checked { #[utoipa::path(get, path = "/v1/registries", summary = "The registries.", tag = "set-up", responses((status = 200, description = "The registries by name", body = Registries)))] pub async fn registries(State(state): App) -> Answer> { - let registries = store(&state).registries().map_err(internal)?; + let store = store(&state); + let mut registries = store.registries().map_err(internal)?; + for found in store.registry_ids().map_err(internal)? { + if let Some(fields) = registries + .get_mut(&found.name) + .and_then(Value::as_object_mut) + { + fields.insert("id".into(), Value::String(found.id)); + } + } Ok(Json(Registries { registries })) } #[utoipa::path(put, path = "/v1/registries/{name}", summary = "Add a registry, or change one.", tag = "set-up", - params(("name" = String, Path, description = "The registry's name here")), + params(("name" = String, Path, description = "The registry's name here, or the id of one there is")), request_body(content = Value, description = "`type`, `url`, and optionally `username` and `repositories`"), responses((status = 200, description = "Saved", body = Done), (status = 400, description = "Not a registry", body = ErrorBody)))] @@ -296,7 +382,11 @@ pub async fn save_registry( ) -> Answer> { let kind = registry["type"].as_str().unwrap_or(""); let url = registry["url"].as_str().unwrap_or(""); - if !plugins::valid_name(&name) || !registry::TYPES.contains(&kind) || url.is_empty() { + if !plugins::valid_name(&name) + || berth_api::ids::looks_like_id(&name) + || !registry::TYPES.contains(&kind) + || url.is_empty() + { return Err(bad(format!( "A registry has a name of lower-case letters, digits, - and _, a type ({}) and a url", registry::TYPES.join(", ") @@ -310,7 +400,7 @@ pub async fn save_registry( } #[utoipa::path(delete, path = "/v1/registries/{name}", summary = "Remove a registry with its stored login.", tag = "set-up", - params(("name" = String, Path, description = "A registry")), + params(("name" = String, Path, description = "A registry, by its name or its id (bare or as `reg_ID`)")), responses((status = 200, description = "Removed", body = Done), (status = 404, description = "No such registry", body = ErrorBody)))] pub async fn remove_registry(State(state): App, Path(name): Path) -> Answer> { @@ -319,15 +409,13 @@ pub async fn remove_registry(State(state): App, Path(name): Path) -> Ans if registries.remove(&name).is_none() { return Err(missing(format!("No registry {name}"))); } - store - .set_secret(&format!("registry/{name}"), None) - .map_err(internal)?; + // Its login goes with it. store.save_registries(registries).map_err(internal)?; Ok(Json(Done { ok: true })) } #[utoipa::path(post, path = "/v1/registries/{name}/check", summary = "Whether a registry answers and takes its login.", tag = "set-up", - params(("name" = String, Path, description = "A registry")), + params(("name" = String, Path, description = "A registry, by its name or its id (bare or as `reg_ID`)")), responses((status = 200, description = "What was found", body = Checked), (status = 404, description = "No such registry", body = ErrorBody)))] pub async fn check_registry(State(state): App, Path(name): Path) -> Answer> { @@ -653,7 +741,9 @@ pub struct Generated { pub async fn generate(State(state): App, Json(asked): Json) -> Answer> { use berth_core::credentials; let kept = match (&asked.host, asked.what.as_str()) { - (Some(host), "password") if asked.password.is_none() => Some(own_place(host)?), + (Some(host), "password") if asked.password.is_none() => { + Some(own_place(&state.paths, host)?) + } _ => None, }; let paths = state.paths.clone(); diff --git a/crates/berth/src/local/serve/tests.rs b/crates/berth/src/local/serve/tests.rs index 32369b5..3118bf3 100644 --- a/crates/berth/src/local/serve/tests.rs +++ b/crates/berth/src/local/serve/tests.rs @@ -1400,6 +1400,7 @@ async fn the_server_is_set_up_through_the_api() { .0, 200 ); + // A registry's login belongs to the registry, which is there first. assert_eq!( send( client, @@ -1409,7 +1410,7 @@ async fn the_server_is_set_up_through_the_api() { ) .await .0, - 200 + 404 ); assert_eq!( send( @@ -1423,7 +1424,51 @@ async fn the_server_is_set_up_through_the_api() { 400 ); let (_, secrets) = send(client, "GET", "/v1/secrets", Value::Null).await; - assert_eq!(secrets, json!({"names": ["github", "registry/main"]})); + assert_eq!(secrets["names"], json!(["github"])); + let github = secrets["secrets"][0]["id"].as_str().unwrap().to_owned(); + assert!(berth_api::ids::is_id(&github)); + assert_eq!(secrets["secrets"][0]["name"], "github"); + // By id, bare or with its prefix; a prefix of another kind is refused. + let (status, _) = send( + client, + "POST", + &format!("/v1/secrets/sec_{github}/rename"), + json!({"name": "gh"}), + ) + .await; + assert_eq!(status, 200); + let (status, _) = send( + client, + "POST", + &format!("/v1/secrets/{github}/rename"), + json!({"name": "github"}), + ) + .await; + assert_eq!(status, 200); + let (status, said) = send( + client, + "PUT", + &format!("/v1/secrets/host_{github}"), + json!({"value": "x"}), + ) + .await; + assert_eq!(status, 400, "{said}"); + let (status, _) = send( + client, + "PUT", + "/v1/secrets/sec_01972c4e8f3a7b219d4e5a6b7c8d9eff", + json!({"value": "x"}), + ) + .await; + assert_eq!(status, 404); + let (status, said) = send( + client, + "PUT", + "/v1/secrets/01972c4e8f3a7b219d4e5a6b7c8d9eff", + json!({"value": "x"}), + ) + .await; + assert_eq!(status, 400, "{said}"); assert_eq!( send(client, "DELETE", "/v1/secrets/github", Value::Null) .await @@ -1457,7 +1502,51 @@ async fn the_server_is_set_up_through_the_api() { 400 ); let (_, registries) = send(client, "GET", "/v1/registries", Value::Null).await; - assert_eq!(registries["registries"]["main"], registry); + let main = registries["registries"]["main"]["id"] + .as_str() + .unwrap() + .to_owned(); + let mut listed = registries["registries"]["main"].clone(); + listed.as_object_mut().unwrap().remove("id"); + assert_eq!(listed, registry); + assert_eq!( + send( + client, + "PUT", + "/v1/secrets/registry%2Fmain", + json!({"value": "pw"}) + ) + .await + .0, + 200 + ); + // Renamed by its id, the login goes with it. + let (status, said) = send( + client, + "POST", + &format!("/v1/registries/reg_{main}/rename"), + json!({"name": "npm"}), + ) + .await; + assert_eq!(status, 200, "{said}"); + let (status, said) = send( + client, + "POST", + "/v1/registries/npm/rename", + json!({"name": "npm"}), + ) + .await; + assert_eq!(status, 400, "{said}"); + let (_, secrets) = send(client, "GET", "/v1/secrets", Value::Null).await; + assert_eq!(secrets["names"], json!(["registry/npm"])); + let (status, said) = send( + client, + "POST", + "/v1/registries/npm/rename", + json!({"name": "main"}), + ) + .await; + assert_eq!(status, 200, "{said}"); assert_eq!( send(client, "DELETE", "/v1/registries/main", Value::Null) .await @@ -1465,8 +1554,8 @@ async fn the_server_is_set_up_through_the_api() { 200 ); assert_eq!( - send(client, "GET", "/v1/secrets", Value::Null).await.1, - json!({"names": []}) + send(client, "GET", "/v1/secrets", Value::Null).await.1["names"], + json!([]) ); // Sending secrets needs their values and the host. @@ -2285,7 +2374,7 @@ async fn a_provisioner_has_a_page_and_actions_that_run_as_jobs() { .as_array() .unwrap() .iter() - .find(|known| known["id"] == id) + .find(|known| known["name"] == id) .unwrap() .clone() }; @@ -2319,7 +2408,7 @@ async fn a_provisioner_has_a_page_and_actions_that_run_as_jobs() { send( client, "PUT", - "/v1/secrets/api_token", + "/v1/secrets/provisioner%2Fvm%2Fapi_token", json!({"value": "tok-s3cret"}), ) .await; @@ -2380,7 +2469,7 @@ async fn a_provisioner_has_a_page_and_actions_that_run_as_jobs() { .await; assert_eq!(status, 202, "{acted}"); assert_eq!( - (&acted["job"]["subject"], &acted["job"]["action"]), + (&acted["job"]["subject_name"], &acted["job"]["action"]), (&json!("provisioner:vm"), &json!("settings")) ); let (output, code) = job_ended(&served.socket, acted["job"]["id"].as_str().unwrap()).await; @@ -2443,7 +2532,7 @@ async fn a_provisioner_has_a_page_and_actions_that_run_as_jobs() { Value::Null, ) .await; - assert_eq!(job["subject"], "provisioner:vm"); + assert_eq!(job["subject_name"], "provisioner:vm"); // One that fails says why, in the job or as the answer. std::fs::write(fake.join("code.act"), "1").unwrap(); std::fs::write(fake.join("say.act"), "engine gone\n").unwrap(); @@ -2585,7 +2674,7 @@ async fn a_provisioner_has_a_page_and_actions_that_run_as_jobs() { .as_array() .unwrap() .iter() - .map(|known| known["id"].as_str().unwrap()) + .map(|known| known["name"].as_str().unwrap()) .collect(); assert_eq!(names, ["vm"]); // A registered one keeps its type, and a type has to be one a plugin brings. @@ -2637,9 +2726,19 @@ async fn a_provisioner_is_renamed_with_everything_that_names_it() { job_ended(&served.socket, &before).await; let own = berth_core::provisioner::secret_name("vm", "api_token"); registries.set_secret(&own, Some("tok-own")).unwrap(); - // One left by a provisioner of the new name before is not taken over. - let stale = berth_core::provisioner::secret_name("lab", "other"); - registries.set_secret(&stale, Some("tok-stale")).unwrap(); + let id_of = |name: &str| { + berth_core::provisioner::id_of(paths, name) + .unwrap() + .unwrap() + }; + let (vm, old) = (id_of("vm"), id_of("old")); + let studio = berth_core::profiles::Place::own(paths, "studio").unwrap(); + studio + .save( + paths, + &json!({"builder": {}, "access": {"address": "192.0.2.10"}}), + ) + .unwrap(); let record = |id: &str, kind: &str, provisioner: &str, host: &str, state: &str| Kept { scope: String::new(), record: Record { @@ -2652,26 +2751,32 @@ async fn a_provisioner_is_renamed_with_everything_that_names_it() { ..Record::default() }, }; - record("r-wiki", "deployment", "vm", "", "running") + let (wiki, plan, other) = ( + berth_core::store::new_id(), + berth_core::store::new_id(), + berth_core::store::new_id(), + ); + record(&wiki, "deployment", &vm, "", "running") .save(paths) .unwrap(); record( - "r-plan", + &plan, "machine", - "vm", - "studio", + &vm, + &studio.key(), berth_core::resource::PLANNED, ) .save(paths) .unwrap(); - record("r-other", "deployment", "old", "", "running") + record(&other, "deployment", &old, "", "running") .save(paths) .unwrap(); // A node's provisioner stays the node's. + let dock = berth_core::store::new_id(); let mut offered = berth_core::provisioner::entry(paths, "vm") .unwrap() .unwrap(); - offered.node = "dock".into(); + offered.node = dock.clone(); berth_core::provisioner::save_entry(paths, "vm", &offered).unwrap(); // A name that is taken or no name is refused before anything starts. @@ -2679,6 +2784,7 @@ async fn a_provisioner_is_renamed_with_everything_that_names_it() { ("old", "There is a provisioner old already"), ("Not A Name", "is not a name for a provisioner"), ("vm", "vm has that name already"), + (vm.as_str(), "is not a name for a provisioner"), ] { let (status, refused) = send( client, @@ -2691,10 +2797,11 @@ async fn a_provisioner_is_renamed_with_everything_that_names_it() { assert!(message(&refused).contains(said), "{refused}"); } + // By its id with its prefix, as by its name. let (status, acted) = send( client, "POST", - "/v1/provisioners/vm/actions/rename", + &format!("/v1/provisioners/prov_{vm}/actions/rename"), json!({"arguments": {"name": "lab"}}), ) .await; @@ -2710,12 +2817,13 @@ async fn a_provisioner_is_renamed_with_everything_that_names_it() { .0, 404 ); + // The id is the same, and everything that is the provisioner's or refers to it stays. let entry = berth_core::provisioner::entry(paths, "lab") .unwrap() .unwrap(); assert_eq!( - (entry.kind.as_str(), entry.node.as_str()), - ("fake/vm", "dock") + (entry.id.as_str(), entry.kind.as_str(), entry.node.as_str()), + (vm.as_str(), "fake/vm", dock.as_str()) ); assert_eq!(entry.settings["server"], "https://192.0.2.10:8443"); assert_eq!( @@ -2726,7 +2834,6 @@ async fn a_provisioner_is_renamed_with_everything_that_names_it() { Some("tok-own") ); assert_eq!(registries.secret(&own).unwrap(), None); - assert_eq!(registries.secret(&stale).unwrap(), None); let by: std::collections::BTreeMap = berth_core::resource::list(paths) .unwrap() .into_iter() @@ -2735,15 +2842,27 @@ async fn a_provisioner_is_renamed_with_everything_that_names_it() { assert_eq!( by, std::collections::BTreeMap::from([ - ("r-other".to_owned(), "old".to_owned()), - ("r-plan".to_owned(), "lab".to_owned()), - ("r-wiki".to_owned(), "lab".to_owned()), + (other.clone(), old.clone()), + (plan.clone(), vm.clone()), + (wiki.clone(), vm.clone()), ]) ); - // Its jobs are the renamed one's, the rename among them. + let (_, shown) = send( + client, + "GET", + &format!("/v1/resources/res_{wiki}"), + Value::Null, + ) + .await; + assert_eq!( + (&shown["provisioner"], &shown["provisioner_id"]), + (&json!("lab"), &json!(vm)) + ); + // Its jobs are the renamed one's, the rename among them, by its id and its name now. for job in [&before, &id] { let (_, found) = send(client, "GET", &format!("/v1/jobs/{job}"), Value::Null).await; - assert_eq!(found["subject"], "provisioner:lab", "{found}"); + assert_eq!(found["subject"], format!("provisioner:{vm}"), "{found}"); + assert_eq!(found["subject_name"], "provisioner:lab", "{found}"); } // They are listed by their subject, a page at a time. let listed = |found: &Value| -> Vec { @@ -2801,7 +2920,7 @@ async fn machines_are_created_and_deleted_through_the_api() { let paths = &served.state.paths; paths.prepare().unwrap(); // How the server reaches it is its own; the address comes from the provisioner. - berth_core::profiles::Place::own("studio") + berth_core::profiles::Place::own(paths, "studio") .unwrap() .save( paths, @@ -2818,9 +2937,10 @@ async fn machines_are_created_and_deleted_through_the_api() { .as_array() .unwrap() .iter() - .find(|known| known["id"] == "vm") + .find(|known| known["name"] == "vm") .unwrap() .clone(); + assert!(berth_api::ids::is_id(vm["id"].as_str().unwrap()), "{vm}"); assert_eq!(vm["missing_secrets"], json!(["api_token"])); assert_eq!(vm["kinds"][1]["actions"][1]["role"], "admin"); @@ -2832,7 +2952,13 @@ async fn machines_are_created_and_deleted_through_the_api() { assert_eq!(status, 412, "{said}"); assert!(message(&said).contains("api_token"), "{said}"); let secret = json!({"value": "tok-s3cret"}); - send(client, "PUT", "/v1/secrets/api_token", secret).await; + send( + client, + "PUT", + "/v1/secrets/provisioner%2Fvm%2Fapi_token", + secret, + ) + .await; for (body, wanted) in [ (json!({"provisioner": "vm", "host": "nobody"}), 404), (json!({"provisioner": "none", "host": "studio"}), 404), @@ -3048,7 +3174,7 @@ async fn a_deployment_keeps_its_spec_and_gets_its_secrets_only_on_standard_input send( client, "PUT", - "/v1/secrets/api_token", + "/v1/secrets/provisioner%2Fvm%2Fapi_token", json!({"value": "tok-s3cret"}), ) .await; @@ -3453,9 +3579,9 @@ async fn a_host_is_renamed_as_a_job_with_what_the_server_keeps_of_it() { let client = &mut client; let paths = &served.state.paths; let profile = json!({"hostname": "test", "builder": {}, "access": {"address": "192.0.2.7", "link": ["sh", "-c", "exit 1"]}}); - let test = berth_core::profiles::Place::own("test").unwrap(); + let test = berth_core::profiles::Place::own(paths, "test").unwrap(); test.save(paths, &profile).unwrap(); - berth_core::profiles::Place::own("lab") + berth_core::profiles::Place::own(paths, "lab") .unwrap() .save(paths, &profile) .unwrap(); @@ -3471,7 +3597,8 @@ async fn a_host_is_renamed_as_a_job_with_what_the_server_keeps_of_it() { }, time: berth_api::rfc3339(berth_api::unix_now()), }; - served.state.store.take_in("test", &message).unwrap(); + // Its messages are kept by its id. + served.state.store.take_in(&test.key(), &message).unwrap(); let (status, listed) = send(client, "GET", "/v1/hosts/test/actions", Value::Null).await; assert_eq!(status, 200, "{listed}"); let rename = listed @@ -3504,8 +3631,9 @@ async fn a_host_is_renamed_as_a_job_with_what_the_server_keeps_of_it() { let id = acted["job"]["id"].as_str().unwrap().to_owned(); let job = job_done(client, &id).await; assert_eq!(job["exit_code"], 0, "{job}"); - // The job is the renamed host's now, as its messages are. - assert_eq!(job["subject"], "host:studio"); + // The job is the renamed host's still, by its id, as its messages are. + assert_eq!(job["subject"], format!("host:{}", test.key())); + assert_eq!(job["subject_name"], "host:studio"); // The server knows it by its new name without being asked for its hosts. assert!(served.state.host("studio").is_some()); assert!(served.state.host("test").is_none()); @@ -3555,7 +3683,7 @@ async fn hosts_and_resources_that_change_are_told_on_the_events_stream() { // Saved by another berth beside the server: told when the server looks. let profile = json!({"hostname": "studio", "builder": {}, "access": {"address": "192.0.2.7", "link": ["sh", "-c", "exit 1"]}}); - berth_core::profiles::Place::own("studio") + berth_core::profiles::Place::own(paths, "studio") .unwrap() .save(paths, &profile) .unwrap(); @@ -3590,7 +3718,7 @@ async fn hosts_and_resources_that_change_are_told_on_the_events_stream() { send( client, "PUT", - "/v1/secrets/api_token", + "/v1/secrets/provisioner%2Fvm%2Fapi_token", json!({"value": "tok-made-up"}), ) .await; @@ -3605,9 +3733,13 @@ async fn hosts_and_resources_that_change_are_told_on_the_events_stream() { let (status, created) = send(client, "POST", "/v1/resources", asked).await; assert_eq!(status, 202, "{created}"); let id = created["resource"]["id"].as_str().unwrap().to_owned(); + let vm = berth_core::provisioner::id_of(paths, "vm") + .unwrap() + .unwrap(); assert_eq!( change_of(&mut events, "resource", "created").await, - json!({"source": "resource", "resource": id, "provisioner": "vm", "kind": "deployment", "change": "created"}) + json!({"source": "resource", "resource": id, "provisioner": "vm", "provisioner_id": vm, + "kind": "deployment", "change": "created"}) ); let (_, code) = job_ended(&served.socket, created["job"]["id"].as_str().unwrap()).await; assert_eq!(code, 0); @@ -3635,7 +3767,7 @@ async fn hosts_and_resources_that_change_are_told_on_the_events_stream() { .unwrap(); let follow = json!({"kv": ["ui"]}).to_string(); quiet.send(Frame::Text(follow.into())).await.unwrap(); - berth_core::profiles::Place::own("pier") + berth_core::profiles::Place::own(paths, "pier") .unwrap() .save(paths, &profile) .unwrap(); @@ -3688,7 +3820,7 @@ fn generated_passwords_are_kept_where_the_host_is_and_shown_to_an_admin() { (status, &kept["credentials"]["ops"]), (200, &json!(password)) ); - let own = Place::own("studio").unwrap(); + let own = Place::own(&served.state.paths, "studio").unwrap(); assert_eq!( credentials::load(&served.state.paths, &own).unwrap()["ops"], json!(password) @@ -3747,7 +3879,7 @@ async fn a_server_in_the_process_names_both_ways_to_give_a_sudo_password() { let mut state = state_in(dir.path(), None); Arc::get_mut(&mut state).unwrap().here = true; let profile = json!({"builder": {}, "access": {"address": "192.0.2.9", "user": "ops", "sudo": "password"}}); - berth_core::profiles::Place::own("studio") + berth_core::profiles::Place::own(&state.paths, "studio") .unwrap() .save(&state.paths, &profile) .unwrap(); @@ -3780,7 +3912,7 @@ async fn a_server_in_the_process_names_both_ways_to_give_a_sudo_password() { // berth serve says what it says to its own user. let other = tempfile::tempdir().unwrap(); let served = state_in(other.path(), None); - berth_core::profiles::Place::own("studio") + berth_core::profiles::Place::own(&served.paths, "studio") .unwrap() .save(&served.paths, &profile) .unwrap(); @@ -3805,7 +3937,7 @@ async fn a_caller_on_this_machine_takes_the_host_key_its_user_knows() { std::fs::write(&known_hosts, "192.0.2.8 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEdockdockdockdockdockdockdockdockdockdockdo\n").unwrap(); *state.known_hosts.lock().unwrap() = Some(known_hosts); for name in ["dock", "studio"] { - berth_core::profiles::Place::own(name) + berth_core::profiles::Place::own(&state.paths, name) .unwrap() .save( &state.paths, @@ -3867,7 +3999,7 @@ async fn first_contact_and_keys_are_actions_of_the_server() { // A host that is reached by a command that fails at once, so that what // needs no controller is told apart from what does, without waiting. let profile = json!({"builder": {}, "access": {"address": "192.0.2.7", "auth": "password", "link": ["sh", "-c", "exit 1"]}}); - berth_core::profiles::Place::own("studio") + berth_core::profiles::Place::own(&served.state.paths, "studio") .unwrap() .save(&served.state.paths, &profile) .unwrap(); @@ -3932,7 +4064,7 @@ async fn first_contact_and_keys_are_actions_of_the_server() { ); let (_, job) = send(client, "GET", &format!("/v1/jobs/{id}"), Value::Null).await; assert_eq!( - (&job["subject"], &job["action"]), + (&job["subject_name"], &job["action"]), (&json!("host:studio"), &json!("trust-key")) ); assert!(!job.to_string().contains("made-up-once-pw") && !output.contains("made-up-once-pw")); @@ -4131,7 +4263,7 @@ async fn a_host_seen_to_be_a_workstation_never_gets_the_controller_alone() { ) .unwrap(); let served = served().await; - berth_core::profiles::Place::own("box") + berth_core::profiles::Place::own(&served.state.paths, "box") .unwrap() .save( &served.state.paths, @@ -4186,7 +4318,7 @@ async fn a_host_whose_controller_does_not_answer_says_why_and_keeps_berths_own_a let said = "ssh: connect to host 192.0.2.11 port 22: Network is unreachable"; let profile = json!({"type": "managed", "access": {"address": "192.0.2.11", "link": ["sh", "-c", format!("echo '{said}' >&2; exit 255")]}}); - berth_core::profiles::Place::own("dock") + berth_core::profiles::Place::own(&served.state.paths, "dock") .unwrap() .save(&served.state.paths, &profile) .unwrap(); @@ -4264,7 +4396,7 @@ async fn a_host_removed_or_changed_in_the_store_by_another_process_is_not_listed let served = served().await; let mut client = served.client().await; let client = &mut client; - let place = berth_core::profiles::Place::own("studio").unwrap(); + let place = berth_core::profiles::Place::own(&served.state.paths, "studio").unwrap(); let paths = &served.state.paths; // A host whose machine is still being made has no address. place.save(paths, &json!({"builder": {}})).unwrap(); @@ -4297,7 +4429,7 @@ async fn the_installer_and_every_setting_are_for_the_user_here() { let dir = tempfile::tempdir().unwrap(); let state = state_in(dir.path(), Some("s3cret-token")); let profile = json!({"hostname": "studio", "builder": {}, "access": {"address": "192.0.2.7", "link": ["sh", "-c", "exit 1"]}}); - berth_core::profiles::Place::own("studio") + berth_core::profiles::Place::own(&state.paths, "studio") .unwrap() .save(&state.paths, &profile) .unwrap(); @@ -4416,7 +4548,7 @@ async fn made_box(served: &Served, fake: &std::path::Path, answer: Value) -> Str send( client, "PUT", - "/v1/secrets/api_token", + "/v1/secrets/provisioner%2Fvm%2Fapi_token", json!({"value": "tok-made-up"}), ) .await; @@ -4992,7 +5124,7 @@ async fn a_profile_does_not_say_how_ansible_reaches_the_host() { // A profile saved before still lists and reads. let paths = &served.state.paths; paths.prepare().unwrap(); - berth_core::profiles::Place::own("dock") + berth_core::profiles::Place::own(paths, "dock") .unwrap() .save( paths, @@ -5036,13 +5168,13 @@ async fn a_host_is_started_stopped_duplicated_and_removed_by_its_actions() { send( client, "PUT", - "/v1/secrets/api_token", + "/v1/secrets/provisioner%2Fvm%2Fapi_token", json!({"value": "tok-s3cret"}), ) .await; let paths = &served.state.paths; paths.prepare().unwrap(); - berth_core::profiles::Place::own("studio") + berth_core::profiles::Place::own(paths, "studio") .unwrap() .save( paths, @@ -5179,7 +5311,7 @@ async fn a_host_is_started_stopped_duplicated_and_removed_by_its_actions() { let planned = machine("lab").unwrap().record; assert_eq!( ( - planned.provisioner.as_str(), + berth_core::provisioner::label(paths, &planned.provisioner).as_str(), planned.state.as_str(), planned.external_id.as_str() ), @@ -5310,7 +5442,7 @@ async fn a_host_that_declares_its_controller_stops_it_asks_the_controller() { .await; assert_eq!(status, 400, "{said}"); assert!( - berth_core::profiles::Place::own("yard") + berth_core::profiles::Place::own(paths, "yard") .unwrap() .load(paths) .unwrap() @@ -5408,7 +5540,7 @@ async fn destroying_a_host_removes_its_device_from_the_tailnet_and_forgetting_it send( client, "PUT", - "/v1/secrets/api_token", + "/v1/secrets/provisioner%2Fvm%2Fapi_token", json!({"value": "tok-s3cret"}), ) .await; @@ -5426,7 +5558,7 @@ async fn destroying_a_host_removes_its_device_from_the_tailnet_and_forgetting_it ("studio", "vm-1", "192.0.2.20"), ("attic", "vm-2", "192.0.2.21"), ] { - let place = berth_core::profiles::Place::own(name).unwrap(); + let place = berth_core::profiles::Place::own(paths, name).unwrap(); place .save(paths, &json!({"hostname": name, "builder": {}, "access": {"user": "ops", "link": ["sh", "-c", "exit 1"]}})) .unwrap(); @@ -5444,9 +5576,11 @@ async fn destroying_a_host_removes_its_device_from_the_tailnet_and_forgetting_it if name == "studio" { "12" } else { "13" }, &format!("{name}.hs.example.org"), ); + // Kept by the host's id. + let id = berth_core::profiles::Place::own(paths, name).unwrap().key(); store::shared(paths) .unwrap() - .write_json(kind::TAILNET, store::OWN, name, &json!({"device": device})) + .write_json(kind::TAILNET, store::OWN, &id, &json!({"device": device})) .unwrap(); } asked.lock().unwrap().clear(); @@ -5514,7 +5648,7 @@ async fn an_apply_records_the_device_its_host_reports_on_the_tailnet() { 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 place = berth_core::profiles::Place::own(&paths, "studio").unwrap(); let config = json!({"hostname": "studio", "builder": {}, "access": {"user": "ops", "link": command}}); place.save(&paths, &config).unwrap(); diff --git a/crates/berth/src/local/store_command.rs b/crates/berth/src/local/store_command.rs index 5f67e26..bb7dcee 100644 --- a/crates/berth/src/local/store_command.rs +++ b/crates/berth/src/local/store_command.rs @@ -321,6 +321,10 @@ mod tests { input: String, } + /// The ids of the secret and the host every scratch store has. + const API: &str = "01972c4e8f3a7b219d4e5a6b7c8d9e0f"; + const STUDIO: &str = "01972c4e8f3a7b219d4e5a6b7c8d9e1f"; + fn scratch() -> Scratch { let dir = tempfile::tempdir().unwrap(); let paths = Paths { @@ -330,9 +334,17 @@ mod tests { // The settings of the files only, whatever the environment of the tests holds. let installation = Installation::with(&paths, &|key| paths.setting_with(key, None)); let store = installation.open().unwrap(); - store.write(kind::SECRET, "", "api", b"hunter2").unwrap(); store - .write(kind::PROFILE, "", "studio", b"{\"hostname\":\"studio\"}") + .write_labelled(kind::SECRET, "", API, "api", b"hunter2") + .unwrap(); + store + .write_labelled( + kind::PROFILE, + "", + STUDIO, + "studio", + b"{\"hostname\":\"studio\"}", + ) .unwrap(); Scratch { _dir: dir, @@ -394,7 +406,7 @@ mod tests { ); let store = s.installation.open().unwrap(); assert_eq!( - store.read(kind::SECRET, "", "api").unwrap().unwrap(), + store.read(kind::SECRET, "", API).unwrap().unwrap(), b"hunter2" ); drop(store); @@ -425,7 +437,7 @@ mod tests { ); let store = s.installation.open().unwrap(); assert_eq!( - store.read(kind::SECRET, "", "api").unwrap().unwrap(), + store.read(kind::SECRET, "", API).unwrap().unwrap(), b"hunter2" ); assert!(s.run(&["rekey"]).is_err()); @@ -475,7 +487,7 @@ mod tests { .open() .unwrap(); assert_eq!( - copied.read(kind::SECRET, "", "api").unwrap().unwrap(), + copied.read(kind::SECRET, "", API).unwrap().unwrap(), b"hunter2" ); assert!( @@ -570,7 +582,7 @@ mod tests { .set_secret("github", Some("ghp-made-up")) .unwrap(); for (name, address) in [("studio", "192.0.2.10"), ("attic", "192.0.2.11")] { - let place = Place::own(name).unwrap(); + let place = Place::own(paths, name).unwrap(); let profile = serde_json::json!({"hostname": name, "builder": {}, "access": {"address": address}}); place.save(paths, &profile).unwrap(); berth_core::credentials::save(paths, &place, "ops", &format!("pw-of-{name}")).unwrap(); @@ -583,7 +595,7 @@ mod tests { kind: "machine".into(), name: "studio".into(), external_id: "vm-1".into(), - host: "studio".into(), + host: Place::own(paths, "studio").unwrap().key(), token: "t0k".into(), ..Default::default() }, @@ -593,9 +605,10 @@ mod tests { // A key of its own in the ring, so no ssh-keygen is needed. berth_core::store::shared(paths) .unwrap() - .write( + .write_labelled( berth_core::store::kind::KEY, berth_core::store::OWN, + &berth_core::store::new_id(), "berth", br#"{"private": "made-up private key\n", "public": "ssh-ed25519 AAAA berth@studio"}"#, ) @@ -632,7 +645,7 @@ mod tests { ("studio".to_owned(), "192.0.2.10".to_owned()) ] ); - let studio = Place::own("studio").unwrap(); + let studio = Place::own(paths, "studio").unwrap(); assert_eq!(studio.load(paths).unwrap().unwrap()["hostname"], "studio"); assert_eq!( berth_core::credentials::load(paths, &studio).unwrap()["ops"], diff --git a/crates/berth/tests/simulate.rs b/crates/berth/tests/simulate.rs index 829cc1a..a026479 100644 --- a/crates/berth/tests/simulate.rs +++ b/crates/berth/tests/simulate.rs @@ -248,9 +248,12 @@ async fn a_host_lives_its_whole_life_on_a_simulated_server() { ); let provisioners = server.call("GET", "/v1/provisioners", None).await; assert_eq!( - (&provisioners[0]["id"], &provisioners[0]["type"]), + (&provisioners[0]["name"], &provisioners[0]["type"]), (&json!("sim"), &json!("sim/cloud")) ); + assert!(berth_api::ids::is_id( + provisioners[0]["id"].as_str().unwrap_or("") + )); let shown = server.call("GET", "/v1/provisioners/sim", None).await; assert!( shown.to_string().contains("ubuntu-26.04"), diff --git a/docs/content/docs/(guide)/deploy/provisioners.mdx b/docs/content/docs/(guide)/deploy/provisioners.mdx index 3cd5d9b..c3f4479 100644 --- a/docs/content/docs/(guide)/deploy/provisioners.mdx +++ b/docs/content/docs/(guide)/deploy/provisioners.mdx @@ -43,7 +43,7 @@ berth provisioner list ```text docker Docker container (docker/container) -ovh-incus Incus virtual machine (incus/vm) · not usable: ovh-incus needs the secrets incus_client_cert, incus_client_key: berth secret set NAME +ovh-incus Incus virtual machine (incus/vm) · not usable: ovh-incus needs the secrets incus_client_cert, incus_client_key: berth provisioner run ovh-incus secrets incus_client_cert=VALUE incus_client_key=VALUE ``` One that cannot be used says what to change, such as a [secret](#secrets) to set or a plugin to update. `berth provisioner list` lists the same with what each asks for: @@ -63,7 +63,7 @@ The listing is shortened here. A secret marked *not set* is required and has no In the terminal UI, `a` on the **Provisioners** tab adds one. It asks for the name and then the type. -`berth provisioner rename NAME NEW` gives a provisioner another name. Its settings, its own secrets, the resources and machines it made or plans, and its jobs go with it, and what it made is not touched. The action `rename` on its page does the same. +`berth provisioner rename NAME NEW` gives a provisioner another name. It keeps its [id](/docs/reference/ids). Its settings, its secrets, the resources and machines it made or plans, and its jobs stay with it, and what it made is not touched. The action `rename` on its page does the same. `berth provisioner remove NAME` forgets a provisioner, with its settings and its own secrets. berth refuses while it holds anything, so remove what it made first. The action `remove` on its page does the same. The action `adopt` makes one you registered the provisioner of a [node](/docs/use/nodes#take-in-a-server-that-exists) that reaches the same server. @@ -139,13 +139,13 @@ Every call to the provisioner uses them, and so does each machine made with it u ### Secrets -The plugin declares the secrets its provisioners need, such as `incus_client_cert`. berth gives each provisioner the secret of that name, which every provisioner of the type shares. One with secrets of its own is given those instead. Each is kept as the secret `provisioner/NAME/SECRET`, and `berth secret set` reads it from standard input: +The plugin declares the secrets its provisioners need, such as `incus_client_cert`. Each provisioner has its own, which belong to it: they keep with it through a rename and go when it is removed. berth gives a provisioner its own secrets alone, never a secret of the same name that is not its own. Each is set as the secret `provisioner/NAME/SECRET`, where NAME is the provisioner's name or id, and `berth secret set` reads it from standard input: ```bash berth secret set provisioner/ovh-incus/incus_server_cert < server.crt ``` -The action `secrets` on the provisioner's page takes each in a hidden field. An empty value keeps what it has, and `-` forgets it. The Secrets tab of the terminal UI leaves them out. This way an Incus server that trusts another certificate of berth gets its own. +The action `secrets` on the provisioner's page takes each in a hidden field. An empty value keeps what it has, and `-` forgets it. The Secrets tab of the terminal UI leaves them out. A provisioner a node offers gets the client certificate the node trusts as its own, at each apply of the node. ### Actions @@ -220,11 +220,11 @@ Make the profile as for any host. The provisioner works from the saved profile. ### Store the secrets -A provisioner that needs provider credentials lists them as secrets. Store them once: +A provisioner that needs provider credentials lists them as secrets. Store them as the provisioner's own: ```bash -berth secret set incus_client_cert < berth.crt -berth secret set incus_client_key < berth.key +berth secret set provisioner/ovh-incus/incus_client_cert < berth.crt +berth secret set provisioner/ovh-incus/incus_client_key < berth.key ``` berth refuses to start while a required secret has no value. @@ -365,7 +365,7 @@ berth host create studio-2 --provisioner docker runtime=sysbox A provisioner of the type `incus/vm` makes a virtual machine, and one of `incus/container` a system container, on an Incus server. Both are set up from the cloud seed, and the plugin puts the release archive into the machine. The profile does without a `release_url`. They offer `start`, `stop`, `change`, `logs` and `list`. In a container Chromium uses its setuid sandbox helper, and in a virtual machine a user namespace, as the [self-test](/docs/reference/on-the-host#the-self-test) describes. -berth talks to the Incus API over HTTPS. `server` is required, as a setting or for each machine. Incus must trust a certificate of berth, which you store as the secrets `incus_client_cert` and `incus_client_key`. A provisioner may have its own instead. The secret `incus_server_cert` is optional and pins the server's own certificate. `plugins/incus/guide.md` shows how to make and trust the certificate. +berth talks to the Incus API over HTTPS. `server` is required, as a setting or for each machine. Incus must trust a certificate of berth, which you store as the provisioner's own secrets `incus_client_cert` and `incus_client_key`. The secret `incus_server_cert` is optional and pins the server's own certificate. `plugins/incus/guide.md` shows how to make and trust the certificate. The page offers the server's pools, networks and instances for the arguments `pool`, `network` and `instance`. diff --git a/docs/content/docs/(guide)/reference/cli.mdx b/docs/content/docs/(guide)/reference/cli.mdx index 39e5e24..e8a2856 100644 --- a/docs/content/docs/(guide)/reference/cli.mdx +++ b/docs/content/docs/(guide)/reference/cli.mdx @@ -1010,6 +1010,21 @@ berth secret set NAME [--value-file FILE] | `NAME` | Its name. | | `--value-file FILE` | Read the value from FILE. | +### berth secret rename + +Give a secret another name. The profiles that hand it to hosts go on doing so. + +`berth secret mv` is the same. + +```text +berth secret rename NAME NEW +``` + +| Argument | What it is | +| --- | --- | +| `NAME` | The secret, by its name or its id. | +| `NEW` | Its new name. | + ### berth secret remove Remove a secret. @@ -1093,6 +1108,21 @@ berth registry check [NAME] | --- | --- | | `NAME` | Its name. | +### berth registry rename + +Give a registry another name. Its login and the hosts that use it stay with it. + +`berth registry mv` is the same. + +```text +berth registry rename NAME NEW +``` + +| Argument | What it is | +| --- | --- | +| `NAME` | The registry, by its name or its id. | +| `NEW` | Its new name. | + ### berth registry remove Remove a registry with its login. diff --git a/docs/content/docs/(guide)/reference/ids.mdx b/docs/content/docs/(guide)/reference/ids.mdx new file mode 100644 index 0000000..289d772 --- /dev/null +++ b/docs/content/docs/(guide)/reference/ids.mdx @@ -0,0 +1,95 @@ +--- +title: IDs and names +description: How berth tells hosts, provisioners, registries, secrets and keys apart, and what a name or an id finds in the API and on the command line. +icon: Fingerprint +--- + +Each host, provisioner, resource, registry, secret and key that berth keeps has an id beside its name. berth gives the id when the thing is made. It stays the same for as long as the thing exists, and berth gives it to nothing else afterwards. You can change the name, and berth finds the thing by either. + +## What has an id + +| Kind | Prefix | Example name | +| --- | --- | --- | +| Host | `host_` | `studio` | +| Provisioner | `prov_` | `incus-vm` | +| Resource | `res_` | `berth-studio` | +| Registry | `reg_` | `nexus` | +| Secret | `sec_` | `github_token` | +| Key | `key_` | `berth` | + +A plugin is known by its package name, such as `claude`. That name is already permanent, and profiles refer to a plugin's features by it on every installation, so a plugin has no id of its own. + +## The form of an id + +An id is a UUID, version 7, written as 32 lowercase hex characters without hyphens: + +```text +01972c4e8f3a7b219d4e5a6b7c8d9e0f +``` + +berth replies with this bare form, and keeps and logs it so. Wherever it takes an id, it also takes the id with the prefix of its kind, such as `prov_01972c4e8f3a7b219d4e5a6b7c8d9e0f`, and drops the prefix. A prefix of another kind is refused, so `host_…` given for a provisioner is an error. The hyphenated form of a UUID is taken too. + +A resource's id is berth's own, and is not the id its provisioner gives it, which a reply calls `external_id`. + +## Finding a thing by name or id + +An argument that refers to a host, provisioner, resource, registry, secret or key takes the name or the id. This is so in the paths of the API, in query parameters and in the fields of a request body. The command line and the screens take both too. berth reads a value in this order: + +1. A value with a prefix is an id of that kind. If there is no such thing, the reply is "not found", and berth does not try it as a name. +2. A value in the form of an id is that id, if there is a thing with it. +3. Anything else is a name. + +A name cannot be in the form of an id, so the two never meet. berth refuses a name of 32 hex characters in either case, with or without a prefix. It does so when a thing is made and when it is renamed. Names are unique among the things of one kind. A secret's name is unique among the global secrets, and among the secrets of its owner. + +## Replies + +A reply that describes a thing has `id` beside `name`. Where a reply refers to another thing by name, a field with that thing's id comes beside it: + +| Reply | Field | Added | +| --- | --- | --- | +| A host | `name` | `id` | +| A provisioner | `node` | `name`, `node_id` | +| A resource | `provisioner`, `host` | `provisioner_id`, `host_id` | +| A registry | `name` | `id` | +| `secrets.list` | `names` | `secrets`, a list of `{id, name}` | +| A key | `name` | `id` | +| A job | `host` | `subject_name`, `host_id` | +| A message of the inbox | `host` | `host_id` | + +A provisioner's `id` was its name before berth had ids, and is its id now. Its name is in `name`. + +A job's `subject` is what it is about by id, such as `provisioner:ID`, which stays the same through renames. Its `subject_name` says the same as it is called now, such as `provisioner:incus-vm`. A job about a thing that is gone gives its id in both. The filters of `jobs.list` take either. + +## Renaming and removing + +A rename changes the label alone. Everything that belongs to the thing stays with it, and everything that refers to it goes on finding it: + +| Rename | How | What stays attached | +| --- | --- | --- | +| A host | its action `rename` | its machine, passwords, SSH keys, messages, jobs and tailnet device | +| A provisioner | its action `rename` | its settings, secrets, node and resources, and its jobs | +| A registry | `registries.rename`, `berth registry rename` | its login, and the hosts that use it | +| A secret | `secrets.rename`, `berth secret rename` | the profiles that hand it to hosts | + +Removing a thing removes everything it owns. berth still refuses to remove what something else needs, such as a provisioner that holds resources. + +## Global and owned secrets + +A secret is either global or owned. + +- A **global** secret has no owner, such as `tailnet_api_key` or a secret a profile hands to its host. `secrets.list` lists these. +- An **owned** secret belongs to one thing, such as a provisioner's server certificate or a registry's login. It follows its owner through a rename and goes when its owner is removed. You set it through its owner, such as the provisioner's action `secrets`. + +A provisioner uses its own secrets alone. It is never given a global secret that has the same name as one of its own. + +## Profiles + +In a profile you see or edit, registries and secrets and keys are given by name. That is so in `berth host edit` and `berth host export`, in `hosts.profile`, in profile files and in the profile a host keeps of itself. berth keeps their ids, and turns names into ids when a profile is saved. A list in a profile that is saved may also give an id in place of a name. + +The profile a host keeps of itself carries the host's id. `berth host adopt` gives the host that id again when no other host here has it, so jobs and messages of the host find it once more. + +## After an upgrade + +The first start of a berth with ids converts the store once, after the copy of the store berth makes before converting it. It gives every thing an id, and moves the secrets that belong to a provisioner, a registry or a host under their owners. + +A provisioner that had no certificate of its own and used a global secret of the same name gets a copy of that secret as its own. The global one goes when nothing else uses it. An older berth cannot read the converted store, so going back needs that copy. diff --git a/docs/content/docs/(guide)/reference/meta.json b/docs/content/docs/(guide)/reference/meta.json index 7af1de5..f57ebe8 100644 --- a/docs/content/docs/(guide)/reference/meta.json +++ b/docs/content/docs/(guide)/reference/meta.json @@ -1,5 +1,5 @@ { "title": "Reference", "icon": "Library", - "pages": ["cli", "tools", "graphics", "box", "on-the-host", "profile", "validation", "building"] + "pages": ["cli", "tools", "graphics", "box", "on-the-host", "profile", "ids", "validation", "building"] } diff --git a/docs/openapi/berth.openapi.json b/docs/openapi/berth.openapi.json index 07d19ee..04cdbd6 100644 --- a/docs/openapi/berth.openapi.json +++ b/docs/openapi/berth.openapi.json @@ -1090,6 +1090,10 @@ "null" ] }, + "id": { + "description": "Its id, which stays when it is renamed; empty for a host that is not saved.", + "type": "string" + }, "machine": { "oneOf": [ { @@ -1218,6 +1222,11 @@ "type": "boolean" }, "host": { + "description": "The host's name.", + "type": "string" + }, + "host_id": { + "description": "The host's id, when it is a saved host.", "type": "string" }, "seq": { @@ -1311,6 +1320,10 @@ "description": "The hosts it is about, for a reader: a name, or several with commas.", "type": "string" }, + "host_id": { + "description": "The ids of those hosts, as `host` lists them; a host in a file by its path.", + "type": "string" + }, "id": { "type": "string" }, @@ -1336,7 +1349,11 @@ ] }, "subject": { - "description": "What it was done to: `host:NAME`, `provisioner:NAME`,\n`resource:ID` or `installation`. Empty for a job from before 0.5.", + "description": "What it was done to, by id: `host:ID`, `provisioner:ID`,\n`resource:ID` or `installation`. Empty for a job from before 0.5.", + "type": "string" + }, + "subject_name": { + "description": "What it was done to, by name as it is called now: `host:NAME`,\n`provisioner:NAME`, `resource:ID` or `installation`; by id for one\nthat is gone.", "type": "string" } }, @@ -1407,6 +1424,10 @@ }, "type": "array" }, + "id": { + "description": "Its id; empty for the key that replaces another while it rotates.", + "type": "string" + }, "name": { "type": "string" }, @@ -1680,6 +1701,23 @@ ], "type": "object" }, + "Named": { + "description": "A thing berth keeps, by its id and its name.", + "properties": { + "id": { + "description": "Its id, which stays when it is renamed.", + "type": "string" + }, + "name": { + "type": "string" + } + }, + "required": [ + "id", + "name" + ], + "type": "object" + }, "NewResource": { "description": "What to create a resource with.", "properties": { @@ -1709,7 +1747,7 @@ "type": "string" }, "provisioner": { - "description": "The provisioner's name.", + "description": "The provisioner, by name or id.", "type": "string" }, "secrets": { @@ -2238,7 +2276,7 @@ "type": "string" }, "id": { - "description": "The name it is registered under, such as `ovh-incus`. In the list of\ntypes, the type itself.", + "description": "Its id, which stays when it is renamed. In the list of types, the\ntype itself.", "type": "string" }, "kinds": { @@ -2258,8 +2296,16 @@ }, "type": "array" }, + "name": { + "description": "The name it is registered under, such as `ovh-incus`. In the list of\ntypes, the type itself.", + "type": "string" + }, "node": { - "description": "The node it lives on, when a feature of that host offered it.", + "description": "The node it lives on, when a feature of that host offered it, by name.", + "type": "string" + }, + "node_id": { + "description": "The id of that node.", "type": "string" }, "own": { @@ -2459,7 +2505,7 @@ "properties": { "registries": { "additionalProperties": {}, - "description": "By name: `type` (nexus, npm, raw or docker), `url`, `username` and, for\na Nexus server, its `repositories`.", + "description": "By name: its `id`, `type` (nexus, npm, raw or docker), `url`,\n`username` and, for a Nexus server, its `repositories`. The id is\nberth's, and is left out of what is saved.", "propertyNames": { "type": "string" }, @@ -2471,6 +2517,18 @@ ], "type": "object" }, + "Rename": { + "description": "A new name for a thing.", + "properties": { + "name": { + "type": "string" + } + }, + "required": [ + "name" + ], + "type": "object" + }, "ReplyFrame": { "description": "The serving program's answer.", "properties": { @@ -2550,12 +2608,16 @@ "type": "string" }, "host": { - "description": "The host a machine belongs to, if any.", + "description": "The host a machine belongs to, if any, by name; a profile in a file\nby its path.", "type": [ "string", "null" ] }, + "host_id": { + "description": "The id of that host, when it is a saved host.", + "type": "string" + }, "id": { "description": "berth's id of it, stable for the life of the record.", "type": "string" @@ -2576,6 +2638,10 @@ "description": "The name of the provisioner that made it.", "type": "string" }, + "provisioner_id": { + "description": "The id of that provisioner, when it is registered.", + "type": "string" + }, "secrets": { "description": "The berth secrets it is given, by name; never their values.", "items": { @@ -2844,15 +2910,23 @@ "description": "The names of the secrets the server keeps. Their values never leave it\nexcept to a host.", "properties": { "names": { - "description": "A registry's password or token is `registry/NAME`.", + "description": "The global secrets' names, and the addresses of those that belong to\nsomething: a registry's password or token is `registry/NAME`, the\npasswords berth logs in to a host with `ssh/HOST` and `sudo/HOST`,\nand a provisioner's own `provisioner/NAME/SECRET`.", "items": { "type": "string" }, "type": "array" + }, + "secrets": { + "description": "The global secrets, by name, with their ids.", + "items": { + "$ref": "#/components/schemas/Named" + }, + "type": "array" } }, "required": [ - "names" + "names", + "secrets" ], "type": "object" }, @@ -3610,7 +3684,7 @@ "operationId": "remove_host", "parameters": [ { - "description": "A saved host", + "description": "A saved host, by its name or its id (bare or as `host_ID`)", "in": "path", "name": "name", "required": true, @@ -3660,7 +3734,7 @@ "operationId": "host", "parameters": [ { - "description": "A saved host", + "description": "A saved host, by its name or its id (bare or as `host_ID`)", "in": "path", "name": "name", "required": true, @@ -3702,7 +3776,7 @@ "operationId": "host_actions", "parameters": [ { - "description": "A saved host", + "description": "A saved host, by its name or its id (bare or as `host_ID`)", "in": "path", "name": "name", "required": true, @@ -3747,7 +3821,7 @@ "operationId": "host_act", "parameters": [ { - "description": "A saved host", + "description": "A saved host, by its name or its id (bare or as `host_ID`)", "in": "path", "name": "name", "required": true, @@ -5734,7 +5808,7 @@ "operationId": "credentials", "parameters": [ { - "description": "A saved host", + "description": "A saved host, by its name or its id (bare or as `host_ID`)", "in": "path", "name": "name", "required": true, @@ -5776,7 +5850,7 @@ "operationId": "profile", "parameters": [ { - "description": "A saved host", + "description": "A saved host, by its name or its id (bare or as `host_ID`)", "in": "path", "name": "name", "required": true, @@ -5814,7 +5888,7 @@ "operationId": "save_profile", "parameters": [ { - "description": "The name to save it under: letters, digits, `-`, `_` and `.`", + "description": "The name to save it under: letters, digits, `-`, `_` and `.`, and not in the shape of an id; or the id of a saved host", "in": "path", "name": "name", "required": true, @@ -5938,7 +6012,7 @@ "operationId": "mark_open", "parameters": [ { - "description": "The host it came from", + "description": "The host it came from, by its name or its id", "in": "path", "name": "host", "required": true, @@ -5989,7 +6063,7 @@ "operationId": "mark_done", "parameters": [ { - "description": "The host it came from", + "description": "The host it came from, by its name or its id", "in": "path", "name": "host", "required": true, @@ -6155,7 +6229,7 @@ "operationId": "jobs", "parameters": [ { - "description": "Only the jobs of this subject: `host:NAME`, `provisioner:NAME`, `resource:ID` or `installation`.", + "description": "Only the jobs of this subject: `host:HOST`, `provisioner:PROVISIONER`, `resource:ID` or `installation`, each by name or id.", "in": "query", "name": "subject", "required": false, @@ -6164,7 +6238,7 @@ } }, { - "description": "Only the jobs about this host, alone or among others, as a roll-out is.", + "description": "Only the jobs about this host, by name or id, alone or among others, as a roll-out is.", "in": "query", "name": "host", "required": false, @@ -6472,7 +6546,7 @@ "operationId": "remove", "parameters": [ { - "description": "The key", + "description": "The key, by its name or its id (bare or as `key_ID`)", "in": "path", "name": "name", "required": true, @@ -6522,7 +6596,7 @@ "operationId": "add", "parameters": [ { - "description": "The key's name: letters, digits, - and _", + "description": "The key's name: letters, digits, - and _, and not in the shape of an id", "in": "path", "name": "name", "required": true, @@ -6590,7 +6664,7 @@ "operationId": "rotate", "parameters": [ { - "description": "The key", + "description": "The key, by its name or its id (bare or as `key_ID`)", "in": "path", "name": "name", "required": true, @@ -7188,7 +7262,7 @@ "operationId": "remove", "parameters": [ { - "description": "Its name", + "description": "Its name or its id (bare or as `prov_ID`)", "in": "path", "name": "name", "required": true, @@ -7231,7 +7305,7 @@ "operationId": "provisioner", "parameters": [ { - "description": "The provisioner's name", + "description": "The provisioner, by its name or its id (bare or as `prov_ID`)", "in": "path", "name": "name", "required": true, @@ -7288,7 +7362,7 @@ "operationId": "save", "parameters": [ { - "description": "Its name: lowercase letters, digits, - and _, starting with a letter", + "description": "Its name: lowercase letters, digits, - and _, starting with a letter, and not in the shape of an id; or the id of a registered one", "in": "path", "name": "name", "required": true, @@ -7340,7 +7414,7 @@ "operationId": "provisioner_actions", "parameters": [ { - "description": "The provisioner's name", + "description": "The provisioner, by its name or its id (bare or as `prov_ID`)", "in": "path", "name": "name", "required": true, @@ -7385,7 +7459,7 @@ "operationId": "provisioner_act", "parameters": [ { - "description": "The provisioner's name", + "description": "The provisioner, by its name or its id (bare or as `prov_ID`)", "in": "path", "name": "name", "required": true, @@ -7534,7 +7608,7 @@ "operationId": "remove_registry", "parameters": [ { - "description": "A registry", + "description": "A registry, by its name or its id (bare or as `reg_ID`)", "in": "path", "name": "name", "required": true, @@ -7574,7 +7648,7 @@ "operationId": "save_registry", "parameters": [ { - "description": "The registry's name here", + "description": "The registry's name here, or the id of one there is", "in": "path", "name": "name", "required": true, @@ -7625,7 +7699,7 @@ "operationId": "check_registry", "parameters": [ { - "description": "A registry", + "description": "A registry, by its name or its id (bare or as `reg_ID`)", "in": "path", "name": "name", "required": true, @@ -7662,6 +7736,79 @@ ] } }, + "/v1/registries/{name}/rename": { + "post": { + "operationId": "rename_registry", + "parameters": [ + { + "description": "The registry, by its name or its id (bare or as `reg_ID`)", + "in": "path", + "name": "name", + "required": true, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Rename" + } + } + }, + "description": "The new name", + "required": true + }, + "responses": { + "200": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Done" + } + } + }, + "description": "Renamed, with its login: the hosts that use it refer to it still" + }, + "400": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ErrorBody" + } + } + }, + "description": "Not a name for a registry" + }, + "404": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ErrorBody" + } + } + }, + "description": "No such registry" + }, + "409": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ErrorBody" + } + } + }, + "description": "The name is taken" + } + }, + "summary": "Give a registry another name.", + "tags": [ + "set-up" + ] + } + }, "/v1/resources": { "get": { "operationId": "list", @@ -7780,7 +7927,7 @@ "operationId": "remove", "parameters": [ { - "description": "berth's id of the resource", + "description": "berth's id of the resource, bare or as `res_ID`", "in": "path", "name": "id", "required": true, @@ -7849,7 +7996,7 @@ "operationId": "get", "parameters": [ { - "description": "berth's id of the resource", + "description": "berth's id of the resource, bare or as `res_ID`", "in": "path", "name": "id", "required": true, @@ -7889,7 +8036,7 @@ "operationId": "change", "parameters": [ { - "description": "berth's id of the resource", + "description": "berth's id of the resource, bare or as `res_ID`", "in": "path", "name": "id", "required": true, @@ -7972,7 +8119,7 @@ "operationId": "actions", "parameters": [ { - "description": "berth's id of the resource", + "description": "berth's id of the resource, bare or as `res_ID`", "in": "path", "name": "id", "required": true, @@ -8017,7 +8164,7 @@ "operationId": "act", "parameters": [ { - "description": "berth's id of the resource", + "description": "berth's id of the resource, bare or as `res_ID`", "in": "path", "name": "id", "required": true, @@ -8160,7 +8307,7 @@ "operationId": "logs", "parameters": [ { - "description": "berth's id of the resource", + "description": "berth's id of the resource, bare or as `res_ID`", "in": "path", "name": "id", "required": true, @@ -8247,7 +8394,7 @@ "operationId": "remove_secret", "parameters": [ { - "description": "The secret", + "description": "The secret, by its name or its id (bare or as `sec_ID`), or the address of one that belongs to something", "in": "path", "name": "name", "required": true, @@ -8287,7 +8434,7 @@ "operationId": "set_secret", "parameters": [ { - "description": "The secret; `registry/NAME` is a registry's login", + "description": "The secret, by its name or its id (bare or as `sec_ID`); `registry/REGISTRY` is a registry's login, `ssh/HOST` and `sudo/HOST` a host's passwords and `provisioner/PROVISIONER/SECRET` a provisioner's own", "in": "path", "name": "name", "required": true, @@ -8335,6 +8482,79 @@ ] } }, + "/v1/secrets/{name}/rename": { + "post": { + "operationId": "rename_secret", + "parameters": [ + { + "description": "The secret, by its name or its id (bare or as `sec_ID`)", + "in": "path", + "name": "name", + "required": true, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Rename" + } + } + }, + "description": "The new name", + "required": true + }, + "responses": { + "200": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Done" + } + } + }, + "description": "Renamed: the profiles that hand it to hosts refer to it still" + }, + "400": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ErrorBody" + } + } + }, + "description": "Not a name for a secret, or one that belongs to something" + }, + "404": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ErrorBody" + } + } + }, + "description": "No such secret" + }, + "409": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ErrorBody" + } + } + }, + "description": "The name is taken" + } + }, + "summary": "Give a global secret another name.", + "tags": [ + "set-up" + ] + } + }, "/v1/settings": { "get": { "operationId": "settings", diff --git a/docs/openapi/berth.operations.json b/docs/openapi/berth.operations.json index 58f10c9..668be6e 100644 --- a/docs/openapi/berth.operations.json +++ b/docs/openapi/berth.operations.json @@ -836,6 +836,10 @@ "null" ] }, + "id": { + "description": "Its id, which stays when it is renamed; empty for a host that is not saved.", + "type": "string" + }, "machine": { "oneOf": [ { @@ -964,6 +968,11 @@ "type": "boolean" }, "host": { + "description": "The host's name.", + "type": "string" + }, + "host_id": { + "description": "The host's id, when it is a saved host.", "type": "string" }, "seq": { @@ -1013,6 +1022,10 @@ "description": "The hosts it is about, for a reader: a name, or several with commas.", "type": "string" }, + "host_id": { + "description": "The ids of those hosts, as `host` lists them; a host in a file by its path.", + "type": "string" + }, "id": { "type": "string" }, @@ -1038,7 +1051,11 @@ ] }, "subject": { - "description": "What it was done to: `host:NAME`, `provisioner:NAME`,\n`resource:ID` or `installation`. Empty for a job from before 0.5.", + "description": "What it was done to, by id: `host:ID`, `provisioner:ID`,\n`resource:ID` or `installation`. Empty for a job from before 0.5.", + "type": "string" + }, + "subject_name": { + "description": "What it was done to, by name as it is called now: `host:NAME`,\n`provisioner:NAME`, `resource:ID` or `installation`; by id for one\nthat is gone.", "type": "string" } }, @@ -1070,6 +1087,10 @@ }, "type": "array" }, + "id": { + "description": "Its id; empty for the key that replaces another while it rotates.", + "type": "string" + }, "name": { "type": "string" }, @@ -1233,6 +1254,23 @@ ], "type": "object" }, + "Named": { + "description": "A thing berth keeps, by its id and its name.", + "properties": { + "id": { + "description": "Its id, which stays when it is renamed.", + "type": "string" + }, + "name": { + "type": "string" + } + }, + "required": [ + "id", + "name" + ], + "type": "object" + }, "NewResource": { "description": "What to create a resource with.", "properties": { @@ -1262,7 +1300,7 @@ "type": "string" }, "provisioner": { - "description": "The provisioner's name.", + "description": "The provisioner, by name or id.", "type": "string" }, "secrets": { @@ -1751,7 +1789,7 @@ "type": "string" }, "id": { - "description": "The name it is registered under, such as `ovh-incus`. In the list of\ntypes, the type itself.", + "description": "Its id, which stays when it is renamed. In the list of types, the\ntype itself.", "type": "string" }, "kinds": { @@ -1771,8 +1809,16 @@ }, "type": "array" }, + "name": { + "description": "The name it is registered under, such as `ovh-incus`. In the list of\ntypes, the type itself.", + "type": "string" + }, "node": { - "description": "The node it lives on, when a feature of that host offered it.", + "description": "The node it lives on, when a feature of that host offered it, by name.", + "type": "string" + }, + "node_id": { + "description": "The id of that node.", "type": "string" }, "own": { @@ -1933,7 +1979,7 @@ "properties": { "registries": { "additionalProperties": {}, - "description": "By name: `type` (nexus, npm, raw or docker), `url`, `username` and, for\na Nexus server, its `repositories`.", + "description": "By name: its `id`, `type` (nexus, npm, raw or docker), `url`,\n`username` and, for a Nexus server, its `repositories`. The id is\nberth's, and is left out of what is saved.", "propertyNames": { "type": "string" }, @@ -1945,6 +1991,18 @@ ], "type": "object" }, + "Rename": { + "description": "A new name for a thing.", + "properties": { + "name": { + "type": "string" + } + }, + "required": [ + "name" + ], + "type": "object" + }, "Resource": { "description": "Something a provisioner made for berth: a machine, which may be a\nhost's, or a kind its plugin names, such as a deployment.", "properties": { @@ -1980,12 +2038,16 @@ "type": "string" }, "host": { - "description": "The host a machine belongs to, if any.", + "description": "The host a machine belongs to, if any, by name; a profile in a file\nby its path.", "type": [ "string", "null" ] }, + "host_id": { + "description": "The id of that host, when it is a saved host.", + "type": "string" + }, "id": { "description": "berth's id of it, stable for the life of the record.", "type": "string" @@ -2006,6 +2068,10 @@ "description": "The name of the provisioner that made it.", "type": "string" }, + "provisioner_id": { + "description": "The id of that provisioner, when it is registered.", + "type": "string" + }, "secrets": { "description": "The berth secrets it is given, by name; never their values.", "items": { @@ -2203,15 +2269,23 @@ "description": "The names of the secrets the server keeps. Their values never leave it\nexcept to a host.", "properties": { "names": { - "description": "A registry's password or token is `registry/NAME`.", + "description": "The global secrets' names, and the addresses of those that belong to\nsomething: a registry's password or token is `registry/NAME`, the\npasswords berth logs in to a host with `ssh/HOST` and `sudo/HOST`,\nand a provisioner's own `provisioner/NAME/SECRET`.", "items": { "type": "string" }, "type": "array" + }, + "secrets": { + "description": "The global secrets, by name, with their ids.", + "items": { + "$ref": "#/components/schemas/Named" + }, + "type": "array" } }, "required": [ - "names" + "names", + "secrets" ], "type": "object" }, @@ -2666,7 +2740,7 @@ { "args": [ { - "description": "A saved host", + "description": "A saved host, by its name or its id (bare or as `host_ID`)", "in": "path", "name": "name", "required": true @@ -2697,7 +2771,7 @@ { "args": [ { - "description": "A saved host", + "description": "A saved host, by its name or its id (bare or as `host_ID`)", "in": "path", "name": "name", "required": true @@ -2732,7 +2806,7 @@ { "args": [ { - "description": "A saved host", + "description": "A saved host, by its name or its id (bare or as `host_ID`)", "in": "path", "name": "name", "required": true @@ -2761,7 +2835,7 @@ { "args": [ { - "description": "The name to save it under: letters, digits, `-`, `_` and `.`", + "description": "The name to save it under: letters, digits, `-`, `_` and `.`, and not in the shape of an id; or the id of a saved host", "in": "path", "name": "name", "required": true @@ -2796,7 +2870,7 @@ { "args": [ { - "description": "A saved host", + "description": "A saved host, by its name or its id (bare or as `host_ID`)", "in": "path", "name": "name", "required": true @@ -2827,7 +2901,7 @@ { "args": [ { - "description": "A saved host", + "description": "A saved host, by its name or its id (bare or as `host_ID`)", "in": "path", "name": "name", "required": true @@ -2861,7 +2935,7 @@ { "args": [ { - "description": "A saved host", + "description": "A saved host, by its name or its id (bare or as `host_ID`)", "in": "path", "name": "name", "required": true @@ -3022,7 +3096,7 @@ { "args": [ { - "description": "The secret; `registry/NAME` is a registry's login", + "description": "The secret, by its name or its id (bare or as `sec_ID`); `registry/REGISTRY` is a registry's login, `ssh/HOST` and `sudo/HOST` a host's passwords and `provisioner/PROVISIONER/SECRET` a provisioner's own", "in": "path", "name": "name", "required": true @@ -3059,7 +3133,7 @@ { "args": [ { - "description": "The secret", + "description": "The secret, by its name or its id (bare or as `sec_ID`), or the address of one that belongs to something", "in": "path", "name": "name", "required": true @@ -3087,6 +3161,51 @@ "role": "admin", "summary": "Remove a secret." }, + { + "args": [ + { + "description": "The secret, by its name or its id (bare or as `sec_ID`)", + "in": "path", + "name": "name", + "required": true + } + ], + "data": { + "description": "The new name", + "schema": { + "$ref": "#/components/schemas/Rename" + } + }, + "errors": [ + { + "description": "Not a name for a secret, or one that belongs to something", + "status": "400" + }, + { + "description": "No such secret", + "status": "404" + }, + { + "description": "The name is taken", + "status": "409" + } + ], + "group": "secrets", + "http": { + "method": "POST", + "path": "/v1/secrets/{name}/rename" + }, + "kind": "call", + "name": "secrets.rename", + "portal": true, + "result": { + "schema": { + "$ref": "#/components/schemas/Done" + } + }, + "role": "admin", + "summary": "Give a global secret another name." + }, { "args": [], "group": "keys", @@ -3108,7 +3227,7 @@ { "args": [ { - "description": "The key's name: letters, digits, - and _", + "description": "The key's name: letters, digits, - and _, and not in the shape of an id", "in": "path", "name": "name", "required": true @@ -3146,7 +3265,7 @@ { "args": [ { - "description": "The key", + "description": "The key, by its name or its id (bare or as `key_ID`)", "in": "path", "name": "name", "required": true @@ -3181,7 +3300,7 @@ { "args": [ { - "description": "The key", + "description": "The key, by its name or its id (bare or as `key_ID`)", "in": "path", "name": "name", "required": true @@ -3234,7 +3353,7 @@ { "args": [ { - "description": "The registry's name here", + "description": "The registry's name here, or the id of one there is", "in": "path", "name": "name", "required": true @@ -3269,7 +3388,7 @@ { "args": [ { - "description": "A registry", + "description": "A registry, by its name or its id (bare or as `reg_ID`)", "in": "path", "name": "name", "required": true @@ -3300,7 +3419,52 @@ { "args": [ { - "description": "A registry", + "description": "The registry, by its name or its id (bare or as `reg_ID`)", + "in": "path", + "name": "name", + "required": true + } + ], + "data": { + "description": "The new name", + "schema": { + "$ref": "#/components/schemas/Rename" + } + }, + "errors": [ + { + "description": "Not a name for a registry", + "status": "400" + }, + { + "description": "No such registry", + "status": "404" + }, + { + "description": "The name is taken", + "status": "409" + } + ], + "group": "registries", + "http": { + "method": "POST", + "path": "/v1/registries/{name}/rename" + }, + "kind": "call", + "name": "registries.rename", + "portal": true, + "result": { + "schema": { + "$ref": "#/components/schemas/Done" + } + }, + "role": "admin", + "summary": "Give a registry another name." + }, + { + "args": [ + { + "description": "A registry, by its name or its id (bare or as `reg_ID`)", "in": "path", "name": "name", "required": true @@ -3654,7 +3818,7 @@ { "args": [ { - "description": "The provisioner's name", + "description": "The provisioner, by its name or its id (bare or as `prov_ID`)", "in": "path", "name": "name", "required": true @@ -3692,7 +3856,7 @@ { "args": [ { - "description": "Its name: lowercase letters, digits, - and _, starting with a letter", + "description": "Its name: lowercase letters, digits, - and _, starting with a letter, and not in the shape of an id; or the id of a registered one", "in": "path", "name": "name", "required": true @@ -3729,7 +3893,7 @@ { "args": [ { - "description": "Its name", + "description": "Its name or its id (bare or as `prov_ID`)", "in": "path", "name": "name", "required": true @@ -3760,7 +3924,7 @@ { "args": [ { - "description": "The provisioner's name", + "description": "The provisioner, by its name or its id (bare or as `prov_ID`)", "in": "path", "name": "name", "required": true @@ -3794,7 +3958,7 @@ { "args": [ { - "description": "The provisioner's name", + "description": "The provisioner, by its name or its id (bare or as `prov_ID`)", "in": "path", "name": "name", "required": true @@ -3941,7 +4105,7 @@ { "args": [ { - "description": "berth's id of the resource", + "description": "berth's id of the resource, bare or as `res_ID`", "in": "path", "name": "id", "required": true @@ -3972,7 +4136,7 @@ { "args": [ { - "description": "berth's id of the resource", + "description": "berth's id of the resource, bare or as `res_ID`", "in": "path", "name": "id", "required": true @@ -4017,7 +4181,7 @@ { "args": [ { - "description": "berth's id of the resource", + "description": "berth's id of the resource, bare or as `res_ID`", "in": "path", "name": "id", "required": true @@ -4059,7 +4223,7 @@ { "args": [ { - "description": "berth's id of the resource", + "description": "berth's id of the resource, bare or as `res_ID`", "in": "path", "name": "id", "required": true @@ -4093,7 +4257,7 @@ { "args": [ { - "description": "berth's id of the resource", + "description": "berth's id of the resource, bare or as `res_ID`", "in": "path", "name": "id", "required": true @@ -4163,7 +4327,7 @@ { "args": [ { - "description": "berth's id of the resource", + "description": "berth's id of the resource, bare or as `res_ID`", "in": "path", "name": "id", "required": true @@ -4309,14 +4473,14 @@ { "args": [ { - "description": "Only the jobs of this subject: `host:NAME`, `provisioner:NAME`, `resource:ID` or `installation`.", + "description": "Only the jobs of this subject: `host:HOST`, `provisioner:PROVISIONER`, `resource:ID` or `installation`, each by name or id.", "header": null, "in": "query", "name": "subject", "required": false }, { - "description": "Only the jobs about this host, alone or among others, as a roll-out is.", + "description": "Only the jobs about this host, by name or id, alone or among others, as a roll-out is.", "header": null, "in": "query", "name": "host", @@ -4666,7 +4830,7 @@ { "args": [ { - "description": "The host it came from", + "description": "The host it came from, by its name or its id", "in": "path", "name": "host", "required": true @@ -4703,7 +4867,7 @@ { "args": [ { - "description": "The host it came from", + "description": "The host it came from, by its name or its id", "in": "path", "name": "host", "required": true diff --git a/tests/e2e/e2e_adopt.py b/tests/e2e/e2e_adopt.py index db16740..783d1f5 100644 --- a/tests/e2e/e2e_adopt.py +++ b/tests/e2e/e2e_adopt.py @@ -42,7 +42,8 @@ def setUpClass(cls): def test_the_host_keeps_its_profile_for_root_only(self): self.assertEqual(self.station.root_on_host(f'stat -c %a:%U:%G {KEPT}').stdout.strip(), '600:root:root') self.assertNotEqual(self.station.ssh(OPERATOR, f'cat {KEPT}', check=False).returncode, 0) - self.assertEqual((self.kept['format'], self.kept['host']), (3, HOST)) + self.assertEqual((self.kept['format'], self.kept['host']), (4, HOST)) + self.assertRegex(self.kept['id'], r'^[0-9a-f]{32}$') self.assertRegex(self.kept['written'], r'^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ$') saved = self.station.read_profile() kept = self.kept['profile'] diff --git a/tests/e2e/e2e_node.py b/tests/e2e/e2e_node.py index 0a8abaa..288c684 100644 --- a/tests/e2e/e2e_node.py +++ b/tests/e2e/e2e_node.py @@ -138,7 +138,7 @@ def provisioners(self): listed = json.loads(self.berth('provisioner', 'list', '-o', 'json').stdout) if isinstance(listed, dict): listed = listed['provisioners'] - return [provisioner['id'] for provisioner in listed] + return [provisioner['name'] for provisioner in listed] def test_1_a_feature_for_a_workstation_is_refused_before_anything_runs(self): added = self.station.read_profile() diff --git a/tests/e2e/e2e_provisioners.py b/tests/e2e/e2e_provisioners.py index 27ec78c..24e6ac1 100644 --- a/tests/e2e/e2e_provisioners.py +++ b/tests/e2e/e2e_provisioners.py @@ -142,7 +142,7 @@ def berth(self, *args, **kwargs): return self.station.berth(*args, **kwargs) def test_a_provisioner_is_listed_with_why_it_cannot_be_used(self): - listed = {found['id']: found for found in json.loads(self.berth('provisioner', 'list', '-o', 'json').stdout)} + listed = {found['name']: found for found in json.loads(self.berth('provisioner', 'list', '-o', 'json').stdout)} self.assertEqual(listed['box']['type'], 'lab/box') self.assertEqual(listed['box']['problem'], 'box needs place: berth provisioner set box place=VALUE') self.assertEqual(listed['old']['type'], 'lab/old') @@ -200,7 +200,8 @@ def test_through_berth_serve_an_action_that_changes_something_is_a_job(self): acted = serve.call('POST', '/v1/provisioners/box/actions/sweep', {'arguments': {'code': 's3cret-code'}, 'token': 'sweep-1'}, expect=202) job = acted['job'] - self.assertEqual((job['subject'], job['action']), ('provisioner:box', 'sweep')) + self.assertEqual((job['subject_name'], job['action']), ('provisioner:box', 'sweep')) + self.assertRegex(job['subject'], r'^provisioner:[0-9a-f]{32}$') portal = serve.portal() texts, data, _ = portal.stream('jobs.follow', {'id': job['id']}, until=lambda texts, data: any('exited' in t for t in texts if t)) @@ -325,10 +326,10 @@ def test_1_a_deployment_is_made_from_its_spec_with_a_secret_by_name(self): self.assertEqual(list(create['environment']), ['TOKEN']) self.assertNotIn('b3nch-t0ken', create['spec'] + json.dumps(create['arguments'])) listed = self.berth('resource', 'list').stdout - self.assertRegex(listed, r'(?m)^r-\S+\s+deployment\s+bench\s+box\s+running\s*$') + self.assertRegex(listed, r'(?m)^[0-9a-f]{32}\s+deployment\s+bench\s+box\s+running\s*$') page = self.berth('provisioner', 'show', 'box').stdout self.assertIn('Bench setup berth made with it', page) - self.assertRegex(page, r' bench\s+project-bench r-') + self.assertRegex(page, r' bench\s+project-bench [0-9a-f]{32}\n') def test_2_its_page_actions_log_and_change(self): shown = self.berth('resource', 'show', 'bench').stdout @@ -383,7 +384,7 @@ def test_5_the_provisioners_own_action_makes_one_and_it_is_kept_while_it_holds_o self.assertNotEqual(taken.returncode, 0) self.assertIn('There is a provisioner old already', taken.stderr) self.assertIn('box is called bench-lab now', self.berth('provisioner', 'rename', 'box', 'bench-lab').stdout) - self.assertRegex(self.berth('resource', 'list').stdout, r'(?m)^r-\S+\s+deployment\s+shed\s+bench-lab\s') + self.assertRegex(self.berth('resource', 'list').stdout, r'(?m)^[0-9a-f]{32}\s+deployment\s+shed\s+bench-lab\s') self.assertIn('The lab at attic answers', self.berth('provisioner', 'show', 'bench-lab').stdout) self.assertIn('State running', self.berth('resource', 'show', 'shed').stdout) self.assertNotEqual(self.berth('provisioner', 'show', 'box', check=False).returncode, 0) diff --git a/tests/e2e/e2e_serve.py b/tests/e2e/e2e_serve.py index ddf1307..4051f80 100644 --- a/tests/e2e/e2e_serve.py +++ b/tests/e2e/e2e_serve.py @@ -202,7 +202,9 @@ def test_secrets_and_registries(self): self.assertNotIn(name, self.serve.call('GET', '/v1/secrets')['names']) registry = {'type': 'npm', 'url': 'http://192.0.2.60/npm/'} self.serve.call('PUT', '/v1/registries/e2e-npm', registry) - self.assertEqual(self.serve.call('GET', '/v1/registries')['registries']['e2e-npm'], registry) + kept = self.serve.call('GET', '/v1/registries')['registries']['e2e-npm'] + self.assertRegex(kept.pop('id'), r'^[0-9a-f]{32}$') + self.assertEqual(kept, registry) self.serve.call('DELETE', '/v1/registries/e2e-npm') self.assertNotIn('e2e-npm', self.serve.call('GET', '/v1/registries')['registries']) diff --git a/tests/e2e/e2e_tailnet.py b/tests/e2e/e2e_tailnet.py index 1ea040f..b3f3734 100644 --- a/tests/e2e/e2e_tailnet.py +++ b/tests/e2e/e2e_tailnet.py @@ -132,7 +132,7 @@ def test_the_settings_and_the_key_are_the_servers(self): store = sqlite3.connect(self.serve.station.data / 'berth/berth.db') try: kept = store.execute("SELECT value FROM state WHERE kind = 'secret' AND scope = ''" - " AND name = 'tailnet_api_key'").fetchone() + " AND label = 'tailnet_api_key'").fetchone() finally: store.close() self.assertEqual(bytes(kept[0]).decode(), headscale().api_key)