Compare commits

...

7 Commits

Author SHA1 Message Date
amery f4b3c3b341 build-sys: use local asciigoat.org/ini
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-07 18:03:22 +00:00
amery 67b2b9be29 vscode: add asciigoat, cyclomatic and Wrapf to the dictionary
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-07 18:03:22 +00:00
amery 3f6cf543d2 Merge branch 'pr-amery-validation' into next-amery
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-07 18:03:16 +00:00
amery 1fd67780b1 zones: ignore unknown wireguard endpoints
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-07 18:01:15 +00:00
amery 6c1e9d0989 zones: ErrUnknownNode and ErrInvalidNode
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-07 18:01:12 +00:00
amery 78110464b4 zones: ignore machine-less zones
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-07 18:01:05 +00:00
amery 1d980a9d6e zones: validate Machine names
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-07 17:59:42 +00:00
7 changed files with 67 additions and 18 deletions
+3
View File
@@ -1,9 +1,12 @@
{
"cSpell.words": [
"asciigoat",
"ceph",
"cyclomatic",
"darvaza",
"gofrs",
"jpictl",
"Wrapf",
"zerolog"
]
}
+2
View File
@@ -2,6 +2,8 @@ module git.jpi.io/amery/jpictl
go 1.19
replace asciigoat.org/ini => ../../../asciigoat.org/ini
require (
asciigoat.org/ini v0.2.5
darvaza.org/core v0.9.8
-2
View File
@@ -1,7 +1,5 @@
asciigoat.org/core v0.3.9 h1:hgDDz4ecm3ZvehX++m8A/IzAt+B5oDPiRtxatzfUHPQ=
asciigoat.org/core v0.3.9/go.mod h1:CAaHwyw8MpAq4a1MYtN2dxJrsK+hmIdW50OndaQZYPI=
asciigoat.org/ini v0.2.5 h1:4gRIp9rU+XQt8+HMqZO5R7GavMv9Yl2+N+je6djDIAE=
asciigoat.org/ini v0.2.5/go.mod h1:gmXzJ9XFqf1NLk5nQkj04USQ4tMtdRJHNQX6vp3DzjU=
darvaza.org/core v0.9.8 h1:luLxgfUc2pzuusYPo/Z/dC/qr9XZPKpSQw8/kS7zNUM=
darvaza.org/core v0.9.8/go.mod h1:Dbme64naxeshQfxcVJX9ZT7AiGyIY8kldfuELVtf8mw=
darvaza.org/resolver v0.5.4 h1:dlSBNV14yYsp7Kg7ipwYOMNsLbrpeXa8Z0HBTa0Ryxs=
+16
View File
@@ -0,0 +1,16 @@
package zones
import "errors"
var (
// ErrInvalidName indicates the name isn't valid
ErrInvalidName = errors.New("invalid name")
// ErrUnknownNode indicates there is a reference to a node
// we don't have on the tree
ErrUnknownNode = errors.New("node does not exist")
// ErrInvalidNode indicates the nodes can't be used for
// the intended purpose
ErrInvalidNode = errors.New("invalid node")
)
+8 -3
View File
@@ -2,6 +2,7 @@ package zones
import (
"bytes"
"errors"
"fmt"
"os"
@@ -135,7 +136,11 @@ func (m *Machine) applyWireguardConfig(ring int, wg *wireguard.Config) error {
}
for _, peer := range wg.Peer {
if err := m.applyWireguardPeerConfig(ring, peer); err != nil {
err := m.applyWireguardPeerConfig(ring, peer)
switch {
case errors.Is(err, ErrUnknownNode):
// ignore unknown peers
case err != nil:
err = core.Wrapf(err, "%s: wg%v:%s", m.Name, ring, addr)
return err
}
@@ -183,8 +188,10 @@ func (m *Machine) applyWireguardPeerConfig(ring int, pc wireguard.PeerConfig) er
switch {
case !found:
// unknown
return core.Wrap(ErrUnknownNode, pc.Endpoint.Host)
case ring == 1 && m.zone != peer.zone:
// invalid zone
return core.Wrap(ErrInvalidNode, peer.Name)
default:
// apply RingInfo
ri := &RingInfo{
@@ -197,8 +204,6 @@ func (m *Machine) applyWireguardPeerConfig(ring int, pc wireguard.PeerConfig) er
return peer.applyRingInfo(ring, ri)
}
return fmt.Errorf("%q: invalid peer endpoint", pc.Endpoint.Host)
}
func (m *Machine) applyZoneNodeID(zoneID, nodeID int) error {
+17 -3
View File
@@ -4,7 +4,10 @@ import (
"context"
"net/netip"
"strconv"
"strings"
"time"
"darvaza.org/core"
)
// LookupNetIP uses the DNS Resolver to get the public addresses associated
@@ -30,21 +33,32 @@ func (m *Machine) UpdatePublicAddresses() error {
func (m *Machine) init() error {
if err := m.setID(); err != nil {
return err
return core.Wrap(err, m.Name)
}
for i := 0; i < RingsCount; i++ {
if err := m.tryReadWireguardKeys(i); err != nil {
return err
return core.Wrap(err, m.Name)
}
}
return nil
}
func (m *Machine) setID() error {
zoneName := m.zone.Name
suffix := m.Name[len(zoneName)+1:]
l := len(zoneName)
switch {
case len(m.Name) < l+2:
return ErrInvalidName
case !strings.HasPrefix(m.Name, zoneName):
return ErrInvalidName
case m.Name[l] != '-':
return ErrInvalidName
}
suffix := m.Name[l+1:]
id, err := strconv.ParseInt(suffix, 10, 8)
if err != nil {
return err
+21 -10
View File
@@ -3,6 +3,8 @@ package zones
import (
"io/fs"
"sort"
"darvaza.org/core"
)
func (m *Zones) scan(opts *ScanOptions) error {
@@ -31,23 +33,32 @@ func (m *Zones) scanDirectory(_ *ScanOptions) error {
for _, e := range entries {
if e.IsDir() {
z := &Zone{
zones: m,
logger: m,
Name: e.Name(),
z, err := m.newZone(e.Name())
switch {
case err != nil:
return core.Wrap(err, e.Name())
case z.Machines.Len() > 0:
m.Zones = append(m.Zones, z)
}
if err := z.scan(); err != nil {
return err
}
m.Zones = append(m.Zones, z)
}
}
return nil
}
func (m *Zones) newZone(name string) (*Zone, error) {
z := &Zone{
zones: m,
logger: m,
Name: name,
}
if err := z.scan(); err != nil {
return nil, err
}
return z, nil
}
func (m *Zones) scanMachines(opts *ScanOptions) error {
var err error
m.ForEachMachine(func(p *Machine) bool {