Compare commits
1 commit
175d6bbeee
...
7b6b454b60
| Author | SHA1 | Date | |
|---|---|---|---|
|
7b6b454b60 |
107 changed files with 460 additions and 5038 deletions
|
|
@ -52,7 +52,6 @@ jobs:
|
|||
- metadata
|
||||
- agent
|
||||
- dhcp
|
||||
- two-hostsvc
|
||||
uses: ./.forgejo/workflows/build.yml
|
||||
with:
|
||||
tag: ${{ needs.set-release-target.outputs.release_cible }}
|
||||
|
|
@ -81,8 +80,6 @@ jobs:
|
|||
name: dhcp@.service
|
||||
- path: systemd/metadata@.service
|
||||
name: metadata@.service
|
||||
- path: systemd/two-hostsvc.service
|
||||
name: two-hostsvc.service
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Move asset
|
||||
|
|
|
|||
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -37,5 +37,3 @@ docs/_build/
|
|||
# Binaires compilés en local pour le lab (test/e2e/build.sh)
|
||||
test/e2e/topologies/bin/*
|
||||
!test/e2e/topologies/bin/.gitkeep
|
||||
test/e2e/topologies/scripts/*
|
||||
!test/e2e/topologies/scripts/.gitkeep
|
||||
|
|
|
|||
|
|
@ -562,17 +562,6 @@ components:
|
|||
primary:
|
||||
type: boolean
|
||||
example: true
|
||||
dhcp_server:
|
||||
type: boolean
|
||||
default: false
|
||||
description: >
|
||||
Lets the VM answer DHCP (UDP from port 67) on this interface, for
|
||||
instance a PXE server for diskless hosts on a `bridge` subnet. Every
|
||||
other interface may not send DHCP answers. The interface still sends
|
||||
only from its own MAC and IP. The agent applies it whatever the mode
|
||||
of the subnet: restricting it is the caller's job. Always returned
|
||||
by GET.
|
||||
example: false
|
||||
|
||||
VMStorage:
|
||||
type: object
|
||||
|
|
|
|||
|
|
@ -1,78 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
hostsvcapi "git.g3e.fr/syonad/two/internal/api/hostsvc"
|
||||
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
||||
"git.g3e.fr/syonad/two/internal/hostsvc"
|
||||
"git.g3e.fr/syonad/two/pkg/logger"
|
||||
)
|
||||
|
||||
var (
|
||||
confFile = flag.String("conf", "/etc/two/agent.yml", "configuration file")
|
||||
runDir = flag.String("run-dir", hostsvcapi.DefaultRunDir, "directory of the control socket and the state file")
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
if err := run(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "two-hostsvc: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run() error {
|
||||
cfg, err := configuration.LoadConfig(*confFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("load config: %w", err)
|
||||
}
|
||||
|
||||
log := logger.New(cfg.Logger.Level, cfg.Logger.Debug)
|
||||
|
||||
svc, err := hostsvc.New(hostsvc.Options{
|
||||
DNS: cfg.HostSvc.DNS(),
|
||||
NTP: cfg.HostSvc.NTP(),
|
||||
Rate: cfg.HostSvc.Rate(),
|
||||
Logger: log,
|
||||
}, hostsvcapi.StatePath(*runDir))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := svc.Restore(); err != nil {
|
||||
log.Error("some vpcs could not be restored", "error", err)
|
||||
}
|
||||
|
||||
control, err := hostsvcapi.Listen(svc, hostsvcapi.SocketPath(*runDir), log)
|
||||
if err != nil {
|
||||
return fmt.Errorf("listen on the control socket: %w", err)
|
||||
}
|
||||
defer control.Close()
|
||||
|
||||
log.Info("two-hostsvc started", "socket", control.Addr(), "vpcs", svc.List())
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
go func() {
|
||||
if err := control.Serve(); err != nil && !errors.Is(err, net.ErrClosed) {
|
||||
log.Error("control socket stopped", "error", err)
|
||||
stop()
|
||||
}
|
||||
}()
|
||||
|
||||
<-ctx.Done()
|
||||
log.Info("two-hostsvc stopping")
|
||||
if err := control.Close(); err != nil && !errors.Is(err, net.ErrClosed) {
|
||||
log.Error("close the control socket", "error", err)
|
||||
}
|
||||
return svc.Shutdown()
|
||||
}
|
||||
|
|
@ -39,12 +39,10 @@ times:
|
|||
# Bridge interface used when the requested iface_type is not found in interfaces
|
||||
default_interface: br-000000
|
||||
|
||||
# Map of logical interface types to physical bridge names on this host.
|
||||
# internet: public bridge, carrying vxlan-public (deploy.sh --public_vni); every VPC
|
||||
# plugs its vp-<id>-e veth into it. br-public when absent, never default_interface.
|
||||
# Map of logical interface types to physical bridge names on this host
|
||||
interfaces:
|
||||
vms: br-000000
|
||||
internet: br-public
|
||||
internet: br-000000
|
||||
admin: br-000000
|
||||
|
||||
# MTU, identical on every hypervisor.
|
||||
|
|
@ -82,20 +80,6 @@ metadata:
|
|||
dhcp:
|
||||
backend: two
|
||||
|
||||
# DNS and NTP relay of the VMs (two-hostsvc, one process per hypervisor).
|
||||
# It listens on 169.254.169.253 in every VPC netns of the host; the VMs reach it
|
||||
# through their gateway (interface_ip, announced by DHCP options 6 and 42).
|
||||
# dns_upstream : resolvers queried first (ip or ip:port, port 53 by default);
|
||||
# the host resolver (/etc/resolv.conf) comes after them
|
||||
# ntp_upstream : NTP servers relayed first (ip or ip:port, port 123 by
|
||||
# default); without an answer, the host clock replies
|
||||
# rate_limit_per_vpc : requests per second, DNS and NTP together, per VPC
|
||||
# Its control socket and state file live in /run/two/hostsvc.
|
||||
hostsvc:
|
||||
dns_upstream: []
|
||||
ntp_upstream: []
|
||||
rate_limit_per_vpc: 100
|
||||
|
||||
# QEMU runtime paths
|
||||
qemu:
|
||||
# UEFI firmware (requires apt install ovmf on Debian/Ubuntu)
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ Réseau
|
|||
* **La route vers le CIDR du VPC garde ``interface_ip`` comme next-hop** dans tous les modes sauf
|
||||
``bridge`` : sur un subnet à IP publique, le trafic interne ne doit pas sortir par la gateway
|
||||
publique.
|
||||
* **La MAC anycast d'une gateway ``vxlan`` ne sort jamais du netns** : règles nftables sur le veth
|
||||
* **La MAC anycast d'une gateway ``vxlan`` ne sort jamais du netns** : règles ebtables sur le veth
|
||||
du subnet (source anycast rejetée en entrée et en sortie) **et** entrée FDB locale dans le bridge
|
||||
du netns. Sans l'entrée FDB, que le macvlan ne crée pas, toute trame d'une VM vers sa gateway est
|
||||
inondée vers le fabric et traitée par la gateway de chaque hôte.
|
||||
|
|
@ -49,9 +49,6 @@ QEMU et VM
|
|||
* QEMU est lancé par ``systemd-run --scope``, **jamais** en unit transitoire : le scope est
|
||||
exécuté par le processus appelant et hérite du netns posé par ``netns.Call``. Une unit
|
||||
transitoire, forkée par PID 1, démarrerait dans le netns racine et ne verrait pas le tap.
|
||||
* **Un tap n'existe jamais sans ses règles** : la chaîne ``pre-tap<id>`` est posée avant la
|
||||
création du tap et retirée après sa suppression. Dans l'autre ordre, une VM pourrait émettre
|
||||
sans filtre entre deux étapes.
|
||||
* L'arrêt d'une VM ne touche **jamais** aux fichiers disque. En revanche, un ``quit`` brutal est
|
||||
envoyé à l'expiration de ``times.timeout_vms_stop``. ``system_powerdown`` est renvoyé toutes les
|
||||
``times.next_vms_shutdown`` : un appui ACPI arrivé avant que l'invité sache le traiter est perdu.
|
||||
|
|
|
|||
|
|
@ -26,7 +26,6 @@ de sérialisation structurée.
|
|||
vm/<name>/nic/<i>/ip → <ip>
|
||||
vm/<name>/nic/<i>/mac → <mac> (fournie par l'appelant, conservée telle quelle)
|
||||
vm/<name>/nic/<i>/primary → "true" (exactement une interface ; absent sur les autres)
|
||||
vm/<name>/nic/<i>/dhcp_server → "true" (absent si l'interface ne sert pas le DHCP)
|
||||
vm/<name>/nic/<i>/tap_id → <int> (alloué au premier démarrage, puis conservé)
|
||||
vm/<name>/metadata_port → <port>
|
||||
vm/<name>/disk/<dev> → <path> (une clé par disque : vda, xvda, xvdb, …)
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ Paquets
|
|||
- network namespaces : create/enter/delete/call
|
||||
* - ``internal/netif``
|
||||
- netlink : bridge, veth, vxlan, tap, routes, adresses
|
||||
* - ``internal/nft``, ``internal/iptables``
|
||||
* - ``internal/ebtables``, ``internal/iptables``
|
||||
- wrappers dédiés ; ne pas appeler ces binaires ailleurs
|
||||
* - ``internal/qemu``, ``internal/qmp``
|
||||
- lancement de QEMU et client QMP sur socket Unix
|
||||
|
|
|
|||
|
|
@ -1,85 +0,0 @@
|
|||
DNS et NTP des VM
|
||||
=================
|
||||
|
||||
Les VM résolvent les noms et lisent l'heure par leur gateway : le DHCP annonce l'``interface_ip``
|
||||
du subnet comme serveur DNS (option 6) et NTP (option 42), dans les deux backends. Un relais
|
||||
unique par hyperviseur, ``two-hostsvc``, répond derrière cette adresse dans le netns de chaque
|
||||
VPC. Une VM sans sortie Internet garde ainsi DNS et NTP, et aucune route n'est ouverte entre la
|
||||
VRF du VPC et l'hôte.
|
||||
|
||||
Chemin d'une requête
|
||||
--------------------
|
||||
|
||||
.. mermaid::
|
||||
|
||||
graph LR
|
||||
V["VM<br/>vers interface_ip:53 ou :123"] --> N["netns du VPC<br/>chaîne TWO-HOSTSVC<br/>DNAT vers 169.254.169.253"]
|
||||
N --> H["two-hostsvc<br/>socket dans le netns<br/>169.254.169.253 sur la dummy hostsvc"]
|
||||
H -->|"depuis l'hôte"| U["serveurs amont<br/>puis résolveur ou horloge de l'hôte"]
|
||||
|
||||
À la création d'un VPC sur l'hyperviseur, l'agent pose dans son netns :
|
||||
|
||||
* une interface ``dummy`` ``hostsvc`` portant ``169.254.169.253/32`` ;
|
||||
* une chaîne iptables ``TWO-HOSTSVC`` (table ``nat``), appelée depuis ``PREROUTING`` pour le seul
|
||||
trafic à destination d'une adresse locale du netns (``addrtype LOCAL`` : les gateways des
|
||||
subnets, anycast compris). Elle renvoie d'abord ce qui arrive par ``br-public`` (le segment
|
||||
public de l'hôte) et par ``l3-+`` (le lien vers la VRF), puis redirige 53 en UDP et TCP et 123
|
||||
en UDP vers ``169.254.169.253`` ;
|
||||
|
||||
puis demande à ``two-hostsvc`` d'ouvrir ses sockets dans ce netns. La réponse repart par
|
||||
conntrack, avec l'adresse de la gateway pour source. Une VM qui interroge un résolveur extérieur
|
||||
(par une passerelle NAT) n'est pas redirigée : seules les adresses locales du netns le sont.
|
||||
|
||||
À la suppression du VPC, l'agent demande la fermeture des sockets ; la ``dummy`` et la chaîne
|
||||
disparaissent avec le netns.
|
||||
|
||||
**Si ``two-hostsvc`` ne répond pas, la création du VPC échoue** (``error``) plutôt que de créer
|
||||
un VPC sans DNS ni NTP ; la suppression échoue de même si la fermeture échoue, et se relance
|
||||
depuis ``error``. ``two-hostsvc`` doit donc tourner sur tout hyperviseur avant la création d'un
|
||||
VPC.
|
||||
|
||||
Le relais
|
||||
---------
|
||||
|
||||
``two-hostsvc`` est un seul processus par hyperviseur (``two-hostsvc.service``). Il entre dans le
|
||||
netns de chaque VPC le temps d'y ouvrir trois sockets sur ``169.254.169.253`` (53/UDP, 53/TCP,
|
||||
123/UDP), qui restent attachées à ce netns ; les requêtes vers l'amont partent de l'espace
|
||||
réseau de l'hôte.
|
||||
|
||||
DNS
|
||||
La requête est transmise telle quelle aux serveurs de ``hostsvc.dns_upstream``, puis à ceux de
|
||||
``/etc/resolv.conf`` de l'hôte. Seule une question standard est acceptée ; une réponse amont
|
||||
dont l'identifiant ne correspond pas est ignorée. Pas de cache : rien de ce qu'un locataire
|
||||
demande n'est gardé pour un autre.
|
||||
|
||||
NTP
|
||||
La requête est relayée aux serveurs de ``hostsvc.ntp_upstream`` ; une réponse dont
|
||||
l'horodatage d'origine ne correspond pas est ignorée. Sans serveur amont, ou sans réponse,
|
||||
``two-hostsvc`` répond depuis l'horloge de l'hôte (SNTP, stratum 3) ; si l'hôte n'est pas
|
||||
synchronisé, la réponse porte l'indicateur d'alarme (LI = 3, stratum 16) et les clients
|
||||
l'ignorent.
|
||||
|
||||
Limite de débit
|
||||
``hostsvc.rate_limit_per_vpc`` requêtes par seconde, DNS et NTP confondus, par VPC ; au-delà,
|
||||
les requêtes sont abandonnées. Au plus 16 connexions TCP simultanées par VPC.
|
||||
|
||||
Le relais garde la liste des VPC servis dans ``/run/two/hostsvc/hostsvc.state`` : à son
|
||||
redémarrage, il rouvre seul les sockets des VPC dont le netns existe encore. Sa socket de
|
||||
contrôle, ``/run/two/hostsvc/hostsvc.sock`` (mode 600), n'est utilisée que par l'agent.
|
||||
|
||||
Isolation
|
||||
---------
|
||||
|
||||
* Le relais n'écoute que sur une adresse link-local, et seulement dans les netns des VPC ; il ne
|
||||
crée aucune route.
|
||||
* Une réponse ne repart que vers l'adresse qui a posé la question, par la socket du VPC.
|
||||
* Le trafic du segment public (``br-public``) et du lien vers la VRF n'atteint pas le relais.
|
||||
* L'unité systemd ne garde que trois capacités : ``CAP_SYS_ADMIN`` (entrer dans un netns),
|
||||
``CAP_NET_BIND_SERVICE`` (ports 53 et 123) et ``CAP_DAC_READ_SEARCH`` (les fichiers de
|
||||
``/run/netns`` sont en mode 000). Le système de fichiers lui est en lecture seule, hors
|
||||
``/run/two/hostsvc``.
|
||||
* Les netns créés après le démarrage du service ne lui sont visibles que si ``/`` est en
|
||||
propagation ``shared``, le réglage par défaut de systemd.
|
||||
|
||||
Si ``two-hostsvc`` s'arrête, DNS et NTP s'arrêtent pour tous les VPC de l'hyperviseur ;
|
||||
systemd le relance (``Restart=on-failure``) et le watchdog le signale.
|
||||
|
|
@ -12,4 +12,3 @@ fonctionnent entre eux. Le contrat HTTP correspondant est dans
|
|||
modes-reseau
|
||||
cycle-de-vie
|
||||
metadata-cloud-init
|
||||
dns-ntp
|
||||
|
|
|
|||
|
|
@ -96,8 +96,8 @@ La MAC anycast ne doit **jamais** sortir du netns : vue depuis le fabric, elle s
|
|||
EVPN par chaque hôte, détectée en double, et déplacerait l'apprentissage des bridges. Trois
|
||||
mécanismes la confinent :
|
||||
|
||||
* nftables, sur le veth du subnet : toute trame de **source** anycast est rejetée à l'entrée
|
||||
(``prerouting``) et à la sortie (``forward`` et ``output``) ;
|
||||
* ebtables, sur le veth du subnet : toute trame de **source** anycast est rejetée à l'entrée
|
||||
(``nat PREROUTING``) et à la sortie (``FORWARD`` et ``OUTPUT``) ;
|
||||
* une **entrée FDB locale** pour la MAC anycast dans le bridge du netns. Le macvlan ne la crée pas
|
||||
lui-même : sans elle, toute trame d'une VM vers sa gateway est inondée vers le fabric et les
|
||||
autres VM, et traitée par la gateway de chaque hôte ;
|
||||
|
|
@ -106,7 +106,7 @@ mécanismes la confinent :
|
|||
|
||||
IPv6 n'est pas désactivé sur le macvlan : ses trames (DAD, ND) sont arrêtées par le confinement.
|
||||
Le watchdog contrôle le macvlan, sa MAC, les deux adresses et l'entrée FDB ; il ne relit pas les
|
||||
règles nftables.
|
||||
règles ebtables.
|
||||
|
||||
Subnets créés avant la gateway anycast
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
|
@ -124,31 +124,19 @@ VPC : le trafic sort par le bridge, et la VM est joignable depuis l'host.
|
|||
|
||||
Le même subnet peut être posé sur plusieurs hôtes raccordés au même segment. Chaque hôte porte
|
||||
alors la **même** ``interface_ip`` dans son netns, avec sa propre MAC. Pour qu'une VM ne voie que
|
||||
l'``interface_ip`` de son hôte (gateway, metadata, DHCP), l'agent pose quatre règles nftables sur
|
||||
l'``interface_ip`` de son hôte (gateway, metadata, DHCP), l'agent pose quatre règles ebtables sur
|
||||
le veth du subnet dans le netns (``v-<subnet>-i``), en sortie vers le bridge de l'host :
|
||||
|
||||
* les requêtes ARP des VM **vers** ``interface_ip`` ne quittent pas l'hôte (``forward``) ;
|
||||
* les requêtes ARP des VM **vers** ``interface_ip`` ne quittent pas l'hôte (``FORWARD``) ;
|
||||
* l'ARP émis **depuis** ``interface_ip`` par le netns, requêtes et réponses, ne sort pas
|
||||
(``output``) : une VM d'un autre hôte n'associe jamais ``interface_ip`` à cet hôte ;
|
||||
* le DHCP émis depuis ``interface_ip`` ne sort ni en transit (``forward``) ni depuis le serveur
|
||||
local lui-même (``output``).
|
||||
(``OUTPUT``) : une VM d'un autre hôte n'associe jamais ``interface_ip`` à cet hôte ;
|
||||
* le DHCP émis depuis ``interface_ip`` ne sort ni en transit (``FORWARD``) ni depuis le serveur
|
||||
local lui-même (``OUTPUT``).
|
||||
|
||||
Le trafic entre VM du subnet, sur un même hôte ou sur des hôtes différents, n'est pas concerné.
|
||||
Par conception, une VM ne joint pas le netns d'un autre hôte. Les règles sont retirées à la
|
||||
suppression du subnet ; une règle déjà absente ne fait pas échouer la suppression. Le watchdog
|
||||
signale les règles manquantes et celles qui ne sont pas de l'agent.
|
||||
|
||||
Règles de pont
|
||||
--------------
|
||||
|
||||
Dans les deux modes, les règles de couche 2 vivent dans une table nftables ``bridge two`` du netns
|
||||
du VPC. Trois chaînes de base (``prerouting``, ``forward``, ``output``) aiguillent chaque trame,
|
||||
selon l'interface d'entrée ou de sortie, vers la chaîne du port concerné (``pre-<port>``,
|
||||
``fwd-<port>``, ``out-<port>``) par les tables ``pre_ports``, ``fwd_ports`` et ``out_ports``.
|
||||
L'agent pose ou retire toutes les règles d'un port en une seule transaction. Les ports sont le
|
||||
veth de chaque subnet (``v-<subnet>-i``, règles décrites plus haut) et le tap de chaque interface
|
||||
de VM (``tap<id>``, chaîne ``pre-tap<id>`` : ce que la VM émet, voir
|
||||
:doc:`/concepts/vpc-subnet-vm`).
|
||||
signale les règles manquantes.
|
||||
|
||||
.. note::
|
||||
|
||||
|
|
|
|||
|
|
@ -18,17 +18,11 @@ VPC
|
|||
Un VPC est un **network namespace** portant un espace d'adressage (``cidr``). C'est l'unité
|
||||
d'isolation : deux VPC ne se voient pas, et peuvent réutiliser les mêmes plages d'adresses.
|
||||
|
||||
À la création, l'agent relie le netns au **bridge public** de l'host par un veth ``vp-<id>-e``
|
||||
(côté host) ↔ ``vp-<id>-i`` (côté netns, rattaché à un ``br-public`` propre au netns). Le bridge
|
||||
public est celui de ``interfaces.internet``, ``br-public`` si la clé est absente, jamais
|
||||
``default_interface`` (:doc:`/exploitation/configuration`). ``deploy.sh -i`` le crée et y
|
||||
accroche ``vxlan-public``, le VXLAN du segment public partagé avec les bordures
|
||||
(:doc:`/demarrage/installation`) ; sans ce bridge, toute création de VPC échoue.
|
||||
|
||||
Dans le netns, l'agent pose aussi la ``dummy`` ``hostsvc`` (``169.254.169.253/32``) et la chaîne
|
||||
iptables ``TWO-HOSTSVC``, puis demande à ``two-hostsvc`` de servir le VPC : c'est le chemin du
|
||||
DNS et du NTP des VM (voir :doc:`dns-ntp`). Si ``two-hostsvc`` ne répond pas, la création du VPC
|
||||
échoue.
|
||||
À la création, l'agent relie le netns au bridge ``br-public`` de l'host par un veth
|
||||
``vp-<id>-e`` (côté host) ↔ ``vp-<id>-i`` (côté netns, rattaché à un ``br-public`` propre au
|
||||
netns). Ce nom de bridge est **en dur** : l'host doit porter un bridge ``br-public``, celui que
|
||||
crée ``deploy.sh -i`` avec la valeur par défaut de ``-P`` (:doc:`/demarrage/installation`) ;
|
||||
sans lui, toute création de VPC échoue.
|
||||
|
||||
Un VPC ne peut être supprimé que si tous ses subnets le sont déjà — sinon 409.
|
||||
|
||||
|
|
@ -41,13 +35,10 @@ plus besoin de porter un subnet pour joindre ses VM. Sans ``l3vni``, le VPC se c
|
|||
avant. Le champ exige la section ``bgp`` de l'agent (:doc:`/exploitation/configuration`) et ne
|
||||
se change pas après création.
|
||||
|
||||
**Plan de VNI.** Segment public, L3VNI et ``vxlan_id`` des subnets partagent l'espace des VNI
|
||||
VXLAN : le VNI **1** est celui du segment public (``deploy.sh --public_vni``, 1 par défaut), la
|
||||
plage **2–65535** (sauf 253–255, tables du noyau) est réservée aux L3VNI des VPC,
|
||||
**65536–16777215** aux subnets. L'agent n'impose que la borne haute des L3VNI (le RD
|
||||
``<VTEP>:<l3vni>`` porte un numéro sur 16 bits) ; le respect du plan revient à l'appelant, qui
|
||||
alloue les L3VNI et les ``vxlan_id``. Un VPC créé avec le VNI du segment public pour ``l3vni``
|
||||
passe en ``error`` : ce VNI est déjà porté par ``vxlan-public``.
|
||||
**Plan de VNI.** L3VNI et ``vxlan_id`` des subnets partagent l'espace des VNI VXLAN : la plage
|
||||
**1–65535** (sauf 253–255, tables du noyau) est réservée aux L3VNI des VPC, **65536–16777215** aux
|
||||
subnets. L'agent n'impose que la première borne (le RD ``<VTEP>:<l3vni>`` porte un numéro sur
|
||||
16 bits) ; le respect du plan revient à l'appelant, qui alloue les deux.
|
||||
|
||||
À la création, l'agent pose sur l'hyperviseur, pour un VPC ``vp-<id>`` :
|
||||
|
||||
|
|
@ -113,30 +104,6 @@ la route par défaut et le serveur de metadata. Tous les subnets d'une VM doiven
|
|||
telle quelle à QEMU et à la réservation DHCP, sans en vérifier le format ni l'unicité : c'est à
|
||||
l'appelant de garantir qu'elle est unique sur le segment L2.
|
||||
|
||||
**Filtrage.** Chaque interface ne peut émettre que depuis sa propre MAC et sa propre IP. L'agent
|
||||
pose sur son tap, avant de le créer, des règles qui rejettent toute trame émise par la VM :
|
||||
|
||||
* dont la MAC source n'est pas celle de l'interface ;
|
||||
* d'un autre type qu'IPv4, ARP ou IPv6 (une trame étiquetée VLAN par exemple) ;
|
||||
* ARP, dont la MAC ou l'adresse d'émetteur n'est pas celle de l'interface, sauf l'adresse
|
||||
``0.0.0.0`` d'une sonde de conflit d'adresse ;
|
||||
* IPv4, dont la source n'est pas l'adresse de l'interface, sauf ``0.0.0.0`` vers le port 67 (un
|
||||
client DHCP qui n'a pas encore de bail) ;
|
||||
* réponse de serveur DHCP (UDP depuis le port 67).
|
||||
|
||||
Une VM ne peut donc ni se faire passer pour la gateway ou une autre VM, ni distribuer de baux. Une
|
||||
VM qui route pour d'autres machines ou porte des adresses supplémentaires ne peut pas émettre
|
||||
depuis ces adresses. L'IPv6 n'est pas filtrée (#61). Les règles sont retirées après la suppression
|
||||
du tap ; le watchdog signale une interface d'une VM ``running`` dont les règles manquent ou ont
|
||||
été modifiées.
|
||||
|
||||
**Serveur DHCP.** ``dhcp_server: true`` sur une interface lève le seul blocage des réponses DHCP,
|
||||
pour cette interface. Cas d'usage : une VM d'un subnet ``bridge`` qui sert le DHCP et le PXE de
|
||||
machines diskless du segment physique, avec des réservations statiques. Les autres contrôles
|
||||
restent en place. L'agent applique ce champ quel que soit le mode du subnet : le réserver au mode
|
||||
``bridge`` revient à l'appelant. Le serveur de la VM reçoit aussi les requêtes des VM du subnet, à
|
||||
côté du DHCP de l'agent : il ne doit répondre qu'aux machines qui lui sont destinées.
|
||||
|
||||
**Routes.** Dans un VPC routé par ``l3vni``, chaque interface sur un subnet ``vxlan`` est annoncée
|
||||
en ``/32`` dans la VRF du VPC une fois QEMU démarré, juste avant le passage en ``running``. À la
|
||||
suppression, ces ``/32`` sont retirés **avant** l'arrêt de QEMU. Comme un subnet ne peut être
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ Prérequis
|
|||
---------
|
||||
|
||||
Un host Linux avec KVM, sur lequel vous avez ``root``. Les opérations réseau (network
|
||||
namespaces, netlink, VXLAN, nftables, iptables) et QEMU ne fonctionnent que sous Linux.
|
||||
namespaces, netlink, VXLAN, ebtables, iptables) et QEMU ne fonctionnent que sous Linux.
|
||||
|
||||
Déploiement
|
||||
-----------
|
||||
|
|
@ -50,12 +50,9 @@ depuis la release, et les vérifie contre le manifeste ``SHA256SUMS``.
|
|||
- bridge principal, auquel l'uplink est rattaché
|
||||
- ``br-000000``
|
||||
* - ``-P <bridge>``
|
||||
- bridge public, sans adresse : il porte ``vxlan-public`` et les veth des VPC ; à
|
||||
reporter dans ``interfaces.internet`` s'il change (voir ci-dessous)
|
||||
- bridge supplémentaire, créé vide et réservé ; garder ``br-public``, nom attendu par
|
||||
l'agent (voir ci-dessous)
|
||||
- ``br-public``
|
||||
* - ``-N <vni>``
|
||||
- VNI du segment public, partagé avec les bordures (``vxlan-public``)
|
||||
- ``1``
|
||||
* - ``-R <secondes>``
|
||||
- délai avant le redémarrage de secours pendant la migration réseau
|
||||
- ``120``
|
||||
|
|
@ -74,7 +71,7 @@ Les options booléennes actives par défaut se **désactivent** par leur forme l
|
|||
Ce que fait ``-i``
|
||||
------------------
|
||||
|
||||
**Paquets** — ``qemu-system-x86``, ``qemu-utils``, ``ovmf``, ``dnsmasq``, ``nftables``,
|
||||
**Paquets** — ``qemu-system-x86``, ``qemu-utils``, ``ovmf``, ``dnsmasq``, ``ebtables``,
|
||||
``iptables``, ``nfs-common``, ``jq``, ``curl``, **sans les paquets recommandés** : ceux de
|
||||
``qemu-system-x86`` (interface graphique, gtk, gstreamer, pilotes de stockage réseau) ne servent
|
||||
pas à un hyperviseur sans écran, et l'installation passe d'environ 270 à 65 Mo — rejouée à chaque
|
||||
|
|
@ -92,19 +89,12 @@ en arrière. Voir :doc:`/exploitation/configuration`.
|
|||
le serveur de metadata : sans elle, iptables ne voit pas le trafic bridgé des VM et cloud-init
|
||||
ne se provisionne pas. Contrepartie assumée : tout le trafic inter-VM traverse les tables NAT.
|
||||
|
||||
**Réseau** — création du bridge public, puis rattachement de l'uplink au bridge principal,
|
||||
**Réseau** — création du bridge réservé, puis rattachement de l'uplink au bridge principal,
|
||||
l'adresse et la route par défaut étant déplacées de l'interface physique vers le bridge.
|
||||
|
||||
**Segment public** — ``vxlan-public`` est créé avec le VNI de ``-N``, l'adresse IPv4 du bridge
|
||||
principal pour VTEP (comme les VXLAN de l'agent), le port 4789, sans apprentissage (les MAC
|
||||
distantes viennent d'EVPN) et un MTU de 1500, puis rattaché au bridge public. FRR l'annonce de
|
||||
lui-même (``advertise-all-vni``). Relancer ``-i`` ne change rien ; le VXLAN est recréé si son VNI
|
||||
ou son VTEP a changé.
|
||||
|
||||
Chaque VPC ``vp-<id>`` raccorde au bridge public un veth ``vp-<id>-e`` côté host, dont l'autre
|
||||
extrémité ``vp-<id>-i`` rejoint un ``br-public`` créé dans le netns du VPC. L'agent lit le nom
|
||||
du bridge public dans ``interfaces.internet`` (``br-public`` par défaut) : avec un autre nom
|
||||
passé à ``-P`` sans le reporter dans cette clé, toute création de VPC échoue.
|
||||
Le nom ``br-public`` est **en dur dans l'agent** : chaque VPC ``vp-<id>`` y raccorde un veth
|
||||
``vp-<id>-e`` côté host, dont l'autre extrémité ``vp-<id>-i`` rejoint un ``br-public`` créé dans
|
||||
le netns du VPC. Avec un autre nom passé à ``-P``, toute création de VPC échoue.
|
||||
|
||||
.. danger::
|
||||
|
||||
|
|
@ -165,12 +155,11 @@ Vérifier l'installation
|
|||
|
||||
.. code-block:: bash
|
||||
|
||||
systemctl status two-hostsvc agent
|
||||
systemctl status agent
|
||||
curl -s http://127.0.0.1:8080/vpcs
|
||||
|
||||
Une liste JSON — vide au premier démarrage — signifie que l'API répond. ``two-hostsvc``, le relais
|
||||
DNS et NTP des VM, doit être actif : sans lui, la création d'un VPC échoue (voir
|
||||
:doc:`/concepts/dns-ntp`). Passez à :doc:`/demarrage/premier-vpc`.
|
||||
Une liste JSON — vide au premier démarrage — signifie que l'API répond. Passez à
|
||||
:doc:`/demarrage/premier-vpc`.
|
||||
|
||||
.. important::
|
||||
|
||||
|
|
|
|||
|
|
@ -30,9 +30,8 @@ encore est décrit sur son ticket :
|
|||
|
||||
* **Internet** (`#31 <https://git.g3e.fr/syonad/two/issues/31>`_,
|
||||
`#42 <https://git.g3e.fr/syonad/two/issues/42>`_). Les bordures partagent avec les hyperviseurs
|
||||
un VXLAN public (VNI 1, ``vxlan-public`` sur ``br-public``, #87) et portent toutes la même IP et
|
||||
la même MAC de gateway. Elles restent sans état : tout NAT vit sur un hyperviseur. Trois façons
|
||||
de sortir :
|
||||
un VXLAN public (VNI 66001) et portent toutes la même IP et la même MAC de gateway. Elles restent
|
||||
sans état : tout NAT vit sur un hyperviseur. Trois façons de sortir :
|
||||
|
||||
* une VM qui porte une IP publique a une NIC sur un subnet public de son VPC, relié au subnet
|
||||
public de l'hôte ;
|
||||
|
|
@ -44,8 +43,7 @@ encore est décrit sur son ticket :
|
|||
l'orchestrateur sont des VM du VNI 66000, partagé entre clusters. Les agents s'y connectent
|
||||
**en sortie**, par leur passerelle ; les routeurs de chaque cluster les traduisent (NAT) vers le
|
||||
VNI 66000.
|
||||
L'orchestrateur a aussi une patte dans le segment public (VNI 1) : son API est joignable
|
||||
depuis Internet.
|
||||
L'orchestrateur a aussi une patte dans le VNI 66001 : son API est joignable depuis Internet.
|
||||
|
||||
* **Route reflectors.** Au moins un ; deux sont prévus dans l'architecture et dans les exemples
|
||||
(plages d'adresses de :doc:`route-reflector`), placés ici dans deux clusters différents pour
|
||||
|
|
@ -174,4 +172,5 @@ Adressage
|
|||
* la plage utilisée pour les adresses d'hyperviseurs, et son rapport avec ``br-000000`` ;
|
||||
* l'allocation des VNI VXLAN : qui la tient, et comment on évite les collisions, puisque
|
||||
l'agent ne valide pas ``vxlan_id`` ;
|
||||
* l'usage prévu de ``br-public``, créé vide et réservé par le bootstrap ;
|
||||
* le plan d'adressage des VPC, et ce qui garantit qu'ils ne se recouvrent pas entre clients.
|
||||
|
|
|
|||
|
|
@ -127,31 +127,12 @@ un subnet créé avec ``mode: bridge`` et ``iface_type: lan`` rattache son veth
|
|||
subnets ``vxlan`` (``iface_type: vms``, clé absente) restent sur ``default_interface`` : le VTEP
|
||||
ne change pas. Le bridge n'est créé qu'au premier démarrage, comme le reste du provisionnement.
|
||||
|
||||
**Bordures (``evpn-3hv``, #87).** ``bd1`` et ``bd2`` (``192.168.14.21`` et ``.22``) jouent les
|
||||
routeurs de bordure du segment public. Ce sont des nœuds ``host`` sans agent : leur provision
|
||||
(``test/e2e/topologies/provision/border.sh``) crée ``br-public``, y accroche ``vxlan-public``
|
||||
(VNI 1, VTEP = adresse de l'underlay, ``nolearning``), et pose sur le bridge la **gateway
|
||||
anycast** ``240.0.0.1/24`` avec la même MAC sur les deux, ``02:01:00:00:00:01`` (la MAC anycast
|
||||
de two pour le VNI 1), ainsi qu'une destination de test ``240.255.0.1/32`` sur une interface
|
||||
``dummy`` ``internet``. Leur ``frr.conf`` est celui d'un hyperviseur — iBGP AS 64600 vers les
|
||||
deux route reflectors, ``advertise-all-vni`` — plus ``advertise-default-gw`` : la MAC et l'IP
|
||||
de la gateway sont annoncées en type 2 avec la communauté *Default Gateway* (RFC 7432, §7.8 et
|
||||
§10.1). Sans elle, la MAC propre du bridge n'est pas annoncée, les hyperviseurs inondent le
|
||||
trafic vers tous les VTEP et les deux bordures répondent ; avec elle, chaque hyperviseur installe
|
||||
la MAC vers une seule bordure et bascule sur l'autre quand elle disparaît (mesuré le 2026-10-25,
|
||||
#87). Les adresses publiques du lab sont prises dans ``240.0.0.0/4``, réservée (RFC 1112,
|
||||
registre RFC 6890) et jamais routée sur Internet. Les bordures sont déclarées après les
|
||||
hyperviseurs : les adresses et les taps de l'underlay ne changent pas, ceux du segment ``lan``
|
||||
sont renumérotés.
|
||||
|
||||
**Tester l'arbre courant plutôt que la release.** ``test/e2e/build.sh`` compile ``agent``,
|
||||
``dhcp``, ``metadata`` et ``two-hostsvc`` pour linux/amd64 dans ``test/e2e/topologies/bin/``
|
||||
(ignoré par git), et y copie l'unit ``two-hostsvc.service``, absente de la release 0.2.0 ; les
|
||||
hyperviseurs reçoivent le répertoire par ``files: {bin: bin}``. Après ``deploy.sh``,
|
||||
``dhcp`` et ``metadata`` pour linux/amd64 dans ``test/e2e/topologies/bin/`` (ignoré par git),
|
||||
que les hyperviseurs reçoivent par ``files: {bin: bin}``. Après ``deploy.sh``,
|
||||
``hypervisor.sh`` installe chaque binaire présent dans ``/opt/two/local/bin/`` et y fait pointer
|
||||
``/opt/two/bin/<nom>``, puis installe, active et redémarre chaque unit ``*.service`` présente ; le
|
||||
reste (units, scripts) vient de la release. ``build.sh --clean`` vide le répertoire : le lab
|
||||
suivant teste la release seule.
|
||||
``/opt/two/bin/<nom>`` ; le reste (units, scripts) vient de la release. ``build.sh --clean`` vide
|
||||
le répertoire : le lab suivant teste la release seule.
|
||||
|
||||
.. code-block:: text
|
||||
|
||||
|
|
@ -274,9 +255,9 @@ code de sortie vaut 1 si une vérification échoue **ou si aucune n'a été fait
|
|||
**Tests autonomes.** Avant chaque test, le runner supprime les ressources ``tNN`` laissées par un
|
||||
passage précédent, puis relève l'état de chaque hôte de l'en-tête ``# hôtes:``. Après le test, il
|
||||
supprime ses VM, ses subnets puis ses VPC, et vérifie que l'hôte est revenu à l'état relevé
|
||||
(``assert_clean`` : netns, liens, adresses, routes, règles nftables de pont et iptables,
|
||||
configuration FRR et ``frr.conf``, fichiers de ``/run/two``, ``dnsmasq.d`` et des volumes, unités
|
||||
``two-vm-*``, processus dnsmasq et QEMU). Aucun test ne dépend d'un autre : n'importe lequel se lance seul.
|
||||
(``assert_clean`` : netns, liens, adresses, routes, règles ebtables et iptables, configuration FRR et
|
||||
``frr.conf``, fichiers de ``/run/two``, ``dnsmasq.d`` et des volumes, unités ``two-vm-*``,
|
||||
processus dnsmasq et QEMU). Aucun test ne dépend d'un autre : n'importe lequel se lance seul.
|
||||
|
||||
**Plages.** Le test ``NN`` nomme ses ressources ``vp-tNN*``, ``sn-tNN*`` et ``tNN-*``, et utilise
|
||||
``10.NN.0.0/16`` et les VNI ``NN00`` à ``NN99`` : aucune collision entre tests.
|
||||
|
|
@ -305,7 +286,7 @@ Générée par ``test/e2e/run.sh --matrice`` à partir des en-têtes ; à régé
|
|||
- hv1 hv2 hv3
|
||||
- #50, #51, #54, #41
|
||||
* - ``02-prerequis``
|
||||
- hypothèses d'environnement de la suite — firmware UEFI, unités modèles de l'agent, journalctl, format de nft list table, br-lan, priorités VRRP lues dans FRR
|
||||
- hypothèses d'environnement de la suite — firmware UEFI, unités modèles de l'agent, journalctl, format de ebtables -L, br-lan, priorités VRRP lues dans FRR
|
||||
- hv1 hv2 hv3
|
||||
- #70
|
||||
* - ``03-image``
|
||||
|
|
@ -352,10 +333,6 @@ Générée par ``test/e2e/run.sh --matrice`` à partir des en-têtes ; à régé
|
|||
- MTU des VM (network.mtu_vm, 1500 par défaut) — annoncé par le DHCP (option 26, serveur intégré sur hv1, dnsmasq sur hv2) et par QEMU (host_mtu), porté par le tap, le bridge, le veth et le VXLAN du subnet ; subnet vxlan refusé sur un underlay qui n'est pas à 9000
|
||||
- hv1 hv2
|
||||
- #56
|
||||
* - ``29-dns-ntp``
|
||||
- DNS et NTP des VM par two-hostsvc — options DHCP 6 et 42 = interface_ip, redirection vers 169.254.169.253 dans le VPC (vxlan et bridge), seuls 53 et 123 servis, alertes du watchdog, VPC en error si two-hostsvc ne répond pas
|
||||
- hv1
|
||||
- #86
|
||||
* - ``31-gateway``
|
||||
- gateway d'un subnet vxlan — gateway anycast et adresse d'émission calculées, default_route et gateway explicite, routes DHCP, macvlan gw-, FDB locale, sysctls ARP, règles de confinement, suppression et recréation
|
||||
- hv1
|
||||
|
|
@ -365,17 +342,17 @@ Générée par ``test/e2e/run.sh --matrice`` à partir des en-têtes ; à régé
|
|||
- hv1
|
||||
- #57, #41
|
||||
* - ``33-bridge``
|
||||
- subnet en mode bridge rattaché au bridge br-lan de l'hôte — un hôte, puis le même subnet sur deux hôtes (interface_ip confinée à son hôte : une seule réponse ARP, métadonnées servies par l'hôte de la VM, ni ARP ni DHCP de la gateway sur le segment, règles surveillées par le watchdog) ; VM serveur DHCP des machines diskless (dhcp_server) entendue sur le segment, voisine bloquée
|
||||
- subnet en mode bridge rattaché au bridge br-lan de l'hôte — un hôte, puis le même subnet sur deux hôtes (interface_ip confinée à son hôte : une seule réponse ARP, métadonnées servies par l'hôte de la VM, ni ARP ni DHCP de la gateway sur le segment, règles surveillées par le watchdog)
|
||||
- hv1 hv2
|
||||
- #70, #75, #76
|
||||
- #70, #75
|
||||
* - ``34-isolation-locale``
|
||||
- isolation entre deux VPC sur un même hôte — ni ICMP ni TCP d'un VPC à l'autre
|
||||
- hv1
|
||||
- #50
|
||||
* - ``35-filtrage-port``
|
||||
- filtrage au port — en sortie vers le fabric (v-<id>-i) : ARP vers la gateway et l'adresse d'émission, DHCP émis depuis ces adresses, MAC anycast confinée dans les deux sens ; à la sortie de chaque VM (pre-tap<id>) : MAC, IP et ARP usurpés, faux serveur DHCP, source 0.0.0.0 hors DHCP, type de trame non IP, NIC autorisée en serveur DHCP (dhcp_server), règles surveillées par le watchdog et retirées avec la VM
|
||||
- filtrage au port — en sortie vers le fabric (v-<id>-i) : ARP vers la gateway et l'adresse d'émission, DHCP émis depuis ces adresses, MAC anycast confinée dans les deux sens ; entre VM d'un même hôte : faux serveur DHCP et usurpation ARP de la gateway (comportement attendu, non filtré à ce jour)
|
||||
- hv1
|
||||
- #57, #76
|
||||
- #57
|
||||
* - ``41-evpn-l2``
|
||||
- subnet vxlan étendu entre deux hôtes par EVPN — VTEP local, VTEP distant et inondation appris, MAC des VM en type 2, trafic ICMP et TCP, MTU 1500 de bout en bout
|
||||
- hv1 hv2
|
||||
|
|
@ -392,10 +369,6 @@ Générée par ``test/e2e/run.sh --matrice`` à partir des en-têtes ; à régé
|
|||
- fenêtre ARP de la gateway anycast entre deux hôtes — relais L2 depuis l'adresse d'émission d'un hôte, gateway de la VM inchangée, métadonnées, renouvellement DHCP et TCP local sans perte pendant la fenêtre
|
||||
- hv1 hv2
|
||||
- #57
|
||||
* - ``45-segment-public``
|
||||
- segment public de l'hôte — vxlan-public (VNI 1) posé par le bootstrap sur br-public, VNI annoncé en EVPN et vu par les bordures, gateway anycast des bordures installée vers une seule bordure, gateway et destination de test joignables sans doublon, L2 entre hyperviseurs, veth des VPC sur le bridge public, watchdog
|
||||
- hv1 hv2 hv3
|
||||
- #87
|
||||
* - ``51-l3vni``
|
||||
- routage inter-subnet entre hôtes par L3VNI (IRB symétrique) — VRF, RMAC, exports /24 et /32, RT-5 réfléchies, TTL, isolation par RT, unreachable default dans la VRF, démontage d'un VPC routé
|
||||
- hv1 hv2
|
||||
|
|
@ -450,6 +423,7 @@ Hypothèses vérifiées en lab, à garder en tête en écrivant un test :
|
|||
* systemd-networkd **ne réécrit pas** un bail renouvelé à l'identique : un renouvellement se
|
||||
vérifie par le DHCPACK dans le journal du serveur (``lease_renewed``), pas par le fichier de bail ;
|
||||
* l'agent n'accepte **qu'un seul disque virtio** (le disque racine, ``vda``) ; les autres sont des ``xvdX`` ;
|
||||
* sur Debian 12, ``ebtables -L`` (nft) garde les zéros de tête des MAC ;
|
||||
* ``interface_ip`` est posée en ``/32`` sur le bridge du netns en mode ``bridge`` ;
|
||||
* depuis le netns d'un hôte, une VM d'un autre hôte ne répond pas : la réponse repart vers la
|
||||
gateway de son propre hôte. Une vérification entre hôtes passe par une VM.
|
||||
|
|
@ -473,7 +447,7 @@ Chaque bloc est précédé des bibliothèques de ``test/e2e/lib/`` :
|
|||
``user_data``, mot de passe), SSH, bail, métadonnées. La MAC de chaque NIC est fournie à l'API :
|
||||
``52:54:00`` suivi des trois derniers octets de l'IP (``mac_for_ip``), unique dans la suite
|
||||
puisque chaque test a sa plage ; ``--nic sn=ip=mac`` en impose une autre ;
|
||||
* ``net.sh`` : VPC, subnets, MAC anycast, trames brutes, règles nftables, VRRP ;
|
||||
* ``net.sh`` : VPC, subnets, MAC anycast, trames brutes, règles ebtables, VRRP ;
|
||||
* ``evpn.sh`` : VRF, BGP, L3VNI, watchdog ;
|
||||
* ``fixture.sh`` : nettoyage et ``assert_clean``, appelés par le runner. Le nettoyage couvre les
|
||||
ressources ``t<NN>`` de l'API, les netns ``t<NN>-*`` et les fichiers ``t<NN>-*`` / ``t<NN>.*`` de
|
||||
|
|
|
|||
|
|
@ -125,18 +125,13 @@ Correspondance des interfaces
|
|||
|
||||
interfaces:
|
||||
vms: br-000000
|
||||
internet: br-public
|
||||
internet: br-000000
|
||||
admin: br-000000
|
||||
|
||||
Traduit les clés logiques ``iface_type`` de l'API vers les bridges physiques de l'host. Une clé
|
||||
inconnue ou omise retombe silencieusement sur ``default_interface`` — ce n'est pas une erreur,
|
||||
mais c'est une source de subnets branchés au mauvais endroit sans le dire.
|
||||
|
||||
``internet`` désigne aussi le **bridge public**, celui qui porte ``vxlan-public`` et où chaque
|
||||
VPC raccorde son veth ``vp-<id>-e`` (:doc:`/concepts/vpc-subnet-vm`). Pour lui, une clé absente
|
||||
ou vide vaut ``br-public``, **jamais** ``default_interface`` : le segment public ne doit pas
|
||||
tomber sur l'underlay. À garder égal au ``-P`` de ``deploy.sh``.
|
||||
|
||||
MTU
|
||||
---
|
||||
|
||||
|
|
@ -282,31 +277,6 @@ La bascule est **manuelle** et suppose un hyperviseur vide :
|
|||
Le retour arrière suit la même procédure. Il n'y a pas de bascule à chaud, dans un sens ni dans
|
||||
l'autre.
|
||||
|
||||
Relais DNS et NTP
|
||||
-----------------
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
hostsvc:
|
||||
dns_upstream: []
|
||||
ntp_upstream: []
|
||||
rate_limit_per_vpc: 100
|
||||
|
||||
Lue par ``two-hostsvc`` (voir :doc:`../concepts/dns-ntp`), au démarrage du service.
|
||||
|
||||
``dns_upstream``
|
||||
résolveurs interrogés en premier, sous la forme ``ip`` ou ``ip:port`` (port 53 par défaut).
|
||||
Ceux de ``/etc/resolv.conf`` de l'hôte viennent ensuite ; liste vide : l'hôte seul.
|
||||
``ntp_upstream``
|
||||
serveurs NTP relayés en premier, ``ip`` ou ``ip:port`` (port 123 par défaut). Sans serveur ou
|
||||
sans réponse, l'horloge de l'hôte répond.
|
||||
``rate_limit_per_vpc``
|
||||
requêtes par seconde, DNS et NTP confondus, pour chaque VPC.
|
||||
|
||||
Un nom d'hôte, un port hors de 1–65535 ou une limite nulle ou négative font échouer le
|
||||
démarrage de l'agent comme de ``two-hostsvc``, qui lisent le même fichier. Une modification prend
|
||||
effet au redémarrage de ``two-hostsvc``.
|
||||
|
||||
Watchdog
|
||||
--------
|
||||
|
||||
|
|
|
|||
|
|
@ -85,8 +85,9 @@ entry``…) disent déjà ce qui manque. À la main, dans le netns du VPC :
|
|||
# route du subnet émise depuis la gateway
|
||||
ip -n <vpc> -4 route show dev br-<subnet>
|
||||
|
||||
# confinement : chaînes pre-, fwd- et out-v-<subnet>-i, source anycast rejetée
|
||||
ip netns exec <vpc> nft list table bridge two
|
||||
# confinement : source anycast rejetée sur le veth du subnet
|
||||
ip netns exec <vpc> ebtables -t nat -L PREROUTING
|
||||
ip netns exec <vpc> ebtables -L FORWARD
|
||||
|
||||
Dans le guest, ``ip neigh show <gateway>`` doit montrer la MAC ``02:01:00:<VNI>``. Une autre MAC —
|
||||
celle d'un hôte, ``02:03:…`` — signale un subnet de l'ancien modèle (sans ``relay_ip``) ou une
|
||||
|
|
@ -96,25 +97,6 @@ Depuis le netns d'un hôte, une VM d'un **autre** hôte ne répond qu'à l'adres
|
|||
``ip netns exec <vpc> ping -I <relay_ip> <ip>``. Un ping sans ``-I`` part de la gateway et la
|
||||
réponse revient à l'hôte de la VM — ce n'est pas une panne.
|
||||
|
||||
Une VM n'arrive pas à émettre certaines trames
|
||||
----------------------------------------------
|
||||
|
||||
Le filtrage du tap rejette ce qu'une interface émet depuis une autre MAC ou une autre IP que les
|
||||
siennes, l'ARP usurpé, les réponses DHCP et les trames d'un autre type qu'IPv4, ARP ou IPv6
|
||||
(:doc:`/concepts/vpc-subnet-vm`). Causes rencontrées : une VM qui route pour d'autres machines,
|
||||
porte une adresse supplémentaire, envoie des trames étiquetées VLAN, ou sert le DHCP sans
|
||||
``dhcp_server: true`` sur son interface. Ce n'est pas une panne : le filtrage est voulu.
|
||||
|
||||
Retrouver la chaîne d'une interface par sa MAC, puis lire ses règles :
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
ip netns exec <vpc> nft list table bridge two | grep -B1 'ether saddr != <mac> drop'
|
||||
ip netns exec <vpc> nft list chain bridge two pre-tap<id>
|
||||
|
||||
Une chaîne absente, ou une interface absente de ``pre_ports``, laisse la VM émettre sans filtre :
|
||||
le watchdog le signale (``interface <i>: nftables rule missing``). Recréer la VM repose ses règles.
|
||||
|
||||
La VM a une adresse mais cloud-init n'applique rien
|
||||
---------------------------------------------------
|
||||
|
||||
|
|
@ -163,41 +145,6 @@ réellement écrit :
|
|||
|
||||
Un base64 invalide, lui, aurait été rejeté en 400 à la création.
|
||||
|
||||
La VM ne résout pas les noms ou n'a pas l'heure
|
||||
-----------------------------------------------
|
||||
|
||||
La VM interroge sa gateway (``interface_ip``), que le DHCP annonce en DNS et en NTP ; la requête
|
||||
est redirigée vers ``two-hostsvc`` dans le netns du VPC (voir :doc:`../concepts/dns-ntp`).
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
# Dans la VM : DNS et NTP annoncés par le bail (systemd-networkd)
|
||||
sudo cat /run/systemd/netif/leases/$(cat /sys/class/net/ens3/ifindex) | grep -E '^(DNS|NTP)='
|
||||
|
||||
# Sur l'hyperviseur
|
||||
systemctl status two-hostsvc
|
||||
cat /run/two/hostsvc/hostsvc.state # le VPC doit y figurer
|
||||
ip -n <vpc> -4 addr show dev hostsvc # 169.254.169.253/32
|
||||
ip netns exec <vpc> iptables -t nat -S TWO-HOSTSVC # 2 RETURN puis 3 DNAT
|
||||
ip netns exec <vpc> iptables -t nat -L TWO-HOSTSVC -v -n # compteurs des redirections
|
||||
ip netns exec <vpc> ss -lntu | grep 169.254.169.253 # 53/udp, 53/tcp, 123/udp
|
||||
journalctl -u two-hostsvc -n 50
|
||||
|
||||
Causes rencontrées :
|
||||
|
||||
* ``two-hostsvc`` arrêté : plus de DNS ni de NTP pour aucun VPC de l'hyperviseur, et un VPC créé
|
||||
pendant ce temps part en ``error`` ;
|
||||
* adresse ``169.254.169.253`` ou règle de la chaîne retirée à la main : la requête n'arrive plus
|
||||
au relais ; le watchdog le signale ;
|
||||
* VPC créé par une version antérieure à la 0.3.0 : ni ``dummy`` ni chaîne ni socket. Le recréer ;
|
||||
* ``open`` refusé par ``two-hostsvc`` avec ``permission denied`` ou ``invalid argument`` sur
|
||||
``/var/run/netns/<vpc>`` : l'unit n'a pas ``CAP_DAC_READ_SEARCH``, ou le netns, créé après le
|
||||
démarrage du service, ne lui est pas visible parce que ``/`` n'est pas en propagation
|
||||
``shared`` (``findmnt -no PROPAGATION /``). Rencontré en conteneur, pas sur un hyperviseur.
|
||||
|
||||
Sans serveur ``ntp_upstream`` qui réponde, l'heure vient de l'horloge de l'hyperviseur : si
|
||||
celle-ci n'est pas synchronisée, la réponse porte l'indicateur d'alarme et la VM l'ignore.
|
||||
|
||||
La VM ne démarre pas (UEFI)
|
||||
---------------------------
|
||||
|
||||
|
|
|
|||
|
|
@ -37,20 +37,7 @@ défaut** (``enabled: false``) ; l'activer explicitement :
|
|||
interval_seconds: 60
|
||||
|
||||
Il contrôle notamment l'existence des network namespaces, des liens réseau des subnets, des taps
|
||||
de VM et la réponse des units systemd associées, ainsi que le **segment public** : le bridge
|
||||
public (``interfaces.internet``) doit exister, être actif et porter un port VXLAN
|
||||
(``vxlan-public``), sinon les VPC de l'hôte n'ont pas de segment public. Il ne vérifie pas le
|
||||
numéro du VNI.
|
||||
|
||||
Dès qu'un VPC est ``running`` sur l'hyperviseur, il contrôle aussi le relais DNS et NTP (voir
|
||||
:doc:`../concepts/dns-ntp`) :
|
||||
|
||||
* ``two-hostsvc`` injoignable sur sa socket de contrôle : une notification, aucun VPC de
|
||||
l'hyperviseur n'a plus de DNS ni de NTP ;
|
||||
* pour chaque VPC ``running`` : VPC absent de la liste servie par ``two-hostsvc``,
|
||||
``169.254.169.253`` absente de la ``dummy`` ``hostsvc``, ou chaîne ``TWO-HOSTSVC`` absente ou
|
||||
incomplète dans son netns. Un VPC créé par une version antérieure à la 0.3.0 n'a rien de tout
|
||||
cela : il est signalé à chaque tick jusqu'à sa recréation.
|
||||
de VM et la réponse des units systemd associées.
|
||||
|
||||
Avec une section ``bgp`` dans la configuration, il contrôle aussi le plan de contrôle :
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
Services systemd
|
||||
================
|
||||
|
||||
Cinq units, installées dans ``/etc/systemd/system`` par ``deploy.sh`` ; les binaires et scripts
|
||||
Quatre units, installées dans ``/etc/systemd/system`` par ``deploy.sh`` ; les binaires et scripts
|
||||
qu'elles lancent sont sous ``/opt/two/bin``. Les deux units DHCP
|
||||
s'excluent : celle qui tourne dépend de ``dhcp.backend`` (voir :doc:`configuration`).
|
||||
|
||||
|
|
@ -24,10 +24,6 @@ s'excluent : celle qui tourne dépend de ``dhcp.backend`` (voir :doc:`configurat
|
|||
* - ``metadata@.service``
|
||||
- ``<nom de la VM>``
|
||||
- serveur de metadata cloud-init, un par VM
|
||||
* - ``two-hostsvc.service``
|
||||
- —
|
||||
- relais DNS et NTP des VM, un par hyperviseur, démarré avant l'agent (voir
|
||||
:doc:`../concepts/dns-ntp`)
|
||||
|
||||
Les instances sont créées et pilotées par l'agent au fil des créations de subnets et de VM : il
|
||||
n'y a pas à les démarrer à la main en fonctionnement normal.
|
||||
|
|
@ -38,7 +34,6 @@ n'y a pas à les démarrer à la main en fonctionnement normal.
|
|||
systemctl status 'dnsmasq@vp-admin_br-000001' # backend dnsmasq
|
||||
systemctl status 'dhcp@vp-admin_br-000001' # backend two
|
||||
systemctl status 'metadata@i-web'
|
||||
systemctl status two-hostsvc
|
||||
|
||||
dnsmasq
|
||||
-------
|
||||
|
|
@ -101,27 +96,6 @@ processus serait mort sans nettoyer (``SIGKILL``, plantage).
|
|||
|
||||
Diagnostic : voir :doc:`diagnostic`, qui montre comment interroger la socket.
|
||||
|
||||
Relais DNS et NTP
|
||||
-----------------
|
||||
|
||||
``two-hostsvc.service`` lance ``/opt/two/bin/two-hostsvc``, un seul processus par hyperviseur,
|
||||
qui sert DNS et NTP aux VM de tous les VPC (voir :doc:`../concepts/dns-ntp`). Il lit la section
|
||||
``hostsvc`` de ``/etc/two/agent.yml`` (voir :doc:`configuration`).
|
||||
|
||||
.. list-table::
|
||||
:widths: 40 60
|
||||
|
||||
* - Socket de contrôle
|
||||
- ``/run/two/hostsvc/hostsvc.sock``
|
||||
* - État
|
||||
- ``/run/two/hostsvc/hostsvc.state`` (VPC servis)
|
||||
* - Journal
|
||||
- ``journalctl -u two-hostsvc``
|
||||
|
||||
L'unit démarre avant ``agent.service`` et se relance en cas d'échec. Son répertoire
|
||||
``/run/two/hostsvc`` survit à un redémarrage du service : le processus y relit la liste des VPC
|
||||
servis et rouvre leurs sockets. Un VPC ne peut pas être créé tant que le service ne répond pas.
|
||||
|
||||
QEMU n'est pas une unit
|
||||
-----------------------
|
||||
|
||||
|
|
|
|||
|
|
@ -40,11 +40,10 @@ type Subnet struct {
|
|||
}
|
||||
|
||||
type VMInterface struct {
|
||||
Subnet string `json:"subnet"`
|
||||
IP string `json:"ip"`
|
||||
MAC string `json:"mac"`
|
||||
Primary bool `json:"primary"`
|
||||
DHCPServer bool `json:"dhcp_server"`
|
||||
Subnet string `json:"subnet"`
|
||||
IP string `json:"ip"`
|
||||
MAC string `json:"mac"`
|
||||
Primary bool `json:"primary"`
|
||||
}
|
||||
|
||||
type VMStorage struct {
|
||||
|
|
|
|||
|
|
@ -104,8 +104,6 @@ func interfacesFromDB(prefix string, entries map[string]string) []VMInterface {
|
|||
byIndex[idx].MAC = value
|
||||
case "primary":
|
||||
byIndex[idx].Primary = value == "true"
|
||||
case "dhcp_server":
|
||||
byIndex[idx].DHCPServer = value == "true"
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -360,62 +360,6 @@ func TestStartVM_StoresAllInterfaces(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestStartVM_StoresDHCPServerOnlyWhenRequested(t *testing.T) {
|
||||
s, db := newTestServer(t)
|
||||
for sn, cidr := range map[string]string{"sn-1": "10.1.0.0/24", "sn-2": "10.2.0.0/24"} {
|
||||
kv.AddInDB(db, "subnet/"+sn+"/state", "running")
|
||||
kv.AddInDB(db, "subnet/"+sn+"/vpc", "vpc-1")
|
||||
kv.AddInDB(db, "subnet/"+sn+"/cidr", cidr)
|
||||
}
|
||||
|
||||
body, _ := json.Marshal(VMCreateRequest{
|
||||
Name: "vm-pxe",
|
||||
Interfaces: []VMInterface{
|
||||
{Subnet: "sn-1", IP: "10.1.0.5", MAC: "52:54:00:00:00:05", Primary: true},
|
||||
{Subnet: "sn-2", IP: "10.2.0.5", MAC: "52:54:00:aa:bb:cc", DHCPServer: true},
|
||||
},
|
||||
Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}},
|
||||
})
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
s.VmsHandler(w, httptest.NewRequest(http.MethodPost, "/vms", bytes.NewReader(body)))
|
||||
if w.Code != http.StatusAccepted {
|
||||
t.Fatalf("attendu 202, obtenu %d : %s", w.Code, w.Body.String())
|
||||
}
|
||||
if got, _ := kv.GetFromDB(db, "vm/vm-pxe/nic/1/dhcp_server"); got != "true" {
|
||||
t.Errorf("dhcp_server de la seconde interface non stocké : %q", got)
|
||||
}
|
||||
if _, err := kv.GetFromDB(db, "vm/vm-pxe/nic/0/dhcp_server"); err == nil {
|
||||
t.Error("une interface sans dhcp_server ne doit pas porter la clé")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVmFromDB_ReturnsDHCPServer(t *testing.T) {
|
||||
vm, err := vmFromDB("vm-pxe", map[string]string{
|
||||
"vm/vm-pxe/state": "running",
|
||||
"vm/vm-pxe/nic/0/subnet": "sn-1",
|
||||
"vm/vm-pxe/nic/0/ip": "10.1.0.5",
|
||||
"vm/vm-pxe/nic/0/primary": "true",
|
||||
"vm/vm-pxe/nic/1/subnet": "sn-2",
|
||||
"vm/vm-pxe/nic/1/ip": "10.2.0.5",
|
||||
"vm/vm-pxe/nic/1/dhcp_server": "true",
|
||||
"vm/vm-pxe/disk/vda": "/data/root.qcow2",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("vmFromDB : %v", err)
|
||||
}
|
||||
if len(vm.Interfaces) != 2 {
|
||||
t.Fatalf("2 interfaces attendues : %+v", vm.Interfaces)
|
||||
}
|
||||
if vm.Interfaces[0].DHCPServer || !vm.Interfaces[1].DHCPServer {
|
||||
t.Errorf("dhcp_server attendu sur l'index 1 seulement : %+v", vm.Interfaces)
|
||||
}
|
||||
raw, _ := json.Marshal(vm.Interfaces[0])
|
||||
if !strings.Contains(string(raw), `"dhcp_server":false`) {
|
||||
t.Errorf("dhcp_server faux absent du JSON : %s", raw)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartVM_RejectsZeroOrTwoPrimaries(t *testing.T) {
|
||||
cases := map[string][]VMInterface{
|
||||
"aucune primaire": {{Subnet: "sn-1", IP: "10.1.0.5", MAC: "52:54:00:00:00:05"}},
|
||||
|
|
|
|||
|
|
@ -78,7 +78,7 @@ func (s *Server) startVM(w http.ResponseWriter, r *http.Request) {
|
|||
json.NewEncoder(w).Encode(ErrorResponse{Error: fmt.Sprintf("interface %d: mac is required", i)})
|
||||
return
|
||||
}
|
||||
nics[i] = dispatcher.VMNIC{Subnet: iface.Subnet, IP: iface.IP, MAC: iface.MAC, Primary: iface.Primary, DHCPServer: iface.DHCPServer}
|
||||
nics[i] = dispatcher.VMNIC{Subnet: iface.Subnet, IP: iface.IP, MAC: iface.MAC, Primary: iface.Primary}
|
||||
if iface.Primary {
|
||||
primaries++
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,31 +0,0 @@
|
|||
package hostsvcapi
|
||||
|
||||
import "path/filepath"
|
||||
|
||||
type Verb string
|
||||
|
||||
const (
|
||||
VerbOpen Verb = "open"
|
||||
VerbClose Verb = "close"
|
||||
VerbList Verb = "list"
|
||||
)
|
||||
|
||||
const (
|
||||
MaxMessageBytes = 64 * 1024
|
||||
DefaultRunDir = "/run/two/hostsvc"
|
||||
Unit = "two-hostsvc.service"
|
||||
)
|
||||
|
||||
func SocketPath(runDir string) string { return filepath.Join(runDir, "hostsvc.sock") }
|
||||
func StatePath(runDir string) string { return filepath.Join(runDir, "hostsvc.state") }
|
||||
|
||||
type Request struct {
|
||||
Verb Verb `json:"verb"`
|
||||
VPC string `json:"vpc,omitempty"`
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
OK bool `json:"ok"`
|
||||
Error string `json:"error,omitempty"`
|
||||
VPCs []string `json:"vpcs,omitempty"`
|
||||
}
|
||||
|
|
@ -1,128 +0,0 @@
|
|||
package hostsvcapi
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"git.g3e.fr/syonad/two/pkg/db/statefile"
|
||||
)
|
||||
|
||||
const SocketMode = 0o600
|
||||
|
||||
type Service interface {
|
||||
Open(vpc string) error
|
||||
Close(vpc string) error
|
||||
List() []string
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
svc Service
|
||||
listener net.Listener
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func Listen(svc Service, path string, logger *slog.Logger) (*Server, error) {
|
||||
dir := filepath.Dir(path)
|
||||
if err := os.MkdirAll(dir, statefile.DirMode); err != nil {
|
||||
return nil, fmt.Errorf("create %s: %w", dir, err)
|
||||
}
|
||||
if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return nil, fmt.Errorf("remove stale socket %s: %w", path, err)
|
||||
}
|
||||
|
||||
listener, err := net.Listen("unix", path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("listen on %s: %w", path, err)
|
||||
}
|
||||
if err := os.Chmod(path, SocketMode); err != nil {
|
||||
listener.Close()
|
||||
return nil, fmt.Errorf("chmod %s: %w", path, err)
|
||||
}
|
||||
|
||||
return &Server{svc: svc, listener: listener, logger: logger}, nil
|
||||
}
|
||||
|
||||
func (s *Server) Addr() string {
|
||||
return s.listener.Addr().String()
|
||||
}
|
||||
|
||||
func (s *Server) Close() error {
|
||||
return s.listener.Close()
|
||||
}
|
||||
|
||||
func (s *Server) Serve() error {
|
||||
for {
|
||||
conn, err := s.listener.Accept()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
go s.handleConn(conn)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) handleConn(conn net.Conn) {
|
||||
defer conn.Close()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
s.logger.Error("control connection panicked", "panic", r)
|
||||
}
|
||||
}()
|
||||
|
||||
scanner := bufio.NewScanner(conn)
|
||||
scanner.Buffer(make([]byte, 0, 4096), MaxMessageBytes)
|
||||
encoder := json.NewEncoder(conn)
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Bytes()
|
||||
if len(line) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
var req Request
|
||||
if err := json.Unmarshal(line, &req); err != nil {
|
||||
if err := encoder.Encode(failure(fmt.Errorf("malformed request: %w", err))); err != nil {
|
||||
return
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if err := encoder.Encode(s.dispatch(req)); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
s.logger.Error("control connection read failed", "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func failure(err error) Response {
|
||||
return Response{OK: false, Error: err.Error()}
|
||||
}
|
||||
|
||||
func (s *Server) dispatch(req Request) Response {
|
||||
switch req.Verb {
|
||||
case VerbOpen:
|
||||
if err := s.svc.Open(req.VPC); err != nil {
|
||||
return failure(err)
|
||||
}
|
||||
return Response{OK: true}
|
||||
|
||||
case VerbClose:
|
||||
if err := s.svc.Close(req.VPC); err != nil {
|
||||
return failure(err)
|
||||
}
|
||||
return Response{OK: true}
|
||||
|
||||
case VerbList:
|
||||
return Response{OK: true, VPCs: s.svc.List()}
|
||||
|
||||
default:
|
||||
return failure(fmt.Errorf("unknown verb %q", req.Verb))
|
||||
}
|
||||
}
|
||||
|
|
@ -248,8 +248,8 @@ func TestProbe_DescribesWhatWouldBeSent(t *testing.T) {
|
|||
if lease.Router != "10.0.5.254" {
|
||||
t.Errorf("router = %s, want 10.0.5.254", lease.Router)
|
||||
}
|
||||
if len(lease.DNS) != 1 || lease.DNS[0] != "10.0.5.1" {
|
||||
t.Errorf("dns = %v, want only the interface ip 10.0.5.1", lease.DNS)
|
||||
if len(lease.DNS) != 2 {
|
||||
t.Errorf("dns = %v, want two servers", lease.DNS)
|
||||
}
|
||||
if len(lease.Routes) != 3 {
|
||||
t.Errorf("routes = %v, want metadata, vpc and default", lease.Routes)
|
||||
|
|
|
|||
|
|
@ -1,82 +0,0 @@
|
|||
package hostsvcclient
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
hostsvcapi "git.g3e.fr/syonad/two/internal/api/hostsvc"
|
||||
)
|
||||
|
||||
const DefaultTimeout = 5 * time.Second
|
||||
|
||||
type Client struct {
|
||||
path string
|
||||
timeout time.Duration
|
||||
}
|
||||
|
||||
func New(path string) *Client {
|
||||
return &Client{path: path, timeout: DefaultTimeout}
|
||||
}
|
||||
|
||||
func (c *Client) WithTimeout(d time.Duration) *Client {
|
||||
return &Client{path: c.path, timeout: d}
|
||||
}
|
||||
|
||||
func (c *Client) call(req hostsvcapi.Request) (hostsvcapi.Response, error) {
|
||||
conn, err := net.DialTimeout("unix", c.path, c.timeout)
|
||||
if err != nil {
|
||||
return hostsvcapi.Response{}, fmt.Errorf("dial %s: %w", c.path, err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
if err := conn.SetDeadline(time.Now().Add(c.timeout)); err != nil {
|
||||
return hostsvcapi.Response{}, fmt.Errorf("set deadline on %s: %w", c.path, err)
|
||||
}
|
||||
|
||||
raw, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
return hostsvcapi.Response{}, fmt.Errorf("encode %s: %w", req.Verb, err)
|
||||
}
|
||||
if _, err := conn.Write(append(raw, '\n')); err != nil {
|
||||
return hostsvcapi.Response{}, fmt.Errorf("send %s: %w", req.Verb, err)
|
||||
}
|
||||
|
||||
scanner := bufio.NewScanner(conn)
|
||||
scanner.Buffer(make([]byte, 0, 4096), hostsvcapi.MaxMessageBytes)
|
||||
if !scanner.Scan() {
|
||||
if err := scanner.Err(); err != nil {
|
||||
return hostsvcapi.Response{}, fmt.Errorf("read reply to %s: %w", req.Verb, err)
|
||||
}
|
||||
return hostsvcapi.Response{}, fmt.Errorf("no reply to %s", req.Verb)
|
||||
}
|
||||
|
||||
var resp hostsvcapi.Response
|
||||
if err := json.Unmarshal(scanner.Bytes(), &resp); err != nil {
|
||||
return hostsvcapi.Response{}, fmt.Errorf("parse reply to %s: %w", req.Verb, err)
|
||||
}
|
||||
if !resp.OK {
|
||||
return resp, fmt.Errorf("%s refused: %s", req.Verb, resp.Error)
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *Client) Open(vpc string) error {
|
||||
_, err := c.call(hostsvcapi.Request{Verb: hostsvcapi.VerbOpen, VPC: vpc})
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Client) Close(vpc string) error {
|
||||
_, err := c.call(hostsvcapi.Request{Verb: hostsvcapi.VerbClose, VPC: vpc})
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Client) List() ([]string, error) {
|
||||
resp, err := c.call(hostsvcapi.Request{Verb: hostsvcapi.VerbList})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.VPCs, nil
|
||||
}
|
||||
|
|
@ -1,163 +0,0 @@
|
|||
package hostsvcclient
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
hostsvcapi "git.g3e.fr/syonad/two/internal/api/hostsvc"
|
||||
)
|
||||
|
||||
type fakeService struct {
|
||||
mu sync.Mutex
|
||||
open map[string]bool
|
||||
failOn string
|
||||
}
|
||||
|
||||
func (f *fakeService) Open(vpc string) error {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
if vpc == f.failOn {
|
||||
return errors.New("netns not found")
|
||||
}
|
||||
f.open[vpc] = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeService) Close(vpc string) error {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
if vpc == f.failOn {
|
||||
return errors.New("close failed")
|
||||
}
|
||||
delete(f.open, vpc)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeService) List() []string {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
var out []string
|
||||
for vpc := range f.open {
|
||||
out = append(out, vpc)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func shortTempDir(t *testing.T) string {
|
||||
t.Helper()
|
||||
dir, err := os.MkdirTemp("", "hostsvc")
|
||||
if err != nil {
|
||||
t.Fatalf("MkdirTemp: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { os.RemoveAll(dir) })
|
||||
return dir
|
||||
}
|
||||
|
||||
func discardLogger() *slog.Logger {
|
||||
return slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
}
|
||||
|
||||
func serve(t *testing.T) (*Client, *fakeService, string) {
|
||||
t.Helper()
|
||||
svc := &fakeService{open: map[string]bool{}, failOn: "vpc-broken"}
|
||||
socketPath := hostsvcapi.SocketPath(shortTempDir(t))
|
||||
server, err := hostsvcapi.Listen(svc, socketPath, discardLogger())
|
||||
if err != nil {
|
||||
t.Fatalf("Listen: %v", err)
|
||||
}
|
||||
go server.Serve()
|
||||
t.Cleanup(func() { server.Close() })
|
||||
return New(socketPath), svc, socketPath
|
||||
}
|
||||
|
||||
func TestListen_SocketIsOwnerOnly(t *testing.T) {
|
||||
_, _, socketPath := serve(t)
|
||||
info, err := os.Stat(socketPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Stat: %v", err)
|
||||
}
|
||||
if got := info.Mode().Perm(); got != 0o600 {
|
||||
t.Errorf("mode = %o, want 600: whoever reaches it opens sockets in any vpc", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListen_ReplacesAStaleSocketFile(t *testing.T) {
|
||||
socketPath := filepath.Join(shortTempDir(t), "stale.sock")
|
||||
if err := os.WriteFile(socketPath, []byte("leftover"), 0o600); err != nil {
|
||||
t.Fatalf("WriteFile: %v", err)
|
||||
}
|
||||
server, err := hostsvcapi.Listen(&fakeService{open: map[string]bool{}}, socketPath, discardLogger())
|
||||
if err != nil {
|
||||
t.Fatalf("a socket left by an unclean stop must not block startup: %v", err)
|
||||
}
|
||||
server.Close()
|
||||
}
|
||||
|
||||
func TestOpenCloseList(t *testing.T) {
|
||||
client, svc, _ := serve(t)
|
||||
if err := client.Open("vpc-00001"); err != nil {
|
||||
t.Fatalf("Open: %v", err)
|
||||
}
|
||||
got, err := client.List()
|
||||
if err != nil {
|
||||
t.Fatalf("List: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(got, []string{"vpc-00001"}) {
|
||||
t.Errorf("List = %v", got)
|
||||
}
|
||||
if err := client.Close("vpc-00001"); err != nil {
|
||||
t.Fatalf("Close: %v", err)
|
||||
}
|
||||
if len(svc.List()) != 0 {
|
||||
t.Errorf("the vpc is still open on the service: %v", svc.List())
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceErrorsAreReported(t *testing.T) {
|
||||
client, _, _ := serve(t)
|
||||
if err := client.Open("vpc-broken"); err == nil || !strings.Contains(err.Error(), "open refused: netns not found") {
|
||||
t.Errorf("Open: error %v", err)
|
||||
}
|
||||
if err := client.Close("vpc-broken"); err == nil || !strings.Contains(err.Error(), "close refused: close failed") {
|
||||
t.Errorf("Close: error %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnknownVerbAndMalformedRequest(t *testing.T) {
|
||||
_, _, socketPath := serve(t)
|
||||
conn, err := net.Dial("unix", socketPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Dial: %v", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
if _, err := conn.Write([]byte("{\"verb\":\"reboot\"}\nnot json\n")); err != nil {
|
||||
t.Fatalf("Write: %v", err)
|
||||
}
|
||||
buf := make([]byte, 4096)
|
||||
var all string
|
||||
for !strings.Contains(all, "malformed request") {
|
||||
n, err := conn.Read(buf)
|
||||
if err != nil {
|
||||
t.Fatalf("Read: %v (got %q)", err, all)
|
||||
}
|
||||
all += string(buf[:n])
|
||||
}
|
||||
if !strings.Contains(all, `unknown verb \"reboot\"`) {
|
||||
t.Errorf("replies = %q, want the unknown verb refused", all)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClient_NoServer(t *testing.T) {
|
||||
client := New(filepath.Join(shortTempDir(t), "absent.sock"))
|
||||
if err := client.Open("vpc-00001"); err == nil || !strings.Contains(err.Error(), "dial") {
|
||||
t.Errorf("error %v, want a dial failure", err)
|
||||
}
|
||||
}
|
||||
|
|
@ -145,21 +145,4 @@ func TestLoadConfig_ExempleFourniEstValide(t *testing.T) {
|
|||
if cfg.QEMU.QMPDir == "" {
|
||||
t.Error("config.exemple.yml devrait définir qemu.qmp_dir")
|
||||
}
|
||||
if got := cfg.PublicBridge(); got != "br-public" {
|
||||
t.Errorf("config.exemple.yml : bridge public %q, br-public attendu", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadConfig_PublicBridgeWithoutInternetKey(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "agent.yml")
|
||||
if err := os.WriteFile(path, []byte("default_interface: br-000000\ninterfaces:\n lan: br-lan\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg, err := LoadConfig(path)
|
||||
if err != nil {
|
||||
t.Fatalf("LoadConfig : %v", err)
|
||||
}
|
||||
if got := cfg.PublicBridge(); got != "br-public" {
|
||||
t.Errorf("bridge public %q, br-public attendu (jamais default_interface)", got)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,79 +0,0 @@
|
|||
package configuration
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const (
|
||||
RateLimitPerVPC = 100
|
||||
DNSPort = 53
|
||||
NTPPort = 123
|
||||
)
|
||||
|
||||
type HostSvc struct {
|
||||
DNSUpstream []string `mapstructure:"dns_upstream"`
|
||||
NTPUpstream []string `mapstructure:"ntp_upstream"`
|
||||
RateLimitPerVPC int `mapstructure:"rate_limit_per_vpc"`
|
||||
}
|
||||
|
||||
func (h HostSvc) DNS() []string { return withPort(h.DNSUpstream, DNSPort) }
|
||||
func (h HostSvc) NTP() []string { return withPort(h.NTPUpstream, NTPPort) }
|
||||
|
||||
func (h HostSvc) Rate() int {
|
||||
if h.RateLimitPerVPC == 0 {
|
||||
return RateLimitPerVPC
|
||||
}
|
||||
return h.RateLimitPerVPC
|
||||
}
|
||||
|
||||
func withPort(servers []string, port int) []string {
|
||||
out := make([]string, 0, len(servers))
|
||||
for _, s := range servers {
|
||||
if net.ParseIP(s) != nil {
|
||||
out = append(out, net.JoinHostPort(s, strconv.Itoa(port)))
|
||||
continue
|
||||
}
|
||||
out = append(out, s)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (h HostSvc) Validate() error {
|
||||
if h.RateLimitPerVPC <= 0 {
|
||||
return fmt.Errorf("hostsvc.rate_limit_per_vpc %d must be a positive number of requests per second", h.RateLimitPerVPC)
|
||||
}
|
||||
for _, list := range []struct {
|
||||
key string
|
||||
servers []string
|
||||
}{
|
||||
{"dns_upstream", h.DNSUpstream},
|
||||
{"ntp_upstream", h.NTPUpstream},
|
||||
} {
|
||||
for _, s := range list.servers {
|
||||
if err := validServer(s); err != nil {
|
||||
return fmt.Errorf("hostsvc.%s: %w", list.key, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validServer(s string) error {
|
||||
if net.ParseIP(s) != nil {
|
||||
return nil
|
||||
}
|
||||
host, port, err := net.SplitHostPort(s)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%q is neither an ip nor ip:port", s)
|
||||
}
|
||||
if net.ParseIP(host) == nil {
|
||||
return fmt.Errorf("%q: %q is not an ip address", s, host)
|
||||
}
|
||||
n, err := strconv.Atoi(port)
|
||||
if err != nil || n < 1 || n > 65535 {
|
||||
return fmt.Errorf("%q: invalid port %q", s, port)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -1,51 +0,0 @@
|
|||
package configuration
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLoadConfig_HostSvcDefaults(t *testing.T) {
|
||||
cfg, err := LoadConfig(writeYAML(t, ""))
|
||||
if err != nil {
|
||||
t.Fatalf("LoadConfig: %v", err)
|
||||
}
|
||||
if len(cfg.HostSvc.DNS()) != 0 || len(cfg.HostSvc.NTP()) != 0 {
|
||||
t.Errorf("without upstreams the lists must stay empty, the host serves: %+v", cfg.HostSvc)
|
||||
}
|
||||
if cfg.HostSvc.Rate() != 100 {
|
||||
t.Errorf("default rate per vpc = %d, want 100", cfg.HostSvc.Rate())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadConfig_HostSvcDefaultPorts(t *testing.T) {
|
||||
cfg, err := LoadConfig(writeYAML(t, "hostsvc:\n dns_upstream: [192.0.2.53, \"192.0.2.54:5353\"]\n ntp_upstream: [192.0.2.123]\n rate_limit_per_vpc: 20\n"))
|
||||
if err != nil {
|
||||
t.Fatalf("LoadConfig: %v", err)
|
||||
}
|
||||
if got, want := cfg.HostSvc.DNS(), []string{"192.0.2.53:53", "192.0.2.54:5353"}; !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("dns = %v, want %v", got, want)
|
||||
}
|
||||
if got, want := cfg.HostSvc.NTP(), []string{"192.0.2.123:123"}; !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("ntp = %v, want %v", got, want)
|
||||
}
|
||||
if cfg.HostSvc.Rate() != 20 {
|
||||
t.Errorf("rate = %d, want 20", cfg.HostSvc.Rate())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadConfig_HostSvcInvalidIsRefused(t *testing.T) {
|
||||
for _, c := range []struct{ yaml, msg string }{
|
||||
{"hostsvc:\n rate_limit_per_vpc: 0\n", "hostsvc.rate_limit_per_vpc 0 must be a positive number"},
|
||||
{"hostsvc:\n rate_limit_per_vpc: -5\n", "hostsvc.rate_limit_per_vpc -5"},
|
||||
{"hostsvc:\n dns_upstream: [dns.example]\n", `hostsvc.dns_upstream: "dns.example" is neither an ip nor ip:port`},
|
||||
{"hostsvc:\n dns_upstream: [\"dns.example:53\"]\n", `"dns.example" is not an ip address`},
|
||||
{"hostsvc:\n ntp_upstream: [\"192.0.2.1:0\"]\n", `hostsvc.ntp_upstream: "192.0.2.1:0": invalid port "0"`},
|
||||
{"hostsvc:\n ntp_upstream: [\"192.0.2.1:70000\"]\n", `invalid port "70000"`},
|
||||
} {
|
||||
if _, err := LoadConfig(writeYAML(t, c.yaml)); err == nil || !strings.Contains(err.Error(), c.msg) {
|
||||
t.Errorf("%q: error %v, want %q", c.yaml, err, c.msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,13 +0,0 @@
|
|||
package configuration
|
||||
|
||||
const (
|
||||
InternetInterface = "internet"
|
||||
DefaultPublicBridge = "br-public"
|
||||
)
|
||||
|
||||
func (c Config) PublicBridge() string {
|
||||
if bridge := c.Interfaces[InternetInterface]; bridge != "" {
|
||||
return bridge
|
||||
}
|
||||
return DefaultPublicBridge
|
||||
}
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
package configuration
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestPublicBridge(t *testing.T) {
|
||||
cases := map[string]struct {
|
||||
cfg Config
|
||||
want string
|
||||
}{
|
||||
"clé absente": {Config{DefaultInterface: "br-000000"}, "br-public"},
|
||||
"clé vide": {Config{DefaultInterface: "br-000000", Interfaces: map[string]string{"internet": ""}}, "br-public"},
|
||||
"clé posée": {Config{Interfaces: map[string]string{"internet": "br-wan"}}, "br-wan"},
|
||||
"autres types, sans default_interface en repli": {Config{DefaultInterface: "br-000000", Interfaces: map[string]string{"lan": "br-lan"}}, "br-public"},
|
||||
}
|
||||
for name, c := range cases {
|
||||
if got := c.cfg.PublicBridge(); got != c.want {
|
||||
t.Errorf("%s : %q, %q attendu", name, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -54,7 +54,6 @@ type Config struct {
|
|||
} `mapstructure:"watchdog"`
|
||||
Network Network `mapstructure:"network"`
|
||||
BGP BGP `mapstructure:"bgp"`
|
||||
HostSvc HostSvc `mapstructure:"hostsvc"`
|
||||
DefaultInterface string `mapstructure:"default_interface"`
|
||||
Interfaces map[string]string `mapstructure:"interfaces"`
|
||||
}
|
||||
|
|
@ -80,9 +79,6 @@ func LoadConfig(path string) (*Config, error) {
|
|||
v.SetDefault("times.poll_interval", PollInterval)
|
||||
v.SetDefault("metadata.run_dir", "/run/two/metadata")
|
||||
v.SetDefault("dhcp.backend", BackendTwo)
|
||||
v.SetDefault("hostsvc.dns_upstream", []string{})
|
||||
v.SetDefault("hostsvc.ntp_upstream", []string{})
|
||||
v.SetDefault("hostsvc.rate_limit_per_vpc", RateLimitPerVPC)
|
||||
v.SetDefault("qemu.ovmf_code_path", "/usr/share/OVMF/OVMF_CODE.fd")
|
||||
v.SetDefault("qemu.ovmf_vars_template", "/usr/share/OVMF/OVMF_VARS.fd")
|
||||
v.SetDefault("qemu.uefi_vars_dir", "/run/two/vms/uefi")
|
||||
|
|
@ -115,9 +111,6 @@ func LoadConfig(path string) (*Config, error) {
|
|||
if err := cfg.Times.Validate(); err != nil {
|
||||
return nil, fmt.Errorf("%s: %w", path, err)
|
||||
}
|
||||
if err := cfg.HostSvc.Validate(); err != nil {
|
||||
return nil, fmt.Errorf("%s: %w", path, err)
|
||||
}
|
||||
|
||||
return &cfg, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -69,24 +69,6 @@ func TestGenerateConfig_ContainsDefaultGateway(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestGenerateConfig_DNSAndNTPAreTheInterfaceIP(t *testing.T) {
|
||||
conf := newConf(t, "192.168.1.0/24")
|
||||
path, err := GenerateConfig(conf)
|
||||
if err != nil {
|
||||
t.Fatalf("GenerateConfig : %v", err)
|
||||
}
|
||||
content, _ := os.ReadFile(path)
|
||||
|
||||
for _, want := range []string{"\ndhcp-option=6,192.168.1.254\n", "\ndhcp-option=42,192.168.1.254\n"} {
|
||||
if !strings.Contains(string(content), want) {
|
||||
t.Errorf("%q absente : DNS et NTP passent par la gateway, relayés par two-hostsvc\n%s", strings.TrimSpace(want), content)
|
||||
}
|
||||
}
|
||||
if strings.Contains(string(content), "1.1.1.1") || strings.Contains(string(content), "8.8.8.8") {
|
||||
t.Errorf("résolveurs publics encore annoncés :\n%s", content)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateConfig_NoDefaultGateway(t *testing.T) {
|
||||
conf := newConf(t, "192.168.1.0/29")
|
||||
conf.DefaultGateway = nil
|
||||
|
|
|
|||
|
|
@ -30,8 +30,7 @@ func GenerateConfig(c Config) (string, error) {
|
|||
} else {
|
||||
fmt.Fprintf(&sb, "dhcp-option=3\n")
|
||||
}
|
||||
fmt.Fprintf(&sb, "dhcp-option=6,%s\n", c.InterfaceIP.String())
|
||||
fmt.Fprintf(&sb, "dhcp-option=42,%s\n", c.InterfaceIP.String())
|
||||
fmt.Fprintf(&sb, "dhcp-option=6,1.1.1.1,8.8.8.8\n")
|
||||
fmt.Fprintf(&sb, "dhcp-option=26,%d\n", c.interfaceMTU())
|
||||
fmt.Fprintf(&sb, "dhcp-hostsdir=%s\n", HostsDir(c.ConfDir, c.Name))
|
||||
fmt.Fprintf(&sb, "dhcp-optsdir=%s\n", OptsDir(c.ConfDir, c.Name))
|
||||
|
|
|
|||
|
|
@ -7,6 +7,13 @@ import (
|
|||
|
||||
const LeaseTime = 12 * time.Hour
|
||||
|
||||
func DNSServers() []net.IP {
|
||||
return []net.IP{
|
||||
net.IPv4(1, 1, 1, 1),
|
||||
net.IPv4(8, 8, 8, 8),
|
||||
}
|
||||
}
|
||||
|
||||
type SubnetConfig struct {
|
||||
Network *net.IPNet
|
||||
InterfaceIP net.IP
|
||||
|
|
|
|||
|
|
@ -86,8 +86,7 @@ func BuildReply(c SubnetConfig, h Host, req *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, err
|
|||
dhcpv4.WithNetmask(c.Network.Mask),
|
||||
dhcpv4.WithLeaseTime(uint32(LeaseTime.Seconds())),
|
||||
dhcpv4.WithOption(dhcpv4.OptServerIdentifier(c.InterfaceIP)),
|
||||
dhcpv4.WithOption(dhcpv4.OptDNS(c.InterfaceIP)),
|
||||
dhcpv4.WithOption(dhcpv4.OptNTPServers(c.InterfaceIP)),
|
||||
dhcpv4.WithOption(dhcpv4.OptDNS(DNSServers()...)),
|
||||
dhcpv4.WithOption(dhcpv4.OptClasslessStaticRoute(routes...)),
|
||||
dhcpv4.WithOption(dhcpv4.OptGeneric(dhcpv4.OptionInterfaceMTU, dhcpv4.Uint16(c.InterfaceMTU()).ToBytes())),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -244,11 +244,8 @@ func TestBuildReply_CarriesAddressMaskLeaseAndServerIdentifier(t *testing.T) {
|
|||
if got := reply.ServerIdentifier(); !got.Equal(c.InterfaceIP) {
|
||||
t.Errorf("server identifier = %s, want %s", got, c.InterfaceIP)
|
||||
}
|
||||
if got := reply.DNS(); len(got) != 1 || !got[0].Equal(net.ParseIP("10.0.5.1")) {
|
||||
t.Errorf("dns = %v, want only the interface ip 10.0.5.1, relayed by two-hostsvc", got)
|
||||
}
|
||||
if got := reply.NTPServers(); len(got) != 1 || !got[0].Equal(net.ParseIP("10.0.5.1")) {
|
||||
t.Errorf("ntp = %v, want only the interface ip 10.0.5.1, relayed by two-hostsvc", got)
|
||||
if got := reply.DNS(); len(got) != 2 || !got[0].Equal(net.IPv4(1, 1, 1, 1)) || !got[1].Equal(net.IPv4(8, 8, 8, 8)) {
|
||||
t.Errorf("dns = %v, want 1.1.1.1 and 8.8.8.8", got)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -22,11 +22,10 @@ type VMDisk struct {
|
|||
}
|
||||
|
||||
type VMNIC struct {
|
||||
Subnet string
|
||||
IP string
|
||||
MAC string
|
||||
Primary bool
|
||||
DHCPServer bool
|
||||
Subnet string
|
||||
IP string
|
||||
MAC string
|
||||
Primary bool
|
||||
}
|
||||
|
||||
type StartVMCommand struct {
|
||||
|
|
@ -71,11 +70,6 @@ func (c StartVMCommand) Prepare(db *badger.DB, _ *configuration.Config) error {
|
|||
return fmt.Errorf("store nic %d primary: %w", i, err)
|
||||
}
|
||||
}
|
||||
if n.DHCPServer {
|
||||
if err := kv.AddInDB(db, prefix+"dhcp_server", "true"); err != nil {
|
||||
return fmt.Errorf("store nic %d dhcp_server: %w", i, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
kv.AddInDB(db, "vm/"+c.Name+"/metadata_port", strconv.Itoa(port))
|
||||
for _, d := range c.Disks {
|
||||
|
|
|
|||
153
internal/ebtables/ebtables.go
Normal file
153
internal/ebtables/ebtables.go
Normal file
|
|
@ -0,0 +1,153 @@
|
|||
package ebtables
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func addRule(args ...string) error {
|
||||
return exec.Command("ebtables", append([]string{"-A"}, args...)...).Run()
|
||||
}
|
||||
|
||||
func deleteRule(args ...string) error {
|
||||
return exec.Command("ebtables", append([]string{"-D"}, args...)...).Run()
|
||||
}
|
||||
|
||||
func DropARPToGateway(iface, ip string) error {
|
||||
if err := addRule("FORWARD",
|
||||
"--out-interface", iface,
|
||||
"-p", "arp",
|
||||
"--arp-op", "Request",
|
||||
"--arp-ip-dst", ip,
|
||||
"-j", "DROP"); err != nil {
|
||||
return fmt.Errorf("ebtables arp rule: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DropDHCP(iface, ip string) error {
|
||||
if err := addRule("FORWARD",
|
||||
"--out-interface", iface,
|
||||
"-p", "IPv4",
|
||||
"--ip-protocol", "udp",
|
||||
"--ip-source-port", "67:68",
|
||||
"--ip-destination-port", "67:68",
|
||||
"--ip-source", ip,
|
||||
"-j", "DROP"); err != nil {
|
||||
return fmt.Errorf("ebtables dhcp rule: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DeleteARPToGateway(iface, ip string) error {
|
||||
return deleteRule("FORWARD",
|
||||
"--out-interface", iface,
|
||||
"-p", "arp",
|
||||
"--arp-op", "Request",
|
||||
"--arp-ip-dst", ip,
|
||||
"-j", "DROP")
|
||||
}
|
||||
|
||||
func DeleteDHCP(iface, ip string) error {
|
||||
return deleteRule("FORWARD",
|
||||
"--out-interface", iface,
|
||||
"-p", "IPv4",
|
||||
"--ip-protocol", "udp",
|
||||
"--ip-source-port", "67:68",
|
||||
"--ip-destination-port", "67:68",
|
||||
"--ip-source", ip,
|
||||
"-j", "DROP")
|
||||
}
|
||||
|
||||
func confineRules(iface, mac string) [][]string {
|
||||
return [][]string{
|
||||
{"-t", "nat", "PREROUTING", "--in-interface", iface, "-s", mac, "-j", "DROP"},
|
||||
{"-t", "filter", "FORWARD", "--out-interface", iface, "-s", mac, "-j", "DROP"},
|
||||
{"-t", "filter", "OUTPUT", "--out-interface", iface, "-s", mac, "-j", "DROP"},
|
||||
}
|
||||
}
|
||||
|
||||
func withAction(action string, rule []string) []string {
|
||||
return append([]string{rule[0], rule[1], action}, rule[2:]...)
|
||||
}
|
||||
|
||||
func ConfineMAC(iface, mac string) error {
|
||||
for _, rule := range confineRules(iface, mac) {
|
||||
if err := exec.Command("ebtables", withAction("-A", rule)...).Run(); err != nil {
|
||||
return fmt.Errorf("ebtables confine %s on %s (%s %s): %w", mac, iface, rule[1], rule[2], err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DeleteConfineMAC(iface, mac string) error {
|
||||
var first error
|
||||
for _, rule := range confineRules(iface, mac) {
|
||||
if err := exec.Command("ebtables", withAction("-D", rule)...).Run(); err != nil && first == nil {
|
||||
first = fmt.Errorf("ebtables unconfine %s on %s (%s %s): %w", mac, iface, rule[1], rule[2], err)
|
||||
}
|
||||
}
|
||||
return first
|
||||
}
|
||||
|
||||
func gatewayRules(iface, ip string) [][]string {
|
||||
dhcp := []string{"-p", "IPv4", "--ip-protocol", "udp", "--ip-source-port", "67:68", "--ip-destination-port", "67:68", "--ip-source", ip, "-j", "DROP"}
|
||||
return [][]string{
|
||||
{"-t", "filter", "FORWARD", "--out-interface", iface, "-p", "arp", "--arp-op", "Request", "--arp-ip-dst", ip, "-j", "DROP"},
|
||||
{"-t", "filter", "OUTPUT", "--out-interface", iface, "-p", "arp", "--arp-ip-src", ip, "-j", "DROP"},
|
||||
append([]string{"-t", "filter", "FORWARD", "--out-interface", iface}, dhcp...),
|
||||
append([]string{"-t", "filter", "OUTPUT", "--out-interface", iface}, dhcp...),
|
||||
}
|
||||
}
|
||||
|
||||
func ruleExists(rule []string) (bool, error) {
|
||||
err := exec.Command("ebtables", withAction("--check", rule)...).Run()
|
||||
var exit *exec.ExitError
|
||||
switch {
|
||||
case err == nil:
|
||||
return true, nil
|
||||
case errors.As(err, &exit) && exit.ExitCode() == 1:
|
||||
return false, nil
|
||||
default:
|
||||
return false, fmt.Errorf("ebtables --check %s: %w", strings.Join(rule, " "), err)
|
||||
}
|
||||
}
|
||||
|
||||
func ConfineGateway(iface, ip string) error {
|
||||
for _, rule := range gatewayRules(iface, ip) {
|
||||
if err := exec.Command("ebtables", withAction("-A", rule)...).Run(); err != nil {
|
||||
return fmt.Errorf("ebtables confine gateway %s on %s (%s): %w", ip, iface, strings.Join(rule, " "), err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DeleteConfineGateway(iface, ip string) error {
|
||||
var first error
|
||||
for _, rule := range gatewayRules(iface, ip) {
|
||||
exists, err := ruleExists(rule)
|
||||
if err == nil && exists {
|
||||
err = exec.Command("ebtables", withAction("-D", rule)...).Run()
|
||||
}
|
||||
if err != nil && first == nil {
|
||||
first = fmt.Errorf("ebtables unconfine gateway %s on %s (%s): %w", ip, iface, strings.Join(rule, " "), err)
|
||||
}
|
||||
}
|
||||
return first
|
||||
}
|
||||
|
||||
func MissingGatewayRules(iface, ip string) ([]string, error) {
|
||||
var missing []string
|
||||
for _, rule := range gatewayRules(iface, ip) {
|
||||
exists, err := ruleExists(rule)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !exists {
|
||||
missing = append(missing, strings.Join(rule, " "))
|
||||
}
|
||||
}
|
||||
return missing, nil
|
||||
}
|
||||
70
internal/ebtables/ebtables_test.go
Normal file
70
internal/ebtables/ebtables_test.go
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
package ebtables
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConfineRules(t *testing.T) {
|
||||
want := []string{
|
||||
"-t nat -A PREROUTING --in-interface v-s9r1-i -s 02:01:00:00:0b:5f -j DROP",
|
||||
"-t filter -A FORWARD --out-interface v-s9r1-i -s 02:01:00:00:0b:5f -j DROP",
|
||||
"-t filter -A OUTPUT --out-interface v-s9r1-i -s 02:01:00:00:0b:5f -j DROP",
|
||||
}
|
||||
rules := confineRules("v-s9r1-i", "02:01:00:00:0b:5f")
|
||||
if len(rules) != len(want) {
|
||||
t.Fatalf("%d règles, %d attendues", len(rules), len(want))
|
||||
}
|
||||
for i, rule := range rules {
|
||||
if got := strings.Join(withAction("-A", rule), " "); got != want[i] {
|
||||
t.Errorf("règle %d : %q, %q attendu", i, got, want[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteUsesTheSameRules(t *testing.T) {
|
||||
for _, rule := range confineRules("v-x-i", "02:01:00:00:00:01") {
|
||||
added := withAction("-A", rule)
|
||||
deleted := withAction("-D", rule)
|
||||
if strings.Replace(strings.Join(added, " "), " -A ", " -D ", 1) != strings.Join(deleted, " ") {
|
||||
t.Errorf("ajout %v et suppression %v divergent", added, deleted)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithActionDoesNotAlterTheRule(t *testing.T) {
|
||||
rule := confineRules("v-x-i", "02:01:00:00:00:01")[0]
|
||||
before := strings.Join(rule, " ")
|
||||
withAction("-A", rule)
|
||||
withAction("-D", rule)
|
||||
if strings.Join(rule, " ") != before {
|
||||
t.Errorf("la règle a été modifiée : %q devenu %q", before, strings.Join(rule, " "))
|
||||
}
|
||||
}
|
||||
|
||||
func TestGatewayRules(t *testing.T) {
|
||||
want := []string{
|
||||
"-t filter -A FORWARD --out-interface v-t33a-i -p arp --arp-op Request --arp-ip-dst 10.33.0.1 -j DROP",
|
||||
"-t filter -A OUTPUT --out-interface v-t33a-i -p arp --arp-ip-src 10.33.0.1 -j DROP",
|
||||
"-t filter -A FORWARD --out-interface v-t33a-i -p IPv4 --ip-protocol udp --ip-source-port 67:68 --ip-destination-port 67:68 --ip-source 10.33.0.1 -j DROP",
|
||||
"-t filter -A OUTPUT --out-interface v-t33a-i -p IPv4 --ip-protocol udp --ip-source-port 67:68 --ip-destination-port 67:68 --ip-source 10.33.0.1 -j DROP",
|
||||
}
|
||||
rules := gatewayRules("v-t33a-i", "10.33.0.1")
|
||||
if len(rules) != len(want) {
|
||||
t.Fatalf("%d règles, %d attendues", len(rules), len(want))
|
||||
}
|
||||
for i, rule := range rules {
|
||||
if got := strings.Join(withAction("-A", rule), " "); got != want[i] {
|
||||
t.Errorf("règle %d : %q, %q attendu", i, got, want[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGatewayRulesDoNotShareSlices(t *testing.T) {
|
||||
rules := gatewayRules("v-x-i", "10.0.0.1")
|
||||
before := strings.Join(rules[2], " ")
|
||||
rules[3][len(rules[3])-1] = "ACCEPT"
|
||||
if strings.Join(rules[2], " ") != before {
|
||||
t.Errorf("les deux règles DHCP partagent leur tableau : %q", strings.Join(rules[2], " "))
|
||||
}
|
||||
}
|
||||
|
|
@ -1,23 +0,0 @@
|
|||
//go:build linux
|
||||
|
||||
package hostsvc
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
type SystemClock struct{}
|
||||
|
||||
func (SystemClock) Now() time.Time { return time.Now() }
|
||||
|
||||
func (SystemClock) Sync() (bool, time.Duration, error) {
|
||||
var tx unix.Timex
|
||||
state, err := unix.Adjtimex(&tx)
|
||||
if err != nil {
|
||||
return false, 0, err
|
||||
}
|
||||
synced := state != unix.TIME_ERROR && tx.Status&unix.STA_UNSYNC == 0
|
||||
return synced, time.Duration(tx.Maxerror) * time.Microsecond, nil
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
//go:build !linux
|
||||
|
||||
package hostsvc
|
||||
|
||||
import "time"
|
||||
|
||||
type SystemClock struct{}
|
||||
|
||||
func (SystemClock) Now() time.Time { return time.Now() }
|
||||
|
||||
func (SystemClock) Sync() (bool, time.Duration, error) { return false, 0, nil }
|
||||
|
|
@ -1,162 +0,0 @@
|
|||
package hostsvc
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
dnsHeaderLen = 12
|
||||
maxDNSMessage = 65535
|
||||
)
|
||||
|
||||
func validQuery(b []byte) error {
|
||||
if len(b) < dnsHeaderLen {
|
||||
return fmt.Errorf("dns message of %d bytes is shorter than its header", len(b))
|
||||
}
|
||||
if b[2]&0x80 != 0 {
|
||||
return errors.New("dns message is a response, not a query")
|
||||
}
|
||||
if op := (b[2] >> 3) & 0x0f; op != 0 {
|
||||
return fmt.Errorf("dns opcode %d is not a standard query", op)
|
||||
}
|
||||
if n := binary.BigEndian.Uint16(b[4:6]); n != 1 {
|
||||
return fmt.Errorf("dns query carries %d questions, want 1", n)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validAnswer(query, answer []byte) error {
|
||||
if len(answer) < dnsHeaderLen {
|
||||
return fmt.Errorf("dns answer of %d bytes is shorter than its header", len(answer))
|
||||
}
|
||||
if answer[0] != query[0] || answer[1] != query[1] {
|
||||
return errors.New("dns answer id does not match the query")
|
||||
}
|
||||
if answer[2]&0x80 == 0 {
|
||||
return errors.New("dns answer is not flagged as a response")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func resolvers(path string) ([]string, error) {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open %s: %w", path, err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var out []string
|
||||
scanner := bufio.NewScanner(f)
|
||||
for scanner.Scan() {
|
||||
fields := strings.Fields(scanner.Text())
|
||||
if len(fields) < 2 || fields[0] != "nameserver" || net.ParseIP(fields[1]) == nil {
|
||||
continue
|
||||
}
|
||||
out = append(out, net.JoinHostPort(fields[1], "53"))
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, fmt.Errorf("read %s: %w", path, err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func exchangeUDP(servers []string, query []byte, timeout time.Duration) ([]byte, error) {
|
||||
var errs []error
|
||||
for _, server := range servers {
|
||||
answer, err := exchangeUDPOne(server, query, timeout)
|
||||
if err == nil {
|
||||
return answer, nil
|
||||
}
|
||||
errs = append(errs, fmt.Errorf("%s: %w", server, err))
|
||||
}
|
||||
return nil, fmt.Errorf("no dns upstream answered: %w", errors.Join(errs...))
|
||||
}
|
||||
|
||||
func exchangeUDPOne(server string, query []byte, timeout time.Duration) ([]byte, error) {
|
||||
conn, err := net.DialTimeout("udp", server, timeout)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer conn.Close()
|
||||
if err := conn.SetDeadline(time.Now().Add(timeout)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err := conn.Write(query); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
buf := make([]byte, maxDNSMessage)
|
||||
for {
|
||||
n, err := conn.Read(buf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if validAnswer(query, buf[:n]) == nil {
|
||||
return buf[:n], nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func exchangeTCP(servers []string, query []byte, timeout time.Duration) ([]byte, error) {
|
||||
var errs []error
|
||||
for _, server := range servers {
|
||||
answer, err := exchangeTCPOne(server, query, timeout)
|
||||
if err == nil {
|
||||
return answer, nil
|
||||
}
|
||||
errs = append(errs, fmt.Errorf("%s: %w", server, err))
|
||||
}
|
||||
return nil, fmt.Errorf("no dns upstream answered: %w", errors.Join(errs...))
|
||||
}
|
||||
|
||||
func exchangeTCPOne(server string, query []byte, timeout time.Duration) ([]byte, error) {
|
||||
conn, err := net.DialTimeout("tcp", server, timeout)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer conn.Close()
|
||||
if err := conn.SetDeadline(time.Now().Add(timeout)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := writeTCPMessage(conn, query); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
answer, err := readTCPMessage(conn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := validAnswer(query, answer); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return answer, nil
|
||||
}
|
||||
|
||||
func readTCPMessage(r io.Reader) ([]byte, error) {
|
||||
var size [2]byte
|
||||
if _, err := io.ReadFull(r, size[:]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
msg := make([]byte, binary.BigEndian.Uint16(size[:]))
|
||||
if _, err := io.ReadFull(r, msg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
func writeTCPMessage(w io.Writer, msg []byte) error {
|
||||
if len(msg) > maxDNSMessage {
|
||||
return fmt.Errorf("dns message of %d bytes does not fit a tcp frame", len(msg))
|
||||
}
|
||||
frame := make([]byte, 2+len(msg))
|
||||
binary.BigEndian.PutUint16(frame, uint16(len(msg)))
|
||||
copy(frame[2:], msg)
|
||||
_, err := w.Write(frame)
|
||||
return err
|
||||
}
|
||||
|
|
@ -1,49 +0,0 @@
|
|||
//go:build linux
|
||||
|
||||
package hostsvc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
const netnsDir = "/var/run/netns"
|
||||
|
||||
func netnsExists(vpc string) bool {
|
||||
_, err := os.Stat(netnsDir + "/" + vpc)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func enterNetns(vpc string, fn func() error) error {
|
||||
runtime.LockOSThread()
|
||||
|
||||
orig, err := os.Open("/proc/thread-self/ns/net")
|
||||
if err != nil {
|
||||
runtime.UnlockOSThread()
|
||||
return fmt.Errorf("open the current netns: %w", err)
|
||||
}
|
||||
defer orig.Close()
|
||||
|
||||
target, err := os.Open(netnsDir + "/" + vpc)
|
||||
if err != nil {
|
||||
runtime.UnlockOSThread()
|
||||
return fmt.Errorf("open netns %s: %w", vpc, err)
|
||||
}
|
||||
defer target.Close()
|
||||
|
||||
if err := unix.Setns(int(target.Fd()), unix.CLONE_NEWNET); err != nil {
|
||||
runtime.UnlockOSThread()
|
||||
return fmt.Errorf("enter netns %s: %w", vpc, err)
|
||||
}
|
||||
|
||||
err = fn()
|
||||
|
||||
if restore := unix.Setns(int(orig.Fd()), unix.CLONE_NEWNET); restore != nil {
|
||||
return fmt.Errorf("leave netns %s, thread discarded: %w", vpc, restore)
|
||||
}
|
||||
runtime.UnlockOSThread()
|
||||
return err
|
||||
}
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
//go:build !linux
|
||||
|
||||
package hostsvc
|
||||
|
||||
func netnsExists(string) bool { return false }
|
||||
|
||||
func enterNetns(_ string, fn func() error) error { return fn() }
|
||||
|
|
@ -1,56 +0,0 @@
|
|||
package hostsvc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func FuzzValidQuery(f *testing.F) {
|
||||
f.Add(query(1))
|
||||
f.Add(answerTo(query(1)))
|
||||
f.Add([]byte{})
|
||||
f.Fuzz(func(t *testing.T, b []byte) {
|
||||
if validQuery(b) == nil && len(b) < dnsHeaderLen {
|
||||
t.Fatalf("a %d-byte message passed", len(b))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func FuzzValidAnswer(f *testing.F) {
|
||||
f.Add(query(1), answerTo(query(1)))
|
||||
f.Add(query(1), []byte{1})
|
||||
f.Fuzz(func(t *testing.T, q, a []byte) {
|
||||
if validQuery(q) != nil {
|
||||
return
|
||||
}
|
||||
if validAnswer(q, a) == nil && (len(a) < dnsHeaderLen || a[0] != q[0] || a[1] != q[1]) {
|
||||
t.Fatalf("answer %x passed for query %x", a, q)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func FuzzReadTCPMessage(f *testing.F) {
|
||||
f.Add([]byte{0, 3, 'a', 'b', 'c'})
|
||||
f.Add([]byte{0xff, 0xff})
|
||||
f.Fuzz(func(t *testing.T, b []byte) {
|
||||
msg, err := readTCPMessage(bytes.NewReader(b))
|
||||
if err == nil && len(msg) != int(b[0])<<8|int(b[1]) {
|
||||
t.Fatalf("message of %d bytes for prefix %x", len(msg), b[:2])
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func FuzzHostReply(f *testing.F) {
|
||||
f.Add(ntpRequest(4, 1))
|
||||
f.Add([]byte("short"))
|
||||
f.Fuzz(func(t *testing.T, b []byte) {
|
||||
if validNTPRequest(b) != nil {
|
||||
return
|
||||
}
|
||||
r := hostReply(b, time.Unix(0, 0), time.Unix(1, 0), true, 0)
|
||||
if err := validNTPAnswer(b, r); err != nil {
|
||||
t.Fatalf("own reply rejected: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
package hostsvc
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type limiter struct {
|
||||
mu sync.Mutex
|
||||
rate float64
|
||||
tokens float64
|
||||
last time.Time
|
||||
now func() time.Time
|
||||
}
|
||||
|
||||
func newLimiter(rate int, now func() time.Time) *limiter {
|
||||
return &limiter{rate: float64(rate), tokens: float64(rate), last: now(), now: now}
|
||||
}
|
||||
|
||||
func (l *limiter) allow() bool {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
t := l.now()
|
||||
l.tokens += t.Sub(l.last).Seconds() * l.rate
|
||||
if l.tokens > l.rate {
|
||||
l.tokens = l.rate
|
||||
}
|
||||
l.last = t
|
||||
if l.tokens < 1 {
|
||||
return false
|
||||
}
|
||||
l.tokens--
|
||||
return true
|
||||
}
|
||||
|
|
@ -1,126 +0,0 @@
|
|||
package hostsvc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
ntpPacketLen = 48
|
||||
ntpModeClient = 3
|
||||
ntpModeServer = 4
|
||||
ntpUnsynced = 3
|
||||
ntpStratumHost = 3
|
||||
ntpStratumNone = 16
|
||||
ntpPrecision = -20
|
||||
ntpEpochOffset = 2208988800
|
||||
)
|
||||
|
||||
var ntpRefID = [4]byte{'L', 'O', 'C', 'L'}
|
||||
|
||||
type Clock interface {
|
||||
Now() time.Time
|
||||
Sync() (synced bool, maxError time.Duration, err error)
|
||||
}
|
||||
|
||||
func validNTPRequest(b []byte) error {
|
||||
if len(b) < ntpPacketLen {
|
||||
return fmt.Errorf("ntp packet of %d bytes is shorter than %d", len(b), ntpPacketLen)
|
||||
}
|
||||
if mode := b[0] & 0x07; mode != ntpModeClient {
|
||||
return fmt.Errorf("ntp mode %d is not a client request", mode)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validNTPAnswer(request, answer []byte) error {
|
||||
if len(answer) < ntpPacketLen {
|
||||
return fmt.Errorf("ntp answer of %d bytes is shorter than %d", len(answer), ntpPacketLen)
|
||||
}
|
||||
if mode := answer[0] & 0x07; mode != ntpModeServer {
|
||||
return fmt.Errorf("ntp answer mode %d is not a server reply", mode)
|
||||
}
|
||||
if !bytes.Equal(answer[24:32], request[40:48]) {
|
||||
return errors.New("ntp answer origin timestamp does not match the request")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ntpTimestamp(t time.Time) uint64 {
|
||||
secs := uint64(t.Unix() + ntpEpochOffset)
|
||||
frac := (uint64(t.Nanosecond()) << 32) / uint64(time.Second)
|
||||
return secs<<32 | frac
|
||||
}
|
||||
|
||||
func ntpShort(d time.Duration) uint32 {
|
||||
if d < 0 {
|
||||
d = 0
|
||||
}
|
||||
secs := uint64(d / time.Second)
|
||||
if secs > 0xffff {
|
||||
return 0xffffffff
|
||||
}
|
||||
frac := (uint64(d%time.Second) << 16) / uint64(time.Second)
|
||||
return uint32(secs<<16 | frac)
|
||||
}
|
||||
|
||||
func hostReply(request []byte, received, sent time.Time, synced bool, maxError time.Duration) []byte {
|
||||
reply := make([]byte, ntpPacketLen)
|
||||
version := (request[0] >> 3) & 0x07
|
||||
leap, stratum := byte(0), byte(ntpStratumHost)
|
||||
if !synced {
|
||||
leap, stratum = ntpUnsynced, ntpStratumNone
|
||||
}
|
||||
reply[0] = leap<<6 | version<<3 | ntpModeServer
|
||||
reply[1] = stratum
|
||||
reply[2] = request[2]
|
||||
precision := int8(ntpPrecision)
|
||||
reply[3] = byte(precision)
|
||||
binary.BigEndian.PutUint32(reply[8:12], ntpShort(maxError))
|
||||
copy(reply[12:16], ntpRefID[:])
|
||||
binary.BigEndian.PutUint64(reply[16:24], ntpTimestamp(received))
|
||||
copy(reply[24:32], request[40:48])
|
||||
binary.BigEndian.PutUint64(reply[32:40], ntpTimestamp(received))
|
||||
binary.BigEndian.PutUint64(reply[40:48], ntpTimestamp(sent))
|
||||
return reply
|
||||
}
|
||||
|
||||
func exchangeNTP(servers []string, request []byte, timeout time.Duration) ([]byte, error) {
|
||||
var errs []error
|
||||
for _, server := range servers {
|
||||
answer, err := exchangeNTPOne(server, request, timeout)
|
||||
if err == nil {
|
||||
return answer, nil
|
||||
}
|
||||
errs = append(errs, fmt.Errorf("%s: %w", server, err))
|
||||
}
|
||||
return nil, fmt.Errorf("no ntp upstream answered: %w", errors.Join(errs...))
|
||||
}
|
||||
|
||||
func exchangeNTPOne(server string, request []byte, timeout time.Duration) ([]byte, error) {
|
||||
conn, err := net.DialTimeout("udp", server, timeout)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer conn.Close()
|
||||
if err := conn.SetDeadline(time.Now().Add(timeout)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err := conn.Write(request[:ntpPacketLen]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
buf := make([]byte, 1024)
|
||||
for {
|
||||
n, err := conn.Read(buf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if validNTPAnswer(request, buf[:n]) == nil {
|
||||
return buf[:n], nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,234 +0,0 @@
|
|||
package hostsvc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func query(id uint16) []byte {
|
||||
q := make([]byte, dnsHeaderLen, dnsHeaderLen+17)
|
||||
binary.BigEndian.PutUint16(q[0:2], id)
|
||||
q[2] = 0x01
|
||||
binary.BigEndian.PutUint16(q[4:6], 1)
|
||||
q = append(q, 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'o', 'r', 'g', 0, 0, 1, 0, 1)
|
||||
return q
|
||||
}
|
||||
|
||||
func answerTo(q []byte) []byte {
|
||||
a := append([]byte{}, q...)
|
||||
a[2] |= 0x80
|
||||
return a
|
||||
}
|
||||
|
||||
func ntpRequest(version byte, transmit uint64) []byte {
|
||||
r := make([]byte, ntpPacketLen)
|
||||
r[0] = version<<3 | ntpModeClient
|
||||
r[2] = 6
|
||||
binary.BigEndian.PutUint64(r[40:48], transmit)
|
||||
return r
|
||||
}
|
||||
|
||||
func TestValidQuery(t *testing.T) {
|
||||
if err := validQuery(query(7)); err != nil {
|
||||
t.Fatalf("a standard query must pass: %v", err)
|
||||
}
|
||||
response := answerTo(query(7))
|
||||
opcode := query(7)
|
||||
opcode[2] |= 2 << 3
|
||||
twoQuestions := query(7)
|
||||
binary.BigEndian.PutUint16(twoQuestions[4:6], 2)
|
||||
noQuestion := query(7)
|
||||
binary.BigEndian.PutUint16(noQuestion[4:6], 0)
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
msg []byte
|
||||
want string
|
||||
}{
|
||||
{"short", query(7)[:11], "11 bytes is shorter than its header"},
|
||||
{"response", response, "is a response, not a query"},
|
||||
{"opcode", opcode, "opcode 2 is not a standard query"},
|
||||
{"two questions", twoQuestions, "carries 2 questions, want 1"},
|
||||
{"no question", noQuestion, "carries 0 questions, want 1"},
|
||||
} {
|
||||
if err := validQuery(c.msg); err == nil || !strings.Contains(err.Error(), c.want) {
|
||||
t.Errorf("%s: error %v, want %q", c.name, err, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidAnswer(t *testing.T) {
|
||||
q := query(0x1234)
|
||||
if err := validAnswer(q, answerTo(q)); err != nil {
|
||||
t.Fatalf("a matching answer must pass: %v", err)
|
||||
}
|
||||
if err := validAnswer(q, answerTo(query(0x1235))); err == nil || !strings.Contains(err.Error(), "id does not match") {
|
||||
t.Errorf("another id: error %v", err)
|
||||
}
|
||||
if err := validAnswer(q, q); err == nil || !strings.Contains(err.Error(), "not flagged as a response") {
|
||||
t.Errorf("a query echoed back: error %v", err)
|
||||
}
|
||||
if err := validAnswer(q, q[:5]); err == nil || !strings.Contains(err.Error(), "shorter than its header") {
|
||||
t.Errorf("short: error %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolvers(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "resolv.conf")
|
||||
content := "# comment\nsearch example.org\nnameserver 192.0.2.1\nnameserver 2001:db8::1\nnameserver not-an-ip\nnameserver\noptions edns0\n"
|
||||
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
|
||||
t.Fatalf("WriteFile: %v", err)
|
||||
}
|
||||
got, err := resolvers(path)
|
||||
if err != nil {
|
||||
t.Fatalf("resolvers: %v", err)
|
||||
}
|
||||
if want := []string{"192.0.2.1:53", "[2001:db8::1]:53"}; !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("resolvers = %v, want %v", got, want)
|
||||
}
|
||||
if _, err := resolvers(filepath.Join(t.TempDir(), "absent")); err == nil {
|
||||
t.Error("a missing resolv.conf must be reported")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTCPMessageRoundTrip(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
if err := writeTCPMessage(&buf, query(9)); err != nil {
|
||||
t.Fatalf("writeTCPMessage: %v", err)
|
||||
}
|
||||
if got := binary.BigEndian.Uint16(buf.Bytes()[:2]); int(got) != len(query(9)) {
|
||||
t.Errorf("length prefix = %d, want %d", got, len(query(9)))
|
||||
}
|
||||
msg, err := readTCPMessage(&buf)
|
||||
if err != nil {
|
||||
t.Fatalf("readTCPMessage: %v", err)
|
||||
}
|
||||
if !bytes.Equal(msg, query(9)) {
|
||||
t.Errorf("message changed on the way: %x", msg)
|
||||
}
|
||||
if err := writeTCPMessage(&buf, make([]byte, maxDNSMessage+1)); err == nil {
|
||||
t.Error("a message larger than a tcp frame must be refused")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidNTPRequest(t *testing.T) {
|
||||
if err := validNTPRequest(ntpRequest(4, 1)); err != nil {
|
||||
t.Fatalf("a client request must pass: %v", err)
|
||||
}
|
||||
server := ntpRequest(4, 1)
|
||||
server[0] = 4<<3 | ntpModeServer
|
||||
if err := validNTPRequest(server); err == nil || !strings.Contains(err.Error(), "mode 4 is not a client request") {
|
||||
t.Errorf("server mode: error %v", err)
|
||||
}
|
||||
if err := validNTPRequest(ntpRequest(4, 1)[:47]); err == nil || !strings.Contains(err.Error(), "47 bytes is shorter than 48") {
|
||||
t.Errorf("short: error %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidNTPAnswer(t *testing.T) {
|
||||
req := ntpRequest(4, 0xdeadbeef)
|
||||
ans := hostReply(req, time.Unix(0, 0), time.Unix(0, 0), true, 0)
|
||||
if err := validNTPAnswer(req, ans); err != nil {
|
||||
t.Fatalf("a matching answer must pass: %v", err)
|
||||
}
|
||||
if err := validNTPAnswer(ntpRequest(4, 0xdeadbeee), ans); err == nil || !strings.Contains(err.Error(), "origin timestamp") {
|
||||
t.Errorf("another request: error %v", err)
|
||||
}
|
||||
if err := validNTPAnswer(req, req); err == nil || !strings.Contains(err.Error(), "mode 3 is not a server reply") {
|
||||
t.Errorf("a request echoed back: error %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNTPTimestamp(t *testing.T) {
|
||||
if got := ntpTimestamp(time.Unix(0, 0)); got != 2208988800<<32 {
|
||||
t.Errorf("unix epoch = %#x, want %#x", got, uint64(2208988800)<<32)
|
||||
}
|
||||
if got := ntpTimestamp(time.Unix(1, 500_000_000)); got != (2208988801<<32 | 0x80000000) {
|
||||
t.Errorf("half a second = %#x", got)
|
||||
}
|
||||
if got := ntpShort(1500 * time.Millisecond); got != 0x00018000 {
|
||||
t.Errorf("ntpShort(1.5 s) = %#x, want 0x18000", got)
|
||||
}
|
||||
if got := ntpShort(-time.Second); got != 0 {
|
||||
t.Errorf("ntpShort of a negative duration = %#x, want 0", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHostReply_Synced(t *testing.T) {
|
||||
req := ntpRequest(3, 0x0102030405060708)
|
||||
received := time.Unix(1000, 0)
|
||||
sent := time.Unix(1001, 0)
|
||||
r := hostReply(req, received, sent, true, 500*time.Millisecond)
|
||||
|
||||
if r[0] != 0<<6|3<<3|4 {
|
||||
t.Errorf("li/vn/mode = %#08b, want li 0, the client version 3, mode 4", r[0])
|
||||
}
|
||||
if r[1] != 3 {
|
||||
t.Errorf("stratum = %d, want 3", r[1])
|
||||
}
|
||||
if r[2] != 6 {
|
||||
t.Errorf("poll = %d, want the client's 6", r[2])
|
||||
}
|
||||
if int8(r[3]) != -20 {
|
||||
t.Errorf("precision = %d, want -20", int8(r[3]))
|
||||
}
|
||||
if got := binary.BigEndian.Uint32(r[8:12]); got != 0x8000 {
|
||||
t.Errorf("root dispersion = %#x, want 0.5 s", got)
|
||||
}
|
||||
if string(r[12:16]) != "LOCL" {
|
||||
t.Errorf("reference id = %q, want LOCL", r[12:16])
|
||||
}
|
||||
if got := binary.BigEndian.Uint64(r[24:32]); got != 0x0102030405060708 {
|
||||
t.Errorf("origin = %#x, want the client's transmit timestamp", got)
|
||||
}
|
||||
if got := binary.BigEndian.Uint64(r[32:40]); got != ntpTimestamp(received) {
|
||||
t.Errorf("receive = %#x, want %#x", got, ntpTimestamp(received))
|
||||
}
|
||||
if got := binary.BigEndian.Uint64(r[40:48]); got != ntpTimestamp(sent) {
|
||||
t.Errorf("transmit = %#x, want %#x", got, ntpTimestamp(sent))
|
||||
}
|
||||
}
|
||||
|
||||
func TestHostReply_UnsyncedRaisesTheAlarm(t *testing.T) {
|
||||
r := hostReply(ntpRequest(4, 1), time.Unix(0, 0), time.Unix(0, 0), false, 0)
|
||||
if li := r[0] >> 6; li != 3 {
|
||||
t.Errorf("leap indicator = %d, want 3: an unsynchronized host must not be trusted", li)
|
||||
}
|
||||
if r[1] != 16 {
|
||||
t.Errorf("stratum = %d, want 16 (unsynchronized)", r[1])
|
||||
}
|
||||
}
|
||||
|
||||
func TestLimiter(t *testing.T) {
|
||||
now := time.Unix(0, 0)
|
||||
l := newLimiter(3, func() time.Time { return now })
|
||||
for i := 0; i < 3; i++ {
|
||||
if !l.allow() {
|
||||
t.Fatalf("request %d refused inside the burst", i+1)
|
||||
}
|
||||
}
|
||||
if l.allow() {
|
||||
t.Fatal("the burst is exhausted, the next request must be refused")
|
||||
}
|
||||
now = now.Add(400 * time.Millisecond)
|
||||
if !l.allow() {
|
||||
t.Fatal("400 ms refill 1.2 tokens at 3 per second, one request must pass")
|
||||
}
|
||||
if l.allow() {
|
||||
t.Fatal("only one token was refilled")
|
||||
}
|
||||
now = now.Add(time.Hour)
|
||||
for i := 0; i < 3; i++ {
|
||||
if !l.allow() {
|
||||
t.Fatalf("after a long pause, request %d refused: the bucket holds 3", i+1)
|
||||
}
|
||||
}
|
||||
if l.allow() {
|
||||
t.Fatal("the bucket must not grow beyond its rate")
|
||||
}
|
||||
}
|
||||
|
|
@ -1,362 +0,0 @@
|
|||
package hostsvc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.g3e.fr/syonad/two/pkg/db/statefile"
|
||||
)
|
||||
|
||||
const (
|
||||
ServiceIP = "169.254.169.253"
|
||||
DefaultResolv = "/etc/resolv.conf"
|
||||
DefaultTimeout = 2 * time.Second
|
||||
tcpIdle = 10 * time.Second
|
||||
maxTCPConns = 16
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
DNS []string
|
||||
NTP []string
|
||||
Rate int
|
||||
Resolv string
|
||||
DNSListen string
|
||||
NTPListen string
|
||||
Timeout time.Duration
|
||||
Clock Clock
|
||||
Enter func(vpc string, fn func() error) error
|
||||
Exists func(vpc string) bool
|
||||
Logger *slog.Logger
|
||||
}
|
||||
|
||||
func (o Options) withDefaults() Options {
|
||||
if o.Resolv == "" {
|
||||
o.Resolv = DefaultResolv
|
||||
}
|
||||
if o.DNSListen == "" {
|
||||
o.DNSListen = net.JoinHostPort(ServiceIP, "53")
|
||||
}
|
||||
if o.NTPListen == "" {
|
||||
o.NTPListen = net.JoinHostPort(ServiceIP, "123")
|
||||
}
|
||||
if o.Timeout == 0 {
|
||||
o.Timeout = DefaultTimeout
|
||||
}
|
||||
if o.Clock == nil {
|
||||
o.Clock = SystemClock{}
|
||||
}
|
||||
if o.Enter == nil {
|
||||
o.Enter = enterNetns
|
||||
}
|
||||
if o.Exists == nil {
|
||||
o.Exists = netnsExists
|
||||
}
|
||||
if o.Logger == nil {
|
||||
o.Logger = slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
}
|
||||
return o
|
||||
}
|
||||
|
||||
type State struct {
|
||||
VPCs []string `json:"vpcs"`
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
opts Options
|
||||
state *statefile.File[State]
|
||||
mu sync.Mutex
|
||||
vpcs map[string]*instance
|
||||
}
|
||||
|
||||
func New(opts Options, statePath string) (*Service, error) {
|
||||
if opts.Rate <= 0 {
|
||||
return nil, fmt.Errorf("rate limit %d must be a positive number of requests per second", opts.Rate)
|
||||
}
|
||||
return &Service{
|
||||
opts: opts.withDefaults(),
|
||||
state: statefile.New[State](statePath),
|
||||
vpcs: map[string]*instance{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func validVPC(vpc string) error {
|
||||
if vpc == "" || vpc == "." || vpc == ".." || strings.ContainsAny(vpc, "/\x00") {
|
||||
return fmt.Errorf("invalid vpc name %q", vpc)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) Restore() error {
|
||||
saved, err := s.state.Load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
var errs []error
|
||||
for _, vpc := range saved.VPCs {
|
||||
if !s.opts.Exists(vpc) {
|
||||
s.opts.Logger.Info("netns gone, vpc dropped from the state", "vpc", vpc)
|
||||
continue
|
||||
}
|
||||
if err := s.open(vpc); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
if err := s.save(); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
|
||||
func (s *Service) Open(vpc string) error {
|
||||
if err := validVPC(vpc); err != nil {
|
||||
return err
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if _, open := s.vpcs[vpc]; open {
|
||||
return nil
|
||||
}
|
||||
if err := s.open(vpc); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.save()
|
||||
}
|
||||
|
||||
func (s *Service) Close(vpc string) error {
|
||||
if err := validVPC(vpc); err != nil {
|
||||
return err
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
inst, open := s.vpcs[vpc]
|
||||
if !open {
|
||||
return nil
|
||||
}
|
||||
delete(s.vpcs, vpc)
|
||||
err := inst.close()
|
||||
return errors.Join(err, s.save())
|
||||
}
|
||||
|
||||
func (s *Service) List() []string {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return s.names()
|
||||
}
|
||||
|
||||
func (s *Service) Shutdown() error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
var errs []error
|
||||
for vpc, inst := range s.vpcs {
|
||||
errs = append(errs, inst.close())
|
||||
delete(s.vpcs, vpc)
|
||||
}
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
|
||||
func (s *Service) names() []string {
|
||||
out := make([]string, 0, len(s.vpcs))
|
||||
for vpc := range s.vpcs {
|
||||
out = append(out, vpc)
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out
|
||||
}
|
||||
|
||||
func (s *Service) save() error {
|
||||
return s.state.Save(State{VPCs: s.names()})
|
||||
}
|
||||
|
||||
func (s *Service) open(vpc string) error {
|
||||
inst := &instance{
|
||||
vpc: vpc,
|
||||
svc: s,
|
||||
limit: newLimiter(s.opts.Rate, time.Now),
|
||||
conns: make(chan struct{}, maxTCPConns),
|
||||
log: s.opts.Logger.With("vpc", vpc),
|
||||
}
|
||||
dns, ntp := s.opts.DNSListen, s.opts.NTPListen
|
||||
err := s.opts.Enter(vpc, func() error {
|
||||
var err error
|
||||
if inst.udpDNS, err = net.ListenPacket("udp4", dns); err != nil {
|
||||
return fmt.Errorf("listen udp %s: %w", dns, err)
|
||||
}
|
||||
if inst.tcpDNS, err = net.Listen("tcp4", dns); err != nil {
|
||||
return fmt.Errorf("listen tcp %s: %w", dns, err)
|
||||
}
|
||||
if inst.udpNTP, err = net.ListenPacket("udp4", ntp); err != nil {
|
||||
return fmt.Errorf("listen udp %s: %w", ntp, err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Join(fmt.Errorf("open vpc %s: %w", vpc, err), inst.close())
|
||||
}
|
||||
inst.serve()
|
||||
s.vpcs[vpc] = inst
|
||||
inst.log.Info("vpc served", "dns", dns, "ntp", ntp)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) dnsServers() []string {
|
||||
servers := append([]string{}, s.opts.DNS...)
|
||||
host, err := resolvers(s.opts.Resolv)
|
||||
if err != nil {
|
||||
s.opts.Logger.Warn("host resolver unreadable", "error", err)
|
||||
}
|
||||
return append(servers, host...)
|
||||
}
|
||||
|
||||
func (s *Service) ntpAnswer(request []byte, received time.Time) []byte {
|
||||
if len(s.opts.NTP) > 0 {
|
||||
answer, err := exchangeNTP(s.opts.NTP, request, s.opts.Timeout)
|
||||
if err == nil {
|
||||
return answer
|
||||
}
|
||||
s.opts.Logger.Warn("ntp upstreams failed, answering from the host clock", "error", err)
|
||||
}
|
||||
synced, maxError, err := s.opts.Clock.Sync()
|
||||
if err != nil {
|
||||
s.opts.Logger.Warn("host clock status unreadable, answering as unsynchronized", "error", err)
|
||||
synced = false
|
||||
}
|
||||
return hostReply(request, received, s.opts.Clock.Now(), synced, maxError)
|
||||
}
|
||||
|
||||
type instance struct {
|
||||
vpc string
|
||||
svc *Service
|
||||
limit *limiter
|
||||
conns chan struct{}
|
||||
log *slog.Logger
|
||||
udpDNS net.PacketConn
|
||||
tcpDNS net.Listener
|
||||
udpNTP net.PacketConn
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
func (i *instance) serve() {
|
||||
i.wg.Add(3)
|
||||
go i.loopUDP(i.udpDNS, i.answerDNS)
|
||||
go i.loopUDP(i.udpNTP, i.answerNTP)
|
||||
go i.loopTCP()
|
||||
}
|
||||
|
||||
func (i *instance) close() error {
|
||||
var errs []error
|
||||
for _, c := range []interface{ Close() error }{i.udpDNS, i.tcpDNS, i.udpNTP} {
|
||||
if c == nil {
|
||||
continue
|
||||
}
|
||||
if err := c.Close(); err != nil && !errors.Is(err, net.ErrClosed) {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
i.wg.Wait()
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
|
||||
func (i *instance) loopUDP(conn net.PacketConn, answer func([]byte, time.Time) ([]byte, error)) {
|
||||
defer i.wg.Done()
|
||||
buf := make([]byte, maxDNSMessage)
|
||||
for {
|
||||
n, from, err := conn.ReadFrom(buf)
|
||||
if err != nil {
|
||||
if !errors.Is(err, net.ErrClosed) {
|
||||
i.log.Error("udp socket stopped", "error", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
received := i.svc.opts.Clock.Now()
|
||||
if !i.limit.allow() {
|
||||
i.log.Debug("rate limit reached, request dropped", "from", from)
|
||||
continue
|
||||
}
|
||||
request := append([]byte{}, buf[:n]...)
|
||||
go func() {
|
||||
reply, err := answer(request, received)
|
||||
if err != nil {
|
||||
i.log.Debug("request dropped", "from", from, "error", err)
|
||||
return
|
||||
}
|
||||
if _, err := conn.WriteTo(reply, from); err != nil && !errors.Is(err, net.ErrClosed) {
|
||||
i.log.Debug("reply not sent", "to", from, "error", err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
func (i *instance) answerDNS(query []byte, _ time.Time) ([]byte, error) {
|
||||
if err := validQuery(query); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return exchangeUDP(i.svc.dnsServers(), query, i.svc.opts.Timeout)
|
||||
}
|
||||
|
||||
func (i *instance) answerNTP(request []byte, received time.Time) ([]byte, error) {
|
||||
if err := validNTPRequest(request); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return i.svc.ntpAnswer(request, received), nil
|
||||
}
|
||||
|
||||
func (i *instance) loopTCP() {
|
||||
defer i.wg.Done()
|
||||
for {
|
||||
conn, err := i.tcpDNS.Accept()
|
||||
if err != nil {
|
||||
if !errors.Is(err, net.ErrClosed) {
|
||||
i.log.Error("tcp socket stopped", "error", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
select {
|
||||
case i.conns <- struct{}{}:
|
||||
go func() {
|
||||
defer func() { <-i.conns }()
|
||||
i.serveTCP(conn)
|
||||
}()
|
||||
default:
|
||||
i.log.Debug("too many tcp connections, connection refused", "from", conn.RemoteAddr())
|
||||
conn.Close()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (i *instance) serveTCP(conn net.Conn) {
|
||||
defer conn.Close()
|
||||
for {
|
||||
if err := conn.SetDeadline(time.Now().Add(tcpIdle)); err != nil {
|
||||
return
|
||||
}
|
||||
query, err := readTCPMessage(conn)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if !i.limit.allow() {
|
||||
i.log.Debug("rate limit reached, connection closed", "from", conn.RemoteAddr())
|
||||
return
|
||||
}
|
||||
if err := validQuery(query); err != nil {
|
||||
i.log.Debug("request dropped", "from", conn.RemoteAddr(), "error", err)
|
||||
return
|
||||
}
|
||||
answer, err := exchangeTCP(i.svc.dnsServers(), query, i.svc.opts.Timeout)
|
||||
if err != nil {
|
||||
i.log.Debug("request dropped", "from", conn.RemoteAddr(), "error", err)
|
||||
return
|
||||
}
|
||||
if err := writeTCPMessage(conn, answer); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,538 +0,0 @@
|
|||
package hostsvc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.g3e.fr/syonad/two/pkg/db/statefile"
|
||||
)
|
||||
|
||||
type fakeClock struct {
|
||||
now time.Time
|
||||
synced bool
|
||||
maxError time.Duration
|
||||
err error
|
||||
}
|
||||
|
||||
func (c fakeClock) Now() time.Time { return c.now }
|
||||
func (c fakeClock) Sync() (bool, time.Duration, error) {
|
||||
return c.synced, c.maxError, c.err
|
||||
}
|
||||
|
||||
type netnsRecorder struct {
|
||||
mu sync.Mutex
|
||||
entered []string
|
||||
fail error
|
||||
}
|
||||
|
||||
func (r *netnsRecorder) enter(vpc string, fn func() error) error {
|
||||
r.mu.Lock()
|
||||
r.entered = append(r.entered, vpc)
|
||||
r.mu.Unlock()
|
||||
if err := fn(); err != nil {
|
||||
return err
|
||||
}
|
||||
return r.fail
|
||||
}
|
||||
|
||||
func (r *netnsRecorder) calls() []string {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
return append([]string{}, r.entered...)
|
||||
}
|
||||
|
||||
func emptyResolv(t *testing.T) string {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), "resolv.conf")
|
||||
if err := os.WriteFile(path, nil, 0o644); err != nil {
|
||||
t.Fatalf("WriteFile: %v", err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func newService(t *testing.T, opts Options) (*Service, *netnsRecorder, string) {
|
||||
t.Helper()
|
||||
rec := &netnsRecorder{}
|
||||
opts.DNSListen = "127.0.0.1:0"
|
||||
opts.NTPListen = "127.0.0.1:0"
|
||||
if opts.Rate == 0 {
|
||||
opts.Rate = 100
|
||||
}
|
||||
if opts.Resolv == "" {
|
||||
opts.Resolv = emptyResolv(t)
|
||||
}
|
||||
if opts.Clock == nil {
|
||||
opts.Clock = fakeClock{now: time.Unix(2000, 0), synced: true}
|
||||
}
|
||||
if opts.Timeout == 0 {
|
||||
opts.Timeout = 300 * time.Millisecond
|
||||
}
|
||||
opts.Enter = rec.enter
|
||||
if opts.Exists == nil {
|
||||
opts.Exists = func(string) bool { return true }
|
||||
}
|
||||
statePath := filepath.Join(t.TempDir(), "hostsvc.state")
|
||||
s, err := New(opts, statePath)
|
||||
if err != nil {
|
||||
t.Fatalf("New: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { s.Shutdown() })
|
||||
return s, rec, statePath
|
||||
}
|
||||
|
||||
func (s *Service) addrs(t *testing.T, vpc string) (dnsUDP, dnsTCP, ntp string) {
|
||||
t.Helper()
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
inst, ok := s.vpcs[vpc]
|
||||
if !ok {
|
||||
t.Fatalf("vpc %s is not open", vpc)
|
||||
}
|
||||
return inst.udpDNS.LocalAddr().String(), inst.tcpDNS.Addr().String(), inst.udpNTP.LocalAddr().String()
|
||||
}
|
||||
|
||||
func savedVPCs(t *testing.T, path string) []string {
|
||||
t.Helper()
|
||||
st, err := statefile.New[State](path).Load()
|
||||
if err != nil {
|
||||
t.Fatalf("load state: %v", err)
|
||||
}
|
||||
return st.VPCs
|
||||
}
|
||||
|
||||
func udpUpstream(t *testing.T, reply func(req []byte) [][]byte) (string, func() int) {
|
||||
t.Helper()
|
||||
conn, err := net.ListenPacket("udp4", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("ListenPacket: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { conn.Close() })
|
||||
var mu sync.Mutex
|
||||
count := 0
|
||||
go func() {
|
||||
buf := make([]byte, 65535)
|
||||
for {
|
||||
n, from, err := conn.ReadFrom(buf)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
mu.Lock()
|
||||
count++
|
||||
mu.Unlock()
|
||||
for _, r := range reply(append([]byte{}, buf[:n]...)) {
|
||||
conn.WriteTo(r, from)
|
||||
}
|
||||
}
|
||||
}()
|
||||
return conn.LocalAddr().String(), func() int {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
return count
|
||||
}
|
||||
}
|
||||
|
||||
func tcpUpstream(t *testing.T) string {
|
||||
t.Helper()
|
||||
l, err := net.Listen("tcp4", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("Listen: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { l.Close() })
|
||||
go func() {
|
||||
for {
|
||||
conn, err := l.Accept()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
defer conn.Close()
|
||||
q, err := readTCPMessage(conn)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
writeTCPMessage(conn, answerTo(q))
|
||||
}()
|
||||
}
|
||||
}()
|
||||
return l.Addr().String()
|
||||
}
|
||||
|
||||
func deadAddr(t *testing.T) string {
|
||||
t.Helper()
|
||||
conn, err := net.ListenPacket("udp4", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("ListenPacket: %v", err)
|
||||
}
|
||||
addr := conn.LocalAddr().String()
|
||||
conn.Close()
|
||||
return addr
|
||||
}
|
||||
|
||||
func ask(t *testing.T, addr string, msg []byte, wait time.Duration) ([]byte, error) {
|
||||
t.Helper()
|
||||
conn, err := net.Dial("udp4", addr)
|
||||
if err != nil {
|
||||
t.Fatalf("Dial: %v", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
conn.SetDeadline(time.Now().Add(wait))
|
||||
if _, err := conn.Write(msg); err != nil {
|
||||
t.Fatalf("Write: %v", err)
|
||||
}
|
||||
buf := make([]byte, 65535)
|
||||
n, err := conn.Read(buf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return buf[:n], nil
|
||||
}
|
||||
|
||||
func TestNew_RefusesANullRate(t *testing.T) {
|
||||
if _, err := New(Options{Rate: 0}, filepath.Join(t.TempDir(), "s")); err == nil || !strings.Contains(err.Error(), "rate limit 0") {
|
||||
t.Errorf("error %v: a null rate would silently drop every request", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpen_EntersTheVPCNetnsAndSavesIt(t *testing.T) {
|
||||
s, rec, statePath := newService(t, Options{})
|
||||
if err := s.Open("vpc-00001"); err != nil {
|
||||
t.Fatalf("Open: %v", err)
|
||||
}
|
||||
if got := rec.calls(); !reflect.DeepEqual(got, []string{"vpc-00001"}) {
|
||||
t.Errorf("netns entered = %v, want the vpc's", got)
|
||||
}
|
||||
if got := savedVPCs(t, statePath); !reflect.DeepEqual(got, []string{"vpc-00001"}) {
|
||||
t.Errorf("state = %v", got)
|
||||
}
|
||||
if got := s.List(); !reflect.DeepEqual(got, []string{"vpc-00001"}) {
|
||||
t.Errorf("List = %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpen_IsIdempotent(t *testing.T) {
|
||||
s, rec, _ := newService(t, Options{})
|
||||
for i := 0; i < 2; i++ {
|
||||
if err := s.Open("vpc-00001"); err != nil {
|
||||
t.Fatalf("Open %d: %v", i+1, err)
|
||||
}
|
||||
}
|
||||
if got := rec.calls(); len(got) != 1 {
|
||||
t.Errorf("netns entered %d times, want once: a second open must not bind again", len(got))
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpen_InvalidNamesAreRefusedWithoutEnteringAnything(t *testing.T) {
|
||||
s, rec, statePath := newService(t, Options{})
|
||||
for _, name := range []string{"", ".", "..", "../etc", "a/b", "x\x00y"} {
|
||||
if err := s.Open(name); err == nil || !strings.Contains(err.Error(), "invalid vpc name") {
|
||||
t.Errorf("Open(%q): error %v", name, err)
|
||||
}
|
||||
if err := s.Close(name); err == nil || !strings.Contains(err.Error(), "invalid vpc name") {
|
||||
t.Errorf("Close(%q): error %v", name, err)
|
||||
}
|
||||
}
|
||||
if got := rec.calls(); len(got) != 0 {
|
||||
t.Errorf("netns entered for an invalid name: %v", got)
|
||||
}
|
||||
if got := savedVPCs(t, statePath); len(got) != 0 {
|
||||
t.Errorf("state = %v, want empty", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpen_FailureLeavesNothingBehind(t *testing.T) {
|
||||
s, rec, statePath := newService(t, Options{})
|
||||
rec.fail = errors.New("boom")
|
||||
if err := s.Open("vpc-00001"); err == nil || !strings.Contains(err.Error(), "open vpc vpc-00001: boom") {
|
||||
t.Fatalf("error %v", err)
|
||||
}
|
||||
if got := s.List(); len(got) != 0 {
|
||||
t.Errorf("List = %v, want empty after a failed open", got)
|
||||
}
|
||||
if got := savedVPCs(t, statePath); len(got) != 0 {
|
||||
t.Errorf("state = %v, want empty", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClose_StopsServingAndForgetsTheVPC(t *testing.T) {
|
||||
up, _ := udpUpstream(t, func(q []byte) [][]byte { return [][]byte{answerTo(q)} })
|
||||
s, _, statePath := newService(t, Options{DNS: []string{up}})
|
||||
if err := s.Open("vpc-00001"); err != nil {
|
||||
t.Fatalf("Open: %v", err)
|
||||
}
|
||||
dns, _, _ := s.addrs(t, "vpc-00001")
|
||||
if _, err := ask(t, dns, query(1), time.Second); err != nil {
|
||||
t.Fatalf("witness: the open vpc must answer: %v", err)
|
||||
}
|
||||
if err := s.Close("vpc-00001"); err != nil {
|
||||
t.Fatalf("Close: %v", err)
|
||||
}
|
||||
if _, err := ask(t, dns, query(2), 300*time.Millisecond); err == nil {
|
||||
t.Error("a closed vpc still answers")
|
||||
}
|
||||
if got := savedVPCs(t, statePath); len(got) != 0 {
|
||||
t.Errorf("state = %v, want empty", got)
|
||||
}
|
||||
if err := s.Close("vpc-00001"); err != nil {
|
||||
t.Errorf("closing twice must succeed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRestore_ReopensOnlyTheVPCsWhoseNetnsExists(t *testing.T) {
|
||||
statePath := filepath.Join(t.TempDir(), "hostsvc.state")
|
||||
if err := statefile.New[State](statePath).Save(State{VPCs: []string{"vpc-gone", "vpc-here"}}); err != nil {
|
||||
t.Fatalf("Save: %v", err)
|
||||
}
|
||||
rec := &netnsRecorder{}
|
||||
s, err := New(Options{
|
||||
Rate: 10, DNSListen: "127.0.0.1:0", NTPListen: "127.0.0.1:0", Resolv: emptyResolv(t),
|
||||
Enter: rec.enter,
|
||||
Exists: func(vpc string) bool { return vpc == "vpc-here" },
|
||||
}, statePath)
|
||||
if err != nil {
|
||||
t.Fatalf("New: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { s.Shutdown() })
|
||||
if err := s.Restore(); err != nil {
|
||||
t.Fatalf("Restore: %v", err)
|
||||
}
|
||||
if got := rec.calls(); !reflect.DeepEqual(got, []string{"vpc-here"}) {
|
||||
t.Errorf("netns entered = %v, want only vpc-here", got)
|
||||
}
|
||||
if got := savedVPCs(t, statePath); !reflect.DeepEqual(got, []string{"vpc-here"}) {
|
||||
t.Errorf("state = %v, the vanished vpc must be dropped", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDNS_UDPQueryIsRelayedToTheUpstream(t *testing.T) {
|
||||
var got []byte
|
||||
var mu sync.Mutex
|
||||
up, _ := udpUpstream(t, func(q []byte) [][]byte {
|
||||
mu.Lock()
|
||||
got = q
|
||||
mu.Unlock()
|
||||
return [][]byte{answerTo(q)}
|
||||
})
|
||||
s, _, _ := newService(t, Options{DNS: []string{up}})
|
||||
if err := s.Open("vpc-00001"); err != nil {
|
||||
t.Fatalf("Open: %v", err)
|
||||
}
|
||||
dns, _, _ := s.addrs(t, "vpc-00001")
|
||||
reply, err := ask(t, dns, query(0x4242), time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("no answer: %v", err)
|
||||
}
|
||||
if !bytes.Equal(reply, answerTo(query(0x4242))) {
|
||||
t.Errorf("reply = %x, want the upstream's answer untouched", reply)
|
||||
}
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if !bytes.Equal(got, query(0x4242)) {
|
||||
t.Errorf("upstream received %x, want the query untouched", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDNS_SpoofedAnswerIsIgnored(t *testing.T) {
|
||||
up, _ := udpUpstream(t, func(q []byte) [][]byte {
|
||||
bad := answerTo(q)
|
||||
bad[0] ^= 0xff
|
||||
return [][]byte{bad, answerTo(q)}
|
||||
})
|
||||
s, _, _ := newService(t, Options{DNS: []string{up}})
|
||||
if err := s.Open("vpc-00001"); err != nil {
|
||||
t.Fatalf("Open: %v", err)
|
||||
}
|
||||
dns, _, _ := s.addrs(t, "vpc-00001")
|
||||
reply, err := ask(t, dns, query(0x1000), time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("no answer: %v", err)
|
||||
}
|
||||
if binary.BigEndian.Uint16(reply[0:2]) != 0x1000 {
|
||||
t.Errorf("reply id = %#x, the answer with a foreign id must be skipped", binary.BigEndian.Uint16(reply[0:2]))
|
||||
}
|
||||
}
|
||||
|
||||
func TestDNS_NextUpstreamWhenTheFirstIsDown(t *testing.T) {
|
||||
up, _ := udpUpstream(t, func(q []byte) [][]byte { return [][]byte{answerTo(q)} })
|
||||
s, _, _ := newService(t, Options{DNS: []string{deadAddr(t), up}})
|
||||
if err := s.Open("vpc-00001"); err != nil {
|
||||
t.Fatalf("Open: %v", err)
|
||||
}
|
||||
dns, _, _ := s.addrs(t, "vpc-00001")
|
||||
if _, err := ask(t, dns, query(3), 2*time.Second); err != nil {
|
||||
t.Fatalf("the second upstream must answer: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDNS_HostResolverComesAfterTheConfiguredUpstreams(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "resolv.conf")
|
||||
if err := os.WriteFile(path, []byte("nameserver 192.0.2.1\n"), 0o644); err != nil {
|
||||
t.Fatalf("WriteFile: %v", err)
|
||||
}
|
||||
s, _, _ := newService(t, Options{DNS: []string{"198.51.100.1:53"}, Resolv: path})
|
||||
if got, want := s.dnsServers(), []string{"198.51.100.1:53", "192.0.2.1:53"}; !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("dnsServers = %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDNS_InvalidQueryGetsNoAnswerAndIsNotRelayed(t *testing.T) {
|
||||
up, count := udpUpstream(t, func(q []byte) [][]byte { return [][]byte{answerTo(q)} })
|
||||
s, _, _ := newService(t, Options{DNS: []string{up}})
|
||||
if err := s.Open("vpc-00001"); err != nil {
|
||||
t.Fatalf("Open: %v", err)
|
||||
}
|
||||
dns, _, _ := s.addrs(t, "vpc-00001")
|
||||
if _, err := ask(t, dns, answerTo(query(5)), 300*time.Millisecond); err == nil {
|
||||
t.Error("a response sent to the relay must not be answered")
|
||||
}
|
||||
if _, err := ask(t, dns, query(6), time.Second); err != nil {
|
||||
t.Fatalf("witness: a valid query must be answered: %v", err)
|
||||
}
|
||||
if n := count(); n != 1 {
|
||||
t.Errorf("upstream saw %d messages, want only the valid one", n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDNS_TCPQueryIsRelayed(t *testing.T) {
|
||||
s, _, _ := newService(t, Options{DNS: []string{tcpUpstream(t)}})
|
||||
if err := s.Open("vpc-00001"); err != nil {
|
||||
t.Fatalf("Open: %v", err)
|
||||
}
|
||||
_, tcp, _ := s.addrs(t, "vpc-00001")
|
||||
conn, err := net.Dial("tcp4", tcp)
|
||||
if err != nil {
|
||||
t.Fatalf("Dial: %v", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
conn.SetDeadline(time.Now().Add(2 * time.Second))
|
||||
for id := uint16(1); id <= 2; id++ {
|
||||
if err := writeTCPMessage(conn, query(id)); err != nil {
|
||||
t.Fatalf("write: %v", err)
|
||||
}
|
||||
reply, err := readTCPMessage(conn)
|
||||
if err != nil {
|
||||
t.Fatalf("query %d: no answer: %v", id, err)
|
||||
}
|
||||
if !bytes.Equal(reply, answerTo(query(id))) {
|
||||
t.Errorf("query %d: reply %x", id, reply)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDNS_RateLimitPerVPC(t *testing.T) {
|
||||
up, _ := udpUpstream(t, func(q []byte) [][]byte { return [][]byte{answerTo(q)} })
|
||||
s, _, _ := newService(t, Options{DNS: []string{up}, Rate: 2})
|
||||
for _, vpc := range []string{"vpc-00001", "vpc-00002"} {
|
||||
if err := s.Open(vpc); err != nil {
|
||||
t.Fatalf("Open %s: %v", vpc, err)
|
||||
}
|
||||
}
|
||||
dns1, _, _ := s.addrs(t, "vpc-00001")
|
||||
dns2, _, _ := s.addrs(t, "vpc-00002")
|
||||
for i := 0; i < 2; i++ {
|
||||
if _, err := ask(t, dns1, query(uint16(i)), time.Second); err != nil {
|
||||
t.Fatalf("query %d within the burst: %v", i+1, err)
|
||||
}
|
||||
}
|
||||
if _, err := ask(t, dns1, query(9), 200*time.Millisecond); err == nil {
|
||||
t.Error("the third query in a row must be dropped at 2 per second")
|
||||
}
|
||||
if _, err := ask(t, dns2, query(10), time.Second); err != nil {
|
||||
t.Errorf("another vpc has its own bucket: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNTP_HostClockWithoutUpstream(t *testing.T) {
|
||||
clock := fakeClock{now: time.Unix(5000, 0), synced: true, maxError: time.Second}
|
||||
s, _, _ := newService(t, Options{Clock: clock})
|
||||
if err := s.Open("vpc-00001"); err != nil {
|
||||
t.Fatalf("Open: %v", err)
|
||||
}
|
||||
_, _, ntp := s.addrs(t, "vpc-00001")
|
||||
req := ntpRequest(4, 0xabcdef)
|
||||
reply, err := ask(t, ntp, req, time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("no answer: %v", err)
|
||||
}
|
||||
if err := validNTPAnswer(req, reply); err != nil {
|
||||
t.Fatalf("invalid answer: %v", err)
|
||||
}
|
||||
if got := binary.BigEndian.Uint64(reply[40:48]); got != ntpTimestamp(clock.now) {
|
||||
t.Errorf("transmit = %#x, want the host clock %#x", got, ntpTimestamp(clock.now))
|
||||
}
|
||||
if reply[0]>>6 != 0 || reply[1] != 3 {
|
||||
t.Errorf("li %d stratum %d, want 0 and 3 for a synced host", reply[0]>>6, reply[1])
|
||||
}
|
||||
}
|
||||
|
||||
func TestNTP_UnreadableClockStatusIsAnsweredAsUnsynced(t *testing.T) {
|
||||
s, _, _ := newService(t, Options{Clock: fakeClock{now: time.Unix(5000, 0), synced: true, err: errors.New("eperm")}})
|
||||
if err := s.Open("vpc-00001"); err != nil {
|
||||
t.Fatalf("Open: %v", err)
|
||||
}
|
||||
_, _, ntp := s.addrs(t, "vpc-00001")
|
||||
reply, err := ask(t, ntp, ntpRequest(4, 1), time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("no answer: %v", err)
|
||||
}
|
||||
if reply[0]>>6 != 3 {
|
||||
t.Errorf("li = %d, want 3 when the clock status cannot be read", reply[0]>>6)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNTP_UpstreamIsPreferredAndChecked(t *testing.T) {
|
||||
up, _ := udpUpstream(t, func(req []byte) [][]byte {
|
||||
spoofed := hostReply(ntpRequest(4, 0), time.Unix(0, 0), time.Unix(0, 0), true, 0)
|
||||
real := hostReply(req, time.Unix(7000, 0), time.Unix(7000, 0), true, 0)
|
||||
real[1] = 1
|
||||
return [][]byte{spoofed, real}
|
||||
})
|
||||
s, _, _ := newService(t, Options{NTP: []string{up}})
|
||||
if err := s.Open("vpc-00001"); err != nil {
|
||||
t.Fatalf("Open: %v", err)
|
||||
}
|
||||
_, _, ntp := s.addrs(t, "vpc-00001")
|
||||
req := ntpRequest(4, 0x55)
|
||||
reply, err := ask(t, ntp, req, time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("no answer: %v", err)
|
||||
}
|
||||
if reply[1] != 1 {
|
||||
t.Errorf("stratum = %d, want the upstream's 1 (spoofed answer skipped, host clock unused)", reply[1])
|
||||
}
|
||||
}
|
||||
|
||||
func TestNTP_HostClockWhenUpstreamsFail(t *testing.T) {
|
||||
clock := fakeClock{now: time.Unix(6000, 0), synced: true}
|
||||
s, _, _ := newService(t, Options{NTP: []string{deadAddr(t)}, Clock: clock})
|
||||
if err := s.Open("vpc-00001"); err != nil {
|
||||
t.Fatalf("Open: %v", err)
|
||||
}
|
||||
_, _, ntp := s.addrs(t, "vpc-00001")
|
||||
reply, err := ask(t, ntp, ntpRequest(4, 2), 2*time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("no answer: %v", err)
|
||||
}
|
||||
if got := binary.BigEndian.Uint64(reply[40:48]); got != ntpTimestamp(clock.now) {
|
||||
t.Errorf("transmit = %#x, want the host clock as fallback", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNTP_InvalidRequestGetsNoAnswer(t *testing.T) {
|
||||
s, _, _ := newService(t, Options{})
|
||||
if err := s.Open("vpc-00001"); err != nil {
|
||||
t.Fatalf("Open: %v", err)
|
||||
}
|
||||
_, _, ntp := s.addrs(t, "vpc-00001")
|
||||
if _, err := ask(t, ntp, []byte("not ntp"), 300*time.Millisecond); err == nil {
|
||||
t.Error("a short packet must not be answered")
|
||||
}
|
||||
}
|
||||
|
|
@ -1,60 +0,0 @@
|
|||
package iptables
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
const HostSvcChain = "TWO-HOSTSVC"
|
||||
|
||||
var hostSvcExcluded = []string{"br-public", "l3-+"}
|
||||
|
||||
func hostSvcJump() []string {
|
||||
return []string{"PREROUTING", "-m", "addrtype", "--dst-type", "LOCAL", "-j", HostSvcChain}
|
||||
}
|
||||
|
||||
func hostSvcRules(target string) [][]string {
|
||||
var rules [][]string
|
||||
for _, iface := range hostSvcExcluded {
|
||||
rules = append(rules, []string{HostSvcChain, "-i", iface, "-j", "RETURN"})
|
||||
}
|
||||
for _, p := range []struct{ proto, port string }{{"udp", "53"}, {"tcp", "53"}, {"udp", "123"}} {
|
||||
rules = append(rules, []string{HostSvcChain,
|
||||
"-p", p.proto, "-m", p.proto, "--dport", p.port,
|
||||
"-j", "DNAT", "--to-destination", target,
|
||||
})
|
||||
}
|
||||
return rules
|
||||
}
|
||||
|
||||
func nat(args ...string) *exec.Cmd {
|
||||
return exec.Command("iptables", append([]string{"-t", "nat"}, args...)...)
|
||||
}
|
||||
|
||||
func AddHostSvcRedirect(target string) error {
|
||||
if out, err := nat("-N", HostSvcChain).CombinedOutput(); err != nil {
|
||||
return fmt.Errorf("iptables create chain %s: %w: %s", HostSvcChain, err, out)
|
||||
}
|
||||
for _, rule := range append(hostSvcRules(target), hostSvcJump()) {
|
||||
if out, err := nat(append([]string{"-A"}, rule...)...).CombinedOutput(); err != nil {
|
||||
return fmt.Errorf("iptables hostsvc redirect %v: %w: %s", rule, err, out)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func HasHostSvcRedirect(target string) (bool, error) {
|
||||
for _, rule := range append(hostSvcRules(target), hostSvcJump()) {
|
||||
err := nat(append([]string{"-C"}, rule...)...).Run()
|
||||
var exit *exec.ExitError
|
||||
switch {
|
||||
case err == nil:
|
||||
case errors.As(err, &exit) && exit.ExitCode() == 1:
|
||||
return false, nil
|
||||
default:
|
||||
return false, fmt.Errorf("iptables check %v: %w", rule, err)
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
|
@ -1,48 +0,0 @@
|
|||
package iptables
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHostSvcJump_OnlyLocalDestinations(t *testing.T) {
|
||||
want := []string{"PREROUTING", "-m", "addrtype", "--dst-type", "LOCAL", "-j", "TWO-HOSTSVC"}
|
||||
if got := hostSvcJump(); !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("jump = %v, want %v: traffic to an outside resolver must not be hijacked", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHostSvcRules_ExclusionsComeFirst(t *testing.T) {
|
||||
rules := hostSvcRules("169.254.169.253")
|
||||
if len(rules) != 5 {
|
||||
t.Fatalf("%d rules, want 2 exclusions and 3 redirections: %v", len(rules), rules)
|
||||
}
|
||||
for i, iface := range []string{"br-public", "l3-+"} {
|
||||
want := []string{"TWO-HOSTSVC", "-i", iface, "-j", "RETURN"}
|
||||
if !reflect.DeepEqual(rules[i], want) {
|
||||
t.Errorf("rule %d = %v, want %v: the public segment and the vrf link must never reach the relay", i, rules[i], want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHostSvcRules_RedirectDNSAndNTPOnly(t *testing.T) {
|
||||
var got []string
|
||||
for _, rule := range hostSvcRules("169.254.169.253") {
|
||||
if rule[len(rule)-3] != "DNAT" {
|
||||
continue
|
||||
}
|
||||
if rule[len(rule)-1] != "169.254.169.253" {
|
||||
t.Errorf("rule %v does not target 169.254.169.253", rule)
|
||||
}
|
||||
got = append(got, strings.Join(rule[1:7], " "))
|
||||
}
|
||||
want := []string{
|
||||
"-p udp -m udp --dport 53",
|
||||
"-p tcp -m tcp --dport 53",
|
||||
"-p udp -m udp --dport 123",
|
||||
}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("redirections = %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,13 +0,0 @@
|
|||
package netif
|
||||
|
||||
import (
|
||||
"github.com/vishvananda/netlink"
|
||||
)
|
||||
|
||||
func CreateDummy(name string) error {
|
||||
dummy := &netlink.Dummy{LinkAttrs: netlink.LinkAttrs{Name: name}}
|
||||
if err := netlink.LinkAdd(dummy); err != nil {
|
||||
return err
|
||||
}
|
||||
return netlink.LinkSetUp(dummy)
|
||||
}
|
||||
|
|
@ -46,21 +46,3 @@ func LinkMTU(name string) (int, error) {
|
|||
}
|
||||
return link.Attrs().MTU, nil
|
||||
}
|
||||
|
||||
func BridgeVxlanPorts(bridge string) ([]string, error) {
|
||||
br, err := netlink.LinkByName(bridge)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
links, err := netlink.LinkList()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var ports []string
|
||||
for _, l := range links {
|
||||
if l.Attrs().MasterIndex == br.Attrs().Index && l.Type() == "vxlan" {
|
||||
ports = append(ports, l.Attrs().Name)
|
||||
}
|
||||
}
|
||||
return ports, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,384 +0,0 @@
|
|||
package nft
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const Table = "two"
|
||||
|
||||
type hook struct {
|
||||
name string
|
||||
chain string
|
||||
prefix string
|
||||
ports string
|
||||
key string
|
||||
priority string
|
||||
}
|
||||
|
||||
var hooks = []hook{
|
||||
{"prerouting", "prerouting", "pre-", "pre_ports", "iifname", "dstnat"},
|
||||
{"forward", "forward", "fwd-", "fwd_ports", "oifname", "filter"},
|
||||
{"output", "output", "out-", "out_ports", "oifname", "filter"},
|
||||
}
|
||||
|
||||
type Port struct {
|
||||
Name string
|
||||
Prerouting []string
|
||||
Forward []string
|
||||
Output []string
|
||||
}
|
||||
|
||||
func (p Port) rules(h hook) []string {
|
||||
switch h.name {
|
||||
case "prerouting":
|
||||
return p.Prerouting
|
||||
case "forward":
|
||||
return p.Forward
|
||||
default:
|
||||
return p.Output
|
||||
}
|
||||
}
|
||||
|
||||
func DropSourceMAC(mac string) string {
|
||||
return "ether saddr " + mac + " drop"
|
||||
}
|
||||
|
||||
func DropARPRequestTo(ip string) string {
|
||||
return "arp operation request arp daddr ip " + ip + " drop"
|
||||
}
|
||||
|
||||
func DropARPFrom(ip string) string {
|
||||
return "arp saddr ip " + ip + " drop"
|
||||
}
|
||||
|
||||
func DropDHCPFrom(ip string) string {
|
||||
return "ip saddr " + ip + " udp sport 67-68 udp dport 67-68 drop"
|
||||
}
|
||||
|
||||
func DropSourceMACOtherThan(mac string) string {
|
||||
return "ether saddr != " + mac + " drop"
|
||||
}
|
||||
|
||||
func DropEtherTypesOtherThanIPAndARP() string {
|
||||
return "ether type != { ip, arp, ip6 } drop"
|
||||
}
|
||||
|
||||
func DropARPSenderMACOtherThan(mac string) string {
|
||||
return "arp saddr ether != " + mac + " drop"
|
||||
}
|
||||
|
||||
func DropARPSenderIPOtherThan(ip string) string {
|
||||
return "arp saddr ip != { 0.0.0.0, " + ip + " } drop"
|
||||
}
|
||||
|
||||
func DropDHCPServer() string {
|
||||
return "udp sport 67 drop"
|
||||
}
|
||||
|
||||
func DropIPSourceOtherThan(ip string) string {
|
||||
return "ip saddr != { 0.0.0.0, " + ip + " } drop"
|
||||
}
|
||||
|
||||
func DropUnspecifiedSourceOutsideDHCP() []string {
|
||||
return []string{
|
||||
"ip saddr 0.0.0.0 ip protocol != udp drop",
|
||||
"ip saddr 0.0.0.0 udp dport != 67 drop",
|
||||
}
|
||||
}
|
||||
|
||||
func run(args []string, script string) ([]byte, error) {
|
||||
cmd := exec.Command("nft", args...)
|
||||
if script != "" {
|
||||
cmd.Stdin = strings.NewReader(script)
|
||||
}
|
||||
var stderr bytes.Buffer
|
||||
cmd.Stderr = &stderr
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("nft %s: %w: %s", strings.Join(args, " "), err, strings.TrimSpace(stderr.String()))
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func apply(script string) error {
|
||||
_, err := run([]string{"-f", "-"}, script)
|
||||
return err
|
||||
}
|
||||
|
||||
func baseScript() string {
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "add table bridge %s\n", Table)
|
||||
for _, h := range hooks {
|
||||
fmt.Fprintf(&b, "add map bridge %s %s { type ifname : verdict; }\n", Table, h.ports)
|
||||
}
|
||||
for _, h := range hooks {
|
||||
fmt.Fprintf(&b, "add chain bridge %s %s { type filter hook %s priority %s; policy accept; }\n", Table, h.chain, h.name, h.priority)
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func setScript(p Port, base table) string {
|
||||
var b strings.Builder
|
||||
b.WriteString(baseScript())
|
||||
for _, h := range hooks {
|
||||
if !base.hasDispatch(h) {
|
||||
fmt.Fprintf(&b, "add rule bridge %s %s %s vmap @%s\n", Table, h.chain, h.key, h.ports)
|
||||
}
|
||||
}
|
||||
for _, h := range hooks {
|
||||
rules := p.rules(h)
|
||||
chain := h.prefix + p.Name
|
||||
if len(rules) == 0 {
|
||||
if _, ok := base.elements[h.ports][p.Name]; ok {
|
||||
fmt.Fprintf(&b, "delete element bridge %s %s { %q }\n", Table, h.ports, p.Name)
|
||||
}
|
||||
if base.chains[chain] {
|
||||
fmt.Fprintf(&b, "delete chain bridge %s %s\n", Table, chain)
|
||||
}
|
||||
continue
|
||||
}
|
||||
fmt.Fprintf(&b, "add chain bridge %s %s\n", Table, chain)
|
||||
fmt.Fprintf(&b, "flush chain bridge %s %s\n", Table, chain)
|
||||
for _, r := range rules {
|
||||
fmt.Fprintf(&b, "add rule bridge %s %s %s\n", Table, chain, r)
|
||||
}
|
||||
target, ok := base.elements[h.ports][p.Name]
|
||||
if ok && target != chain {
|
||||
fmt.Fprintf(&b, "delete element bridge %s %s { %q }\n", Table, h.ports, p.Name)
|
||||
}
|
||||
if target != chain {
|
||||
fmt.Fprintf(&b, "add element bridge %s %s { %q : jump %s }\n", Table, h.ports, p.Name, chain)
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func deleteScript(name string, t table) string {
|
||||
var b strings.Builder
|
||||
for _, h := range hooks {
|
||||
if _, ok := t.elements[h.ports][name]; ok {
|
||||
fmt.Fprintf(&b, "delete element bridge %s %s { %q }\n", Table, h.ports, name)
|
||||
}
|
||||
}
|
||||
for _, h := range hooks {
|
||||
if chain := h.prefix + name; t.chains[chain] {
|
||||
fmt.Fprintf(&b, "delete chain bridge %s %s\n", Table, chain)
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func SetPort(p Port) error {
|
||||
t, err := readTable()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := apply(setScript(p, t)); err != nil {
|
||||
return fmt.Errorf("set port %s: %w", p.Name, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DeletePort(name string) error {
|
||||
t, err := readTable()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
script := deleteScript(name, t)
|
||||
if script == "" {
|
||||
return nil
|
||||
}
|
||||
if err := apply(script); err != nil {
|
||||
return fmt.Errorf("delete port %s: %w", name, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func MissingRules(p Port) ([]string, error) {
|
||||
t, err := readTable()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
listings := map[string][]string{}
|
||||
for _, h := range hooks {
|
||||
chain := h.prefix + p.Name
|
||||
if len(p.rules(h)) == 0 || !t.chains[chain] {
|
||||
continue
|
||||
}
|
||||
out, err := run([]string{"list", "chain", "bridge", Table, chain}, "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
listings[chain] = chainRules(string(out))
|
||||
}
|
||||
return missing(p, t, listings), nil
|
||||
}
|
||||
|
||||
func missing(p Port, t table, listings map[string][]string) []string {
|
||||
var out []string
|
||||
for _, h := range hooks {
|
||||
rules := p.rules(h)
|
||||
if len(rules) == 0 {
|
||||
continue
|
||||
}
|
||||
chain := h.prefix + p.Name
|
||||
if !t.hasDispatch(h) {
|
||||
out = append(out, fmt.Sprintf("%s: %s vmap @%s", h.chain, h.key, h.ports))
|
||||
}
|
||||
if t.elements[h.ports][p.Name] != chain {
|
||||
out = append(out, fmt.Sprintf("%s: %q : jump %s", h.ports, p.Name, chain))
|
||||
}
|
||||
want := map[string]bool{}
|
||||
for _, r := range rules {
|
||||
want[r] = true
|
||||
}
|
||||
have := map[string]bool{}
|
||||
for _, r := range listings[chain] {
|
||||
have[r] = true
|
||||
if !want[r] {
|
||||
out = append(out, chain+": unexpected "+r)
|
||||
}
|
||||
}
|
||||
for _, r := range rules {
|
||||
if !have[r] {
|
||||
out = append(out, chain+": "+r)
|
||||
}
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func chainRules(listing string) []string {
|
||||
var rules []string
|
||||
depth := 0
|
||||
for _, line := range strings.Split(listing, "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
switch {
|
||||
case strings.HasSuffix(line, "{"):
|
||||
depth++
|
||||
case line == "}":
|
||||
depth--
|
||||
case depth == 2 && line != "":
|
||||
rules = append(rules, line)
|
||||
}
|
||||
}
|
||||
return rules
|
||||
}
|
||||
|
||||
type table struct {
|
||||
exists bool
|
||||
chains map[string]bool
|
||||
elements map[string]map[string]string
|
||||
dispatch map[string]string
|
||||
}
|
||||
|
||||
func (t table) hasDispatch(h hook) bool {
|
||||
return t.dispatch[h.chain] == h.key+" @"+h.ports
|
||||
}
|
||||
|
||||
type listing struct {
|
||||
Nftables []struct {
|
||||
Table *struct {
|
||||
Family string `json:"family"`
|
||||
Name string `json:"name"`
|
||||
} `json:"table"`
|
||||
Chain *struct {
|
||||
Name string `json:"name"`
|
||||
} `json:"chain"`
|
||||
Map *struct {
|
||||
Name string `json:"name"`
|
||||
Elem []json.RawMessage `json:"elem"`
|
||||
} `json:"map"`
|
||||
Rule *struct {
|
||||
Chain string `json:"chain"`
|
||||
Expr []struct {
|
||||
Vmap *struct {
|
||||
Key struct {
|
||||
Meta struct {
|
||||
Key string `json:"key"`
|
||||
} `json:"meta"`
|
||||
} `json:"key"`
|
||||
Data string `json:"data"`
|
||||
} `json:"vmap"`
|
||||
} `json:"expr"`
|
||||
} `json:"rule"`
|
||||
} `json:"nftables"`
|
||||
}
|
||||
|
||||
func readTable() (table, error) {
|
||||
out, err := run([]string{"-j", "list", "tables", "bridge"}, "")
|
||||
if err != nil {
|
||||
return table{}, err
|
||||
}
|
||||
tables, err := parseListing(out)
|
||||
if err != nil || !tables.exists {
|
||||
return tables, err
|
||||
}
|
||||
out, err = run([]string{"-j", "list", "table", "bridge", Table}, "")
|
||||
if err != nil {
|
||||
return table{}, err
|
||||
}
|
||||
return parseListing(out)
|
||||
}
|
||||
|
||||
func parseListing(data []byte) (table, error) {
|
||||
var l listing
|
||||
if err := json.Unmarshal(data, &l); err != nil {
|
||||
return table{}, fmt.Errorf("nft json: %w", err)
|
||||
}
|
||||
t := table{chains: map[string]bool{}, elements: map[string]map[string]string{}, dispatch: map[string]string{}}
|
||||
for _, o := range l.Nftables {
|
||||
switch {
|
||||
case o.Table != nil:
|
||||
if o.Table.Family == "bridge" && o.Table.Name == Table {
|
||||
t.exists = true
|
||||
}
|
||||
case o.Chain != nil:
|
||||
t.chains[o.Chain.Name] = true
|
||||
case o.Map != nil:
|
||||
elems := map[string]string{}
|
||||
for _, raw := range o.Map.Elem {
|
||||
name, target, err := parseElem(raw)
|
||||
if err != nil {
|
||||
return table{}, fmt.Errorf("nft json: map %s: %w", o.Map.Name, err)
|
||||
}
|
||||
elems[name] = target
|
||||
}
|
||||
t.elements[o.Map.Name] = elems
|
||||
case o.Rule != nil:
|
||||
for _, e := range o.Rule.Expr {
|
||||
if e.Vmap != nil {
|
||||
t.dispatch[o.Rule.Chain] = e.Vmap.Key.Meta.Key + " " + e.Vmap.Data
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
|
||||
func parseElem(raw json.RawMessage) (string, string, error) {
|
||||
var pair []json.RawMessage
|
||||
if err := json.Unmarshal(raw, &pair); err != nil || len(pair) != 2 {
|
||||
return "", "", fmt.Errorf("element %s: not a key and verdict pair", raw)
|
||||
}
|
||||
var name string
|
||||
if err := json.Unmarshal(pair[0], &name); err != nil {
|
||||
return "", "", fmt.Errorf("element %s: key: %w", raw, err)
|
||||
}
|
||||
var verdict struct {
|
||||
Jump *struct {
|
||||
Target string `json:"target"`
|
||||
} `json:"jump"`
|
||||
}
|
||||
if err := json.Unmarshal(pair[1], &verdict); err != nil {
|
||||
return "", "", fmt.Errorf("element %s: verdict: %w", raw, err)
|
||||
}
|
||||
if verdict.Jump == nil {
|
||||
return name, "", nil
|
||||
}
|
||||
return name, verdict.Jump.Target, nil
|
||||
}
|
||||
|
|
@ -1,230 +0,0 @@
|
|||
package nft
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const listedTable = `{"nftables": [{"metainfo": {"version": "1.0.6", "release_name": "Lester Gooch #5", "json_schema_version": 1}}, {"table": {"family": "bridge", "name": "two", "handle": 1}}, {"map": {"family": "bridge", "name": "pre_ports", "table": "two", "type": "ifname", "handle": 1, "map": "verdict"}}, {"map": {"family": "bridge", "name": "fwd_ports", "table": "two", "type": "ifname", "handle": 2, "map": "verdict", "elem": [["v-t33a-i", {"jump": {"target": "fwd-v-t33a-i"}}], ["v-other-i", {"jump": {"target": "fwd-v-other-i"}}]]}}, {"map": {"family": "bridge", "name": "out_ports", "table": "two", "type": "ifname", "handle": 3, "map": "verdict", "elem": [["v-t33a-i", {"jump": {"target": "out-v-t33a-i"}}]]}}, {"chain": {"family": "bridge", "table": "two", "name": "prerouting", "handle": 4, "type": "filter", "hook": "prerouting", "prio": -300, "policy": "accept"}}, {"chain": {"family": "bridge", "table": "two", "name": "forward", "handle": 5, "type": "filter", "hook": "forward", "prio": -200, "policy": "accept"}}, {"chain": {"family": "bridge", "table": "two", "name": "output", "handle": 6, "type": "filter", "hook": "output", "prio": -200, "policy": "accept"}}, {"chain": {"family": "bridge", "table": "two", "name": "fwd-v-t33a-i", "handle": 10}}, {"chain": {"family": "bridge", "table": "two", "name": "out-v-t33a-i", "handle": 11}}, {"chain": {"family": "bridge", "table": "two", "name": "fwd-v-other-i", "handle": 12}}, {"rule": {"family": "bridge", "table": "two", "chain": "prerouting", "handle": 7, "expr": [{"vmap": {"key": {"meta": {"key": "iifname"}}, "data": "@pre_ports"}}]}}, {"rule": {"family": "bridge", "table": "two", "chain": "forward", "handle": 8, "expr": [{"vmap": {"key": {"meta": {"key": "oifname"}}, "data": "@fwd_ports"}}]}}, {"rule": {"family": "bridge", "table": "two", "chain": "output", "handle": 9, "expr": [{"vmap": {"key": {"meta": {"key": "oifname"}}, "data": "@out_ports"}}]}}]}`
|
||||
|
||||
const noTable = `{"nftables": [{"metainfo": {"version": "1.0.6", "release_name": "Lester Gooch #5", "json_schema_version": 1}}]}`
|
||||
|
||||
const listedChain = `table bridge two {
|
||||
chain fwd-v-t33a-i {
|
||||
arp operation request arp daddr ip 10.33.0.1 drop
|
||||
ip saddr 10.33.0.1 udp sport 67-68 udp dport 67-68 drop
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
func bridgePort() Port {
|
||||
return Port{
|
||||
Name: "v-t33a-i",
|
||||
Forward: []string{"arp operation request arp daddr ip 10.33.0.1 drop", "ip saddr 10.33.0.1 udp sport 67-68 udp dport 67-68 drop"},
|
||||
Output: []string{"arp saddr ip 10.33.0.1 drop", "ip saddr 10.33.0.1 udp sport 67-68 udp dport 67-68 drop"},
|
||||
}
|
||||
}
|
||||
|
||||
func mustParse(t *testing.T, data string) table {
|
||||
t.Helper()
|
||||
tb, err := parseListing([]byte(data))
|
||||
if err != nil {
|
||||
t.Fatalf("parseListing: %v", err)
|
||||
}
|
||||
return tb
|
||||
}
|
||||
|
||||
func TestRuleTexts(t *testing.T) {
|
||||
for got, want := range map[string]string{
|
||||
DropSourceMAC("02:01:00:00:0b:5f"): "ether saddr 02:01:00:00:0b:5f drop",
|
||||
DropARPRequestTo("10.33.0.1"): "arp operation request arp daddr ip 10.33.0.1 drop",
|
||||
DropARPFrom("10.33.0.1"): "arp saddr ip 10.33.0.1 drop",
|
||||
DropDHCPFrom("10.33.0.1"): "ip saddr 10.33.0.1 udp sport 67-68 udp dport 67-68 drop",
|
||||
DropSourceMACOtherThan("52:54:00:00:0a:05"): "ether saddr != 52:54:00:00:0a:05 drop",
|
||||
DropEtherTypesOtherThanIPAndARP(): "ether type != { ip, arp, ip6 } drop",
|
||||
DropARPSenderMACOtherThan("52:54:00:00:0a:05"): "arp saddr ether != 52:54:00:00:0a:05 drop",
|
||||
DropARPSenderIPOtherThan("10.0.0.5"): "arp saddr ip != { 0.0.0.0, 10.0.0.5 } drop",
|
||||
DropDHCPServer(): "udp sport 67 drop",
|
||||
DropIPSourceOtherThan("10.0.0.5"): "ip saddr != { 0.0.0.0, 10.0.0.5 } drop",
|
||||
} {
|
||||
if got != want {
|
||||
t.Errorf("%q, %q attendu", got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDropUnspecifiedSourceOutsideDHCP(t *testing.T) {
|
||||
want := "ip saddr 0.0.0.0 ip protocol != udp drop|ip saddr 0.0.0.0 udp dport != 67 drop"
|
||||
if got := strings.Join(DropUnspecifiedSourceOutsideDHCP(), "|"); got != want {
|
||||
t.Errorf("%q, %q attendu", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetScriptOnAnEmptyNetns(t *testing.T) {
|
||||
want := `add table bridge two
|
||||
add map bridge two pre_ports { type ifname : verdict; }
|
||||
add map bridge two fwd_ports { type ifname : verdict; }
|
||||
add map bridge two out_ports { type ifname : verdict; }
|
||||
add chain bridge two prerouting { type filter hook prerouting priority dstnat; policy accept; }
|
||||
add chain bridge two forward { type filter hook forward priority filter; policy accept; }
|
||||
add chain bridge two output { type filter hook output priority filter; policy accept; }
|
||||
add rule bridge two prerouting iifname vmap @pre_ports
|
||||
add rule bridge two forward oifname vmap @fwd_ports
|
||||
add rule bridge two output oifname vmap @out_ports
|
||||
add chain bridge two fwd-v-t33a-i
|
||||
flush chain bridge two fwd-v-t33a-i
|
||||
add rule bridge two fwd-v-t33a-i arp operation request arp daddr ip 10.33.0.1 drop
|
||||
add rule bridge two fwd-v-t33a-i ip saddr 10.33.0.1 udp sport 67-68 udp dport 67-68 drop
|
||||
add element bridge two fwd_ports { "v-t33a-i" : jump fwd-v-t33a-i }
|
||||
add chain bridge two out-v-t33a-i
|
||||
flush chain bridge two out-v-t33a-i
|
||||
add rule bridge two out-v-t33a-i arp saddr ip 10.33.0.1 drop
|
||||
add rule bridge two out-v-t33a-i ip saddr 10.33.0.1 udp sport 67-68 udp dport 67-68 drop
|
||||
add element bridge two out_ports { "v-t33a-i" : jump out-v-t33a-i }
|
||||
`
|
||||
if got := setScript(bridgePort(), mustParse(t, noTable)); got != want {
|
||||
t.Errorf("script :\n%s\nattendu :\n%s", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetScriptAgainKeepsDispatchAndElements(t *testing.T) {
|
||||
got := setScript(bridgePort(), mustParse(t, listedTable))
|
||||
for _, absent := range []string{"vmap", "add element", "delete"} {
|
||||
if strings.Contains(got, absent) {
|
||||
t.Errorf("%q présent alors que la table est déjà en place :\n%s", absent, got)
|
||||
}
|
||||
}
|
||||
if !strings.Contains(got, "flush chain bridge two fwd-v-t33a-i\n") {
|
||||
t.Errorf("la chaîne n'est pas vidée avant d'être réécrite :\n%s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetScriptReplacesAnElementPointingElsewhere(t *testing.T) {
|
||||
data := strings.Replace(listedTable, `["v-t33a-i", {"jump": {"target": "fwd-v-t33a-i"}}]`, `["v-t33a-i", {"jump": {"target": "fwd-v-other-i"}}]`, 1)
|
||||
got := setScript(bridgePort(), mustParse(t, data))
|
||||
want := "delete element bridge two fwd_ports { \"v-t33a-i\" }\nadd element bridge two fwd_ports { \"v-t33a-i\" : jump fwd-v-t33a-i }\n"
|
||||
if !strings.Contains(got, want) {
|
||||
t.Errorf("élément non remplacé :\n%s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetScriptRemovesAHookThePortNoLongerUses(t *testing.T) {
|
||||
p := bridgePort()
|
||||
p.Output = nil
|
||||
got := setScript(p, mustParse(t, listedTable))
|
||||
want := "delete element bridge two out_ports { \"v-t33a-i\" }\ndelete chain bridge two out-v-t33a-i\n"
|
||||
if !strings.Contains(got, want) {
|
||||
t.Errorf("crochet output non retiré :\n%s", got)
|
||||
}
|
||||
if strings.Contains(got, "pre-v-t33a-i") {
|
||||
t.Errorf("chaîne prerouting touchée alors qu'elle n'existe pas :\n%s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteScriptRemovesElementsBeforeChains(t *testing.T) {
|
||||
want := `delete element bridge two fwd_ports { "v-t33a-i" }
|
||||
delete element bridge two out_ports { "v-t33a-i" }
|
||||
delete chain bridge two fwd-v-t33a-i
|
||||
delete chain bridge two out-v-t33a-i
|
||||
`
|
||||
if got := deleteScript("v-t33a-i", mustParse(t, listedTable)); got != want {
|
||||
t.Errorf("script :\n%s\nattendu :\n%s", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteScriptLeavesOtherPorts(t *testing.T) {
|
||||
got := deleteScript("v-t33a-i", mustParse(t, listedTable))
|
||||
if strings.Contains(got, "v-other-i") {
|
||||
t.Errorf("un autre port est touché :\n%s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteScriptIsEmptyForAnAbsentPort(t *testing.T) {
|
||||
for name, data := range map[string]string{"sans table": noTable, "port inconnu": listedTable} {
|
||||
if got := deleteScript("v-gone-i", mustParse(t, data)); got != "" {
|
||||
t.Errorf("%s : script %q, vide attendu", name, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseListing(t *testing.T) {
|
||||
tb := mustParse(t, listedTable)
|
||||
if !tb.exists {
|
||||
t.Errorf("table two non vue")
|
||||
}
|
||||
if got := tb.elements["fwd_ports"]["v-t33a-i"]; got != "fwd-v-t33a-i" {
|
||||
t.Errorf("fwd_ports[v-t33a-i] = %q", got)
|
||||
}
|
||||
if got := len(tb.elements["pre_ports"]); got != 0 {
|
||||
t.Errorf("pre_ports : %d éléments, 0 attendu", got)
|
||||
}
|
||||
for _, c := range []string{"prerouting", "forward", "output", "fwd-v-t33a-i", "out-v-t33a-i", "fwd-v-other-i"} {
|
||||
if !tb.chains[c] {
|
||||
t.Errorf("chaîne %s non vue", c)
|
||||
}
|
||||
}
|
||||
for _, h := range hooks {
|
||||
if !tb.hasDispatch(h) {
|
||||
t.Errorf("aiguillage de %s non vu : %q", h.chain, tb.dispatch[h.chain])
|
||||
}
|
||||
}
|
||||
if mustParse(t, noTable).exists {
|
||||
t.Errorf("table vue alors qu'absente")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseListingRejectsAMalformedElement(t *testing.T) {
|
||||
data := strings.Replace(listedTable, `["v-other-i", {"jump": {"target": "fwd-v-other-i"}}]`, `"v-other-i"`, 1)
|
||||
if _, err := parseListing([]byte(data)); err == nil || !strings.Contains(err.Error(), "map fwd_ports") {
|
||||
t.Errorf("erreur = %v, erreur sur fwd_ports attendue", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChainRules(t *testing.T) {
|
||||
got := chainRules(listedChain)
|
||||
want := []string{"arp operation request arp daddr ip 10.33.0.1 drop", "ip saddr 10.33.0.1 udp sport 67-68 udp dport 67-68 drop"}
|
||||
if strings.Join(got, "|") != strings.Join(want, "|") {
|
||||
t.Errorf("règles %q, %q attendues", got, want)
|
||||
}
|
||||
if got := chainRules("table bridge two {\n\tchain fwd-b {\n\t}\n}\n"); len(got) != 0 {
|
||||
t.Errorf("chaîne vide : %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func healthyListings() map[string][]string {
|
||||
return map[string][]string{
|
||||
"fwd-v-t33a-i": chainRules(listedChain),
|
||||
"out-v-t33a-i": {"arp saddr ip 10.33.0.1 drop", "ip saddr 10.33.0.1 udp sport 67-68 udp dport 67-68 drop"},
|
||||
}
|
||||
}
|
||||
|
||||
func TestMissingIsEmptyWhenEverythingIsInPlace(t *testing.T) {
|
||||
if got := missing(bridgePort(), mustParse(t, listedTable), healthyListings()); len(got) != 0 {
|
||||
t.Errorf("manques %q, aucun attendu", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMissingReportsEachDefect(t *testing.T) {
|
||||
data := strings.Replace(listedTable, `, "elem": [["v-t33a-i", {"jump": {"target": "out-v-t33a-i"}}]]`, "", 1)
|
||||
data = strings.Replace(data, `"data": "@fwd_ports"`, `"data": "@pre_ports"`, 1)
|
||||
listings := healthyListings()
|
||||
listings["fwd-v-t33a-i"] = []string{"arp operation request arp daddr ip 10.33.0.1 drop", "accept"}
|
||||
got := strings.Join(missing(bridgePort(), mustParse(t, data), listings), "\n")
|
||||
for _, want := range []string{
|
||||
"forward: oifname vmap @fwd_ports",
|
||||
`out_ports: "v-t33a-i" : jump out-v-t33a-i`,
|
||||
"fwd-v-t33a-i: ip saddr 10.33.0.1 udp sport 67-68 udp dport 67-68 drop",
|
||||
"fwd-v-t33a-i: unexpected accept",
|
||||
} {
|
||||
if !strings.Contains(got, want) {
|
||||
t.Errorf("%q absent de :\n%s", want, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMissingReportsEverythingWithoutTable(t *testing.T) {
|
||||
got := missing(bridgePort(), mustParse(t, noTable), nil)
|
||||
if len(got) != 8 {
|
||||
t.Errorf("%d manques, 8 attendus (2 aiguillages, 2 éléments, 4 règles) : %q", len(got), got)
|
||||
}
|
||||
}
|
||||
|
|
@ -3,10 +3,11 @@ package subnet
|
|||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"git.g3e.fr/syonad/two/internal/ebtables"
|
||||
"git.g3e.fr/syonad/two/internal/netif"
|
||||
"git.g3e.fr/syonad/two/internal/netns"
|
||||
"git.g3e.fr/syonad/two/internal/nft"
|
||||
"git.g3e.fr/syonad/two/internal/sysctl"
|
||||
)
|
||||
|
||||
|
|
@ -31,9 +32,7 @@ func setupAnycastGateway(d subnetData, vethI string) error {
|
|||
name string
|
||||
fn func() error
|
||||
}{
|
||||
{"filter " + vethI, func() error {
|
||||
return nft.SetPort(AnycastFabricPort(vethI, mac.String(), d.interfaceIP.String(), d.relayIP.String()))
|
||||
}},
|
||||
{"confine " + mac.String(), func() error { return ebtables.ConfineMAC(vethI, mac.String()) }},
|
||||
{"arp sysctls on " + d.bridge, func() error { return setSysctls(arpSysctls(d.bridge)) }},
|
||||
{"relay address on " + d.bridge, func() error { return netif.AddrAdd(d.bridge, d.relayIP) }},
|
||||
{"create " + gw, func() error { return netif.CreateMacvlan(gw, d.bridge, mac) }},
|
||||
|
|
@ -42,6 +41,10 @@ func setupAnycastGateway(d subnetData, vethI string) error {
|
|||
{"gateway address on " + gw, func() error { return netif.AddrAdd(gw, d.interfaceIP) }},
|
||||
{"route " + d.cidr.String(), func() error { return netif.RouteAddFrom(d.bridge, d.cidr, d.interfaceIP) }},
|
||||
{"set up " + gw, func() error { return netif.LinkSetUp(gw) }},
|
||||
{"drop arp to gateway", func() error { return ebtables.DropARPToGateway(vethI, d.interfaceIP.String()) }},
|
||||
{"drop arp to relay", func() error { return ebtables.DropARPToGateway(vethI, d.relayIP.String()) }},
|
||||
{"drop dhcp from gateway", func() error { return ebtables.DropDHCP(vethI, d.interfaceIP.String()) }},
|
||||
{"drop dhcp from relay", func() error { return ebtables.DropDHCP(vethI, d.relayIP.String()) }},
|
||||
}
|
||||
return netns.Call(d.vpc, func() error {
|
||||
for _, step := range steps {
|
||||
|
|
@ -54,9 +57,21 @@ func setupAnycastGateway(d subnetData, vethI string) error {
|
|||
}
|
||||
|
||||
func deleteAnycastGateway(d subnetData, vethI string) error {
|
||||
mac, err := netif.AnycastMAC(d.vxlanID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return netns.Call(d.vpc, func() error {
|
||||
var errs []error
|
||||
if err := nft.DeletePort(vethI); err != nil {
|
||||
for _, ip := range []net.IP{d.interfaceIP, d.relayIP} {
|
||||
if err := ebtables.DeleteARPToGateway(vethI, ip.String()); err != nil {
|
||||
errs = append(errs, fmt.Errorf("delete ebtables arp rule %s: %w", ip, err))
|
||||
}
|
||||
if err := ebtables.DeleteDHCP(vethI, ip.String()); err != nil {
|
||||
errs = append(errs, fmt.Errorf("delete ebtables dhcp rule %s: %w", ip, err))
|
||||
}
|
||||
}
|
||||
if err := ebtables.DeleteConfineMAC(vethI, mac.String()); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
if err := netif.DeleteLink(d.gatewayLink()); err != nil {
|
||||
|
|
|
|||
|
|
@ -6,10 +6,10 @@ import (
|
|||
|
||||
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
||||
"git.g3e.fr/syonad/two/internal/dhcpbackend"
|
||||
"git.g3e.fr/syonad/two/internal/ebtables"
|
||||
"git.g3e.fr/syonad/two/internal/l3vni"
|
||||
"git.g3e.fr/syonad/two/internal/netif"
|
||||
"git.g3e.fr/syonad/two/internal/netns"
|
||||
"git.g3e.fr/syonad/two/internal/nft"
|
||||
"git.g3e.fr/syonad/two/internal/state"
|
||||
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
|
|
@ -118,7 +118,10 @@ func createSubnet(db *badger.DB, subnetName string, d subnetData, backend dhcpba
|
|||
if err := netif.RouteAdd(d.bridge, d.cidr); err != nil {
|
||||
return fmt.Errorf("add route: %w", err)
|
||||
}
|
||||
return nft.SetPort(VxlanFabricPort(vethI, d.interfaceIP.String()))
|
||||
if err := ebtables.DropARPToGateway(vethI, d.interfaceIP.String()); err != nil {
|
||||
return err
|
||||
}
|
||||
return ebtables.DropDHCP(vethI, d.interfaceIP.String())
|
||||
}); err != nil {
|
||||
return fmt.Errorf("configure netns: %w", err)
|
||||
}
|
||||
|
|
@ -130,7 +133,7 @@ func createSubnet(db *badger.DB, subnetName string, d subnetData, backend dhcpba
|
|||
if err := netif.RouteAdd(d.bridge, d.cidr); err != nil {
|
||||
return fmt.Errorf("add route: %w", err)
|
||||
}
|
||||
return nft.SetPort(BridgeFabricPort(vethI, d.interfaceIP.String()))
|
||||
return ebtables.ConfineGateway(vethI, d.interfaceIP.String())
|
||||
}); err != nil {
|
||||
return fmt.Errorf("configure netns: %w", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,10 +5,10 @@ import (
|
|||
|
||||
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
||||
"git.g3e.fr/syonad/two/internal/dhcpbackend"
|
||||
"git.g3e.fr/syonad/two/internal/ebtables"
|
||||
"git.g3e.fr/syonad/two/internal/l3vni"
|
||||
"git.g3e.fr/syonad/two/internal/netif"
|
||||
"git.g3e.fr/syonad/two/internal/netns"
|
||||
"git.g3e.fr/syonad/two/internal/nft"
|
||||
"git.g3e.fr/syonad/two/internal/state"
|
||||
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
|
|
@ -76,8 +76,11 @@ func deleteSubnetVxlan(d subnetData) error {
|
|||
return fmt.Errorf("delete netns resources: %w", err)
|
||||
}
|
||||
} else if err := netns.Call(d.vpc, func() error {
|
||||
if err := nft.DeletePort(vethI); err != nil {
|
||||
return err
|
||||
if err := ebtables.DeleteARPToGateway(vethI, d.interfaceIP.String()); err != nil {
|
||||
return fmt.Errorf("delete ebtables arp rule: %w", err)
|
||||
}
|
||||
if err := ebtables.DeleteDHCP(vethI, d.interfaceIP.String()); err != nil {
|
||||
return fmt.Errorf("delete ebtables dhcp rule: %w", err)
|
||||
}
|
||||
return netif.DeleteLink(d.bridge)
|
||||
}); err != nil {
|
||||
|
|
@ -102,8 +105,8 @@ func deleteSubnetBridge(d subnetData) error {
|
|||
vethI := "v-" + d.subnetID + "-i"
|
||||
|
||||
if err := netns.Call(d.vpc, func() error {
|
||||
if err := nft.DeletePort(vethI); err != nil {
|
||||
return err
|
||||
if err := ebtables.DeleteConfineGateway(vethI, d.interfaceIP.String()); err != nil {
|
||||
return fmt.Errorf("delete ebtables gateway rules: %w", err)
|
||||
}
|
||||
return netif.DeleteLink(d.bridge)
|
||||
}); err != nil {
|
||||
|
|
|
|||
|
|
@ -1,35 +0,0 @@
|
|||
package subnet
|
||||
|
||||
import (
|
||||
"git.g3e.fr/syonad/two/internal/nft"
|
||||
)
|
||||
|
||||
func VxlanFabricPort(vethI, gatewayIP string) nft.Port {
|
||||
return nft.Port{
|
||||
Name: vethI,
|
||||
Forward: []string{nft.DropARPRequestTo(gatewayIP), nft.DropDHCPFrom(gatewayIP)},
|
||||
}
|
||||
}
|
||||
|
||||
func AnycastFabricPort(vethI, mac, gatewayIP, relayIP string) nft.Port {
|
||||
return nft.Port{
|
||||
Name: vethI,
|
||||
Prerouting: []string{nft.DropSourceMAC(mac)},
|
||||
Forward: []string{
|
||||
nft.DropSourceMAC(mac),
|
||||
nft.DropARPRequestTo(gatewayIP),
|
||||
nft.DropARPRequestTo(relayIP),
|
||||
nft.DropDHCPFrom(gatewayIP),
|
||||
nft.DropDHCPFrom(relayIP),
|
||||
},
|
||||
Output: []string{nft.DropSourceMAC(mac)},
|
||||
}
|
||||
}
|
||||
|
||||
func BridgeFabricPort(vethI, gatewayIP string) nft.Port {
|
||||
return nft.Port{
|
||||
Name: vethI,
|
||||
Forward: []string{nft.DropARPRequestTo(gatewayIP), nft.DropDHCPFrom(gatewayIP)},
|
||||
Output: []string{nft.DropARPFrom(gatewayIP), nft.DropDHCPFrom(gatewayIP)},
|
||||
}
|
||||
}
|
||||
|
|
@ -1,52 +0,0 @@
|
|||
package subnet
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func assertRules(t *testing.T, hook string, got, want []string) {
|
||||
t.Helper()
|
||||
if strings.Join(got, "\n") != strings.Join(want, "\n") {
|
||||
t.Errorf("%s : %q, %q attendu", hook, got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVxlanFabricPort(t *testing.T) {
|
||||
p := VxlanFabricPort("v-s9r1-i", "10.9.1.254")
|
||||
if p.Name != "v-s9r1-i" {
|
||||
t.Errorf("port %q", p.Name)
|
||||
}
|
||||
assertRules(t, "prerouting", p.Prerouting, nil)
|
||||
assertRules(t, "forward", p.Forward, []string{
|
||||
"arp operation request arp daddr ip 10.9.1.254 drop",
|
||||
"ip saddr 10.9.1.254 udp sport 67-68 udp dport 67-68 drop",
|
||||
})
|
||||
assertRules(t, "output", p.Output, nil)
|
||||
}
|
||||
|
||||
func TestAnycastFabricPort(t *testing.T) {
|
||||
p := AnycastFabricPort("v-s9r1-i", "02:01:00:00:0b:5f", "10.9.1.254", "10.9.1.253")
|
||||
assertRules(t, "prerouting", p.Prerouting, []string{"ether saddr 02:01:00:00:0b:5f drop"})
|
||||
assertRules(t, "forward", p.Forward, []string{
|
||||
"ether saddr 02:01:00:00:0b:5f drop",
|
||||
"arp operation request arp daddr ip 10.9.1.254 drop",
|
||||
"arp operation request arp daddr ip 10.9.1.253 drop",
|
||||
"ip saddr 10.9.1.254 udp sport 67-68 udp dport 67-68 drop",
|
||||
"ip saddr 10.9.1.253 udp sport 67-68 udp dport 67-68 drop",
|
||||
})
|
||||
assertRules(t, "output", p.Output, []string{"ether saddr 02:01:00:00:0b:5f drop"})
|
||||
}
|
||||
|
||||
func TestBridgeFabricPort(t *testing.T) {
|
||||
p := BridgeFabricPort("v-t33a-i", "10.33.0.1")
|
||||
assertRules(t, "prerouting", p.Prerouting, nil)
|
||||
assertRules(t, "forward", p.Forward, []string{
|
||||
"arp operation request arp daddr ip 10.33.0.1 drop",
|
||||
"ip saddr 10.33.0.1 udp sport 67-68 udp dport 67-68 drop",
|
||||
})
|
||||
assertRules(t, "output", p.Output, []string{
|
||||
"arp saddr ip 10.33.0.1 drop",
|
||||
"ip saddr 10.33.0.1 udp sport 67-68 udp dport 67-68 drop",
|
||||
})
|
||||
}
|
||||
|
|
@ -15,7 +15,6 @@ import (
|
|||
"git.g3e.fr/syonad/two/internal/metadata"
|
||||
"git.g3e.fr/syonad/two/internal/netif"
|
||||
"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/state"
|
||||
|
||||
|
|
@ -42,17 +41,6 @@ func StartVM(db *badger.DB, name string, cfg *configuration.Config) error {
|
|||
return err
|
||||
}
|
||||
|
||||
if err := netns.Call(nic.vpcName, func() error {
|
||||
for _, n := range d.nics {
|
||||
if err := nft.SetPort(n.tapPort()); err != nil {
|
||||
return fmt.Errorf("interface %d: %w", n.index, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return fmt.Errorf("filter taps: %w", err)
|
||||
}
|
||||
|
||||
for _, n := range d.nics {
|
||||
if err := netif.CreateTap(n.tapID, n.bridge, n.vpcName, cfg.Network.VM()); err != nil {
|
||||
return fmt.Errorf("create tap of interface %d: %w", n.index, err)
|
||||
|
|
|
|||
|
|
@ -27,7 +27,6 @@ type nicData struct {
|
|||
mac string
|
||||
tapID int
|
||||
primary bool
|
||||
dhcpServer bool
|
||||
mode string
|
||||
vpcCIDR *net.IPNet
|
||||
}
|
||||
|
|
@ -180,7 +179,6 @@ func loadNIC(db *badger.DB, name string, idx int, entries map[string]string) (ni
|
|||
}
|
||||
n.bridge = "br-" + strings.SplitN(n.subnetName, "-", 2)[1]
|
||||
n.primary = entries[prefix+"primary"] == "true"
|
||||
n.dhcpServer = entries[prefix+"dhcp_server"] == "true"
|
||||
|
||||
vpcName, err := kv.GetFromDB(db, "subnet/"+n.subnetName+"/vpc")
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -125,23 +125,6 @@ func TestLoadVM_SingleNIC(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestLoadVM_ReadsDHCPServer(t *testing.T) {
|
||||
db := newVMInDB(t)
|
||||
addNIC(t, db, 1, "sn-000002", "10.2.0.5", "00:22:33:00:02:05", false)
|
||||
kv.AddInDB(db, "vm/vm-1/nic/1/dhcp_server", "true")
|
||||
|
||||
d, err := loadVM(db, "vm-1")
|
||||
if err != nil {
|
||||
t.Fatalf("loadVM : %v", err)
|
||||
}
|
||||
if len(d.nics) != 2 {
|
||||
t.Fatalf("2 interfaces attendues, obtenu %d", len(d.nics))
|
||||
}
|
||||
if d.nics[0].dhcpServer || !d.nics[1].dhcpServer {
|
||||
t.Errorf("dhcp_server attendu sur l'index 1 seulement : %+v", d.nics)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadVM_MultipleNICsSortedByIndex(t *testing.T) {
|
||||
db := newVMInDB(t)
|
||||
addNIC(t, db, 2, "sn-000003", "10.3.0.9", "00:22:33:00:03:09", false)
|
||||
|
|
|
|||
|
|
@ -16,7 +16,6 @@ import (
|
|||
"git.g3e.fr/syonad/two/internal/metadata"
|
||||
"git.g3e.fr/syonad/two/internal/netif"
|
||||
"git.g3e.fr/syonad/two/internal/netns"
|
||||
"git.g3e.fr/syonad/two/internal/nft"
|
||||
"git.g3e.fr/syonad/two/internal/qmp"
|
||||
"git.g3e.fr/syonad/two/internal/state"
|
||||
|
||||
|
|
@ -80,17 +79,6 @@ func StopVM(db *badger.DB, name string, cfg *configuration.Config) error {
|
|||
}
|
||||
}
|
||||
|
||||
if err := netns.Call(nic.vpcName, func() error {
|
||||
for _, n := range d.nics {
|
||||
if err := nft.DeletePort(n.tapPort().Name); err != nil {
|
||||
return fmt.Errorf("interface %d: %w", n.index, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return fmt.Errorf("unfilter taps: %w", err)
|
||||
}
|
||||
|
||||
if err := removeDHCPFiles(d, name, backend); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,28 +0,0 @@
|
|||
package vm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.g3e.fr/syonad/two/internal/nft"
|
||||
)
|
||||
|
||||
func TapPort(tapID int, mac, ip string, dhcpServer bool) nft.Port {
|
||||
mac = strings.ToLower(mac)
|
||||
rules := []string{
|
||||
nft.DropSourceMACOtherThan(mac),
|
||||
nft.DropEtherTypesOtherThanIPAndARP(),
|
||||
nft.DropARPSenderMACOtherThan(mac),
|
||||
nft.DropARPSenderIPOtherThan(ip),
|
||||
nft.DropIPSourceOtherThan(ip),
|
||||
}
|
||||
rules = append(rules, nft.DropUnspecifiedSourceOutsideDHCP()...)
|
||||
if !dhcpServer {
|
||||
rules = append(rules, nft.DropDHCPServer())
|
||||
}
|
||||
return nft.Port{Name: fmt.Sprintf("tap%d", tapID), Prerouting: rules}
|
||||
}
|
||||
|
||||
func (n nicData) tapPort() nft.Port {
|
||||
return TapPort(n.tapID, n.mac, n.ip, n.dhcpServer)
|
||||
}
|
||||
|
|
@ -1,53 +0,0 @@
|
|||
package vm
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTapPortFiltersEverythingTheVMMustNotSend(t *testing.T) {
|
||||
p := TapPort(12345678, "52:54:00:00:0A:05", "10.0.0.5", false)
|
||||
if p.Name != "tap12345678" {
|
||||
t.Errorf("port %q, tap12345678 attendu", p.Name)
|
||||
}
|
||||
want := []string{
|
||||
"ether saddr != 52:54:00:00:0a:05 drop",
|
||||
"ether type != { ip, arp, ip6 } drop",
|
||||
"arp saddr ether != 52:54:00:00:0a:05 drop",
|
||||
"arp saddr ip != { 0.0.0.0, 10.0.0.5 } drop",
|
||||
"ip saddr != { 0.0.0.0, 10.0.0.5 } drop",
|
||||
"ip saddr 0.0.0.0 ip protocol != udp drop",
|
||||
"ip saddr 0.0.0.0 udp dport != 67 drop",
|
||||
"udp sport 67 drop",
|
||||
}
|
||||
if strings.Join(p.Prerouting, "\n") != strings.Join(want, "\n") {
|
||||
t.Errorf("règles :\n%s\nattendues :\n%s", strings.Join(p.Prerouting, "\n"), strings.Join(want, "\n"))
|
||||
}
|
||||
if len(p.Forward) != 0 || len(p.Output) != 0 {
|
||||
t.Errorf("seul ce qu'émet la VM est filtré : forward %q, output %q", p.Forward, p.Output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTapPortLetsAnAuthorizedNICAnswerDHCP(t *testing.T) {
|
||||
allowed := TapPort(12345678, "52:54:00:00:0a:05", "10.0.0.5", true)
|
||||
denied := TapPort(12345678, "52:54:00:00:0a:05", "10.0.0.5", false)
|
||||
for _, r := range allowed.Prerouting {
|
||||
if r == "udp sport 67 drop" {
|
||||
t.Errorf("la NIC autorisée garde le blocage du serveur DHCP")
|
||||
}
|
||||
}
|
||||
if len(allowed.Prerouting) != len(denied.Prerouting)-1 {
|
||||
t.Errorf("seul le blocage du serveur DHCP doit disparaître : %q", allowed.Prerouting)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNICTapPortUsesItsOwnData(t *testing.T) {
|
||||
n := nicData{tapID: 87654321, mac: "52:54:00:00:0b:07", ip: "10.0.1.7", dhcpServer: true}
|
||||
p := n.tapPort()
|
||||
if p.Name != "tap87654321" || p.Prerouting[0] != "ether saddr != 52:54:00:00:0b:07 drop" || p.Prerouting[3] != "arp saddr ip != { 0.0.0.0, 10.0.1.7 } drop" {
|
||||
t.Errorf("port %+v", p)
|
||||
}
|
||||
if strings.Contains(strings.Join(p.Prerouting, "|"), "udp sport 67 drop") {
|
||||
t.Errorf("dhcpServer de la NIC ignoré")
|
||||
}
|
||||
}
|
||||
|
|
@ -32,7 +32,7 @@ func CreateVPC(db *badger.DB, name string, cfg *configuration.Config) error {
|
|||
return err
|
||||
}
|
||||
|
||||
if err := netif.BridgeSetMaster("vp-"+vpcID+"-e", cfg.PublicBridge()); err != nil {
|
||||
if err := netif.BridgeSetMaster("vp-"+vpcID+"-e", "br-public"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
@ -53,9 +53,6 @@ func CreateVPC(db *badger.DB, name string, cfg *configuration.Config) error {
|
|||
if err := l3vni.Create(db, name, cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := setupHostSvc(name, newSystemHostSvc()); err != nil {
|
||||
return err
|
||||
}
|
||||
return state.Set(db, "vpc/"+name, state.Running)
|
||||
}
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -17,10 +17,6 @@ func DeleteVPC(db *badger.DB, name string) error {
|
|||
} else if current == state.Deleting {
|
||||
vpcID := strings.SplitN(name, "-", 2)[1]
|
||||
|
||||
if err := teardownHostSvc(name, newSystemHostSvc()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := l3vni.Delete(db, name); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,72 +0,0 @@
|
|||
package vpc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
hostsvcapi "git.g3e.fr/syonad/two/internal/api/hostsvc"
|
||||
hostsvcclient "git.g3e.fr/syonad/two/internal/client/hostsvc"
|
||||
"git.g3e.fr/syonad/two/internal/hostsvc"
|
||||
"git.g3e.fr/syonad/two/internal/iptables"
|
||||
"git.g3e.fr/syonad/two/internal/netif"
|
||||
"git.g3e.fr/syonad/two/internal/netns"
|
||||
)
|
||||
|
||||
const HostSvcLink = "hostsvc"
|
||||
|
||||
type hostSvcHost interface {
|
||||
InNetns(vpc string, fn func() error) error
|
||||
CreateDummy(name string) error
|
||||
AddrAdd(iface string, ip net.IP) error
|
||||
AddRedirect(target string) error
|
||||
Open(vpc string) error
|
||||
Close(vpc string) error
|
||||
}
|
||||
|
||||
type systemHostSvc struct {
|
||||
client *hostsvcclient.Client
|
||||
}
|
||||
|
||||
func newSystemHostSvc() systemHostSvc {
|
||||
return systemHostSvc{client: hostsvcclient.New(hostsvcapi.SocketPath(hostsvcapi.DefaultRunDir))}
|
||||
}
|
||||
|
||||
func (systemHostSvc) InNetns(vpc string, fn func() error) error { return netns.Call(vpc, fn) }
|
||||
func (systemHostSvc) CreateDummy(name string) error { return netif.CreateDummy(name) }
|
||||
func (systemHostSvc) AddrAdd(iface string, ip net.IP) error { return netif.AddrAdd(iface, ip) }
|
||||
func (systemHostSvc) AddRedirect(target string) error { return iptables.AddHostSvcRedirect(target) }
|
||||
func (s systemHostSvc) Open(vpc string) error { return s.client.Open(vpc) }
|
||||
func (s systemHostSvc) Close(vpc string) error { return s.client.Close(vpc) }
|
||||
|
||||
func setupHostSvc(vpc string, h hostSvcHost) error {
|
||||
ip := net.ParseIP(hostsvc.ServiceIP)
|
||||
if err := h.InNetns(vpc, func() error {
|
||||
steps := []struct {
|
||||
name string
|
||||
fn func() error
|
||||
}{
|
||||
{"create " + HostSvcLink, func() error { return h.CreateDummy(HostSvcLink) }},
|
||||
{"address " + hostsvc.ServiceIP + " on " + HostSvcLink, func() error { return h.AddrAdd(HostSvcLink, ip) }},
|
||||
{"redirect dns and ntp to " + hostsvc.ServiceIP, func() error { return h.AddRedirect(hostsvc.ServiceIP) }},
|
||||
}
|
||||
for _, step := range steps {
|
||||
if err := step.fn(); err != nil {
|
||||
return fmt.Errorf("%s: %w", step.name, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return fmt.Errorf("hostsvc in %s: %w", vpc, err)
|
||||
}
|
||||
if err := h.Open(vpc); err != nil {
|
||||
return fmt.Errorf("hostsvc open %s: %w", vpc, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func teardownHostSvc(vpc string, h hostSvcHost) error {
|
||||
if err := h.Close(vpc); err != nil {
|
||||
return fmt.Errorf("hostsvc close %s: %w", vpc, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -1,92 +0,0 @@
|
|||
package vpc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type fakeHostSvc struct {
|
||||
calls []string
|
||||
inside string
|
||||
failOn string
|
||||
}
|
||||
|
||||
func (f *fakeHostSvc) record(kind, detail string) error {
|
||||
call := kind + " " + detail
|
||||
if f.inside != "" {
|
||||
call = f.inside + ": " + call
|
||||
}
|
||||
f.calls = append(f.calls, call)
|
||||
if kind == f.failOn {
|
||||
return errors.New("boom")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeHostSvc) InNetns(vpc string, fn func() error) error {
|
||||
f.inside = vpc
|
||||
defer func() { f.inside = "" }()
|
||||
return fn()
|
||||
}
|
||||
|
||||
func (f *fakeHostSvc) CreateDummy(name string) error { return f.record("dummy", name) }
|
||||
func (f *fakeHostSvc) AddrAdd(iface string, ip net.IP) error {
|
||||
return f.record("addr", ip.String()+" "+iface)
|
||||
}
|
||||
func (f *fakeHostSvc) AddRedirect(target string) error { return f.record("redirect", target) }
|
||||
func (f *fakeHostSvc) Open(vpc string) error { return f.record("open", vpc) }
|
||||
func (f *fakeHostSvc) Close(vpc string) error { return f.record("close", vpc) }
|
||||
|
||||
func TestSetupHostSvc_OrderAndPlace(t *testing.T) {
|
||||
f := &fakeHostSvc{}
|
||||
if err := setupHostSvc("vpc-00001", f); err != nil {
|
||||
t.Fatalf("setupHostSvc: %v", err)
|
||||
}
|
||||
want := []string{
|
||||
"vpc-00001: dummy hostsvc",
|
||||
"vpc-00001: addr 169.254.169.253 hostsvc",
|
||||
"vpc-00001: redirect 169.254.169.253",
|
||||
"open vpc-00001",
|
||||
}
|
||||
if !reflect.DeepEqual(f.calls, want) {
|
||||
t.Errorf("calls = %v, want %v: the address and the redirection live in the vpc netns, the open is asked from the host once they exist", f.calls, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetupHostSvc_StopsAtTheFirstFailure(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
failOn string
|
||||
want string
|
||||
last string
|
||||
}{
|
||||
{"dummy", "hostsvc in vpc-00001: create hostsvc: boom", "vpc-00001: dummy hostsvc"},
|
||||
{"addr", "hostsvc in vpc-00001: address 169.254.169.253 on hostsvc: boom", "vpc-00001: addr 169.254.169.253 hostsvc"},
|
||||
{"redirect", "hostsvc in vpc-00001: redirect dns and ntp to 169.254.169.253: boom", "vpc-00001: redirect 169.254.169.253"},
|
||||
{"open", "hostsvc open vpc-00001: boom", "open vpc-00001"},
|
||||
} {
|
||||
f := &fakeHostSvc{failOn: c.failOn}
|
||||
err := setupHostSvc("vpc-00001", f)
|
||||
if err == nil || err.Error() != c.want {
|
||||
t.Errorf("fail on %s: error %v, want %q", c.failOn, err, c.want)
|
||||
}
|
||||
if got := f.calls[len(f.calls)-1]; got != c.last {
|
||||
t.Errorf("fail on %s: last call %q, want %q: nothing must run after a failure", c.failOn, got, c.last)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTeardownHostSvc(t *testing.T) {
|
||||
f := &fakeHostSvc{}
|
||||
if err := teardownHostSvc("vpc-00001", f); err != nil {
|
||||
t.Fatalf("teardownHostSvc: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(f.calls, []string{"close vpc-00001"}) {
|
||||
t.Errorf("calls = %v: only the close is needed, the dummy and the rules go away with the netns", f.calls)
|
||||
}
|
||||
f = &fakeHostSvc{failOn: "close"}
|
||||
if err := teardownHostSvc("vpc-00001", f); err == nil || err.Error() != "hostsvc close vpc-00001: boom" {
|
||||
t.Errorf("error %v: a failed close must stop the deletion", err)
|
||||
}
|
||||
}
|
||||
|
|
@ -3,9 +3,8 @@ package watchdog
|
|||
import (
|
||||
"fmt"
|
||||
|
||||
"git.g3e.fr/syonad/two/internal/ebtables"
|
||||
"git.g3e.fr/syonad/two/internal/netns"
|
||||
"git.g3e.fr/syonad/two/internal/nft"
|
||||
"git.g3e.fr/syonad/two/internal/subnet"
|
||||
"git.g3e.fr/syonad/two/internal/watchdog/notify"
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
|
||||
|
|
@ -22,13 +21,13 @@ func checkBridgeGateway(db *badger.DB, name, vpc, nsVeth string, n notify.Notifi
|
|||
return
|
||||
}
|
||||
if err := netns.Call(vpc, func() error {
|
||||
missing, err := nft.MissingRules(subnet.BridgeFabricPort(nsVeth, gatewayIP))
|
||||
missing, err := ebtables.MissingGatewayRules(nsVeth, gatewayIP)
|
||||
if err != nil {
|
||||
n.Notify(kindSubnet, name, fmt.Sprintf("gateway nftables rules unreadable in netns %s: %v", vpc, err))
|
||||
n.Notify(kindSubnet, name, fmt.Sprintf("gateway ebtables rules unreadable in netns %s: %v", vpc, err))
|
||||
return nil
|
||||
}
|
||||
for _, rule := range missing {
|
||||
n.Notify(kindSubnet, name, fmt.Sprintf("nftables rule missing in netns %s: %s", vpc, rule))
|
||||
n.Notify(kindSubnet, name, fmt.Sprintf("ebtables rule missing in netns %s: %s", vpc, rule))
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
|
|
|
|||
|
|
@ -1,110 +0,0 @@
|
|||
package watchdog
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
hostsvcapi "git.g3e.fr/syonad/two/internal/api/hostsvc"
|
||||
hostsvcclient "git.g3e.fr/syonad/two/internal/client/hostsvc"
|
||||
"git.g3e.fr/syonad/two/internal/hostsvc"
|
||||
"git.g3e.fr/syonad/two/internal/iptables"
|
||||
"git.g3e.fr/syonad/two/internal/netif"
|
||||
"git.g3e.fr/syonad/two/internal/netns"
|
||||
"git.g3e.fr/syonad/two/internal/state"
|
||||
"git.g3e.fr/syonad/two/internal/vpc"
|
||||
"git.g3e.fr/syonad/two/internal/watchdog/notify"
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
)
|
||||
|
||||
const kindHostSvc = "hostsvc"
|
||||
|
||||
type hostSvcProbe interface {
|
||||
Served() ([]string, error)
|
||||
ServiceAddress(vpc string) (bool, error)
|
||||
Redirect(vpc string) (bool, error)
|
||||
}
|
||||
|
||||
type systemHostSvcProbe struct {
|
||||
client *hostsvcclient.Client
|
||||
}
|
||||
|
||||
func newSystemHostSvcProbe() systemHostSvcProbe {
|
||||
return systemHostSvcProbe{client: hostsvcclient.New(hostsvcapi.SocketPath(hostsvcapi.DefaultRunDir))}
|
||||
}
|
||||
|
||||
func (p systemHostSvcProbe) Served() ([]string, error) { return p.client.List() }
|
||||
|
||||
func (systemHostSvcProbe) ServiceAddress(name string) (bool, error) {
|
||||
want := net.ParseIP(hostsvc.ServiceIP)
|
||||
found := false
|
||||
err := netns.Call(name, func() error {
|
||||
ips, err := netif.LinkIPv4(vpc.HostSvcLink)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, ip := range ips {
|
||||
if ip.Equal(want) {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return found, err
|
||||
}
|
||||
|
||||
func (systemHostSvcProbe) Redirect(name string) (bool, error) {
|
||||
var ok bool
|
||||
err := netns.Call(name, func() error {
|
||||
var err error
|
||||
ok, err = iptables.HasHostSvcRedirect(hostsvc.ServiceIP)
|
||||
return err
|
||||
})
|
||||
return ok, err
|
||||
}
|
||||
|
||||
func CheckHostSvc(db *badger.DB, p hostSvcProbe, n notify.Notifier) error {
|
||||
pairs, err := kv.ListByPrefix(db, prefixVPC)
|
||||
if err != nil {
|
||||
return fmt.Errorf("watchdog: listing vpcs: %w", err)
|
||||
}
|
||||
var running []string
|
||||
for _, name := range resourceNames(pairs, prefixVPC) {
|
||||
if st, err := state.Get(db, prefixVPC+name); err == nil && st == state.Running {
|
||||
running = append(running, name)
|
||||
}
|
||||
}
|
||||
if len(running) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
served, err := p.Served()
|
||||
if err != nil {
|
||||
n.Notify(kindHostSvc, hostsvcapi.Unit, fmt.Sprintf("unreachable, no vpc of this host gets dns or ntp: %v", err))
|
||||
} else {
|
||||
open := map[string]bool{}
|
||||
for _, name := range served {
|
||||
open[name] = true
|
||||
}
|
||||
for _, name := range running {
|
||||
if !open[name] {
|
||||
n.Notify(kindVPC, name, "not served by two-hostsvc: its vms get no dns nor ntp")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, name := range running {
|
||||
if ok, err := p.ServiceAddress(name); err != nil {
|
||||
n.Notify(kindVPC, name, fmt.Sprintf("%s on %s unreadable: %v", hostsvc.ServiceIP, vpc.HostSvcLink, err))
|
||||
} else if !ok {
|
||||
n.Notify(kindVPC, name, fmt.Sprintf("%s missing on %s (in netns)", hostsvc.ServiceIP, vpc.HostSvcLink))
|
||||
}
|
||||
if ok, err := p.Redirect(name); err != nil {
|
||||
n.Notify(kindVPC, name, fmt.Sprintf("dns and ntp redirection unreadable: %v", err))
|
||||
} else if !ok {
|
||||
n.Notify(kindVPC, name, fmt.Sprintf("dns and ntp redirection to %s missing or incomplete (chain %s)", hostsvc.ServiceIP, iptables.HostSvcChain))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -1,129 +0,0 @@
|
|||
package watchdog
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"git.g3e.fr/syonad/two/internal/state"
|
||||
)
|
||||
|
||||
type fakeHostSvcProbe struct {
|
||||
served []string
|
||||
servedErr error
|
||||
address map[string]bool
|
||||
redirect map[string]bool
|
||||
readErr error
|
||||
asked int
|
||||
}
|
||||
|
||||
func (f *fakeHostSvcProbe) Served() ([]string, error) {
|
||||
f.asked++
|
||||
return f.served, f.servedErr
|
||||
}
|
||||
|
||||
func (f *fakeHostSvcProbe) ServiceAddress(vpc string) (bool, error) {
|
||||
return f.address[vpc], f.readErr
|
||||
}
|
||||
|
||||
func (f *fakeHostSvcProbe) Redirect(vpc string) (bool, error) {
|
||||
return f.redirect[vpc], f.readErr
|
||||
}
|
||||
|
||||
func healthyHostSvc(vpcs ...string) *fakeHostSvcProbe {
|
||||
f := &fakeHostSvcProbe{served: vpcs, address: map[string]bool{}, redirect: map[string]bool{}}
|
||||
for _, v := range vpcs {
|
||||
f.address[v] = true
|
||||
f.redirect[v] = true
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
func TestCheckHostSvc_SilentWhenEverythingIsInPlace(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
seedResource(t, db, prefixVPC, "vp-a", state.Running)
|
||||
r := &recorder{}
|
||||
if err := CheckHostSvc(db, healthyHostSvc("vp-a"), r); err != nil {
|
||||
t.Fatalf("CheckHostSvc: %v", err)
|
||||
}
|
||||
if len(r.calls) != 0 {
|
||||
t.Errorf("notifications = %+v, want none", r.calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckHostSvc_NoRunningVPCDoesNotAskTheRelay(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
seedResource(t, db, prefixVPC, "vp-a", state.Creating)
|
||||
p := &fakeHostSvcProbe{servedErr: errors.New("down")}
|
||||
r := &recorder{}
|
||||
if err := CheckHostSvc(db, p, r); err != nil {
|
||||
t.Fatalf("CheckHostSvc: %v", err)
|
||||
}
|
||||
if p.asked != 0 || len(r.calls) != 0 {
|
||||
t.Errorf("asked %d times, notifications %+v: a host without running vpc needs no relay", p.asked, r.calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckHostSvc_ReportsAnUnreachableRelay(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
seedResource(t, db, prefixVPC, "vp-a", state.Running)
|
||||
p := healthyHostSvc("vp-a")
|
||||
p.servedErr = errors.New("dial /run/two/hostsvc/hostsvc.sock: connection refused")
|
||||
r := &recorder{}
|
||||
if err := CheckHostSvc(db, p, r); err != nil {
|
||||
t.Fatalf("CheckHostSvc: %v", err)
|
||||
}
|
||||
got := r.forName("two-hostsvc.service")
|
||||
if len(got) != 1 || got[0].kind != "hostsvc" {
|
||||
t.Fatalf("notifications = %+v, want one for two-hostsvc.service", r.calls)
|
||||
}
|
||||
if !r.hasProblemContaining("unreachable, no vpc of this host gets dns or ntp: dial /run/two/hostsvc/hostsvc.sock: connection refused") {
|
||||
t.Errorf("problem = %q", got[0].problem)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckHostSvc_ReportsEachMissingPiece(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
for _, v := range []string{"vp-ok", "vp-unserved", "vp-noaddr", "vp-noredirect"} {
|
||||
seedResource(t, db, prefixVPC, v, state.Running)
|
||||
}
|
||||
p := healthyHostSvc("vp-ok", "vp-unserved", "vp-noaddr", "vp-noredirect")
|
||||
p.served = []string{"vp-ok", "vp-noaddr", "vp-noredirect"}
|
||||
p.address["vp-noaddr"] = false
|
||||
p.redirect["vp-noredirect"] = false
|
||||
r := &recorder{}
|
||||
if err := CheckHostSvc(db, p, r); err != nil {
|
||||
t.Fatalf("CheckHostSvc: %v", err)
|
||||
}
|
||||
for name, want := range map[string]string{
|
||||
"vp-unserved": "not served by two-hostsvc: its vms get no dns nor ntp",
|
||||
"vp-noaddr": "169.254.169.253 missing on hostsvc (in netns)",
|
||||
"vp-noredirect": "dns and ntp redirection to 169.254.169.253 missing or incomplete (chain TWO-HOSTSVC)",
|
||||
} {
|
||||
got := r.forName(name)
|
||||
if len(got) != 1 || got[0].kind != "vpc" || got[0].problem != want {
|
||||
t.Errorf("%s: notifications %+v, want one vpc problem %q", name, got, want)
|
||||
}
|
||||
}
|
||||
if got := r.forName("vp-ok"); len(got) != 0 {
|
||||
t.Errorf("vp-ok: notifications %+v, want none", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckHostSvc_ReportsUnreadableChecks(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
seedResource(t, db, prefixVPC, "vp-a", state.Running)
|
||||
p := healthyHostSvc("vp-a")
|
||||
p.readErr = errors.New("cannot enter netns")
|
||||
r := &recorder{}
|
||||
if err := CheckHostSvc(db, p, r); err != nil {
|
||||
t.Fatalf("CheckHostSvc: %v", err)
|
||||
}
|
||||
for _, want := range []string{
|
||||
"169.254.169.253 on hostsvc unreadable: cannot enter netns",
|
||||
"dns and ntp redirection unreadable: cannot enter netns",
|
||||
} {
|
||||
if !r.hasProblemContaining(want) {
|
||||
t.Errorf("missing %q in %+v", want, r.calls)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,39 +0,0 @@
|
|||
package watchdog
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
||||
"git.g3e.fr/syonad/two/internal/netif"
|
||||
"git.g3e.fr/syonad/two/internal/watchdog/notify"
|
||||
)
|
||||
|
||||
const kindPublic = "public"
|
||||
|
||||
type publicProbe interface {
|
||||
LinkProblem(name string) string
|
||||
VxlanPorts(bridge string) ([]string, error)
|
||||
}
|
||||
|
||||
type systemPublicProbe struct{}
|
||||
|
||||
func (systemPublicProbe) LinkProblem(name string) string { return linkProblem(name) }
|
||||
|
||||
func (systemPublicProbe) VxlanPorts(bridge string) ([]string, error) {
|
||||
return netif.BridgeVxlanPorts(bridge)
|
||||
}
|
||||
|
||||
func CheckPublicSegment(cfg *configuration.Config, p publicProbe, n notify.Notifier) {
|
||||
bridge := cfg.PublicBridge()
|
||||
if problem := p.LinkProblem(bridge); problem != "" {
|
||||
n.Notify(kindPublic, bridge, problem+" (public bridge, interfaces.internet): the vpcs of this host have no public segment")
|
||||
return
|
||||
}
|
||||
ports, err := p.VxlanPorts(bridge)
|
||||
switch {
|
||||
case err != nil:
|
||||
n.Notify(kindPublic, bridge, fmt.Sprintf("vxlan ports unreadable: %v", err))
|
||||
case len(ports) == 0:
|
||||
n.Notify(kindPublic, bridge, "no vxlan port: the public segment is unreachable from this host")
|
||||
}
|
||||
}
|
||||
|
|
@ -1,77 +0,0 @@
|
|||
package watchdog
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
||||
)
|
||||
|
||||
type fakePublicProbe struct {
|
||||
problems map[string]string
|
||||
ports []string
|
||||
err error
|
||||
asked []string
|
||||
}
|
||||
|
||||
func (f *fakePublicProbe) LinkProblem(name string) string {
|
||||
f.asked = append(f.asked, name)
|
||||
return f.problems[name]
|
||||
}
|
||||
|
||||
func (f *fakePublicProbe) VxlanPorts(bridge string) ([]string, error) {
|
||||
f.asked = append(f.asked, "ports:"+bridge)
|
||||
return f.ports, f.err
|
||||
}
|
||||
|
||||
func TestCheckPublicSegment_SilentWithAVxlanPort(t *testing.T) {
|
||||
r := &recorder{}
|
||||
p := &fakePublicProbe{ports: []string{"vxlan-public"}}
|
||||
CheckPublicSegment(&configuration.Config{}, p, r)
|
||||
if len(r.calls) != 0 {
|
||||
t.Errorf("notifications = %+v, aucune attendue", r.calls)
|
||||
}
|
||||
if len(p.asked) != 2 || p.asked[0] != "br-public" || p.asked[1] != "ports:br-public" {
|
||||
t.Errorf("sondes = %q, br-public attendu par défaut", p.asked)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckPublicSegment_FollowsInterfacesInternet(t *testing.T) {
|
||||
r := &recorder{}
|
||||
p := &fakePublicProbe{ports: []string{"vxlan-public"}}
|
||||
CheckPublicSegment(&configuration.Config{DefaultInterface: "br-000000", Interfaces: map[string]string{"internet": "br-wan"}}, p, r)
|
||||
if len(p.asked) != 2 || p.asked[0] != "br-wan" || p.asked[1] != "ports:br-wan" {
|
||||
t.Errorf("sondes = %q, br-wan attendu", p.asked)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckPublicSegment_ReportsAMissingBridgeWithoutReadingPorts(t *testing.T) {
|
||||
r := &recorder{}
|
||||
p := &fakePublicProbe{problems: map[string]string{"br-public": "interface br-public not found: Link not found"}}
|
||||
CheckPublicSegment(&configuration.Config{}, p, r)
|
||||
got := r.forName("br-public")
|
||||
want := "interface br-public not found: Link not found (public bridge, interfaces.internet): the vpcs of this host have no public segment"
|
||||
if len(got) != 1 || got[0].kind != "public" || got[0].problem != want {
|
||||
t.Fatalf("notifications = %+v, une attendue : %q", r.calls, want)
|
||||
}
|
||||
if len(p.asked) != 1 {
|
||||
t.Errorf("ports lus alors que le bridge manque : %q", p.asked)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckPublicSegment_ReportsABridgeWithoutVxlan(t *testing.T) {
|
||||
r := &recorder{}
|
||||
CheckPublicSegment(&configuration.Config{}, &fakePublicProbe{}, r)
|
||||
got := r.forName("br-public")
|
||||
if len(got) != 1 || got[0].problem != "no vxlan port: the public segment is unreachable from this host" {
|
||||
t.Errorf("notifications = %+v", r.calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckPublicSegment_ReportsUnreadablePorts(t *testing.T) {
|
||||
r := &recorder{}
|
||||
CheckPublicSegment(&configuration.Config{}, &fakePublicProbe{err: errors.New("netlink: permission denied")}, r)
|
||||
if !r.hasProblemContaining("vxlan ports unreadable: netlink: permission denied") {
|
||||
t.Errorf("notifications = %+v", r.calls)
|
||||
}
|
||||
}
|
||||
|
|
@ -10,11 +10,9 @@ 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"
|
||||
|
||||
|
|
@ -73,7 +71,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, name, vpc, idx, n)
|
||||
checkVMTap(entries[prefix+"tap_id"], name, vpc, idx, n)
|
||||
}
|
||||
|
||||
checkVMQemu(cfg, name, n)
|
||||
|
|
@ -101,12 +99,7 @@ func nicIndexes(entries map[string]string, prefix string) []int {
|
|||
return indexes
|
||||
}
|
||||
|
||||
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"]
|
||||
func checkVMTap(raw, name, vpc string, idx int, n notify.Notifier) {
|
||||
if raw == "" {
|
||||
n.Notify(kindVM, name, fmt.Sprintf("interface %d has no tap_id in database", idx))
|
||||
return
|
||||
|
|
@ -126,14 +119,6 @@ func checkVMTap(entries map[string]string, prefix, name, vpc string, idx int, n
|
|||
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,27 +34,6 @@ 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{}
|
||||
|
|
|
|||
|
|
@ -23,8 +23,6 @@ type Watchdog struct {
|
|||
interval time.Duration
|
||||
dbusDown bool
|
||||
l3 l3Probe
|
||||
hostsvc hostSvcProbe
|
||||
public publicProbe
|
||||
}
|
||||
|
||||
func New(db *badger.DB, cfg *configuration.Config, n notify.Notifier, logger *slog.Logger, interval time.Duration) *Watchdog {
|
||||
|
|
@ -43,8 +41,6 @@ func New(db *badger.DB, cfg *configuration.Config, n notify.Notifier, logger *sl
|
|||
logger: logger,
|
||||
interval: interval,
|
||||
l3: systemProbe{frr: frr.NewClient()},
|
||||
hostsvc: newSystemHostSvcProbe(),
|
||||
public: systemPublicProbe{},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -72,16 +68,12 @@ func (w *Watchdog) tick() {
|
|||
if err := CheckVPCs(w.db, w.notifier); err != nil {
|
||||
w.logger.Error("watchdog: vpc check failed", "err", err)
|
||||
}
|
||||
if err := CheckHostSvc(w.db, w.hostsvc, w.notifier); err != nil {
|
||||
w.logger.Error("watchdog: hostsvc check failed", "err", err)
|
||||
}
|
||||
if err := CheckSubnets(w.db, w.cfg, u, w.notifier); err != nil {
|
||||
w.logger.Error("watchdog: subnet check failed", "err", err)
|
||||
}
|
||||
if err := CheckVMs(w.db, w.cfg, u, w.notifier); err != nil {
|
||||
w.logger.Error("watchdog: vm check failed", "err", err)
|
||||
}
|
||||
CheckPublicSegment(w.cfg, w.public, w.notifier)
|
||||
CheckBGP(w.cfg, w.l3, w.notifier)
|
||||
if err := CheckL3VPCs(w.db, w.cfg, w.l3, w.notifier); err != nil {
|
||||
w.logger.Error("watchdog: l3 vpc check failed", "err", err)
|
||||
|
|
|
|||
|
|
@ -74,13 +74,6 @@ func TestNew_IntervalleValideConserve(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestNew_ChecksThePublicSegmentOnTheSystem(t *testing.T) {
|
||||
w := New(newTestDB(t), testCfg(t), &recorder{}, nil, time.Second)
|
||||
if _, ok := w.public.(systemPublicProbe); !ok {
|
||||
t.Errorf("sonde du segment public = %T, systemPublicProbe attendue", w.public)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNew_LoggerNil(t *testing.T) {
|
||||
w := New(newTestDB(t), testCfg(t), &recorder{}, nil, time.Second)
|
||||
if w.logger == nil {
|
||||
|
|
@ -148,7 +141,6 @@ func TestRun_ErreurDeBaseLogueeEtBoucleContinue(t *testing.T) {
|
|||
n := newChanNotifier()
|
||||
l, buf := testLogger()
|
||||
w := New(db, testCfg(t), n, l, 10*time.Millisecond)
|
||||
w.public = &fakePublicProbe{ports: []string{"vxlan-public"}}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 300*time.Millisecond)
|
||||
defer cancel()
|
||||
|
|
|
|||
|
|
@ -14,49 +14,6 @@
|
|||
quand ses VM sont en `deleting`, et attend leur suppression jusqu'à `timeout_subnet_stop`. Le
|
||||
409 reste pour une VM ni supprimée ni en `deleting`
|
||||
|
||||
**DNS et NTP des VM par l'hyperviseur : `two-hostsvc` (#86)**
|
||||
|
||||
- Nouveau service `two-hostsvc.service`, un par hyperviseur, démarré avant l'agent : il relaie
|
||||
DNS (UDP et TCP) et NTP pour les VM de tous les VPC, depuis l'adresse `169.254.169.253` posée
|
||||
dans le netns de chaque VPC. Une VM sans sortie Internet garde DNS et NTP, sans route entre la
|
||||
VRF du VPC et l'hôte
|
||||
- **Le DHCP annonce l'`interface_ip` du subnet comme DNS (option 6) et NTP (option 42)**, dans les
|
||||
deux backends ; `1.1.1.1` et `8.8.8.8` ne sont plus annoncés. Les requêtes des VM vers leur
|
||||
gateway sur 53 et 123 sont redirigées vers `two-hostsvc` ; celles vers un résolveur extérieur
|
||||
ne le sont pas
|
||||
- Nouvelle section `hostsvc` de la configuration : `dns_upstream` et `ntp_upstream` (serveurs
|
||||
interrogés en premier ; à défaut, le résolveur et l'horloge de l'hôte) et `rate_limit_per_vpc`
|
||||
(100 requêtes par seconde par défaut, DNS et NTP confondus)
|
||||
- **La création d'un VPC échoue si `two-hostsvc` ne répond pas**, plutôt que de créer un VPC sans
|
||||
DNS ni NTP. Le watchdog signale un `two-hostsvc` injoignable et, pour chaque VPC, un élément
|
||||
manquant (adresse, redirection, VPC non servi)
|
||||
|
||||
**Filtrage au port de chaque VM (#76)**
|
||||
|
||||
- Chaque interface de VM ne peut émettre que depuis sa propre MAC et sa propre IP. Sont rejetés
|
||||
à la sortie du tap : une MAC source étrangère, l'ARP dont la MAC ou l'adresse d'émetteur n'est
|
||||
pas celle de l'interface (sauf la sonde `0.0.0.0`), l'IPv4 d'une autre source (sauf `0.0.0.0`
|
||||
vers le port 67), les réponses de serveur DHCP et les trames d'un autre type qu'IPv4, ARP ou
|
||||
IPv6. Une VM ne peut plus se faire passer pour la gateway, une autre VM ou un serveur DHCP
|
||||
auprès de ses voisines. L'IPv6 n'est pas filtrée (#61)
|
||||
- Nouveau champ `dhcp_server` d'une interface (faux par défaut, toujours renvoyé par le GET) : il
|
||||
autorise cette interface à répondre en DHCP, par exemple pour servir le PXE de machines diskless
|
||||
sur un subnet `bridge`
|
||||
- Les règles sont posées avant la création du tap et retirées après sa suppression ; le watchdog
|
||||
signale une interface d'une VM `running` dont les règles manquent ou ont été modifiées
|
||||
|
||||
**Segment public de l'hôte (#87)**
|
||||
|
||||
- `deploy.sh -i` crée `vxlan-public`, VXLAN du segment public partagé avec les bordures, sur le
|
||||
bridge public (`-P`, `br-public`) : VNI de la nouvelle option `-N`/`--public_vni` (1 par
|
||||
défaut), VTEP = IPv4 du bridge principal, sans apprentissage ; recréé si le VNI ou le VTEP
|
||||
change. FRR l'annonce par `advertise-all-vni`
|
||||
- Plan de VNI : 1 pour le segment public, 2–65535 pour les L3VNI, 65536 et au-delà pour les
|
||||
subnets. L'agent ne vérifie pas : l'appelant n'alloue jamais le VNI du segment public
|
||||
- L'agent raccorde le veth `vp-<id>-e` de chaque VPC au bridge de `interfaces.internet`
|
||||
(`br-public` par défaut, jamais `default_interface`), au lieu d'un nom en dur
|
||||
- Le watchdog signale un bridge public absent, éteint ou sans port VXLAN
|
||||
|
||||
## Correctifs
|
||||
|
||||
- **Chaque VRF de VPC porte une route `unreachable default` (#81).** Une recherche qui échouait
|
||||
|
|
@ -80,32 +37,9 @@
|
|||
- **La suppression d'un VPC échoue si l'un de ses subnets échoue (#79).** Elle attendait qu'aucun
|
||||
subnet ne soit plus en `deleting`, puis supprimait le VPC même si un subnet était passé en
|
||||
`error`. Il en va de même pour un subnet dont une VM attendue passe en `error`
|
||||
- **Un thread de l'agent ne reste plus dans le netns d'un VPC (#91).** Après une opération dans
|
||||
un netns, l'agent revenait au netns du thread principal du processus et non à celui du thread
|
||||
courant. Si le thread principal était lui-même dans un VPC à cet instant, le thread restait dans
|
||||
ce VPC et la goroutine suivante y travaillait : en lab, la suppression d'un subnet a échoué sur
|
||||
`delete vxlan: Link not found` et le watchdog a signalé `br-000000` et des VRF absents. L'agent
|
||||
revient désormais au netns du thread courant, et un thread qui ne peut pas y revenir est
|
||||
détruit au lieu d'être réutilisé. La création d'un netns est corrigée de la même façon
|
||||
|
||||
## Mise à jour depuis la 0.2.0
|
||||
|
||||
- **Les règles de pont passent d'ebtables à nftables (#76)** : l'agent appelle `nft` et pose ses
|
||||
règles dans une table `bridge two` du netns de chaque VPC. `bootstrap_kvm.sh` installe
|
||||
`nftables` à la place d'`ebtables`. Les règles ebtables des subnets existants ne sont ni
|
||||
reprises ni retirées : vider l'hyperviseur (VM, subnets, VPC) avant la mise à jour, puis les
|
||||
recréer
|
||||
- **Une VM ne peut plus émettre depuis une adresse qui n'est pas la sienne (#76)** : une VM qui
|
||||
route pour d'autres machines, porte une adresse supplémentaire ou sert le DHCP perd ce trafic.
|
||||
Pour un serveur DHCP, créer l'interface avec `dhcp_server: true`
|
||||
|
||||
- **Relancer `deploy.sh -i` (#87)** sur chaque hyperviseur pour créer `vxlan-public` ; sans lui,
|
||||
le watchdog signale un bridge public sans port VXLAN à chaque tick. Un VPC routé dont le
|
||||
`l3vni` vaut 1 doit être recréé avec un autre L3VNI avant : sinon la création de
|
||||
`vxlan-public` échoue sur ce VNI déjà porté
|
||||
- **`interfaces.internet` désigne désormais le bridge public (#87)** : une valeur héritée de
|
||||
l'ancien exemple (`internet: br-000000`) raccorderait les VPC à l'underlay. La retirer, ou la
|
||||
mettre à `br-public`
|
||||
- **`default_route: true` exige une `gateway` hors mode `bridge`** : un appel qui l'omettait en
|
||||
`vxlan` reçoit désormais un 400. Les subnets existants gardent la route par défaut qu'ils
|
||||
annonçaient jusqu'à leur recréation
|
||||
|
|
@ -118,12 +52,3 @@
|
|||
et les valeurs par défaut de `times` s'appliquent. Une valeur personnalisée de
|
||||
`dispatcher.timeout_seconds` est à reporter dans `times.timeout_vms_stop` (et dans les délais
|
||||
qui en dépendent)
|
||||
- **DNS et NTP (#86) : vider l'hyperviseur avant la mise à jour, puis recréer les VPC.** Les VPC
|
||||
créés par la 0.2.0 n'ont ni l'adresse `169.254.169.253`, ni la redirection, ni de place dans
|
||||
`two-hostsvc`, et l'agent ne les reprend pas au démarrage. Or le serveur DHCP intégré (backend
|
||||
`two`) annonce la gateway comme DNS dès son redémarrage par `deploy.sh` : au renouvellement
|
||||
de leur bail (au plus 6 h), les VM de ces VPC perdraient leur DNS. Avec dnsmasq, le fichier de
|
||||
configuration n'est pas régénéré et les VM gardent `1.1.1.1`. Dans les deux cas, le watchdog
|
||||
signale ces VPC à chaque tick. Procédure : supprimer VM, subnets et VPC de l'hyperviseur,
|
||||
mettre à jour avec `deploy.sh` (qui installe et démarre `two-hostsvc`), vérifier
|
||||
`systemctl status two-hostsvc`, puis recréer VPC, subnets et VM
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@
|
|||
# L'host est stateless (root sur tmpfs) : rien de ce qui suit ne survit à un
|
||||
# reboot, deploy.sh --bootstrap est rejoué à chaque démarrage.
|
||||
|
||||
KVM_PACKAGES="qemu-system-x86 qemu-utils ovmf dnsmasq nftables iptables nfs-common jq curl"
|
||||
KVM_PACKAGES="qemu-system-x86 qemu-utils ovmf dnsmasq ebtables iptables nfs-common jq curl"
|
||||
|
||||
kvm_packages () {
|
||||
local DRY_RUN="${1}"
|
||||
|
|
@ -77,8 +77,8 @@ kvm_network () {
|
|||
|
||||
info "réseau"
|
||||
|
||||
# Bridge public : sans adresse. Il reçoit le VXLAN du segment public
|
||||
# (kvm_public_segment) et les veth vp-<id>-e des VPC (interfaces.internet).
|
||||
# Bridge public : créé à vide, réservé pour un usage futur. Aucune adresse,
|
||||
# aucun esclave, pas référencé dans la config agent.
|
||||
kvm_ensure_bridge "${DRY_RUN}" "${PUBLIC_BRIDGE}"
|
||||
|
||||
MASTER=""
|
||||
|
|
@ -153,49 +153,6 @@ EOF
|
|||
info " rollback désarmé"
|
||||
}
|
||||
|
||||
# VXLAN du segment public, partagé avec les bordures, esclave du bridge public.
|
||||
# VTEP = IPv4 du bridge principal, comme les VXLAN de l'agent ; sans
|
||||
# apprentissage, les MAC distantes viennent d'EVPN. Recréé si le VNI ou le VTEP
|
||||
# a changé.
|
||||
kvm_public_segment () {
|
||||
local DRY_RUN="${1}"
|
||||
local WITH_NETWORK="${2}"
|
||||
local BRIDGE="${3}"
|
||||
local PUBLIC_BRIDGE="${4}"
|
||||
local PUBLIC_VNI="${5}"
|
||||
local NAME="vxlan-public"
|
||||
local VTEP CURRENT=""
|
||||
|
||||
[[ ${WITH_NETWORK} -eq ${FLAGS_TRUE} ]] || return 0
|
||||
[[ "${PUBLIC_VNI}" =~ ^[0-9]+$ ]] && (( PUBLIC_VNI >= 1 && PUBLIC_VNI <= 16777215 )) \
|
||||
|| die "VNI du segment public invalide : ${PUBLIC_VNI} (1-16777215)"
|
||||
|
||||
info "segment public : ${NAME}, VNI ${PUBLIC_VNI}, sur ${PUBLIC_BRIDGE}"
|
||||
|
||||
if [[ ${DRY_RUN} -eq ${FLAGS_TRUE} ]]
|
||||
then
|
||||
VTEP="<ip(${BRIDGE})>"
|
||||
else
|
||||
VTEP=$(ip -j addr show dev "${BRIDGE}" | jq -r '[.[0].addr_info[] | select(.family=="inet" and .scope=="global")][0].local // ""')
|
||||
[[ -n "${VTEP}" ]] || die "aucune adresse IPv4 globale sur ${BRIDGE} pour le VTEP du segment public"
|
||||
if ip link show dev "${NAME}" >/dev/null 2>&1
|
||||
then
|
||||
CURRENT=$(ip -j -d link show dev "${NAME}" | jq -r '.[0].linkinfo.info_data | "\(.id) \(.local)"')
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ -n "${CURRENT}" && "${CURRENT}" != "${PUBLIC_VNI} ${VTEP}" ]]
|
||||
then
|
||||
info " ${NAME} porte ${CURRENT} (VNI VTEP) : recréé"
|
||||
run "${DRY_RUN}" "ip link del dev '${NAME}'"
|
||||
CURRENT=""
|
||||
fi
|
||||
[[ -n "${CURRENT}" ]] \
|
||||
|| run "${DRY_RUN}" "ip link add '${NAME}' type vxlan id ${PUBLIC_VNI} local '${VTEP}' dstport 4789 nolearning"
|
||||
run "${DRY_RUN}" "ip link set dev '${NAME}' mtu 1500 master '${PUBLIC_BRIDGE}'"
|
||||
run "${DRY_RUN}" "ip link set up dev '${NAME}'"
|
||||
}
|
||||
|
||||
# Point d'entrée appelé par deploy.sh --bootstrap.
|
||||
bootstrap_host () {
|
||||
local DRY_RUN="${1}"
|
||||
|
|
@ -205,10 +162,8 @@ bootstrap_host () {
|
|||
local BRIDGE="${5}"
|
||||
local PUBLIC_BRIDGE="${6}"
|
||||
local ROLLBACK_DELAY="${7}"
|
||||
local PUBLIC_VNI="${8:-1}"
|
||||
|
||||
kvm_packages "${DRY_RUN}" "${WITH_PACKAGES}"
|
||||
kvm_kernel "${DRY_RUN}"
|
||||
kvm_network "${DRY_RUN}" "${WITH_NETWORK}" "${UPLINK}" "${BRIDGE}" "${PUBLIC_BRIDGE}" "${ROLLBACK_DELAY}"
|
||||
kvm_public_segment "${DRY_RUN}" "${WITH_NETWORK}" "${BRIDGE}" "${PUBLIC_BRIDGE}" "${PUBLIC_VNI}"
|
||||
kvm_packages "${DRY_RUN}" "${WITH_PACKAGES}"
|
||||
kvm_kernel "${DRY_RUN}"
|
||||
kvm_network "${DRY_RUN}" "${WITH_NETWORK}" "${UPLINK}" "${BRIDGE}" "${PUBLIC_BRIDGE}" "${ROLLBACK_DELAY}"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -116,7 +116,7 @@ start_services () {
|
|||
|
||||
profile_units () {
|
||||
case "${1}" in
|
||||
kvm) echo "two-hostsvc.service agent.service dnsmasq@.service dhcp@.service metadata@.service" ;;
|
||||
kvm) echo "agent.service dnsmasq@.service dhcp@.service metadata@.service" ;;
|
||||
intel) echo "" ;;
|
||||
*) return 1 ;;
|
||||
esac
|
||||
|
|
@ -124,7 +124,7 @@ profile_units () {
|
|||
|
||||
profile_binaries () {
|
||||
case "${1}" in
|
||||
kvm) echo "agent metadata dhcp two-hostsvc run-dnsmasq-in-netns.sh run-dhcp-in-netns.sh" ;;
|
||||
kvm) echo "agent metadata dhcp run-dnsmasq-in-netns.sh run-dhcp-in-netns.sh" ;;
|
||||
intel) echo "" ;;
|
||||
*) return 1 ;;
|
||||
esac
|
||||
|
|
@ -159,7 +159,7 @@ run_bootstrap () {
|
|||
|| die "bootstrap_host() introuvable — ni ${SCRIPTS_DIR}/${NAME} ni ${BOOTSTRAP_URL} n'ont pu être chargés"
|
||||
|
||||
bootstrap_host "${DRY_RUN}" "${FLAGS_packages}" "${FLAGS_network}" \
|
||||
"${FLAGS_uplink}" "${FLAGS_bridge}" "${FLAGS_pub_bridge}" "${FLAGS_rollback}" "${FLAGS_public_vni}"
|
||||
"${FLAGS_uplink}" "${FLAGS_bridge}" "${FLAGS_pub_bridge}" "${FLAGS_rollback}"
|
||||
}
|
||||
|
||||
resolve_tag () {
|
||||
|
|
@ -335,8 +335,7 @@ main () {
|
|||
DEFINE_boolean 'network' true 'Configure bridges during --bootstrap' 'n'
|
||||
DEFINE_string 'uplink' 'eno1' 'Physical uplink interface' 'u'
|
||||
DEFINE_string 'bridge' 'br-000000' 'Main bridge, uplink is enslaved to it' 'B'
|
||||
DEFINE_string 'pub_bridge' 'br-public' 'Public bridge, carries vxlan-public' 'P'
|
||||
DEFINE_integer 'public_vni' 1 'VNI of the public segment (vxlan-public)' 'N'
|
||||
DEFINE_string 'pub_bridge' 'br-public' 'Reserved empty bridge' 'P'
|
||||
DEFINE_integer 'rollback' 120 'Rollback reboot delay in seconds' 'R'
|
||||
DEFINE_boolean 'verify' true 'Check assets against SHA256SUMS' 'V'
|
||||
|
||||
|
|
|
|||
|
|
@ -1,25 +0,0 @@
|
|||
[Unit]
|
||||
Description=two DNS and NTP relay of the VPCs
|
||||
After=network.target
|
||||
Before=agent.service
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
ExecStart=/opt/two/bin/two-hostsvc
|
||||
Restart=on-failure
|
||||
RuntimeDirectory=two/hostsvc
|
||||
RuntimeDirectoryMode=0700
|
||||
RuntimeDirectoryPreserve=yes
|
||||
CapabilityBoundingSet=CAP_SYS_ADMIN CAP_NET_BIND_SERVICE CAP_DAC_READ_SEARCH
|
||||
NoNewPrivileges=yes
|
||||
ProtectSystem=strict
|
||||
ProtectHome=yes
|
||||
PrivateTmp=yes
|
||||
PrivateDevices=yes
|
||||
ProtectKernelModules=yes
|
||||
ProtectControlGroups=yes
|
||||
LockPersonality=yes
|
||||
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 AF_NETLINK
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
|
|
@ -3,47 +3,34 @@
|
|||
E2E_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
REPO_DIR="$(cd "${E2E_DIR}/../.." && pwd)"
|
||||
BIN_DIR="${BIN_DIR:-${E2E_DIR}/topologies/bin}"
|
||||
SCRIPTS_DIR="${SCRIPTS_DIR:-${E2E_DIR}/topologies/scripts}"
|
||||
BINARIES="agent dhcp metadata two-hostsvc"
|
||||
UNITS="two-hostsvc.service"
|
||||
SCRIPTS="bootstrap_kvm.sh"
|
||||
BINARIES="agent dhcp metadata"
|
||||
|
||||
usage () {
|
||||
cat >&2 <<USAGE
|
||||
usage: ${0##*/} [--clean]
|
||||
|
||||
sans option : compile ${BINARIES} pour linux/amd64 depuis l'arbre courant dans ${BIN_DIR}
|
||||
et y copie ${UNITS}, copie ${SCRIPTS} dans ${SCRIPTS_DIR} ; le prochain lab
|
||||
les installe par-dessus la release épinglée dans topologies/provision/hypervisor.sh
|
||||
--clean : vide ${BIN_DIR} et ${SCRIPTS_DIR} ; le prochain lab teste la release seule
|
||||
sans option : compile ${BINARIES} pour linux/amd64 depuis l'arbre courant dans ${BIN_DIR} ;
|
||||
le prochain lab les installe par-dessus la release épinglée dans
|
||||
topologies/provision/hypervisor.sh
|
||||
--clean : vide ${BIN_DIR} ; le prochain lab teste la release seule
|
||||
USAGE
|
||||
exit 2
|
||||
}
|
||||
|
||||
clean () {
|
||||
local file script
|
||||
for file in ${BINARIES} ${UNITS}; do
|
||||
rm -f "${BIN_DIR}/${file}" || return 1
|
||||
done
|
||||
for script in ${SCRIPTS}; do
|
||||
rm -f "${SCRIPTS_DIR}/${script}" || return 1
|
||||
local binary
|
||||
for binary in ${BINARIES}; do
|
||||
rm -f "${BIN_DIR}/${binary}" || return 1
|
||||
done
|
||||
}
|
||||
|
||||
build () {
|
||||
local binary unit script
|
||||
local binary
|
||||
for binary in ${BINARIES}; do
|
||||
echo "== ${binary}" >&2
|
||||
(cd "${REPO_DIR}" && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o "${BIN_DIR}/${binary}" "./cmd/${binary}") || return 1
|
||||
done
|
||||
for unit in ${UNITS}; do
|
||||
cp "${REPO_DIR}/systemd/${unit}" "${BIN_DIR}/${unit}" || return 1
|
||||
done
|
||||
for script in ${SCRIPTS}; do
|
||||
echo "== ${script}" >&2
|
||||
cp "${REPO_DIR}/scripts/${script}" "${SCRIPTS_DIR}/${script}" || return 1
|
||||
done
|
||||
(cd "${BIN_DIR}" && sha256sum ${BINARIES} ${UNITS} 2>/dev/null || shasum -a 256 ${BINARIES} ${UNITS})
|
||||
(cd "${BIN_DIR}" && sha256sum ${BINARIES} 2>/dev/null || shasum -a 256 ${BINARIES})
|
||||
}
|
||||
|
||||
main () {
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@ host_state () {
|
|||
echo "## liens"; ip -o link show | awk -F': ' '{print $2}' | cut -d@ -f1 | sort
|
||||
echo "## adresses"; ip -o -4 addr show | awk '{print $2, $4}' | sort
|
||||
echo "## routes"; ip -4 route show table all | grep -v '^local \|^broadcast ' | sort
|
||||
echo "## nftables bridge"; nft list ruleset bridge
|
||||
echo "## ebtables"; ebtables -t filter -L; ebtables -t nat -L
|
||||
echo "## iptables"; iptables-save | grep -v '^#' | sed 's/ \[[0-9]*:[0-9]*\]$//'
|
||||
echo "## frr"; vtysh -c 'show running-config' | grep -v '^Building\|^Current configuration\|^!'
|
||||
echo "## frr.conf"; sha256sum /etc/frr/frr.conf | cut -d' ' -f1
|
||||
|
|
|
|||
|
|
@ -139,8 +139,6 @@ if cmd == 'arp':
|
|||
emit(a[0], arp_req(ifmac(a[0]), a[1], a[2]), int(a[3]))
|
||||
elif cmd == 'dhcp':
|
||||
emit(a[0], udp_bcast(ifmac(a[0]), a[1], 67, int(a[2]), b'two-e2e-dhcp'.ljust(240, b'\0')), int(a[3]))
|
||||
elif cmd == 'udp':
|
||||
emit(a[0], udp_bcast(a[5] if len(a) > 5 else ifmac(a[0]), a[1], int(a[2]), int(a[3]), b'two-e2e-udp'.ljust(46, b'\0')), int(a[4]))
|
||||
elif cmd == 'garp':
|
||||
emit(a[0], garp(ifmac(a[0]), a[1]), int(a[2]))
|
||||
elif cmd == 'marker':
|
||||
|
|
@ -168,12 +166,6 @@ vm_frames () {
|
|||
vm_ssh "${vpc}" "${ip}" "echo $(frames_py | base64 | tr -d '\n') | base64 -d > /tmp/frames.py && sudo python3 /tmp/frames.py $*"
|
||||
}
|
||||
|
||||
ns_frames () {
|
||||
local ns="${1}"
|
||||
shift
|
||||
frames_py | ip netns exec "${ns}" python3 - "$@"
|
||||
}
|
||||
|
||||
sniff_start () {
|
||||
local out="${1}"
|
||||
shift
|
||||
|
|
@ -188,41 +180,24 @@ vm_sniff_start () {
|
|||
sleep 3
|
||||
}
|
||||
|
||||
nft_table () {
|
||||
local tables
|
||||
tables=$(ip netns exec "${1}" nft list tables bridge) || return 1
|
||||
grep -qx 'table bridge two' <<<"${tables}" || return 0
|
||||
ip netns exec "${1}" nft list table bridge two
|
||||
ebt_mac () {
|
||||
awk -F: 'BEGIN {OFS = ":"} {for (i = 1; i <= NF; i++) sub(/^0/, "", $i); $1 = $1; print tolower($0)}' <<<"${1}"
|
||||
}
|
||||
|
||||
nft_rules () {
|
||||
local table
|
||||
table=$(nft_table "${1}") || return 1
|
||||
awk -v p="${2}" '$1 == "chain" {c = ($2 == "pre-" p || $2 == "fwd-" p || $2 == "out-" p) ? $2 : ""; next} $1 == "}" {c = ""; next} c != "" && NF {$1 = $1; print c, $0}' <<<"${table}"
|
||||
ebt_rules () {
|
||||
{ ip netns exec "${1}" ebtables -t nat -L; ip netns exec "${1}" ebtables -t filter -L; } | grep -- "${2}"
|
||||
}
|
||||
|
||||
nft_count () {
|
||||
nft_rules "${1}" "${2}" | cut -d' ' -f2- | grep -cxF -- "${3}"
|
||||
ebt_confined () {
|
||||
ebt_rules "${1}" "${2}" | grep -ciE -- "-s ($(ebt_mac "${3}")|${3}) .*DROP"
|
||||
}
|
||||
|
||||
nft_confined () {
|
||||
nft_count "${1}" "${2}" "ether saddr $(tr 'A-F' 'a-f' <<<"${3}") drop"
|
||||
ebt_arp_drop () {
|
||||
ebt_rules "${1}" "${2}" | grep -i arp | grep -i request | grep -q -- "${3} "
|
||||
}
|
||||
|
||||
nft_arp_drop () {
|
||||
[ "$(nft_count "${1}" "${2}" "arp operation request arp daddr ip ${3} drop")" -gt 0 ]
|
||||
}
|
||||
|
||||
nft_dhcp_drop () {
|
||||
[ "$(nft_count "${1}" "${2}" "ip saddr ${3} udp sport 67-68 udp dport 67-68 drop")" -gt 0 ]
|
||||
}
|
||||
|
||||
nft_tap_of () {
|
||||
nft_table "${1}" | awk -v r="ether saddr != $(tr 'A-F' 'a-f' <<<"${2}") drop" '$1 == "chain" {c = $2; next} {line = $0; sub(/^[ \t]+/, "", line)} line == r && c ~ /^pre-tap/ {print substr(c, 5)}'
|
||||
}
|
||||
|
||||
nft_handle () {
|
||||
ip netns exec "${1}" nft -a list chain bridge two "${2}" | awk -v r="${3}" '{line = $0; sub(/^[ \t]+/, "", line); if (sub(/ # handle [0-9]+$/, "", line) && line == r) print $NF}'
|
||||
ebt_dhcp_drop () {
|
||||
ebt_rules "${1}" "${2}" | grep -i udp | grep -- "${3} " | grep -q "67:68"
|
||||
}
|
||||
|
||||
vrrp_expected_master () {
|
||||
|
|
|
|||
|
|
@ -122,7 +122,6 @@ vm_json () {
|
|||
sn=${2%%=*}; rest=${2#*=}; ip=${rest%%=*}; mac=$(mac_for_ip "${ip}")
|
||||
[ "${rest}" != "${ip}" ] && mac=${rest#*=}
|
||||
nics=$(jq -c --arg s "${sn}" --arg ip "${ip}" --arg m "${mac}" --argjson p "${primary}" '. + [{subnet: $s, ip: $ip, mac: $m, primary: $p}]' <<<"${nics}"); primary=false; shift 2 ;;
|
||||
--dhcp-server) nics=$(jq -c '.[-1].dhcp_server = true' <<<"${nics}"); shift ;;
|
||||
--disk) disks=$(jq -c --arg d "${2%%=*}" --arg p "${2#*=}" '. + [{path: $p, dev: $d}]' <<<"${disks}"); shift 2 ;;
|
||||
--sshkey) meta=$(jq -c --arg v "${2}" '. + {sshkey: $v}' <<<"${meta}"); shift 2 ;;
|
||||
--password) meta=$(jq -c --arg v "${2}" '. + {password: $v}' <<<"${meta}"); shift 2 ;;
|
||||
|
|
|
|||
|
|
@ -464,8 +464,6 @@ test_vm_json_builds_nics_disks_metadata_and_uefi () {
|
|||
[[ "${out}" == '{"name":"t21-a","memory":1024,"cpus":1,"metadata":{},"interfaces":[{"subnet":"sn-a","ip":"10.0.1.10","mac":"52:54:00:00:01:0a","primary":true}],"storage":[],"uefi":false}' ]] || fail "minimal : ${out}"
|
||||
out=$(node 'vm_json t21-m --nic sn-a=10.0.1.12=02:de:ad:be:ef:01 | jq -c ".interfaces[0] | [.ip, .mac]"')
|
||||
[[ "${out}" == '["10.0.1.12","02:de:ad:be:ef:01"]' ]] || fail "mac forcée : ${out}"
|
||||
out=$(node 'vm_json t76-p --nic sn-a=10.0.1.10 --nic sn-b=10.0.2.10 --dhcp-server | jq -c "[.interfaces[] | .dhcp_server]"')
|
||||
[[ "${out}" == '[null,true]' ]] || fail "--dhcp-server : ${out}"
|
||||
[[ $(node 'vm_json t || echo NON' 2>&1) == '{"name":"t","memory":1024,"cpus":1,"metadata":{},"interfaces":[],"storage":[],"uefi":false}' ]] || fail "vide"
|
||||
[[ $(node 'vm_json t --inconnue x || echo NON') == *NON ]] || fail "option inconnue acceptée"
|
||||
teardown
|
||||
|
|
@ -554,89 +552,30 @@ test_subnet_create_bridge_sends_mode_iface_type_and_interface_ip () {
|
|||
teardown
|
||||
}
|
||||
|
||||
nft_listing () {
|
||||
printf '%s\n' 'table bridge two {' ' map fwd_ports {' ' type ifname : verdict' ' elements = { "v-x-i" : jump fwd-v-x-i,' ' "v-y-i" : jump fwd-v-y-i }' ' }' '' \
|
||||
' chain forward {' ' type filter hook forward priority filter; policy accept;' ' oifname vmap @fwd_ports' ' }' '' \
|
||||
' chain pre-v-x-i {' ' ether saddr 02:01:00:00:0c:27 drop' ' }' '' \
|
||||
' chain fwd-v-x-i {' ' ether saddr 02:01:00:00:0c:27 drop' ' arp operation request arp daddr ip 10.2.1.254 drop' ' ip saddr 10.2.1.254 udp sport 67-68 udp dport 67-68 drop' ' ip saddr 10.2.1.254 udp dport 53 drop' ' }' '' \
|
||||
' chain fwd-v-x-ix {' ' arp operation request arp daddr ip 10.2.9.254 drop' ' }' '' \
|
||||
' chain fwd-v-y-i {' ' arp operation request arp daddr ip 10.2.2.254 drop' ' }' '}'
|
||||
}
|
||||
|
||||
test_nft_table_reads_the_two_table_only_when_present () {
|
||||
setup "nft_table : table bridge two lue si elle existe, rien sinon, échec si nft échoue"
|
||||
local listing
|
||||
listing=$(mktemp)
|
||||
nft_listing > "${listing}"
|
||||
[[ $(node "ip () { case \"\$*\" in 'netns exec ns nft list tables bridge') echo 'table bridge filter'; echo 'table bridge two' ;; 'netns exec ns nft list table bridge two') cat '${listing}' ;; *) echo \"ip \$*\"; return 1 ;; esac; }; nft_table ns") == "$(cat "${listing}")" ]] || fail "table présente non lue"
|
||||
[[ $(node "ip () { case \"\$*\" in 'netns exec ns nft list tables bridge') echo 'table bridge filter' ;; *) echo \"ip \$*\"; return 1 ;; esac; }; nft_table ns && echo VIDE") == VIDE ]] || fail "table absente : $(node "ip () { echo 'table bridge filter'; }; nft_table ns")"
|
||||
[[ $(node "ip () { return 1; }; nft_table ns || echo ÉCHEC") == ÉCHEC ]] || fail "échec de nft avalé"
|
||||
[[ $(node "nft_table () { return 1; }; nft_rules ns v-x-i || echo ÉCHEC") == ÉCHEC ]] || fail "nft_rules avale l'échec de nft_table"
|
||||
rm -f "${listing}"
|
||||
test_ebt_confined_reads_both_mac_forms () {
|
||||
setup "ebt_confined : MAC avec ou sans zéros de tête (ebtables legacy et nft)"
|
||||
local stub='ebt_rules () { printf "%s\\n" "-s 02:01:00:00:0c:27 -o v-x-i -j DROP" "-s 2:1:0:0:c:27 -i v-x-i -j DROP" "-s 02:01:00:00:0c:28 -o v-x-i -j DROP"; }; '
|
||||
[[ $(node "${stub}ebt_confined ns v-x-i 02:01:00:00:0C:27") == 2 ]] || fail "$(node "${stub}ebt_confined ns v-x-i 02:01:00:00:0C:27")"
|
||||
teardown
|
||||
}
|
||||
|
||||
test_nft_rules_reads_only_the_chains_of_the_port () {
|
||||
setup "nft_rules : règles des chaînes pre-, fwd- et out- du port, préfixées par la chaîne"
|
||||
local stub got want
|
||||
stub="nft_table () { $(declare -f nft_listing | sed 1,2d | sed '$d'); }; "
|
||||
got=$(node "${stub}nft_rules ns v-x-i")
|
||||
want=$(printf '%s\n' 'pre-v-x-i ether saddr 02:01:00:00:0c:27 drop' 'fwd-v-x-i ether saddr 02:01:00:00:0c:27 drop' 'fwd-v-x-i arp operation request arp daddr ip 10.2.1.254 drop' 'fwd-v-x-i ip saddr 10.2.1.254 udp sport 67-68 udp dport 67-68 drop' 'fwd-v-x-i ip saddr 10.2.1.254 udp dport 53 drop')
|
||||
[[ "${got}" == "${want}" ]] || fail "règles lues : ${got}"
|
||||
teardown
|
||||
}
|
||||
|
||||
test_nft_matchers_read_the_rules () {
|
||||
setup "nft_confined / nft_arp_drop / nft_dhcp_drop : règle exacte, MAC en minuscules"
|
||||
local stub
|
||||
stub="nft_table () { $(declare -f nft_listing | sed 1,2d | sed '$d'); }; "
|
||||
[[ $(node "${stub}nft_confined ns v-x-i 02:01:00:00:0C:27") == 2 ]] || fail "confinement : $(node "${stub}nft_confined ns v-x-i 02:01:00:00:0C:27")"
|
||||
[[ $(node "${stub}nft_confined ns v-x-i 02:01:00:00:0c:28") == 0 ]] || fail "autre MAC comptée"
|
||||
[[ $(node "${stub}nft_arp_drop ns v-x-i 10.2.1.254 && echo OUI") == OUI ]] || fail "ARP non lu"
|
||||
[[ $(node "${stub}nft_arp_drop ns v-x-i 10.2.1.25 || echo NON") == NON ]] || fail "10.2.1.25 pris pour 10.2.1.254"
|
||||
[[ $(node "${stub}nft_arp_drop ns v-x-i 10.2.9.254 || echo NON") == NON ]] || fail "chaîne de v-x-ix lue pour v-x-i"
|
||||
[[ $(node "${stub}nft_dhcp_drop ns v-x-i 10.2.1.254 && echo OUI") == OUI ]] || fail "DHCP non lu"
|
||||
[[ $(node "${stub}nft_dhcp_drop ns v-y-i 10.2.1.254 || echo NON") == NON ]] || fail "autre interface acceptée"
|
||||
[[ $(node "nft_rules () { echo 'fwd-v-z-i ip saddr 10.2.1.254 udp dport 53 drop'; }; nft_dhcp_drop ns v-z-i 10.2.1.254 || echo NON") == NON ]] || fail "règle UDP hors DHCP prise pour un blocage DHCP"
|
||||
teardown
|
||||
}
|
||||
|
||||
test_nft_tap_of_finds_the_tap_of_a_mac () {
|
||||
setup "nft_tap_of : tap dont la chaîne pre- n'accepte que cette MAC"
|
||||
local stub
|
||||
stub="nft_table () { printf '%s\\n' 'table bridge two {' ' chain pre-v-x-i {' ' ether saddr != 52:54:00:00:0a:05 drop' ' }' ' chain pre-tap11 {' ' ether saddr != 52:54:00:00:0a:06 drop' ' }' ' chain pre-tap22 {' ' ether saddr != 52:54:00:00:0a:05 drop' ' udp sport 67 drop' ' }' '}'; }; "
|
||||
[[ $(node "${stub}nft_tap_of ns 52:54:00:00:0A:05") == tap22 ]] || fail "$(node "${stub}nft_tap_of ns 52:54:00:00:0A:05")"
|
||||
[[ -z $(node "${stub}nft_tap_of ns 52:54:00:00:0a:07") ]] || fail "MAC inconnue trouvée"
|
||||
teardown
|
||||
}
|
||||
|
||||
test_nft_handle_finds_the_exact_rule () {
|
||||
setup "nft_handle : numéro de la règle exacte dans nft -a list chain"
|
||||
local stub
|
||||
stub="ip () { printf '%s\\n' 'table bridge two {' ' chain out-v-x-i { # handle 9' ' arp saddr ip 10.2.1.25 drop # handle 11' ' arp saddr ip 10.2.1.254 drop # handle 12' ' }' '}'; }; "
|
||||
[[ $(node "${stub}nft_handle ns out-v-x-i 'arp saddr ip 10.2.1.254 drop'") == 12 ]] || fail "$(node "${stub}nft_handle ns out-v-x-i 'arp saddr ip 10.2.1.254 drop'")"
|
||||
[[ -z $(node "${stub}nft_handle ns out-v-x-i 'arp saddr ip 10.2.1.2 drop'") ]] || fail "règle absente trouvée"
|
||||
test_ebt_mac_drops_leading_zeros_like_ebtables () {
|
||||
setup "ebt_mac : octets sans zéro de tête, en minuscules, comme ebtables -L"
|
||||
[[ $(node 'ebt_mac 02:01:00:00:0C:1d') == 2:1:0:0:c:1d ]] || fail "$(node 'ebt_mac 02:01:00:00:0C:1d')"
|
||||
[[ $(node 'ebt_mac aa:10:00:ff:01:00') == aa:10:0:ff:1:0 ]] || fail "$(node 'ebt_mac aa:10:00:ff:01:00')"
|
||||
teardown
|
||||
}
|
||||
|
||||
test_frames_builds_arp_dhcp_and_marker_frames () {
|
||||
setup "frames : requête ARP, DHCP et UDP quelconque en broadcast (somme IP juste), trame marquée"
|
||||
setup "frames : requête ARP, DHCP en broadcast (somme IP juste) et trame marquée"
|
||||
local arp dhcp mark
|
||||
arp=$(node 'FRAMES_DRY=1 FRAMES_MAC=02:00:00:00:00:0a frames arp ens3 10.35.1.10 10.35.1.254 1')
|
||||
dhcp=$(node 'FRAMES_DRY=1 FRAMES_MAC=02:00:00:00:00:0a frames dhcp ens3 10.35.1.254 68 1')
|
||||
mark=$(node 'FRAMES_DRY=1 frames marker v-x-e 02:01:00:00:0d:ad t35-x 1')
|
||||
garp=$(node 'FRAMES_DRY=1 FRAMES_MAC=02:00:00:00:00:0a frames garp ens3 10.35.1.254 1')
|
||||
udp=$(node 'FRAMES_DRY=1 FRAMES_MAC=02:00:00:00:00:0a frames udp ens3 0.0.0.0 68 53 1')
|
||||
spoof=$(node 'FRAMES_DRY=1 FRAMES_MAC=02:00:00:00:00:0a frames udp ens3 10.35.1.99 4000 4001 1 02:35:00:00:00:01')
|
||||
python3 - "${arp}" "${dhcp}" "${mark}" "${garp}" "${udp}" "${spoof}" <<'PY' || fail "trames : ${arp} / ${dhcp} / ${mark} / ${garp} / ${udp} / ${spoof}"
|
||||
python3 - "${arp}" "${dhcp}" "${mark}" "${garp}" <<'PY' || fail "trames : ${arp} / ${dhcp} / ${mark} / ${garp}"
|
||||
import socket, struct, sys
|
||||
arp, dhcp, mark, garp, udp, spoof = (bytes.fromhex(x) for x in sys.argv[1:])
|
||||
assert udp[6:12] == bytes.fromhex('02000000000a') and udp[12:14] == b'\x08\x00' and udp[23] == 17, 'udp type'
|
||||
assert udp[26:30] == socket.inet_aton('0.0.0.0') and struct.unpack('!HH', udp[34:38]) == (68, 53), 'udp source et ports'
|
||||
assert spoof[6:12] == bytes.fromhex('023500000001'), 'udp mac source imposée'
|
||||
assert spoof[26:30] == socket.inet_aton('10.35.1.99') and struct.unpack('!HH', spoof[34:38]) == (4000, 4001), 'udp ip et ports'
|
||||
s = sum(struct.unpack('!10H', spoof[14:34])); s = (s >> 16) + (s & 0xffff); s += s >> 16
|
||||
assert s & 0xffff == 0xffff, 'somme IP udp'
|
||||
arp, dhcp, mark, garp = (bytes.fromhex(x) for x in sys.argv[1:])
|
||||
assert garp[:6] == b'\xff' * 6 and garp[6:12] == bytes.fromhex('02000000000a'), 'garp mac'
|
||||
assert garp[12:14] == b'\x08\x06' and garp[20:22] == b'\x00\x02', 'garp type/op'
|
||||
assert garp[22:28] == bytes.fromhex('02000000000a') and garp[28:32] == socket.inet_aton('10.35.1.254'), 'garp émetteur'
|
||||
|
|
@ -725,6 +664,19 @@ test_log_gap_measures_the_longest_silence_in_a_window () {
|
|||
teardown
|
||||
}
|
||||
|
||||
test_ebt_matchers_read_the_ebtables_listing () {
|
||||
setup "ebt_confined / ebt_arp_drop / ebt_dhcp_drop : lecture de ebtables -L"
|
||||
local stub='ebt_rules () { printf "%s\n" "-s 2:1:0:0:c:27 -i v-x-i -j DROP" "-s 2:1:0:0:c:27 -o v-x-i -j DROP" "-p ARP -o v-x-i --arp-op Request --arp-ip-dst 10.2.1.254 -j DROP" "-p IPv4 -o v-x-i --ip-src 10.2.1.254 --ip-proto udp --ip-sport 67:68 --ip-dport 67:68 -j DROP" | grep -- "$2"; }; '
|
||||
[[ $(node "${stub}ebt_confined ns v-x-i 02:01:00:00:0c:27") == 2 ]] || fail "confinement : $(node "${stub}ebt_confined ns v-x-i 02:01:00:00:0c:27")"
|
||||
[[ $(node "${stub}ebt_confined ns v-x-i 02:01:00:00:0c:28") == 0 ]] || fail "autre MAC comptée"
|
||||
[[ $(node "${stub}ebt_arp_drop ns v-x-i 10.2.1.254 && echo OUI") == OUI ]] || fail "ARP non lu"
|
||||
[[ $(node "${stub}ebt_arp_drop ns v-x-i 10.2.1.25 || echo NON") == NON ]] || fail "10.2.1.25 pris pour 10.2.1.254"
|
||||
[[ $(node "${stub}ebt_dhcp_drop ns v-x-i 10.2.1.254 && echo OUI") == OUI ]] || fail "DHCP non lu"
|
||||
[[ $(node "${stub}ebt_dhcp_drop ns v-y-i 10.2.1.254 || echo NON") == NON ]] || fail "autre interface acceptée"
|
||||
[[ $(node 'ebt_rules () { echo "-p IPv4 -o v-z-i --ip-src 10.2.1.254 --ip-proto udp --ip-dport 53 -j DROP"; }; ebt_dhcp_drop ns v-z-i 10.2.1.254 || echo NON') == NON ]] || fail "règle UDP hors DHCP prise pour un blocage DHCP"
|
||||
teardown
|
||||
}
|
||||
|
||||
test_wait_bgp_paths_waits_for_the_expected_count () {
|
||||
setup "wait_bgp_paths : nombre de chemins attendu, erreur explicite sinon"
|
||||
[[ $(node 'bgp_paths () { echo 0; }; sleep () { :; }; wait_bgp_paths vp-t53 10.53.1.50/32 0 && echo OUI') == OUI ]] || fail "0 attendu refusé"
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
# fonctionnalité: hypothèses d'environnement de la suite — firmware UEFI, unités modèles de l'agent, journalctl, format de nft list table, br-lan, priorités VRRP lues dans FRR
|
||||
# fonctionnalité: hypothèses d'environnement de la suite — firmware UEFI, unités modèles de l'agent, journalctl, format de ebtables -L, br-lan, priorités VRRP lues dans FRR
|
||||
# hôtes: hv1 hv2 hv3
|
||||
# tickets: #70
|
||||
|
||||
|
|
@ -17,32 +17,20 @@ check "${HOST} : journalctl accepte --since @<epoch>" journalctl --since "@$(dat
|
|||
for unit in dhcp@.service dnsmasq@.service metadata@.service; do
|
||||
check "${HOST} : unité modèle ${unit} installée" systemctl cat "${unit}"
|
||||
done
|
||||
NS=t02-nft
|
||||
NS=t02-ebt
|
||||
MAC=02:01:00:00:0c:27
|
||||
ip netns add "${NS}"
|
||||
check "${HOST} : nft installé" command -v nft
|
||||
check "${HOST} : nft list table, aucune règle avant (témoin)" test "$(nft_confined "${NS}" v-t02-i "${MAC}")" -eq 0
|
||||
ip netns exec "${NS}" nft -f - <<NFT
|
||||
add table bridge two
|
||||
add chain bridge two pre-v-t02-i
|
||||
add rule bridge two pre-v-t02-i ether saddr ${MAC} drop
|
||||
add chain bridge two fwd-v-t02-i
|
||||
add rule bridge two fwd-v-t02-i ether saddr ${MAC} drop
|
||||
add rule bridge two fwd-v-t02-i arp operation request arp daddr ip 10.2.1.254 drop
|
||||
add rule bridge two fwd-v-t02-i ip saddr 10.2.1.254 udp sport 67-68 udp dport 67-68 drop
|
||||
add chain bridge two out-v-t02-i
|
||||
add rule bridge two out-v-t02-i ether saddr ${MAC} drop
|
||||
add chain bridge two fwd-v-t02x-i
|
||||
add rule bridge two fwd-v-t02x-i arp operation request arp daddr ip 10.2.9.254 drop
|
||||
NFT
|
||||
check "${HOST} : nft list table, confinement de MAC lu par nft_confined (3 règles)" test "$(nft_confined "${NS}" v-t02-i "${MAC}")" -eq 3
|
||||
check "${HOST} : nft list table, blocage ARP vers la gateway lu par nft_arp_drop" nft_arp_drop "${NS}" v-t02-i 10.2.1.254
|
||||
check "${HOST} : nft list table, blocage DHCP lu par nft_dhcp_drop" nft_dhcp_drop "${NS}" v-t02-i 10.2.1.254
|
||||
other_ip_ignored () { ! nft_arp_drop "${NS}" v-t02-i 10.2.1.25; }
|
||||
check "${HOST} : nft_arp_drop ne confond pas une autre adresse (témoin)" other_ip_ignored
|
||||
other_port_ignored () { ! nft_arp_drop "${NS}" v-t02-i 10.2.9.254; }
|
||||
check "${HOST} : nft_rules ne lit pas la chaîne d'un autre port (témoin)" other_port_ignored
|
||||
check "${HOST} : nft_handle trouve le numéro d'une règle" test -n "$(nft_handle "${NS}" out-v-t02-i "ether saddr ${MAC} drop")"
|
||||
check "${HOST} : ebtables -L, aucune règle avant (témoin)" test "$(ebt_confined "${NS}" v-t02-i "${MAC}")" -eq 0
|
||||
ip netns exec "${NS}" ebtables -t nat -A PREROUTING --in-interface v-t02-i -s "${MAC}" -j DROP
|
||||
ip netns exec "${NS}" ebtables -t filter -A FORWARD --out-interface v-t02-i -s "${MAC}" -j DROP
|
||||
ip netns exec "${NS}" ebtables -t filter -A OUTPUT --out-interface v-t02-i -s "${MAC}" -j DROP
|
||||
ip netns exec "${NS}" ebtables -A FORWARD --out-interface v-t02-i -p arp --arp-op Request --arp-ip-dst 10.2.1.254 -j DROP
|
||||
ip netns exec "${NS}" ebtables -A FORWARD --out-interface v-t02-i -p IPv4 --ip-protocol udp --ip-source-port 67:68 --ip-destination-port 67:68 --ip-source 10.2.1.254 -j DROP
|
||||
check "${HOST} : ebtables -L, confinement de MAC lu par ebt_confined (3 règles)" test "$(ebt_confined "${NS}" v-t02-i "${MAC}")" -eq 3
|
||||
check "${HOST} : ebtables -L, blocage ARP vers la gateway lu par ebt_arp_drop" ebt_arp_drop "${NS}" v-t02-i 10.2.1.254
|
||||
check "${HOST} : ebtables -L, blocage DHCP lu par ebt_dhcp_drop" ebt_dhcp_drop "${NS}" v-t02-i 10.2.1.254
|
||||
other_ip_ignored () { ! ebt_arp_drop "${NS}" v-t02-i 10.2.1.25; }
|
||||
check "${HOST} : ebt_arp_drop ne confond pas une autre adresse (témoin)" other_ip_ignored
|
||||
ip netns del "${NS}"
|
||||
case "${HOST}" in
|
||||
hv1) LAN=10.33.0.11 ;;
|
||||
|
|
|
|||
|
|
@ -1,118 +0,0 @@
|
|||
# fonctionnalité: DNS et NTP des VM par two-hostsvc — options DHCP 6 et 42 = interface_ip, redirection vers 169.254.169.253 dans le VPC (vxlan et bridge), seuls 53 et 123 servis, alertes du watchdog, VPC en error si two-hostsvc ne répond pas
|
||||
# hôtes: hv1
|
||||
# tickets: #86
|
||||
|
||||
on hv1 <<'NODE'
|
||||
KEY=$(vm_key)
|
||||
dns_py () {
|
||||
cat <<'PY'
|
||||
import socket, struct, sys
|
||||
server, proto, name = sys.argv[1], sys.argv[2], sys.argv[3]
|
||||
q = struct.pack('!HHHHHH', 0x2929, 0x0100, 1, 0, 0, 0)
|
||||
q += b''.join(bytes([len(p)]) + p.encode() for p in name.split('.')) + b'\0' + struct.pack('!HH', 1, 1)
|
||||
if proto == 'tcp':
|
||||
s = socket.create_connection((server, 53), timeout=5)
|
||||
s.sendall(struct.pack('!H', len(q)) + q)
|
||||
size = struct.unpack('!H', s.recv(2))[0]
|
||||
a = b''
|
||||
while len(a) < size:
|
||||
a += s.recv(size - len(a))
|
||||
else:
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
s.settimeout(5)
|
||||
s.sendto(q, (server, int(sys.argv[4]) if len(sys.argv) > 4 else 53))
|
||||
a = s.recv(65535)
|
||||
print('id=%d rcode=%d answers=%d' % (struct.unpack('!H', a[:2])[0], a[3] & 15, struct.unpack('!H', a[6:8])[0]))
|
||||
PY
|
||||
}
|
||||
ntp_py () {
|
||||
cat <<'PY'
|
||||
import socket, struct, sys, time
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
s.settimeout(5)
|
||||
req = b'\x23' + b'\0' * 39 + struct.pack('!Q', 0x2929292929292929)
|
||||
s.sendto(req, (sys.argv[1], 123))
|
||||
r = s.recv(1024)
|
||||
t = struct.unpack('!I', r[40:44])[0] - 2208988800
|
||||
print('mode=%d origin=%s li=%d stratum=%d offset=%d' % (r[0] & 7, 'ok' if r[24:32] == req[40:48] else 'ko', r[0] >> 6, r[1], abs(t - time.time())))
|
||||
PY
|
||||
}
|
||||
vm_py () {
|
||||
local vpc="${1}" ip="${2}" code="${3}"
|
||||
shift 3
|
||||
vm_ssh "${vpc}" "${ip}" "echo $(${code} | base64 | tr -d '\n') | base64 -d | timeout 10 python3 - $*"
|
||||
}
|
||||
dns_ok () { vm_py "${1}" "${2}" dns_py "${3}" "${4}" deb.debian.org | grep -qE '^id=10537 rcode=0 answers=[1-9]'; }
|
||||
ntp_ok () {
|
||||
local out
|
||||
out=$(vm_py "${1}" "${2}" ntp_py "${3}") || { echo "pas de réponse NTP de ${3}"; return 1; }
|
||||
echo "${out}" | grep -qE '^mode=4 origin=ok li=[0-3] stratum=[0-9]+ offset=[0-4]$' || { echo "${out}"; return 1; }
|
||||
}
|
||||
no_answer () { ! vm_py "${1}" "${2}" dns_py "${3}" udp deb.debian.org "${4}" >/dev/null 2>&1; }
|
||||
served_ports () { ip netns exec "${1}" ss -Hlntu | awk '$5 ~ /^169\.254\.169\.253:/ {print $1, $5}' | sort | tr '\n' ' '; }
|
||||
rules () { ip netns exec "${1}" iptables -t nat -S TWO-HOSTSVC; }
|
||||
state_has () { grep -q "\"${1}\"" /run/two/hostsvc/hostsvc.state; }
|
||||
excluded_first () { [ "$(rules "${1}" | sed -n '2p;3p' | tr '\n' ' ')" = "-A TWO-HOSTSVC -i br-public -j RETURN -A TWO-HOSTSVC -i l3-+ -j RETURN " ]; }
|
||||
no_dns () { ! dns_ok "$@" >/dev/null 2>&1; }
|
||||
no_ntp () { ! ntp_ok "$@" >/dev/null 2>&1; }
|
||||
vpc_in_error () {
|
||||
local out
|
||||
out=$(wait_state "vpcs/${1}" running 2>&1) && { echo "${1} est running"; return 1; }
|
||||
grep -q "vpcs/${1}: error" <<<"${out}" || { echo "${out}"; return 1; }
|
||||
}
|
||||
restored () { state_has vp-t29 && state_has vp-t29b && ! state_has vp-t29c; }
|
||||
vpc_delete () { api_delete "vpcs/${1}" && wait_state "vpcs/${1}" deleted; }
|
||||
|
||||
check "two-hostsvc actif" systemctl is-active --quiet two-hostsvc.service
|
||||
check "VPC vp-t29 (vxlan)" vpc_create vp-t29 10.29.0.0/16
|
||||
check "subnet sn-t29 (vxlan)" subnet_create sn-t29 vp-t29 2901 10.29.1.0/24
|
||||
check "VPC vp-t29b (bridge)" vpc_create vp-t29b 10.33.0.0/16
|
||||
check "subnet sn-t29b en mode bridge sur br-lan, interface_ip 10.33.0.29" subnet_create_bridge sn-t29b vp-t29b 10.33.0.0/24 10.33.0.29
|
||||
for vpc in vp-t29 vp-t29b; do
|
||||
check "${vpc} : 169.254.169.253 sur la dummy hostsvc" sh -c "ip -n ${vpc} -4 -o addr show dev hostsvc | grep -q ' 169.254.169.253/32 '"
|
||||
check "${vpc} : two-hostsvc n'écoute que 53/udp, 53/tcp et 123/udp sur 169.254.169.253" test "$(served_ports "${vpc}")" = "tcp 169.254.169.253:53 udp 169.254.169.253:123 udp 169.254.169.253:53 "
|
||||
check "${vpc} : chaîne TWO-HOSTSVC, br-public et l3-+ exclus avant toute redirection" excluded_first "${vpc}"
|
||||
check "${vpc} : redirection de 53/udp, 53/tcp et 123/udp vers 169.254.169.253" test "$(rules "${vpc}" | grep -c 'DNAT --to-destination 169.254.169.253')" -eq 3
|
||||
check "${vpc} : appel depuis PREROUTING pour les destinations locales seulement" sh -c "ip netns exec ${vpc} iptables -t nat -S PREROUTING | grep -qx -- '-A PREROUTING -m addrtype --dst-type LOCAL -j TWO-HOSTSVC'"
|
||||
check "${vpc} : inscrit dans l'état de two-hostsvc" state_has "${vpc}"
|
||||
done
|
||||
|
||||
check "VM t29-a 10.29.1.10 (vxlan)" vm_create t29-a sn-t29 10.29.1.10 "${KEY}"
|
||||
check "VM t29-b 10.33.0.129 (bridge)" vm_launch t29-b --nic sn-t29b=10.33.0.129 --sshkey "${KEY}"
|
||||
for vm in vp-t29:10.29.1.10 vp-t29b:10.33.0.129; do
|
||||
vpc=${vm%%:*}; ip=${vm#*:}
|
||||
check "${ip} joignable" vm_wait "${vpc}" "${ip}"
|
||||
GW=$(vm_lease "${vpc}" "${ip}" SERVER_ADDRESS)
|
||||
check "${ip} : gateway lue dans le bail (${GW:-absente})" test -n "${GW}"
|
||||
check "${ip} : option 6, DNS = la gateway ${GW}" test "$(vm_lease "${vpc}" "${ip}" DNS)" = "${GW}"
|
||||
check "${ip} : option 42, NTP = la gateway ${GW}" test "$(vm_lease "${vpc}" "${ip}" NTP)" = "${GW}"
|
||||
check "${ip} : DNS en UDP via ${GW}" dns_ok "${vpc}" "${ip}" "${GW}" udp
|
||||
check "${ip} : DNS en TCP via ${GW}" dns_ok "${vpc}" "${ip}" "${GW}" tcp
|
||||
check "${ip} : NTP via ${GW}, réponse serveur, origine vérifiée, écart < 5 s" ntp_ok "${vpc}" "${ip}" "${GW}"
|
||||
info "${ip} : NTP $(vm_py "${vpc}" "${ip}" ntp_py "${GW}" 2>&1)"
|
||||
info "${ip} : résolveur système : $(vm_ssh "${vpc}" "${ip}" 'getent hosts deb.debian.org' 2>&1 | head -1)"
|
||||
check "${ip} : port 5353 de la gateway non redirigé (aucune réponse)" no_answer "${vpc}" "${ip}" "${GW}" 5353
|
||||
done
|
||||
|
||||
ip -n vp-t29 addr del 169.254.169.253/32 dev hostsvc
|
||||
check "watchdog : adresse 169.254.169.253 retirée signalée" wait_watchdog "169.254.169.253 missing on hostsvc (in netns)"
|
||||
ip -n vp-t29 addr add 169.254.169.253/32 dev hostsvc
|
||||
check "10.29.1.10 : DNS de nouveau servi une fois l'adresse remise" dns_ok vp-t29 10.29.1.10 10.29.1.254 udp
|
||||
ip netns exec vp-t29 iptables -t nat -D TWO-HOSTSVC -p udp -m udp --dport 123 -j DNAT --to-destination 169.254.169.253
|
||||
check "watchdog : redirection incomplète signalée" wait_watchdog "dns and ntp redirection to 169.254.169.253 missing or incomplete (chain TWO-HOSTSVC)"
|
||||
check "10.29.1.10 : sans la règle 123, plus de NTP (témoin)" no_ntp vp-t29 10.29.1.10 10.29.1.254
|
||||
ip netns exec vp-t29 iptables -t nat -A TWO-HOSTSVC -p udp -m udp --dport 123 -j DNAT --to-destination 169.254.169.253
|
||||
check "10.29.1.10 : NTP de nouveau servi une fois la règle remise" ntp_ok vp-t29 10.29.1.10 10.29.1.254
|
||||
|
||||
systemctl stop two-hostsvc.service
|
||||
check "watchdog : two-hostsvc arrêté signalé" wait_watchdog "unreachable, no vpc of this host gets dns or ntp"
|
||||
check "10.29.1.10 : plus de DNS pendant l'arrêt (témoin)" no_dns vp-t29 10.29.1.10 10.29.1.254 udp
|
||||
api_post vpcs '{"name":"vp-t29c","cidr":"10.129.0.0/16"}'
|
||||
check "VPC vp-t29c créé pendant l'arrêt : en error" vpc_in_error vp-t29c
|
||||
systemctl start two-hostsvc.service
|
||||
check "two-hostsvc redémarré" systemctl is-active --quiet two-hostsvc.service
|
||||
check "redémarrage : vp-t29 et vp-t29b rouverts depuis le fichier d'état" restored
|
||||
check "10.29.1.10 : DNS de nouveau servi après le redémarrage" dns_ok vp-t29 10.29.1.10 10.29.1.254 udp
|
||||
check "10.33.0.129 : DNS de nouveau servi après le redémarrage" dns_ok vp-t29b 10.33.0.129 10.33.0.29 udp
|
||||
check "VPC vp-t29c supprimé depuis error" vpc_delete vp-t29c
|
||||
NODE
|
||||
|
|
@ -18,10 +18,10 @@ check "sn-t31a : route du subnet émise depuis la gateway" sh -c "ip -n vp-t31 -
|
|||
for dev in br-t31a gw-t31a; do
|
||||
check "sn-t31a : ${dev}, arp_ignore=1 et arp_announce=2" sh -c "[ \"\$(ip netns exec vp-t31 sysctl -n net.ipv4.conf.${dev}.arp_ignore) \$(ip netns exec vp-t31 sysctl -n net.ipv4.conf.${dev}.arp_announce)\" = '1 2' ]"
|
||||
done
|
||||
check "sn-t31a : confinement de la MAC anycast sur v-t31a-i (prerouting, forward, output)" test "$(nft_confined vp-t31 v-t31a-i "${MAC}")" -eq 3
|
||||
check "sn-t31a : confinement de la MAC anycast sur v-t31a-i (nat PREROUTING, FORWARD, OUTPUT)" test "$(ebt_confined vp-t31 v-t31a-i "${MAC}")" -eq 3
|
||||
for ip in 10.31.1.254 10.31.1.253; do
|
||||
check "sn-t31a : requêtes ARP vers ${ip} bloquées en sortie de v-t31a-i" nft_arp_drop vp-t31 v-t31a-i "${ip}"
|
||||
check "sn-t31a : DHCP émis depuis ${ip} bloqué en sortie de v-t31a-i" nft_dhcp_drop vp-t31 v-t31a-i "${ip}"
|
||||
check "sn-t31a : requêtes ARP vers ${ip} bloquées en sortie de v-t31a-i" ebt_arp_drop vp-t31 v-t31a-i "${ip}"
|
||||
check "sn-t31a : DHCP émis depuis ${ip} bloqué en sortie de v-t31a-i" ebt_dhcp_drop vp-t31 v-t31a-i "${ip}"
|
||||
done
|
||||
check "VM t31-a" vm_create t31-a sn-t31a 10.31.1.10 "${KEY}"
|
||||
check "VM t31-b" vm_create t31-b sn-t31b 10.31.2.10 "${KEY}"
|
||||
|
|
@ -39,8 +39,8 @@ check "suppression de t31-a" vm_delete t31-a
|
|||
check "suppression de sn-t31a" api_delete subnets/sn-t31a
|
||||
check "sn-t31a supprimé" wait_state subnets/sn-t31a deleted
|
||||
check "sn-t31a : plus de macvlan gw-t31a dans le netns" sh -c "! ip -n vp-t31 link show gw-t31a >/dev/null 2>&1"
|
||||
check "sn-t31a : plus aucune règle nftables pour v-t31a-i" test -z "$(nft_rules vp-t31 v-t31a-i)"
|
||||
check "sn-t31b : règles de son propre subnet toujours présentes (témoin)" test -n "$(nft_rules vp-t31 v-t31b-i)"
|
||||
check "sn-t31a : plus aucune règle ebtables pour v-t31a-i" test -z "$(ebt_rules vp-t31 v-t31a-i)"
|
||||
check "sn-t31b : règles de son propre subnet toujours présentes (témoin)" test -n "$(ebt_rules vp-t31 v-t31b-i)"
|
||||
check "sn-t31a recréé" subnet_create sn-t31a vp-t31 3101 10.31.1.0/24
|
||||
check "VM t31-a2 sur le subnet recréé" vm_create t31-a2 sn-t31a 10.31.1.11 "${KEY}"
|
||||
check "t31-a2 joignable" vm_wait vp-t31 10.31.1.11
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
# fonctionnalité: subnet en mode bridge rattaché au bridge br-lan de l'hôte — un hôte, puis le même subnet sur deux hôtes (interface_ip confinée à son hôte : une seule réponse ARP, métadonnées servies par l'hôte de la VM, ni ARP ni DHCP de la gateway sur le segment, règles surveillées par le watchdog) ; VM serveur DHCP des machines diskless (dhcp_server) entendue sur le segment, voisine bloquée
|
||||
# fonctionnalité: subnet en mode bridge rattaché au bridge br-lan de l'hôte — un hôte, puis le même subnet sur deux hôtes (interface_ip confinée à son hôte : une seule réponse ARP, métadonnées servies par l'hôte de la VM, ni ARP ni DHCP de la gateway sur le segment, règles surveillées par le watchdog)
|
||||
# hôtes: hv1 hv2
|
||||
# tickets: #70, #75, #76
|
||||
# tickets: #70, #75
|
||||
|
||||
on hv1 <<'NODE'
|
||||
KEY=$(vm_key)
|
||||
|
|
@ -47,7 +47,7 @@ own_meta () {
|
|||
done
|
||||
[ "${ok}" -eq 5 ] || { echo "${2} : ${ok}/5 réponses avec ses propres métadonnées"; return 1; }
|
||||
}
|
||||
check "hv1 : quatre règles de confinement de 10.33.0.1 sur v-t33a-i" test "$(nft_rules vp-t33 v-t33a-i | grep -c '10.33.0.1 ')" -eq 4
|
||||
check "hv1 : quatre règles de confinement de 10.33.0.1 sur v-t33a-i" test "$(ebt_rules vp-t33 v-t33a-i | grep -c '10.33.0.1 ')" -eq 4
|
||||
RESPONDERS=$(vm_frames vp-t33 10.33.0.21 arp-who ens3 10.33.0.21 10.33.0.1 2)
|
||||
info "MAC qui répondent pour 10.33.0.1 vues de t33-a1 : $(echo ${RESPONDERS})"
|
||||
check "deux hôtes : t33-a1 (hv1) joint t33-b1 (hv2) sur le segment lan" vm_ssh vp-t33 10.33.0.21 'ping -n -c 3 -W 2 10.33.0.23'
|
||||
|
|
@ -74,11 +74,10 @@ check "témoin : l'ARP émis par t33-a1 atteint le segment par v-t33a-e (capture
|
|||
check "aucune trame ARP depuis 10.33.0.1 vers le segment (v-t33a-e)" test "$(cat "${S}/arp")" -eq 0
|
||||
check "aucune trame DHCP depuis 10.33.0.1 vers le segment (v-t33a-e)" test "$(cat "${S}/dhcp")" -eq 0
|
||||
rm -r "${S}"
|
||||
RULE='arp saddr ip 10.33.0.1 drop'
|
||||
HANDLE=$(nft_handle vp-t33 out-v-t33a-i "${RULE}")
|
||||
check "règle ARP output retirée à la main (handle ${HANDLE:-absent})" ip netns exec vp-t33 nft delete rule bridge two out-v-t33a-i handle "${HANDLE}"
|
||||
check "watchdog : règle manquante signalée pour sn-t33a" wait_watchdog "nftables rule missing in netns vp-t33: out-v-t33a-i: ${RULE}"
|
||||
check "règle ARP output reposée" ip netns exec vp-t33 nft add rule bridge two out-v-t33a-i ${RULE}
|
||||
RULE='OUTPUT --out-interface v-t33a-i -p arp --arp-ip-src 10.33.0.1 -j DROP'
|
||||
check "règle ARP OUTPUT retirée à la main" ip netns exec vp-t33 ebtables -D ${RULE}
|
||||
check "watchdog : règle manquante signalée pour sn-t33a" wait_watchdog "ebtables rule missing in netns vp-t33"
|
||||
check "règle ARP OUTPUT reposée" ip netns exec vp-t33 ebtables -A ${RULE}
|
||||
NODE
|
||||
|
||||
on hv2 <<'NODE'
|
||||
|
|
@ -93,19 +92,3 @@ own_meta () {
|
|||
check "deux hôtes : t33-b1 (hv2) reçoit toujours ses propres métadonnées (5 essais, cache ARP vidé)" own_meta 10.33.0.23 t33-b1
|
||||
check "deux hôtes : t33-b1 (hv2) joint t33-a2 (hv1)" vm_ssh vp-t33 10.33.0.23 'ping -n -c 3 -W 2 10.33.0.22'
|
||||
NODE
|
||||
|
||||
on hv1 <<'NODE'
|
||||
KEY=$(vm_key)
|
||||
check "VM t33-px 10.33.0.24, serveur DHCP des machines diskless (dhcp_server)" vm_launch t33-px --nic sn-t33a=10.33.0.24 --dhcp-server --sshkey "${KEY}"
|
||||
check "10.33.0.24 joignable" vm_wait vp-t33 10.33.0.24
|
||||
S=$(mktemp -d)
|
||||
sniff_start "${S}/px" v-t33a-e 8 udp-from 10.33.0.24 67
|
||||
vm_frames vp-t33 10.33.0.24 dhcp ens3 10.33.0.24 68 5 >/dev/null 2>&1
|
||||
wait
|
||||
check "réponse DHCP de t33-px, autorisée : atteint le segment par v-t33a-e ($(cat "${S}/px") vue(s))" test "$(cat "${S}/px")" -gt 0
|
||||
sniff_start "${S}/a1" v-t33a-e 8 udp-from 10.33.0.21 67
|
||||
vm_frames vp-t33 10.33.0.21 dhcp ens3 10.33.0.21 68 5 >/dev/null 2>&1
|
||||
wait
|
||||
check "même réponse DHCP de t33-a1, non autorisée : n'atteint pas le segment par v-t33a-e ($(cat "${S}/a1") vue(s))" test "$(cat "${S}/a1")" -eq 0
|
||||
rm -r "${S}"
|
||||
NODE
|
||||
|
|
|
|||
|
|
@ -1,37 +1,19 @@
|
|||
# fonctionnalité: filtrage au port — en sortie vers le fabric (v-<id>-i) : ARP vers la gateway et l'adresse d'émission, DHCP émis depuis ces adresses, MAC anycast confinée dans les deux sens ; à la sortie de chaque VM (pre-tap<id>) : MAC, IP et ARP usurpés, faux serveur DHCP, source 0.0.0.0 hors DHCP, type de trame non IP, NIC autorisée en serveur DHCP (dhcp_server), règles surveillées par le watchdog et retirées avec la VM
|
||||
# fonctionnalité: filtrage au port — en sortie vers le fabric (v-<id>-i) : ARP vers la gateway et l'adresse d'émission, DHCP émis depuis ces adresses, MAC anycast confinée dans les deux sens ; entre VM d'un même hôte : faux serveur DHCP et usurpation ARP de la gateway (comportement attendu, non filtré à ce jour)
|
||||
# hôtes: hv1
|
||||
# tickets: #57, #76
|
||||
# tickets: #57
|
||||
|
||||
on hv1 <<'NODE'
|
||||
KEY=$(vm_key)
|
||||
MAC=$(anycast_mac 3501)
|
||||
FAB=v-t35a-e
|
||||
INJ=t35-inj
|
||||
S=/tmp/t35
|
||||
check "VPC vp-t35" vpc_create vp-t35 10.35.0.0/16
|
||||
check "subnet sn-t35a (VNI 3501)" subnet_create sn-t35a vp-t35 3501 10.35.1.0/24
|
||||
check "VM t35-a" vm_create t35-a sn-t35a 10.35.1.10 "${KEY}"
|
||||
check "VM t35-b" vm_create t35-b sn-t35a 10.35.1.11 "${KEY}"
|
||||
check "t35-a joignable" vm_wait vp-t35 10.35.1.10
|
||||
check "VM t35-c, autorisée en serveur DHCP" vm_launch t35-c --nic sn-t35a=10.35.1.12 --dhcp-server --sshkey "${KEY}"
|
||||
check "t35-b joignable" vm_wait vp-t35 10.35.1.11
|
||||
check "t35-c joignable" vm_wait vp-t35 10.35.1.12
|
||||
check "python3 présent dans la VM (prérequis des trames brutes)" vm_ssh vp-t35 10.35.1.10 'command -v python3'
|
||||
check "t35-c : dhcp_server vrai dans le GET" sh -c "curl -fsS ${API}/vms/t35-c | jq -e '.interfaces[0].dhcp_server == true'"
|
||||
check "t35-a : dhcp_server faux dans le GET" sh -c "curl -fsS ${API}/vms/t35-a | jq -e '.interfaces[0].dhcp_server == false'"
|
||||
AMAC=$(vm_ssh vp-t35 10.35.1.10 'cat /sys/class/net/ens3/address')
|
||||
CMAC=$(vm_ssh vp-t35 10.35.1.12 'cat /sys/class/net/ens3/address')
|
||||
ATAP=$(nft_tap_of vp-t35 "${AMAC}")
|
||||
CTAP=$(nft_tap_of vp-t35 "${CMAC}")
|
||||
check "t35-a : chaîne pre-<tap> trouvée par sa MAC ${AMAC} (${ATAP:-aucune})" test -n "${ATAP}"
|
||||
check "t35-c : chaîne pre-<tap> trouvée par sa MAC ${CMAC} (${CTAP:-aucune})" test -n "${CTAP}"
|
||||
check "t35-a : ${ATAP} est un port de br-t35a" sh -c "ip -n vp-t35 -o link show ${ATAP} | grep -q 'master br-t35a'"
|
||||
check "t35-a : 8 règles sur ${ATAP}, dont le blocage du serveur DHCP" test "$(nft_rules vp-t35 "${ATAP}" | grep -c .)" -eq 8 -a "$(nft_count vp-t35 "${ATAP}" 'udp sport 67 drop')" -eq 1
|
||||
check "t35-c : 7 règles sur ${CTAP}, sans le blocage du serveur DHCP" test "$(nft_rules vp-t35 "${CTAP}" | grep -c .)" -eq 7 -a "$(nft_count vp-t35 "${CTAP}" 'udp sport 67 drop')" -eq 0
|
||||
ip -n vp-t35 link add "${INJ}" type veth peer name "${INJ}p"
|
||||
ip -n vp-t35 link set "${INJ}" master br-t35a up
|
||||
ip -n vp-t35 link set "${INJ}p" up
|
||||
check "port d'injection ${INJ} sur br-t35a, sans filtre (trames d'une VM non filtrée)" sh -c "ip -n vp-t35 -o link show ${INJ} | grep -q 'master br-t35a'"
|
||||
|
||||
seen_on_fabric () {
|
||||
local out="${1}" kind="${2}"
|
||||
|
|
@ -42,24 +24,6 @@ seen_on_fabric () {
|
|||
cat "${out}"
|
||||
}
|
||||
|
||||
injected_on_fabric () {
|
||||
local out="${1}" kind="${2}"
|
||||
shift 2
|
||||
sniff_start "${out}" "${FAB}" 6 "${kind}" "${@:1:$(( $# - 1 ))}"
|
||||
ns_frames vp-t35 ${!#} >/dev/null 2>&1
|
||||
wait
|
||||
cat "${out}"
|
||||
}
|
||||
|
||||
received_by_b () {
|
||||
local out="${1}" sender="${2}" cmd="${3}"
|
||||
shift 3
|
||||
vm_sniff_start "${out}" vp-t35 10.35.1.11 ens3 8 "$@"
|
||||
vm_frames vp-t35 "${sender}" ${cmd} >/dev/null 2>&1
|
||||
wait
|
||||
cat "${out}"
|
||||
}
|
||||
|
||||
for ip in 10.35.1.254 10.35.1.253; do
|
||||
n=$(seen_on_fabric "${S}.arp" arp-req "${ip}" "arp ens3 10.35.1.10 ${ip} 5")
|
||||
check "requête ARP de t35-a vers ${ip} : aucune ne sort vers le fabric (${n} vue(s))" test "${n}" -eq 0
|
||||
|
|
@ -68,16 +32,16 @@ n=$(seen_on_fabric "${S}.arp" arp-req 10.35.1.200 "arp ens3 10.35.1.10 10.35.1.2
|
|||
check "requête ARP de t35-a vers 10.35.1.200 : sort vers le fabric (témoin, ${n} vue(s))" test "${n}" -gt 0
|
||||
|
||||
for ip in 10.35.1.254 10.35.1.253; do
|
||||
n=$(injected_on_fabric "${S}.dhcp" udp-from "${ip}" 67 "dhcp ${INJ}p ${ip} 68 5")
|
||||
check "réponse DHCP injectée depuis ${ip} : aucune ne sort vers le fabric (${n} vue(s))" test "${n}" -eq 0
|
||||
n=$(seen_on_fabric "${S}.dhcp" udp-from "${ip}" 67 "dhcp ens3 ${ip} 68 5")
|
||||
check "réponse DHCP émise par t35-a depuis ${ip} : aucune ne sort vers le fabric (${n} vue(s))" test "${n}" -eq 0
|
||||
done
|
||||
n=$(injected_on_fabric "${S}.dhcp" udp-from 10.35.1.10 67 "dhcp ${INJ}p 10.35.1.10 68 5")
|
||||
check "réponse DHCP injectée depuis 10.35.1.10 : sort vers le fabric (témoin, ${n} vue(s))" test "${n}" -gt 0
|
||||
n=$(seen_on_fabric "${S}.dhcp" udp-from 10.35.1.10 67 "dhcp ens3 10.35.1.10 68 5")
|
||||
check "réponse DHCP émise par t35-a depuis sa propre adresse : sort vers le fabric (témoin, ${n} vue(s))" test "${n}" -gt 0
|
||||
|
||||
n=$(injected_on_fabric "${S}.mac" marker t35-anycast "marker ${INJ}p ${MAC} t35-anycast 5")
|
||||
check "trame injectée avec la MAC anycast ${MAC} en source : aucune ne sort vers le fabric (${n} vue(s))" test "${n}" -eq 0
|
||||
n=$(injected_on_fabric "${S}.mac" marker t35-autre "marker ${INJ}p 02:35:00:00:00:01 t35-autre 5")
|
||||
check "trame injectée avec une autre MAC source : sort vers le fabric (témoin, ${n} vue(s))" test "${n}" -gt 0
|
||||
n=$(seen_on_fabric "${S}.mac" marker t35-anycast "marker ens3 ${MAC} t35-anycast 5")
|
||||
check "trame de t35-a avec la MAC anycast ${MAC} en source : aucune ne sort vers le fabric (${n} vue(s))" test "${n}" -eq 0
|
||||
n=$(seen_on_fabric "${S}.mac" marker t35-autre "marker ens3 02:35:00:00:00:01 t35-autre 5")
|
||||
check "trame de t35-a avec une autre MAC source : sort vers le fabric (témoin, ${n} vue(s))" test "${n}" -gt 0
|
||||
|
||||
vm_sniff_start "${S}.in" vp-t35 10.35.1.11 ens3 8 marker t35-entrant
|
||||
frames marker "${FAB}" "${MAC}" t35-entrant 5
|
||||
|
|
@ -97,66 +61,27 @@ ip netns exec vp-t35 ping -n -c 3 -W 1 10.35.1.200 >/dev/null 2>&1
|
|||
wait
|
||||
check "trafic de la gateway (ping des VM, réponses du netns) : aucune trame avec la MAC anycast sur le fabric ($(cat "${S}.gw") vue(s))" test "$(cat "${S}.gw")" -eq 0
|
||||
check "trafic du netns vers une adresse inconnue : ARP du bridge ${BRMAC} sur le fabric (témoin, $(cat "${S}.br") vue(s))" test "$(cat "${S}.br")" -gt 0
|
||||
n=$(received_by_b "${S}.r" 10.35.1.10 "dhcp ens3 10.35.1.10 68 5" udp-from 10.35.1.10 67)
|
||||
check "faux serveur DHCP dans t35-a (67 → 68) : n'atteint pas t35-b, même subnet, même hôte (${n} vue(s))" test "${n}" -eq 0
|
||||
n=$(received_by_b "${S}.r" 10.35.1.12 "dhcp ens3 10.35.1.12 68 5" udp-from 10.35.1.12 67)
|
||||
check "réponse DHCP de t35-c, autorisée (dhcp_server) : atteint t35-b (témoin, ${n} vue(s))" test "${n}" -gt 0
|
||||
n=$(received_by_b "${S}.r" 10.35.1.10 "udp ens3 10.35.1.10 4000 4000 5" udp-from 10.35.1.10 4000)
|
||||
check "UDP de t35-a depuis sa propre adresse et sa MAC : atteint t35-b (témoin, ${n} vue(s))" test "${n}" -gt 0
|
||||
for ip in 10.35.1.99 10.35.1.254 10.35.1.11; do
|
||||
n=$(received_by_b "${S}.r" 10.35.1.10 "udp ens3 ${ip} 4000 4000 5" udp-from "${ip}" 4000)
|
||||
check "UDP de t35-a avec l'adresse source usurpée ${ip} : n'atteint pas t35-b (${n} vue(s))" test "${n}" -eq 0
|
||||
done
|
||||
n=$(received_by_b "${S}.r" 10.35.1.10 "udp ens3 10.35.1.10 4002 4002 5 02:35:00:00:00:01" udp-from 10.35.1.10 4002)
|
||||
check "UDP de t35-a avec une MAC source usurpée : n'atteint pas t35-b (${n} vue(s))" test "${n}" -eq 0
|
||||
n=$(received_by_b "${S}.r" 10.35.1.10 "udp ens3 0.0.0.0 68 67 5" udp-from 0.0.0.0 68)
|
||||
check "client DHCP de t35-a depuis 0.0.0.0 (68 → 67) : atteint t35-b (témoin, ${n} vue(s))" test "${n}" -gt 0
|
||||
n=$(received_by_b "${S}.r" 10.35.1.10 "udp ens3 0.0.0.0 68 53 5" udp-from 0.0.0.0 68)
|
||||
check "UDP de t35-a depuis 0.0.0.0 hors DHCP (68 → 53) : n'atteint pas t35-b (${n} vue(s))" test "${n}" -eq 0
|
||||
n=$(received_by_b "${S}.r" 10.35.1.10 "arp ens3 0.0.0.0 10.35.1.11 5" arp-from 0.0.0.0)
|
||||
check "sonde ARP de t35-a depuis 0.0.0.0 : atteint t35-b (témoin, ${n} vue(s))" test "${n}" -gt 0
|
||||
n=$(received_by_b "${S}.r" 10.35.1.10 "arp ens3 10.35.1.99 10.35.1.11 5" arp-from 10.35.1.99)
|
||||
check "requête ARP de t35-a avec l'adresse d'émetteur usurpée 10.35.1.99 : n'atteint pas t35-b (${n} vue(s))" test "${n}" -eq 0
|
||||
n=$(received_by_b "${S}.r" 10.35.1.10 "marker ens3 ${AMAC} t35-type 5" marker t35-type)
|
||||
check "trame de t35-a d'un type autre qu'IPv4, ARP ou IPv6 (0x88b5), avec sa MAC : n'atteint pas t35-b (${n} vue(s))" test "${n}" -eq 0
|
||||
vm_sniff_start "${S}.r" vp-t35 10.35.1.11 ens3 8 marker t35-type-temoin
|
||||
ns_frames vp-t35 marker "${INJ}p" 02:35:00:00:00:03 t35-type-temoin 5 >/dev/null 2>&1
|
||||
UNF="aucun filtrage entre VM d'un même hôte à ce jour"
|
||||
vm_sniff_start "${S}.rogue" vp-t35 10.35.1.11 ens3 8 udp-from 10.35.1.10 67
|
||||
vm_frames vp-t35 10.35.1.10 dhcp ens3 10.35.1.10 68 5 >/dev/null 2>&1
|
||||
wait
|
||||
check "même type de trame par le port d'injection : reçu par t35-b (témoin, $(cat "${S}.r") vue(s))" test "$(cat "${S}.r")" -gt 0
|
||||
known "#76" "faux serveur DHCP dans t35-a (67 → 68) : n'atteint pas t35-b, même subnet, même hôte (${UNF} ; $(cat "${S}.rogue") vue(s))" test "$(cat "${S}.rogue")" = 0
|
||||
vm_sniff_start "${S}.rogue" vp-t35 10.35.1.11 ens3 8 udp-from 10.35.1.10 67
|
||||
vm_frames vp-t35 10.35.1.10 dhcp ens3 10.35.1.10 6868 5 >/dev/null 2>&1
|
||||
wait
|
||||
check "même envoi de t35-a vers le port 6868 : reçu par t35-b (témoin, $(cat "${S}.rogue") vue(s))" test "$(cat "${S}.rogue")" -gt 0
|
||||
|
||||
AMAC=$(vm_ssh vp-t35 10.35.1.10 'cat /sys/class/net/ens3/address')
|
||||
vm_ssh vp-t35 10.35.1.11 'ping -n -c 2 -W 1 10.35.1.254; ping -n -c 2 -W 1 10.35.1.253' >/dev/null 2>&1
|
||||
RELAY_MAC=$(vm_neigh_mac vp-t35 10.35.1.11 10.35.1.253)
|
||||
check "prérequis : t35-b résout la gateway vers la MAC anycast ${MAC}" test "$(vm_neigh_mac vp-t35 10.35.1.11 10.35.1.254)" = "${MAC}"
|
||||
check "prérequis : t35-b résout l'adresse d'émission 10.35.1.253 (${RELAY_MAC:-non résolue})" test -n "${RELAY_MAC}"
|
||||
vm_frames vp-t35 10.35.1.10 garp ens3 10.35.1.254 5 >/dev/null 2>&1
|
||||
check "usurpation ARP de la gateway 10.35.1.254 par t35-a : t35-b garde la MAC anycast (lue : $(vm_neigh_mac vp-t35 10.35.1.11 10.35.1.254))" test "$(vm_neigh_mac vp-t35 10.35.1.11 10.35.1.254)" = "${MAC}"
|
||||
known "#76" "usurpation ARP de la gateway 10.35.1.254 par t35-a : t35-b garde la MAC anycast (${UNF} ; lue : $(vm_neigh_mac vp-t35 10.35.1.11 10.35.1.254))" test "$(vm_neigh_mac vp-t35 10.35.1.11 10.35.1.254)" = "${MAC}"
|
||||
vm_frames vp-t35 10.35.1.10 garp ens3 10.35.1.253 5 >/dev/null 2>&1
|
||||
check "usurpation ARP de l'adresse d'émission 10.35.1.253 par t35-a : t35-b garde ${RELAY_MAC:-?} (lue : $(vm_neigh_mac vp-t35 10.35.1.11 10.35.1.253))" test "$(vm_neigh_mac vp-t35 10.35.1.11 10.35.1.253)" = "${RELAY_MAC}"
|
||||
vm_ssh vp-t35 10.35.1.11 'sudo ip neigh replace 10.35.1.10 lladdr 02:35:00:00:00:99 dev ens3 nud stale' >/dev/null 2>&1
|
||||
vm_frames vp-t35 10.35.1.10 garp ens3 10.35.1.10 5 >/dev/null 2>&1
|
||||
check "ARP gratuit de t35-a pour sa propre adresse : t35-b l'associe à ${AMAC} (témoin, lue : $(vm_neigh_mac vp-t35 10.35.1.11 10.35.1.10))" test "$(vm_neigh_mac vp-t35 10.35.1.11 10.35.1.10)" = "${AMAC}"
|
||||
|
||||
HANDLE=$(nft_handle vp-t35 "pre-${ATAP}" 'udp sport 67 drop')
|
||||
check "blocage du serveur DHCP retiré à la main de pre-${ATAP} (handle ${HANDLE:-absent})" ip netns exec vp-t35 nft delete rule bridge two "pre-${ATAP}" handle "${HANDLE}"
|
||||
check "watchdog : règle manquante signalée pour t35-a" wait_watchdog "interface 0: nftables rule missing in netns vp-t35: pre-${ATAP}: udp sport 67 drop"
|
||||
n=$(received_by_b "${S}.r" 10.35.1.10 "dhcp ens3 10.35.1.10 68 5" udp-from 10.35.1.10 67)
|
||||
check "sans la règle, le faux serveur DHCP de t35-a atteint t35-b (témoin, ${n} vue(s))" test "${n}" -gt 0
|
||||
check "blocage du serveur DHCP reposé" ip netns exec vp-t35 nft add rule bridge two "pre-${ATAP}" udp sport 67 drop
|
||||
n=$(received_by_b "${S}.r" 10.35.1.10 "dhcp ens3 10.35.1.10 68 5" udp-from 10.35.1.10 67)
|
||||
check "règle reposée : le faux serveur DHCP de t35-a n'atteint plus t35-b (${n} vue(s))" test "${n}" -eq 0
|
||||
|
||||
check "VM t35-c supprimée" api_delete vms/t35-c
|
||||
check "t35-c en deleted" wait_state vms/t35-c deleted
|
||||
check "t35-c supprimée : plus aucune règle pour ${CTAP}" test -z "$(nft_rules vp-t35 "${CTAP}")"
|
||||
dispatched () {
|
||||
local map
|
||||
map=$(ip netns exec vp-t35 nft list map bridge two pre_ports) || return 2
|
||||
grep -qF "\"${1}\" : jump pre-${1}" <<<"${map}"
|
||||
}
|
||||
not_dispatched () { dispatched "${1}"; [ $? -eq 1 ]; }
|
||||
check "t35-c supprimée : ${CTAP} retiré de pre_ports" not_dispatched "${CTAP}"
|
||||
check "t35-a toujours aiguillé par pre_ports (témoin)" dispatched "${ATAP}"
|
||||
check "t35-a garde ses 8 règles (témoin)" test "$(nft_rules vp-t35 "${ATAP}" | grep -c .)" -eq 8
|
||||
check "port d'injection ${INJ} retiré" ip -n vp-t35 link del "${INJ}"
|
||||
known "#76" "usurpation ARP de l'adresse d'émission 10.35.1.253 par t35-a : t35-b garde ${RELAY_MAC:-?} (${UNF} ; lue : $(vm_neigh_mac vp-t35 10.35.1.11 10.35.1.253))" test "$(vm_neigh_mac vp-t35 10.35.1.11 10.35.1.253)" = "${RELAY_MAC}"
|
||||
vm_ssh vp-t35 10.35.1.11 'sudo ip neigh replace 10.35.1.200 lladdr 02:35:00:00:00:99 dev ens3 nud stale' >/dev/null 2>&1
|
||||
vm_frames vp-t35 10.35.1.10 garp ens3 10.35.1.200 5 >/dev/null 2>&1
|
||||
check "usurpation ARP de 10.35.1.200 (adresse non protégée) par t35-a : t35-b l'associe à ${AMAC} (témoin, lue : $(vm_neigh_mac vp-t35 10.35.1.11 10.35.1.200))" test "$(vm_neigh_mac vp-t35 10.35.1.11 10.35.1.200)" = "${AMAC}"
|
||||
rm -f "${S}".*
|
||||
NODE
|
||||
|
|
|
|||
|
|
@ -1,64 +0,0 @@
|
|||
# fonctionnalité: segment public de l'hôte — vxlan-public (VNI 1) posé par le bootstrap sur br-public, VNI annoncé en EVPN et vu par les bordures, gateway anycast des bordures installée vers une seule bordure, gateway et destination de test joignables sans doublon, L2 entre hyperviseurs, veth des VPC sur le bridge public, watchdog
|
||||
# hôtes: hv1 hv2 hv3
|
||||
# tickets: #87
|
||||
|
||||
for host in hv1 hv2 hv3; do
|
||||
case "${host}" in
|
||||
hv1) pub=240.0.0.11 ;;
|
||||
hv2) pub=240.0.0.12 ;;
|
||||
hv3) pub=240.0.0.13 ;;
|
||||
esac
|
||||
on "${host}" "HOST=${host}" "PUB=${pub}" <<'NODE'
|
||||
NS=t45-pub
|
||||
GW_MAC=02:01:00:00:00:01
|
||||
vxlan_info () { ip -j -d link show dev vxlan-public | jq -r '.[0] | "\(.linkinfo.info_data.id) \(.linkinfo.info_data.local) \(.linkinfo.info_data.learning) \(.master) \(.operstate)"'; }
|
||||
remote_vtep () { vtysh -c 'show evpn vni 1' | grep -qwF "${1}"; }
|
||||
pings_clean () {
|
||||
local out
|
||||
out=$(ip netns exec "${NS}" ping -n -c 5 -i 0.2 -W 1 "${1}" 2>&1) || { echo "${out}" | tail -2; return 1; }
|
||||
! grep -q DUP <<<"${out}" || { echo "${out}" | grep -m1 DUP; return 1; }
|
||||
}
|
||||
check "${HOST} : vxlan-public, VNI 1, VTEP $(vtep_host), sans apprentissage, esclave de br-public, actif ($(vxlan_info))" test "$(vxlan_info)" = "1 $(vtep_host) false br-public UNKNOWN"
|
||||
check "${HOST} : VNI 1 de type L2 dans FRR" sh -c "vtysh -c 'show evpn vni 1' | grep -q 'Type: L2'"
|
||||
for bd in 192.168.14.21 192.168.14.22; do
|
||||
check "${HOST} : bordure ${bd} parmi les VTEP distants du VNI 1" remote_vtep "${bd}"
|
||||
done
|
||||
check "${HOST} : MAC de la gateway ${GW_MAC} installée vers une seule bordure" test "$(bridge fdb show dev vxlan-public | grep -c "^${GW_MAC} .*dst 192\.168\.14\.2[12] ")" -eq 1
|
||||
ip netns add "${NS}"
|
||||
ip link add t45-pub-h type veth peer name t45-pub-n netns "${NS}"
|
||||
ip link set t45-pub-h master br-public up
|
||||
ip -n "${NS}" link set t45-pub-n up
|
||||
ip -n "${NS}" addr add "${PUB}/24" dev t45-pub-n
|
||||
ip -n "${NS}" route add default via 240.0.0.1
|
||||
check "${HOST} : netns de test ${NS} sur br-public, ${PUB}/24" sh -c "ip -n ${NS} -4 -o addr show dev t45-pub-n | grep -q ' ${PUB}/24 '"
|
||||
check "${HOST} : gateway des bordures 240.0.0.1 joignable, sans doublon" pings_clean 240.0.0.1
|
||||
check "${HOST} : destination de test 240.255.0.1 joignable par la gateway, sans doublon" pings_clean 240.255.0.1
|
||||
check "${HOST} : ARP de la gateway résolu vers ${GW_MAC}" sh -c "ip -n ${NS} neigh show 240.0.0.1 | grep -q 'lladdr ${GW_MAC}'"
|
||||
NODE
|
||||
done
|
||||
|
||||
on bd1 <<'NODE'
|
||||
for hv in 192.168.14.11 192.168.14.12 192.168.14.13; do
|
||||
check "bd1 : hyperviseur ${hv} parmi les VTEP distants du VNI 1" sh -c "vtysh -c 'show evpn vni 1' | grep -qwF ${hv}"
|
||||
done
|
||||
NODE
|
||||
|
||||
on hv1 <<'NODE'
|
||||
NS=t45-pub
|
||||
pings_clean () {
|
||||
local out
|
||||
out=$(ip netns exec "${NS}" ping -n -c 5 -i 0.2 -W 1 "${1}" 2>&1) || { echo "${out}" | tail -2; return 1; }
|
||||
! grep -q DUP <<<"${out}" || { echo "${out}" | grep -m1 DUP; return 1; }
|
||||
}
|
||||
for peer in 240.0.0.12 240.0.0.13; do
|
||||
check "hv1 : ${peer} (netns de test d'un autre hyperviseur) joignable en L2 sur le VNI 1" pings_clean "${peer}"
|
||||
done
|
||||
check "VPC vp-t45 sur hv1" vpc_create vp-t45 10.45.0.0/16
|
||||
check "vp-t45 : vp-t45-e esclave du bridge public br-public (interfaces.internet par défaut)" sh -c "ip -o link show vp-t45-e | grep -q 'master br-public'"
|
||||
ip link set vxlan-public nomaster
|
||||
check "watchdog : br-public sans port VXLAN signalé" wait_watchdog "no vxlan port: the public segment is unreachable from this host"
|
||||
check "sans vxlan-public, la gateway des bordures ne répond plus (témoin)" sh -c "! ip netns exec ${NS} ping -n -c 2 -W 1 240.0.0.1 >/dev/null 2>&1"
|
||||
check "vxlan-public remis sur br-public" ip link set vxlan-public master br-public
|
||||
check "vxlan-public remis : la gateway des bordures répond de nouveau" sh -c "for i in 1 2 3 4 5 6 7 8 9 10; do ip netns exec ${NS} ping -n -c 1 -W 1 240.0.0.1 >/dev/null 2>&1 && exit 0; sleep 2; done; exit 1"
|
||||
check "vxlan-public remis : sans doublon" pings_clean 240.0.0.1
|
||||
NODE
|
||||
|
|
@ -48,7 +48,7 @@ sleep 20
|
|||
check "après redémarrage : même processus QEMU pour t61-a (ni arrêtée ni recréée)" test "$(pgrep -f "${VOLUMES}/t61-a.qcow2" | sort | tr '\n' ' ')" = "${QEMU}"
|
||||
check "après redémarrage : même processus QEMU pour t61-d" test "$(pgrep -f "${VOLUMES}/t61-d.qcow2" | sort | tr '\n' ' ')" = "${QEMU_D}"
|
||||
check "après redémarrage : mêmes fichiers sous /run/two, /etc/dnsmasq.d et les volumes (sinon, fichiers en cause)" section_unchanged "${STATE_DIR}/t61.restart" fichiers
|
||||
check "après redémarrage : hôte inchangé (netns, liens, taps, adresses, routes, nftables de pont, iptables, FRR, frr.conf, fichiers, unités)" same_as "${STATE_DIR}/t61.restart" host_state
|
||||
check "après redémarrage : hôte inchangé (netns, liens, taps, adresses, routes, ebtables, iptables, FRR, frr.conf, fichiers, unités)" same_as "${STATE_DIR}/t61.restart" host_state
|
||||
check "après redémarrage : l'API rend les mêmes ressources dans le même état" same_as "${STATE_DIR}/t61.api" api_snapshot
|
||||
for rr in 10.255.255.1 10.255.255.2; do
|
||||
check "après redémarrage : session EVPN vers ${rr} établie, jamais coupée" sh -c "vtysh -c 'show bgp neighbors ${rr}' | grep -q 'BGP state = Established' && [ \"\$(vtysh -c 'show bgp neighbors ${rr}' | grep -o 'dropped [0-9]*')\" = \"\$(cat '${STATE_DIR}/t61.${rr}')\" ]"
|
||||
|
|
|
|||
|
|
@ -11,13 +11,13 @@ for kind in vms subnets vpcs; do
|
|||
done
|
||||
check "${HOST} : aucun netns de VPC" none sh -c "ip netns list | awk '{print \$1}' | grep '^vp-'"
|
||||
check "${HOST} : aucun tap" none sh -c "ip -o link show | awk -F': ' '{print \$2}' | grep '^tap'"
|
||||
check "${HOST} : aucune interface de subnet (v-*, vxlan-*)" none sh -c "ip -o link show | awk -F': ' '{print \$2}' | cut -d@ -f1 | grep -E '^(v-|vxlan-[0-9])'"
|
||||
check "${HOST} : aucune interface de subnet (v-*, vxlan-*)" none sh -c "ip -o link show | awk -F': ' '{print \$2}' | cut -d@ -f1 | grep -E '^(v-|vxlan-)'"
|
||||
check "${HOST} : aucune VRF de VPC (vrf-*)" none sh -c "ip -o link show type vrf | awk -F': ' '{print \$2}' | grep '^vrf-'"
|
||||
check "${HOST} : aucune unité two-vm-*" none systemctl list-units --all --plain --no-legend 'two-vm-*'
|
||||
check "${HOST} : aucune instance DHCP (dhcp@, dnsmasq@) ni de métadonnées (metadata@) active" none sh -c "systemctl list-units --plain --no-legend 'dhcp@*' 'dnsmasq@*' 'metadata@*' | grep -w active"
|
||||
check "${HOST} : aucun socket de contrôle DHCP" none sh -c "ls /run/two/dhcp/*.sock 2>/dev/null"
|
||||
check "${HOST} : aucun processus QEMU" none pgrep -a qemu-system
|
||||
check "${HOST} : aucune règle nftables de pont qui vise une interface de subnet" none sh -c "nft list ruleset bridge | grep -E 'v-[a-z0-9]'"
|
||||
check "${HOST} : aucune règle ebtables qui vise une interface de subnet" none sh -c "{ ebtables -t filter -L; ebtables -t nat -L; } | grep -E -- '-(i|o) v-'"
|
||||
check "${HOST} : aucune règle iptables qui vise une interface de subnet ou de VPC" none sh -c "iptables-save | grep -E -- '(v-|vp-|tap)[a-z0-9]'"
|
||||
check "${HOST} : aucun volume de VM de test" none sh -c "ls '${VOLUMES}'/t[0-9][0-9]-* 2>/dev/null"
|
||||
check "${HOST} : FRR garde la base BGP (témoin)" sh -c "vtysh -c 'show running-config' | grep -q 'peer-group fabric'"
|
||||
|
|
|
|||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue