feldera-mtls 0.375.0

Client certificate identities of Feldera components and the server-side checks
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//! Client certificate identities of Feldera components, and the server side of checking
//! them: the compiler, runner, pipeline and coordinator servers with
//! `--require-client-certificates`.
//!
//! The control plane presents a certificate with the URI SAN [`CONTROL_PLANE_CERTIFICATE_URI`],
//! a pipeline one with [`pipeline_certificate_uri`]. The private CA signs both. Pipelines may
//! also admit external clients, such as sidecars, by a URI SAN outside `urn:feldera:`.

use actix_tls::accept::rustls_0_23::TlsStream;
use actix_web::body::{BoxBody, MessageBody};
use actix_web::dev::{Extensions, ServiceRequest, ServiceResponse};
use actix_web::http::{Method, StatusCode};
use actix_web::middleware::Next;
use actix_web::rt::net::TcpStream;
use actix_web::{HttpResponse, ResponseError};
use feldera_types::error::{DetailedError, ErrorResponse};
use rustls::RootCertStore;
use rustls::crypto::CryptoProvider;
use rustls::pki_types::CertificateDer;
use rustls::server::WebPkiClientVerifier;
use rustls::server::danger::ClientCertVerifier;
use serde::Serialize;
use std::any::Any;
use std::borrow::Cow;
use std::fmt;
use std::sync::Arc;
use tracing::warn;
use uuid::Uuid;

/// Environment variable that makes the compiler, runner, pipeline and coordinator servers
/// require client certificates.
pub const REQUIRE_CLIENT_CERTIFICATES_ENV: &str = "FELDERA_REQUIRE_CLIENT_CERTIFICATES";

/// Environment variable with the URI SANs of the extra clients that pipelines admit.
pub const EXTRA_CLIENT_CERTIFICATE_URIS_ENV: &str = "FELDERA_EXTRA_CLIENT_CERTIFICATE_URIS";

/// URI SAN of the control-plane client certificate (API server, runner, compiler replicas).
pub const CONTROL_PLANE_CERTIFICATE_URI: &str = "urn:feldera:control-plane";

/// Prefix of the URI SAN of a pipeline certificate. The pipeline id follows it.
const PIPELINE_CERTIFICATE_URI_PREFIX: &str = "urn:feldera:pipeline:";

/// Namespace of the URI SANs of Feldera components. URN namespaces are case-insensitive.
const FELDERA_URI_PREFIX: &str = "urn:feldera:";

fn is_feldera_uri(uri: &str) -> bool {
    uri.get(..FELDERA_URI_PREFIX.len())
        .is_some_and(|prefix| prefix.eq_ignore_ascii_case(FELDERA_URI_PREFIX))
}

/// URI SAN of the certificate of pipeline `pipeline_id`.
pub fn pipeline_certificate_uri(pipeline_id: Uuid) -> String {
    format!("{PIPELINE_CERTIFICATE_URI_PREFIX}{pipeline_id}")
}

/// Who called a server, according to the client certificate of the connection.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Caller {
    /// No client certificate, or a certificate without exactly one identity.
    Anonymous,
    ControlPlane,
    Pipeline(Uuid),
    /// A client outside Feldera, by its URI SAN.
    External(String),
}

impl Caller {
    /// Maps a certificate to its caller. Only a certificate that the TLS handshake verified
    /// proves the identity.
    pub fn from_certificate(cert: &CertificateDer<'_>) -> Self {
        let Ok(cert) = webpki::EndEntityCert::try_from(cert) else {
            return Self::Anonymous;
        };
        let mut callers = cert.valid_uri_names().filter_map(|uri| {
            if uri == CONTROL_PLANE_CERTIFICATE_URI {
                Some(Self::ControlPlane)
            } else if let Some(id) = uri.strip_prefix(PIPELINE_CERTIFICATE_URI_PREFIX) {
                Uuid::parse_str(id).ok().map(Self::Pipeline)
            } else if is_feldera_uri(uri) {
                // Not an identity that Feldera issues, in any spelling.
                None
            } else {
                Some(Self::External(uri.to_string()))
            }
        });
        match (callers.next(), callers.next()) {
            (Some(caller), None) => caller,
            // A certificate with two identities is ambiguous, so it grants neither.
            _ => Self::Anonymous,
        }
    }

    /// Stores the caller of a new connection, for [`Caller::of`]. Pass it to
    /// `HttpServer::on_connect`.
    pub fn record(connection: &dyn Any, extensions: &mut Extensions) {
        let caller = connection
            .downcast_ref::<TlsStream<TcpStream>>()
            .and_then(|stream| stream.get_ref().1.peer_certificates())
            .and_then(|certs| certs.first())
            .map_or(Self::Anonymous, Self::from_certificate);
        extensions.insert(caller);
    }

