f-42: vpc: br-public du netns fermé, br_netfilter coupé dans le netns #42
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
74e8c6220e
commit
1df0d2dad6
8 changed files with 128 additions and 0 deletions
28
internal/nft/guard.go
Normal file
28
internal/nft/guard.go
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
package nft
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var guardChains = []natChain{
|
||||
{"in-", "filter", "input", "filter"},
|
||||
{"fwd-", "filter", "forward", "filter"},
|
||||
}
|
||||
|
||||
func guardScript(bridge string) string {
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "add table ip %s\n", Table)
|
||||
for _, c := range guardChains {
|
||||
name := c.declare(&b, bridge)
|
||||
fmt.Fprintf(&b, "add rule ip %s %s iifname %q drop\n", Table, name, bridge)
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func SetBridgeGuard(bridge string) error {
|
||||
if err := apply(guardScript(bridge)); err != nil {
|
||||
return fmt.Errorf("guard %s: %w", bridge, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
17
internal/nft/guard_test.go
Normal file
17
internal/nft/guard_test.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package nft
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestGuardScript(t *testing.T) {
|
||||
want := `add table ip two
|
||||
add chain ip two in-br-public { type filter hook input priority filter; policy accept; }
|
||||
flush chain ip two in-br-public
|
||||
add rule ip two in-br-public iifname "br-public" drop
|
||||
add chain ip two fwd-br-public { type filter hook forward priority filter; policy accept; }
|
||||
flush chain ip two fwd-br-public
|
||||
add rule ip two fwd-br-public iifname "br-public" drop
|
||||
`
|
||||
if got := guardScript("br-public"); got != want {
|
||||
t.Errorf("script :\n%s\nattendu :\n%s", got, want)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,9 @@
|
|||
package sysctl
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
|
@ -21,3 +23,13 @@ func Set(key, value string) error {
|
|||
}
|
||||
return f.Close()
|
||||
}
|
||||
|
||||
func SetIfPresent(key, value string) (bool, error) {
|
||||
path := filepath.Join(root, strings.ReplaceAll(key, ".", "/"))
|
||||
if _, err := os.Stat(path); errors.Is(err, fs.ErrNotExist) {
|
||||
return false, nil
|
||||
} else if err != nil {
|
||||
return false, fmt.Errorf("sysctl %s: %w", key, err)
|
||||
}
|
||||
return true, Set(key, value)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -46,3 +46,25 @@ func TestSetRefusesAnUnknownKeyWithoutCreatingIt(t *testing.T) {
|
|||
t.Errorf("clé inconnue créée : %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetIfPresentSkipsAnAbsentKey(t *testing.T) {
|
||||
dir := fakeRoot(t)
|
||||
set, err := SetIfPresent("net.bridge.bridge-nf-call-iptables", "0")
|
||||
if err != nil || set {
|
||||
t.Errorf("clé absente : %v, %v ; false, nil attendus", set, err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, "net", "bridge")); err == nil {
|
||||
t.Error("répertoire créé pour une clé absente")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetIfPresentWritesAPresentKey(t *testing.T) {
|
||||
dir := fakeRoot(t)
|
||||
set, err := SetIfPresent("net.ipv4.ip_forward", "1")
|
||||
if err != nil || !set {
|
||||
t.Fatalf("clé présente : %v, %v", set, err)
|
||||
}
|
||||
if got, _ := os.ReadFile(filepath.Join(dir, "net", "ipv4", "ip_forward")); string(got) != "1" {
|
||||
t.Errorf("contenu %q", got)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,6 +32,10 @@ func CreateVPC(db *badger.DB, name string, cfg *configuration.Config) error {
|
|||
return err
|
||||
}
|
||||
|
||||
if err := netns.Call(name, guardPublicBridge); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := netif.BridgeSetMaster("vp-"+vpcID+"-e", cfg.PublicBridge()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
26
internal/vpc/guard.go
Normal file
26
internal/vpc/guard.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package vpc
|
||||
|
||||
import (
|
||||
"git.g3e.fr/syonad/two/internal/nft"
|
||||
"git.g3e.fr/syonad/two/internal/sysctl"
|
||||
)
|
||||
|
||||
const netnsPublicBridge = "br-public"
|
||||
|
||||
var bridgeNetfilterKeys = []string{
|
||||
"net.bridge.bridge-nf-call-iptables",
|
||||
"net.bridge.bridge-nf-call-ip6tables",
|
||||
"net.bridge.bridge-nf-call-arptables",
|
||||
}
|
||||
|
||||
func guardPublicBridge() error {
|
||||
for _, key := range bridgeNetfilterKeys {
|
||||
if _, err := sysctl.SetIfPresent(key, "0"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := sysctl.Set("net.ipv4.conf."+netnsPublicBridge+".arp_ignore", "1"); err != nil {
|
||||
return err
|
||||
}
|
||||
return nft.SetBridgeGuard(netnsPublicBridge)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue