-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kafka watcher component in network mapper (#82)
- Loading branch information
Showing
46 changed files
with
2,114 additions
and
289 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
*_gen.go linguist-generated=true | ||
generated.go linguist-generated=true | ||
go.mod linguist-generated=true | ||
go.sum linguist-generated=true | ||
schema.graphql linguist-generated=true | ||
go.sum linguist-generated=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
FROM --platform=linux/amd64 golang:1.19-alpine as buildenv | ||
RUN apk add --no-cache ca-certificates git protoc | ||
RUN apk add build-base libpcap-dev | ||
WORKDIR /src | ||
|
||
# restore dependencies | ||
COPY go.mod go.sum ./ | ||
RUN go mod download | ||
|
||
COPY . . | ||
|
||
FROM buildenv as test | ||
RUN go test ./exp/kafka-watcher/... | ||
|
||
FROM test as builder | ||
ARG TARGETOS | ||
ARG TARGETARCH | ||
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o /main ./exp/kafka-watcher/cmd | ||
|
||
# Use distroless as minimal base image to package the manager binary | ||
# Refer to https://github.com/GoogleContainerTools/distroless for more details | ||
FROM gcr.io/distroless/static:nonroot | ||
COPY --from=builder /main /main | ||
USER 65532:65532 | ||
|
||
ENTRYPOINT ["/main"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/otterize/network-mapper/src/exp/kafka-watcher/pkg/config" | ||
"github.com/otterize/network-mapper/src/exp/kafka-watcher/pkg/logwatcher" | ||
"github.com/otterize/network-mapper/src/exp/kafka-watcher/pkg/mapperclient" | ||
"k8s.io/apimachinery/pkg/types" | ||
"strings" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func parseKafkaServers(serverNames []string) ([]types.NamespacedName, error) { | ||
var servers []types.NamespacedName | ||
for _, serverName := range serverNames { | ||
nameParts := strings.Split(serverName, ".") | ||
if len(nameParts) != 2 { | ||
return nil, fmt.Errorf("error parsing server pod name %s - should be formatted as 'name.namespace'", serverName) | ||
} | ||
servers = append(servers, types.NamespacedName{ | ||
Name: nameParts[0], | ||
Namespace: nameParts[1], | ||
}) | ||
} | ||
return servers, nil | ||
} | ||
|
||
func main() { | ||
if viper.GetBool(config.DebugKey) { | ||
logrus.SetLevel(logrus.DebugLevel) | ||
} | ||
|
||
kafkaServers, err := parseKafkaServers(viper.GetStringSlice(config.KafkaServersKey)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
mapperClient := mapperclient.NewMapperClient(viper.GetString(config.MapperApiUrlKey)) | ||
w, err := logwatcher.NewWatcher( | ||
mapperClient, | ||
kafkaServers, | ||
) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if err := w.RunForever(context.Background()); err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package config | ||
|
||
import ( | ||
"github.com/spf13/viper" | ||
"strings" | ||
"time" | ||
) | ||
|
||
const ( | ||
EnvPrefix = "OTTERIZE" | ||
MapperApiUrlKey = "mapper-api-url" | ||
MapperApiUrlDefault = "http://mapper:9090/query" | ||
ReportIntervalKey = "report-interval" | ||
ReportIntervalDefault = 10 * time.Second | ||
CallsTimeoutKey = "calls-timeout" | ||
CallsTimeoutDefault = 5 * time.Second | ||
CooldownIntervalKey = "cooldown-interval" | ||
CooldownIntervalDefault = 10 * time.Second | ||
DebugKey = "debug" | ||
DebugDefault = false | ||
|
||
KafkaServersKey = "kafka-servers" | ||
) | ||
|
||
func init() { | ||
viper.SetDefault(MapperApiUrlKey, MapperApiUrlDefault) | ||
viper.SetDefault(ReportIntervalKey, ReportIntervalDefault) | ||
viper.SetDefault(CallsTimeoutKey, CallsTimeoutDefault) | ||
viper.SetDefault(CooldownIntervalKey, CooldownIntervalDefault) | ||
viper.SetDefault(DebugKey, DebugDefault) | ||
viper.SetDefault(KafkaServersKey, []string{}) | ||
viper.SetEnvPrefix(EnvPrefix) | ||
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) | ||
viper.AutomaticEnv() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
package logwatcher | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"errors" | ||
"github.com/oriser/regroup" | ||
"github.com/otterize/network-mapper/src/exp/kafka-watcher/pkg/config" | ||
mapperclient2 "github.com/otterize/network-mapper/src/exp/kafka-watcher/pkg/mapperclient" | ||
"github.com/samber/lo" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/viper" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
"math" | ||
"sync" | ||
"time" | ||
) | ||
|
||
// AclAuthorizerRegex matches & decodes AclAuthorizer log records. | ||
// Sample log record for reference: | ||
// [2023-03-12 13:51:55,904] INFO Principal = User:2.5.4.45=#13206331373734376636373865323137613636346130653335393130326638303662,CN=myclient.otterize-tutorial-kafka-mtls,O=SPIRE,C=US is Denied Operation = Describe from host = 10.244.0.27 on resource = Topic:LITERAL:mytopic for request = Metadata with resourceRefCount = 1 (kafka.authorizer.logger) | ||
var AclAuthorizerRegex = regroup.MustCompile( | ||
`^\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d+\] [A-Z]+ Principal = \S+ is (?P<access>\S+) Operation = (?P<operation>\S+) from host = (?P<host>\S+) on resource = Topic:LITERAL:(?P<topic>.+) for request = \S+ with resourceRefCount = \d+ \(kafka\.authorizer\.logger\)$`, | ||
) | ||
|
||
type AuthorizerRecord struct { | ||
Server types.NamespacedName | ||
Access string `regroup:"access"` | ||
Operation string `regroup:"operation"` | ||
Host string `regroup:"host"` | ||
Topic string `regroup:"topic"` | ||
} | ||
|
||
type SeenRecordsStore map[AuthorizerRecord]time.Time | ||
|
||
type Watcher struct { | ||
clientset *kubernetes.Clientset | ||
mu sync.Mutex | ||
seen SeenRecordsStore | ||
mapperClient mapperclient2.MapperClient | ||
kafkaServers []types.NamespacedName | ||
} | ||
|
||
func NewWatcher(mapperClient mapperclient2.MapperClient, kafkaServers []types.NamespacedName) (*Watcher, error) { | ||
conf, err := rest.InClusterConfig() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cs, err := kubernetes.NewForConfig(conf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
w := &Watcher{ | ||
clientset: cs, | ||
mu: sync.Mutex{}, | ||
seen: SeenRecordsStore{}, | ||
mapperClient: mapperClient, | ||
kafkaServers: kafkaServers, | ||
} | ||
|
||
return w, nil | ||
} | ||
|
||
func (w *Watcher) processLogRecord(kafkaServer types.NamespacedName, record string) { | ||
r := AuthorizerRecord{ | ||
Server: kafkaServer, | ||
} | ||
if err := AclAuthorizerRegex.MatchToTarget(record, &r); errors.Is(err, ®roup.NoMatchFoundError{}) { | ||
return | ||
} else if err != nil { | ||
logrus.Errorf("Error matching authorizer regex: %s", err) | ||
return | ||
} | ||
|
||
w.mu.Lock() | ||
defer w.mu.Unlock() | ||
w.seen[r] = time.Now() | ||
} | ||
|
||
func (w *Watcher) WatchOnce(ctx context.Context, kafkaServer types.NamespacedName) error { | ||
podLogOpts := corev1.PodLogOptions{ | ||
Follow: true, | ||
SinceSeconds: lo.ToPtr(int64(math.Ceil(viper.GetDuration(config.CooldownIntervalKey).Seconds()))), | ||
} | ||
req := w.clientset.CoreV1().Pods(kafkaServer.Namespace).GetLogs(kafkaServer.Name, &podLogOpts) | ||
reader, err := req.Stream(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer reader.Close() | ||
|
||
s := bufio.NewScanner(reader) | ||
s.Split(bufio.ScanLines) | ||
for s.Scan() { | ||
w.processLogRecord(kafkaServer, s.Text()) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (w *Watcher) WatchForever(ctx context.Context, kafkaServer types.NamespacedName) { | ||
log := logrus.WithField("pod", kafkaServer) | ||
cooldownPeriod := viper.GetDuration(config.CooldownIntervalKey) | ||
for { | ||
log.Info("Watching logs") | ||
err := w.WatchOnce(ctx, kafkaServer) | ||
if err != nil { | ||
log.WithError(err).Error("Error watching logs") | ||
} | ||
log.Infof("Watcher stopped, will retry after cooldown period (%s)...", cooldownPeriod) | ||
time.Sleep(cooldownPeriod) | ||
} | ||
} | ||
|
||
func (w *Watcher) Flush() SeenRecordsStore { | ||
w.mu.Lock() | ||
defer w.mu.Unlock() | ||
r := w.seen | ||
w.seen = SeenRecordsStore{} | ||
return r | ||
} | ||
|
||
func (w *Watcher) ReportResults(ctx context.Context) error { | ||
records := w.Flush() | ||
logrus.Infof("Reporting %d records", len(records)) | ||
|
||
results := lo.MapToSlice(records, func(r AuthorizerRecord, t time.Time) mapperclient2.KafkaMapperResult { | ||
return mapperclient2.KafkaMapperResult{ | ||
SrcIp: r.Host, | ||
ServerPodName: r.Server.Name, | ||
ServerNamespace: r.Server.Namespace, | ||
Topic: r.Topic, | ||
Operation: r.Operation, | ||
LastSeen: t, | ||
} | ||
}) | ||
|
||
return w.mapperClient.ReportKafkaMapperResults(ctx, mapperclient2.KafkaMapperResults{Results: results}) | ||
} | ||
|
||
func (w *Watcher) RunForever(ctx context.Context) error { | ||
for _, kafkaServer := range w.kafkaServers { | ||
go w.WatchForever(ctx, kafkaServer) | ||
} | ||
|
||
for { | ||
time.Sleep(viper.GetDuration(config.ReportIntervalKey)) | ||
if err := w.ReportResults(ctx); err != nil { | ||
logrus.WithError(err).Errorf("Failed reporting watcher results to mapper") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package mapperclient | ||
|
||
import ( | ||
"context" | ||
"github.com/Khan/genqlient/graphql" | ||
"net/http" | ||
) | ||
|
||
type MapperClient interface { | ||
ReportKafkaMapperResults(ctx context.Context, results KafkaMapperResults) error | ||
} | ||
|
||
type mapperClientImpl struct { | ||
mapperAddress string | ||
gqlClient graphql.Client | ||
} | ||
|
||
func NewMapperClient(mapperAddress string) MapperClient { | ||
return &mapperClientImpl{ | ||
mapperAddress: mapperAddress, | ||
gqlClient: graphql.NewClient(mapperAddress, http.DefaultClient), | ||
} | ||
} | ||
|
||
func (c *mapperClientImpl) ReportKafkaMapperResults(ctx context.Context, results KafkaMapperResults) error { | ||
_, err := reportKafkaMapperResults(ctx, c.gqlClient, results) | ||
return err | ||
} |
Oops, something went wrong.