Skip to content

Commit acc8033

Browse files
authored
fix(email): observe mail ingress at the receiving port (#6703)
Use the port reached after Kubernetes translates the Service target. Keep the filter and the wire parser bound to the same port. Part of #6005
1 parent 9962898 commit acc8033

5 files changed

Lines changed: 41 additions & 18 deletions

File tree

‎scripts/diagnose-email-startup/ingress_flow.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ var errIngressEvidence = errors.New("inconclusive ingress evidence")
1616

1717
// decodeIngressFlows accepts complete jsonpb observations of new connections
1818
// delivered to the bound application endpoint. It never returns partial proof.
19-
func decodeIngressFlows(data []byte, destination netip.Addr, node string, since, until time.Time) ([]netip.Addr, error) {
19+
func decodeIngressFlows(data []byte, destination netip.Addr, port uint16, node string, since, until time.Time) ([]netip.Addr, error) {
2020
refuse := func() ([]netip.Addr, error) { return nil, errIngressEvidence }
2121
if len(data) == 0 || len(data) > maxAPIOutput || !destination.IsGlobalUnicast() || destination.Is4In6() ||
22-
node == "" || since.IsZero() || !until.After(since) || until.Sub(since) > 45*time.Second {
22+
port == 0 || node == "" || since.IsZero() || !until.After(since) || until.Sub(since) > 45*time.Second {
2323
return refuse()
2424
}
2525
fields := map[string][]string{
@@ -97,7 +97,7 @@ func decodeIngressFlows(data []byte, destination netip.Addr, node string, since,
9797
if flow.Verdict != "FORWARDED" || (flow.Direction != "INGRESS" && flow.Direction != "EGRESS") || flow.Point != "TO_ENDPOINT" ||
9898
flow.Node != node || (flow.Reply != nil && *flow.Reply) || flow.Source.Identity != 8 || !slices.Contains(flow.Source.Labels, "reserved:ingress") ||
9999
flow.Destination.Namespace != "private-mail" || flow.Destination.Pod != "private-mail-0" || flow.IP.Destination != destination.String() ||
100-
flow.L4.TCP.Port != 8080 || !flow.L4.TCP.Flags.SYN || flow.L4.TCP.Flags.ACK {
100+
flow.L4.TCP.Port != port || !flow.L4.TCP.Flags.SYN || flow.L4.TCP.Flags.ACK {
101101
return refuse()
102102
}
103103
original, err := netip.ParseAddr(flow.IP.Source)

‎scripts/diagnose-email-startup/ingress_flow_test.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func TestIngressFlowRequiresBoundWireEvidence(t *testing.T) {
4747
{"oversized", strings.Repeat(" ", maxAPIOutput+1) + ingressFlowSample, false},
4848
} {
4949
t.Run(test.name, func(t *testing.T) {
50-
addresses, err := decodeIngressFlows([]byte(test.raw), destination, "prod/worker-1", start, start.Add(10*time.Second))
50+
addresses, err := decodeIngressFlows([]byte(test.raw), destination, ingressManagementPort, "prod/worker-1", start, start.Add(10*time.Second))
5151
if (err == nil) != test.valid {
5252
t.Fatal("unbound or incomplete flow evidence was accepted, or real wire evidence refused")
5353
}
@@ -66,12 +66,12 @@ func TestIngressFlowRequiresBoundWireEvidence(t *testing.T) {
6666
func TestIngressFlowUsesPostTranslationSource(t *testing.T) {
6767
start := time.Date(2026, 10, 28, 16, 0, 0, 0, time.UTC)
6868
raw := strings.Replace(ingressFlowSample, `"source":"10.244.1.1"`, `"source":"10.244.1.1","source_xlated":"10.0.1.1"`, 1)
69-
addresses, err := decodeIngressFlows([]byte(raw), netip.MustParseAddr("10.244.2.2"), "prod/worker-1", start, start.Add(10*time.Second))
69+
addresses, err := decodeIngressFlows([]byte(raw), netip.MustParseAddr("10.244.2.2"), ingressManagementPort, "prod/worker-1", start, start.Add(10*time.Second))
7070
if err != nil || len(addresses) != 1 || addresses[0] != netip.MustParseAddr("10.0.1.1") {
7171
t.Fatal("post-translation wire address was replaced by the original source")
7272
}
7373
invalid := strings.Replace(raw, `"source_xlated":"10.0.1.1"`, `"source_xlated":"not-an-address"`, 1)
74-
if addresses, err := decodeIngressFlows([]byte(invalid), netip.MustParseAddr("10.244.2.2"), "prod/worker-1", start, start.Add(10*time.Second)); err == nil || len(addresses) != 0 {
74+
if addresses, err := decodeIngressFlows([]byte(invalid), netip.MustParseAddr("10.244.2.2"), ingressManagementPort, "prod/worker-1", start, start.Add(10*time.Second)); err == nil || len(addresses) != 0 {
7575
t.Fatal("an invalid translated address fell back to a different peer")
7676
}
7777
}

‎scripts/diagnose-email-startup/ingress_native_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestIngressNativeFilter(t *testing.T) {
1616
if binary == "" {
1717
t.Skip("native Hubble parser runs in the hosted transport gate")
1818
}
19-
filter, err := ingressFlowFilter(netip.MustParseAddr("192.0.2.20"))
19+
filter, err := ingressFlowFilter(netip.MustParseAddr("192.0.2.20"), ingressProxyPort)
2020
if err != nil {
2121
t.Fatal(err)
2222
}

‎scripts/diagnose-email-startup/ingress_observer.go‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"encoding/json"
77
"net/netip"
8+
"strconv"
89
"time"
910

1011
"github.com/devantler-tech/platform/internal/strictjson"
@@ -17,6 +18,11 @@ type ingressObservation struct {
1718

1819
type ingressCluster struct{ UID, Revision, Name string }
1920

21+
const (
22+
ingressManagementPort uint16 = 8080
23+
ingressProxyPort uint16 = 8081
24+
)
25+
2026
func bindIngressCluster(raw []byte) (ingressCluster, error) {
2127
fields := map[string][]string{"": {"apiVersion", "kind", "metadata", "data"}, "metadata": {"name", "namespace", "uid", "resourceVersion", "deletionTimestamp"}, "data": {"cluster-name", "enable-hubble"}}
2228
var object struct {
@@ -33,14 +39,14 @@ func bindIngressCluster(raw []byte) (ingressCluster, error) {
3339
return ingressCluster{UID: object.Metadata.UID, Revision: object.Metadata.ResourceVersion, Name: object.Data["cluster-name"]}, nil
3440
}
3541

36-
func ingressFlowFilter(destination netip.Addr) ([]byte, error) {
42+
func ingressFlowFilter(destination netip.Addr, port uint16) ([]byte, error) {
3743
// Cilium v1.20.3 monitor API: trace type 4, to-endpoint subtype 0.
3844
// Native TCP flag filters require the listed bits; a false ACK bit would
3945
// not exclude ACK packets. SYN, reply direction and the receiver port are
4046
// constrained here; the strict wire parser independently refuses SYN+ACK.
4147
return json.Marshal(map[string]any{
4248
"destination_ip": []string{destination.String()}, "destination_pod": []string{"private-mail/private-mail-0"},
43-
"destination_port": []string{"8080"}, "protocol": []string{"tcp"}, "source_identity": []uint32{8},
49+
"destination_port": []string{strconv.FormatUint(uint64(port), 10)}, "protocol": []string{"tcp"}, "source_identity": []uint32{8},
4450
"tcp_flags": []map[string]bool{{"SYN": true}}, "reply": []bool{false},
4551
"verdict": []string{"FORWARDED"}, "traffic_direction": []string{"INGRESS", "EGRESS"},
4652
"event_type": []map[string]any{{"type": 4, "match_sub_type": true, "sub_type": 0}},
@@ -135,7 +141,12 @@ func observeIngressSources(ctx context.Context, run executor, app binding, addre
135141
return refuse()
136142
}
137143
stage = "flow_filter"
138-
filter, err := ingressFlowFilter(addresses.Pod)
144+
port := ingressManagementPort
145+
// Hubble observes the receiving pod port after Service translation.
146+
if app.ProxyConfig != "" {
147+
port = ingressProxyPort
148+
}
149+
filter, err := ingressFlowFilter(addresses.Pod, port)
139150
if err != nil {
140151
return refuse()
141152
}
@@ -154,7 +165,7 @@ func observeIngressSources(ctx context.Context, run executor, app binding, addre
154165
flowNode = cluster.Name + "/" + app.Node
155166
}
156167
stage = "flow_decode"
157-
sources, err := decodeIngressFlows(raw, addresses.Pod, flowNode, since, until)
168+
sources, err := decodeIngressFlows(raw, addresses.Pod, port, flowNode, since, until)
158169
if err != nil {
159170
return refuse()
160171
}

‎scripts/diagnose-email-startup/ingress_observer_test.go‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ const ingressDaemonSample = `{"apiVersion":"apps/v1","kind":"DaemonSet","metadat
1616
// Unexpected containers, observer failures and identity changes must prevent a
1717
// usable result, even after the observer has returned plausible flow bytes.
1818
func TestIngressObserverBindsExecutionAndRefusesPartialEvidence(t *testing.T) {
19-
for _, mode := range []string{"matching", "named-cluster", "cluster-churn", "unready-agent", "agent-churn", "daemon-churn", "probe-failure", "observer-failure", "empty-flow", "truncated-flow"} {
19+
for _, mode := range []string{"matching", "named-cluster", "active-proxy", "native-port-while-proxied", "cluster-churn", "unready-agent", "agent-churn", "daemon-churn", "probe-failure", "observer-failure", "empty-flow", "truncated-flow"} {
2020
t.Run(mode, func(t *testing.T) {
2121
podReads, daemonReads, executions, probes, clockReads := 0, 0, 0, 0, 0
2222
clusterReads := 0
2323
start := time.Date(2026, 10, 28, 16, 0, 0, 0, time.UTC)
24+
app := binding{UID: "44444444-4444-4444-4444-444444444444", Node: "worker-1"}
25+
if mode == "active-proxy" || mode == "native-port-while-proxied" {
26+
app.ProxyConfig = approvedProxyMap
27+
}
2428
run := func(_ context.Context, args ...string) ([]byte, error) {
2529
switch strings.Join(args, " ") {
2630
case "--context admin@prod --request-timeout=10s get configmap cilium-config -n kube-system -o json":
@@ -56,8 +60,12 @@ func TestIngressObserverBindsExecutionAndRefusesPartialEvidence(t *testing.T) {
5660
if len(args) != 24 || !reflect.DeepEqual(args[:16], prefix) || args[16] != "--since" || args[17] != "2026-10-28T16:00:00Z" || args[18] != "--until" || args[19] != "2026-10-28T16:00:10Z" || args[20] != "--allowlist" || args[22] != "--output" || args[23] != "jsonpb" {
5761
return nil, errors.New("unexpected native operation")
5862
}
63+
expectedFilter := `{"destination_ip":["10.244.2.2"],"destination_pod":["private-mail/private-mail-0"],"destination_port":["8080"],"protocol":["tcp"],"source_identity":[8],"tcp_flags":[{"SYN":true}],"reply":[false],"verdict":["FORWARDED"],"traffic_direction":["INGRESS","EGRESS"],"event_type":[{"type":4,"match_sub_type":true,"sub_type":0}]}`
64+
if app.ProxyConfig != "" {
65+
expectedFilter = strings.Replace(expectedFilter, `["8080"]`, `["8081"]`, 1)
66+
}
5967
var actual, expected any
60-
if json.Unmarshal([]byte(args[21]), &actual) != nil || json.Unmarshal([]byte(`{"destination_ip":["10.244.2.2"],"destination_pod":["private-mail/private-mail-0"],"destination_port":["8080"],"protocol":["tcp"],"source_identity":[8],"tcp_flags":[{"SYN":true}],"reply":[false],"verdict":["FORWARDED"],"traffic_direction":["INGRESS","EGRESS"],"event_type":[{"type":4,"match_sub_type":true,"sub_type":0}]}`), &expected) != nil || !reflect.DeepEqual(actual, expected) {
68+
if json.Unmarshal([]byte(args[21]), &actual) != nil || json.Unmarshal([]byte(expectedFilter), &expected) != nil || !reflect.DeepEqual(actual, expected) {
6169
return nil, errors.New("unexpected filter")
6270
}
6371
executions++
@@ -68,6 +76,9 @@ func TestIngressObserverBindsExecutionAndRefusesPartialEvidence(t *testing.T) {
6876
return []byte(" \n\t"), nil
6977
}
7078
raw := ingressFlowSample
79+
if mode == "active-proxy" {
80+
raw = strings.Replace(raw, `"destination_port":8080`, `"destination_port":8081`, 1)
81+
}
7182
if mode != "named-cluster" {
7283
raw = strings.Replace(raw, `"prod/worker-1"`, `"worker-1"`, 1)
7384
}
@@ -90,11 +101,12 @@ func TestIngressObserverBindsExecutionAndRefusesPartialEvidence(t *testing.T) {
90101
}
91102
return start.Add(10 * time.Second)
92103
}
93-
result, err := observeIngressSources(context.Background(), run, binding{UID: "44444444-4444-4444-4444-444444444444", Node: "worker-1"}, accessAddresses{Pod: netip.MustParseAddr("10.244.2.2")}, probe, func(context.Context) error { return nil }, now)
94-
if (err == nil) != (mode == "matching" || mode == "named-cluster") {
95-
t.Fatal("partial observation accepted or bound observation refused")
104+
result, err := observeIngressSources(context.Background(), run, app, accessAddresses{Pod: netip.MustParseAddr("10.244.2.2")}, probe, func(context.Context) error { return nil }, now)
105+
wantSuccess := mode == "matching" || mode == "named-cluster" || mode == "active-proxy"
106+
if (err == nil) != wantSuccess {
107+
t.Fatalf("partial observation accepted or bound observation refused at %s", ingressFailureStage(err))
96108
}
97-
if (mode == "matching" || mode == "named-cluster") && (len(result.Sources) != 1 || result.Sources[0] != netip.MustParseAddr("10.244.1.1") || result.Public503 != 2 || result.Public200 != 0 || clusterReads != 2) {
109+
if wantSuccess && (len(result.Sources) != 1 || result.Sources[0] != netip.MustParseAddr("10.244.1.1") || result.Public503 != 2 || result.Public200 != 0 || clusterReads != 2) {
98110
t.Fatal("observed peer or public response was lost")
99111
}
100112
if err != nil && (len(result.Sources) != 0 || strings.Contains(err.Error(), "private-")) {
@@ -104,7 +116,7 @@ func TestIngressObserverBindsExecutionAndRefusesPartialEvidence(t *testing.T) {
104116
want := map[string]string{
105117
"cluster-churn": "cluster_after", "unready-agent": "agent_before", "agent-churn": "agent_after",
106118
"daemon-churn": "daemon_after", "probe-failure": "public_probes", "observer-failure": "flow_read",
107-
"empty-flow": "flow_empty", "truncated-flow": "flow_decode",
119+
"empty-flow": "flow_empty", "truncated-flow": "flow_decode", "native-port-while-proxied": "flow_decode",
108120
}[mode]
109121
if ingressFailureStage(err) != want {
110122
t.Fatal("failed observation lost its closed failure stage")

0 commit comments

Comments
 (0)