Repository navigation
✨ lb: limit a Service's NodePort to its Load Balancer - #90
Merged
Merged
Conversation
A LoadBalancer Service's NodePort is open on every node address. Without a private network or firewall in front of the nodes, it is reachable directly on each node's public IP, bypassing the Load Balancer. With HCLOUD_LOAD_BALANCERS_RESTRICT_NODE_PORT_SOURCE=true, or the load-balancer.hetzner.cloud/restrict-node-port-source annotation, the CCM adds the Load Balancer's IPv4 (public, and private if attached to a network) as /32 entries to spec.loadBalancerSourceRanges. A Service proxy that applies the field to the NodePort (Cilium with bpf.lbSourceRangeAllTypes=true) then drops everything else. - Runs at the end of EnsureLoadBalancer. The cloud-provider service controller resets the spec before patching, so the update goes through a client created in cloud.Initialize. - Keeps entries set by others. The entries the CCM owns are recorded in the read-only load-balancer.hetzner.cloud/node-port-source-ranges annotation, so it can replace them when the LB IP changes and remove them on opt-out or in EnsureLoadBalancerDeleted (e.g. after a change to type NodePort). - Off by default: kube-proxy applies the field to in-cluster traffic to the LB IP, so enabling it there drops pods calling the Service via that IP.
- Skip Services whose ranges Cilium treats as a deny-list (service.cilium.io/src-ranges-policy: deny); adding the LB there would block the LB itself. Entries added before are removed. - Write with a JSON merge patch of only the two fields, guarded by the resourceVersion read, under field manager hcloud-ccm-node-port-source. A full-object update could drop Service fields this client does not know. - Compare CIDRs by network, so another spelling of the LB /32 counts as the customer's entry instead of getting a duplicate. - An empty restrict-node-port-source value falls back to the default. - EnsureLoadBalancerDeleted removes the entries after deleting the LB and only logs a failure, so a rejected Service update cannot keep the LB or the finalizer around. - Keep existing entries when the LB has no usable IPv4 instead of opening the NodePort. - Docs: type changes need the field removed in the same request, deny-list skip, effect on client-CIDR-only lists, externalIPs and UDP, fail-open on an earlier sync error.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A
type=LoadBalancerService gets a NodePort, and the Hetzner LB targets that NodePort on every node. The NodePort is open on every node address, not only to the LB. On clusters with public node IPs and no private network or firewall in front of the nodes, anyone can reach the Service directly on<node-ip>:<nodePort>, bypassing the LB.Hetzner LBs have no source filter (upstream hetznercloud#218, hetznercloud#286, hetznercloud#291), so the CCM can't fix this on the LB. The Service proxy can, if
spec.loadBalancerSourceRangesnames the LB.Change
New opt-in:
HCLOUD_LOAD_BALANCERS_RESTRICT_NODE_PORT_SOURCE=true(cluster default) orload-balancer.hetzner.cloud/restrict-node-port-source: "true|false"(per Service, overrides the default both ways).When enabled,
EnsureLoadBalanceradds the LB's own IPv4s as/32entries tospec.loadBalancerSourceRanges:IPv6 is not added, because the LB reaches nodes over IPv4 even for IPv6 clients.
How it behaves:
servicehelper.PatchService), so a client is created incloud.Initialize(which was empty).load-balancer.hetzner.cloud/node-port-source-rangesannotation. An entry someone else already listed stays theirs.EnsureLoadBalancerDeleted, e.g. after a change totype=NodePort, which would otherwise stay locked to a gone LB.EnsureLoadBalancer, which finds nothing to change.servicesupdate, which the service controller already uses.Why off by default
bpf.lbSourceRangeAllTypes=true) applies the field to the NodePort, and only to traffic arriving on the node's network interface. In-cluster clients are unaffected.Tests
go test ./...passes../hack/golangci-lint.shreports 0 issues.EnsureLoadBalancerend-to-end case with a fake clientset.Not yet validated on a live cluster.
Known limit
The NodePort is open from Service creation until the LB exists and its IP has been written, usually seconds.