You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
582 lines
22 KiB
Go
582 lines
22 KiB
Go
//go:build linux
|
|
|
|
package vpcrouting
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
type fakeFirewalldZone struct {
|
|
target string
|
|
forward bool
|
|
interfaces map[string]struct{}
|
|
}
|
|
|
|
type fakeFirewalldPolicy struct {
|
|
target string
|
|
ingress map[string]struct{}
|
|
egress map[string]struct{}
|
|
richRules map[string]struct{}
|
|
}
|
|
|
|
type fakeFirewalldScope struct {
|
|
zones map[string]*fakeFirewalldZone
|
|
policies map[string]*fakeFirewalldPolicy
|
|
}
|
|
|
|
type fakeFirewalldRunner struct {
|
|
active bool
|
|
nftOwned bool
|
|
runtime fakeFirewalldScope
|
|
permanent fakeFirewalldScope
|
|
calls []string
|
|
}
|
|
|
|
func newFakeFirewalldRunner(active bool) *fakeFirewalldRunner {
|
|
newScope := func() fakeFirewalldScope {
|
|
return fakeFirewalldScope{
|
|
zones: make(map[string]*fakeFirewalldZone),
|
|
policies: make(map[string]*fakeFirewalldPolicy),
|
|
}
|
|
}
|
|
return &fakeFirewalldRunner{active: active, runtime: newScope(), permanent: newScope()}
|
|
}
|
|
|
|
func (r *fakeFirewalldRunner) Run(_ context.Context, path string, arguments []string, _ []byte) ([]byte, error) {
|
|
command := filepath.Base(path) + " " + strings.Join(arguments, " ")
|
|
r.calls = append(r.calls, command)
|
|
if filepath.Base(path) == "nft" {
|
|
if strings.Join(arguments, " ") == "-j list table inet virt_cloud_vpc" {
|
|
if !r.nftOwned {
|
|
return nil, fakeCommandFailure(path, 1, "No such file or directory")
|
|
}
|
|
return []byte(`{"nftables":[{"table":{"family":"inet","name":"virt_cloud_vpc"}},{"chain":{"family":"inet","table":"virt_cloud_vpc","name":"owner_v1"}}]}`), nil
|
|
}
|
|
if strings.Join(arguments, " ") == "-f -" {
|
|
r.nftOwned = true
|
|
return nil, nil
|
|
}
|
|
return []byte(`{"nftables":[]}`), nil
|
|
}
|
|
if len(arguments) == 1 && arguments[0] == "--state" {
|
|
if r.active {
|
|
return []byte("running\n"), nil
|
|
}
|
|
return nil, fakeCommandFailure(path, 252, "not running")
|
|
}
|
|
scope, arguments := r.scope(arguments)
|
|
if len(arguments) == 0 {
|
|
return nil, errors.New("empty firewalld command")
|
|
}
|
|
if len(arguments) == 1 && arguments[0] == "--get-zones" {
|
|
return []byte(strings.Join(sortedKeys(scope.zones), " ") + "\n"), nil
|
|
}
|
|
if len(arguments) == 1 && strings.HasPrefix(arguments[0], "--new-zone=") {
|
|
name := strings.TrimPrefix(arguments[0], "--new-zone=")
|
|
scope.zones[name] = &fakeFirewalldZone{target: "default", interfaces: make(map[string]struct{})}
|
|
return []byte("success\n"), nil
|
|
}
|
|
if len(arguments) == 1 && arguments[0] == "--get-policies" {
|
|
return []byte(strings.Join(sortedKeys(scope.policies), " ") + "\n"), nil
|
|
}
|
|
if len(arguments) == 1 && strings.HasPrefix(arguments[0], "--new-policy=") {
|
|
name := strings.TrimPrefix(arguments[0], "--new-policy=")
|
|
scope.policies[name] = &fakeFirewalldPolicy{
|
|
target: "CONTINUE", ingress: make(map[string]struct{}), egress: make(map[string]struct{}), richRules: make(map[string]struct{}),
|
|
}
|
|
return []byte("success\n"), nil
|
|
}
|
|
if len(arguments) == 1 && arguments[0] == "--reload" {
|
|
r.runtime = cloneFakeFirewalldScope(r.permanent)
|
|
return []byte("success\n"), nil
|
|
}
|
|
if len(arguments) == 1 && strings.HasPrefix(arguments[0], "--get-zone-of-interface=") {
|
|
name := strings.TrimPrefix(arguments[0], "--get-zone-of-interface=")
|
|
found := ""
|
|
for zoneName, zone := range scope.zones {
|
|
if _, exists := zone.interfaces[name]; !exists {
|
|
continue
|
|
}
|
|
if found != "" {
|
|
return nil, errors.New("interface appears in multiple zones")
|
|
}
|
|
found = zoneName
|
|
}
|
|
if found == "" {
|
|
// Real firewall-cmd releases print this ordinary empty state while
|
|
// returning a non-zero status.
|
|
return nil, fakeCommandFailure(path, 2, "no zone")
|
|
}
|
|
return []byte(found + "\n"), nil
|
|
}
|
|
if len(arguments) >= 2 && strings.HasPrefix(arguments[0], "--zone=") {
|
|
return r.zoneCommand(path, scope, strings.TrimPrefix(arguments[0], "--zone="), arguments[1:])
|
|
}
|
|
if len(arguments) >= 2 && strings.HasPrefix(arguments[0], "--policy=") {
|
|
return r.policyCommand(path, scope, strings.TrimPrefix(arguments[0], "--policy="), arguments[1:])
|
|
}
|
|
return nil, errors.New("unexpected firewalld command: " + command)
|
|
}
|
|
|
|
func (r *fakeFirewalldRunner) scope(arguments []string) (*fakeFirewalldScope, []string) {
|
|
if len(arguments) != 0 && arguments[0] == "--permanent" {
|
|
return &r.permanent, arguments[1:]
|
|
}
|
|
return &r.runtime, arguments
|
|
}
|
|
|
|
func cloneFakeFirewalldScope(source fakeFirewalldScope) fakeFirewalldScope {
|
|
result := fakeFirewalldScope{
|
|
zones: make(map[string]*fakeFirewalldZone, len(source.zones)),
|
|
policies: make(map[string]*fakeFirewalldPolicy, len(source.policies)),
|
|
}
|
|
for name, zone := range source.zones {
|
|
result.zones[name] = &fakeFirewalldZone{
|
|
target: zone.target, forward: zone.forward, interfaces: cloneStringSet(zone.interfaces),
|
|
}
|
|
}
|
|
for name, policy := range source.policies {
|
|
result.policies[name] = &fakeFirewalldPolicy{
|
|
target: policy.target, ingress: cloneStringSet(policy.ingress), egress: cloneStringSet(policy.egress), richRules: cloneStringSet(policy.richRules),
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
func cloneStringSet(source map[string]struct{}) map[string]struct{} {
|
|
result := make(map[string]struct{}, len(source))
|
|
for value := range source {
|
|
result[value] = struct{}{}
|
|
}
|
|
return result
|
|
}
|
|
|
|
func (r *fakeFirewalldRunner) zoneCommand(path string, scope *fakeFirewalldScope, name string, arguments []string) ([]byte, error) {
|
|
zone := scope.zones[name]
|
|
if zone == nil {
|
|
return nil, fakeCommandFailure(path, 2, "INVALID_ZONE")
|
|
}
|
|
argument := arguments[0]
|
|
switch {
|
|
case argument == "--list-all":
|
|
forward := "no"
|
|
if zone.forward {
|
|
forward = "yes"
|
|
}
|
|
return []byte(strings.Join([]string{
|
|
name,
|
|
" target: " + zone.target,
|
|
" icmp-block-inversion: no",
|
|
" interfaces: " + strings.Join(sortedKeys(zone.interfaces), " "),
|
|
" sources:",
|
|
" services:",
|
|
" ports:",
|
|
" protocols:",
|
|
" forward: " + forward,
|
|
" masquerade: no",
|
|
" forward-ports:",
|
|
" source-ports:",
|
|
" icmp-blocks:",
|
|
" rich rules:",
|
|
}, "\n") + "\n"), nil
|
|
case argument == "--get-target":
|
|
return []byte(zone.target + "\n"), nil
|
|
case strings.HasPrefix(argument, "--set-target="):
|
|
zone.target = strings.TrimPrefix(argument, "--set-target=")
|
|
return []byte("success\n"), nil
|
|
case argument == "--query-forward":
|
|
if zone.forward {
|
|
return []byte("yes\n"), nil
|
|
}
|
|
return nil, fakeCommandFailure(path, 1, "no")
|
|
case argument == "--add-forward":
|
|
zone.forward = true
|
|
return []byte("success\n"), nil
|
|
case argument == "--list-interfaces":
|
|
return []byte(strings.Join(sortedKeys(zone.interfaces), " ") + "\n"), nil
|
|
case strings.HasPrefix(argument, "--add-interface="):
|
|
zone.interfaces[strings.TrimPrefix(argument, "--add-interface=")] = struct{}{}
|
|
return []byte("success\n"), nil
|
|
case strings.HasPrefix(argument, "--remove-interface="):
|
|
delete(zone.interfaces, strings.TrimPrefix(argument, "--remove-interface="))
|
|
return []byte("success\n"), nil
|
|
case strings.HasPrefix(argument, "--query-interface="):
|
|
if _, exists := zone.interfaces[strings.TrimPrefix(argument, "--query-interface=")]; exists {
|
|
return []byte("yes\n"), nil
|
|
}
|
|
return nil, fakeCommandFailure(path, 1, "no")
|
|
case isEmptyZoneQuery(argument):
|
|
return []byte("\n"), nil
|
|
case argument == "--query-masquerade" || argument == "--query-icmp-block-inversion":
|
|
return nil, fakeCommandFailure(path, 1, "no")
|
|
default:
|
|
return nil, errors.New("unexpected zone command: " + strings.Join(arguments, " "))
|
|
}
|
|
}
|
|
|
|
func (r *fakeFirewalldRunner) policyCommand(path string, scope *fakeFirewalldScope, name string, arguments []string) ([]byte, error) {
|
|
policy := scope.policies[name]
|
|
if policy == nil {
|
|
return nil, fakeCommandFailure(path, 2, "INVALID_POLICY")
|
|
}
|
|
argument := arguments[0]
|
|
switch {
|
|
case argument == "--list-all":
|
|
return []byte(strings.Join([]string{
|
|
name,
|
|
" priority: -1",
|
|
" target: " + policy.target,
|
|
" ingress-zones: " + strings.Join(sortedKeys(policy.ingress), " "),
|
|
" egress-zones: " + strings.Join(sortedKeys(policy.egress), " "),
|
|
" services:",
|
|
" ports:",
|
|
" protocols:",
|
|
" masquerade: no",
|
|
" forward-ports:",
|
|
" source-ports:",
|
|
" icmp-blocks:",
|
|
" rich rules:",
|
|
}, "\n") + "\n"), nil
|
|
case argument == "--get-target":
|
|
return []byte(policy.target + "\n"), nil
|
|
case strings.HasPrefix(argument, "--set-target="):
|
|
policy.target = strings.TrimPrefix(argument, "--set-target=")
|
|
return []byte("success\n"), nil
|
|
case argument == "--list-ingress-zones":
|
|
return []byte(strings.Join(sortedKeys(policy.ingress), " ") + "\n"), nil
|
|
case strings.HasPrefix(argument, "--add-ingress-zone="):
|
|
policy.ingress[strings.TrimPrefix(argument, "--add-ingress-zone=")] = struct{}{}
|
|
return []byte("success\n"), nil
|
|
case argument == "--list-egress-zones":
|
|
return []byte(strings.Join(sortedKeys(policy.egress), " ") + "\n"), nil
|
|
case strings.HasPrefix(argument, "--add-egress-zone="):
|
|
policy.egress[strings.TrimPrefix(argument, "--add-egress-zone=")] = struct{}{}
|
|
return []byte("success\n"), nil
|
|
case argument == "--list-rich-rules":
|
|
return []byte(strings.Join(sortedKeys(policy.richRules), "\n") + "\n"), nil
|
|
case strings.HasPrefix(argument, "--add-rich-rule="):
|
|
policy.richRules[strings.TrimPrefix(argument, "--add-rich-rule=")] = struct{}{}
|
|
return []byte("success\n"), nil
|
|
case isEmptyPolicyQuery(argument):
|
|
return []byte("\n"), nil
|
|
case argument == "--query-masquerade":
|
|
return nil, fakeCommandFailure(path, 1, "no")
|
|
default:
|
|
return nil, errors.New("unexpected policy command: " + strings.Join(arguments, " "))
|
|
}
|
|
}
|
|
|
|
func fakeCommandFailure(path string, code int, output string) error {
|
|
return &commandError{path: path, exitCode: code, cause: errors.New(output), output: output}
|
|
}
|
|
|
|
func sortedKeys[V any](values map[string]V) []string {
|
|
result := make([]string, 0, len(values))
|
|
for value := range values {
|
|
result = append(result, value)
|
|
}
|
|
sort.Strings(result)
|
|
return result
|
|
}
|
|
|
|
func isEmptyZoneQuery(argument string) bool {
|
|
switch argument {
|
|
case "--list-sources", "--list-services", "--list-ports", "--list-protocols",
|
|
"--list-source-ports", "--list-forward-ports", "--list-icmp-blocks", "--list-rich-rules":
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
func isEmptyPolicyQuery(argument string) bool {
|
|
switch argument {
|
|
case "--list-services", "--list-ports", "--list-protocols", "--list-source-ports",
|
|
"--list-forward-ports", "--list-icmp-blocks":
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
func TestReconcileFirewalldCreatesDedicatedForwardingBoundary(t *testing.T) {
|
|
runner := newFakeFirewalldRunner(true)
|
|
driver := &linuxDriver{
|
|
ipPath: "/usr/sbin/ip", nftPath: "/usr/sbin/nft",
|
|
firewallCmdPath: "/usr/bin/firewall-cmd", run: runner,
|
|
}
|
|
network := testNetwork(1)
|
|
if err := driver.reconcileFirewalld(t.Context(), nil, map[string]Network{network.VPCKey: network}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if countCallsContaining(runner.calls, "firewall-cmd --reload") != 1 {
|
|
t.Fatalf("first reconcile reload calls=%v, want exactly one", runner.calls)
|
|
}
|
|
if err := driver.reconcileFirewalld(t.Context(), map[string]Network{network.VPCKey: network}, map[string]Network{network.VPCKey: network}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if countCallsContaining(runner.calls, "firewall-cmd --reload") != 1 {
|
|
t.Fatalf("steady-state reconcile unexpectedly reloaded firewalld: %v", runner.calls)
|
|
}
|
|
for _, redundant := range []string{
|
|
"--list-services", "--list-ports", "--list-protocols", "--list-source-ports",
|
|
"--list-forward-ports", "--list-icmp-blocks", "--query-masquerade", "--query-forward",
|
|
} {
|
|
if countCallsContaining(runner.calls, redundant) != 0 {
|
|
t.Fatalf("firewalld inspection regressed to per-field subprocesses (%s): %v", redundant, runner.calls)
|
|
}
|
|
}
|
|
bridge, _ := BridgeName(network.VPCKey)
|
|
for scopeName, scope := range map[string]fakeFirewalldScope{"runtime": runner.runtime, "permanent": runner.permanent} {
|
|
guest := scope.zones[firewalldGuestZone]
|
|
transport := scope.zones[firewalldTransportZone]
|
|
if guest == nil || transport == nil || guest.target != "default" || transport.target != "default" || !guest.forward || transport.forward {
|
|
t.Fatalf("%s zones do not preserve the host boundary: guest=%#v transport=%#v", scopeName, guest, transport)
|
|
}
|
|
if _, exists := guest.interfaces[bridge]; !exists || len(guest.interfaces) != 1 {
|
|
t.Fatalf("%s guest interfaces=%v, want only %s", scopeName, sortedKeys(guest.interfaces), bridge)
|
|
}
|
|
if _, exists := transport.interfaces[network.Transport.Interface]; !exists || len(transport.interfaces) != 1 {
|
|
t.Fatalf("%s transport interfaces=%v, want only %s", scopeName, sortedKeys(transport.interfaces), network.Transport.Interface)
|
|
}
|
|
outbound := scope.policies[firewalldGuestToTransportPolicy]
|
|
inbound := scope.policies[firewalldTransportToGuestPolicy]
|
|
if outbound == nil || inbound == nil || outbound.target != "ACCEPT" || inbound.target != "ACCEPT" ||
|
|
!sameStringSet(outbound.ingress, firewalldGuestZone) || !sameStringSet(outbound.egress, firewalldTransportZone) ||
|
|
!sameStringSet(inbound.ingress, firewalldTransportZone) || !sameStringSet(inbound.egress, firewalldGuestZone) ||
|
|
len(outbound.richRules) != 0 || len(inbound.richRules) != 0 {
|
|
t.Fatalf("%s policies do not allow only VPC guest/transport forwarding: outbound=%#v inbound=%#v", scopeName, outbound, inbound)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestReconcileFirewalldDoesNothingWhenServiceIsInactive(t *testing.T) {
|
|
runner := newFakeFirewalldRunner(false)
|
|
driver := &linuxDriver{
|
|
ipPath: "/usr/sbin/ip", nftPath: "/usr/sbin/nft",
|
|
firewallCmdPath: "/usr/bin/firewall-cmd", run: runner,
|
|
}
|
|
network := testNetwork(1)
|
|
if err := driver.reconcileFirewalld(t.Context(), nil, map[string]Network{network.VPCKey: network}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(runner.runtime.zones) != 0 || len(runner.permanent.zones) != 0 || len(runner.runtime.policies) != 0 || len(runner.permanent.policies) != 0 {
|
|
t.Fatal("inactive firewalld was mutated")
|
|
}
|
|
}
|
|
|
|
func TestFirewalldReadinessUsesCommittedProjectionWithoutMutation(t *testing.T) {
|
|
runner := newFakeFirewalldRunner(true)
|
|
driver := &linuxDriver{
|
|
ipPath: "/usr/sbin/ip", nftPath: "/usr/sbin/nft",
|
|
firewallCmdPath: "/usr/bin/firewall-cmd", run: runner,
|
|
}
|
|
network := testNetwork(1)
|
|
applied := map[string]Network{network.VPCKey: network}
|
|
if err := driver.reconcileFirewalld(t.Context(), nil, applied); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runner.calls = nil
|
|
if err := driver.firewalldReadiness(t.Context(), applied); err != nil {
|
|
t.Fatalf("committed firewalld projection was rejected: %v", err)
|
|
}
|
|
if got, want := len(runner.calls), 25; got != want {
|
|
t.Fatalf("readiness command count=%d, want %d single-pass inspections: %v", got, want, runner.calls)
|
|
}
|
|
for _, call := range runner.calls {
|
|
for _, mutation := range []string{"--new-", "--set-", "--add-", "--remove-", "--reload"} {
|
|
if strings.Contains(call, mutation) {
|
|
t.Fatalf("readiness probe mutated firewalld with %q", call)
|
|
}
|
|
}
|
|
}
|
|
if err := driver.firewalldReadiness(t.Context(), nil); err == nil {
|
|
t.Fatal("orphaned firewalld bindings were accepted without committed state")
|
|
} else {
|
|
var public interface{ PublicCode() string }
|
|
if !errors.As(err, &public) || public.PublicCode() != "vpc_firewalld_conflict" {
|
|
t.Fatalf("orphaned binding error=%v, want vpc_firewalld_conflict", err)
|
|
}
|
|
}
|
|
bridge, _ := BridgeName(network.VPCKey)
|
|
delete(runner.runtime.zones[firewalldGuestZone].interfaces, bridge)
|
|
delete(runner.permanent.zones[firewalldGuestZone].interfaces, bridge)
|
|
if err := driver.firewalldReadiness(t.Context(), applied); err == nil {
|
|
t.Fatal("incomplete committed firewalld bindings were reported ready")
|
|
} else {
|
|
var public interface{ PublicCode() string }
|
|
if !errors.As(err, &public) || public.PublicCode() != "vpc_firewalld_apply_unconfirmed" {
|
|
t.Fatalf("incomplete binding error=%v, want vpc_firewalld_apply_unconfirmed", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFirewalldReadinessRejectsCommittedInterfaceInForeignZone(t *testing.T) {
|
|
runner := newFakeFirewalldRunner(true)
|
|
driver := &linuxDriver{
|
|
ipPath: "/usr/sbin/ip", nftPath: "/usr/sbin/nft",
|
|
firewallCmdPath: "/usr/bin/firewall-cmd", run: runner,
|
|
}
|
|
network := testNetwork(1)
|
|
applied := map[string]Network{network.VPCKey: network}
|
|
if err := driver.reconcileFirewalld(t.Context(), nil, applied); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
bridge, _ := BridgeName(network.VPCKey)
|
|
delete(runner.runtime.zones[firewalldGuestZone].interfaces, bridge)
|
|
runner.runtime.zones["public"] = &fakeFirewalldZone{
|
|
target: "default", interfaces: map[string]struct{}{bridge: {}},
|
|
}
|
|
|
|
err := driver.firewalldReadiness(t.Context(), applied)
|
|
var public interface{ PublicCode() string }
|
|
if err == nil || !errors.As(err, &public) || public.PublicCode() != "vpc_firewalld_conflict" {
|
|
t.Fatalf("foreign interface ownership was not rejected: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestReconcileFirewalldRejectsForeignInterfaceBeforeMutation(t *testing.T) {
|
|
runner := newFakeFirewalldRunner(true)
|
|
runner.runtime.zones[firewalldGuestZone] = &fakeFirewalldZone{
|
|
target: "default", forward: true,
|
|
interfaces: map[string]struct{}{"foreign0": {}},
|
|
}
|
|
driver := &linuxDriver{
|
|
ipPath: "/usr/sbin/ip", nftPath: "/usr/sbin/nft",
|
|
firewallCmdPath: "/usr/bin/firewall-cmd", run: runner,
|
|
}
|
|
network := testNetwork(1)
|
|
err := driver.reconcileFirewalld(t.Context(), nil, map[string]Network{network.VPCKey: network})
|
|
var public interface{ PublicCode() string }
|
|
if err == nil || !errors.As(err, &public) || public.PublicCode() != "vpc_firewalld_conflict" {
|
|
t.Fatalf("foreign firewalld binding was not rejected: %v", err)
|
|
}
|
|
for _, call := range runner.calls {
|
|
for _, mutation := range []string{"--new-", "--set-", "--add-", "--remove-"} {
|
|
if strings.Contains(call, mutation) {
|
|
t.Fatalf("preflight conflict performed mutation %q", call)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestReconcileFirewalldRemovalKeepsDefinitionsButUnbindsInterfaces(t *testing.T) {
|
|
runner := newFakeFirewalldRunner(true)
|
|
driver := &linuxDriver{
|
|
ipPath: "/usr/sbin/ip", nftPath: "/usr/sbin/nft",
|
|
firewallCmdPath: "/usr/bin/firewall-cmd", run: runner,
|
|
}
|
|
network := testNetwork(1)
|
|
applied := map[string]Network{network.VPCKey: network}
|
|
if err := driver.reconcileFirewalld(t.Context(), nil, applied); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := driver.reconcileFirewalld(t.Context(), applied, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for scopeName, scope := range map[string]fakeFirewalldScope{"runtime": runner.runtime, "permanent": runner.permanent} {
|
|
if len(scope.zones[firewalldGuestZone].interfaces) != 0 || len(scope.zones[firewalldTransportZone].interfaces) != 0 {
|
|
t.Fatalf("%s scope retained removed VPC interfaces", scopeName)
|
|
}
|
|
if scope.policies[firewalldGuestToTransportPolicy] == nil || scope.policies[firewalldTransportToGuestPolicy] == nil {
|
|
t.Fatalf("%s scope lost owned policy definitions", scopeName)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestReconcileFirewalldRepairsIncompleteRuntimeFailClosed(t *testing.T) {
|
|
runner := newFakeFirewalldRunner(true)
|
|
driver := &linuxDriver{
|
|
ipPath: "/usr/sbin/ip", nftPath: "/usr/sbin/nft",
|
|
firewallCmdPath: "/usr/bin/firewall-cmd", run: runner,
|
|
}
|
|
network := testNetwork(1)
|
|
applied := map[string]Network{network.VPCKey: network}
|
|
if err := driver.reconcileFirewalld(t.Context(), nil, applied); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runner.calls = nil
|
|
runner.runtime.zones[firewalldGuestZone].forward = false
|
|
delete(runner.runtime.policies[firewalldGuestToTransportPolicy].egress, firewalldTransportZone)
|
|
if err := driver.reconcileFirewalld(t.Context(), applied, applied); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if countCallsContaining(runner.calls, "firewall-cmd --reload") != 1 {
|
|
t.Fatalf("incomplete runtime did not receive exactly one repair reload: %v", runner.calls)
|
|
}
|
|
removeAt := indexCallContaining(runner.calls, "firewall-cmd --zone="+firewalldTransportZone+" --remove-interface=")
|
|
reloadAt := indexCallContaining(runner.calls, "firewall-cmd --reload")
|
|
nftAt := indexCallContaining(runner.calls, "nft -f -")
|
|
bindAt := indexCallContaining(runner.calls, "firewall-cmd --zone="+firewalldTransportZone+" --add-interface=")
|
|
if removeAt < 0 || reloadAt <= removeAt || nftAt <= reloadAt || bindAt <= nftAt {
|
|
t.Fatalf("runtime repair was not fail-closed: remove=%d reload=%d nft=%d bind=%d calls=%v", removeAt, reloadAt, nftAt, bindAt, runner.calls)
|
|
}
|
|
complete, err := driver.firewalldDefinitionsComplete(t.Context(), firewalldScope{})
|
|
if err != nil || !complete {
|
|
t.Fatalf("runtime definitions remain incomplete: complete=%v err=%v", complete, err)
|
|
}
|
|
}
|
|
|
|
func TestRestrictFirewalldRemovesRetiredBindingsWithoutAddingPaths(t *testing.T) {
|
|
runner := newFakeFirewalldRunner(true)
|
|
driver := &linuxDriver{
|
|
ipPath: "/usr/sbin/ip", nftPath: "/usr/sbin/nft",
|
|
firewallCmdPath: "/usr/bin/firewall-cmd", run: runner,
|
|
}
|
|
network := testNetwork(1)
|
|
applied := map[string]Network{network.VPCKey: network}
|
|
if err := driver.reconcileFirewalld(t.Context(), nil, applied); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runner.calls = nil
|
|
if err := driver.restrictFirewalld(t.Context(), firewalldPlan{active: true}, applied, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for scopeName, scope := range map[string]fakeFirewalldScope{"runtime": runner.runtime, "permanent": runner.permanent} {
|
|
if len(scope.zones[firewalldGuestZone].interfaces) != 0 || len(scope.zones[firewalldTransportZone].interfaces) != 0 {
|
|
t.Fatalf("%s scope retained retired VPC bindings", scopeName)
|
|
}
|
|
}
|
|
for _, call := range runner.calls {
|
|
if strings.Contains(call, " --add-") || strings.Contains(call, "--reload") {
|
|
t.Fatalf("restrict phase opened or reloaded a path: %q", call)
|
|
}
|
|
}
|
|
}
|
|
|
|
func sameStringSet(values map[string]struct{}, expected ...string) bool {
|
|
if len(values) != len(expected) {
|
|
return false
|
|
}
|
|
for _, value := range expected {
|
|
if _, exists := values[value]; !exists {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func countCallsContaining(calls []string, fragment string) int {
|
|
count := 0
|
|
for _, call := range calls {
|
|
if strings.Contains(call, fragment) {
|
|
count++
|
|
}
|
|
}
|
|
return count
|
|
}
|
|
|
|
func indexCallContaining(calls []string, fragment string) int {
|
|
for index, call := range calls {
|
|
if strings.Contains(call, fragment) {
|
|
return index
|
|
}
|
|
}
|
|
return -1
|
|
}
|