Repository navigation
feat(auth): mesh enrollment in moq auth serve #7301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
678173b
0ffe099
f72c8a2
5c29a2c
f1b9fef
017e318
132cac5
56824a1
fc89cc6
96d3fff
9af8ab2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,7 @@ mod set; | |
| mod wire; | ||
|
|
||
| pub mod lease; | ||
| pub mod mesh; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| pub mod refusal; | ||
| pub use refusal::Refusal; | ||
| #[cfg(feature = "serve")] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| //! The device side of the contract: enroll, then keep the token fresh. | ||
|
|
||
| use std::sync::Arc; | ||
| use std::time::Duration; | ||
|
|
||
| use url::Url; | ||
|
|
||
| use super::{Challenge, EnrollRequest, Enrollment, ErrorCode, ErrorReply, Proof, Signer}; | ||
| use crate::Error; | ||
|
|
||
| /// Every request is bounded so a hung coordinator fails rather than parks the device. | ||
| const TIMEOUT: Duration = Duration::from_secs(10); | ||
|
|
||
| /// The largest reply read from the coordinator; a mesh's member list fits easily. | ||
| const MAX_REPLY: usize = 4 << 20; | ||
|
|
||
| /// The longest wait between refresh attempts while the coordinator is unreachable. | ||
| const MAX_BACKOFF: Duration = Duration::from_secs(60); | ||
|
|
||
| /// A device's client for one coordinator, proving `signer`'s key on every request that | ||
| /// needs it. | ||
| #[derive(Clone)] | ||
| pub struct Client { | ||
| http: reqwest::Client, | ||
| base: Url, | ||
| signer: Arc<dyn Signer>, | ||
| } | ||
|
|
||
| impl Client { | ||
| /// A client for the coordinator at `base`, the URL the contract's paths hang off. | ||
| /// | ||
| /// `https://` is required, except `http://` to a loopback host, since the token rides | ||
| /// the reply. | ||
| pub fn new(base: Url, signer: Arc<dyn Signer>) -> crate::Result<Self> { | ||
| let loopback = match base.host() { | ||
| Some(url::Host::Ipv4(ip)) => ip.is_loopback(), | ||
| Some(url::Host::Ipv6(ip)) => ip.is_loopback(), | ||
| Some(url::Host::Domain(host)) => host == "localhost", | ||
| None => false, | ||
| }; | ||
| match base.scheme() { | ||
| "https" => {} | ||
| "http" if loopback => {} | ||
| "http" => return Err(Error::InsecureUrl(base.to_string())), | ||
| _ => return Err(Error::InvalidUrl(base.to_string())), | ||
| } | ||
| // A redirect would resend a proof or enrollment key to wherever it points. | ||
| let http = reqwest::Client::builder() | ||
| .redirect(reqwest::redirect::Policy::none()) | ||
| .timeout(TIMEOUT) | ||
| .build()?; | ||
| Ok(Self { http, base, signer }) | ||
| } | ||
|
|
||
| /// Join with a pre-authorized enrollment key, asking for `name`. | ||
| /// | ||
| /// When the coordinator refuses the key but this endpoint is already enrolled, such as | ||
| /// a retry after a single-use key's reply was lost, this refreshes instead, keeping the | ||
| /// name the device already has. | ||
| pub async fn enroll(&self, key: &str, name: Option<&str>) -> crate::Result<Enrollment> { | ||
| let proof = self.proof().await?; | ||
| let request = EnrollRequest { | ||
| proof, | ||
| key: key.into(), | ||
| name: name.map(Into::into), | ||
| }; | ||
| match self.post("enroll", &request).await { | ||
| Err(Error::Mesh(refused)) if refused.error == ErrorCode::InvalidGrant => match self.refresh().await { | ||
| // Not enrolled after all, so the key's refusal is the answer. | ||
| Err(Error::Mesh(reply)) if reply.error == ErrorCode::AccessDenied => Err(Error::Mesh(refused)), | ||
| res => res, | ||
| }, | ||
| res => res, | ||
| } | ||
| } | ||
|
|
||
| /// Prove the key again for a fresh token. Fails with [`ErrorCode::AccessDenied`] once | ||
| /// the device is revoked. | ||
| pub async fn refresh(&self) -> crate::Result<Enrollment> { | ||
| let proof = self.proof().await?; | ||
| self.post("refresh", &proof).await | ||
| } | ||
|
|
||
| /// Wait until `current` is two thirds through its remaining life, then refresh it, | ||
| /// retrying through an outage until it expires. A refusal is returned at once, so a | ||
| /// revoked device stops rather than retrying, except a transient one: a server error, | ||
| /// `slow_down`, or a challenge that expired or outlived a coordinator restart. | ||
| pub async fn renew(&self, current: &Enrollment) -> crate::Result<Enrollment> { | ||
| let remaining = || { | ||
| current | ||
| .expires | ||
| .checked_duration_since(moq_clock::wall()) | ||
| .unwrap_or_default() | ||
| }; | ||
| moq_clock::sleep(remaining() * 2 / 3).await; | ||
|
|
||
| let mut backoff = Duration::from_secs(1); | ||
| loop { | ||
| match self.refresh().await { | ||
| Err(Error::Mesh(reply)) | ||
| if !matches!( | ||
| reply.error, | ||
| ErrorCode::ServerError | ErrorCode::SlowDown | ErrorCode::InvalidChallenge | ||
| ) => | ||
| { | ||
| return Err(Error::Mesh(reply)); | ||
| } | ||
| Ok(enrollment) => return Ok(enrollment), | ||
| Err(err) if remaining().is_zero() => return Err(err), | ||
| Err(err) => { | ||
| tracing::warn!(%err, "mesh refresh failed; retrying"); | ||
| moq_clock::sleep(backoff.min(remaining())).await; | ||
| backoff = (backoff * 2).min(MAX_BACKOFF); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async fn proof(&self) -> crate::Result<Proof> { | ||
| let challenge: Challenge = self.post("challenge", &serde_json::Value::Null).await?; | ||
| Ok(Proof::sign(self.signer.as_ref(), &self.base, challenge.challenge)) | ||
| } | ||
|
|
||
| async fn post<T: serde::Serialize, R: serde::de::DeserializeOwned>( | ||
| &self, | ||
| path: &str, | ||
| body: &T, | ||
| ) -> crate::Result<R> { | ||
| let mut url = self.base.clone(); | ||
| let joined = format!("{}/{path}", url.path().trim_end_matches('/')); | ||
| url.set_path(&joined); | ||
|
|
||
| let request = self.http.post(url); | ||
| let request = match serde_json::to_value(body)? { | ||
| serde_json::Value::Null => request, | ||
| body => request.json(&body), | ||
| }; | ||
| let mut response = request.send().await?; | ||
| let status = response.status(); | ||
|
|
||
| let mut bytes = Vec::new(); | ||
| while let Some(chunk) = response.chunk().await? { | ||
| if bytes.len() + chunk.len() > MAX_REPLY { | ||
| return Err(Error::Unavailable("the coordinator's reply is too large".into())); | ||
| } | ||
| bytes.extend_from_slice(&chunk); | ||
| } | ||
|
|
||
| if status.is_success() { | ||
| return Ok(serde_json::from_slice(&bytes)?); | ||
| } | ||
| match serde_json::from_slice::<ErrorReply>(&bytes) { | ||
| Ok(reply) => Err(Error::Mesh(reply)), | ||
| Err(_) => Err(Error::Unavailable(format!("the coordinator answered {status}"))), | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct the default name: the shipped coordinator names a device
device, not its hostname.Line 406 says every member's name defaults to the device's hostname. The shipped code does not do that:
Client::enrollsendsnameonly when the caller passes one.Coordinator::admitcallssanitize(name.unwrap_or_default()), andsanitizereturns"device"for an empty name.device,device-2, and so on.The hostname default exists only in the daemon plan (
quest/m2/mesh/daemon.md), and the daemon is not shipped. A user of a hosted coordinator or a custom client follows this page and gets the wrong name.As per coding guidelines, "Describe only what ships. Plans live in quests."
📝 Proposed fix
Committable suggestion
Source: Coding guidelines