    /// The caller of the connection that carried `req`.
    pub fn of(req: &ServiceRequest) -> Self {
        req.conn_data::<Self>().cloned().unwrap_or(Self::Anonymous)
    }
}

/// Runs `next` if `is_allowed` admits the request for the caller of its connection, and
/// refuses it with 401 (no identity) or 403 (another identity) otherwise.
pub async fn authorize<B: MessageBody>(
    server: &'static str,
    is_allowed: impl FnOnce(&Caller, &Method, &str) -> bool,
    req: ServiceRequest,
    next: Next<B>,
) -> Result<ServiceResponse<B>, actix_web::Error> {
    let caller = Caller::of(&req);
    if is_allowed(&caller, req.method(), req.path()) {
        return next.call(req).await;
    }
    let method = req.method().to_string();
    let path = req.path().to_string();
    warn!("The {server} server refused {method} {path} from {caller:?}");
    Err(match caller {
        Caller::Anonymous => ClientAuthError::ClientCertificateRequired {
            server,
            method,
            path,
        },
        _ => ClientAuthError::CallerNotAllowed {
            server,
            caller: format!("{caller:?}"),
            method,
            path,
        },
    }
    .into())
}

/// A request that a server refused because of the client certificate of its connection.
#[derive(Debug, Serialize)]
#[serde(untagged)]
pub enum ClientAuthError {
    ClientCertificateRequired {
        server: &'static str,
        method: String,
        path: String,
    },
    CallerNotAllowed {
        server: &'static str,
        caller: String,
        method: String,
        path: String,
    },
}

impl DetailedError for ClientAuthError {
    fn error_code(&self) -> Cow<'static, str> {
        match self {
            Self::ClientCertificateRequired { .. } => Cow::from("ClientCertificateRequired"),
            Self::CallerNotAllowed { .. } => Cow::from("CallerNotAllowed"),
        }
    }
}

impl fmt::Display for ClientAuthError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::ClientCertificateRequired {
                server,
                method,
                path,
            } => write!(
                f,
                "The {server} server requires a client certificate for {method} {path}: present a certificate that the private CA signed (control plane: --https-client-cert-path)"
            ),
            Self::CallerNotAllowed {
                server,
                caller,
                method,
                path,
            } => write!(
                f,
                "{caller} may not send {method} {path} to the {server} server"
            ),
        }
    }
}

impl std::error::Error for ClientAuthError {}

impl ResponseError for ClientAuthError {
    fn status_code(&self) -> StatusCode {
        match self {
            Self::ClientCertificateRequired { .. } => StatusCode::UNAUTHORIZED,
            Self::CallerNotAllowed { .. } => StatusCode::FORBIDDEN,
        }
    }

    fn error_response(&self) -> HttpResponse<BoxBody> {
        HttpResponse::build(self.status_code()).json(ErrorResponse::from_error_nolog(self))
    }
}

/// The rustls provider that the process installed, or the aws-lc-rs one that Feldera binaries
/// install. With several rustls crypto features compiled in, rustls has no default unless the
/// process installed one.
pub fn crypto_provider() -> Arc<CryptoProvider> {
    CryptoProvider::get_default()
        .cloned()
        .unwrap_or_else(|| Arc::new(rustls::crypto::aws_lc_rs::default_provider()))
}

