Open-source AI Security Operations Center: alert fusion, LLM-agent triage, MITRE ATT&CK investigation, and a replayable decision ledger for every agent step. Self-hostable, runs with no API keys, MIT licensed. Ships an MCP server for Claude, Cursor and Continue.
Python agentic-aiai-securityalert-triageblue-teamcybersecuritydetection-engineeringincident-responselanggraphllm-agentsmcpmitre-attackmodel-context-protocolpurple-teamsecurity-operationsself-hostedsiemsoarsocthreat-detectionthreat-intelligence
feat(ingest): Kafka input for customer topics (depth 4.2)
Beenu Arora3c6960547314
diff --git a/services/ingest/internal/kafkainput/kafkainput.go b/services/ingest/internal/kafkainput/kafkainput.gonew file mode 100644index 000000000..a50d539fb--- /dev/null+++ b/services/ingest/internal/kafkainput/kafkainput.go@@ -0,0 +1,442 @@+// Package kafkainput consumes a customer's own Kafka topics and feeds them+// into the normal ingest path.+//+// Depth item 4.2 / parity 6.10. Some estates already have every log they+// care about on a broker, and asking them to stand up a forwarder that+// re-publishes those records over HTTP is asking them to run a second+// pipeline to feed the first one.+//+// Where the tenant comes from+// ---------------------------+// From a minted ingest token, and from nowhere else. This is the same+// reasoning the syslog listener records: a Kafka record carries no+// authenticated principal, so a tenant read out of the payload or a header+// is caller-controlled, and a tenant a producer can set is a cross-tenant+// write waiting for someone to notice. One source therefore serves one+// tenant, and a deployment reading several tenants' topics declares one+// source per tenant.+//+// What this loop does about failure+// ---------------------------------+// Everything in internal/streamhealth, which is shared with the graph+// broadcaster. Briefly: a dial failure is transient and backs off; a+// `TopicAuthorizationFailed` is permanent, stops the loop and names the+// operator action; an undecodable record is poison and is skipped without+// penalising a subscription that is demonstrably working. Readiness names+// every source and its state, including when it is healthy, because a+// consumer detached from its topic behind a 200 that never mentions it is+// indistinguishable from an idle one.+package kafkainput++import (+ "context"+ "encoding/json"+ "errors"+ "fmt"+ "strings"+ "sync"+ "time"++ "github.com/beenuar/aisoc/services/ingest/internal/inbox"+ "github.com/beenuar/aisoc/services/ingest/internal/kafkatls"+ "github.com/beenuar/aisoc/services/ingest/internal/normalizer"+ "github.com/beenuar/aisoc/services/ingest/internal/streamhealth"+ "github.com/google/uuid"+ "github.com/prometheus/client_golang/prometheus"+ "github.com/prometheus/client_golang/prometheus/promauto"+ "github.com/rs/zerolog/log"+ kafka "github.com/segmentio/kafka-go"+)++// Names is the subsystem's half of the shared health machinery.+var names = streamhealth.Names{+ Subsystem: "kafka_input",+ MetricPrefix: "aisoc_ingest_kafka_input",+ BrokersVar: "AISOC_KAFKA_INPUT_SOURCES[].brokers",+ TopicVar: "AISOC_KAFKA_INPUT_SOURCES[].topic",+ GroupVar: "AISOC_KAFKA_INPUT_SOURCES[].group_id",+}++// Classifier is exported so a test can assert the table this input uses is+// the shared one rather than a second copy.+var Classifier = streamhealth.NewClassifier(names)++var recordsAccepted = promauto.NewCounterVec(prometheus.CounterOpts{+ Name: "aisoc_ingest_kafka_input_records_total",+ Help: "Customer Kafka records accepted, by source name.",+}, []string{"source"})++// SourceConfig describes one customer topic.+//+// Deserialised from AISOC_KAFKA_INPUT_SOURCES, a JSON array, rather than+// from a flat set of environment variables: a deployment that reads three+// tenants' topics needs three of everything, and numbered variables are how+// a fourth source silently gets the third one's token.+type SourceConfig struct {+ // Name identifies the source in logs, metrics and readiness. Required,+ // because "source 2 is detached" is not an operator-facing sentence.+ Name string `json:"name"`+ // Brokers is a comma-separated bootstrap list. Split before it reaches+ // kafka.TCP, which is variadic and panics on a comma-joined string.+ Brokers string `json:"brokers"`+ // Topic is the customer's topic.+ Topic string `json:"topic"`+ // GroupID is this deployment's consumer group on that topic. Defaulted+ // per source rather than per process, so two sources cannot silently+ // join one group and split each other's partitions.+ GroupID string `json:"group_id"`+ // Token is a minted ingest token. The tenant comes from it.+ Token string `json:"token"`+ // TemplateID is the OCSF template each record is projected through.+ TemplateID string `json:"template_id"`+ // FromBeginning replays the topic from its earliest retained offset on+ // the first run of a new group. Off by default: attaching to a+ // customer's production topic and replaying a week of history into the+ // detection pipeline is not a thing to do by accident.+ FromBeginning bool `json:"from_beginning"`+ // MaxBytes caps one fetch. Zero takes the default.+ MaxBytes int `json:"max_bytes"`+}++func (c SourceConfig) validate() error {+ switch {+ case strings.TrimSpace(c.Name) == "":+ return errors.New("a source needs a name; it is what readiness and the logs call it")+ case strings.TrimSpace(c.Brokers) == "":+ return fmt.Errorf("source %q has no brokers", c.Name)+ case strings.TrimSpace(c.Topic) == "":+ return fmt.Errorf("source %q has no topic", c.Name)+ case strings.TrimSpace(c.Token) == "":+ return fmt.Errorf("source %q has no ingest token, so there is no tenant to attribute its records to; mint one with POST /api/v1/inbox/tokens", c.Name)+ case strings.TrimSpace(c.TemplateID) == "":+ return fmt.Errorf("source %q has no template_id, so its records cannot be normalised", c.Name)+ }+ return nil+}++// ParseSources reads the JSON array an operator supplies.+//+// An empty string is "no sources", which is not an error. A non-empty+// string that does not parse *is* an error and the service says so rather+// than starting with the input silently off — the whole class of defect+// this repository keeps finding is a feature that looks configured and is+// not.+func ParseSources(raw string) ([]SourceConfig, error) {+ raw = strings.TrimSpace(raw)+ if raw == "" {+ return nil, nil+ }+ var sources []SourceConfig+ if err := json.Unmarshal([]byte(raw), &sources); err != nil {+ return nil, fmt.Errorf("AISOC_KAFKA_INPUT_SOURCES is not a JSON array of sources: %w", err)+ }+ seen := map[string]bool{}+ for i := range sources {+ if err := sources[i].validate(); err != nil {+ return nil, err+ }+ if seen[sources[i].Name] {+ return nil, fmt.Errorf("two sources are both named %q; readiness reports by name, so they must differ", sources[i].Name)+ }+ seen[sources[i].Name] = true+ if sources[i].GroupID == "" {+ // Per source, not per process. Two sources sharing a group id+ // on different topics is legal in Kafka and is almost never+ // what the operator meant.+ sources[i].GroupID = "aisoc-ingest-" + sources[i].Name+ }+ }+ return sources, nil+}++// TokenResolver is the half of inbox.Store this package needs.+type TokenResolver interface {+ Resolve(ctx context.Context, token string) (*inbox.Token, error)+}++// TemplateSource resolves a template id to a template.+type TemplateSource interface {+ Get(id string) (*inbox.Template, error)+}++// Publisher is the half of publisher.Publisher this package needs.+type Publisher interface {+ PublishBatch(ctx context.Context, events []*normalizer.NormalizedEvent) error+}++// Reader is the half of kafka.Reader this package needs, so the loop can be+// driven in a test without a broker. The thing under test is the failure+// handling, which is where the defects are.+type Reader interface {+ FetchMessage(ctx context.Context) (kafka.Message, error)+ CommitMessages(ctx context.Context, msgs ...kafka.Message) error+ Close() error+}++// Source is one running subscription.+type Source struct {+ cfg SourceConfig+ reader Reader+ tokens TokenResolver+ templates TemplateSource+ pub Publisher+ health *streamhealth.State++ stopOnce sync.Once+ stopCh chan struct{}+ done chan struct{}+}++// NewSource wires one subscription. The reader is built here so a dial+// failure is visible at startup, but the *token* is resolved on first+// delivery: a resolver needs a database, and refusing to start because+// Postgres was slow would take the whole ingest service down with it.+func NewSource(cfg SourceConfig, tokens TokenResolver, templates TemplateSource, pub Publisher) (*Source, error) {+ if err := cfg.validate(); err != nil {+ return nil, err+ }+ if tokens == nil || templates == nil || pub == nil {+ return nil, fmt.Errorf("kafka_input: source %q needs a token resolver, a template source and a publisher", cfg.Name)+ }++ brokers := splitBrokers(cfg.Brokers)+ if len(brokers) == 0 {+ return nil, fmt.Errorf("kafka_input: source %q has no usable broker address", cfg.Name)+ }+ // A nil Dialer is plaintext. Resolve refuses cleartext in a protected+ // environment, and a reader that cannot be built securely must not be+ // built at all.+ transport, err := kafkatls.Resolve()+ if err != nil {+ return nil, fmt.Errorf("kafka_input: source %q transport: %w", cfg.Name, err)+ }++ health := streamhealth.NewState(Classifier, cfg.Topic, 0)+ start := kafka.LastOffset+ if cfg.FromBeginning {+ start = kafka.FirstOffset+ }+ maxBytes := cfg.MaxBytes+ if maxBytes <= 0 {+ maxBytes = 10 * 1024 * 1024+ }+ rd := kafka.NewReader(kafka.ReaderConfig{+ Dialer: transport.Dialer(),+ Brokers: brokers,+ Topic: cfg.Topic,+ GroupID: cfg.GroupID,+ StartOffset: start,+ MinBytes: 1,+ MaxBytes: maxBytes,+ // Without this the reader falls back to a silent logger, and with a+ // GroupID set that is where every dial, join and rebalance failure+ // goes while FetchMessage stays blocked. The single most likely+ // permanent fault — an unreachable or misnamed broker — would be+ // discarded inside the library before this package could see it.+ ErrorLogger: kafka.LoggerFunc(func(format string, args ...interface{}) {+ health.ObserveReaderError(fmt.Sprintf(format, args...))+ }),+ })+ return newSourceWithReader(cfg, rd, tokens, templates, pub, health), nil+}++func newSourceWithReader(cfg SourceConfig, rd Reader, tokens TokenResolver, templates TemplateSource, pub Publisher, health *streamhealth.State) *Source {+ return &Source{+ cfg: cfg,+ reader: rd,+ tokens: tokens,+ templates: templates,+ pub: pub,+ health: health,+ stopCh: make(chan struct{}),+ done: make(chan struct{}),+ }+}++// Name is the operator-facing identifier.+func (s *Source) Name() string { return s.cfg.Name }++// Health is what readiness reports.+func (s *Source) Health() streamhealth.Health { return s.health.Health() }++// Start runs the consume loop until ctx is cancelled, Stop is called, or a+// permanent failure is reached.+func (s *Source) Start(ctx context.Context) {+ go s.run(ctx)+}++// Stop ends the loop and closes the reader.+func (s *Source) Stop() {+ s.stopOnce.Do(func() { close(s.stopCh) })+ <-s.done+ _ = s.reader.Close()+}++func (s *Source) run(ctx context.Context) {+ defer close(s.done)+ s.health.MarkAttached()+ defer func() { s.health.MarkDetached("loop exited") }()++ for {+ select {+ case <-ctx.Done():+ return+ case <-s.stopCh:+ return+ default:+ }++ msg, err := s.reader.FetchMessage(ctx)+ if err != nil {+ if ctx.Err() != nil || streamhealth.IsContextError(err) || s.stopping() {+ return+ }+ class, wait := s.health.RecordFailure(err)+ if class == streamhealth.ClassPermanent {+ // Stop. The wait is meaningless because no number of+ // retries changes the answer, and a loop that keeps+ // retrying a misconfiguration turns it into churn an+ // operator has to filter out of the logs.+ return+ }+ if !streamhealth.SleepFor(ctx, s.stopCh, wait) {+ return+ }+ continue+ }++ if err := s.handle(ctx, msg); err != nil {+ class, wait := s.health.RecordFailure(err)+ switch class {+ case streamhealth.ClassPoison:+ // One record is unreadable and the subscription is fine.+ // Committed so the loop does not re-read it forever, which+ // is the other way a poison record stops a working+ // consumer.+ s.commit(ctx, msg)+ continue+ case streamhealth.ClassPermanent:+ return+ default:+ // Not committed: a transient publish failure means this+ // record has not been delivered, and committing it would+ // lose it. Kafka is the one input in this service that can+ // withhold an acknowledgement, which is exactly why syslog+ // cannot.+ if !streamhealth.SleepFor(ctx, s.stopCh, wait) {+ return+ }+ continue+ }+ }++ s.health.RecordMessage()+ recordsAccepted.WithLabelValues(s.cfg.Name).Inc()+ s.commit(ctx, msg)+ }+}++func (s *Source) commit(ctx context.Context, msg kafka.Message) {+ if err := s.reader.CommitMessages(ctx, msg); err != nil && ctx.Err() == nil {+ // Not fatal and not silent: an uncommitted offset means a replay on+ // restart, which the sink deduplicates, but an operator should know+ // the group is not advancing.+ log.Warn().Err(err).Str("source", s.cfg.Name).Int64("offset", msg.Offset).+ Msg("kafka_input: offset commit failed; these records will be re-read after a restart")+ }+}++// handle projects one record and publishes it.+//+// Errors are classified at the point they are produced rather than by the+// caller guessing: a record that is not JSON is poison, a revoked token is+// permanent, a publish failure is transient.+func (s *Source) handle(ctx context.Context, msg kafka.Message) error {+ var payload map[string]interface{}+ if err := json.Unmarshal(msg.Value, &payload); err != nil {+ // Not every topic carries JSON. A record that is not an object is+ // still usable as a log line, so it is wrapped rather than+ // refused — a plain text topic is a legitimate source, and+ // refusing it would be a receiver that drops everything while+ // looking attached.+ if len(msg.Value) == 0 {+ return streamhealth.Poison(fmt.Errorf("kafka_input: empty record at offset %d", msg.Offset))+ }+ payload = map[string]interface{}{"message": string(msg.Value)}+ }++ tok, err := s.tokens.Resolve(ctx, s.cfg.Token)+ if err != nil {+ // A revoked token and an unreachable database are different+ // problems and only one of them is fixed by waiting, so the+ // resolver's own error text is carried through and classified by+ // the shared table rather than flattened here.+ return fmt.Errorf("kafka_input: resolving the ingest token for source %q: %w", s.cfg.Name, err)+ }+ if tok.TemplateID != s.cfg.TemplateID {+ return streamhealth.Permanent(+ fmt.Errorf("kafka_input: source %q names template %q but its token was minted with %q",+ s.cfg.Name, s.cfg.TemplateID, tok.TemplateID),+ "mint a token with the template this source declares, or change the source's template_id to match",+ )+ }+ tmpl, err := s.templates.Get(s.cfg.TemplateID)+ if err != nil {+ return streamhealth.Permanent(+ fmt.Errorf("kafka_input: this build has no %q template", s.cfg.TemplateID),+ "set template_id to one this build ships; GET /v1/inbox/templates lists them",+ )+ }++ // Kafka's own metadata, carried as fields so a rule can clause on which+ // topic and partition a record came from. Set before Apply so a+ // template that maps them can, and prefixed so they cannot collide with+ // a customer field of the same name.+ payload["kafka_topic"] = msg.Topic+ payload["kafka_partition"] = msg.Partition+ payload["kafka_offset"] = msg.Offset+ if len(msg.Key) > 0 {+ payload["kafka_key"] = string(msg.Key)+ }+ for _, h := range msg.Headers {+ // Headers are a flat namespace the producer controls; they are+ // carried for context and are explicitly *not* read for a tenant.+ payload["kafka_header_"+h.Key] = string(h.Value)+ }++ receivedAt := time.Now().UTC().Format(time.RFC3339Nano)+ tenantID := tok.TenantID.String()+ connectorRef := "kafka-input:" + s.cfg.Name+ ocsf := tmpl.Apply(payload, tenantID, connectorRef, receivedAt)++ return s.pub.PublishBatch(ctx, []*normalizer.NormalizedEvent{{+ ID: uuid.NewString(),+ ConnectorID: connectorRef,+ TenantID: tenantID,+ OcsfEvent: ocsf,+ NormalizationVersion: "kafka-input/1.0",+ }})+}++func (s *Source) stopping() bool {+ select {+ case <-s.stopCh:+ return true+ default:+ return false+ }+}++// splitBrokers turns a comma-joined bootstrap list into the slice+// kafka.ReaderConfig wants. kafka.TCP is variadic and a comma-joined string+// reaches it as one hostname, which panics rather than failing to dial.+func splitBrokers(s string) []string {+ out := []string{}+ for _, part := range strings.Split(s, ",") {+ part = strings.TrimSpace(part)+ if part != "" {+ out = append(out, part)+ }+ }+ return out+}diff --git a/services/ingest/internal/kafkainput/kafkainput_test.go b/services/ingest/internal/kafkainput/kafkainput_test.gonew file mode 100644index 000000000..00f5ae5af--- /dev/null+++ b/services/ingest/internal/kafkainput/kafkainput_test.go@@ -0,0 +1,497 @@+package kafkainput++import (+ "context"+ "errors"+ "strings"+ "sync"+ "testing"+ "time"++ "github.com/beenuar/aisoc/services/ingest/internal/inbox"+ "github.com/beenuar/aisoc/services/ingest/internal/normalizer"+ "github.com/beenuar/aisoc/services/ingest/internal/streamhealth"+ "github.com/google/uuid"+ kafka "github.com/segmentio/kafka-go"+)++// ---------------------------------------------------------------------------+// Doubles. Each is deliberately *less* capable than the real thing where the+// real thing would hide a defect: the token resolver answers only the token+// it was given, and the template source only the template it was given.+// ---------------------------------------------------------------------------++type fakeTokens struct {+ token string+ tok *inbox.Token+ err error+ calls int+}++func (f *fakeTokens) Resolve(_ context.Context, token string) (*inbox.Token, error) {+ f.calls+++ if f.err != nil {+ return nil, f.err+ }+ if token != f.token {+ return nil, errors.New("no such token")+ }+ return f.tok, nil+}++type fakeTemplates struct {+ id string+ tmpl *inbox.Template+}++func (f *fakeTemplates) Get(id string) (*inbox.Template, error) {+ if id != f.id {+ return nil, inbox.ErrTemplateNotFound+ }+ return f.tmpl, nil+}++type fakePublisher struct {+ mu sync.Mutex+ published []*normalizer.NormalizedEvent+ err error+}++func (f *fakePublisher) PublishBatch(_ context.Context, events []*normalizer.NormalizedEvent) error {+ f.mu.Lock()+ defer f.mu.Unlock()+ if f.err != nil {+ return f.err+ }+ f.published = append(f.published, events...)+ return nil+}++func (f *fakePublisher) count() int {+ f.mu.Lock()+ defer f.mu.Unlock()+ return len(f.published)+}++func (f *fakePublisher) at(i int) *normalizer.NormalizedEvent {+ f.mu.Lock()+ defer f.mu.Unlock()+ return f.published[i]+}++// scriptedReader hands out a fixed sequence and then blocks on the context,+// which is what a real reader does on an idle topic.+type scriptedReader struct {+ mu sync.Mutex+ msgs []kafka.Message+ errs []error+ idx int+ committed []kafka.Message+ closed bool+}++func (r *scriptedReader) FetchMessage(ctx context.Context) (kafka.Message, error) {+ r.mu.Lock()+ i := r.idx+ r.idx+++ r.mu.Unlock()+ if i < len(r.errs) && r.errs[i] != nil {+ return kafka.Message{}, r.errs[i]+ }+ if i < len(r.msgs) {+ return r.msgs[i], nil+ }+ <-ctx.Done()+ return kafka.Message{}, ctx.Err()+}++func (r *scriptedReader) CommitMessages(_ context.Context, msgs ...kafka.Message) error {+ r.mu.Lock()+ defer r.mu.Unlock()+ r.committed = append(r.committed, msgs...)+ return nil+}++func (r *scriptedReader) Close() error {+ r.mu.Lock()+ defer r.mu.Unlock()+ r.closed = true+ return nil+}++func (r *scriptedReader) commitCount() int {+ r.mu.Lock()+ defer r.mu.Unlock()+ return len(r.committed)+}++// ---------------------------------------------------------------------------++const (+ testToken = "tok_live_abcdef"+ testTemplate = "generic-json"+)++var testTenant = uuid.MustParse("00000000-0000-0000-0000-0000000000a1")++func testHarness(t *testing.T, msgs []kafka.Message, errs []error) (*Source, *scriptedReader, *fakePublisher, *fakeTokens) {+ t.Helper()+ rd := &scriptedReader{msgs: msgs, errs: errs}+ tokens := &fakeTokens{token: testToken, tok: &inbox.Token{+ Token: testToken, TenantID: testTenant, TemplateID: testTemplate,+ }}+ templates := &fakeTemplates{id: testTemplate, tmpl: &inbox.Template{+ ID: testTemplate,+ ClassUID: 2001,+ ClassName: "Security Finding",+ }}+ pub := &fakePublisher{}+ cfg := SourceConfig{+ Name: "customer-a", Brokers: "broker:9092", Topic: "customer.security",+ GroupID: "aisoc-ingest-customer-a", Token: testToken, TemplateID: testTemplate,+ }+ health := streamhealth.NewState(Classifier, cfg.Topic, 50*time.Millisecond)+ return newSourceWithReader(cfg, rd, tokens, templates, pub, health), rd, pub, tokens+}++func runUntil(t *testing.T, s *Source, cond func() bool) {+ t.Helper()+ ctx, cancel := context.WithCancel(context.Background())+ s.Start(ctx)+ deadline := time.Now().Add(3 * time.Second)+ for time.Now().Before(deadline) && !cond() {+ time.Sleep(5 * time.Millisecond)+ }+ cancel()+ s.Stop()+ if !cond() {+ t.Fatalf("condition never held; last health: %+v", s.Health())+ }+}++// ---------------------------------------------------------------------------+// The happy path, and what it proves about the tenant.+// ---------------------------------------------------------------------------++func TestARecordIsNormalisedAndPublishedUnderTheTokensTenant(t *testing.T) {+ s, rd, pub, _ := testHarness(t, []kafka.Message{{+ Topic: "customer.security", Partition: 3, Offset: 41,+ Key: []byte("host-7"),+ Value: []byte(`{"message":"root login","src_ip":"198.51.100.4"}`),+ }}, nil)++ runUntil(t, s, func() bool { return pub.count() == 1 })++ ev := pub.at(0)+ if ev.TenantID != testTenant.String() {+ t.Fatalf("tenant %q, want %q", ev.TenantID, testTenant)+ }+ if ev.ConnectorID != "kafka-input:customer-a" {+ t.Fatalf("connector ref %q", ev.ConnectorID)+ }+ if rd.commitCount() != 1 {+ t.Fatalf("committed %d offsets, want 1", rd.commitCount())+ }+}++// A tenant a producer can set is a cross-tenant write. The record carries+// one in its payload *and* in a header; neither may win.+func TestATenantInThePayloadOrAHeaderIsIgnored(t *testing.T) {+ attacker := "00000000-0000-0000-0000-00000000beef"+ s, _, pub, _ := testHarness(t, []kafka.Message{{+ Topic: "customer.security", Offset: 1,+ Value: []byte(`{"message":"hello","tenant_id":"` + attacker + `"}`),+ Headers: []kafka.Header{{Key: "tenant_id", Value: []byte(attacker)}},+ }}, nil)++ runUntil(t, s, func() bool { return pub.count() == 1 })++ if got := pub.at(0).TenantID; got != testTenant.String() {+ t.Fatalf("tenant came from the record: %q", got)+ }+}++// ---------------------------------------------------------------------------+// The three classes. This is the whole reason the package exists.+// ---------------------------------------------------------------------------++// Poison: one record is unreadable, the subscription is not. It must be+// committed, or the loop re-reads it forever and a working consumer is+// stopped by one bad producer.+func TestAnEmptyRecordIsPoisonAndDoesNotStopTheSubscription(t *testing.T) {+ s, rd, pub, _ := testHarness(t, []kafka.Message{+ {Topic: "customer.security", Offset: 1, Value: []byte("")},+ {Topic: "customer.security", Offset: 2, Value: []byte(`{"message":"after the bad one"}`)},+ }, nil)++ runUntil(t, s, func() bool { return pub.count() == 1 })++ h := s.Health()+ if h.PoisonMessages != 1 {+ t.Fatalf("poison counted %d, want 1", h.PoisonMessages)+ }+ if h.NotResolving {+ t.Fatal("a poison record marked the subscription as not resolving; the subscription is demonstrably working")+ }+ if rd.commitCount() != 2 {+ t.Fatalf("committed %d, want 2 — a poison record that is not committed is re-read forever", rd.commitCount())+ }+}++// A topic of plain text is a legitimate source. Refusing it would be a+// receiver that looks attached and drops everything.+func TestANonJSONRecordIsCarriedAsALogLineRatherThanRefused(t *testing.T) {+ s, _, pub, _ := testHarness(t, []kafka.Message{{+ Topic: "customer.security", Offset: 1, Value: []byte("Oct 11 09:00:00 host sshd[1]: accepted"),+ }}, nil)++ runUntil(t, s, func() bool { return pub.count() == 1 })++ if s.Health().PoisonMessages != 0 {+ t.Fatal("a plain-text record was counted as poison")+ }+}++// Permanent: the loop stops and names the operator action. Retrying a+// misconfiguration turns it into churn an operator has to filter out.+func TestAPermanentProtocolErrorStopsTheLoopAndNamesTheAction(t *testing.T) {+ s, _, _, _ := testHarness(t, nil, []error{kafka.TopicAuthorizationFailed})++ ctx, cancel := context.WithCancel(context.Background())+ defer cancel()+ s.Start(ctx)++ deadline := time.Now().Add(3 * time.Second)+ for time.Now().Before(deadline) {+ if h := s.Health(); h.PermanentFailure && !h.Attached {+ break+ }+ time.Sleep(5 * time.Millisecond)+ }+ s.Stop()++ h := s.Health()+ if !h.PermanentFailure {+ t.Fatal("a topic-authorization failure was not classified permanent")+ }+ if h.Attached {+ t.Fatal("the loop stayed attached after a permanent failure")+ }+ if !h.NotResolving {+ t.Fatal("a permanent failure must be not-resolving immediately")+ }+ if h.Advice == "" {+ t.Fatal("a permanent verdict with no next move is just a louder silence")+ }+ if !strings.Contains(h.Detail(), "grant this client read") {+ t.Fatalf("readiness detail does not carry the action: %q", h.Detail())+ }+}++// A rebalance happens on every deploy. Stopping on it would turn this into+// an outage, which is why permanence is enumerated rather than derived from+// kafka.Error.Temporary().+func TestARebalanceIsTransientAndTheLoopKeepsGoing(t *testing.T) {+ s, _, pub, _ := testHarness(t,+ []kafka.Message{{}, {Topic: "customer.security", Offset: 9, Value: []byte(`{"message":"after rebalance"}`)}},+ []error{kafka.RebalanceInProgress, nil},+ )++ runUntil(t, s, func() bool { return pub.count() == 1 })++ h := s.Health()+ if h.PermanentFailure {+ t.Fatal("a rebalance stopped the consumer; every deploy would be an outage")+ }+ if h.TransientFailures != 1 {+ t.Fatalf("transient failures %d, want 1", h.TransientFailures)+ }+}++// Transient on the *publish* side must not commit. Kafka is the one input+// in this service that can withhold an acknowledgement.+func TestAPublishFailureDoesNotCommitTheOffset(t *testing.T) {+ s, rd, pub, _ := testHarness(t, []kafka.Message{{+ Topic: "customer.security", Offset: 1, Value: []byte(`{"message":"x"}`),+ }}, nil)+ pub.err = errors.New("kafka publish: broker unreachable")++ ctx, cancel := context.WithCancel(context.Background())+ s.Start(ctx)+ deadline := time.Now().Add(2 * time.Second)+ for time.Now().Before(deadline) && s.Health().TransientFailures == 0 {+ time.Sleep(5 * time.Millisecond)+ }+ cancel()+ s.Stop()++ if s.Health().TransientFailures == 0 {+ t.Fatal("the publish failure was never recorded")+ }+ if rd.commitCount() != 0 {+ t.Fatal("an undelivered record was committed; it is now lost")+ }+}++// A token minted with a different template is a configuration fault that no+// retry clears.+func TestATokenMintedWithAnotherTemplateIsPermanent(t *testing.T) {+ s, _, _, tokens := testHarness(t, []kafka.Message{{+ Topic: "customer.security", Offset: 1, Value: []byte(`{"message":"x"}`),+ }}, nil)+ tokens.tok.TemplateID = "splunk-hec"++ ctx, cancel := context.WithCancel(context.Background())+ s.Start(ctx)+ deadline := time.Now().Add(2 * time.Second)+ for time.Now().Before(deadline) && !s.Health().PermanentFailure {+ time.Sleep(5 * time.Millisecond)+ }+ cancel()+ s.Stop()++ h := s.Health()+ if !h.PermanentFailure {+ t.Fatal("a template mismatch kept retrying")+ }+ if !strings.Contains(h.Advice, "mint a token") {+ t.Fatalf("advice %q does not name the fix", h.Advice)+ }+}++// ---------------------------------------------------------------------------+// Readiness has to be informative when healthy too. "attached" alone is a+// lie of omission: a quiet topic is a legitimate state.+// ---------------------------------------------------------------------------++func TestReadinessDetailIsInformativeWhenHealthy(t *testing.T) {+ s, _, pub, _ := testHarness(t, []kafka.Message{{+ Topic: "customer.security", Offset: 1, Value: []byte(`{"message":"x"}`),+ }}, nil)++ ctx, cancel := context.WithCancel(context.Background())+ s.Start(ctx)+ deadline := time.Now().Add(2 * time.Second)+ for time.Now().Before(deadline) && pub.count() == 0 {+ time.Sleep(5 * time.Millisecond)+ }+ detail := s.Health().Detail()+ cancel()+ s.Stop()++ for _, want := range []string{"customer.security", "received 1"} {+ if !strings.Contains(detail, want) {+ t.Fatalf("healthy readiness detail %q does not mention %q", detail, want)+ }+ }+}++func TestAnIdleSubscriptionSaysNothingHasArrivedRatherThanLookingBroken(t *testing.T) {+ s, _, _, _ := testHarness(t, nil, nil)++ ctx, cancel := context.WithCancel(context.Background())+ s.Start(ctx)+ time.Sleep(50 * time.Millisecond)+ h := s.Health()+ cancel()+ s.Stop()++ if !h.Attached {+ t.Fatal("an idle subscription reported detached")+ }+ if h.NotResolving {+ t.Fatal("an idle subscription reported as not resolving; a quiet topic is legitimate")+ }+ if !strings.Contains(h.Detail(), "nothing has arrived yet") {+ t.Fatalf("idle detail %q does not distinguish quiet from broken", h.Detail())+ }+}++// ---------------------------------------------------------------------------+// Configuration.+// ---------------------------------------------------------------------------++func TestParseSourcesRejectsWhatWouldStartSilentlyBroken(t *testing.T) {+ cases := []struct {+ name string+ raw string+ want string+ }{+ {"not JSON", `{"name":"a"}`, "JSON array"},+ {"no name", `[{"brokers":"b:9092","topic":"t","token":"x","template_id":"generic-json"}]`, "needs a name"},+ {"no brokers", `[{"name":"a","topic":"t","token":"x","template_id":"generic-json"}]`, "no brokers"},+ {"no topic", `[{"name":"a","brokers":"b:9092","token":"x","template_id":"generic-json"}]`, "no topic"},+ {"no token", `[{"name":"a","brokers":"b:9092","topic":"t","template_id":"generic-json"}]`, "no ingest token"},+ {"no template", `[{"name":"a","brokers":"b:9092","topic":"t","token":"x"}]`, "no template_id"},+ {+ "duplicate names",+ `[{"name":"a","brokers":"b:9092","topic":"t","token":"x","template_id":"generic-json"},` ++ `{"name":"a","brokers":"b:9092","topic":"u","token":"y","template_id":"generic-json"}]`,+ "both named",+ },+ }+ for _, tc := range cases {+ t.Run(tc.name, func(t *testing.T) {+ _, err := ParseSources(tc.raw)+ if err == nil {+ t.Fatalf("accepted %s", tc.name)+ }+ if !strings.Contains(err.Error(), tc.want) {+ t.Fatalf("error %q does not mention %q", err, tc.want)+ }+ })+ }+}++func TestAnEmptyConfigurationIsNoSourcesRatherThanAnError(t *testing.T) {+ sources, err := ParseSources(" ")+ if err != nil {+ t.Fatalf("empty configuration errored: %v", err)+ }+ if len(sources) != 0 {+ t.Fatalf("got %d sources", len(sources))+ }+}++// Two sources defaulting to one group id would split each other's+// partitions, which looks like message loss.+func TestGroupIDDefaultsPerSourceNotPerProcess(t *testing.T) {+ sources, err := ParseSources(`[+ {"name":"a","brokers":"b:9092","topic":"t","token":"x","template_id":"generic-json"},+ {"name":"b","brokers":"b:9092","topic":"u","token":"y","template_id":"generic-json"}+ ]`)+ if err != nil {+ t.Fatal(err)+ }+ if sources[0].GroupID == sources[1].GroupID {+ t.Fatalf("both sources defaulted to group %q", sources[0].GroupID)+ }+}++// kafka.TCP is variadic; a comma-joined string reaches it as one hostname+// and panics rather than failing to dial.+func TestBrokerListIsSplitOnCommas(t *testing.T) {+ got := splitBrokers(" a:9092, b:9092 ,, c:9092 ")+ want := []string{"a:9092", "b:9092", "c:9092"}+ if len(got) != len(want) {+ t.Fatalf("split to %v", got)+ }+ for i := range want {+ if got[i] != want[i] {+ t.Fatalf("split to %v, want %v", got, want)+ }+ }+}++// The input must use the shared classifier, not a second copy of the table.+func TestTheInputUsesTheSharedClassifier(t *testing.T) {+ shared := streamhealth.NewClassifier(names)+ if len(Classifier.PermanentCodes()) != len(shared.PermanentCodes()) {+ t.Fatal("the input's permanent set differs from streamhealth's")+ }+ for code, advice := range Classifier.PermanentCodes() {+ if advice == "" {+ t.Fatalf("%v is permanent with no operator action", code)+ }+ }+}