Skip to content

Setup & Connection

Connect to a Danube broker with a gRPC endpoint:

Basic connection
use danube_client::DanubeClient;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = DanubeClient::builder()
.service_url("http://127.0.0.1:6650")
.build()
.await?;
Ok(())
}

Endpoint format: http://host:port or https://host:port for TLS.


For secure production environments, enable TLS encryption:

TLS with custom CA
use danube_client::DanubeClient;
use rustls::crypto;
use tokio::sync::OnceCell;
static CRYPTO_PROVIDER: OnceCell<()> = OnceCell::const_new();
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
CRYPTO_PROVIDER.get_or_init(|| async {
let crypto_provider = crypto::ring::default_provider();
crypto_provider
.install_default()
.expect("Failed to install default CryptoProvider");
})
.await;
let client = DanubeClient::builder()
.service_url("https://127.0.0.1:6650")
.with_tls("./certs/ca-cert.pem")?
.build()
.await?;
Ok(())
}

Requirements:

  • CA certificate file (PEM format)
  • HTTPS URL (https:// instead of http://)
  • Broker must be configured with TLS enabled

When the broker runs with authentication enabled, all client requests must carry a valid JWT token. Tokens are created offline using danube-admin.

Generate JWT tokens
# Create a service account token (default: 1-year TTL)
danube-admin security tokens create \
--subject my-app \
--secret-key your-secret-key
# Create a token with custom TTL and issuer
danube-admin security tokens create \
--subject my-app \
--ttl 24h \
--issuer danube-auth \
--secret-key your-secret-key

The secret_key must match the broker’s jwt.secret_key configuration.

JWT authentication
let token = std::env::var("DANUBE_TOKEN")
.expect("DANUBE_TOKEN environment variable not set");
let client = DanubeClient::builder()
.service_url("https://127.0.0.1:6650")
.with_tls("./certs/ca-cert.pem")?
.with_token(&token)
.build()
.await?;

How it works:

  1. Token is created offline using danube-admin security tokens create
  2. Client sends the token as Authorization: Bearer <token> on every gRPC request
  3. Broker validates the token signature and expiration, then checks RBAC policies

For environments where tokens refresh at runtime (Kubernetes projected volumes, vault-injected secrets), use with_token_supplier. The supplier function is called on every gRPC request:

Token rotation
let client = DanubeClient::builder()
.service_url("https://127.0.0.1:6650")
.with_tls("./certs/ca-cert.pem")?
.with_token_supplier(|| {
std::fs::read_to_string("/var/run/secrets/danube/token")
.unwrap_or_default()
})
.build()
.await?;