f-76: vm: filtrage nftables par tap, champ dhcp_server, watchdog #76
Chaque NIC a sa chaîne pre-tap<id>, posée avant le tap et retirée après : MAC, ARP et IPv4 source limités à ceux de la NIC (0.0.0.0 pour le client DHCP et la sonde ARP), réponses DHCP refusées sauf si dhcp_server, seuls IPv4, ARP et IPv6 émis. Le watchdog vérifie la chaîne de chaque NIC. Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
d98e42e0a8
commit
74bfda7004
16 changed files with 295 additions and 15 deletions
|
|
@ -10,9 +10,11 @@ import (
|
|||
|
||||
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
||||
"git.g3e.fr/syonad/two/internal/netns"
|
||||
"git.g3e.fr/syonad/two/internal/nft"
|
||||
"git.g3e.fr/syonad/two/internal/qemu"
|
||||
"git.g3e.fr/syonad/two/internal/qmp"
|
||||
"git.g3e.fr/syonad/two/internal/state"
|
||||
"git.g3e.fr/syonad/two/internal/vm"
|
||||
"git.g3e.fr/syonad/two/internal/watchdog/notify"
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
|
||||
|
|
@ -71,7 +73,7 @@ func checkVM(db *badger.DB, cfg *configuration.Config, name string, u unitChecke
|
|||
n.Notify(kindVM, name, fmt.Sprintf("vpc of subnet %s unreadable in database: %v", subnetName, err))
|
||||
continue
|
||||
}
|
||||
checkVMTap(entries[prefix+"tap_id"], name, vpc, idx, n)
|
||||
checkVMTap(entries, prefix, name, vpc, idx, n)
|
||||
}
|
||||
|
||||
checkVMQemu(cfg, name, n)
|
||||
|
|
@ -99,7 +101,12 @@ func nicIndexes(entries map[string]string, prefix string) []int {
|
|||
return indexes
|
||||
}
|
||||
|
||||
func checkVMTap(raw, name, vpc string, idx int, n notify.Notifier) {
|
||||
func tapPortFromEntries(entries map[string]string, prefix string, tapID int) nft.Port {
|
||||
return vm.TapPort(tapID, entries[prefix+"mac"], entries[prefix+"ip"], entries[prefix+"dhcp_server"] == "true")
|
||||
}
|
||||
|
||||
func checkVMTap(entries map[string]string, prefix, name, vpc string, idx int, n notify.Notifier) {
|
||||
raw := entries[prefix+"tap_id"]
|
||||
if raw == "" {
|
||||
n.Notify(kindVM, name, fmt.Sprintf("interface %d has no tap_id in database", idx))
|
||||
return
|
||||
|
|
@ -119,6 +126,14 @@ func checkVMTap(raw, name, vpc string, idx int, n notify.Notifier) {
|
|||
if p := linkProblem(tapName(tapID)); p != "" {
|
||||
n.Notify(kindVM, name, p+" (in netns "+vpc+")")
|
||||
}
|
||||
missing, err := nft.MissingRules(tapPortFromEntries(entries, prefix, tapID))
|
||||
if err != nil {
|
||||
n.Notify(kindVM, name, fmt.Sprintf("interface %d: nftables rules unreadable in netns %s: %v", idx, vpc, err))
|
||||
return nil
|
||||
}
|
||||
for _, rule := range missing {
|
||||
n.Notify(kindVM, name, fmt.Sprintf("interface %d: nftables rule missing in netns %s: %s", idx, vpc, rule))
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
n.Notify(kindVM, name, fmt.Sprintf("cannot enter netns %s: %v", vpc, err))
|
||||
|
|
|
|||
|
|
@ -34,6 +34,27 @@ func TestTapName(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestTapPortFromEntries(t *testing.T) {
|
||||
prefix := prefixVM + "i-pxe/nic/1/"
|
||||
entries := map[string]string{
|
||||
prefix + "mac": "52:54:00:00:0B:07",
|
||||
prefix + "ip": "10.0.1.7",
|
||||
prefix + "dhcp_server": "true",
|
||||
}
|
||||
p := tapPortFromEntries(entries, prefix, 87654321)
|
||||
got := strings.Join(p.Prerouting, "|")
|
||||
if p.Name != "tap87654321" || !strings.Contains(got, "ether saddr != 52:54:00:00:0b:07 drop") || !strings.Contains(got, "ip saddr != { 0.0.0.0, 10.0.1.7 } drop") {
|
||||
t.Errorf("port %+v", p)
|
||||
}
|
||||
if strings.Contains(got, "udp sport 67 drop") {
|
||||
t.Errorf("dhcp_server lu en base ignoré : %s", got)
|
||||
}
|
||||
delete(entries, prefix+"dhcp_server")
|
||||
if !strings.Contains(strings.Join(tapPortFromEntries(entries, prefix, 87654321).Prerouting, "|"), "udp sport 67 drop") {
|
||||
t.Errorf("sans dhcp_server, le blocage du serveur DHCP doit être attendu")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckVMs_ConfigNil(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
r := &recorder{}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue