kfk 0.1.8

A pure Rust Kafka CLI tool for cluster management, topic operations, and message produce/consume
use std::time::Duration;

use kafka_client::{
    AutoOffsetReset, Client, ClientBuilder, ConsumerConfig, KerberosCredentials, ProducerConfig,
    SaslMechanismType, TlsConfig,
};

use crate::config::{ClusterConfig, SaslMechanism, SecurityProtocolType};

use super::CliResult;

/// Create Client from cluster config
pub async fn create_client(config: &ClusterConfig) -> CliResult<Client> {
    let builder = Client::builder(config.brokers.clone())
        .with_client_id("kfk-cli")
        .with_metadata_ttl(Duration::from_secs(30));

    let builder = config.apply_to_builder(builder)?;

    builder
        .build()
        .await
        .map_err(|e| format!("Failed to connect: {e}"))
}

impl ClusterConfig {
    /// Apply this cluster's security configuration to a [`ClientBuilder`].
    pub fn apply_to_builder(&self, builder: ClientBuilder) -> CliResult<ClientBuilder> {
        Ok(match &self.security_protocol {
            SecurityProtocolType::Plaintext => builder.with_plaintext(),
            SecurityProtocolType::Ssl => {
                let tls_cfg = build_kafka_tls_config(&self.tls);
                builder.with_tls_config(tls_cfg)
            }
            SecurityProtocolType::SaslPlaintext => {
                let sasl = require_sasl_config(&self.sasl)?;
                if sasl.mechanism == SaslMechanism::Gssapi {
                    let creds = build_kerberos_credentials(sasl)?;
                    let mut builder = builder.with_kerberos(creds);
                    if let Some(kdc) = &sasl.kdc_host {
                        builder = builder.with_kdc(kdc.clone(), sasl.kdc_port.unwrap_or(88));
                    }
                    if let Some(hostname) = &sasl.broker_hostname {
                        builder = builder.with_broker_hostname(hostname.clone());
                    }
                    builder
                } else {
                    let (mechanism, username, password) = require_sasl_creds(sasl)?;
                    builder.with_sasl(mechanism, username, password)
                }
            }
            SecurityProtocolType::SaslSsl => {
                let tls_cfg = build_kafka_tls_config(&self.tls);
                let sasl = require_sasl_config(&self.sasl)?;
                if sasl.mechanism == SaslMechanism::Gssapi {
                    let creds = build_kerberos_credentials(sasl)?;
                    let mut builder = builder.with_kerberos_tls(tls_cfg, creds);
                    if let Some(kdc) = &sasl.kdc_host {
                        builder = builder.with_kdc(kdc.clone(), sasl.kdc_port.unwrap_or(88));
                    }
                    if let Some(hostname) = &sasl.broker_hostname {
                        builder = builder.with_broker_hostname(hostname.clone());
                    }
                    builder
                } else {
                    let (mechanism, username, password) = require_sasl_creds(sasl)?;
                    builder.with_sasl_tls(tls_cfg, mechanism, username, password)
                }
            }
        })
    }
}

/// Create a consumer with given group_id and offset strategy.
/// When `group_id` is empty, creates a direct-mode consumer (no consumer group).
pub async fn create_consumer(
    client: &Client,
    group_id: &str,
    offset: AutoOffsetReset,
) -> CliResult<kafka_client::Consumer> {
    let mut config = ConsumerConfig::new()
        .with_auto_commit(true)
        .with_auto_commit_interval(Duration::from_millis(5000))
        .with_auto_offset_reset(offset)
        .with_min_bytes(1)
        .with_max_bytes(10 * 1024 * 1024)
        .with_partition_max_bytes(1024 * 1024)
        .with_max_wait(Duration::from_millis(1000))
        .with_session_timeout(Duration::from_millis(45000))
        .with_rebalance_timeout(Duration::from_millis(60000))
        .with_heartbeat_interval(Duration::from_millis(3000))
        .with_assignment_strategy(kafka_client::PartitionAssignmentStrategy::Range);

    if !group_id.is_empty() {
        config = config.with_group_id(group_id.to_string());
    }

    Ok(client.consumer(config))
}

/// Create a producer
#[allow(dead_code)]
pub async fn create_producer(client: &Client) -> kafka_client::Producer {
    let config = ProducerConfig::new()
        .with_acks(1)
        .with_timeout(5000)
        .with_retries(3)
        .with_batch_size(16384)
        .with_linger(50);
    client.producer(config).await
}

/// Map our config TLS to kafka_client TlsConfig
fn build_kafka_tls_config(tls: &Option<crate::config::TlsConfig>) -> TlsConfig {
    match tls {
        Some(cfg) => TlsConfig {
            domain: "localhost".to_string(),
            ca_cert_path: cfg.ca_file.clone(),
            client_cert_path: cfg.cert_file.clone(),
            client_key_path: cfg.key_file.clone(),
        },
        None => TlsConfig {
            domain: "localhost".to_string(),
            ..Default::default()
        },
    }
}

/// Extract SASL credentials or return an error
fn require_sasl_config(
    sasl: &Option<crate::config::SaslConfig>,
) -> Result<&crate::config::SaslConfig, String> {
    sasl.as_ref().ok_or_else(|| {
        "SASL credentials required but not provided (--sasl-username / --sasl-password)".to_string()
    })
}

/// Extract SASL username/password credentials for PLAIN/SCRAM mechanisms
fn require_sasl_creds(
    sasl: &crate::config::SaslConfig,
) -> Result<(SaslMechanismType, &str, &str), String> {
    let mechanism = match sasl.mechanism {
        SaslMechanism::Plain => SaslMechanismType::Plain,
        SaslMechanism::ScramSha256 => SaslMechanismType::ScramSha256,
        SaslMechanism::ScramSha512 => SaslMechanismType::ScramSha512,
        SaslMechanism::Gssapi => {
            return Err(
                "GSSAPI mechanism should be handled via Kerberos credentials, not SASL creds"
                    .to_string(),
            );
        }
    };
    Ok((mechanism, &sasl.username, &sasl.password))
}

/// Build KerberosCredentials from SASL config for GSSAPI authentication
fn build_kerberos_credentials(
    sasl: &crate::config::SaslConfig,
) -> Result<KerberosCredentials, String> {
    let mut creds = KerberosCredentials::new(&sasl.username);
    if let Some(keytab) = &sasl.keytab {
        creds = creds.with_keytab(keytab);
    }
    if let Some(service_name) = &sasl.service_name {
        creds = creds.with_service_name(service_name);
    }
    if let Some(realm) = &sasl.realm {
        creds = creds.with_realm(realm);
    }
    if let Some(hostname) = &sasl.broker_hostname {
        creds = creds.with_broker_hostname(hostname);
    }
    Ok(creds)
}