Browse Source

zones: validate Machine names

Signed-off-by: Alejandro Mery <amery@jpi.io>
Alejandro Mery 1 year ago
parent
commit
1d980a9d6e
  1. 8
      pkg/zones/errors.go
  2. 20
      pkg/zones/machine_scan.go

8
pkg/zones/errors.go

@ -0,0 +1,8 @@
package zones
import "errors"
var (
// ErrInvalidName indicates the name isn't valid
ErrInvalidName = errors.New("invalid name")
)

20
pkg/zones/machine_scan.go

@ -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

Loading…
Cancel
Save