Skip to main content

CanopyClient

Struct CanopyClient 

Source
pub struct CanopyClient { /* private fields */ }
Expand description

HTTP client with auth configured for talking to a canopy server.

Tries two auth paths in order of preference:

  1. Tailscale: if the canopy tailnet endpoint is reachable, plain HTTPS works (auth is implicit via tailscale identity).
  2. mTLS: a fresh self-signed cert from the device key, short-lived ([CERT_VALIDITY_DAYS]); for long-running daemons, Self::renew should tick on CERT_RENEW_AFTER to swap in a fresh cert before expiry.

Self::refresh re-probes tailscale and swaps modes on reload.

Implementations§

Source§

impl CanopyClient

Source

pub async fn new( tamanu_version: impl Into<String>, device_key_pem: Option<&str>, make_builder: impl Fn() -> ClientBuilder + Send + Sync + 'static, ) -> Result<Option<Self>>

Build a canopy client, preferring tailscale and falling back to mTLS.

Probes the tailscale canopy endpoint first; if reachable, uses it. Otherwise, if a device key PEM is provided, builds an mTLS client. Returns Ok(None) if neither path is available.

tamanu_version is the version of the Tamanu install this client speaks for; sent on every request via the X-Version header.

make_builder supplies the base reqwest::ClientBuilder — see ClientBuilderFactory. Use client_builder for a sensible default.

Source

pub async fn is_tailscale(&self) -> bool

Returns true if the client is currently using the tailscale path.

Source

pub async fn refresh(&self) -> Result<()>

Re-probe tailscale and swap modes if the picture has changed.

Intended to be called when the daemon receives a reload signal.

Source

pub async fn renew(&self) -> Result<()>

Rebuild the underlying HTTP client with a fresh certificate.

No-op in tailscale mode (no cert to rotate). In mTLS mode, atomically replaces the live client; in-flight requests continue with the old client until they complete.

Source

pub async fn post_status( &self, base_url: &Url, server_id: &str, payload: &Value, ) -> Result<()>

POST a status snapshot to the canopy server.

In tailscale mode, base_url is ignored and a {TAILSCALE_URL}/public/status/{server_id} URL is used. In mTLS mode, posts to {base_url}/status/{server_id}.

The payload is free-form JSON; the canopy /status contract reserves the top-level healthy: bool and health: [] keys. The body is gzip-encoded with Content-Encoding: gzip.

Source

pub async fn get( &self, base_url: &Url, tailscale_path: &str, mtls_path: &str, ) -> Result<Response>

GET a path on the canopy server, routed via tailscale when available.

In tailscale mode, the request goes to {TAILSCALE_URL}{tailscale_path} (typically /public/..., the only mount that accepts tagged-device tailscale callers). In mTLS mode, the request goes to {base_url}{mtls_path}.

Returns the raw response — the caller is responsible for status checks and body parsing so they can choose how to fall back if the response isn’t usable.

Source

pub async fn post_event( &self, base_url: &Url, event: NewEvent<'_>, ) -> Result<()>

POST an event to the canopy server.

In tailscale mode, base_url is ignored and TAILSCALE_URL is used. In mTLS mode, posts to {base_url}/events.

Trait Implementations§

Source§

impl Debug for CanopyClient

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more