kafkab

Kafka 集群检查 CLI,给人和 AI agent 用。参考 slsBuddy / dbBuddy / qiyuBuddy 的约定:统一 JSON 信封、显式 profile、权限守卫、审计日志、稳定退出码。

默认只读。 produce 是唯一写操作,必须在 profile 里把 permissions.mode 改成 readwrite 才能执行;拦截发生在连接 broker 之前。

kafkab topics --profile dev --grep '^order'
kafkab group order-consumer --profile dev --topic orders
kafkab messages orders --profile dev --since -15m --grep ERROR

能力边界

命令作用权限 id
cluster查看 broker、controller、clusterIdcluster.describe
topicstopic 列表 + 分区/副本摘要topics.list
topic <name>单 topic 分区、ISR、configstopic.describe
groupsconsumer group 列表和状态groups.list
group <id>成员、assignment、每个分区的 offset 和 laggroup.describe
messages <topic>读取消息,不提交 offsetmessages.read
produce <topic>发送一条消息produce.send(写)
doctor配置、权限策略、连通性、可选 topic 检查doctor.run
config / profile配置文件管理和 profile 列表无需 profile

messages 使用一个随机临时 consumer group,autoCommit: false,读完即断开,不动任何现有 group 的位点。代价是 broker 上会短暂出现一个 kafkab-read-* 空 group,之后由 broker 按 offsets.retention 清理。

安装

cd D:/workspace/tools/kafkaBuddy
npm install
npm run build
npm link # 之后可直接用 kafkab

Node.js >= 20.12。

配置

cp kafkab.config.example.json kafkab.config.json   # 已被 .gitignore 忽略

配置文件查找顺序:

--config → KAFKAB_CONFIG → ./kafkab.config.json → 工具目录的 kafkab.config.json → 用户目录配置(Windows %APPDATA%\kafkab\config.json,其他 ~/.config/kafkab/config.json)。

{
"profiles": {
"dev": {
"brokers": ["localhost:9092"],
"clientId": "kafkab",
"ssl": false,
"timeout": 10000,
"defaultLimit": 20,
"permissions": { "mode": "readonly" }
},
"prod": {
"brokers": ["broker-1:9092", "broker-2:9092"],
"ssl": true,
"sasl": {
"mechanism": "scram-sha-512",
"usernameEnv": "KAFKAB_PROD_USERNAME",
"passwordEnv": "KAFKAB_PROD_PASSWORD"
},
"permissions": {
"mode": "readwrite",
"allow": ["read", "produce.send"],
"deny": ["messages.read"]
}
}
},
"output": { "format": "auto", "maxColWidth": 60 },
"audit": { "enabled": true, "path": "./logs/calls.jsonl" }
}

关键规则

  • 数据命令必须显式指定 profile:--profile dev 或 KAFKAB_PROFILE=dev。没有隐式默认 profile,防止误查错环境。配置里的 defaultProfile 只用于 config/profile 辅助命令的展示场景,数据命令会忽略它。
  • brokers 可用 KAFKAB_BROKERS(逗号分隔)覆盖。
  • sasl.mechanism 支持 plain、scram-sha-256、scram-sha-512。凭据优先从 usernameEnv / passwordEnv 指定的环境变量取,其次全局 KAFKAB_SASL_USERNAME / KAFKAB_SASL_PASSWORD,最后才用内联值。不要把密码写进配置文件。
  • sslCaFile 可选,内容是 CA 证书文本文件路径。
  • timeout 同时用于连接、认证、请求超时,单位毫秒,范围 100–300000。
  • defaultLimit 是 messages 默认读取条数,范围 1–1000。
  • 当前工作目录下的 .env 会自动加载,不覆盖已有环境变量。

kafkab profile list 只显示 profile 名、broker、SASL mechanism 和权限策略,不显示任何凭据值。

权限

permissions 有三层:

字段说明
modereadonly(默认)禁止所有写;readwrite 默认放行
allow非空时只放行匹配的操作,其余拦截
deny优先级最高,命中即拦截

通配语法:*、read、write、topic.*、topics.list、produce.send。

优先级:deny > readonly 对写的硬阻断 > allow 白名单 > 放行。readonly 下即使 allow: ["*"] 也无法 produce。

被拦截时:

