flows

package
v0.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 18, 2026 License: Apache-2.0 Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package flows turns observed traffic from any source into one normalised record: agentgateway and Envoy access logs, CoreDNS query logs, and flow logs exported from a CNI or a cloud network (Cilium Hubble, Calico, AWS, GCP and Azure VPC flow logs, or a generic CSV / JSON Lines file).

Everything here is a pure function over text, so each format is tested with fixture lines taken from the upstream projects. The workspace (package store) decides which log lines to feed in and keeps the results in DuckDB.

Index

Constants

View Source
const (
	SrcZtunnel      = "ztunnel"
	SrcAgentgateway = "agentgateway"
	SrcEnvoy        = "envoy"
	SrcCoreDNS      = "coredns"
	FmtHubble       = "hubble"
	FmtCalico       = "calico"
	FmtAWS          = "aws-vpc"
	FmtGCP          = "gcp-vpc"
	FmtAzure        = "azure-vnet"
	FmtGeneric      = "generic"

	Denied  = "denied"
	Allowed = "allowed"
)

Sources and formats.

View Source
const MaxLine = 16 << 20

MaxLine is the longest single record a line-oriented flow file may hold.

Variables

View Source
var AgentgatewayFields = []string{
	"gateway", "listener", "route", "endpoint", "src.addr",
	"http.method", "http.host", "http.path", "http.status", "tls.sni", "protocol",
	"jwt.sub", "mcp.method.name", "mcp.method", "mcp.target", "gen_ai.tool.name", "mcp.tool", "mcp.tool.name",
	"a2a.method", "gen_ai.provider.name", "llm.provider", "inferencepool.selected_endpoint",
}

AgentgatewayFields are the request log fields FromAgentgateway reads. The workspace selects exactly these from its parsed log lines, grouped, so the same conversion runs on text (key=value) and JSON request logs.

Field names come from agentgateway's request logger (crates/agentgateway/src/telemetry/log.rs): mcp.method.name, mcp.target and gen_ai.tool.name in current builds; mcp.method and mcp.tool in older ones.

View Source
var ClusterDomains = []string{
	"cluster.local", "svc", "in-addr.arpa", "ip6.arpa",
	"internal",
	"localdomain", "lan",

	"default", "kube-system", "kube-public", "kube-node-lease",
}

ClusterDomains are suffixes CoreDNS answers from the cluster itself, or that come from resolv.conf search-path expansion (the pod tries each search domain before the bare name). None of them is an external lookup.

Formats lists the flow log formats ParseFile understands.

Functions

func Detect

func Detect(head []byte) string

Detect names the format of a flow file from its first bytes: one of Formats, or "" when it is not recognised.

func EnvoyFields

func EnvoyFields(line string) (map[string]string, bool)

EnvoyFields parses an Envoy access log line in Istio's default text format or its JSON encoding into the JSON key names. ok is false for other lines.

func Filter

func Filter(data []byte, format string, allowed func(ns string) bool, scrub func(string) string) ([]byte, int, error)

