rushls-common 0.1.1

Configuration, outbound HTTP, lifecycle hooks, rotating TLS, and listener helpers shared by Rushls services
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! Requests this node makes to services an administrator configured.
//!
//! Inbound HTTP serving remains the responsibility of each application.
//! Both admission and lifecycle hooks talk to operator-supplied endpoints, and
//! they share exactly this much: a pooled connection, bounded time, a bounded
//! response, and a bearer credential. What they do with the answer is entirely
//! different — admission fails closed and never retries, hooks queue and retry
//! — so none of that is here.
//!
//! Two properties come from what is *not* enabled rather than from code, and
//! both are load-bearing. `hyper-util`'s client does not follow redirects, so a
//! bearer token can never be replayed to another origin by a redirecting
//! endpoint. Its `client-proxy` feature is off, so no environment variable can
//! silently route these requests through a third party.
//!
//! Cleartext endpoints are permitted. Whether a hop is protected is not
//! knowable from the URL — a service mesh or a private network may be doing it
//! — so [`Endpoint::is_encrypted`] reports the scheme and leaves the judgement
//! to the operator who chose the address.

mod client_tls;
pub use client_tls::{ClientTls, ClientTlsError, ClientTlsFiles};

use std::{sync::Arc, time::Duration};

use bytes::Bytes;
use http::{HeaderMap, HeaderValue, Request, StatusCode, Uri, header};
use http_body_util::{BodyExt, Full, Limited};
use hyper_util::{
    client::legacy::{Client, connect::HttpConnector},
    rt::TokioExecutor,
};
use rustls::client::ResolvesClientCert;
use rustls::pki_types::CertificateDer;
use thiserror::Error;

/// A validated destination.
///
/// Parsed once at start-up so a malformed endpoint stops the process rather
/// than failing at the first publisher, and so the checks below happen where an
/// operator can still act on them.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Endpoint(Uri);

impl Endpoint {
    pub fn parse(value: &str) -> Result<Self, EndpointError> {
        let uri: Uri = value
            .parse()
            .map_err(|_| EndpointError::Malformed(value.to_owned()))?;
        let scheme = uri.scheme_str().unwrap_or_default();
        if !matches!(scheme, "http" | "https") {
            return Err(EndpointError::Scheme(value.to_owned()));
        }
        // Cleartext is permitted anywhere. A sidecar reached over a private
        // network or a service mesh that terminates its own TLS is the ordinary
        // deployment, and `http://auth-sidecar:8081` is not a loopback address
        // — refusing it would reject the common case to catch the careless one.
        uri.host().ok_or(EndpointError::NoHost(value.to_owned()))?;
        Ok(Self(uri))
    }

    pub fn uri(&self) -> &Uri {
        &self.0
    }

    /// Whether credentials sent here are protected in transit by this node.
    ///
    /// Cleartext may still be entirely appropriate, so this is a fact to report
    /// rather than a verdict: what protects the hop may be a mesh or a private
    /// network that this process cannot see.
    pub fn is_encrypted(&self) -> bool {
        self.0.scheme_str() == Some("https")
    }
}

impl std::fmt::Display for Endpoint {
    fn fmt(&self, output: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(output, "{}", self.0)
    }
}

#[derive(Clone, Debug, Eq, PartialEq, Error)]
pub enum EndpointError {
    #[error("`{0}` is not a URL")]
    Malformed(String),
    #[error("`{0}` must use http or https")]
    Scheme(String),
    #[error("`{0}` names no host")]
    NoHost(String),
}

/// A bearer credential, kept out of anything that prints.
#[derive(Clone, derive_more::Debug)]
#[debug("BearerToken([REDACTED])")]
pub struct BearerToken(#[debug(skip)] HeaderValue);

impl BearerToken {
    pub fn new(token: &str) -> Result<Self, InvalidToken> {
        let mut value =
            HeaderValue::try_from(format!("Bearer {token}")).map_err(|_| InvalidToken)?;
        // Marks the value for redaction in hyper's own logging, which would
        // otherwise print it whenever request tracing is turned on.
        value.set_sensitive(true);
        Ok(Self(value))
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Error)]
#[error("a bearer token must be printable ASCII without control characters")]
pub struct InvalidToken;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ClientConfig {
    pub connect_timeout: Duration,
    /// Covers connect, send, and reading the whole response.
    pub request_timeout: Duration,
    /// Refused past this, so a misbehaving endpoint cannot exhaust memory.
    pub maximum_response_bytes: usize,
}

impl Default for ClientConfig {
    fn default() -> Self {
        Self {
            connect_timeout: Duration::from_millis(500),
            request_timeout: Duration::from_secs(2),
            maximum_response_bytes: 64 * 1024,
        }
    }
}

/// What an endpoint answered.
///
/// Reported, not interpreted: whether a status is success, worth retrying, or
/// permanent is the caller's policy, and it differs between admission and
/// hooks. The one exception is [`Self::retry_after`], which parses a header
/// rather than deciding anything with it.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Response {
    pub status: StatusCode,
    pub headers: HeaderMap,
    pub body: Bytes,
}

