| Crates.io | spiffe-rustls-tokio |
| lib.rs | spiffe-rustls-tokio |
| version | 0.1.1 |
| created_at | 2026-01-21 23:40:22.000326+00 |
| updated_at | 2026-01-22 04:24:05.926656+00 |
| description | Tokio-native async accept/connect helpers for spiffe-rustls |
| homepage | |
| repository | https://github.com/maxlambrecht/rust-spiffe |
| max_upload_size | |
| id | 2060412 |
| size | 82,199 |
Tokio-native async accept/connect helpers for spiffe-rustls configs.
Integrates tokio-rustls with automatic peer SPIFFE ID extraction. Provides TlsAcceptor and
TlsConnector that return (TlsStream, PeerIdentity) after successful handshakes. Runtime-agnostic
TLS configuration remains in spiffe-rustls.
Add spiffe-rustls-tokio to your Cargo.toml:
[dependencies]
spiffe-rustls-tokio = "0.1"
spiffe-rustls = "0.4"
spiffe = { version = "0.11", features = ["x509-source"] }
spiffe-rustls configurationuse spiffe::X509Source;
use spiffe_rustls::{authorizer, mtls_client};
let source = X509Source::new().await?;
let client_config = mtls_client(source)
.authorize(authorizer::any())
.build()?;
TlsConnector or TlsAcceptoruse spiffe_rustls_tokio::TlsConnector;
use std::sync::Arc;
let connector = TlsConnector::new(Arc::new(client_config));
// ... use connector.connect() or connector.connect_addr()
connect_addr Convenience Methoduse spiffe::X509Source;
use spiffe_rustls::{authorizer, mtls_client};
use spiffe_rustls_tokio::TlsConnector;
use std::net::SocketAddr;
use std::sync::Arc;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let source = X509Source::new().await?;
let client_config = mtls_client(source)
.authorize(authorizer::any())
.build()?;
let connector = TlsConnector::new(Arc::new(client_config));
let addr: SocketAddr = "127.0.0.1:8443".parse()?;
let server_name = rustls::pki_types::ServerName::try_from("example.org")?;
// connect_addr combines TCP connection and TLS handshake
match connector.connect_addr(addr, server_name).await {
Ok((tls_stream, peer_identity)) => {
if let Some(spiffe_id) = peer_identity.spiffe_id() {
println!("Connected (SPIFFE ID: {})", spiffe_id);
}
// Use tls_stream...
}
Err(e) => eprintln!("Connection failed: {}", e),
}
Ok(())
}
TlsAcceptor — Server-side TLS acceptor that extracts peer SPIFFE identityTlsConnector — Client-side TLS connector that extracts peer SPIFFE identityPeerIdentity — Contains the extracted SPIFFE ID from the peer certificateError — Error type for TLS and identity extraction failuresTlsAcceptor::accept() — Accepts a TLS connection and returns (TlsStream, PeerIdentity)TlsConnector::connect() — Establishes a TLS connection and returns (TlsStream, PeerIdentity)TlsConnector::connect_addr() — Convenience method that combines TCP connection and TLS handshakePeerIdentity::spiffe_id() — Returns the peer's SPIFFE ID, if presentThe crate includes complete working examples that demonstrate real mTLS connections using SPIRE.
SPIFFE_ENDPOINT_SOCKET environment variable set (or use default Unix socket path)The examples are located in the examples/ directory:
mtls_tcp_server - A server that accepts mTLS connections and extracts peer SPIFFE IDsmtls_tcp_client - A client that connects to the server and extracts peer SPIFFE IDsTo run the examples:
# Terminal 1: Start the server
cargo run --package spiffe-rustls-tokio --example mtls_tcp_server
# Terminal 2: Run the client
cargo run --package spiffe-rustls-tokio --example mtls_tcp_client
The examples will:
The server listens on 127.0.0.1:8443 by default. Both examples demonstrate:
PeerIdentity::spiffe_id()After a successful TLS handshake, the peer's SPIFFE ID is extracted from their certificate's URI SAN.
According to the SPIFFE specification, an X.509-SVID must contain exactly one SPIFFE ID. When using spiffe-rustls verifiers correctly, this is enforced during the TLS handshake.
API behavior:
PeerIdentity::spiffe_idspiffe_id is None (no error; accept()/connect() succeed)Error::CertParse (accept()/connect() fail)A None value for spiffe_id is unexpected in SPIFFE-compliant configurations and may indicate misconfiguration or a non-SPIFFE peer.
Licensed under the Apache License, Version 2.0.