feat(egress): network-policy@1 matcher

This commit is contained in:
sam committed 2026-09-26 02:27:25 +10:00
1 parent a7edc0a5cd
commit 6bc7a85404
2 files changed
+150

No files matched your search

+93
View File
@@ -0,0 +1,93 @@
package egress
import (
"net"
"strconv"
"strings"
"github.com/docker/sandbox-kit-spec/v3/spec"
)
// pattern is one network-policy@1 entry: an exact host, a host on one port,
// a single-label wildcard, or everything.
type pattern struct {
everything bool
suffix string // ".example.com" for "*.example.com"
host string
port int // 0 means any port
}
// rules is a compiled allow/deny pair for one phase. Deny wins; an empty or
// absent phase allows nothing, so egress is closed unless a Kit asks for it.
type rules struct {
allow []pattern
deny []pattern
}
func compileRules(r *spec.NetworkRules) rules {
if r == nil {
return rules{}
}
return rules{allow: compilePatterns(r.Allow), deny: compilePatterns(r.Deny)}
}
func compilePatterns(entries []string) []pattern {
out := make([]pattern, 0, len(entries))
for _, e := range entries {
out = append(out, compilePattern(e))
}
return out
}
func compilePattern(entry string) pattern {
entry = strings.TrimSpace(entry)
if entry == "*" || entry == "**" {
return pattern{everything: true}
}
var p pattern
if h, portText, err := net.SplitHostPort(entry); err == nil {
if port, err := strconv.Atoi(portText); err == nil {
entry, p.port = h, port
}
}
entry = normalizeHost(entry)
if rest, ok := strings.CutPrefix(entry, "*."); ok {
p.suffix = "." + rest
return p
}
p.host = entry
return p
}
func normalizeHost(host string) string {
return strings.TrimSuffix(strings.ToLower(host), ".")
}
func (p pattern) matches(host string, port int) bool {
if p.everything {
return true
}
if p.port != 0 && p.port != port {
return false
}
if p.suffix != "" {
label, ok := strings.CutSuffix(host, p.suffix)
return ok && label != "" && !strings.Contains(label, ".") && net.ParseIP(host) == nil
}
return host == p.host
}
func (r rules) allows(host string, port int) bool {
host = normalizeHost(host)
for _, p := range r.deny {
if p.matches(host, port) {
return false
}
}
for _, p := range r.allow {
if p.matches(host, port) {
return true
}
}
return false
}
@@ -0,0 +1,57 @@
package egress
import (
"testing"
"github.com/docker/sandbox-kit-spec/v3/spec"
)
func TestRulesAllows(t *testing.T) {
rules := compileRules(&spec.NetworkRules{
Allow: []string{"api.example.com:443", "downloads.example.com", "*.cdn.example.net", "10.0.0.5:8080"},
Deny: []string{"blocked.cdn.example.net"},
})
tests := []struct {
host string
port int
want bool
}{
{"api.example.com", 443, true},
{"api.example.com", 80, false},
{"API.Example.COM.", 443, true},
{"downloads.example.com", 443, true},
{"downloads.example.com", 8443, true},
{"a.cdn.example.net", 443, true},
{"cdn.example.net", 443, false},
{"a.b.cdn.example.net", 443, false},
{"blocked.cdn.example.net", 443, false},
{"10.0.0.5", 8080, true},
{"10.0.0.5", 80, false},
{"example.com", 443, false},
{"evil-api.example.com", 443, false},
{"api.example.com.evil.test", 443, false},
}
for _, tt := range tests {
if got := rules.allows(tt.host, tt.port); got != tt.want {
t.Errorf("allows(%q, %d) = %v, want %v", tt.host, tt.port, got, tt.want)
}
}
}
func TestRulesEverything(t *testing.T) {
for _, all := range []string{"*", "**"} {
rules := compileRules(&spec.NetworkRules{Allow: []string{all}, Deny: []string{"telemetry.example.com"}})
if !rules.allows("anything.example.org", 443) {
t.Errorf("%q does not allow an arbitrary host", all)
}
if rules.allows("telemetry.example.com", 443) {
t.Errorf("deny does not win over %q", all)
}
}
}
func TestNilRulesAllowNothing(t *testing.T) {
if compileRules(nil).allows("api.example.com", 443) {
t.Fatal("a phase with no rules allowed egress")
}
}