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
- Variables
- func Detect(head []byte) string
- func EnvoyFields(line string) (map[string]string, bool)
- func Filter(data []byte, format string, allowed func(ns string) bool, ...) ([]byte, int, error)
- func InClusterName(name string, extra ...string) bool
- func Namespaces(record []byte) []string
- func ProtoName(p string) string
- func ServiceNamespace(host string) string
- func Sort(fs []Flow)
- func SplitHostPort(s string) (string, int)
- func StripQuery(p string) string
- type AWSParser
- type EnvoyPod
- type Flow
- func Aggregate(in []Flow) []Flow
- func FromAgentgateway(f map[string]string) (Flow, bool)
- func ParseAzure(doc []byte) ([]Flow, error)
- func ParseCalico(line []byte) (Flow, bool)
- func ParseCoreDNS(line string, extraDomains ...string) (Flow, bool)
- func ParseEnvoy(line string, pod EnvoyPod) (Flow, bool)
- func ParseFile(r io.Reader, format string) ([]Flow, string, error)
- func ParseGCP(line []byte) (Flow, bool)
- func ParseGenericCSV(r io.Reader, comma rune) ([]Flow, error)
- func ParseGenericJSON(line []byte) (Flow, bool)
- func ParseHubble(line []byte) (Flow, bool)
Constants ¶
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.
const MaxLine = 16 << 20
MaxLine is the longest single record a line-oriented flow file may hold.
Variables ¶
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.
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.
var Formats = []string{FmtHubble, FmtCalico, FmtAWS, FmtGCP, FmtAzure, FmtGeneric}
Formats lists the flow log formats ParseFile understands.
Functions ¶
func Detect ¶
Detect names the format of a flow file from its first bytes: one of Formats, or "" when it is not recognised.
func EnvoyFields ¶
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 ¶
InClusterName reports whether a DNS name is in-cluster, a reverse lookup or search-path noise rather than a real external hostname.
func Namespaces ¶
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 ServiceNamespace ¶
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 ¶
SplitHostPort splits "1.2.3.4:80", "[::1]:80", "host:80" or a bare host.
func StripQuery ¶
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} ...").
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 FromAgentgateway ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
ParseFile reads a flow file in the given format ("" to detect it) and returns its flows, aggregated, with the detected format.
func ParseGCP ¶
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 ¶
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 ¶
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 ¶
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}}