impl Response {
    /// `Retry-After` in its delta-seconds form, capped by the caller.
    ///
    /// Only the seconds form. The HTTP-date alternative needs a parsed clock to
    /// mean anything, and an endpoint that answers an event POST with an
    /// absolute date is not a case worth carrying a date parser for; treating
    /// it as absent falls back to the caller's own backoff, which is safe.
    pub fn retry_after(&self, maximum: Duration) -> Option<Duration> {
        let seconds: u64 = self
            .headers
            .get(header::RETRY_AFTER)?
            .to_str()
            .ok()?
            .trim()
            .parse()
            .ok()?;
        Some(Duration::from_secs(seconds).min(maximum))
    }
}

#[derive(Clone, Debug, Eq, PartialEq, Error)]
pub enum OutboundError {
    #[error("the request did not complete within its deadline")]
    Timeout,
    #[error("could not reach the endpoint: {0}")]
    Unreachable(String),
    #[error("the response was not readable: {0}")]
    Response(String),
    #[error("the response exceeded {limit} bytes")]
    ResponseTooLarge { limit: usize },
}

/// Builds at most one outbound client, and only if something needs it.
///
/// Reading the platform trust store and building a TLS configuration is real
/// work, and a node that calls nothing out should not pay for it — nor should
/// the tests covering those configurations. Callers ask for their own deadline
/// and response ceiling, which are per-request and so cost nothing to vary;
/// what they share is the connector underneath.
#[derive(Default)]
pub struct LazyHttpClient(Option<HttpClient>);

impl LazyHttpClient {
    /// A client with the caller's limits, over the shared connector.
    pub fn with_limits(
        &mut self,
        request_timeout: Duration,
        maximum_response_bytes: usize,
    ) -> Result<HttpClient, OutboundError> {
        let shared = match &mut self.0 {
            Some(client) => client,
            slot @ None => slot.insert(HttpClient::new(ClientConfig::default())?),
        };
        Ok(shared.with_limits(request_timeout, maximum_response_bytes))
    }
}

/// A pooled HTTP client shared by everything that calls out.
///
/// Cloning shares the connection pool, which is the point: a per-publisher
/// client would pay a TCP and TLS handshake on every admission.
#[derive(Clone, derive_more::Debug)]
pub struct HttpClient {
    #[debug(skip)]
    inner: Client<hyper_rustls::HttpsConnector<HttpConnector>, Full<Bytes>>,
    config: ClientConfig,
}

impl HttpClient {
    /// The same connection pool under different per-request limits.
    ///
    /// A deadline and a response ceiling are properties of a request, not of a
    /// pool, and admission and hooks want different ones: admission runs inside
    /// a publisher's handshake, while a hook is free to wait. Sharing the
    /// client keeps the platform trust store read once and the TLS
    /// configuration built once, which is the part worth sharing.
    ///
    /// `connect_timeout` is fixed when the connector is built and so is not
    /// overridable here.
    #[must_use]
    pub fn with_limits(&self, request_timeout: Duration, maximum_response_bytes: usize) -> Self {
        Self {
            inner: self.inner.clone(),
            config: ClientConfig {
                request_timeout,
                maximum_response_bytes,
                ..self.config
            },
        }
    }

    /// Builds a client that also presents an identity, and may pin its trust.
    ///
    /// Separate from [`Self::new`] rather than an option on it, because the
    /// two answer different questions: `new` is "who do I trust", this is
    /// additionally "who am I". A caller that needs neither should not have to
    /// say so.
    ///
    /// `identity` resolves per handshake, so a rotated certificate is picked up
    /// without rebuilding this client or restarting the process. `roots`
    /// replaces the platform store when present: a deployment pinning the
    /// authority that signed its own services wants exactly that authority and
    /// not every public one as well.
    pub fn with_identity(
        config: ClientConfig,
        identity: Option<Arc<dyn ResolvesClientCert>>,
        roots: Option<Vec<CertificateDer<'static>>>,
    ) -> Result<Self, OutboundError> {
        let mut connector = HttpConnector::new();
        connector.set_connect_timeout(Some(config.connect_timeout));
        // The TLS layer wraps this one, so it must not reject an https URL
        // before ever seeing it.
        connector.enforce_http(false);

        let builder = rustls::ClientConfig::builder();
        let builder = match roots {
            Some(roots) => {
                let mut store = rustls::RootCertStore::empty();
                for root in roots {
                    store
                        .add(root)
                        .map_err(|error| OutboundError::Unreachable(error.to_string()))?;
                }
                builder.with_root_certificates(store)
            }
            // The platform store, so an endpoint behind an internal authority
            // works once that authority is installed the way everything else
            // on the host already expects.
            None => {
                let mut store = rustls::RootCertStore::empty();
                let loaded = rustls_native_certs::load_native_certs();
                if loaded.certs.is_empty() {
                    let reason = loaded
                        .errors
                        .iter()
                        .map(ToString::to_string)
                        .collect::<Vec<_>>()
                        .join("; ");
                    return Err(OutboundError::Unreachable(format!(
                        "no platform trust roots could be loaded: {reason}"
                    )));
                }
                store.add_parsable_certificates(loaded.certs);
                builder.with_root_certificates(store)
            }
        };

        let tls = match identity {
            Some(resolver) => builder.with_client_cert_resolver(resolver),
            None => builder.with_no_client_auth(),
        };

        let connector = hyper_rustls::HttpsConnectorBuilder::new()
            .with_tls_config(tls)
            .https_or_http()
            .enable_http1()
            .wrap_connector(connector);

        Ok(Self {
            inner: Client::builder(TokioExecutor::new()).build(connector),
            config,
        })
    }

    /// Builds a client trusting the platform's certificate store.
    ///
    /// The platform store rather than a bundled root set, so an endpoint behind
    /// an internal certificate authority works once that authority is installed
    /// the way everything else on the host already expects.
    pub fn new(config: ClientConfig) -> Result<Self, OutboundError> {
        Self::with_identity(config, None, None)
    }

    /// Fetches one URL and reads the whole response.
    ///
    /// Used for rotating key sets: the body is JSON, not a form, and there is
    /// nothing to POST. Redirects are still not followed — a JWKS URL that
    /// 30x's the origin elsewhere would be a configuration error, not a hop
    /// this client should take on its own.
    pub async fn get(&self, endpoint: &Endpoint) -> Result<Response, OutboundError> {
        let request = Request::get(endpoint.uri())
            .body(Full::new(Bytes::new()))
            .map_err(|error| OutboundError::Unreachable(error.to_string()))?;
        tokio::time::timeout(self.config.request_timeout, self.send(request))
            .await
            .map_err(|_| OutboundError::Timeout)?
    }

    /// Sends one request and reads the whole response.
    pub async fn post(
        &self,
        endpoint: &Endpoint,
        content_type: &'static str,
        bearer: Option<&BearerToken>,
        body: Bytes,
    ) -> Result<Response, OutboundError> {
        self.post_with_headers(endpoint, content_type, bearer, body, &HeaderMap::new())
            .await
    }

    /// Sends application headers alongside the ordinary content type and credential.
    /// The latter remain authoritative even if the supplied map contains them.
    pub async fn post_with_headers(
        &self,
        endpoint: &Endpoint,
        content_type: &'static str,
        bearer: Option<&BearerToken>,
        body: Bytes,
        headers: &HeaderMap,
    ) -> Result<Response, OutboundError> {
        let mut request = Request::post(endpoint.uri())
            .header(header::CONTENT_TYPE, HeaderValue::from_static(content_type))
            .body(Full::new(body))
            .map_err(|error| OutboundError::Unreachable(error.to_string()))?;
        request.headers_mut().extend(headers.clone());
        request
            .headers_mut()
            .insert(header::CONTENT_TYPE, HeaderValue::from_static(content_type));
        request.headers_mut().remove(header::AUTHORIZATION);
        if let Some(bearer) = bearer {
            request
                .headers_mut()
                .insert(header::AUTHORIZATION, bearer.0.clone());
        }

        tokio::time::timeout(self.config.request_timeout, self.send(request))
            .await
            .map_err(|_| OutboundError::Timeout)?
    }

    async fn send(&self, request: Request<Full<Bytes>>) -> Result<Response, OutboundError> {
        let response = self
            .inner
            .request(request)
            .await
            .map_err(|error| OutboundError::Unreachable(error.to_string()))?;
        let status = response.status();
        let headers = response.headers().clone();
        let limit = self.config.maximum_response_bytes;
        // Bounded while it is read rather than checked afterwards: a
        // Content-Length is only a claim, and by the time an oversized body has
        // been collected the memory is already spent.
        let body = Limited::new(response.into_body(), limit)
            .collect()
            .await
            .map_err(|error| {
                if error.is::<http_body_util::LengthLimitError>() {
                    OutboundError::ResponseTooLarge { limit }
                } else {
                    OutboundError::Response(error.to_string())
                }
            })?
            .to_bytes();

        Ok(Response {
            status,
            headers,
            body,
        })
    }
}

#[cfg(test)]
mod tests;