feat(check): MQTT, Kafka and RabbitMQ checks
MQTT signs in and, with a topic, waits for a message (retained ones count) that may have to contain a text. Kafka fetches cluster metadata from one or more brokers, checks that a topic exists with a leader for every partition and can produce a message, with SASL PLAIN or SCRAM. RabbitMQ signs in over AMQP, opens a channel and checks that a queue exists. All connect through the network guard with optional TLS, and the TLS modes and error redaction are now shared with the database checks. Tested against an in-process MQTT broker and real Kafka and RabbitMQ. Refs #94 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
fb03b28df7
commit
8ca56635cd
8 files changed
+545
-20
No files matched your search
@@ -0,0 +1,158 @@
|
||||
package check
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
mqttserver "github.com/mochi-mqtt/server/v2"
|
||||
"github.com/mochi-mqtt/server/v2/hooks/auth"
|
||||
"github.com/mochi-mqtt/server/v2/listeners"
|
||||
"github.com/twmb/franz-go/pkg/kerr"
|
||||
"github.com/twmb/franz-go/pkg/kgo"
|
||||
"github.com/twmb/franz-go/pkg/kmsg"
|
||||
)
|
||||
|
||||
// mqttBroker starts an in-process broker that accepts check/checkpw and
|
||||
// holds a retained status message.
|
||||
func mqttBroker(t *testing.T) string {
|
||||
t.Helper()
|
||||
l, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
addr := l.Addr().String()
|
||||
_ = l.Close()
|
||||
srv := mqttserver.New(&mqttserver.Options{InlineClient: true, Logger: slog.New(slog.NewTextHandler(io.Discard, nil))})
|
||||
ledger := &auth.Ledger{
|
||||
Auth: auth.AuthRules{{Username: "check", Password: "checkpw", Allow: true}},
|
||||
ACL: auth.ACLRules{{Filters: auth.Filters{"#": auth.ReadWrite}}},
|
||||
}
|
||||
if err := srv.AddHook(new(auth.Hook), &auth.Options{Ledger: ledger}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := srv.AddListener(listeners.NewTCP(listeners.Config{ID: "t", Address: addr})); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := srv.Serve(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = srv.Close() })
|
||||
if err := srv.Publish("plant/pump", []byte(`{"state":"running"}`), true, 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return addr
|
||||
}
|
||||
|
||||
func TestBrokerChecks(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
typ Type
|
||||
target func(*testing.T) string
|
||||
good []map[string]any
|
||||
bad map[string]any
|
||||
// fails are working credentials with a failing condition.
|
||||
fails map[string]string
|
||||
}{
|
||||
{"mqtt", MQTT, mqttBroker,
|
||||
[]map[string]any{
|
||||
{"user": "check", "password": "checkpw"},
|
||||
{"user": "check", "password": "checkpw", "topic": "plant/pump", "expect": "running"},
|
||||
},
|
||||
map[string]any{"user": "check", "password": "wrong-pw-123"},
|
||||
map[string]string{
|
||||
`{"user":"check","password":"checkpw","topic":"plant/pump","expect":"stopped"}`: `does not contain "stopped"`,
|
||||
`{"user":"check","password":"checkpw","topic":"plant/silent"}`: "no message on plant/silent",
|
||||
}},
|
||||
{"rabbitmq", RabbitMQ, func(t *testing.T) string { return dbTarget(t, "UPTIME_TEST_RABBITMQ_ADDR") },
|
||||
[]map[string]any{{"user": "check", "password": "checkpw"}},
|
||||
map[string]any{"user": "check", "password": "wrong-pw-123"},
|
||||
map[string]string{
|
||||
`{"user":"check","password":"checkpw","queue":"no-such-queue"}`: "queue no-such-queue",
|
||||
`{"user":"check","password":"checkpw","vhost":"nope"}`: "vhost nope not found",
|
||||
}},
|
||||
{"kafka", Kafka, kafkaWithTopic,
|
||||
[]map[string]any{
|
||||
{},
|
||||
{"topic": "uptimesebi-check", "message": "probe"},
|
||||
{"topic": "uptimesebi-check"},
|
||||
},
|
||||
nil,
|
||||
map[string]string{
|
||||
`{"topic":"no-such-topic-for-sure"}`: "no-such-topic-for-sure",
|
||||
}},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
target := tc.target(t)
|
||||
for _, cfg := range tc.good {
|
||||
if o := runDB(t, tc.typ, target, cfg, true); !o.OK {
|
||||
t.Errorf("%v: %+v", cfg, o)
|
||||
}
|
||||
}
|
||||
if tc.bad != nil {
|
||||
o := runDB(t, tc.typ, target, tc.bad, true)
|
||||
if o.OK || strings.Contains(o.Error, "wrong-pw-123") {
|
||||
t.Errorf("wrong password: %+v", o)
|
||||
}
|
||||
}
|
||||
for cfg, want := range tc.fails {
|
||||
o := runShort(t, tc.typ, target, cfg)
|
||||
if o.OK || !strings.Contains(o.Error, want) {
|
||||
t.Errorf("%s: %+v, want error containing %q", cfg, o, want)
|
||||
}
|
||||
}
|
||||
if o := runDB(t, tc.typ, target, tc.good[0], false); o.OK || !strings.Contains(o.Error, "non-public") && !strings.Contains(o.Error, "timeout") && !strings.Contains(o.Error, "in time") {
|
||||
t.Errorf("private address reached without permission: %+v", o)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// kafkaWithTopic returns the test Kafka with the topic uptimesebi-check.
|
||||
func kafkaWithTopic(t *testing.T) string {
|
||||
addr := dbTarget(t, "UPTIME_TEST_KAFKA_ADDR")
|
||||
client, err := kgo.NewClient(kgo.SeedBrokers(addr))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer client.Close()
|
||||
req := kmsg.NewPtrCreateTopicsRequest()
|
||||
topic := kmsg.NewCreateTopicsRequestTopic()
|
||||
topic.Topic, topic.NumPartitions, topic.ReplicationFactor = "uptimesebi-check", 1, 1
|
||||
req.Topics = append(req.Topics, topic)
|
||||
resp, err := req.RequestWith(context.Background(), client)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := kerr.ErrorForCode(resp.Topics[0].ErrorCode); err != nil && !errors.Is(err, kerr.TopicAlreadyExists) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return addr
|
||||
}
|
||||
|
||||
func TestBrokerValidate(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
typ Type
|
||||
target, cfg string
|
||||
}{
|
||||
{MQTT, "a:1883,b:1883", `{}`},
|
||||
{MQTT, "broker:1883", `{"topic":"plant/#"}`},
|
||||
{MQTT, "broker:1883", `{"expect":"ok"}`},
|
||||
{MQTT, "broker:1883", `{"queue":"q"}`},
|
||||
{Kafka, "a:9092", `{"user":"u","sasl":"gssapi"}`},
|
||||
{Kafka, "a:9092", `{"message":"m"}`},
|
||||
{RabbitMQ, "mq:5672", `{"topic":"t"}`},
|
||||
} {
|
||||
if _, err := Validate(tc.typ, tc.target, []byte(tc.cfg)); err == nil {
|
||||
t.Errorf("Validate(%s, %s, %s) accepted", tc.typ, tc.target, tc.cfg)
|
||||
}
|
||||
}
|
||||
if _, err := Validate(Kafka, "a:9092, b:9092", []byte(`{"user":"u","password":"p"}`)); err != nil {
|
||||
t.Errorf("Kafka with two brokers: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user