rift-mock-core 0.25.0

Rift core engine: imposter lifecycle, matching, behaviors, scripting and flow-state — usable in-process without the CLI or admin server.
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
//! Trust policy for every TLS connection Rift *initiates* (issue #974).
//!
//! Before this module each outbound client decided its own trust independently: the imposter
//! `proxy` stub client and the config-source fetcher carried reqwest's compiled-in webpki roots
//! with no knob at all, while the (since removed, #975) reverse-proxy client used the OS store.
//! A privately-issued origin — a corporate API gateway — was therefore
//! unreachable from a `proxy` stub no matter how the host was configured, because that client
//! never consulted the OS trust store where the CA lives.
//!
//! [`OutboundTls`] is that decision made once, as data, and shared. It is deliberately plain: the
//! PEM arrives already read, mirroring [`TlsDefaults`](crate::imposter::TlsDefaults), so nothing
//! here touches the filesystem and the "which file, and does it exist" failure stays at the CLI
//! boundary where it belongs.

use std::sync::Arc;

use anyhow::Context;
use rustls::RootCertStore;
use tracing::warn;

use super::client_identity::ClientIdentity;
use super::tls::NoVerifier;

/// Trust policy for outbound TLS. `Default` means "the OS trust store, verification on", which is
/// what every caller wants outside development.
///
/// The two knobs are not symmetric: `ca_pem` **adds** an anchor and keeps verification intact,
/// while `skip_verify` abandons verification entirely. Prefer the former; the latter exists for
/// development against an origin whose chain is not available.
#[derive(Debug, Clone, Default)]
pub struct OutboundTls {
    /// Extra trust anchor(s) as PEM, appended to the OS trust store.
    ///
    /// Appended rather than replacing, which is the difference that matters in practice:
    /// `SSL_CERT_FILE` (which `rustls-native-certs` honours) *replaces* the store, so pointing it
    /// at a lone private CA silently drops every public root. This keeps both.
    pub ca_pem: Option<String>,
    /// Accept any certificate. Development only — logs a warning whenever a config is built.
    pub skip_verify: bool,
}

impl OutboundTls {
    /// The rustls config this policy describes.
    ///
    /// The returned config leaves `alpn_protocols` **empty**: this is the shared base, and each
    /// consumer sets the protocols it actually negotiates. [`Self::reqwest_client_config`] adds
    /// `http/1.1`, because reqwest populates ALPN only on its own rustls path and never for a
    /// config handed to `use_preconfigured_tls`.
    ///
    /// Keep it empty for any future hyper-rustls consumer too:
    /// `HttpsConnectorBuilder::with_tls_config` *asserts* `alpn_protocols.is_empty()` and sets ALPN
    /// itself, so a pre-set value there is a runtime panic rather than a preference.
    ///
    /// # Errors
    /// - `ca_pem` is malformed, or contains no certificate;
    /// - no trust anchors could be assembled at all (an image with no CA bundle and no `ca_pem`).
    ///
    /// Never panics: this replaces a `.expect` that aborted the process on a TLS/DNS
    /// misconfiguration (the same fix #543 made for the reverse-proxy client, itself since removed).
    pub fn client_config(&self) -> anyhow::Result<rustls::ClientConfig> {
        self.client_config_with_identity(None)
    }

