Compare commits

..

8 Commits

Author SHA1 Message Date
amery c421dd5ca8 WIP
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-26 16:09:48 +01:00
amery 107b0d5ea5 zones: change WriteEnv() to not fake gateways
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-26 03:33:39 +00:00
amery fa58cf8d43 zones: add Zone.SetGateway() to set one by ID
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-26 03:33:39 +00:00
amery c10dc4545d 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-26 03:33:39 +00:00
amery 7dd3ea8f96 zones: Machine.Zone()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-26 03:33:33 +00:00
amery 07b4a22752 zones: introduce MachineIterator interface
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-26 03:33:33 +00:00
amery 609f48a2d1 wireguard: Config.WriteTo()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-26 03:33:18 +00:00
amery d1f7d225ae zones: fix RingOneAddress()'s generated address
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-26 03:10:37 +00:00
6 changed files with 271 additions and 67 deletions
+10 -5
View File
@@ -15,19 +15,24 @@ import (
)
var configTemplate = template.Must(template.New("config").Funcs(template.FuncMap{
"StringsJoin": strings.Join,
"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)
},
}).Parse(`[Interface]
Address = {{.Interface.Address}}
PrivateKey = {{.Interface.PrivateKey}}
ListenPort = {{.Interface.ListenPort}}
{{range .Peer}}
{{- range .Peer }}
[Peer]
# {{.Endpoint.Name}}
PublicKey = {{.PublicKey}}
Endpoint = {{.Endpoint}}
AllowedIPs = {{ StringsJoin .AllowedIPs ", "}}
{{end}}
AllowedIPs = {{ PrefixJoin .AllowedIPs ", "}}
{{- end }}
`))
// Config represents a wgN.conf file
+5
View File
@@ -61,6 +61,11 @@ 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)
}
+6 -5
View File
@@ -268,22 +268,23 @@ func (m *Machine) SyncWireguardConfig(ring int) error {
return m.zone.SyncWireguardConfig(ring)
}
// WriteWireguardConfig ...
func (m *Machine) WriteWireguardConfig(ring int) error {
r, err := m.zone.GetRing(ring)
r, err := NewRing(m.zone, ring)
if err != nil {
return err
}
return m.writeWireguardRing(r)
return m.writeWireguardRingConfig(r)
}
func (m *Machine) writeWireguardRing(r *Ring) error {
wg, err := r.ExportConfig(m.ID)
func (m *Machine) writeWireguardRingConfig(r *Ring) error {
wg, err := r.ExportConfig(m)
if err != nil {
return nil
}
f, err := m.CreateTruncFile("wg%v.conf", r.Ring)
f, err := m.CreateTruncFile("wg%v.conf", r.ID)
if err != nil {
return err
}
+173 -1
View File
@@ -2,6 +2,7 @@ package zones
import (
"fmt"
"io/fs"
"net/netip"
"git.jpi.io/amery/jpictl/pkg/wireguard"
@@ -175,7 +176,178 @@ func RingOneAddress(zoneID, nodeID int) (netip.Addr, bool) {
case !ValidZoneID(zoneID) || !ValidNodeID(nodeID):
return netip.Addr{}, false
default:
a4 := [4]uint8{10, 0, uint8(zoneID << 4), uint8(nodeID)}
a4 := [4]uint8{10, uint8(zoneID << 4), 0, 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
}
+67 -56
View File
@@ -1,39 +1,88 @@
package zones
import (
"io/fs"
"os"
"git.jpi.io/amery/jpictl/pkg/wireguard"
)
type Ring struct {
Ring int
}
var (
_ machineRinger = (*Zone)(nil)
_ machineRinger = (*Zones)(nil)
)
func (*Ring) ExportConfig(_ int) (*wireguard.Config, error) {
return nil, nil
}
type machineRinger interface {
MachineIterator
func (*Zone) GetRing(_ int) (*Ring, error) {
return &Ring{}, nil
PruneWireguardConfig(ring int) error
}
// SyncWireguardConfig updates all wgN.conf files for the specified
// ring
func (z *Zone) SyncWireguardConfig(ring int) error {
err := z.PruneWireguardConfig(ring)
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)
if err != nil {
return err
}
r, err := z.GetRing(ring)
r, err := NewRing(m, ring)
if err != nil {
return err
}
z.ForEachMachine(func(p *Machine) bool {
m.ForEachMachine(func(p *Machine) bool {
if _, ok := p.getRingInfo(ring); ok {
err = p.writeWireguardRing(r)
err = p.writeWireguardRingConfig(r)
}
return err != nil
})
@@ -41,12 +90,10 @@ func (z *Zone) SyncWireguardConfig(ring int) error {
return err
}
// PruneWireguardConfig removes wgN.conf files of machines with
// the corresponding ring disabled.
func (z *Zone) PruneWireguardConfig(ring int) error {
func pruneWireguardConfig(m MachineIterator, ring int) error {
var err error
z.ForEachMachine(func(p *Machine) bool {
m.ForEachMachine(func(p *Machine) bool {
_, ok := p.getRingInfo(ring)
if !ok {
err = p.RemoveWireguardConfig(ring)
@@ -57,11 +104,10 @@ func (z *Zone) PruneWireguardConfig(ring int) error {
return err
}
// WriteWireguardKeys rewrites all wgN.{key,pub} files on this zone
func (z *Zone) WriteWireguardKeys(ring int) error {
func writeWireguardKeys(m MachineIterator, ring int) error {
var err error
z.ForEachMachine(func(p *Machine) bool {
m.ForEachMachine(func(p *Machine) bool {
err = p.WriteWireguardKeys(ring)
if os.IsNotExist(err) {
// ignore
@@ -73,38 +119,3 @@ func (z *Zone) WriteWireguardKeys(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,6 +10,16 @@ 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