/// Builds a client certificate verifier that trusts only the first certificate of `ca_chain`,
/// the private CA that issues client certificates, so a client may send its leaf certificate
/// without the chain. The rest of the chain is not trusted, because other CAs under the same
/// root could issue Feldera identities. Clients without a certificate may connect, so that
/// they get an actionable 401 or reach routes open to everyone.
pub fn client_certificate_verifier(
    ca_chain: &[CertificateDer<'static>],
) -> Result<Arc<dyn ClientCertVerifier>, String> {
    let issuing_ca = ca_chain
        .first()
        .ok_or("the CA chain holds no certificate")?;
    let mut roots = RootCertStore::empty();
    roots
        .add(issuing_ca.clone())
        .map_err(|e| format!("invalid CA certificate: {e}"))?;
    WebPkiClientVerifier::builder_with_provider(Arc::new(roots), crypto_provider())
        .allow_unauthenticated()
        .build()
        .map_err(|e| format!("client certificate verifier: {e}"))
}

/// The client verifier and admitted callers of a pipeline or coordinator server. Its
/// certificate chain names the pipeline in the leaf and ends with the private CA chain, which
/// the runner appends, so the server needs no separate CA file.
pub struct PipelineClientAuth {
    pub own: Caller,
    /// URI SANs of the external clients that the server admits like the control plane.
    pub extra_client_uris: Vec<String>,
    pub verifier: Arc<dyn ClientCertVerifier>,
}

impl PipelineClientAuth {
    pub fn from_chain(
        chain: &[CertificateDer<'static>],
        extra_client_uris: &[String],
    ) -> Result<Self, String> {
        // Feldera mints `urn:feldera:` URIs for its own components, so admitting one here
        // could admit another pipeline.
        if let Some(uri) = extra_client_uris
            .iter()
            .find(|uri| uri.is_empty() || uri.contains(char::is_whitespace) || is_feldera_uri(uri))
        {
            return Err(format!(
                "extra client URI '{uri}' must be non-empty, have no whitespace, and lie outside {FELDERA_URI_PREFIX}, which Feldera components use"
            ));
        }
        let (leaf, ca_chain) = chain
            .split_first()
            .ok_or("the certificate file holds no certificate")?;
        let own @ Caller::Pipeline(_) = Caller::from_certificate(leaf) else {
            return Err(format!(
                "the certificate names no pipeline: client certificates need the URI SAN {PIPELINE_CERTIFICATE_URI_PREFIX}<id>, which the runner adds"
            ));
        };
        if ca_chain.is_empty() {
            return Err(
                "the certificate file holds no CA chain after the leaf to verify clients with"
                    .to_string(),
            );
        }
        Ok(Self {
            own,
            extra_client_uris: extra_client_uris.to_vec(),
            verifier: client_certificate_verifier(ca_chain)?,
        })
    }

    /// Whether a pipeline or coordinator server admits `caller`: the control plane, the
    /// pipeline itself (the coordinator calls the hosts of its pipeline), or a listed external
    /// client.
    pub fn is_allowed(&self, caller: &Caller) -> bool {
        match caller {
            Caller::ControlPlane => true,
            Caller::Pipeline(_) => *caller == self.own,
            Caller::External(uri) => self.extra_client_uris.contains(uri),
            Caller::Anonymous => false,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{
        CONTROL_PLANE_CERTIFICATE_URI, Caller, PipelineClientAuth, client_certificate_verifier,
        pipeline_certificate_uri,
    };
    use rcgen::{
        BasicConstraints, CertificateParams, DnType, ExtendedKeyUsagePurpose, IsCa, Issuer,
        KeyPair, SanType,
    };
    use rustls::pki_types::{CertificateDer, UnixTime};
    use uuid::Uuid;

    const PIPELINE_A: Uuid = Uuid::from_u128(0xa);
    const PIPELINE_B: Uuid = Uuid::from_u128(0xb);

    fn ca() -> (Issuer<'static, KeyPair>, CertificateDer<'static>) {
        named_ca("ca", None)
    }

    /// A CA certificate signed by `parent`, or self-signed without one.
    fn named_ca(
        name: &str,
        parent: Option<&Issuer<'static, KeyPair>>,
    ) -> (Issuer<'static, KeyPair>, CertificateDer<'static>) {
        let mut params = CertificateParams::new(Vec::<String>::new()).unwrap();
        params.distinguished_name.push(DnType::CommonName, name);
        params.is_ca = IsCa::Ca(BasicConstraints::Unconstrained);
        let key = KeyPair::generate().unwrap();
        let cert = match parent {
            Some(parent) => params.signed_by(&key, parent),
            None => params.self_signed(&key),
        }
        .unwrap();
        (Issuer::new(params, key), cert.der().clone())
    }

    fn leaf(issuer: &Issuer<'static, KeyPair>, uris: &[String]) -> CertificateDer<'static> {
        let mut params = CertificateParams::default();
        params.subject_alt_names = uris
            .iter()
            .map(|uri| SanType::URI(uri.as_str().try_into().unwrap()))
            .collect();
        params.extended_key_usages = vec![ExtendedKeyUsagePurpose::ClientAuth];
        let key = KeyPair::generate().unwrap();
        params.signed_by(&key, issuer).unwrap().der().clone()
    }

    fn caller_of(uris: &[String]) -> Caller {
        Caller::from_certificate(&leaf(&ca().0, uris))
    }

    const SIDECAR: &str = "urn:example:sidecar";

    /// Certificate URIs map to callers, and a certificate with two identities grants neither.
    #[test]
    fn caller_from_certificate() {
        let control_plane = CONTROL_PLANE_CERTIFICATE_URI.to_string();
        let a = pipeline_certificate_uri(PIPELINE_A);
        let b = pipeline_certificate_uri(PIPELINE_B);
        let sidecar = SIDECAR.to_string();
        assert_eq!(
            caller_of(std::slice::from_ref(&control_plane)),
            Caller::ControlPlane
        );
        assert_eq!(
            caller_of(std::slice::from_ref(&a)),
            Caller::Pipeline(PIPELINE_A)
        );
        assert_eq!(caller_of(&[]), Caller::Anonymous);
        assert_eq!(
            caller_of(&["urn:feldera:pipeline:not-a-uuid".to_string()]),
            Caller::Anonymous
        );
        assert_eq!(
            caller_of(std::slice::from_ref(&sidecar)),
            Caller::External(SIDECAR.to_string())
        );
        assert_eq!(
            caller_of(&[format!("URN:Feldera:pipeline:{PIPELINE_A}")]),
            Caller::Anonymous
        );
        assert_eq!(
            caller_of(&["urn:feldera:sidecar".to_string()]),
            Caller::Anonymous
        );
        assert_eq!(
            caller_of(&[control_plane.clone(), a.clone()]),
            Caller::Anonymous
        );
        assert_eq!(caller_of(&[control_plane, sidecar]), Caller::Anonymous);
        assert_eq!(caller_of(&[a, b]), Caller::Anonymous);
    }

    /// A pipeline server admits the control plane, its own pipeline and the listed external
    /// clients, and needs a CA chain and a pipeline identity in its own certificate file.
    #[test]
    fn pipeline_client_auth() {
        let (issuer, ca_cert) = ca();
        let own_leaf = leaf(&issuer, &[pipeline_certificate_uri(PIPELINE_A)]);
        let chain = [own_leaf.clone(), ca_cert.clone()];
        let auth = PipelineClientAuth::from_chain(&chain, &[SIDECAR.to_string()]).unwrap();
        assert!(auth.is_allowed(&Caller::ControlPlane));
        assert!(auth.is_allowed(&Caller::Pipeline(PIPELINE_A)));
        assert!(auth.is_allowed(&Caller::External(SIDECAR.to_string())));
        assert!(!auth.is_allowed(&Caller::External("urn:example:other".to_string())));
        assert!(!auth.is_allowed(&Caller::Pipeline(PIPELINE_B)));
        assert!(!auth.is_allowed(&Caller::Anonymous));

        let no_external = PipelineClientAuth::from_chain(&chain, &[]).unwrap();
        assert!(!no_external.is_allowed(&Caller::External(SIDECAR.to_string())));

        for invalid in [
            pipeline_certificate_uri(PIPELINE_B),
            format!("URN:FELDERA:pipeline:{PIPELINE_B}"),
            String::new(),
            "urn:example:a urn:example:b".to_string(),
        ] {
            assert!(
                PipelineClientAuth::from_chain(&chain, std::slice::from_ref(&invalid)).is_err(),
                "{invalid}"
            );
        }

        assert!(PipelineClientAuth::from_chain(&[own_leaf], &[]).is_err());
        let server_leaf = leaf(&issuer, &[]);
        assert!(PipelineClientAuth::from_chain(&[server_leaf, ca_cert], &[]).is_err());
    }

    /// Servers trust client certificates from the private CA, the first certificate of the CA
    /// chain, and refuse ones from its root or from another CA under that root.
    #[test]
    fn verifier_trusts_only_private_ca() {
        let (root, root_cert) = named_ca("root", None);
        let (private_ca, private_ca_cert) = named_ca("private", Some(&root));
        let (other_ca, other_ca_cert) = named_ca("other", Some(&root));
        let own_leaf = leaf(&private_ca, &[pipeline_certificate_uri(PIPELINE_A)]);
        let pipeline_chain = [own_leaf, private_ca_cert.clone(), root_cert.clone()];
        let ca_chain = [private_ca_cert, root_cert];
        let control_plane = [CONTROL_PLANE_CERTIFICATE_URI.to_string()];
        let now = UnixTime::now();
        for verifier in [
            client_certificate_verifier(&ca_chain).unwrap(),
            PipelineClientAuth::from_chain(&pipeline_chain, &[])
                .unwrap()
                .verifier,
        ] {
            let from_private_ca = leaf(&private_ca, &control_plane);
            assert!(
                verifier
                    .verify_client_cert(&from_private_ca, &[], now)
                    .is_ok()
            );
            let from_root = leaf(&root, &control_plane);
            assert!(verifier.verify_client_cert(&from_root, &[], now).is_err());
            let from_other_ca = leaf(&other_ca, &control_plane);
            let other_chain = std::slice::from_ref(&other_ca_cert);
            assert!(
                verifier
                    .verify_client_cert(&from_other_ca, other_chain, now)
                    .is_err()
            );
        }
    }
}