diff options
| -rw-r--r-- | CHANGELOG.md | 5 | ||||
| -rw-r--r-- | docs/configuration.md | 6 | ||||
| -rw-r--r-- | docs/guides/federation/running-a-node.md | 3 | ||||
| -rw-r--r-- | src/federation/party.rs | 84 |
4 files changed, 87 insertions, 11 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index c0c3c75..3d04acf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ it from the release before ([`docs/operations.md`](docs/operations.md), ## Unreleased +* `continuum-party` logs in as its party by its key where `--node` names no user and no + `--token` is given ([`docs/configuration.md`](docs/configuration.md), + [The party's client](docs/configuration.md#the-partys-client); + [`src/federation/party.rs`](src/federation/party.rs)). + * A party's keys log in to every federated node without a password, as the party (`POST /auth/login/party`): a session that reads and puts events and checks forks, and does nothing else ([`docs/guides/federation/running-a-node.md`](docs/guides/federation/running-a-node.md), diff --git a/docs/configuration.md b/docs/configuration.md index 56c3347..ea401f5 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -272,6 +272,12 @@ continuum-party --federation krebs --node http://tv:secret@localhost:3000 \ continuum-party vote 8d02 yes # krebs, the only one ``` +Where `node` names no user and no `token` is given, the client logs in to the node as its +party, by `key`, where the federated node takes it: a party's session, which reads and +puts events and nothing else ([`guides/federation/running-a-node.md`](guides/federation/running-a-node.md), +[Its API](guides/federation/running-a-node.md#its-api)). So `node = "https://node.example"` +and a key whose party a version names are all a party's people need. + `key` is an OpenSSH private key file, or a public key an ssh-agent holds: the key itself (`key = "ecdsa-sha2-nistp256 AAAA…"`), or a file of such lines, whose first signs ([`guides/federation/keys.md`](guides/federation/keys.md), diff --git a/docs/guides/federation/running-a-node.md b/docs/guides/federation/running-a-node.md index ea51f8a..44b309a 100644 --- a/docs/guides/federation/running-a-node.md +++ b/docs/guides/federation/running-a-node.md @@ -79,7 +79,8 @@ alone, so that a party listing many nodes can't be all of a node's neighbours. (`POST /events`), while that key signs for the party (`401` once it doesn't). So a party's people, each with a key of its set, work with the federation through any node that holds it, as no operator has to let them in, and with nothing of an operator's - powers. + powers. `continuum-party` logs in so by itself, where `--node` names no user and no + `--token` is given, as the party its record names. * **The events** (`docs/openapi.yaml`, Events): `GET /events` (the store's ID, its checkpoint, its heads, how many events it took, the node's party, and whether it votes for it by itself), `POST /events` (an event, whoever signed it), `GET /events/{id}`, diff --git a/src/federation/party.rs b/src/federation/party.rs index 8f69e49..72c18e9 100644 --- a/src/federation/party.rs +++ b/src/federation/party.rs @@ -1,8 +1,9 @@ //! `continuum-party`: a party's client of a federation (`docs/federation.md`). It signs its //! party's events with one of its keys (`--key`: an OpenSSH private key file, or a public //! key the ssh-agent at `SSH_AUTH_SOCK` holds, given itself or in a file), puts them -//! through a node's API (`--node`, its user and password in the URL, or `--token`), and -//! keeps its party's record (`--record`, `client::Record`), by which it votes, written +//! through a node's API (`--node`, its user and password in the URL, or `--token`; with +//! neither, as its party, logging in there by that key: `Node::log_in`), and keeps its +//! party's record (`--record`, `client::Record`), by which it votes, written //! before a vote is sent. Its options may be set in configuration files as the node's are //! (`--config`, `config::PARTY_OPTIONS`): the same format, imports and all. //! @@ -616,7 +617,23 @@ pub(crate) async fn main() -> Result<()> { print!("{}", judge::judge(&histories)); return Ok(()); } - let node = Node::new(&options)?; + let mut node = Node::new(&options)?; + // Without a user of the node or a token: as the party, by its key, where the record + // (or `init`) names the party. + if node.password.is_none() && node.token.is_none() && options.key.is_some() { + let party = match (name, &options.record) { + ("init", _) => sub.get_one::<String>("party").cloned(), + (_, Some(path)) if Path::new(path).exists() => Some( + Record::load(Path::new(path)) + .map_err(|why| anyhow!(why))? + .party, + ), + _ => None, + }; + if let Some(party) = party { + node.log_in(&party, &signing_key(&options)?).await?; + } + } if name == "propose" && proposing_flags(sub).get_flag("print") { let current = node.current().await?; let version = to_propose(&node, ¤t, sub).await?; @@ -2161,18 +2178,65 @@ impl Node { }) } - async fn answer(response: reqwest::Response) -> Result<reqwest::Response> { + async fn answer(&self, response: reqwest::Response) -> Result<reqwest::Response> { let status = response.status(); if status.is_success() { return Ok(response); } let body = response.text().await.unwrap_or_default(); + if status == reqwest::StatusCode::UNAUTHORIZED + && self.password.is_none() + && self.token.is_none() + { + bail!( + "the node answers {status}: {body}. Name a user of it in --node, give --token, \ + or --key and --record to log in as the party" + ); + } bail!("the node answers {status}: {body}") } + /// Logs in as `party` by its key `key` (`POST /auth/login/party`): a party's session, + /// which does federation work alone, its token going with every request then. + async fn log_in(&mut self, party: &str, key: &Signing) -> Result<()> { + let namespace = crate::access::challenge::NAMESPACE; + let asked = async { + let response = self + .request(reqwest::Method::POST, "/auth/challenge")? + .send() + .await?; + let challenge: JsonValue = self.answer(response).await?.json().await?; + let challenge = challenge["challenge"] + .as_str() + .context("the node gives no challenge")? + .to_owned(); + let signature = match key { + Signing::File(key) => keys::sign_in(namespace, key, challenge.as_bytes()), + Signing::Agent(key, socket) => { + keys::sign_through_in(namespace, socket, key, challenge.as_bytes()) + .map_err(|why| anyhow!(why))? + } + }; + let body = serde_json::json!({ + "party": party, + "challenge": challenge, + "signature": signature, + }); + self.post("/auth/login/party", &body).await + }; + let answer = asked.await.with_context(|| { + format!("Logging in as {party} by its key, with no user of the node") + })?; + let token = answer["token"] + .as_str() + .context("the node gives no token")?; + self.token = Some(token.to_owned()); + Ok(()) + } + async fn get(&self, path: &str) -> Result<JsonValue> { let response = self.request(reqwest::Method::GET, path)?.send().await?; - Ok(Self::answer(response).await?.json().await?) + Ok(self.answer(response).await?.json().await?) } /// The store's ID, and its heads. @@ -2217,7 +2281,7 @@ impl Node { .request(reqwest::Method::GET, &format!("/events/{id}"))? .send() .await?; - let bytes = Self::answer(response).await?.bytes().await?.to_vec(); + let bytes = self.answer(response).await?.bytes().await?.to_vec(); if Id::of(&bytes) != *id { bail!("the node's event {id} isn't of that ID"); } @@ -2230,7 +2294,7 @@ impl Node { .json(body) .send() .await?; - Ok(Self::answer(response).await?.json().await?) + Ok(self.answer(response).await?.json().await?) } /// What the node answers to `body`, as bytes. @@ -2240,7 +2304,7 @@ impl Node { .json(body) .send() .await?; - Ok(Self::answer(response).await?.bytes().await?.to_vec()) + Ok(self.answer(response).await?.bytes().await?.to_vec()) } /// What the node answers to the bytes `body`. @@ -2251,7 +2315,7 @@ impl Node { .body(body) .send() .await?; - Ok(Self::answer(response).await?.json().await?) + Ok(self.answer(response).await?.json().await?) } async fn put(&self, event: &[u8]) -> Result<()> { @@ -2261,7 +2325,7 @@ impl Node { .body(event.to_vec()) .send() .await?; - let answer: JsonValue = Self::answer(response).await?.json().await?; + let answer: JsonValue = self.answer(response).await?.json().await?; println!( "{} {}", answer["id"].as_str().unwrap_or_default(), |
