f-90: nft: chaînes hostsvc et md-<vm> dans la table ip two #90

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-31 14:15:01 +01:00
commit 4f7d55a9ed
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
5 changed files with 274 additions and 1 deletions

81
internal/nft/hostsvc.go Normal file
View file

@ -0,0 +1,81 @@
package nft
import (
"fmt"
"strings"
)
const HostSvcChain = "hostsvc"
var hostSvc = natChain{"", "nat", "prerouting", "dstnat"}
var hostSvcExcluded = []string{"br-public", "l3-*"}
func hostSvcRules(target string) []string {
var rules []string
for _, iface := range hostSvcExcluded {
rules = append(rules, fmt.Sprintf("iifname %q return", iface))
}
for _, p := range []struct{ proto, port string }{{"udp", "53"}, {"tcp", "53"}, {"udp", "123"}} {
rules = append(rules, fmt.Sprintf("fib daddr type local %s dport %s dnat to %s", p.proto, p.port, target))
}
return rules
}
func hostSvcScript(target string) string {
var b strings.Builder
fmt.Fprintf(&b, "add table ip %s\n", Table)
name := hostSvc.declare(&b, HostSvcChain)
for _, r := range hostSvcRules(target) {
fmt.Fprintf(&b, "add rule ip %s %s %s\n", Table, name, r)
}
return b.String()
}
func SetHostSvcRedirect(target string) error {
if err := apply(hostSvcScript(target)); err != nil {
return fmt.Errorf("set dns and ntp redirect: %w", err)
}
return nil
}
func HasHostSvcRedirect(target string) (bool, error) {
out, err := run([]string{"list", "chains", "ip"}, "")
if err != nil {
return false, err
}
if !hasChain(string(out), HostSvcChain) {
return false, nil
}
out, err = run([]string{"list", "chain", "ip", Table, HostSvcChain}, "")
if err != nil {
return false, err
}
return sameRules(chainRules(string(out)), append([]string{hostSvc.header()}, hostSvcRules(target)...)), nil
}
func hasChain(listing, chain string) bool {
table := ""
for _, line := range strings.Split(listing, "\n") {
fields := strings.Fields(line)
switch {
case len(fields) == 4 && fields[0] == "table" && fields[1] == "ip":
table = fields[2]
case len(fields) == 3 && fields[0] == "chain" && table == Table && fields[1] == chain:
return true
}
}
return false
}
func sameRules(have, want []string) bool {
if len(have) != len(want) {
return false
}
for i := range want {
if have[i] != want[i] {
return false
}
}
return true
}

View file

@ -0,0 +1,104 @@
package nft
import (
"strings"
"testing"
)
const listedHostSvcChains = `table ip two {
chain snat-ng-t46 {
type nat hook postrouting priority srcnat; policy accept;
}
chain hostsvc {
type nat hook prerouting priority dstnat; policy accept;
}
chain md-t90-m1 {
type nat hook prerouting priority dstnat; policy accept;
}
}
`
const listedHostSvcChain = `table ip two {
chain hostsvc {
type nat hook prerouting priority dstnat; policy accept;
iifname "br-public" return
iifname "l3-*" return
fib daddr type local udp dport 53 dnat to 169.254.169.253
fib daddr type local tcp dport 53 dnat to 169.254.169.253
fib daddr type local udp dport 123 dnat to 169.254.169.253
}
}
`
func TestHostSvcScript(t *testing.T) {
want := `add table ip two
add chain ip two hostsvc { type nat hook prerouting priority dstnat; policy accept; }
flush chain ip two hostsvc
add rule ip two hostsvc iifname "br-public" return
add rule ip two hostsvc iifname "l3-*" return
add rule ip two hostsvc fib daddr type local udp dport 53 dnat to 169.254.169.253
add rule ip two hostsvc fib daddr type local tcp dport 53 dnat to 169.254.169.253
add rule ip two hostsvc fib daddr type local udp dport 123 dnat to 169.254.169.253
`
if got := hostSvcScript("169.254.169.253"); got != want {
t.Errorf("script :\n%s\nattendu :\n%s", got, want)
}
}
func TestHostSvcRules_ExclusionsComeFirst(t *testing.T) {
rules := hostSvcRules("169.254.169.253")
if len(rules) != 5 {
t.Fatalf("%d règles, 2 exclusions et 3 redirections attendues : %v", len(rules), rules)
}
for i, iface := range []string{"br-public", "l3-*"} {
if want := `iifname "` + iface + `" return`; rules[i] != want {
t.Errorf("règle %d = %q, %q attendu : le segment public et le lien de la VRF n'atteignent jamais le relais", i, rules[i], want)
}
}
}
func TestHostSvcRules_OnlyLocalDestinations(t *testing.T) {
for _, r := range hostSvcRules("169.254.169.253") {
if strings.Contains(r, "dnat") && !strings.HasPrefix(r, "fib daddr type local ") {
t.Errorf("%q : un résolveur extérieur ne doit pas être détourné", r)
}
}
}
func TestHasChain(t *testing.T) {
if !hasChain(listedHostSvcChains, "hostsvc") {
t.Error("hostsvc non trouvée")
}
if hasChain(listedHostSvcChains, "md-t90") {
t.Error("md-t90 trouvée par préfixe")
}
if hasChain(strings.Replace(listedHostSvcChains, "table ip two", "table ip nat", 1), "hostsvc") {
t.Error("hostsvc trouvée dans une autre table")
}
if hasChain("", "hostsvc") {
t.Error("hostsvc trouvée sans table")
}
}
func TestHostSvcListingMatches(t *testing.T) {
want := append([]string{hostSvc.header()}, hostSvcRules("169.254.169.253")...)
if !sameRules(chainRules(listedHostSvcChain), want) {
t.Errorf("affichage nft %v, %v attendu", chainRules(listedHostSvcChain), want)
}
}
func TestHostSvcListingMismatches(t *testing.T) {
want := append([]string{hostSvc.header()}, hostSvcRules("169.254.169.253")...)
for label, listing := range map[string]string{
"règle 123 absente": strings.Replace(listedHostSvcChain, "\t\tfib daddr type local udp dport 123 dnat to 169.254.169.253\n", "", 1),
"règle 123 sans fib": strings.Replace(listedHostSvcChain, "fib daddr type local udp dport 123", "udp dport 123", 1),
"autre cible": strings.Replace(listedHostSvcChain, "53 dnat to 169.254.169.253", "53 dnat to 10.0.0.1", 1),
"exclusion après dnat": strings.Replace(strings.Replace(listedHostSvcChain, "\t\tiifname \"br-public\" return\n", "", 1), "dport 123 dnat to 169.254.169.253\n", "dport 123 dnat to 169.254.169.253\n\t\tiifname \"br-public\" return\n", 1),
"chaîne sans crochet": strings.Replace(listedHostSvcChain, "\t\ttype nat hook prerouting priority dstnat; policy accept;\n", "", 1),
"crochet d'une autre priorité": strings.Replace(listedHostSvcChain, "priority dstnat", "priority srcnat", 1),
} {
if sameRules(chainRules(listing), want) {
t.Errorf("%s : chaîne jugée conforme", label)
}
}
}

46
internal/nft/metadata.go Normal file
View file

@ -0,0 +1,46 @@
package nft
import (
"fmt"
"strings"
"git.g3e.fr/syonad/two/internal/metadata"
)
var metadataChain = natChain{"md-", "nat", "prerouting", "dstnat"}
func metadataRule(ip, target string) string {
return fmt.Sprintf("ip saddr %s ip daddr %s tcp dport 80 dnat to %s", ip, metadata.ServiceIP, target)
}
func metadataSetScript(vm string, ips []string, target string) string {
var b strings.Builder
fmt.Fprintf(&b, "add table ip %s\n", Table)
name := metadataChain.declare(&b, vm)
for _, ip := range ips {
fmt.Fprintf(&b, "add rule ip %s %s %s\n", Table, name, metadataRule(ip, target))
}
return b.String()
}
func metadataDeleteScript(vm string) string {
var b strings.Builder
fmt.Fprintf(&b, "add table ip %s\n", Table)
name := metadataChain.declare(&b, vm)
fmt.Fprintf(&b, "delete chain ip %s %s\n", Table, name)
return b.String()
}
func SetMetadataRedirect(vm string, ips []string, target string) error {
if err := apply(metadataSetScript(vm, ips, target)); err != nil {
return fmt.Errorf("set metadata redirect %s: %w", vm, err)
}
return nil
}
func DeleteMetadataRedirect(vm string) error {
if err := apply(metadataDeleteScript(vm)); err != nil {
return fmt.Errorf("delete metadata redirect %s: %w", vm, err)
}
return nil
}

View file

@ -0,0 +1,38 @@
package nft
import (
"strings"
"testing"
)
func TestMetadataSetScript(t *testing.T) {
want := `add table ip two
add chain ip two md-t90-m1 { type nat hook prerouting priority dstnat; policy accept; }
flush chain ip two md-t90-m1
add rule ip two md-t90-m1 ip saddr 10.90.1.10 ip daddr 169.254.169.254 tcp dport 80 dnat to 10.90.1.1:8001
add rule ip two md-t90-m1 ip saddr 10.90.2.10 ip daddr 169.254.169.254 tcp dport 80 dnat to 10.90.1.1:8001
`
if got := metadataSetScript("t90-m1", []string{"10.90.1.10", "10.90.2.10"}, "10.90.1.1:8001"); got != want {
t.Errorf("script :\n%s\nattendu :\n%s", got, want)
}
}
func TestMetadataDeleteScript(t *testing.T) {
want := `add table ip two
add chain ip two md-t90-m1 { type nat hook prerouting priority dstnat; policy accept; }
flush chain ip two md-t90-m1
delete chain ip two md-t90-m1
`
if got := metadataDeleteScript("t90-m1"); got != want {
t.Errorf("script :\n%s\nattendu :\n%s", got, want)
}
}
func TestMetadataScriptsStayInTheirChain(t *testing.T) {
script := metadataSetScript("t90-m1", []string{"10.90.1.10"}, "10.90.1.1:8001") + metadataDeleteScript("t90-m1")
for _, line := range strings.Split(strings.TrimSpace(script), "\n") {
if line != "add table ip two" && !strings.Contains(line, " ip two md-t90-m1") {
t.Errorf("ligne hors de la chaîne md-t90-m1 : %q", line)
}
}
}

View file

@ -23,9 +23,13 @@ var natChains = []natChain{
{"in-", "filter", "input", "filter"},
}
func (c natChain) header() string {
return fmt.Sprintf("type %s hook %s priority %s; policy accept;", c.kind, c.hook, c.priority)
}
func (c natChain) declare(b *strings.Builder, iface string) string {
name := c.prefix + iface
fmt.Fprintf(b, "add chain ip %s %s { type %s hook %s priority %s; policy accept; }\n", Table, name, c.kind, c.hook, c.priority)
fmt.Fprintf(b, "add chain ip %s %s { %s }\n", Table, name, c.header())
fmt.Fprintf(b, "flush chain ip %s %s\n", Table, name)
return name
}