{
"ok": false,
"command": "produce",
"code": "PERMISSION",
"reason": "profile &quot;dev&quot; is readonly; write operation produce.send is blocked. ...",
"durationMs": 0
}

退出码 3,且不会连接 broker。

用法

messages

kafkab messages <topic> [--partition n] [--limit n] [--grep regex]
[--from-beginning | --offset n | --since time]
[--timeout ms] [--profile name]

  • 默认从每个分区的最新 offset 开始读,最多 defaultLimit 条,5 秒无数据即返回。
  • --offset 必须配合 --partition,避免对多个分区套用同一个绝对 offset。指定前会检查分区的 log start / log end;越界直接报 USAGE,避免 KafkaJS 把已过期的 offset 静默重置成 latest。
  • --since 接受 ISO 时间、yyyy-MM-dd HH:mm:ss、yyyy-MM-dd,或相对时间 -15m / -2h / -1d / -30s(相对当前时间)。底层用 Kafka 的 fetchTopicOffsetsByTimestamp 定位起点。
  • --grep 是正则,匹配 key 或 value 的文本。
  • 二进制 value 用 base64:... 表示,并标记 binaryValue: true。
  • 超时返回 ok: true,timedOut: true,并给出 warning;不是错误。

成功信封:

{
"ok": true,
"command": "messages",
"profile": "dev",
"topicName": "orders",
"readFrom": "beginning",
"count": 2,
"timedOut": false,
"messages": [
{
"partition": 0,
"offset": "10",
"timestamp": "2026-11-29T10:00:00.000Z",
"key": "k1",
"value": "{&quot;id&quot;:1}",
"binaryKey": false,
"binaryValue": false,
"headers": {},
"size": 9
}
],
"durationMs": 42
}

group

kafkab group order-consumer --profile dev
kafkab group order-consumer --profile dev --topic orders

offsets[] 中 currentOffset 为 null 表示该分区没有已提交位点,lag 也相应为 null。成员 assignment 按 topic:partition 解码;如果 group 没有活跃成员,会扫描前 200 个 topic 寻找位点并给出 warning,--topic 可以收窄范围。

produce

kafkab produce orders --profile prod --key k1 --value '{"id":1}' --header trace=abc
echo '{"id":2}' | kafkab produce orders --profile prod
kafkab produce orders --profile prod -f payload.json

--header k=v 可重复。默认不自动创建 topic,topic 不存在会直接报错。

输出与审计

  • --format auto:TTY 输出表格,管道输出 JSON。agent 场景默认拿 JSON。
  • 错误信封:{ ok:false, command, code, reason, durationMs }。config / profile 的 JSON 输出也带 ok / command / profile(辅助命令无 profile 时为空串),便于统一解析。
  • 单字段最多 4000 字符,数组最多 200 行,对象最多 100 键,整体 payload 最多 512 KB。截断会在 stderr 报 warning,并同步改写 count / topicCount / groupCount / offsetCount,不会出现计数和数组长度不一致。--no-truncate 可关闭。
  • audit.path 或 --audit-log 开启时,每次调用追加一行 JSONL。审计失败只报 warning,不影响命令结果。

退出码

码含义
0成功
1Kafka / 上游业务错误、doctor 的非连接类问题
2用法或配置错误
3权限拦截
4连接失败或超时(doctor 失败也按同一规则映射,并在信封里带 code)
130中断

环境变量

KAFKAB_PROFILE、KAFKAB_CONFIG、KAFKAB_BROKERS、KAFKAB_CLIENT_ID、KAFKAB_TIMEOUT、KAFKAB_FORMAT、KAFKAB_MAX_COL_WIDTH、KAFKAB_AUDIT_LOG、KAFKAB_SASL_USERNAME、KAFKAB_SASL_PASSWORD。

开发

npm run build       # tsc -> dist/
npm test # jest,38 个用例,使用假 gateway
npm run typecheck # tsc --noEmit

测试没有连接真实 Kafka。上线前用 kafkab doctor --profile <name> --topic <topic> 验证真实 broker、ACL 和凭据。

已知限制

  • 不支持 admin 类写操作(创建/删除 topic、重置位点、删记录)。有意为之,保持写面最小。
  • 不支持 AWS IAM / OAuthBearer SASL。需要时再扩展配置模型。
  • messages 会创建临时 consumer group;极严格的环境需要给 kafkab-read-* 授权。