Compare commits

..

5 Commits

Author SHA1 Message Date
amery fdb0f0324f zones: finish scan sorting the content
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-22 21:29:12 +00:00
amery 9aef92f32d zones: assign zoneID to zones inferable ID
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-22 21:02:21 +00:00
amery e5baf53758 zones: import wireguard keys from wgN.conf files
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-22 20:37:10 +00:00
amery 0fe451eed0 zones: introduce RingInfo.Merge()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-22 20:32:29 +00:00
amery cb5ea80e66 zones: introduce Zones.GetMachineByName()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-22 20:32:26 +00:00
5 changed files with 236 additions and 1 deletions
+4
View File
@@ -82,3 +82,7 @@ func (m *Machine) getFilename(name string, args ...any) string {
return filepath.Join(s...)
}
func (m *Machine) getPeerByName(name string) (*Machine, bool) {
return m.zone.zones.GetMachineByName(name)
}
+68
View File
@@ -45,9 +45,77 @@ func (m *Machine) applyWireguardConfig(ring int, wg *wireguard.Config) error {
return err
}
if err := m.applyWireguardInterfaceConfig(ring, wg.Interface); err != nil {
err = core.Wrapf(err, "%s: wg%v:%s", m.Name, ring, addr)
return err
}
for _, peer := range wg.Peer {
if err := m.applyWireguardPeerConfig(ring, peer); err != nil {
err = core.Wrapf(err, "%s: wg%v:%s", m.Name, ring, addr)
return err
}
}
return nil
}
func (m *Machine) applyRingInfo(ring int, new *RingInfo) error {
var cur *RingInfo
for _, ri := range m.RingAddresses {
if ri.Ring == ring {
cur = ri
break
}
}
if cur == nil {
// first, append
m.RingAddresses = append(m.RingAddresses, new)
return nil
}
// extra, merge
return cur.Merge(new)
}
func (m *Machine) applyWireguardInterfaceConfig(ring int, data wireguard.InterfaceConfig) error {
ri := &RingInfo{
Ring: ring,
Enabled: true,
Address: data.Address,
Keys: &wireguard.KeyPair{
PrivateKey: data.PrivateKey,
},
}
return m.applyRingInfo(ring, ri)
}
func (m *Machine) applyWireguardPeerConfig(ring int, pc wireguard.PeerConfig) error {
peer, found := m.getPeerByName(pc.Endpoint.Name())
switch {
case !found:
// unknown
case ring == 1 && m.zone != peer.zone:
// invalid zone
default:
// apply RingInfo
ri := &RingInfo{
Ring: ring,
Enabled: true,
Keys: &wireguard.KeyPair{
PublicKey: pc.PublicKey,
},
}
return peer.applyRingInfo(ring, ri)
}
return fmt.Errorf("%q: invalid peer endpoint", pc.Endpoint.Host)
}
func (m *Machine) applyZoneNodeID(zoneID, nodeID int) error {
switch {
case zoneID == 0:
+68
View File
@@ -1,6 +1,7 @@
package zones
import (
"fmt"
"net/netip"
"git.jpi.io/amery/jpictl/pkg/wireguard"
@@ -24,6 +25,73 @@ type RingInfo struct {
Address netip.Addr `toml:"address,omitempty"`
}
// Merge attempts to combine two RingInfo structs
func (ri *RingInfo) Merge(alter *RingInfo) error {
switch {
case ri.Ring != alter.Ring:
// different ring
return fmt.Errorf("invalid %s: %v ≠ %v", "ring", ri.Ring, alter.Ring)
case ri.Enabled != alter.Enabled:
// different state
return fmt.Errorf("invalid %s: %v ≠ %v", "enabled", ri.Enabled, alter.Enabled)
case !canMergeAddress(ri.Address, alter.Address):
// different address
return fmt.Errorf("invalid %s: %v ≠ %v", "address", ri.Address, alter.Address)
case !canMergeKeyPairs(ri.Keys, alter.Keys):
// incompatible keypairs
return fmt.Errorf("invalid %s: %s ≠ %s", "keys", ri.Keys, alter.Keys)
}
switch {
case ri.Keys == nil:
// assign keypair
ri.Keys = alter.Keys
case alter.Keys != nil:
// fill the gaps on our keypair
if ri.Keys.PrivateKey.IsZero() {
ri.Keys.PrivateKey = alter.Keys.PrivateKey
}
if ri.Keys.PublicKey.IsZero() {
ri.Keys.PublicKey = alter.Keys.PublicKey
}
}
if addressEqual(ri.Address, netip.Addr{}) {
// assign address
ri.Address = alter.Address
}
return nil
}
func canMergeAddress(ip1, ip2 netip.Addr) bool {
var zero netip.Addr
switch {
case addressEqual(ip1, zero) || addressEqual(ip2, zero) || addressEqual(ip1, ip2):
return true
default:
return false
}
}
func addressEqual(ip1, ip2 netip.Addr) bool {
return ip1.Compare(ip2) == 0
}
func canMergeKeyPairs(p1, p2 *wireguard.KeyPair) bool {
switch {
case p1 == nil || p2 == nil:
return true
case !p1.PrivateKey.IsZero() && !p2.PrivateKey.IsZero() && !p1.PrivateKey.Equal(p2.PrivateKey):
return false
case !p1.PublicKey.IsZero() && !p2.PublicKey.IsZero() && !p1.PublicKey.Equal(p2.PublicKey):
return false
default:
return true
}
}
// RingAddressEncoder provides encoder/decoder access for a particular
// Wireguard ring
type RingAddressEncoder struct {
+78 -1
View File
@@ -2,9 +2,25 @@ package zones
import (
"io/fs"
"sort"
)
func (m *Zones) scan() error {
for _, fn := range []func() error{
m.scanDirectory,
m.scanMachines,
m.scanZoneIDs,
m.scanSort,
} {
if err := fn(); err != nil {
return err
}
}
return nil
}
func (m *Zones) scanDirectory() error {
// each directory is a zone
entries, err := fs.ReadDir(m.dir, ".")
if err != nil {
@@ -26,7 +42,7 @@ func (m *Zones) scan() error {
}
}
return m.scanMachines()
return nil
}
func (m *Zones) scanMachines() error {
@@ -38,6 +54,67 @@ func (m *Zones) scanMachines() error {
return err
}
func (m *Zones) scanZoneIDs() error {
var hasMissing bool
var lastZoneID int
m.ForEachZone(func(z *Zone) bool {
switch {
case z.ID == 0:
hasMissing = true
case z.ID > lastZoneID:
lastZoneID = z.ID
}
return false
})
if hasMissing {
next := lastZoneID + 1
m.ForEachZone(func(z *Zone) bool {
if z.ID == 0 {
z.ID, next = next, next+1
}
return false
})
}
return nil
}
func (m *Zones) scanSort() error {
sort.SliceStable(m.Zones, func(i, j int) bool {
id1 := m.Zones[i].ID
id2 := m.Zones[j].ID
return id1 < id2
})
m.ForEachZone(func(z *Zone) bool {
sort.SliceStable(z.Machines, func(i, j int) bool {
id1 := z.Machines[i].ID()
id2 := z.Machines[j].ID()
return id1 < id2
})
return false
})
m.ForEachMachine(func(p *Machine) bool {
sort.SliceStable(p.RingAddresses, func(i, j int) bool {
ri1 := p.RingAddresses[i]
ri2 := p.RingAddresses[j]
return ri1.Ring < ri2.Ring
})
return false
})
return nil
}
func (z *Zone) scan() error {
// each directory is a machine
entries, err := fs.ReadDir(z.zones.dir, z.Name)
+18
View File
@@ -53,6 +53,24 @@ func (m *Zones) ForEachZone(fn func(*Zone) bool) {
}
}
// GetMachineByName looks for a machine with the specified
// name on any zone
func (m *Zones) GetMachineByName(name string) (*Machine, bool) {
var out *Machine
if name != "" {
m.ForEachMachine(func(p *Machine) bool {
if p.Name == name {
out = p
}
return out != nil
})
}
return out, out != nil
}
// NewFS builds a [Zones] tree using the given directory
func NewFS(dir fs.FS, domain string) (*Zones, error) {
lockuper := resolver.NewCloudflareLookuper()