Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ bun run kafka:up # optional: Kafka on localhost:9092 with demo data
bun run tauri dev # first build compiles librdkafka; takes a few minutes
```

Then add a cluster with bootstrap `localhost:9092`.
Then add a cluster with bootstrap `localhost:9092`. [docs/LOCAL_KAFKA.md](docs/LOCAL_KAFKA.md) lists the demo
topics and ACLs, explains the broker's authorizer setup, and shows how to add seed data. `bun run kafka:acl-demo`
logs in as the demo users and checks the broker enforces the seeded ACLs.

The [Makefile](Makefile) wraps the common steps; on Windows, run it from Git Bash:

Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ Send a message with its own key and headers, or fill a topic with generated test

See how far behind each consumer group is, and reset its offsets when it needs a fresh start.

### Control who can access what

List a cluster's ACLs and filter them by principal, resource or host. Grant or deny operations on topics,
groups, transactional IDs or the cluster, and delete ACLs you no longer need. The brokers need an authorizer:
on KRaft, set `authorizer.class.name=org.apache.kafka.metadata.authorizer.StandardAuthorizer`, and add your
admin principals to `super.users` first so they aren't locked out.

### Make it yours

<img src="docs/screenshots/04_settings.png" alt="The Settings window" width="820" />
Expand Down
26 changes: 26 additions & 0 deletions crates/kui_desktop/src/features/acls/commands.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use tauri::State;

use super::model::AclBinding;
use super::service;
use crate::blocking::run_blocking;
use crate::error::Result;
use crate::state::AppState;

#[tauri::command]
pub async fn list_acls(state: State<'_, AppState>, cluster_id: String) -> Result<Vec<AclBinding>> {
let clients = state.clients(&cluster_id)?;
run_blocking(move || service::list(&clients)).await
}

#[tauri::command]
pub async fn create_acls(state: State<'_, AppState>, cluster_id: String, acls: Vec<AclBinding>) -> Result<()> {
let clients = state.clients(&cluster_id)?;
run_blocking(move || service::create(&clients, &acls)).await
}

/// Deletes exactly the given ACLs; returns how many were removed.
#[tauri::command]
pub async fn delete_acls(state: State<'_, AppState>, cluster_id: String, acls: Vec<AclBinding>) -> Result<usize> {
let clients = state.clients(&cluster_id)?;
run_blocking(move || service::delete(&clients, &acls)).await
}
6 changes: 6 additions & 0 deletions crates/kui_desktop/src/features/acls/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//! ACLs: who may read, write or administer which topics, groups and
//! transactional ids, and the cluster itself.

pub mod commands;
pub mod model;
pub mod service;
4 changes: 4 additions & 0 deletions crates/kui_desktop/src/features/acls/model.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//! The ACL types are plain data shared with the Kafka layer, so the UI sees
//! exactly what `kui_kafka::acl_admin` reads and writes.

pub use kui_kafka::acls::AclBinding;
73 changes: 73 additions & 0 deletions crates/kui_desktop/src/features/acls/service.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use super::model::AclBinding;
use crate::error::{Error, Result};
use kui_kafka::KafkaClients;
use kui_kafka::acl_admin;
use kui_kafka::acls::validate;
use kui_kafka::connection::rejection;

/// Most ACLs one create or delete request may carry. The dialog sends one per
/// chosen operation, so a larger request didn't come from the UI.
const MAX_PER_REQUEST: usize = 100;

/// Every ACL, ordered by resource type, resource, then principal.
pub fn list(clients: &KafkaClients) -> Result<Vec<AclBinding>> {
let mut acls = acl_admin::describe(&clients.admin).map_err(|e| connection_error(clients, e))?;
acls.sort();
Ok(acls)
}

pub fn create(clients: &KafkaClients, acls: &[AclBinding]) -> Result<()> {
check_batch(acls)?;
for acl in acls {
validate(acl)?;
}
Ok(acl_admin::create(&clients.admin, acls).map_err(|e| connection_error(clients, e))?)
}

/// Deletes exactly these ACLs and returns how many Kafka removed.
pub fn delete(clients: &KafkaClients, acls: &[AclBinding]) -> Result<usize> {
check_batch(acls)?;
let deleted = acl_admin::delete(&clients.admin, acls).map_err(|e| connection_error(clients, e))?;
if deleted == 0 {
return Err(Error::msg("No matching ACL was found. It may have been deleted already."));
}
Ok(deleted)
}

fn check_batch(acls: &[AclBinding]) -> Result<()> {
match acls.len() {
0 => Err(Error::msg("No ACLs given")),
n if n > MAX_PER_REQUEST => Err(Error::msg(format!("At most {MAX_PER_REQUEST} ACLs can be changed at once"))),
_ => Ok(()),
}
}

fn connection_error(clients: &KafkaClients, e: kui_kafka::Error) -> kui_kafka::Error {
rejection(&clients.metadata).unwrap_or(e)
}

#[cfg(test)]
mod tests {
use super::*;
use kui_kafka::acls::{Operation, PatternType, Permission, ResourceType};

fn acl() -> AclBinding {
AclBinding {
resource_type: ResourceType::Topic,
resource_name: "orders".into(),
pattern_type: PatternType::Literal,
principal: "User:alice".into(),
host: "*".into(),
operation: Operation::Read,
permission: Permission::Allow,
}
}

#[test]
fn batches_must_be_non_empty_and_bounded() {
assert!(check_batch(&[]).is_err());
assert!(check_batch(&[acl()]).is_ok());
assert!(check_batch(&vec![acl(); MAX_PER_REQUEST]).is_ok());
assert!(check_batch(&vec![acl(); MAX_PER_REQUEST + 1]).is_err());
}
}
1 change: 1 addition & 0 deletions crates/kui_desktop/src/features/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//! Features only depend on `kui_kafka` (shared infra), never on each other.
//! Even `ClusterConfig`, which every feature takes as input, lives in `kafka`.

pub mod acls;
pub mod brokers;
pub mod clusters;
pub mod consumer_groups;
Expand Down
38 changes: 38 additions & 0 deletions crates/kui_desktop/src/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,3 +697,41 @@ async fn a_message_above_the_retained_limit_is_matched_whole() {
assert_eq!(walk("NOT value.late = 'ok'"), [2, 3]);
topics::service::delete(&clients, &[topic]).await.unwrap();
}

#[tokio::test]
#[ignore = "needs a running Kafka broker with an authorizer"]
async fn acls_are_created_listed_and_deleted_exactly() {
use crate::features::acls;
use crate::features::acls::model::AclBinding;
use kui_kafka::acls::{Operation, PatternType, Permission, ResourceType};

let clients = KafkaClients::connect(local_cluster()).unwrap();
let principal = format!("User:kui-it-{}", uuid::Uuid::new_v4());
let acl = |operation| AclBinding {
resource_type: ResourceType::Topic,
resource_name: "kui-it-".into(),
pattern_type: PatternType::Prefixed,
principal: principal.clone(),
host: "*".into(),
operation,
permission: Permission::Allow,
};
let mine = || -> Vec<AclBinding> {
acls::service::list(&clients).unwrap().into_iter().filter(|a| a.principal == principal).collect()
};

acls::service::create(&clients, &[acl(Operation::Read), acl(Operation::Describe)]).unwrap();
// ACL changes reach the broker's cache through the metadata log, a moment after they are accepted.
for _ in 0..20 {
if mine().len() == 2 {
break;
}
tokio::time::sleep(Duration::from_millis(250)).await;
}
assert_eq!(mine(), [acl(Operation::Read), acl(Operation::Describe)]);

// Deleting one ACL leaves its sibling, which differs only in the operation.
assert_eq!(acls::service::delete(&clients, &[acl(Operation::Read)]).unwrap(), 1);
assert!(acls::service::delete(&clients, &[acl(Operation::Read)]).is_err(), "deleting twice should fail");
assert_eq!(acls::service::delete(&clients, &[acl(Operation::Describe)]).unwrap(), 1);
}
5 changes: 4 additions & 1 deletion crates/kui_desktop/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod security_tests;

use tauri::Manager;

use features::{brokers, clusters, consumer_groups, generator, messages, topics};
use features::{acls, brokers, clusters, consumer_groups, generator, messages, topics};
use state::AppState;

#[cfg_attr(mobile, tauri::mobile_entry_point)]
Expand Down Expand Up @@ -74,6 +74,9 @@ pub fn run() {
consumer_groups::commands::delete_consumer_group,
consumer_groups::commands::preview_offset_reset,
consumer_groups::commands::reset_consumer_group_offsets,
acls::commands::list_acls,
acls::commands::create_acls,
acls::commands::delete_acls,
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
Expand Down
1 change: 1 addition & 0 deletions crates/kui_desktop/src/security_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ const READ_ONLY_COMMANDS: &[&str] = &[
"consumer_group_lags",
"topic_consumer_groups",
"preview_offset_reset",
"list_acls",
];

#[test]
Expand Down
Loading
Loading