Compare commits

..

5 Commits

Author SHA1 Message Date
amery 09678d8eb8 WIP
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 22:15:54 +00:00
amery 3662fc7510 zones: change WriteEnv() to not fake gateways
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 22:15:54 +00:00
amery cade4b83bc zones: add Zone.SetGateway() to set one by ID
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 22:15:54 +00:00
amery 2bff61d6f2 zones: set first node of a Zone as ring0 gateway if it doesn't have one already
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 22:15:54 +00:00
amery b537a4438d wireguard: Config.WriteTo()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 22:15:52 +00:00
6 changed files with 67 additions and 271 deletions
+5 -10
View File
@@ -15,24 +15,19 @@ import (
)
var configTemplate = template.Must(template.New("config").Funcs(template.FuncMap{
"PrefixJoin": func(a []netip.Prefix, sep string) string {
s := make([]string, len(a))
for i, p := range a {
s[i] = p.String()
}
return strings.Join(s, sep)
},
"StringsJoin": strings.Join,
}).Parse(`[Interface]
Address = {{.Interface.Address}}
PrivateKey = {{.Interface.PrivateKey}}
ListenPort = {{.Interface.ListenPort}}
{{- range .Peer }}
{{range .Peer}}
[Peer]
# {{.Endpoint.Name}}
PublicKey = {{.PublicKey}}
Endpoint = {{.Endpoint}}
AllowedIPs = {{ PrefixJoin .AllowedIPs ", "}}
{{- end }}
AllowedIPs = {{ StringsJoin .AllowedIPs ", "}}
{{end}}
`))
// Config represents a wgN.conf file
-5
View File
@@ -61,11 +61,6 @@ func (m *Machine) SetGateway(enabled bool) error {
return m.SyncWireguardConfig(0)
}
// Zone indicates the [Zone] this machine belongs to
func (m *Machine) Zone() int {
return m.zone.ID
}
func (m *Machine) getPeerByName(name string) (*Machine, bool) {
return m.zone.zones.GetMachineByName(name)
}
+5 -6
View File
@@ -268,23 +268,22 @@ func (m *Machine) SyncWireguardConfig(ring int) error {
return m.zone.SyncWireguardConfig(ring)
}
// WriteWireguardConfig ...
func (m *Machine) WriteWireguardConfig(ring int) error {
r, err := NewRing(m.zone, ring)
r, err := m.zone.GetRing(ring)
if err != nil {
return err
}
return m.writeWireguardRingConfig(r)
return m.writeWireguardRing(r)
}
func (m *Machine) writeWireguardRingConfig(r *Ring) error {
wg, err := r.ExportConfig(m)
func (m *Machine) writeWireguardRing(r *Ring) error {
wg, err := r.ExportConfig(m.ID)
if err != nil {
return nil
}
f, err := m.CreateTruncFile("wg%v.conf", r.ID)
f, err := m.CreateTruncFile("wg%v.conf", r.Ring)
if err != nil {
return err
}
+1 -173
View File
@@ -2,7 +2,6 @@ package zones
import (
"fmt"
"io/fs"
"net/netip"
"git.jpi.io/amery/jpictl/pkg/wireguard"
@@ -176,178 +175,7 @@ func RingOneAddress(zoneID, nodeID int) (netip.Addr, bool) {
case !ValidZoneID(zoneID) || !ValidNodeID(nodeID):
return netip.Addr{}, false
default:
a4 := [4]uint8{10, uint8(zoneID << 4), 0, uint8(nodeID)}
a4 := [4]uint8{10, 0, uint8(zoneID << 4), uint8(nodeID)}
return netip.AddrFrom4(a4), true
}
}
var (
_ MachineIterator = (*Ring)(nil)
)
// A Ring describes all peers on a ring
type Ring struct {
RingAddressEncoder
Peers []*RingPeer
}
// AddPeer adds a [Machine] to the ring
func (r *Ring) AddPeer(p *Machine) bool {
_, ok := p.getRingInfo(r.ID)
if !ok {
return false
}
rp := r.newPeer(p)
r.Peers = append(r.Peers, rp)
return true
}
func (r *Ring) newPeer(p *Machine) *RingPeer {
nodeID := p.ID
zoneID := p.Zone()
ri, _ := p.getRingInfo(r.ID)
addr, _ := r.Encode(zoneID, nodeID)
rp := &RingPeer{
Node: p,
Address: addr,
PrivateKey: ri.Keys.PrivateKey,
PeerConfig: wireguard.PeerConfig{
PublicKey: ri.Keys.PublicKey,
Endpoint: wireguard.EndpointAddress{
Host: p.FullName(),
Port: r.Port,
},
},
}
if r.ID == 0 {
rp.AllowRingOneNetwork(zoneID)
rp.AllowIP(addr)
} else {
rp.AllowIP(addr)
if p.IsGateway() {
zones := p.zone.zones
rp.AllowOtherRingOneNetworks(zones, zoneID)
rp.AllowRingZeroGateways(zones)
}
}
return rp
}
// ForEachMachine calls a function for each Machine in the ring
// until instructed to terminate the loop
func (r *Ring) ForEachMachine(fn func(*Machine) bool) {
for _, pp := range r.Peers {
if fn(pp.Node) {
return
}
}
}
// ExportConfig ...
func (r *Ring) ExportConfig(p *Machine) (*wireguard.Config, error) {
cur, ok := r.getMachine(p)
if !ok {
return nil, fs.ErrNotExist
}
out := &wireguard.Config{
Interface: wireguard.InterfaceConfig{
Address: cur.Address,
PrivateKey: cur.PrivateKey,
ListenPort: r.Port,
},
}
for _, pp := range r.Peers {
if pp.Node != p {
pc := pp.PeerConfig
out.Peer = append(out.Peer, pc)
}
}
return out, nil
}
func (r *Ring) getMachine(p *Machine) (*RingPeer, bool) {
for _, pp := range r.Peers {
if pp.Node == p {
return pp, true
}
}
return nil, false
}
// A RingPeer is a node on a [Ring]
type RingPeer struct {
Node *Machine
Address netip.Addr
PrivateKey wireguard.PrivateKey
PeerConfig wireguard.PeerConfig
}
// AllowIP ...
func (rp *RingPeer) AllowIP(addr netip.Addr) {
rp.PeerConfig.AllowedIPs = append(rp.PeerConfig.AllowedIPs,
netip.PrefixFrom(addr, 32),
)
}
// AllowNetwork ...
func (rp *RingPeer) AllowNetwork(addr netip.Addr, bits int) {
rp.PeerConfig.AllowedIPs = append(rp.PeerConfig.AllowedIPs,
netip.PrefixFrom(addr, bits),
)
}
// AllowRingOneNetwork ...
func (rp *RingPeer) AllowRingOneNetwork(zoneID int) {
addr, _ := RingOneAddress(zoneID, 0)
rp.AllowNetwork(addr, 12)
}
// AllowOtherRingOneNetworks ...
func (rp *RingPeer) AllowOtherRingOneNetworks(m *Zones, zoneID int) {
m.ForEachZone(func(z *Zone) bool {
if z.ID != zoneID {
addr, _ := RingOneAddress(z.ID, 0)
rp.AllowNetwork(addr, 12)
}
return false
})
}
// AllowRingZeroGateways ...
func (rp *RingPeer) AllowRingZeroGateways(m *Zones) {
m.ForEachMachine(func(p *Machine) bool {
switch {
case !p.IsGateway():
// skip regular nodes
default:
addr, _ := RingZeroAddress(p.Zone(), p.ID)
rp.AllowIP(addr)
}
return false
})
}
// NewRing ...
func NewRing(m MachineIterator, ring int) (*Ring, error) {
r := &Ring{
RingAddressEncoder: Rings[ring],
}
m.ForEachMachine(func(p *Machine) bool {
r.AddPeer(p)
return false
})
return r, nil
}
+56 -67
View File
@@ -1,88 +1,39 @@
package zones
import (
"io/fs"
"os"
"git.jpi.io/amery/jpictl/pkg/wireguard"
)
var (
_ machineRinger = (*Zone)(nil)
_ machineRinger = (*Zones)(nil)
)
type Ring struct {
Ring int
}
type machineRinger interface {
MachineIterator
func (*Ring) ExportConfig(_ int) (*wireguard.Config, error) {
return nil, nil
}
PruneWireguardConfig(ring int) error
func (*Zone) GetRing(_ int) (*Ring, error) {
return &Ring{}, nil
}
// SyncWireguardConfig updates all wgN.conf files for the specified
// ring
func (z *Zone) SyncWireguardConfig(ring int) error {
switch ring {
case 0:
return syncWireguardConfig(z.zones, ring)
case 1:
return syncWireguardConfig(z, ring)
default:
return fs.ErrInvalid
}
}
// PruneWireguardConfig removes wgN.conf files of machines with
// the corresponding ring disabled.
func (z *Zone) PruneWireguardConfig(ring int) error {
return pruneWireguardConfig(z, ring)
}
// WriteWireguardKeys rewrites all wgN.{key,pub} files on this zone
func (z *Zone) WriteWireguardKeys(ring int) error {
return writeWireguardKeys(z, ring)
}
// SyncWireguardConfig updates all wgN.conf files for the specified
// ring
func (m *Zones) SyncWireguardConfig(ring int) error {
switch ring {
case 0:
return syncWireguardConfig(m, ring)
case 1:
var err error
m.ForEachZone(func(z *Zone) bool {
err = syncWireguardConfig(z, ring)
return err != nil
})
return err
default:
return fs.ErrInvalid
}
}
// PruneWireguardConfig removes wgN.conf files of machines with
// the corresponding ring disabled on all zones
func (m *Zones) PruneWireguardConfig(ring int) error {
return pruneWireguardConfig(m, ring)
}
// WriteWireguardKeys rewrites all wgN.{key,pub} files
func (m *Zones) WriteWireguardKeys(ring int) error {
return writeWireguardKeys(m, ring)
}
func syncWireguardConfig(m machineRinger, ring int) error {
err := m.PruneWireguardConfig(ring)
err := z.PruneWireguardConfig(ring)
if err != nil {
return err
}
r, err := NewRing(m, ring)
r, err := z.GetRing(ring)
if err != nil {
return err
}
m.ForEachMachine(func(p *Machine) bool {
z.ForEachMachine(func(p *Machine) bool {
if _, ok := p.getRingInfo(ring); ok {
err = p.writeWireguardRingConfig(r)
err = p.writeWireguardRing(r)
}
return err != nil
})
@@ -90,10 +41,12 @@ func syncWireguardConfig(m machineRinger, ring int) error {
return err
}
func pruneWireguardConfig(m MachineIterator, ring int) error {
// PruneWireguardConfig removes wgN.conf files of machines with
// the corresponding ring disabled.
func (z *Zone) PruneWireguardConfig(ring int) error {
var err error
m.ForEachMachine(func(p *Machine) bool {
z.ForEachMachine(func(p *Machine) bool {
_, ok := p.getRingInfo(ring)
if !ok {
err = p.RemoveWireguardConfig(ring)
@@ -104,10 +57,11 @@ func pruneWireguardConfig(m MachineIterator, ring int) error {
return err
}
func writeWireguardKeys(m MachineIterator, ring int) error {
// WriteWireguardKeys rewrites all wgN.{key,pub} files on this zone
func (z *Zone) WriteWireguardKeys(ring int) error {
var err error
m.ForEachMachine(func(p *Machine) bool {
z.ForEachMachine(func(p *Machine) bool {
err = p.WriteWireguardKeys(ring)
if os.IsNotExist(err) {
// ignore
@@ -119,3 +73,38 @@ func writeWireguardKeys(m MachineIterator, ring int) error {
return err
}
// PruneWireguardConfig removes wgN.conf files of machines with
// the corresponding ring disabled on all zones
func (m *Zones) PruneWireguardConfig(ring int) error {
var err error
m.ForEachZone(func(z *Zone) bool {
err = z.PruneWireguardConfig(ring)
return err != nil
})
return err
}
// WriteWireguardKeys rewrites all wgN.{key,pub} files
func (m *Zones) WriteWireguardKeys(ring int) error {
var err error
m.ForEachZone(func(z *Zone) bool {
err = z.WriteWireguardKeys(ring)
return err != nil
})
return err
}
// SyncWireguardConfig updates all wgN.conf files for the specified
// ring
func (m *Zones) SyncWireguardConfig(ring int) error {
err := m.PruneWireguardConfig(ring)
if err != nil {
return err
}
}
-10
View File
@@ -10,16 +10,6 @@ import (
"darvaza.org/resolver"
)
var (
_ MachineIterator = (*Zone)(nil)
_ MachineIterator = (*Zones)(nil)
)
// A MachineIterator is a set of Machines we can iterate on
type MachineIterator interface {
ForEachMachine(func(*Machine) bool)
}
// Zone represents one zone in a cluster
type Zone struct {
zones *Zones