Filter rewrites a flow file for a bundle: records naming a namespace that allowed rejects are dropped, and every kept line goes through scrub (the collector's redactor). JSON array exports become JSON Lines so each record can be judged on its own. Formats with no namespaces (AWS, Azure) are only scrubbed. It returns the new content and how many records were dropped.

func InClusterName

func InClusterName(name string, extra ...string) bool

InClusterName reports whether a DNS name is in-cluster, a reverse lookup or search-path noise rather than a real external hostname.

func Namespaces

func Namespaces(record []byte) []string

Namespaces lists the Kubernetes namespaces a flow record names, for policy filtering: any JSON value under a key ending in "namespace" (hubble, Calico, GCP GKE details, generic), the pod-namespace label hubble carries, or a CSV column named like one.

func ProtoName

func ProtoName(p string) string

ProtoName maps IANA protocol numbers and one-letter codes to names.

func ServiceNamespace

func ServiceNamespace(host string) string

ServiceNamespace returns the namespace of a Kubernetes Service host (name.ns.svc.cluster.local or name.ns.svc), or "".

func Sort

func Sort(fs []Flow)

Sort orders flows by count, then source and destination, for stable output.

func SplitHostPort

func SplitHostPort(s string) (string, int)

SplitHostPort splits "1.2.3.4:80", "[::1]:80", "host:80" or a bare host.

func StripQuery

func StripQuery(p string) string

StripQuery removes the query string and fragment from a path or URL path.

Types

type AWSParser

type AWSParser struct {
	// contains filtered or unexported fields
}

AWSParser reads AWS VPC flow log records. Records in the default format need no header; a custom format is recognised from a header line naming the fields (as written to S3: "version account-id ... srcaddr dstaddr ...", or the format string itself: "${version} ${srcaddr} ...").

func (*AWSParser) Line

func (p *AWSParser) Line(line string) (Flow, bool)

Line parses one record. The header line itself, NODATA and SKIPDATA records return ok=false.

2 123456789010 eni-1235b8ca123456789 172.31.16.139 172.31.16.21 20641 22 6 20 4249 1418530010 1418530070 ACCEPT OK

type EnvoyPod

type EnvoyPod struct {
	Namespace, Pod string
	// Gateway is true for ingress/egress gateways and waypoints: their
	// outbound lines are on behalf of the downstream client, not the pod.
	Gateway bool
	Via     string // namespace/name of the gateway or waypoint, when Gateway
}

EnvoyPod describes the pod whose log a line came from.

type Flow

type Flow struct {
	Cluster string `json:"cluster"`
	Source  string `json:"source"` // ztunnel, agentgateway, envoy, coredns, hubble, calico, aws-vpc, gcp-vpc, azure-vnet, generic

	SrcIP        string `json:"srcIp,omitempty"`
	SrcNamespace string `json:"srcNamespace,omitempty"`
	SrcPod       string `json:"srcPod,omitempty"`
	SrcIdentity  string `json:"srcIdentity,omitempty"` // SPIFFE ID or JWT subject when known

	DstIP        string `json:"dstIp,omitempty"`
	DstPort      int    `json:"dstPort,omitempty"`
	DstHost      string `json:"dstHost,omitempty"` // service host, HTTP host, SNI or DNS name
	DstNamespace string `json:"dstNamespace,omitempty"`
	DstWorkload  string `json:"dstWorkload,omitempty"` // pod name when known

	Protocol string `json:"protocol,omitempty"` // tcp, udp, icmp, http, grpc, mcp, a2a, llm, dns
	Method   string `json:"method,omitempty"`   // HTTP method, MCP/A2A method, or DNS query type
	Path     string `json:"path,omitempty"`     // without the query string
	Tool     string `json:"tool,omitempty"`     // MCP tool name
	Route    string `json:"route,omitempty"`    // gateway route
	Via      string `json:"via,omitempty"`      // gateway or waypoint that carried it
	Verdict  string `json:"verdict,omitempty"`  // "denied" when refused; flow logs also say "allowed"
	Count    int    `json:"count"`

	// Direction is "inbound" or "outbound" when the source logs one side of a
	// connection (Envoy sidecars log both ends of an in-mesh request).
	Direction string `json:"direction,omitempty"`
	// File is the flow log file a record came from ("" for capture logs).
	File string `json:"file,omitempty"`
	// Origin says where a record came from: "logs" (derived from captured
	// logs), "bundle" (a flow file attached at collection, covered by the
	// manifest) or "reviewer" (added when the bundle was opened; not signed).
	Origin string `json:"origin,omitempty"`
}

Flow is one observed connection or request, aggregated: Count is the number of lines, connections or flow records it stands for. Endpoints are named when the source names them and otherwise carried as IPs.

func Aggregate

func Aggregate(in []Flow) []Flow

Aggregate merges flows that differ only in Count, keeping first-seen order.

func FromAgentgateway

func FromAgentgateway(f map[string]string) (Flow, bool)

FromAgentgateway converts one agentgateway request log line (its parsed fields) into a flow. ok is false when the fields are not a request log.

info request gateway=agentgateway-system/ar-ingress listener=http route=ar-keycloak/keycloak
  endpoint=10.244.1.131:8080 src.addr=10.244.1.1:29278 http.method=GET
  http.host=keycloak.example.com http.path=/realms/x http.status=200 protocol=http

func ParseAzure

func ParseAzure(doc []byte) ([]Flow, error)

ParseAzure converts an Azure flow log blob: NSG flow logs (version 1 and 2, records[].properties.flows[].flows[].flowTuples) or VNet flow logs (records[].flowRecords.flows[].flowGroups[].flowTuples). A blob is one JSON document, so this takes the whole file.

NSG v2 tuple: ts,srcIP,dstIP,srcPort,dstPort,proto(T|U),dir(I|O),decision(A|D),state(B|C|E),pkts,bytes,pkts,bytes VNet tuple: ts,srcIP,dstIP,srcPort,dstPort,proto(6|17),dir(I|O),state(B|C|E|D),encryption,pkts,bytes,pkts,bytes

Only tuples that begin a flow (state B), or deny it, are counted; C and E are updates to a flow already counted.

func ParseCalico

func ParseCalico(line []byte) (Flow, bool)

ParseCalico converts one Calico Enterprise / Calico Cloud flow log record (JSON, as written to /var/log/calico/flowlogs/flows.log or exported from the tigera_secure_ee_flows index).

Calico aggregates by prefix, so names can end in "*" (source_name_aggr) and the full name may be "-". Both ends report a flow (reporter src and dst); an allowed flow between two workload endpoints is counted from the source side only so it is not doubled, while a denial is kept from whichever side denied it.

{"start_time":1700000000,"end_time":1700000300,"source_ip":"10.0.1.5","source_name":"-",
 "source_name_aggr":"frontend-7d9f8c-*","source_namespace":"shop","source_type":"wep",
 "dest_ip":"10.0.2.9","dest_name_aggr":"checkout-5c6b-*","dest_namespace":"shop","dest_type":"wep",
 "dest_port":8080,"proto":"tcp","action":"allow","reporter":"src","num_flows":3}

func ParseCoreDNS

func ParseCoreDNS(line string, extraDomains ...string) (Flow, bool)

ParseCoreDNS converts one CoreDNS query log line into a flow for an external name. In-cluster names and search-path noise return ok=false.

func ParseEnvoy

func ParseEnvoy(line string, pod EnvoyPod) (Flow, bool)

ParseEnvoy converts an Envoy access log line from a sidecar, gateway or waypoint into a flow.

  • outbound|8081||artifactory.tools.svc.cluster.local: the destination host and port; the source is the pod itself (sidecar) or the downstream client (gateway, waypoint).
  • inbound|8080||: the pod is the destination; the downstream remote address is the caller.
  • inbound-vip|8080|http|reviews.default.svc.cluster.local (waypoints).
  • PassthroughCluster / BlackHoleCluster: traffic to hosts outside the registry; BlackHole means it was refused (REGISTRY_ONLY).

func ParseFile

func ParseFile(r io.Reader, format string) ([]Flow, string, error)

ParseFile reads a flow file in the given format ("" to detect it) and returns its flows, aggregated, with the detected format.

func ParseGCP

func ParseGCP(line []byte) (Flow, bool)

ParseGCP converts one GCP VPC flow log (or firewall rules log) entry as exported from Cloud Logging: the LogEntry with jsonPayload, or the payload alone.

{"jsonPayload":{"connection":{"src_ip":"10.8.0.14","src_port":41234,"dest_ip":"10.8.1.3","dest_port":8080,"protocol":6},
  "reporter":"SRC","src_gke_details":{"pod":{"pod_name":"web-6d4","pod_namespace":"shop"}},
  "dest_gke_details":{"pod":{"pod_name":"api-7f9","pod_namespace":"shop"}}},
 "logName":"projects/p/logs/compute.googleapis.com%2Fvpc_flows"}

Both ends of a flow can report it; a DEST-reported record whose source is also a VM or pod in the VPC is skipped so the flow counts once.

func ParseGenericCSV

func ParseGenericCSV(r io.Reader, comma rune) ([]Flow, error)

ParseGenericCSV reads a CSV (or TSV) file whose header row names generic columns.

src_ip,src_namespace,src_pod,dst_host,dst_port,protocol,action,count
10.244.1.7,payments,checkout-7d9,api.stripe.com,443,tcp,allow,12

func ParseGenericJSON

func ParseGenericJSON(line []byte) (Flow, bool)

ParseGenericJSON converts one JSON Lines record with generic column names.

{"src_namespace":"payments","src_pod":"checkout-7d9","dst_host":"api.stripe.com","dst_port":443,"protocol":"tcp","action":"allow","count":12}

func ParseHubble

func ParseHubble(line []byte) (Flow, bool)

ParseHubble converts one line of `hubble observe -o json` or `-o jsonpb`. Replies (is_reply) are skipped: they are the return leg of a flow already counted. ok is false for other lines (including hubble's own lost-events notices).

{"flow":{"verdict":"FORWARDED","IP":{"source":"10.244.1.107","destination":"10.244.2.98"},
  "l4":{"TCP":{"source_port":52262,"destination_port":80}},
  "source":{"namespace":"default","pod_name":"xwing"},"destination":{"namespace":"default","pod_name":"deathstar-..."},
  "Type":"L3_L4","traffic_direction":"EGRESS","is_reply":false}}

func (Flow) DstName

func (f Flow) DstName() string

DstName is the best label for the destination, with the port when known.

func (Flow) SrcName

func (f Flow) SrcName() string

SrcName is the best label for the source: namespace/pod, identity or IP.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL