diff --git a/.forgejo/workflows/build.yml b/.forgejo/workflows/build.yml index 947e046..d80e151 100644 --- a/.forgejo/workflows/build.yml +++ b/.forgejo/workflows/build.yml @@ -33,7 +33,7 @@ jobs: echo "Building for ${BINARI}/${GOOS}/${GOARCH} (release: ${RELEASE_CIBLE})" go env GOOS GOARCH mkdir -p dist/ - go build -ldflags "-X main.version=${RELEASE_CIBLE}" -o dist/${BINARI}_${GOOS}_${GOARCH} ./cmd/${BINARI} + go build -o dist/${BINARI}_${GOOS}_${GOARCH} ./cmd/${BINARI} echo "artifact pour ${RELEASE_CIBLE} ${BINARI} ${GOOS} ${GOARCH}" > dist/${BINARI}-${GOOS}-${GOARCH}.txt ls -l ./dist - name: Upload artifact diff --git a/README.md b/README.md index 7aa1b95..9a1d9fa 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ bash ./deploy.sh -t 0.2.0 -i `deploy.sh` se met à jour lui-même depuis la branche, télécharge binaires, units systemd et scripts depuis la release, et les vérifie contre le manifeste `SHA256SUMS`. Le drapeau `-i` -prépare l'host : paquets, `sysctl`, et bridges. +prépare l'host : paquets, module `br_netfilter`, `sysctl`, et bridges. Options utiles : diff --git a/api/agent.yaml b/api/agent.yaml index 865ce7c..bb3bb10 100644 --- a/api/agent.yaml +++ b/api/agent.yaml @@ -489,63 +489,6 @@ paths: "500": $ref: "#/components/responses/InternalError" - # ── Host ─────────────────────────────────────────────────────────────────── - - /register: - get: - summary: Facts of the host that do not change while it runs - operationId: getRegister - description: >- - Read once when the agent starts and kept in memory. Will also be sent to the broker of the - orchestrator at startup. - responses: - "200": - description: Host facts - content: - application/json: - schema: - $ref: "#/components/schemas/HostRegister" - "405": - description: Method other than GET - content: - application/json: - schema: - $ref: "#/components/schemas/Error" - "503": - description: Host facts could not be read when the agent started (the error says why) - content: - application/json: - schema: - $ref: "#/components/schemas/Error" - - /ping: - get: - summary: Current state of the host and of the agent - operationId: getPing - description: >- - Last reading, taken when the agent starts then every times.host_state_interval seconds and - kept in memory; collected_at gives its age. boot_time and agent_start_time tell a host - reboot from an agent restart; boot_time plus uptime_seconds give the clock of the host. - responses: - "200": - description: Last host state - content: - application/json: - schema: - $ref: "#/components/schemas/HostPing" - "405": - description: Method other than GET - content: - application/json: - schema: - $ref: "#/components/schemas/Error" - "503": - description: No reading has succeeded yet - content: - application/json: - schema: - $ref: "#/components/schemas/Error" - components: parameters: @@ -989,87 +932,6 @@ components: type: string example: "203.0.113.1" - HostRegister: - type: object - properties: - hostname: - type: string - example: hv1 - cpu: - type: object - properties: - vendor: - type: string - example: AuthenticAMD - model: - type: string - example: AMD EPYC 7272 12-Core Processor - sockets: - type: integer - example: 1 - cores: - type: integer - description: Physical cores, all sockets together - example: 12 - threads: - type: integer - description: Logical processors, all sockets together - example: 24 - memory_bytes: - type: integer - format: int64 - example: 134217728000 - os: - type: object - description: From /etc/os-release - properties: - id: - type: string - example: debian - version_id: - type: string - example: "12" - pretty_name: - type: string - example: Debian GNU/Linux 12 (bookworm) - kernel: - type: string - example: 6.1.0-26-amd64 - - HostPing: - type: object - properties: - agent_version: - type: string - description: Release tag, commit for a lab build, undefined when not set at build time - example: 0.3.0 - collected_at: - type: integer - format: int64 - description: Unix time (seconds) of the reading - example: 1793520853 - boot_time: - type: integer - format: int64 - description: Unix time (seconds) the host booted - example: 1793499123 - agent_start_time: - type: integer - format: int64 - description: Unix time (seconds) the agent started - example: 1793499190 - uptime_seconds: - type: number - example: 21730.4 - load: - type: array - description: Load average over 1, 5 and 15 minutes - minItems: 3 - maxItems: 3 - items: - type: number - example: [0.12, 0.3, 0.25] - Error: type: object properties: diff --git a/cmd/agent/main.go b/cmd/agent/main.go index 3690f5c..4fed2d8 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -14,7 +14,6 @@ import ( configuration "git.g3e.fr/syonad/two/internal/config/agent" dispatcher "git.g3e.fr/syonad/two/internal/dispatcher/agent" "git.g3e.fr/syonad/two/internal/frr" - "git.g3e.fr/syonad/two/internal/host" "git.g3e.fr/syonad/two/internal/migration" "git.g3e.fr/syonad/two/internal/netif" agentmetrics "git.g3e.fr/syonad/two/internal/prometheus/agent" @@ -29,8 +28,6 @@ import ( const shutdownTimeout = 20 * time.Second -var version = "undefined" - func main() { confFile := flag.String("config", "/etc/two/agent.yml", "config file path") flag.Parse() @@ -38,7 +35,6 @@ func main() { } func run(confFile string) int { - start := time.Now() cfg, err := configuration.LoadConfig(confFile) if err != nil { slog.Error("failed to load config", "error", err) @@ -100,7 +96,6 @@ func run(confFile string) int { promAddr := fmt.Sprintf("%s:%d", cfg.Prometheus.Address, cfg.Prometheus.Port) log.Info("starting agent", - "version", version, "api", apiAddr, "prometheus", promAddr, "workers", cfg.Worker.Count, @@ -113,10 +108,7 @@ func run(confFile string) int { d := dispatcher.New(q, db, cfg, log.With(slog.String("component", "dispatcher"))) - hostInfo := host.NewCollector(os.DirFS("/"), version, start, cfg.Times.HostState(), log.With(slog.String("component", "host"))) - go hostInfo.Run(ctx) - - apiSrv := agentapi.New(d, db, log.With(slog.String("component", "api")), apiAddr, hostInfo) + apiSrv := agentapi.New(d, db, log.With(slog.String("component", "api")), apiAddr) go apiSrv.Start() promSrv := promserver.New(promAddr, registry) diff --git a/conf/agent/config.exemple.yml b/conf/agent/config.exemple.yml index 1424bcb..5bc380f 100644 --- a/conf/agent/config.exemple.yml +++ b/conf/agent/config.exemple.yml @@ -35,8 +35,6 @@ times: timeout_vms_start: 300 # Interval between each state check poll_interval: 2 - # Interval between two readings of the host state served by GET /ping - host_state_interval: 10 # Bridge interface used when the requested iface_type is not found in interfaces default_interface: br-000000 diff --git a/docs/architecture/vue-densemble.rst b/docs/architecture/vue-densemble.rst index ee0d8d5..1c06d4c 100644 --- a/docs/architecture/vue-densemble.rst +++ b/docs/architecture/vue-densemble.rst @@ -44,20 +44,18 @@ Paquets ``/24`` et ``/32`` * - ``internal/frr`` - génération des commandes FRR, seul point d'appel à ``vtysh`` - * - ``internal/host`` - - connaissance de l'hôte : faits lus au démarrage (``/register``), état mesuré périodiquement en mémoire (``/ping``) * - ``internal/sysctl`` - écriture sous ``/proc/sys``, dans un netns si besoin * - ``internal/netns`` - network namespaces : create/delete/call * - ``internal/netif`` - netlink : bridge, veth, vxlan, tap, routes, adresses - * - ``internal/nft`` - - wrapper dédié ; ne pas appeler ``nft`` ailleurs + * - ``internal/nft``, ``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 * - ``internal/vm`` - - cycle de vie d'une VM : tap, nftables, metadata, qemu + - cycle de vie d'une VM : tap, iptables, metadata, qemu * - ``internal/dhcp`` - plan d'adressage ip → mac, et configurations dnsmasq du backend historique * - ``internal/dhcpbackend`` diff --git a/docs/concepts/dns-ntp.rst b/docs/concepts/dns-ntp.rst index f92bbec..04b2f31 100644 --- a/docs/concepts/dns-ntp.rst +++ b/docs/concepts/dns-ntp.rst @@ -13,18 +13,18 @@ Chemin d'une requête .. mermaid:: graph LR - V["VM
vers interface_ip:53 ou :123"] --> N["netns du VPC
chaîne nftables hostsvc
DNAT vers 169.254.169.253"] + V["VM
vers interface_ip:53 ou :123"] --> N["netns du VPC
chaîne TWO-HOSTSVC
DNAT vers 169.254.169.253"] N --> H["two-hostsvc
socket dans le netns
169.254.169.253 sur la dummy hostsvc"] H -->|"depuis l'hôte"| U["serveurs amont
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 nftables ``hostsvc`` dans la table ``ip two``, accrochée à ``prerouting`` en - priorité ``dstnat``. 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``, pour le seul trafic à destination d'une adresse locale du netns - (``fib daddr type local`` : les gateways des subnets, anycast compris) ; +* 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 diff --git a/docs/concepts/vpc-subnet-vm.rst b/docs/concepts/vpc-subnet-vm.rst index 21ad46e..856dbfb 100644 --- a/docs/concepts/vpc-subnet-vm.rst +++ b/docs/concepts/vpc-subnet-vm.rst @@ -34,7 +34,7 @@ reçoivent du trafic. Il désactive aussi ``br_netfilter`` dans le netns conntrack voit chaque trame pontée et le SNAT de la passerelle NAT ne s'applique plus. Dans le netns, l'agent pose aussi la ``dummy`` ``hostsvc`` (``169.254.169.253/32``) et la chaîne -nftables ``hostsvc`` (table ``ip two``), puis demande à ``two-hostsvc`` de servir le VPC : c'est le chemin du +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. diff --git a/docs/demarrage/installation.rst b/docs/demarrage/installation.rst index 75778d2..92215b0 100644 --- a/docs/demarrage/installation.rst +++ b/docs/demarrage/installation.rst @@ -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) et QEMU ne fonctionnent que sous Linux. +namespaces, netlink, VXLAN, nftables, iptables) et QEMU ne fonctionnent que sous Linux. Déploiement ----------- @@ -75,7 +75,7 @@ Ce que fait ``-i`` ------------------ **Paquets** — ``qemu-system-x86``, ``qemu-utils``, ``ovmf``, ``dnsmasq``, ``nftables``, -``nfs-common``, ``jq``, ``curl``, **sans les paquets recommandés** : ceux de +``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 démarrage, la racine étant en tmpfs. ``qemu-utils`` est demandé explicitement : ``qemu-img`` sert @@ -87,11 +87,10 @@ dans les netns. aux hyperviseurs qui déclarent ``dhcp.backend: dnsmasq``, et tant qu'un hyperviseur peut revenir en arrière. Voir :doc:`/exploitation/configuration`. -**Noyau** — ``net.ipv4.ip_forward = 1``. ``br_netfilter`` n'est pas chargé : le filtrage des -ponts est en nftables (famille ``bridge``) et les redirections vers le serveur de metadata et -vers ``two-hostsvc`` sont posées dans le netns de chaque VPC, où l'agent coupe déjà -``bridge-nf-call-*``. Chargé, le module ferait passer tout le trafic bridgé des VM par netfilter -(``bridge-nf-call-iptables`` vaut 1 par défaut). +**Noyau** — chargement de ``br_netfilter``, puis ``net.ipv4.ip_forward = 1`` et +``net.bridge.bridge-nf-call-iptables = 1``. Cette dernière clé est **requise** par la DNAT vers +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, l'adresse et la route par défaut étant déplacées de l'interface physique vers le bridge. diff --git a/docs/developpement/lab.rst b/docs/developpement/lab.rst index ae90aef..f748423 100644 --- a/docs/developpement/lab.rst +++ b/docs/developpement/lab.rst @@ -274,7 +274,7 @@ 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 l'hôte et de chaque netns, +(``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. @@ -320,10 +320,6 @@ Générée par ``test/e2e/run.sh --matrice`` à partir des en-têtes ; à régé - machine d'états — 409 pendant creating, suppression depuis error, sockets QEMU d'une VM en error retirés, dépendances VM → subnet → VPC, suppression d'un subnet en attente de ses VM, arrêt d'une VM juste démarrée par renvoi de system_powerdown, recréation sous le même nom - hv1 - #41, #84, #79 - * - ``13-hote`` - - connaissance de l'hôte — /register (hostname, CPU, RAM, OS, noyau) lu au démarrage, /ping (version, démarrages de l'hôte et de l'agent, uptime, load) mesuré périodiquement, lecture seule, redémarrage de l'agent distingué de celui de l'hôte - - hv1 - - #39 * - ``21-dhcp-two`` - DHCP par le serveur intégré (backend two) — bail, server-id, renouvellement, option 121, un serveur par subnet, démarrages simultanés - hv1 diff --git a/docs/exploitation/configuration.rst b/docs/exploitation/configuration.rst index 030694f..9c788ab 100644 --- a/docs/exploitation/configuration.rst +++ b/docs/exploitation/configuration.rst @@ -80,7 +80,6 @@ Exécution des commandes timeout_subnet_start: 300 timeout_vms_start: 300 poll_interval: 2 - host_state_interval: 10 ``worker.count`` est le nombre de goroutines qui exécutent les commandes ; ``buffer_size`` le nombre de commandes en attente au-delà duquel ``Dispatch`` bloque. @@ -102,9 +101,6 @@ La section ``times`` règle, en secondes, les attentes des commandes asynchrones création d'une VM : attente des subnets de ses interfaces ``running``. ``poll_interval`` pas de toutes ces attentes. -``host_state_interval`` - intervalle entre deux mesures de l'état de l'hôte servi par ``GET /ping`` (voir - :doc:`observabilite`). Chaque valeur doit être strictement positive, et ``next_vms_shutdown`` inférieur à ``timeout_vms_stop`` ; sinon l'agent refuse de démarrer. Les attentes s'enchaînent — une VM, diff --git a/docs/exploitation/diagnostic.rst b/docs/exploitation/diagnostic.rst index 08a79db..1cb045f 100644 --- a/docs/exploitation/diagnostic.rst +++ b/docs/exploitation/diagnostic.rst @@ -150,7 +150,6 @@ Depuis l'host, l'instance correspondante : systemctl status 'metadata@' ls -l /run/two/metadata// - ip netns exec nft list chain ip two md- # une dnat par IP de la VM Le user-data est servi vide --------------------------- @@ -180,7 +179,8 @@ est redirigée vers ``two-hostsvc`` dans le netns du VPC (voir :doc:`../concepts systemctl status two-hostsvc cat /run/two/hostsvc/hostsvc.state # le VPC doit y figurer ip -n -4 addr show dev hostsvc # 169.254.169.253/32 - ip netns exec nft list chain ip two hostsvc # 2 return puis 3 dnat + ip netns exec iptables -t nat -S TWO-HOSTSVC # 2 RETURN puis 3 DNAT + ip netns exec iptables -t nat -L TWO-HOSTSVC -v -n # compteurs des redirections ip netns exec ss -lntu | grep 169.254.169.253 # 53/udp, 53/tcp, 123/udp journalctl -u two-hostsvc -n 50 diff --git a/docs/exploitation/observabilite.rst b/docs/exploitation/observabilite.rst index 7bd8b12..dee3e6a 100644 --- a/docs/exploitation/observabilite.rst +++ b/docs/exploitation/observabilite.rst @@ -27,31 +27,6 @@ Les états sont ceux du :doc:`cycle de vie `. Une valeur durable sur ``error`` est l'alerte la plus utile à poser ; une valeur durable sur ``creating`` ou ``deleting`` signale une opération qui n'aboutit pas. -Connaissance de l'hôte ----------------------- - -Deux routes de l'API de l'agent, en lecture seule (voir :doc:`api-agent/reference`) : - -``GET /register`` - ce qui ne change pas tant que l'hôte tourne : hostname, CPU (fabricant, modèle, sockets, - cœurs, threads), RAM totale, OS (``/etc/os-release``) et noyau. Lu une fois au démarrage de - l'agent ; si la lecture échoue, l'agent démarre quand même, journalise ``host facts - unavailable`` et la route répond 503 avec l'erreur. -``GET /ping`` - l'état courant : version de l'agent, démarrage de l'hôte (``boot_time``) et de l'agent - (``agent_start_time``), uptime de l'hôte et load sur 1, 5 et 15 minutes. Mesuré au démarrage - puis toutes les ``times.host_state_interval`` secondes (10 par défaut), gardé en mémoire ; - ``collected_at`` donne l'heure de la mesure. 503 tant qu'aucune mesure n'a réussi ; une mesure - en échec est journalisée (``host state collection failed``) et la précédente reste servie. - -Les dates sont des timestamps Unix en secondes. Un ``boot_time`` qui change signale un -redémarrage de l'hôte, un ``agent_start_time`` seul un redémarrage de l'agent ; -``boot_time`` + ``uptime_seconds`` donne l'horloge de l'hôte au moment de la mesure. - -La version de l'agent est injectée au build : le tag pour une release, le commit court -(suffixé ``-dirty`` si l'arbre avait des modifications) pour les binaires de test du lab, -``undefined`` sinon. Elle figure aussi dans la ligne ``starting agent`` du journal. - Watchdog -------- @@ -77,8 +52,8 @@ Dès qu'un VPC est ``running`` sur l'hyperviseur, il contrôle aussi le relais D * ``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 ``hostsvc`` de la table - ``ip two`` absente ou incomplète dans son netns. Un VPC créé par une version antérieure à la 0.3.0 n'a rien de tout + ``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. Avec une section ``bgp`` dans la configuration, il contrôle aussi le plan de contrôle : diff --git a/internal/api/agent/helpers_test.go b/internal/api/agent/helpers_test.go index 325ccb7..d3808ce 100644 --- a/internal/api/agent/helpers_test.go +++ b/internal/api/agent/helpers_test.go @@ -27,7 +27,7 @@ func newTestServerWith(t *testing.T, cfg *configuration.Config) (*Server, *badge q := worker.New(100) logger := slog.New(slog.NewTextHandler(io.Discard, nil)) d := dispatcher.New(q, db, cfg, logger) - return New(d, db, logger, "127.0.0.1:0", &fakeHost{}), db + return New(d, db, logger, "127.0.0.1:0"), db } func init() { diff --git a/internal/api/agent/host.go b/internal/api/agent/host.go deleted file mode 100644 index c4f0719..0000000 --- a/internal/api/agent/host.go +++ /dev/null @@ -1,56 +0,0 @@ -package agentapi - -import ( - "encoding/json" - "net/http" - - "git.g3e.fr/syonad/two/internal/host" -) - -type HostInfo interface { - Register() (host.Facts, error) - Ping() (host.Ping, bool) -} - -func (s *Server) RegisterHandler(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json") - if r.Method != http.MethodGet { - http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed) - return - } - f, err := s.host.Register() - if err != nil { - w.WriteHeader(http.StatusServiceUnavailable) - json.NewEncoder(w).Encode(ErrorResponse{Error: "host facts unavailable: " + err.Error()}) - return - } - json.NewEncoder(w).Encode(HostRegister{ - Hostname: f.Hostname, - CPU: HostCPU{Vendor: f.CPU.Vendor, Model: f.CPU.Model, Sockets: f.CPU.Sockets, Cores: f.CPU.Cores, Threads: f.CPU.Threads}, - MemoryBytes: f.MemoryBytes, - OS: HostOS{ID: f.OS.ID, VersionID: f.OS.VersionID, PrettyName: f.OS.PrettyName}, - Kernel: f.Kernel, - }) -} - -func (s *Server) PingHandler(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json") - if r.Method != http.MethodGet { - http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed) - return - } - p, ok := s.host.Ping() - if !ok { - w.WriteHeader(http.StatusServiceUnavailable) - json.NewEncoder(w).Encode(ErrorResponse{Error: "no host state collected yet"}) - return - } - json.NewEncoder(w).Encode(HostPing{ - AgentVersion: p.AgentVersion, - CollectedAt: p.CollectedAt, - BootTime: p.BootTime, - AgentStartTime: p.AgentStartTime, - UptimeSeconds: p.UptimeSeconds, - Load: p.Load, - }) -} diff --git a/internal/api/agent/host_test.go b/internal/api/agent/host_test.go deleted file mode 100644 index 455b7bc..0000000 --- a/internal/api/agent/host_test.go +++ /dev/null @@ -1,103 +0,0 @@ -package agentapi - -import ( - "encoding/json" - "errors" - "net/http" - "net/http/httptest" - "strings" - "testing" - - "git.g3e.fr/syonad/two/internal/host" -) - -type fakeHost struct { - facts host.Facts - factsErr error - ping *host.Ping -} - -func (f *fakeHost) Register() (host.Facts, error) { return f.facts, f.factsErr } - -func (f *fakeHost) Ping() (host.Ping, bool) { - if f.ping == nil { - return host.Ping{}, false - } - return *f.ping, true -} - -func serveHost(t *testing.T, h *fakeHost, method, path string) *httptest.ResponseRecorder { - t.Helper() - s, _ := newTestServer(t) - s.host = h - w := httptest.NewRecorder() - s.srv.Handler.ServeHTTP(w, httptest.NewRequest(method, path, nil)) - return w -} - -func TestRegister_ServesTheFacts(t *testing.T) { - h := &fakeHost{facts: host.Facts{ - Hostname: "hv1", - CPU: host.CPU{Vendor: "AuthenticAMD", Model: "AMD EPYC 7272 12-Core Processor", Sockets: 1, Cores: 12, Threads: 24}, - MemoryBytes: 134217728000, - OS: host.OS{ID: "debian", VersionID: "12", PrettyName: "Debian GNU/Linux 12 (bookworm)"}, - Kernel: "6.1.0-26-amd64", - }} - w := serveHost(t, h, http.MethodGet, "/register") - if w.Code != http.StatusOK { - t.Fatalf("attendu 200, obtenu %d : %s", w.Code, w.Body.String()) - } - want := `{"hostname":"hv1","cpu":{"vendor":"AuthenticAMD","model":"AMD EPYC 7272 12-Core Processor","sockets":1,"cores":12,"threads":24},"memory_bytes":134217728000,"os":{"id":"debian","version_id":"12","pretty_name":"Debian GNU/Linux 12 (bookworm)"},"kernel":"6.1.0-26-amd64"}` - if got := strings.TrimSpace(w.Body.String()); got != want { - t.Errorf("corps :\n%s\nattendu :\n%s", got, want) - } -} - -func TestRegister_UnavailableFacts(t *testing.T) { - w := serveHost(t, &fakeHost{factsErr: errors.New("open proc/cpuinfo: file does not exist")}, http.MethodGet, "/register") - if w.Code != http.StatusServiceUnavailable { - t.Fatalf("attendu 503, obtenu %d", w.Code) - } - var e ErrorResponse - if err := json.NewDecoder(w.Body).Decode(&e); err != nil || e.Error != "host facts unavailable: open proc/cpuinfo: file does not exist" { - t.Errorf("erreur %q, %v", e.Error, err) - } -} - -func TestPing_ServesTheLastState(t *testing.T) { - h := &fakeHost{ping: &host.Ping{ - State: host.State{BootTime: 1793499123, UptimeSeconds: 21730.4, Load: [3]float64{0.12, 0.3, 0.25}}, - AgentVersion: "0.3.0", - AgentStartTime: 1793499190, - CollectedAt: 1793520853, - }} - w := serveHost(t, h, http.MethodGet, "/ping") - if w.Code != http.StatusOK { - t.Fatalf("attendu 200, obtenu %d : %s", w.Code, w.Body.String()) - } - want := `{"agent_version":"0.3.0","collected_at":1793520853,"boot_time":1793499123,"agent_start_time":1793499190,"uptime_seconds":21730.4,"load":[0.12,0.3,0.25]}` - if got := strings.TrimSpace(w.Body.String()); got != want { - t.Errorf("corps :\n%s\nattendu :\n%s", got, want) - } -} - -func TestPing_BeforeTheFirstCollection(t *testing.T) { - w := serveHost(t, &fakeHost{}, http.MethodGet, "/ping") - if w.Code != http.StatusServiceUnavailable { - t.Fatalf("attendu 503, obtenu %d", w.Code) - } - if !strings.Contains(w.Body.String(), "no host state collected yet") { - t.Errorf("corps %s", w.Body.String()) - } -} - -func TestHostRoutes_ReadOnly(t *testing.T) { - h := &fakeHost{ping: &host.Ping{}} - for _, path := range []string{"/register", "/ping"} { - for _, method := range []string{http.MethodPost, http.MethodPut, http.MethodDelete} { - if w := serveHost(t, h, method, path); w.Code != http.StatusMethodNotAllowed { - t.Errorf("%s %s : attendu 405, obtenu %d", method, path, w.Code) - } - } - } -} diff --git a/internal/api/agent/models.go b/internal/api/agent/models.go index 4cfb827..e5b74b0 100644 --- a/internal/api/agent/models.go +++ b/internal/api/agent/models.go @@ -123,34 +123,3 @@ type FloatingIP struct { IP string `json:"ip,omitempty"` Gateway string `json:"gateway,omitempty"` } - -type HostCPU struct { - Vendor string `json:"vendor"` - Model string `json:"model"` - Sockets int `json:"sockets"` - Cores int `json:"cores"` - Threads int `json:"threads"` -} - -type HostOS struct { - ID string `json:"id"` - VersionID string `json:"version_id"` - PrettyName string `json:"pretty_name"` -} - -type HostRegister struct { - Hostname string `json:"hostname"` - CPU HostCPU `json:"cpu"` - MemoryBytes uint64 `json:"memory_bytes"` - OS HostOS `json:"os"` - Kernel string `json:"kernel"` -} - -type HostPing struct { - AgentVersion string `json:"agent_version"` - CollectedAt int64 `json:"collected_at"` - BootTime int64 `json:"boot_time"` - AgentStartTime int64 `json:"agent_start_time"` - UptimeSeconds float64 `json:"uptime_seconds"` - Load [3]float64 `json:"load"` -} diff --git a/internal/api/agent/server.go b/internal/api/agent/server.go index 524c686..fe7ddf1 100644 --- a/internal/api/agent/server.go +++ b/internal/api/agent/server.go @@ -17,12 +17,11 @@ type Server struct { dispatcher *dispatcher.Dispatcher db *badger.DB logger *slog.Logger - host HostInfo srv *http.Server } -func New(d *dispatcher.Dispatcher, db *badger.DB, logger *slog.Logger, address string, h HostInfo) *Server { - s := &Server{dispatcher: d, db: db, logger: logger, host: h} +func New(d *dispatcher.Dispatcher, db *badger.DB, logger *slog.Logger, address string) *Server { + s := &Server{dispatcher: d, db: db, logger: logger} mux := http.NewServeMux() mux.HandleFunc("/vpcs", s.VpcsHandler) mux.HandleFunc("/vpcs/", s.VpcByNameHandler) @@ -34,8 +33,6 @@ func New(d *dispatcher.Dispatcher, db *badger.DB, logger *slog.Logger, address s mux.HandleFunc("/nat-gateways/", s.NATGatewayByNameHandler) mux.HandleFunc("/floating-ips", s.FloatingIPsHandler) mux.HandleFunc("/floating-ips/", s.FloatingIPByNameHandler) - mux.HandleFunc("/register", s.RegisterHandler) - mux.HandleFunc("/ping", s.PingHandler) s.srv = &http.Server{Addr: address, Handler: s.logMiddleware(mux)} return s } diff --git a/internal/config/agent/struct.go b/internal/config/agent/struct.go index 37285d7..d0ea279 100644 --- a/internal/config/agent/struct.go +++ b/internal/config/agent/struct.go @@ -78,7 +78,6 @@ func LoadConfig(path string) (*Config, error) { v.SetDefault("times.timeout_subnet_start", TimeoutSubnetStart) v.SetDefault("times.timeout_vms_start", TimeoutVMsStart) v.SetDefault("times.poll_interval", PollInterval) - v.SetDefault("times.host_state_interval", HostStateInterval) v.SetDefault("metadata.run_dir", "/run/two/metadata") v.SetDefault("dhcp.backend", BackendTwo) v.SetDefault("hostsvc.dns_upstream", []string{}) diff --git a/internal/config/agent/times.go b/internal/config/agent/times.go index 13952e1..c482029 100644 --- a/internal/config/agent/times.go +++ b/internal/config/agent/times.go @@ -13,7 +13,6 @@ const ( TimeoutSubnetStart = 300 TimeoutVMsStart = 300 PollInterval = 2 - HostStateInterval = 10 ) type Times struct { @@ -24,7 +23,6 @@ type Times struct { TimeoutSubnetStart int `mapstructure:"timeout_subnet_start"` TimeoutVMsStart int `mapstructure:"timeout_vms_start"` PollInterval int `mapstructure:"poll_interval"` - HostStateInterval int `mapstructure:"host_state_interval"` } func seconds(value, fallback int) time.Duration { @@ -41,7 +39,6 @@ func (t Times) VPCStop() time.Duration { return seconds(t.TimeoutVPCStop, Ti func (t Times) SubnetStart() time.Duration { return seconds(t.TimeoutSubnetStart, TimeoutSubnetStart) } func (t Times) VMsStart() time.Duration { return seconds(t.TimeoutVMsStart, TimeoutVMsStart) } func (t Times) Poll() time.Duration { return seconds(t.PollInterval, PollInterval) } -func (t Times) HostState() time.Duration { return seconds(t.HostStateInterval, HostStateInterval) } func (t Times) Validate() error { for _, f := range []struct { @@ -55,7 +52,6 @@ func (t Times) Validate() error { {"timeout_subnet_start", t.TimeoutSubnetStart}, {"timeout_vms_start", t.TimeoutVMsStart}, {"poll_interval", t.PollInterval}, - {"host_state_interval", t.HostStateInterval}, } { if f.value <= 0 { return fmt.Errorf("times.%s %d must be a positive number of seconds", f.key, f.value) diff --git a/internal/config/agent/times_test.go b/internal/config/agent/times_test.go index 75a91ae..b35330c 100644 --- a/internal/config/agent/times_test.go +++ b/internal/config/agent/times_test.go @@ -19,7 +19,6 @@ func TestLoadConfig_TimesDefauts(t *testing.T) { TimeoutSubnetStart: 300, TimeoutVMsStart: 300, PollInterval: 2, - HostStateInterval: 10, } if cfg.Times != want { t.Errorf("times par défaut : %+v, attendu %+v", cfg.Times, want) @@ -27,11 +26,11 @@ func TestLoadConfig_TimesDefauts(t *testing.T) { } func TestLoadConfig_TimesValeursExplicites(t *testing.T) { - cfg, err := LoadConfig(writeYAML(t, "times:\n timeout_vms_stop: 600\n next_vms_shutdown: 10\n timeout_subnet_stop: 700\n timeout_vpc_stop: 800\n timeout_subnet_start: 50\n timeout_vms_start: 60\n poll_interval: 1\n host_state_interval: 5\n")) + cfg, err := LoadConfig(writeYAML(t, "times:\n timeout_vms_stop: 600\n next_vms_shutdown: 10\n timeout_subnet_stop: 700\n timeout_vpc_stop: 800\n timeout_subnet_start: 50\n timeout_vms_start: 60\n poll_interval: 1\n")) if err != nil { t.Fatalf("LoadConfig a échoué : %v", err) } - want := Times{600, 10, 700, 800, 50, 60, 1, 5} + want := Times{600, 10, 700, 800, 50, 60, 1} if cfg.Times != want { t.Errorf("times : %+v, attendu %+v", cfg.Times, want) } @@ -56,7 +55,6 @@ func TestLoadConfig_TimesInvalidesRefuses(t *testing.T) { {"times:\n timeout_subnet_start: 0\n", "times.timeout_subnet_start 0"}, {"times:\n timeout_vms_start: 0\n", "times.timeout_vms_start 0"}, {"times:\n poll_interval: 0\n", "times.poll_interval 0"}, - {"times:\n host_state_interval: 0\n", "times.host_state_interval 0"}, {"times:\n next_vms_shutdown: 300\n", "times.next_vms_shutdown 300 must be below times.timeout_vms_stop 300"}, } { if _, err := LoadConfig(writeYAML(t, c.yaml)); err == nil || !strings.Contains(err.Error(), c.msg) { @@ -81,15 +79,14 @@ func TestTimesDurees(t *testing.T) { {"SubnetStart", zero.SubnetStart(), 300 * time.Second}, {"VMsStart", zero.VMsStart(), 300 * time.Second}, {"Poll", zero.Poll(), 2 * time.Second}, - {"HostState", zero.HostState(), 10 * time.Second}, } for _, c := range cases { if c.got != c.want { t.Errorf("Times{}.%s() = %v, attendu %v", c.name, c.got, c.want) } } - set := Times{11, 12, 13, 14, 15, 16, 17, 18} - got := []time.Duration{set.VMsStop(), set.VMsShutdown(), set.SubnetStop(), set.VPCStop(), set.SubnetStart(), set.VMsStart(), set.Poll(), set.HostState()} + set := Times{11, 12, 13, 14, 15, 16, 17} + got := []time.Duration{set.VMsStop(), set.VMsShutdown(), set.SubnetStop(), set.VPCStop(), set.SubnetStart(), set.VMsStart(), set.Poll()} for i, d := range got { if want := time.Duration(11+i) * time.Second; d != want { t.Errorf("durée %d = %v, attendu %v", i, d, want) diff --git a/internal/host/collector.go b/internal/host/collector.go deleted file mode 100644 index e215154..0000000 --- a/internal/host/collector.go +++ /dev/null @@ -1,78 +0,0 @@ -package host - -import ( - "context" - "io/fs" - "log/slog" - "sync" - "time" -) - -type Ping struct { - State - AgentVersion string - AgentStartTime int64 - CollectedAt int64 -} - -type Collector struct { - fsys fs.FS - version string - start time.Time - interval time.Duration - logger *slog.Logger - now func() time.Time - facts Facts - factsErr error - - mu sync.RWMutex - last *Ping -} - -func NewCollector(fsys fs.FS, version string, start time.Time, interval time.Duration, logger *slog.Logger) *Collector { - c := &Collector{fsys: fsys, version: version, start: start, interval: interval, logger: logger, now: time.Now} - c.facts, c.factsErr = ReadFacts(fsys) - if c.factsErr != nil { - logger.Error("host facts unavailable", "error", c.factsErr) - } - return c -} - -func (c *Collector) Register() (Facts, error) { - return c.facts, c.factsErr -} - -func (c *Collector) Ping() (Ping, bool) { - c.mu.RLock() - defer c.mu.RUnlock() - if c.last == nil { - return Ping{}, false - } - return *c.last, true -} - -func (c *Collector) Run(ctx context.Context) { - ticker := time.NewTicker(c.interval) - defer ticker.Stop() - c.collect() - for { - select { - case <-ctx.Done(): - return - case <-ticker.C: - c.collect() - } - } -} - -func (c *Collector) collect() { - s, err := ReadState(c.fsys) - if err != nil { - c.logger.Error("host state collection failed", "error", err) - return - } - p := Ping{State: s, AgentVersion: c.version, AgentStartTime: c.start.Unix(), CollectedAt: c.now().Unix()} - c.mu.Lock() - c.last = &p - c.mu.Unlock() -} diff --git a/internal/host/collector_test.go b/internal/host/collector_test.go deleted file mode 100644 index 90205cb..0000000 --- a/internal/host/collector_test.go +++ /dev/null @@ -1,102 +0,0 @@ -package host - -import ( - "context" - "io" - "log/slog" - "strings" - "testing" - "time" -) - -func discard() *slog.Logger { return slog.New(slog.NewTextHandler(io.Discard, nil)) } - -func TestCollector_RegisterServesTheFactsReadAtCreation(t *testing.T) { - fsys := debian12() - c := NewCollector(fsys, "0.3.0", time.Unix(1792210950, 0), time.Hour, discard()) - fsys["proc/sys/kernel/hostname"].Data = []byte("autre\n") - f, err := c.Register() - if err != nil || f.Hostname != "hv1" { - t.Errorf("Register = %q, %v ; hv1 lu à la création attendu", f.Hostname, err) - } -} - -func TestCollector_RegisterKeepsTheReadError(t *testing.T) { - fsys := debian12() - delete(fsys, "proc/cpuinfo") - var logs strings.Builder - c := NewCollector(fsys, "0.3.0", time.Unix(0, 0), time.Hour, slog.New(slog.NewTextHandler(&logs, nil))) - if _, err := c.Register(); err == nil { - t.Error("Register sans cpuinfo : aucune erreur") - } - if !strings.Contains(logs.String(), "host facts unavailable") { - t.Errorf("erreur non journalisée : %q", logs.String()) - } -} - -func TestCollector_PingBeforeAnyCollection(t *testing.T) { - c := NewCollector(debian12(), "0.3.0", time.Unix(0, 0), time.Hour, discard()) - if _, ok := c.Ping(); ok { - t.Error("Ping avant toute mesure : instantané rendu") - } -} - -func TestCollector_PingServesTheLastCollection(t *testing.T) { - c := NewCollector(debian12(), "0.3.0", time.Unix(1792210950, 0), time.Hour, discard()) - c.now = func() time.Time { return time.Unix(1793454325, 0) } - c.collect() - got, ok := c.Ping() - want := Ping{ - State: State{BootTime: 1792210880, UptimeSeconds: 1243445.41, Load: [3]float64{0.08, 0.03, 0.04}}, - AgentVersion: "0.3.0", - AgentStartTime: 1792210950, - CollectedAt: 1793454325, - } - if !ok || got != want { - t.Errorf("Ping = %+v, %v ; %+v attendu", got, ok, want) - } -} - -func TestCollector_FailedCollectionKeepsThePreviousOne(t *testing.T) { - fsys := debian12() - var logs strings.Builder - c := NewCollector(fsys, "0.3.0", time.Unix(0, 0), time.Hour, slog.New(slog.NewTextHandler(&logs, nil))) - c.now = func() time.Time { return time.Unix(100, 0) } - c.collect() - delete(fsys, "proc/loadavg") - c.now = func() time.Time { return time.Unix(200, 0) } - c.collect() - got, ok := c.Ping() - if !ok || got.CollectedAt != 100 { - t.Errorf("Ping = %+v, %v ; mesure de 100 gardée attendue", got, ok) - } - if !strings.Contains(logs.String(), "host state collection failed") { - t.Errorf("échec non journalisé : %q", logs.String()) - } -} - -func TestCollector_RunCollectsAtOnceAndStopsWithTheContext(t *testing.T) { - c := NewCollector(debian12(), "0.3.0", time.Unix(0, 0), time.Hour, discard()) - ctx, cancel := context.WithCancel(context.Background()) - done := make(chan struct{}) - go func() { - c.Run(ctx) - close(done) - }() - deadline := time.Now().Add(5 * time.Second) - for { - if _, ok := c.Ping(); ok { - break - } - if time.Now().After(deadline) { - t.Fatal("aucune mesure au démarrage de Run") - } - time.Sleep(10 * time.Millisecond) - } - cancel() - select { - case <-done: - case <-time.After(5 * time.Second): - t.Fatal("Run ne s'arrête pas à l'annulation du contexte") - } -} diff --git a/internal/host/host.go b/internal/host/host.go deleted file mode 100644 index adcb7f7..0000000 --- a/internal/host/host.go +++ /dev/null @@ -1,225 +0,0 @@ -package host - -import ( - "bufio" - "fmt" - "io/fs" - "strconv" - "strings" -) - -type CPU struct { - Vendor string - Model string - Sockets int - Cores int - Threads int -} - -type OS struct { - ID string - VersionID string - PrettyName string -} - -type Facts struct { - Hostname string - CPU CPU - MemoryBytes uint64 - OS OS - Kernel string -} - -type State struct { - BootTime int64 - UptimeSeconds float64 - Load [3]float64 -} - -func ReadFacts(fsys fs.FS) (Facts, error) { - var f Facts - var err error - if f.Hostname, err = readLine(fsys, "proc/sys/kernel/hostname"); err != nil { - return Facts{}, err - } - if f.Kernel, err = readLine(fsys, "proc/sys/kernel/osrelease"); err != nil { - return Facts{}, err - } - data, err := fs.ReadFile(fsys, "proc/cpuinfo") - if err != nil { - return Facts{}, err - } - if f.CPU, err = parseCPUInfo(string(data)); err != nil { - return Facts{}, err - } - data, err = fs.ReadFile(fsys, "proc/meminfo") - if err != nil { - return Facts{}, err - } - if f.MemoryBytes, err = parseMemTotal(string(data)); err != nil { - return Facts{}, err - } - data, err = fs.ReadFile(fsys, "etc/os-release") - if err != nil { - return Facts{}, err - } - f.OS = parseOSRelease(string(data)) - return f, nil -} - -func ReadState(fsys fs.FS) (State, error) { - var s State - data, err := fs.ReadFile(fsys, "proc/stat") - if err != nil { - return State{}, err - } - if s.BootTime, err = parseBootTime(string(data)); err != nil { - return State{}, err - } - line, err := readLine(fsys, "proc/uptime") - if err != nil { - return State{}, err - } - if s.UptimeSeconds, err = parseUptime(line); err != nil { - return State{}, err - } - if line, err = readLine(fsys, "proc/loadavg"); err != nil { - return State{}, err - } - if s.Load, err = parseLoadAvg(line); err != nil { - return State{}, err - } - return s, nil -} - -func readLine(fsys fs.FS, name string) (string, error) { - data, err := fs.ReadFile(fsys, name) - if err != nil { - return "", err - } - line := strings.TrimSpace(string(data)) - if line == "" { - return "", fmt.Errorf("%s: empty", name) - } - return line, nil -} - -func parseCPUInfo(data string) (CPU, error) { - var cpu CPU - sockets := map[string]bool{} - cores := map[string]bool{} - var physical string - scanner := bufio.NewScanner(strings.NewReader(data)) - for scanner.Scan() { - key, value, ok := strings.Cut(scanner.Text(), ":") - if !ok { - continue - } - value = strings.TrimSpace(value) - switch strings.TrimSpace(key) { - case "processor": - cpu.Threads++ - physical = "" - case "vendor_id": - cpu.Vendor = value - case "model name": - cpu.Model = value - case "physical id": - physical = value - sockets[value] = true - case "core id": - if physical == "" { - return CPU{}, fmt.Errorf("cpuinfo: core id %s without physical id", value) - } - cores[physical+"/"+value] = true - } - } - if err := scanner.Err(); err != nil { - return CPU{}, fmt.Errorf("cpuinfo: %w", err) - } - if cpu.Threads == 0 || len(sockets) == 0 || len(cores) == 0 { - return CPU{}, fmt.Errorf("cpuinfo: %d processors, %d physical ids, %d core ids", cpu.Threads, len(sockets), len(cores)) - } - cpu.Sockets = len(sockets) - cpu.Cores = len(cores) - return cpu, nil -} - -func parseMemTotal(data string) (uint64, error) { - for _, line := range strings.Split(data, "\n") { - fields := strings.Fields(line) - if len(fields) != 3 || fields[0] != "MemTotal:" || fields[2] != "kB" { - continue - } - kb, err := strconv.ParseUint(fields[1], 10, 64) - if err != nil { - return 0, fmt.Errorf("meminfo: MemTotal: %w", err) - } - return kb * 1024, nil - } - return 0, fmt.Errorf("meminfo: no MemTotal in kB") -} - -func parseOSRelease(data string) OS { - var rel OS - for _, line := range strings.Split(data, "\n") { - key, value, ok := strings.Cut(strings.TrimSpace(line), "=") - if !ok { - continue - } - if unquoted, err := strconv.Unquote(value); err == nil { - value = unquoted - } - switch key { - case "ID": - rel.ID = value - case "VERSION_ID": - rel.VersionID = value - case "PRETTY_NAME": - rel.PrettyName = value - } - } - return rel -} - -func parseBootTime(data string) (int64, error) { - for _, line := range strings.Split(data, "\n") { - fields := strings.Fields(line) - if len(fields) == 2 && fields[0] == "btime" { - t, err := strconv.ParseInt(fields[1], 10, 64) - if err != nil { - return 0, fmt.Errorf("stat: btime: %w", err) - } - return t, nil - } - } - return 0, fmt.Errorf("stat: no btime") -} - -func parseUptime(line string) (float64, error) { - fields := strings.Fields(line) - if len(fields) != 2 { - return 0, fmt.Errorf("uptime: %q", line) - } - up, err := strconv.ParseFloat(fields[0], 64) - if err != nil { - return 0, fmt.Errorf("uptime: %w", err) - } - return up, nil -} - -func parseLoadAvg(line string) ([3]float64, error) { - var load [3]float64 - fields := strings.Fields(line) - if len(fields) != 5 { - return load, fmt.Errorf("loadavg: %q", line) - } - for i := range load { - v, err := strconv.ParseFloat(fields[i], 64) - if err != nil { - return load, fmt.Errorf("loadavg: %w", err) - } - load[i] = v - } - return load, nil -} diff --git a/internal/host/host_test.go b/internal/host/host_test.go deleted file mode 100644 index e2a13b8..0000000 --- a/internal/host/host_test.go +++ /dev/null @@ -1,128 +0,0 @@ -package host - -import ( - "os" - "strings" - "testing" - "testing/fstest" -) - -func debian12() fstest.MapFS { - fsys := fstest.MapFS{} - for _, name := range []string{ - "proc/cpuinfo", "proc/meminfo", "proc/stat", "proc/uptime", "proc/loadavg", - "proc/sys/kernel/osrelease", "proc/sys/kernel/hostname", "etc/os-release", - } { - data, err := os.ReadFile("testdata/debian12/" + name) - if err != nil { - panic(err) - } - fsys[name] = &fstest.MapFile{Data: data} - } - return fsys -} - -func TestReadFacts_Debian12(t *testing.T) { - got, err := ReadFacts(debian12()) - if err != nil { - t.Fatalf("ReadFacts: %v", err) - } - want := Facts{ - Hostname: "hv1", - CPU: CPU{Vendor: "AuthenticAMD", Model: "QEMU Virtual CPU version 2.5+", Sockets: 1, Cores: 2, Threads: 2}, - MemoryBytes: 2010432 * 1024, - OS: OS{ID: "debian", VersionID: "12", PrettyName: "Debian GNU/Linux 12 (bookworm)"}, - Kernel: "6.8.0-50-generic", - } - if got != want { - t.Errorf("faits %+v, %+v attendus", got, want) - } -} - -func TestReadState_Debian12(t *testing.T) { - got, err := ReadState(debian12()) - if err != nil { - t.Fatalf("ReadState: %v", err) - } - want := State{BootTime: 1792210880, UptimeSeconds: 1243445.41, Load: [3]float64{0.08, 0.03, 0.04}} - if got != want { - t.Errorf("état %+v, %+v attendu", got, want) - } -} - -func TestParseCPUInfo_SocketsCoresAndThreadsAreDistinct(t *testing.T) { - data, err := os.ReadFile("testdata/cpuinfo-2x2x2") - if err != nil { - t.Fatal(err) - } - got, err := parseCPUInfo(string(data)) - if err != nil { - t.Fatalf("parseCPUInfo: %v", err) - } - if got.Sockets != 2 || got.Cores != 4 || got.Threads != 8 { - t.Errorf("%d sockets, %d cœurs, %d threads ; 2, 4 et 8 attendus", got.Sockets, got.Cores, got.Threads) - } -} - -func TestParseCPUInfo_RefusesMissingTopology(t *testing.T) { - data, err := os.ReadFile("testdata/debian12/proc/cpuinfo") - if err != nil { - t.Fatal(err) - } - for label, input := range map[string]string{ - "vide": "", - "sans physical id": strings.ReplaceAll(string(data), "physical id\t: 0\n", ""), - "sans core id": strings.ReplaceAll(strings.ReplaceAll(string(data), "core id\t\t: 0\n", ""), "core id\t\t: 1\n", ""), - } { - if _, err := parseCPUInfo(input); err == nil { - t.Errorf("%s : accepté", label) - } - } -} - -func TestReadFacts_FailsOnAMissingFile(t *testing.T) { - for _, name := range []string{"proc/cpuinfo", "proc/meminfo", "etc/os-release", "proc/sys/kernel/osrelease", "proc/sys/kernel/hostname"} { - fsys := debian12() - delete(fsys, name) - if _, err := ReadFacts(fsys); err == nil || !strings.Contains(err.Error(), name) { - t.Errorf("sans %s : erreur %v", name, err) - } - } -} - -func TestReadState_FailsOnAMissingFile(t *testing.T) { - for _, name := range []string{"proc/stat", "proc/uptime", "proc/loadavg"} { - fsys := debian12() - delete(fsys, name) - if _, err := ReadState(fsys); err == nil || !strings.Contains(err.Error(), name) { - t.Errorf("sans %s : erreur %v", name, err) - } - } -} - -func TestParseOSRelease_Rocky10Quoted(t *testing.T) { - data, err := os.ReadFile("testdata/rocky10/etc/os-release") - if err != nil { - t.Fatal(err) - } - got := parseOSRelease(string(data)) - want := OS{ID: "rocky", VersionID: "10.2", PrettyName: "Rocky Linux 10.2 (Red Quartz)"} - if got != want { - t.Errorf("%+v, %+v attendu", got, want) - } -} - -func TestParsers_RefuseMalformedInput(t *testing.T) { - if _, err := parseMemTotal("MemFree: 12 kB\n"); err == nil { - t.Error("meminfo sans MemTotal accepté") - } - if _, err := parseBootTime("cpu 1 2 3\n"); err == nil { - t.Error("stat sans btime accepté") - } - if _, err := parseUptime("12.5"); err == nil { - t.Error("uptime à un champ accepté") - } - if _, err := parseLoadAvg("0.1 0.2 x 1/2 3"); err == nil { - t.Error("loadavg invalide accepté") - } -} diff --git a/internal/host/testdata/cpuinfo-2x2x2 b/internal/host/testdata/cpuinfo-2x2x2 deleted file mode 100644 index 6b02b3d..0000000 --- a/internal/host/testdata/cpuinfo-2x2x2 +++ /dev/null @@ -1,224 +0,0 @@ -processor : 0 -vendor_id : AuthenticAMD -cpu family : 15 -model : 107 -model name : QEMU Virtual CPU version 2.5+ -stepping : 1 -microcode : 0x1000065 -cpu MHz : 999.997 -cache size : 512 KB -physical id : 0 -siblings : 4 -core id : 0 -cpu cores : 2 -apicid : 0 -initial apicid : 0 -fpu : yes -fpu_exception : yes -cpuid level : 13 -wp : yes -flags : fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx lm rep_good nopl cpuid extd_apicid pni cx16 hypervisor lahf_lm cmp_legacy svm 3dnowprefetch vmmcall -bugs : fxsave_leak sysret_ss_attrs null_seg swapgs_fence amd_e400 spectre_v1 spectre_v2 -bogomips : 1999.99 -TLB size : 1024 4K pages -clflush size : 64 -cache_alignment : 64 -address sizes : 40 bits physical, 48 bits virtual -power management: - -processor : 1 -vendor_id : AuthenticAMD -cpu family : 15 -model : 107 -model name : QEMU Virtual CPU version 2.5+ -stepping : 1 -microcode : 0x1000065 -cpu MHz : 999.997 -cache size : 512 KB -physical id : 0 -siblings : 4 -core id : 1 -cpu cores : 2 -apicid : 0 -initial apicid : 0 -fpu : yes -fpu_exception : yes -cpuid level : 13 -wp : yes -flags : fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx lm rep_good nopl cpuid extd_apicid pni cx16 hypervisor lahf_lm cmp_legacy svm 3dnowprefetch vmmcall -bugs : fxsave_leak sysret_ss_attrs null_seg swapgs_fence amd_e400 spectre_v1 spectre_v2 -bogomips : 1999.99 -TLB size : 1024 4K pages -clflush size : 64 -cache_alignment : 64 -address sizes : 40 bits physical, 48 bits virtual -power management: - -processor : 2 -vendor_id : AuthenticAMD -cpu family : 15 -model : 107 -model name : QEMU Virtual CPU version 2.5+ -stepping : 1 -microcode : 0x1000065 -cpu MHz : 999.997 -cache size : 512 KB -physical id : 1 -siblings : 4 -core id : 0 -cpu cores : 2 -apicid : 0 -initial apicid : 0 -fpu : yes -fpu_exception : yes -cpuid level : 13 -wp : yes -flags : fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx lm rep_good nopl cpuid extd_apicid pni cx16 hypervisor lahf_lm cmp_legacy svm 3dnowprefetch vmmcall -bugs : fxsave_leak sysret_ss_attrs null_seg swapgs_fence amd_e400 spectre_v1 spectre_v2 -bogomips : 1999.99 -TLB size : 1024 4K pages -clflush size : 64 -cache_alignment : 64 -address sizes : 40 bits physical, 48 bits virtual -power management: - -processor : 3 -vendor_id : AuthenticAMD -cpu family : 15 -model : 107 -model name : QEMU Virtual CPU version 2.5+ -stepping : 1 -microcode : 0x1000065 -cpu MHz : 999.997 -cache size : 512 KB -physical id : 1 -siblings : 4 -core id : 1 -cpu cores : 2 -apicid : 0 -initial apicid : 0 -fpu : yes -fpu_exception : yes -cpuid level : 13 -wp : yes -flags : fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx lm rep_good nopl cpuid extd_apicid pni cx16 hypervisor lahf_lm cmp_legacy svm 3dnowprefetch vmmcall -bugs : fxsave_leak sysret_ss_attrs null_seg swapgs_fence amd_e400 spectre_v1 spectre_v2 -bogomips : 1999.99 -TLB size : 1024 4K pages -clflush size : 64 -cache_alignment : 64 -address sizes : 40 bits physical, 48 bits virtual -power management: - -processor : 4 -vendor_id : AuthenticAMD -cpu family : 15 -model : 107 -model name : QEMU Virtual CPU version 2.5+ -stepping : 1 -microcode : 0x1000065 -cpu MHz : 999.997 -cache size : 512 KB -physical id : 0 -siblings : 4 -core id : 0 -cpu cores : 2 -apicid : 0 -initial apicid : 0 -fpu : yes -fpu_exception : yes -cpuid level : 13 -wp : yes -flags : fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx lm rep_good nopl cpuid extd_apicid pni cx16 hypervisor lahf_lm cmp_legacy svm 3dnowprefetch vmmcall -bugs : fxsave_leak sysret_ss_attrs null_seg swapgs_fence amd_e400 spectre_v1 spectre_v2 -bogomips : 1999.99 -TLB size : 1024 4K pages -clflush size : 64 -cache_alignment : 64 -address sizes : 40 bits physical, 48 bits virtual -power management: - -processor : 5 -vendor_id : AuthenticAMD -cpu family : 15 -model : 107 -model name : QEMU Virtual CPU version 2.5+ -stepping : 1 -microcode : 0x1000065 -cpu MHz : 999.997 -cache size : 512 KB -physical id : 0 -siblings : 4 -core id : 1 -cpu cores : 2 -apicid : 0 -initial apicid : 0 -fpu : yes -fpu_exception : yes -cpuid level : 13 -wp : yes -flags : fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx lm rep_good nopl cpuid extd_apicid pni cx16 hypervisor lahf_lm cmp_legacy svm 3dnowprefetch vmmcall -bugs : fxsave_leak sysret_ss_attrs null_seg swapgs_fence amd_e400 spectre_v1 spectre_v2 -bogomips : 1999.99 -TLB size : 1024 4K pages -clflush size : 64 -cache_alignment : 64 -address sizes : 40 bits physical, 48 bits virtual -power management: - -processor : 6 -vendor_id : AuthenticAMD -cpu family : 15 -model : 107 -model name : QEMU Virtual CPU version 2.5+ -stepping : 1 -microcode : 0x1000065 -cpu MHz : 999.997 -cache size : 512 KB -physical id : 1 -siblings : 4 -core id : 0 -cpu cores : 2 -apicid : 0 -initial apicid : 0 -fpu : yes -fpu_exception : yes -cpuid level : 13 -wp : yes -flags : fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx lm rep_good nopl cpuid extd_apicid pni cx16 hypervisor lahf_lm cmp_legacy svm 3dnowprefetch vmmcall -bugs : fxsave_leak sysret_ss_attrs null_seg swapgs_fence amd_e400 spectre_v1 spectre_v2 -bogomips : 1999.99 -TLB size : 1024 4K pages -clflush size : 64 -cache_alignment : 64 -address sizes : 40 bits physical, 48 bits virtual -power management: - -processor : 7 -vendor_id : AuthenticAMD -cpu family : 15 -model : 107 -model name : QEMU Virtual CPU version 2.5+ -stepping : 1 -microcode : 0x1000065 -cpu MHz : 999.997 -cache size : 512 KB -physical id : 1 -siblings : 4 -core id : 1 -cpu cores : 2 -apicid : 0 -initial apicid : 0 -fpu : yes -fpu_exception : yes -cpuid level : 13 -wp : yes -flags : fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx lm rep_good nopl cpuid extd_apicid pni cx16 hypervisor lahf_lm cmp_legacy svm 3dnowprefetch vmmcall -bugs : fxsave_leak sysret_ss_attrs null_seg swapgs_fence amd_e400 spectre_v1 spectre_v2 -bogomips : 1999.99 -TLB size : 1024 4K pages -clflush size : 64 -cache_alignment : 64 -address sizes : 40 bits physical, 48 bits virtual -power management: - diff --git a/internal/host/testdata/debian12/etc/os-release b/internal/host/testdata/debian12/etc/os-release deleted file mode 100644 index 3320862..0000000 --- a/internal/host/testdata/debian12/etc/os-release +++ /dev/null @@ -1,9 +0,0 @@ -PRETTY_NAME="Debian GNU/Linux 12 (bookworm)" -NAME="Debian GNU/Linux" -VERSION_ID="12" -VERSION="12 (bookworm)" -VERSION_CODENAME=bookworm -ID=debian -HOME_URL="https://www.debian.org/" -SUPPORT_URL="https://www.debian.org/support" -BUG_REPORT_URL="https://bugs.debian.org/" diff --git a/internal/host/testdata/debian12/proc/cpuinfo b/internal/host/testdata/debian12/proc/cpuinfo deleted file mode 100644 index 60660f2..0000000 --- a/internal/host/testdata/debian12/proc/cpuinfo +++ /dev/null @@ -1,56 +0,0 @@ -processor : 0 -vendor_id : AuthenticAMD -cpu family : 15 -model : 107 -model name : QEMU Virtual CPU version 2.5+ -stepping : 1 -microcode : 0x1000065 -cpu MHz : 999.997 -cache size : 512 KB -physical id : 0 -siblings : 2 -core id : 0 -cpu cores : 2 -apicid : 0 -initial apicid : 0 -fpu : yes -fpu_exception : yes -cpuid level : 13 -wp : yes -flags : fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx lm rep_good nopl cpuid extd_apicid pni cx16 hypervisor lahf_lm cmp_legacy svm 3dnowprefetch vmmcall -bugs : fxsave_leak sysret_ss_attrs null_seg swapgs_fence amd_e400 spectre_v1 spectre_v2 -bogomips : 1999.99 -TLB size : 1024 4K pages -clflush size : 64 -cache_alignment : 64 -address sizes : 40 bits physical, 48 bits virtual -power management: - -processor : 1 -vendor_id : AuthenticAMD -cpu family : 15 -model : 107 -model name : QEMU Virtual CPU version 2.5+ -stepping : 1 -microcode : 0x1000065 -cpu MHz : 999.997 -cache size : 512 KB -physical id : 0 -siblings : 2 -core id : 1 -cpu cores : 2 -apicid : 1 -initial apicid : 1 -fpu : yes -fpu_exception : yes -cpuid level : 13 -wp : yes -flags : fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx lm rep_good nopl cpuid extd_apicid pni cx16 hypervisor lahf_lm cmp_legacy svm 3dnowprefetch vmmcall -bugs : fxsave_leak sysret_ss_attrs null_seg swapgs_fence amd_e400 spectre_v1 spectre_v2 -bogomips : 2503.44 -TLB size : 1024 4K pages -clflush size : 64 -cache_alignment : 64 -address sizes : 40 bits physical, 48 bits virtual -power management: - diff --git a/internal/host/testdata/debian12/proc/loadavg b/internal/host/testdata/debian12/proc/loadavg deleted file mode 100644 index 2c12067..0000000 --- a/internal/host/testdata/debian12/proc/loadavg +++ /dev/null @@ -1 +0,0 @@ -0.08 0.03 0.04 1/144 6 diff --git a/internal/host/testdata/debian12/proc/meminfo b/internal/host/testdata/debian12/proc/meminfo deleted file mode 100644 index 52e8f5c..0000000 --- a/internal/host/testdata/debian12/proc/meminfo +++ /dev/null @@ -1,54 +0,0 @@ -MemTotal: 2010432 kB -MemFree: 690340 kB -MemAvailable: 1620016 kB -Buffers: 177720 kB -Cached: 837308 kB -SwapCached: 0 kB -Active: 409508 kB -Inactive: 681136 kB -Active(anon): 81680 kB -Inactive(anon): 72 kB -Active(file): 327828 kB -Inactive(file): 681064 kB -Unevictable: 11868 kB -Mlocked: 0 kB -SwapTotal: 0 kB -SwapFree: 0 kB -Zswap: 0 kB -Zswapped: 0 kB -Dirty: 568 kB -Writeback: 0 kB -AnonPages: 80808 kB -Mapped: 166636 kB -Shmem: 4936 kB -KReclaimable: 105056 kB -Slab: 159000 kB -SReclaimable: 105056 kB -SUnreclaim: 53944 kB -KernelStack: 2320 kB -PageTables: 2928 kB -SecPageTables: 0 kB -NFS_Unstable: 0 kB -Bounce: 0 kB -WritebackTmp: 0 kB -CommitLimit: 1005216 kB -Committed_AS: 496972 kB -VmallocTotal: 34359738367 kB -VmallocUsed: 17080 kB -VmallocChunk: 0 kB -Percpu: 1192 kB -HardwareCorrupted: 0 kB -AnonHugePages: 0 kB -ShmemHugePages: 0 kB -ShmemPmdMapped: 0 kB -FileHugePages: 0 kB -FilePmdMapped: 0 kB -Unaccepted: 0 kB -HugePages_Total: 0 -HugePages_Free: 0 -HugePages_Rsvd: 0 -HugePages_Surp: 0 -Hugepagesize: 2048 kB -Hugetlb: 0 kB -DirectMap4k: 112268 kB -DirectMap2M: 1980416 kB diff --git a/internal/host/testdata/debian12/proc/stat b/internal/host/testdata/debian12/proc/stat deleted file mode 100644 index 5e6dd88..0000000 --- a/internal/host/testdata/debian12/proc/stat +++ /dev/null @@ -1,10 +0,0 @@ -cpu 2014808 0 3797047 237290637 7608 0 66047 0 0 0 -cpu0 997273 0 1861281 118761074 3753 0 27566 0 0 0 -cpu1 1017534 0 1935766 118529563 3854 0 38481 0 0 0 -intr 191922858 235 9 0 0 1122 0 0 0 0 0 0 0 144 0 0 0 0 0 0 0 0 0 0 0 0 6 0 3 0 274341 277139 0 0 0 281571 324044 0 1603949 1543300 0 20236 0 1770807 0 21 0 0 0 0 0 0 0 0 0 5953816 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -ctxt 212551065 -btime 1792210880 -processes 2962640 -procs_running 1 -procs_blocked 0 -softirq 141699889 5 32984936 1057 2776040 0 0 74 54302609 3 51635165 diff --git a/internal/host/testdata/debian12/proc/sys/kernel/hostname b/internal/host/testdata/debian12/proc/sys/kernel/hostname deleted file mode 100644 index b457dec..0000000 --- a/internal/host/testdata/debian12/proc/sys/kernel/hostname +++ /dev/null @@ -1 +0,0 @@ -hv1 diff --git a/internal/host/testdata/debian12/proc/sys/kernel/osrelease b/internal/host/testdata/debian12/proc/sys/kernel/osrelease deleted file mode 100644 index aa9cb65..0000000 --- a/internal/host/testdata/debian12/proc/sys/kernel/osrelease +++ /dev/null @@ -1 +0,0 @@ -6.8.0-50-generic diff --git a/internal/host/testdata/debian12/proc/uptime b/internal/host/testdata/debian12/proc/uptime deleted file mode 100644 index db23e51..0000000 --- a/internal/host/testdata/debian12/proc/uptime +++ /dev/null @@ -1 +0,0 @@ -1243445.41 2372906.39 diff --git a/internal/host/testdata/rocky10/etc/os-release b/internal/host/testdata/rocky10/etc/os-release deleted file mode 100644 index da89a9f..0000000 --- a/internal/host/testdata/rocky10/etc/os-release +++ /dev/null @@ -1,20 +0,0 @@ -NAME="Rocky Linux" -VERSION="10.2 (Red Quartz)" -RELEASE_TYPE="stable" -ID="rocky" -ID_LIKE="rhel centos fedora" -VERSION_ID="10.2" -PLATFORM_ID="platform:el10" -PRETTY_NAME="Rocky Linux 10.2 (Red Quartz)" -ANSI_COLOR="0;32" -LOGO="fedora-logo-icon" -CPE_NAME="cpe:/o:rocky:rocky:10::baseos" -HOME_URL="https://rockylinux.org/" -VENDOR_NAME="RESF" -VENDOR_URL="https://resf.org/" -BUG_REPORT_URL="https://bugs.rockylinux.org/" -SUPPORT_END="2035-05-31" -ROCKY_SUPPORT_PRODUCT="Rocky-Linux-10" -ROCKY_SUPPORT_PRODUCT_VERSION="10.2" -REDHAT_SUPPORT_PRODUCT="Rocky Linux" -REDHAT_SUPPORT_PRODUCT_VERSION="10.2" diff --git a/internal/iptables/hostsvc.go b/internal/iptables/hostsvc.go new file mode 100644 index 0000000..1f9195a --- /dev/null +++ b/internal/iptables/hostsvc.go @@ -0,0 +1,60 @@ +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 +} diff --git a/internal/iptables/hostsvc_test.go b/internal/iptables/hostsvc_test.go new file mode 100644 index 0000000..76f7587 --- /dev/null +++ b/internal/iptables/hostsvc_test.go @@ -0,0 +1,48 @@ +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) + } +} diff --git a/internal/iptables/iptables.go b/internal/iptables/iptables.go new file mode 100644 index 0000000..5e39e3e --- /dev/null +++ b/internal/iptables/iptables.go @@ -0,0 +1,44 @@ +package iptables + +import ( + "fmt" + "os/exec" + + "git.g3e.fr/syonad/two/internal/metadata" +) + +func addRule(args ...string) error { + return exec.Command("iptables", append([]string{"-t", "nat", "-A"}, args...)...).Run() +} + +func deleteRule(args ...string) error { + return exec.Command("iptables", append([]string{"-t", "nat", "-D"}, args...)...).Run() +} + +func AddMetadataRedirect(vmIP, gatewayIP, metadataPort string) error { + if err := addRule("PREROUTING", + "-s", vmIP+"/32", + "-d", metadata.ServiceIP+"/32", + "-p", "tcp", "-m", "tcp", + "--dport", "80", + "-j", "DNAT", + "--to-destination", gatewayIP+":"+metadataPort, + ); err != nil { + return fmt.Errorf("iptables metadata redirect: %w", err) + } + return nil +} + +func DeleteMetadataRedirect(vmIP, gatewayIP, metadataPort string) error { + if err := deleteRule("PREROUTING", + "-s", vmIP+"/32", + "-d", metadata.ServiceIP+"/32", + "-p", "tcp", "-m", "tcp", + "--dport", "80", + "-j", "DNAT", + "--to-destination", gatewayIP+":"+metadataPort, + ); err != nil { + return fmt.Errorf("iptables delete metadata redirect: %w", err) + } + return nil +} diff --git a/internal/metadata/struct.go b/internal/metadata/struct.go index 7d2c340..d30e7ea 100644 --- a/internal/metadata/struct.go +++ b/internal/metadata/struct.go @@ -1,7 +1,7 @@ package metadata // ServiceIP is the well-known address guests use to reach the metadata server. -// Traffic to it is DNATed to the subnet gateway by internal/nft, and the +// Traffic to it is DNATed to the subnet gateway by internal/iptables, and the // route to it is advertised by internal/dhcp. const ServiceIP = "169.254.169.254" diff --git a/internal/nft/hostsvc.go b/internal/nft/hostsvc.go deleted file mode 100644 index 384b69e..0000000 --- a/internal/nft/hostsvc.go +++ /dev/null @@ -1,81 +0,0 @@ -package nft - -import ( - "fmt" - "strings" -) - -const HostSvcChain = "hostsvc" - -var hostSvc = natChain{"", "nat", "prerouting", "dstnat"} - -var hostSvcExcluded = []string{"br-public", "l3-*"} - -func hostSvcRules(target string) []string { - var rules []string - for _, iface := range hostSvcExcluded { - rules = append(rules, fmt.Sprintf("iifname %q return", iface)) - } - for _, p := range []struct{ proto, port string }{{"udp", "53"}, {"tcp", "53"}, {"udp", "123"}} { - rules = append(rules, fmt.Sprintf("fib daddr type local %s dport %s dnat to %s", p.proto, p.port, target)) - } - return rules -} - -func hostSvcScript(target string) string { - var b strings.Builder - fmt.Fprintf(&b, "add table ip %s\n", Table) - name := hostSvc.declare(&b, HostSvcChain) - for _, r := range hostSvcRules(target) { - fmt.Fprintf(&b, "add rule ip %s %s %s\n", Table, name, r) - } - return b.String() -} - -func SetHostSvcRedirect(target string) error { - if err := apply(hostSvcScript(target)); err != nil { - return fmt.Errorf("set dns and ntp redirect: %w", err) - } - return nil -} - -func HasHostSvcRedirect(target string) (bool, error) { - out, err := run([]string{"list", "chains", "ip"}, "") - if err != nil { - return false, err - } - if !hasChain(string(out), HostSvcChain) { - return false, nil - } - out, err = run([]string{"list", "chain", "ip", Table, HostSvcChain}, "") - if err != nil { - return false, err - } - return sameRules(chainRules(string(out)), append([]string{hostSvc.header()}, hostSvcRules(target)...)), nil -} - -func hasChain(listing, chain string) bool { - table := "" - for _, line := range strings.Split(listing, "\n") { - fields := strings.Fields(line) - switch { - case len(fields) == 4 && fields[0] == "table" && fields[1] == "ip": - table = fields[2] - case len(fields) == 3 && fields[0] == "chain" && table == Table && fields[1] == chain: - return true - } - } - return false -} - -func sameRules(have, want []string) bool { - if len(have) != len(want) { - return false - } - for i := range want { - if have[i] != want[i] { - return false - } - } - return true -} diff --git a/internal/nft/hostsvc_test.go b/internal/nft/hostsvc_test.go deleted file mode 100644 index af60234..0000000 --- a/internal/nft/hostsvc_test.go +++ /dev/null @@ -1,104 +0,0 @@ -package nft - -import ( - "strings" - "testing" -) - -const listedHostSvcChains = `table ip two { - chain snat-ng-t46 { - type nat hook postrouting priority srcnat; policy accept; - } - chain hostsvc { - type nat hook prerouting priority dstnat; policy accept; - } - chain md-t90-m1 { - type nat hook prerouting priority dstnat; policy accept; - } -} -` - -const listedHostSvcChain = `table ip two { - chain hostsvc { - type nat hook prerouting priority dstnat; policy accept; - iifname "br-public" return - iifname "l3-*" return - fib daddr type local udp dport 53 dnat to 169.254.169.253 - fib daddr type local tcp dport 53 dnat to 169.254.169.253 - fib daddr type local udp dport 123 dnat to 169.254.169.253 - } -} -` - -func TestHostSvcScript(t *testing.T) { - want := `add table ip two -add chain ip two hostsvc { type nat hook prerouting priority dstnat; policy accept; } -flush chain ip two hostsvc -add rule ip two hostsvc iifname "br-public" return -add rule ip two hostsvc iifname "l3-*" return -add rule ip two hostsvc fib daddr type local udp dport 53 dnat to 169.254.169.253 -add rule ip two hostsvc fib daddr type local tcp dport 53 dnat to 169.254.169.253 -add rule ip two hostsvc fib daddr type local udp dport 123 dnat to 169.254.169.253 -` - if got := hostSvcScript("169.254.169.253"); got != want { - t.Errorf("script :\n%s\nattendu :\n%s", got, want) - } -} - -func TestHostSvcRules_ExclusionsComeFirst(t *testing.T) { - rules := hostSvcRules("169.254.169.253") - if len(rules) != 5 { - t.Fatalf("%d règles, 2 exclusions et 3 redirections attendues : %v", len(rules), rules) - } - for i, iface := range []string{"br-public", "l3-*"} { - if want := `iifname "` + iface + `" return`; rules[i] != want { - t.Errorf("règle %d = %q, %q attendu : le segment public et le lien de la VRF n'atteignent jamais le relais", i, rules[i], want) - } - } -} - -func TestHostSvcRules_OnlyLocalDestinations(t *testing.T) { - for _, r := range hostSvcRules("169.254.169.253") { - if strings.Contains(r, "dnat") && !strings.HasPrefix(r, "fib daddr type local ") { - t.Errorf("%q : un résolveur extérieur ne doit pas être détourné", r) - } - } -} - -func TestHasChain(t *testing.T) { - if !hasChain(listedHostSvcChains, "hostsvc") { - t.Error("hostsvc non trouvée") - } - if hasChain(listedHostSvcChains, "md-t90") { - t.Error("md-t90 trouvée par préfixe") - } - if hasChain(strings.Replace(listedHostSvcChains, "table ip two", "table ip nat", 1), "hostsvc") { - t.Error("hostsvc trouvée dans une autre table") - } - if hasChain("", "hostsvc") { - t.Error("hostsvc trouvée sans table") - } -} - -func TestHostSvcListingMatches(t *testing.T) { - want := append([]string{hostSvc.header()}, hostSvcRules("169.254.169.253")...) - if !sameRules(chainRules(listedHostSvcChain), want) { - t.Errorf("affichage nft %v, %v attendu", chainRules(listedHostSvcChain), want) - } -} - -func TestHostSvcListingMismatches(t *testing.T) { - want := append([]string{hostSvc.header()}, hostSvcRules("169.254.169.253")...) - for label, listing := range map[string]string{ - "règle 123 absente": strings.Replace(listedHostSvcChain, "\t\tfib daddr type local udp dport 123 dnat to 169.254.169.253\n", "", 1), - "règle 123 sans fib": strings.Replace(listedHostSvcChain, "fib daddr type local udp dport 123", "udp dport 123", 1), - "autre cible": strings.Replace(listedHostSvcChain, "53 dnat to 169.254.169.253", "53 dnat to 10.0.0.1", 1), - "exclusion après dnat": strings.Replace(strings.Replace(listedHostSvcChain, "\t\tiifname \"br-public\" return\n", "", 1), "dport 123 dnat to 169.254.169.253\n", "dport 123 dnat to 169.254.169.253\n\t\tiifname \"br-public\" return\n", 1), - "chaîne sans crochet": strings.Replace(listedHostSvcChain, "\t\ttype nat hook prerouting priority dstnat; policy accept;\n", "", 1), - "crochet d'une autre priorité": strings.Replace(listedHostSvcChain, "priority dstnat", "priority srcnat", 1), - } { - if sameRules(chainRules(listing), want) { - t.Errorf("%s : chaîne jugée conforme", label) - } - } -} diff --git a/internal/nft/metadata.go b/internal/nft/metadata.go deleted file mode 100644 index 6d7ad00..0000000 --- a/internal/nft/metadata.go +++ /dev/null @@ -1,46 +0,0 @@ -package nft - -import ( - "fmt" - "strings" - - "git.g3e.fr/syonad/two/internal/metadata" -) - -var metadataChain = natChain{"md-", "nat", "prerouting", "dstnat"} - -func metadataRule(ip, target string) string { - return fmt.Sprintf("ip saddr %s ip daddr %s tcp dport 80 dnat to %s", ip, metadata.ServiceIP, target) -} - -func metadataSetScript(vm string, ips []string, target string) string { - var b strings.Builder - fmt.Fprintf(&b, "add table ip %s\n", Table) - name := metadataChain.declare(&b, vm) - for _, ip := range ips { - fmt.Fprintf(&b, "add rule ip %s %s %s\n", Table, name, metadataRule(ip, target)) - } - return b.String() -} - -func metadataDeleteScript(vm string) string { - var b strings.Builder - fmt.Fprintf(&b, "add table ip %s\n", Table) - name := metadataChain.declare(&b, vm) - fmt.Fprintf(&b, "delete chain ip %s %s\n", Table, name) - return b.String() -} - -func SetMetadataRedirect(vm string, ips []string, target string) error { - if err := apply(metadataSetScript(vm, ips, target)); err != nil { - return fmt.Errorf("set metadata redirect %s: %w", vm, err) - } - return nil -} - -func DeleteMetadataRedirect(vm string) error { - if err := apply(metadataDeleteScript(vm)); err != nil { - return fmt.Errorf("delete metadata redirect %s: %w", vm, err) - } - return nil -} diff --git a/internal/nft/metadata_test.go b/internal/nft/metadata_test.go deleted file mode 100644 index 1792461..0000000 --- a/internal/nft/metadata_test.go +++ /dev/null @@ -1,38 +0,0 @@ -package nft - -import ( - "strings" - "testing" -) - -func TestMetadataSetScript(t *testing.T) { - want := `add table ip two -add chain ip two md-t90-m1 { type nat hook prerouting priority dstnat; policy accept; } -flush chain ip two md-t90-m1 -add rule ip two md-t90-m1 ip saddr 10.90.1.10 ip daddr 169.254.169.254 tcp dport 80 dnat to 10.90.1.1:8001 -add rule ip two md-t90-m1 ip saddr 10.90.2.10 ip daddr 169.254.169.254 tcp dport 80 dnat to 10.90.1.1:8001 -` - if got := metadataSetScript("t90-m1", []string{"10.90.1.10", "10.90.2.10"}, "10.90.1.1:8001"); got != want { - t.Errorf("script :\n%s\nattendu :\n%s", got, want) - } -} - -func TestMetadataDeleteScript(t *testing.T) { - want := `add table ip two -add chain ip two md-t90-m1 { type nat hook prerouting priority dstnat; policy accept; } -flush chain ip two md-t90-m1 -delete chain ip two md-t90-m1 -` - if got := metadataDeleteScript("t90-m1"); got != want { - t.Errorf("script :\n%s\nattendu :\n%s", got, want) - } -} - -func TestMetadataScriptsStayInTheirChain(t *testing.T) { - script := metadataSetScript("t90-m1", []string{"10.90.1.10"}, "10.90.1.1:8001") + metadataDeleteScript("t90-m1") - for _, line := range strings.Split(strings.TrimSpace(script), "\n") { - if line != "add table ip two" && !strings.Contains(line, " ip two md-t90-m1") { - t.Errorf("ligne hors de la chaîne md-t90-m1 : %q", line) - } - } -} diff --git a/internal/nft/nat.go b/internal/nft/nat.go index 7c17096..308b804 100644 --- a/internal/nft/nat.go +++ b/internal/nft/nat.go @@ -23,13 +23,9 @@ var natChains = []natChain{ {"in-", "filter", "input", "filter"}, } -func (c natChain) header() string { - return fmt.Sprintf("type %s hook %s priority %s; policy accept;", c.kind, c.hook, c.priority) -} - func (c natChain) declare(b *strings.Builder, iface string) string { name := c.prefix + iface - fmt.Fprintf(b, "add chain ip %s %s { %s }\n", Table, name, c.header()) + fmt.Fprintf(b, "add chain ip %s %s { type %s hook %s priority %s; policy accept; }\n", Table, name, c.kind, c.hook, c.priority) fmt.Fprintf(b, "flush chain ip %s %s\n", Table, name) return name } diff --git a/internal/vm/create.go b/internal/vm/create.go index b6e6d45..83462b5 100644 --- a/internal/vm/create.go +++ b/internal/vm/create.go @@ -10,6 +10,7 @@ import ( configuration "git.g3e.fr/syonad/two/internal/config/agent" "git.g3e.fr/syonad/two/internal/dhcpbackend" + "git.g3e.fr/syonad/two/internal/iptables" "git.g3e.fr/syonad/two/internal/l3vni" "git.g3e.fr/syonad/two/internal/metadata" "git.g3e.fr/syonad/two/internal/netif" @@ -62,7 +63,12 @@ func StartVM(db *badger.DB, name string, cfg *configuration.Config) error { // métadonnées de l'interface primaire. Toutes les interfaces étant dans le // même VPC, donc le même netns, il est joignable depuis n'importe laquelle. if err := netns.Call(nic.vpcName, func() error { - return nft.SetMetadataRedirect(name, nicIPs(d.nics), nic.interfaceIP+":"+d.metadataPort) + for _, n := range d.nics { + if err := iptables.AddMetadataRedirect(n.ip, nic.interfaceIP, d.metadataPort); err != nil { + return fmt.Errorf("interface %d: %w", n.index, err) + } + } + return nil }); err != nil { return fmt.Errorf("add metadata redirect: %w", err) } @@ -180,14 +186,6 @@ func copyFile(src, dst string) error { return out.Sync() } -func nicIPs(nics []nicData) []string { - ips := make([]string, len(nics)) - for i, n := range nics { - ips[i] = n.ip - } - return ips -} - func nicMACs(nics []nicData) []string { macs := make([]string, len(nics)) for i, n := range nics { diff --git a/internal/vm/create_test.go b/internal/vm/create_test.go index 84dfd79..1ed9bd2 100644 --- a/internal/vm/create_test.go +++ b/internal/vm/create_test.go @@ -18,17 +18,3 @@ func TestNicMACs_KeepsTheOrderAndLowersTheCase(t *testing.T) { t.Errorf("nicMACs = %v, attendu %v", got, want) } } - -func TestNicIPs_EveryInterfaceGetsTheRedirect(t *testing.T) { - nics := []nicData{ - {index: 0, ip: "10.49.1.10"}, - {index: 1, ip: "10.49.2.10", primary: true}, - } - - got := nicIPs(nics) - - want := []string{"10.49.1.10", "10.49.2.10"} - if !reflect.DeepEqual(got, want) { - t.Errorf("nicIPs = %v, attendu %v", got, want) - } -} diff --git a/internal/vm/delete.go b/internal/vm/delete.go index a76db5d..597aed4 100644 --- a/internal/vm/delete.go +++ b/internal/vm/delete.go @@ -11,6 +11,7 @@ import ( configuration "git.g3e.fr/syonad/two/internal/config/agent" "git.g3e.fr/syonad/two/internal/dhcpbackend" + "git.g3e.fr/syonad/two/internal/iptables" "git.g3e.fr/syonad/two/internal/l3vni" "git.g3e.fr/syonad/two/internal/metadata" "git.g3e.fr/syonad/two/internal/netif" @@ -59,7 +60,12 @@ func StopVM(db *badger.DB, name string, cfg *configuration.Config) error { } if err := netns.Call(nic.vpcName, func() error { - return nft.DeleteMetadataRedirect(name) + for _, n := range d.nics { + if err := iptables.DeleteMetadataRedirect(n.ip, nic.interfaceIP, d.metadataPort); err != nil { + return fmt.Errorf("interface %d: %w", n.index, err) + } + } + return nil }); err != nil { return fmt.Errorf("delete metadata redirect: %w", err) } diff --git a/internal/vpc/hostsvc.go b/internal/vpc/hostsvc.go index b04e295..d6118e1 100644 --- a/internal/vpc/hostsvc.go +++ b/internal/vpc/hostsvc.go @@ -7,9 +7,9 @@ import ( 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/nft" ) const HostSvcLink = "hostsvc" @@ -34,7 +34,7 @@ func newSystemHostSvc() systemHostSvc { 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 nft.SetHostSvcRedirect(target) } +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) } diff --git a/internal/watchdog/check_hostsvc.go b/internal/watchdog/check_hostsvc.go index fa4fd4e..b0b7ec3 100644 --- a/internal/watchdog/check_hostsvc.go +++ b/internal/watchdog/check_hostsvc.go @@ -7,9 +7,9 @@ import ( 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/nft" "git.g3e.fr/syonad/two/internal/state" "git.g3e.fr/syonad/two/internal/vpc" "git.g3e.fr/syonad/two/internal/watchdog/notify" @@ -58,7 +58,7 @@ func (systemHostSvcProbe) Redirect(name string) (bool, error) { var ok bool err := netns.Call(name, func() error { var err error - ok, err = nft.HasHostSvcRedirect(hostsvc.ServiceIP) + ok, err = iptables.HasHostSvcRedirect(hostsvc.ServiceIP) return err }) return ok, err @@ -103,7 +103,7 @@ func CheckHostSvc(db *badger.DB, p hostSvcProbe, n notify.Notifier) error { 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 (nft chain ip %s %s)", hostsvc.ServiceIP, nft.Table, nft.HostSvcChain)) + n.Notify(kindVPC, name, fmt.Sprintf("dns and ntp redirection to %s missing or incomplete (chain %s)", hostsvc.ServiceIP, iptables.HostSvcChain)) } } return nil diff --git a/internal/watchdog/check_hostsvc_test.go b/internal/watchdog/check_hostsvc_test.go index 31df6d6..1b1bfe9 100644 --- a/internal/watchdog/check_hostsvc_test.go +++ b/internal/watchdog/check_hostsvc_test.go @@ -97,7 +97,7 @@ func TestCheckHostSvc_ReportsEachMissingPiece(t *testing.T) { 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 (nft chain ip two hostsvc)", + "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 { diff --git a/release_notes/0.3.0.md b/release_notes/0.3.0.md index e2a5593..59a3223 100644 --- a/release_notes/0.3.0.md +++ b/release_notes/0.3.0.md @@ -113,16 +113,6 @@ - Une IP flottante est refusée (409) sur une interface d'un subnet `public_ip`. Le watchdog contrôle les subnets `public_ip` comme les subnets `bridge` -**Connaissance de l'hôte (#39)** - -- `GET /register` : hostname, CPU (fabricant, modèle, sockets, cœurs, threads), RAM totale, OS - et noyau, lus au démarrage de l'agent (503 si la lecture a échoué) -- `GET /ping` : version de l'agent, démarrages de l'hôte et de l'agent (timestamps Unix), - uptime de l'hôte et load, mesurés toutes les `times.host_state_interval` secondes (10 par - défaut) et gardés en mémoire, avec l'heure de la mesure (`collected_at`) -- **L'agent connaît sa version** : injectée au build (tag de la release), affichée dans - `/ping` et dans la ligne `starting agent` du journal - ## Correctifs - **Chaque VRF de VPC porte une route `unreachable default` (#81).** Une recherche qui échouait @@ -174,13 +164,6 @@ `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 -- **Les redirections passent d'iptables à nftables (#90)** : celle des métadonnées (une chaîne - `md-` par VM) et celle du DNS et du NTP (chaîne `hostsvc`) sont posées dans la table - `ip two` du netns de chaque VPC. `bootstrap_kvm.sh` n'installe plus `iptables`, ne charge plus - `br_netfilter` et n'écrit plus `net.bridge.bridge-nf-call-iptables = 1`. L'agent ne reprend - pas les règles iptables des VM créées par la 0.2.0 : le vidage de l'hyperviseur demandé - ci-dessous les retire. Sur un hyperviseur déjà déployé, le module reste chargé jusqu'au - redémarrage (ou `modprobe -r br_netfilter`) - **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` diff --git a/scripts/bootstrap_kvm.sh b/scripts/bootstrap_kvm.sh index 52ddb83..575bb60 100755 --- a/scripts/bootstrap_kvm.sh +++ b/scripts/bootstrap_kvm.sh @@ -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 nfs-common jq curl" +KVM_PACKAGES="qemu-system-x86 qemu-utils ovmf dnsmasq nftables iptables nfs-common jq curl" kvm_packages () { local DRY_RUN="${1}" @@ -39,11 +39,15 @@ kvm_kernel () { info "kernel" - # Pas de br_netfilter : le filtrage des ponts est en nftables (famille - # bridge) et les DNAT vivent dans les netns, où l'agent coupe déjà - # bridge-nf-call-*. Chargé, le module ferait passer tout le trafic bridgé - # des VM par netfilter (bridge-nf-call-iptables vaut 1 par défaut). - run "${DRY_RUN}" "printf 'net.ipv4.ip_forward = 1\n' > /etc/sysctl.d/90-two.conf" + # modprobe AVANT sysctl : les clés net.bridge.* n'existent pas tant que + # br_netfilter n'est pas chargé, et sysctl --system échouerait. + run "${DRY_RUN}" "modprobe br_netfilter" + run "${DRY_RUN}" "echo br_netfilter > /etc/modules-load.d/two.conf" + + # bridge-nf-call-iptables est requis par le DNAT metadata (169.254.169.254, + # cf. internal/iptables) : sans lui, iptables ne voit pas le trafic bridgé + # des VMs. Contrepartie : tout le trafic inter-VM traverse les tables nat. + run "${DRY_RUN}" "printf 'net.ipv4.ip_forward = 1\nnet.bridge.bridge-nf-call-iptables = 1\n' > /etc/sysctl.d/90-two.conf" run "${DRY_RUN}" "sysctl --system >/dev/null" } diff --git a/test/e2e/build.sh b/test/e2e/build.sh index 749b25b..abba46f 100755 --- a/test/e2e/build.sh +++ b/test/e2e/build.sh @@ -30,24 +30,11 @@ clean () { done } -build_version () { - local commit changes - commit=$(git -C "${REPO_DIR}" rev-parse --short HEAD) || return 1 - changes=$(git -C "${REPO_DIR}" status --porcelain) || return 1 - if [ -n "${changes}" ]; then - echo "${commit}-dirty" - else - echo "${commit}" - fi -} - build () { - local binary unit script version - version=$(build_version) || return 1 - echo "== version ${version}" >&2 + local binary unit script for binary in ${BINARIES}; do echo "== ${binary}" >&2 - (cd "${REPO_DIR}" && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-X main.version=${version}" -o "${BIN_DIR}/${binary}" "./cmd/${binary}") || return 1 + (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 diff --git a/test/e2e/lib/fixture.sh b/test/e2e/lib/fixture.sh index 9a381c2..b160501 100644 --- a/test/e2e/lib/fixture.sh +++ b/test/e2e/lib/fixture.sh @@ -56,8 +56,7 @@ host_state () { 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 "## nftables ip"; nft list ruleset ip - echo "## nftables des netns"; local ns; for ns in $(ip netns list | awk '{print $1}' | sort); do echo "# ${ns}"; ip netns exec "${ns}" nft list ruleset; done + 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 echo "## fichiers"; find /run/two /etc/dnsmasq.d "${VOLUMES}" -mindepth 1 ! -type d 2>/dev/null | sort diff --git a/test/e2e/run_test.sh b/test/e2e/run_test.sh index 8371111..cb983cf 100755 --- a/test/e2e/run_test.sh +++ b/test/e2e/run_test.sh @@ -826,23 +826,6 @@ PY teardown } -test_build_version_is_the_commit_dirty_when_the_tree_changed () { - setup "build_version : commit court, suffixé -dirty si l'arbre a des modifications, échec hors dépôt" - local repo="${WORK}/repo" commit - git init -q "${repo}" && printf 'a\n' > "${repo}/f" && git -C "${repo}" add f \ - && git -C "${repo}" -c user.name=t -c user.email=t@t commit -q -m t || fail "dépôt de test" - commit=$(git -C "${repo}" rev-parse --short HEAD) - build_version_in () { bash -c ". '${DIR}/build.sh'; REPO_DIR='${1}'; build_version" 2>&1; } - [[ $(build_version_in "${repo}") == "${commit}" ]] || fail "arbre propre : $(build_version_in "${repo}")" - printf 'b\n' > "${repo}/f" - [[ $(build_version_in "${repo}") == "${commit}-dirty" ]] || fail "fichier modifié : $(build_version_in "${repo}")" - git -C "${repo}" checkout -q f && printf 'n\n' > "${repo}/nouveau" - [[ $(build_version_in "${repo}") == "${commit}-dirty" ]] || fail "fichier non suivi : $(build_version_in "${repo}")" - mkdir "${WORK}/hors-depot" - build_version_in "${WORK}/hors-depot" >/dev/null && fail "hors dépôt : réussite" - teardown -} - for T in $(declare -F | awk '{print $3}' | grep '^test_'); do "${T}" done diff --git a/test/e2e/suite/13-hote.sh b/test/e2e/suite/13-hote.sh deleted file mode 100644 index 4b7668b..0000000 --- a/test/e2e/suite/13-hote.sh +++ /dev/null @@ -1,44 +0,0 @@ -# fonctionnalité: connaissance de l'hôte — /register (hostname, CPU, RAM, OS, noyau) lu au démarrage, /ping (version, démarrages de l'hôte et de l'agent, uptime, load) mesuré périodiquement, lecture seule, redémarrage de l'agent distingué de celui de l'hôte -# hôtes: hv1 -# tickets: #39 - -on hv1 <<'NODE' -reg () { api_field register "${1}"; } -ping_field () { api_field ping "${1}"; } -lscpu_field () { lscpu | awk -F: -v k="${1}" '$1 == k {gsub(/ /, "", $2); print $2}'; } -os_field () { (. /etc/os-release && eval "echo \"\${${1}}\""); } -near () { awk -v a="${1}" -v b="${2}" -v d="${3}" 'BEGIN { exit !((a - b) <= d && (b - a) <= d) }' || { echo "${1} et ${2} à plus de ${3} s"; return 1; }; } -ping_ready () { local i; for i in $(seq 1 30); do [ "$(api_code GET ping)" = 200 ] && return 0; sleep 1; done; return 1; } - -check "/register : 200" expect_code 200 api_code GET register -check "/register : hostname = hostname" test "$(reg hostname)" = "$(hostname)" -check "/register : threads = nproc" test "$(reg cpu.threads)" = "$(nproc --all)" -check "/register : sockets = lscpu" test "$(reg cpu.sockets)" = "$(lscpu_field 'Socket(s)')" -check "/register : cœurs = sockets × cœurs par socket (lscpu)" test "$(reg cpu.cores)" = "$(( $(lscpu_field 'Socket(s)') * $(lscpu_field 'Core(s) per socket') ))" -check "/register : modèle = lscpu" test "$(reg cpu.model)" = "$(lscpu | sed -n 's/^Model name: *//p')" -check "/register : fabricant = lscpu" test "$(reg cpu.vendor)" = "$(lscpu | sed -n 's/^Vendor ID: *//p')" -check "/register : RAM = MemTotal" test "$(reg memory_bytes)" = "$(awk '/^MemTotal:/ {printf "%.0f\n", $2 * 1024}' /proc/meminfo)" -check "/register : noyau = uname -r" test "$(reg kernel)" = "$(uname -r)" -check "/register : OS = os-release (ID, VERSION_ID, PRETTY_NAME)" test "$(reg os.id)|$(reg os.version_id)|$(reg os.pretty_name)" = "$(os_field ID)|$(os_field VERSION_ID)|$(os_field PRETTY_NAME)" - -check "/ping : 200" expect_code 200 api_code GET ping -VERSION=$(ping_field agent_version) -info "version de l'agent : ${VERSION}" -check "/ping : version injectée au build (commit ou tag, pas undefined)" sh -c "echo '${VERSION}' | grep -Eq '^([0-9a-f]{7,}(-dirty)?|[0-9]+\.[0-9]+\.[0-9]+.*)$'" -check "/ping : boot_time = btime" test "$(ping_field boot_time)" = "$(awk '/^btime/ {print $2}' /proc/stat)" -check "/ping : boot_time + uptime_seconds = collected_at (horloge de l'hôte)" near "$(awk -v b="$(ping_field boot_time)" -v u="$(ping_field uptime_seconds)" 'BEGIN { printf "%d", b + u }')" "$(ping_field collected_at)" 2 -check "/ping : mesure de moins de 15 s" near "$(ping_field collected_at)" "$(date +%s)" 15 -check "/ping : trois valeurs de load" test "$(api_field ping 'load | length')" = 3 -check "/ping : agent_start_time = démarrage de l'unité agent" near "$(ping_field agent_start_time)" "$(date -d "$(systemctl show agent -p ActiveEnterTimestamp --value)" +%s)" 2 -for path in register ping; do - check "POST /${path} : 405" expect_code 405 api_code POST "${path}" -done - -BOOT=$(ping_field boot_time) -START=$(ping_field agent_start_time) -sleep 1 -check "redémarrage de l'agent" systemctl restart agent -check "/ping de nouveau servi après le redémarrage" ping_ready -check "après redémarrage : boot_time inchangé" test "$(ping_field boot_time)" = "${BOOT}" -check "après redémarrage : agent_start_time plus récent" test "$(ping_field agent_start_time)" -gt "${START}" -NODE diff --git a/test/e2e/suite/29-dns-ntp.sh b/test/e2e/suite/29-dns-ntp.sh index 941fe83..ebeac52 100644 --- a/test/e2e/suite/29-dns-ntp.sh +++ b/test/e2e/suite/29-dns-ntp.sh @@ -50,9 +50,9 @@ ntp_ok () { } 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}" nft list chain ip two hostsvc | sed -n '/^\t\t/s/^\t\t//p'; } +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' ' ')" = 'iifname "br-public" return iifname "l3-*" return ' ]; } +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 () { @@ -71,10 +71,9 @@ check "subnet sn-t29b en mode bridge sur br-lan, interface_ip 10.33.0.29" subnet 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 hostsvc de ip two accrochée à prerouting en dstnat" test "$(rules "${vpc}" | head -n 1)" = "type nat hook prerouting priority dstnat; policy accept;" - check "${vpc} : 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 169.254.169.253$')" -eq 3 - check "${vpc} : redirection pour les destinations locales seulement" test "$(rules "${vpc}" | grep -c '^fib daddr type local .* dnat to 169.254.169.253$')" -eq 3 + 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 @@ -99,11 +98,10 @@ 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 -HANDLE=$(ip netns exec vp-t29 nft -a list chain ip two hostsvc | awk '/udp dport 123 / {print $NF}') -check "règle 123 retirée à la main de hostsvc (handle ${HANDLE:-absent})" ip netns exec vp-t29 nft delete rule ip two hostsvc handle "${HANDLE}" -check "watchdog : redirection incomplète signalée" wait_watchdog "dns and ntp redirection to 169.254.169.253 missing or incomplete (nft chain ip two hostsvc)" +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 -check "règle 123 reposée" ip netns exec vp-t29 nft add rule ip two hostsvc fib daddr type local udp dport 123 dnat to 169.254.169.253 +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 diff --git a/test/e2e/suite/61-redemarrage-agent.sh b/test/e2e/suite/61-redemarrage-agent.sh index 93dcd01..f161674 100644 --- a/test/e2e/suite/61-redemarrage-agent.sh +++ b/test/e2e/suite/61-redemarrage-agent.sh @@ -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, ip et des netns, 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, nftables de pont, 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}')\" ]" diff --git a/test/e2e/suite/62-nettoyage-global.sh b/test/e2e/suite/62-nettoyage-global.sh index 02912aa..5bea382 100644 --- a/test/e2e/suite/62-nettoyage-global.sh +++ b/test/e2e/suite/62-nettoyage-global.sh @@ -18,7 +18,7 @@ check "${HOST} : aucune instance DHCP (dhcp@, dnsmasq@) ni de métadonnées (met 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 nftables ip qui vise une interface de subnet ou de VPC" none sh -c "nft list ruleset ip | grep -E -- '(v-|vp-|tap)[a-z0-9]'" +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'" check "${HOST} : FRR sans aucune VRF de VPC (en mémoire)" none sh -c "vtysh -c 'show running-config' | grep -E '^(vrf |router bgp [0-9]+ vrf )'"