    /// [`Self::client_config`], presenting `identity` to a server that asks for a client
    /// certificate (issue #1609). Trust is decided exactly as without one: an identity never
    /// relaxes verification of the upstream.
    ///
    /// The identity is served through a resolver rather than `with_client_auth_cert`, whose
    /// consistency check parses the certificate with webpki and so refuses X.509 v1 (issue #1599);
    /// [`ClientIdentity::from_pem`] has already checked that the key matches.
    ///
    /// # Errors
    /// Whatever [`Self::client_config`] returns.
    pub fn client_config_with_identity(
        &self,
        identity: Option<&ClientIdentity>,
    ) -> anyhow::Result<rustls::ClientConfig> {
        let finish = |builder: rustls::ConfigBuilder<
            rustls::ClientConfig,
            rustls::client::WantsClientCert,
        >| match identity {
            Some(identity) => builder.with_client_cert_resolver(Arc::new(
                rustls::sign::SingleCertAndKey::from(identity.certified_key()),
            )),
            None => builder.with_no_client_auth(),
        };
        // An explicit provider rather than the process default: `install_default()` is called by
        // the binary's `main`, but a unit test or an embedder constructing an `Imposter` directly
        // has never called it, and `ClientConfig::builder()` panics when no default is installed.
        let provider = Arc::new(rustls::crypto::ring::default_provider());
        let builder = rustls::ClientConfig::builder_with_provider(Arc::clone(&provider))
            .with_safe_default_protocol_versions()
            .context("building the outbound TLS configuration")?;

        if self.skip_verify {
            warn!(
                "TLS certificate verification DISABLED for outbound connections \
                 (development/testing only)"
            );
            if self.ca_pem.is_some() {
                // Say which setting is void. The generic warning above is easy to read as "the
                // private-CA check is skipped, public roots still apply", which is not what this
                // mode does — and an operator mid-migration may have both set.
                warn!(
                    "an outbound CA was also supplied (--upstream-ca-file / upstreamCaPem); it is \
                     NOT read or used while verification is disabled"
                );
            }
            // `ca_pem` is deliberately not parsed here: nothing is verified against it, so
            // reporting a malformed anchor would be noise about a value with no effect.
            return Ok(finish(
                builder
                    .dangerous()
                    .with_custom_certificate_verifier(Arc::new(NoVerifier::new(&provider))),
            ));
        }

        let mut roots = RootCertStore::empty();
        let native = rustls_native_certs::load_native_certs();
        // A partially-readable store is normal (macOS keychains routinely hold entries rustls
        // cannot parse), so these are warnings; the emptiness check below is what actually fails.
        for error in &native.errors {
            warn!(%error, "ignoring an unreadable entry in the OS trust store");
        }
        let (added, ignored) = roots.add_parsable_certificates(native.certs);
        if ignored > 0 {
            warn!(added, ignored, "some OS trust anchors were not usable");
        }

        if let Some(pem) = &self.ca_pem {
            let extra: Vec<_> = rustls_pemfile::certs(&mut pem.as_bytes())
                .collect::<Result<_, _>>()
                .context("parsing the outbound CA certificate PEM")?;
            if extra.is_empty() {
                anyhow::bail!(
                    "no certificates found in the outbound CA PEM (--upstream-ca-file / \
                     upstreamCaPem): expected at least one -----BEGIN CERTIFICATE----- block"
                );
            }
            let (_, ignored) = roots.add_parsable_certificates(extra);
            if ignored > 0 {
                anyhow::bail!(
                    "the outbound CA PEM contains {ignored} certificate(s) rustls cannot use"
                );
            }
        }

        if roots.is_empty() {
            anyhow::bail!(
                "no TLS trust anchors available: the OS trust store is empty or unreadable and no \
                 CA PEM was supplied. Install a CA bundle (e.g. ca-certificates), set \
                 SSL_CERT_FILE, or pass --upstream-ca-file"
            );
        }

        Ok(finish(builder.with_root_certificates(roots)))
    }

    /// Whether the operator actually asked for anything beyond the default.
    ///
    /// Callers use this to stay lazy: realising the default policy means reading the OS trust
    /// store, and doing that eagerly on every startup delays the first bind for a cost nothing
    /// asked for. A *configured* policy is different — it is realised at startup precisely so a
    /// bad anchor fails there rather than on the first proxied request.
    #[must_use]
    pub fn is_configured(&self) -> bool {
        self.ca_pem.is_some() || self.skip_verify
    }

    /// The config a reqwest consumer needs: [`Self::client_config`] plus `http/1.1` ALPN.
    ///
    /// reqwest only populates `alpn_protocols` on its own `TlsBackend::Rustls` path; a config
    /// handed to `use_preconfigured_tls` becomes `TlsBackend::BuiltRustls` and is used verbatim.
    /// Without this, these clients would offer no ALPN at all where plain reqwest offered
    /// `http/1.1`, and an origin that requires ALPN would abort the handshake.
    ///
    /// # Errors
    /// Whatever [`Self::client_config`] returns.
    pub fn reqwest_client_config(&self) -> anyhow::Result<rustls::ClientConfig> {
        self.reqwest_client_config_with_identity(None)
    }

    /// [`Self::reqwest_client_config`] presenting `identity` (issue #1609).
    ///
    /// # Errors
    /// Whatever [`Self::client_config`] returns.
    pub fn reqwest_client_config_with_identity(
        &self,
        identity: Option<&ClientIdentity>,
    ) -> anyhow::Result<rustls::ClientConfig> {
        let mut config = self.client_config_with_identity(identity)?;
        config.alpn_protocols = vec![b"http/1.1".to_vec()];
        Ok(config)
    }

    /// A reqwest builder carrying this policy.
    ///
    /// # Errors
    /// Whatever [`Self::client_config`] returns.
    pub fn reqwest_builder(&self) -> anyhow::Result<reqwest::ClientBuilder> {
        self.reqwest_builder_with_identity(None)
    }

