Procházet zdrojové kódy

fix(role): a host with virtual interfaces is not a switch (#2032)

Nacho před 2 týdny
rodič
revize
25da8de9b8

+ 21 - 2
server-go/internal/adapters/live.go

@@ -1652,7 +1652,10 @@ func (p *routerPolled) inventorySource() string {
 // reclamar clientes con la FDB (#1417). Aqui solo se decide el texto:
 //   - clientes inalambricos y sin WAN -> AP (un AP puente no enruta).
 //   - managed-switch o SNMP -> Switch (un switch de flota se anuncia asi).
-//   - pusher agent-only con 4 o mas bocas y sin radios -> Switch (crs305).
+//   - pusher agent-only con 4 o mas bocas fisicas, sin radios y sin
+//     interfaces que solo expone un host (docker0, sit0, bridges, VLAN) ->
+//     Switch (crs305). Un NAS con Docker anuncia diez interfaces y no es un
+//     chasis: los puertos no fisicos lo delatan (#1892).
 //   - external sin nada de lo anterior -> External (un NAS no es un switch).
 //   - el resto -> AP, que es el papel de las unidades OpenWrt de la flota.
 //
@@ -1679,7 +1682,7 @@ func displayRole(p *routerPolled, cfg RouterConfig, isGw bool) (string, string)
 	if cfg.Type == "managed-switch" || cfg.SnmpEnabled {
 		return "Switch", "SW"
 	}
-	if cfg.AgentOnly && !wireless && len(p.ports) >= 4 {
+	if cfg.AgentOnly && !wireless && len(p.ports) >= 4 && !hostLikePorts(p.ports) {
 		return "Switch", "SW"
 	}
 	if cfg.Type == "external" {
@@ -1688,6 +1691,22 @@ func displayRole(p *routerPolled, cfg RouterConfig, isGw bool) (string, string)
 	return "Punto de acceso", "AP"
 }
 
+// hostLikePorts dice si el payload anuncia interfaces que solo expone un host:
+// puentes de Docker, tuneles, VLAN, LAG o WiFi. Un chasis anuncia sus bocas
+// ethernet y nada mas, asi que ver una de estas significa que la unidad es una
+// maquina con varias NICs, no un switch (#1892). Familia vacia o "ethernet" es
+// boca fisica.
+func hostLikePorts(ports []EthPort) bool {
+	for _, port := range ports {
+		switch port.Family {
+		case "", "ethernet":
+		default:
+			return true
+		}
+	}
+	return false
+}
+
 func (l *Live) buildRouter(p *routerPolled, history []histPoint) Router {
 	l.mu.Lock()
 	gw := l.gatewayCfg

+ 30 - 3
server-go/internal/adapters/role_badge_1892_test.go

@@ -21,6 +21,17 @@ func portsOf(n int) []EthPort {
 	return out
 }
 
+// portsOfFamilies arma puertos con la familia que el agente les asigna. Vacio
+// es boca fisica, el resto (virtual, tunnel, bridge, vlan) solo lo expone un
+// host.
+func portsOfFamilies(families ...string) []EthPort {
+	out := make([]EthPort, 0, len(families))
+	for i, f := range families {
+		out = append(out, EthPort{ID: string(rune('a' + i)), Up: true, Family: f})
+	}
+	return out
+}
+
 func TestDisplayRoleReporterFleet(t *testing.T) {
 	cases := []struct {
 		name      string
@@ -52,9 +63,25 @@ func TestDisplayRoleReporterFleet(t *testing.T) {
 			wantRole: "Punto de acceso", wantBadge: "AP",
 		},
 		{
-			name:     "NAS with one wired client is not a switch",
-			cfg:      RouterConfig{ID: "ds1821plus", Type: "external", AgentOnly: true},
-			p:        &routerPolled{ports: portsOf(1)},
+			name: "NAS with one wired client is not a switch",
+			cfg:  RouterConfig{ID: "ds1821plus", Type: "external", AgentOnly: true},
+			p:    &routerPolled{ports: portsOf(1)},
+			// El fixture original contaba un puerto; el agente real anuncia
+			// diez y el badge salia SW (#1892, comentario del reportero).
+			wantRole: "External", wantBadge: "EXT",
+		},
+		{
+			name: "NAS with Docker: ten interfaces, four of them not physical",
+			cfg:  RouterConfig{ID: "ds1821plus", Type: "external", AgentOnly: true},
+			// wan, eth0, eth1, eth2, eth3 y eth5 fisicas; los puentes de
+			// Docker y el tunel sit son de host.
+			p:        &routerPolled{ports: portsOfFamilies("", "", "", "", "", "", "virtual", "virtual", "virtual", "tunnel")},
+			wantRole: "External", wantBadge: "EXT",
+		},
+		{
+			name:     "host with four NICs and one docker bridge is not a switch",
+			cfg:      RouterConfig{ID: "media-box", Type: "external", AgentOnly: true},
+			p:        &routerPolled{ports: portsOfFamilies("", "", "", "", "bridge")},
 			wantRole: "External", wantBadge: "EXT",
 		},
 		{