Files
egutierrez 621e8895c9 feat(infra): auto-commit con 86 cambios
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-26 19:38:15 +02:00

2.9 KiB

name, kind, lang, domain, version, purity, signature, description, tags, params, output, uses_functions, uses_types, returns, returns_optional, error_type, imports, tested, tests, test_file_path, file_path
name kind lang domain version purity signature description tags params output uses_functions uses_types returns returns_optional error_type imports tested tests test_file_path file_path
wg_peer_add function go infra 1.0.0 impure func WGPeerAdd(spec WGPeerSpec, configPath, subnetCIDR string) (WGPeerResult, error) Hub-side: anade peer WireGuard al wg0.conf con IP asignada del pool, syncconf en caliente sin reiniciar interface. Idempotente por PublicKey + DeviceID. Mantiene comentario # DeviceID:<id> sobre cada bloque [Peer] para tracking inverso.
wireguard
hub
peer
mesh
infra
name desc
spec WGPeerSpec con DeviceID (identificador logico), PublicKey (base64), PresharedKey (base64, opcional), AllowedIPs (CIDR; vacio = autoasignar del pool)
name desc
configPath Ruta absoluta al wg0.conf del hub, ej /etc/wireguard/wg0.conf
name desc
subnetCIDR Subnet del pool de IPs WireGuard, ej '10.42.0.0/24'. La .1 se reserva para el hub y se excluye del pool.
WGPeerResult con DeviceID, AssignedIP (pura sin CIDR), ConfigPath y Status ('added'|'already-present'|'reconfigured')
wg_peer_spec_go_infra
wg_peer_result_go_infra
wg_peer_result_go_infra
false error_go_core
true
peer nuevo con AllowedIPs vacio asigna 10.42.0.2
agregar segundo peer asigna 10.42.0.3
agregar mismo PublicKey otra vez retorna already-present
agregar DeviceID existente con clave distinta retorna reconfigured
functions/infra/wg_peer_add_test.go functions/infra/wg_peer_add.go

Ejemplo

spec := infra.WGPeerSpec{
    DeviceID:  "pc-aurgi",
    PublicKey: "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB=",
}
res, err := infra.WGPeerAdd(spec, "/etc/wireguard/wg0.conf", "10.42.0.0/24")
// res.AssignedIP == "10.42.0.2"  (primera IP libre del pool)
// res.Status    == "added"

Cuando usarla

Cuando un dispositivo nuevo (PC, contenedor, mobile) se une al mesh WireGuard del hub. Llamar tras generar las claves con wg_keygen_go_infra. Idempotente: si el DeviceID ya existe con la misma clave, devuelve already-present sin tocar el config.

Gotchas

  • Race condition: si dos llamadas concurrentes añaden peers simultáneamente, la segunda puede asignar la misma IP libre. Usar file lock (flock) sobre configPath para serializar en produccion.
  • WG_SKIP_SYNCCONF=1: en entornos CI sin WireGuard instalado, establecer esta variable para saltarse el exec de wg syncconf. Los tests ya la activan en el init().
  • syncconf falla → rollback automático: si el wg syncconf devuelve error, se restaura el backup .bak y se devuelve error. El config queda intacto.
  • chmod 600: la función hace chmod 600 sobre configPath tras cada escritura. Asegúrate de que el proceso tiene permisos sobre el archivo.
  • Hub IP .1: la función excluye <red>.1 del pool de autoasignación. El hub debe tener esa IP. Si el hub usa otra IP, ajustar la lógica de wgNextFreeIP.