    /// A reqwest builder carrying this policy and presenting `identity` (issue #1609).
    ///
    /// # Errors
    /// Whatever [`Self::client_config`] returns.
    pub fn reqwest_builder_with_identity(
        &self,
        identity: Option<&ClientIdentity>,
    ) -> anyhow::Result<reqwest::ClientBuilder> {
        Ok(reqwest::Client::builder()
            .use_preconfigured_tls(self.reqwest_client_config_with_identity(identity)?))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// A self-signed certificate PEM, generated rather than vendored so nothing expires.
    fn a_certificate_pem() -> String {
        rcgen::generate_simple_self_signed(vec!["localhost".to_string()])
            .expect("generate a test certificate")
            .cert
            .pem()
    }

    #[test]
    fn default_policy_builds_from_the_os_trust_store() {
        // The default carries no CA and does not skip verification: it must still produce a usable
        // config on any host with a CA bundle, which is every host CI runs on.
        let config = OutboundTls::default()
            .client_config()
            .expect("the default policy builds");
        // Empty ALPN on the shared base is deliberate: each consumer sets its own (see
        // `reqwest_client_config`), and hyper-rustls' `with_tls_config` asserts on a pre-set value.
        assert!(
            config.alpn_protocols.is_empty(),
            "the shared config must not pin ALPN, got {:?}",
            config.alpn_protocols
        );
    }

    // Issue #974: reqwest populates ALPN only on its own Rustls path, never for a config passed
    // to `use_preconfigured_tls`. Without this the proxy/config-source clients would offer no
    // ALPN where plain reqwest offered `http/1.1`.
    #[test]
    fn the_reqwest_config_offers_http11_alpn() {
        let config = OutboundTls::default()
            .reqwest_client_config()
            .expect("builds");
        assert_eq!(
            config.alpn_protocols,
            vec![b"http/1.1".to_vec()],
            "reqwest consumers must keep offering http/1.1"
        );
    }

    #[tracing_test::traced_test]
    #[test]
    fn skip_verify_names_the_ca_it_is_ignoring() {
        let _ = OutboundTls {
            ca_pem: Some("-----BEGIN CERTIFICATE-----".into()),
            skip_verify: true,
        }
        .client_config();
        assert!(
            logs_contain("NOT read or used"),
            "an ignored CA must be called out by name, not left to inference"
        );
    }

    #[test]
    fn ca_pem_is_accepted_alongside_the_os_roots() {
        let policy = OutboundTls {
            ca_pem: Some(a_certificate_pem()),
            skip_verify: false,
        };
        assert!(
            policy.client_config().is_ok(),
            "a valid CA PEM must build a config"
        );
    }

    #[test]
    fn ca_pem_without_any_certificate_is_an_error() {
        // A private key, no certificate — the classic wrong-half-of-the-pair mistake. It must be a
        // returned error naming the flag, not a silent no-op that leaves the anchor missing.
        let key_only = rcgen::generate_simple_self_signed(vec!["localhost".to_string()])
            .expect("generate")
            .signing_key
            .serialize_pem();
        let policy = OutboundTls {
            ca_pem: Some(key_only),
            skip_verify: false,
        };
        let err = policy
            .client_config()
            .expect_err("a PEM with no certificate must fail");
        assert!(
            err.to_string().contains("no certificates found"),
            "error must name the problem, got: {err}"
        );
    }

    #[test]
    fn ca_pem_that_is_not_pem_at_all_is_an_error() {
        let policy = OutboundTls {
            ca_pem: Some("clearly not a PEM document".to_string()),
            skip_verify: false,
        };
        let err = policy
            .client_config()
            .expect_err("garbage must not be accepted as a trust anchor");
        assert!(
            err.to_string().contains("no certificates found"),
            "error must name the problem, got: {err}"
        );
    }

    #[test]
    fn empty_ca_pem_is_an_error() {
        let policy = OutboundTls {
            ca_pem: Some(String::new()),
            skip_verify: false,
        };
        assert!(
            policy.client_config().is_err(),
            "an empty CA PEM must not silently mean 'no extra anchor'"
        );
    }

    #[test]
    fn skip_verify_builds_without_consulting_any_trust_store() {
        let policy = OutboundTls {
            ca_pem: None,
            skip_verify: true,
        };
        assert!(
            policy.client_config().is_ok(),
            "skip-verify must not depend on the OS store"
        );
    }

    #[test]
    fn skip_verify_ignores_a_malformed_ca_pem() {
        // Nothing is verified against the anchor in this mode, so a bad value must not fail the
        // build — reporting it would be an error about a setting with no effect.
        let policy = OutboundTls {
            ca_pem: Some("not a pem".to_string()),
            skip_verify: true,
        };
        assert!(
            policy.client_config().is_ok(),
            "skip-verify takes precedence over ca_pem"
        );
    }

    #[test]
    fn is_configured_is_false_only_for_the_untouched_default() {
        assert!(!OutboundTls::default().is_configured());
        assert!(
            OutboundTls {
                ca_pem: Some("x".into()),
                skip_verify: false
            }
            .is_configured()
        );
        assert!(
            OutboundTls {
                ca_pem: None,
                skip_verify: true
            }
            .is_configured()
        );
    }

    #[test]
    fn reqwest_builder_surfaces_the_same_error() {
        let policy = OutboundTls {
            ca_pem: Some("not a pem".to_string()),
            skip_verify: false,
        };
        assert!(
            policy.reqwest_builder().is_err(),
            "the reqwest path must not swallow a policy error"
        );
    }

    #[tracing_test::traced_test]
    #[test]
    fn skip_verify_warns() {
        let _ = OutboundTls {
            ca_pem: None,
            skip_verify: true,
        }
        .client_config();
        assert!(
            logs_contain("verification DISABLED"),
            "disabling verification must be loud"
        );
    }
}