Compare commits

..

6 Commits

Author SHA1 Message Date
amery 2207e4a4a4 zones: fix New() to handle relative paths on hackpadfs
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 16:02:00 +00:00
amery 7ca01aa1e4 zones: Machine.RemoveWireguardConfig()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 15:36:58 +00:00
amery 8b72667f4d zones: Machine.RemoveWireguardKeys()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 15:20:07 +00:00
amery 49694eb7cb zones: Machine.WriteWireguardKeys()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 14:53:31 +00:00
amery 15a98c05ec zones: Machine.WriteStringFile()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 14:53:24 +00:00
amery a005823d44 zones: Machine.CreateFile() and Machine.CreateTruncFile()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 14:53:24 +00:00
5 changed files with 113 additions and 151 deletions
+17 -14
View File
@@ -10,21 +10,18 @@ import (
// WriteEnv generates environment variables for shell scripts
func (m *Zones) WriteEnv(w io.Writer) error {
var buf bytes.Buffer
var err error
m.writeEnvVarFn(&buf, genEnvZones, "ZONES")
m.ForEachZone(func(z *Zone) bool {
err = m.writeEnvZone(&buf, z)
return err != nil
m.writeEnvZone(&buf, z)
return false
})
if err == nil {
_, err = buf.WriteTo(w)
}
_, err := buf.WriteTo(w)
return err
}
func (m *Zones) writeEnvZone(w io.Writer, z *Zone) error {
func (m *Zones) writeEnvZone(w io.Writer, z *Zone) {
zoneID := z.ID
// ZONE{zoneID}
@@ -38,11 +35,8 @@ func (m *Zones) writeEnvZone(w io.Writer, z *Zone) error {
m.writeEnvVar(w, fmt.Sprintf("%v", gatewayID), "ZONE%v_%s", zoneID, "GW")
// ZONE{zoneID}_IP
if ip, ok := RingZeroAddress(zoneID, gatewayID); ok {
m.writeEnvVar(w, ip.String(), "ZONE%v_%s", zoneID, "IP")
}
return nil
ip, _ := RingZeroAddress(zoneID, gatewayID)
m.writeEnvVar(w, ip.String(), "ZONE%v_%s", zoneID, "IP")
}
func (m *Zones) writeEnvVarFn(w io.Writer, fn func(*Zones) string, name string, args ...any) {
@@ -85,9 +79,13 @@ func genEnvZoneNodes(z *Zone) string {
}
func getRingZeroGatewayID(z *Zone) int {
var gatewayID int
var firstNodeID, gatewayID int
z.ForEachMachine(func(p *Machine) bool {
if firstNodeID == 0 {
firstNodeID = p.ID
}
if p.IsGateway() {
gatewayID = p.ID
}
@@ -95,5 +93,10 @@ func getRingZeroGatewayID(z *Zone) int {
return gatewayID != 0
})
return gatewayID
switch {
case gatewayID == 0:
return firstNodeID
default:
return gatewayID
}
}
-23
View File
@@ -3,12 +3,10 @@ package zones
import (
"net/netip"
"strings"
"sync"
)
// A Machine is a machine on a Zone
type Machine struct {
mu sync.Mutex
zone *Zone
ID int
Name string `toml:"name"`
@@ -41,27 +39,6 @@ func (m *Machine) IsGateway() bool {
return ok
}
// SetGateway enables/disables a Machine ring0 integration
func (m *Machine) SetGateway(enabled bool) error {
m.mu.Lock()
defer m.mu.Unlock()
ri, found := m.getRingInfo(0)
switch {
case !found && !enabled:
return nil
case !found:
var err error
if ri, err = m.createRingInfo(0, false); err != nil {
return err
}
}
ri.Enabled = enabled
return m.syncRingConfig(0)
}
func (m *Machine) getPeerByName(name string) (*Machine, bool) {
return m.zone.zones.GetMachineByName(name)
}
+28 -4
View File
@@ -1,7 +1,9 @@
package zones
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
@@ -17,13 +19,22 @@ func (m *Machine) OpenFile(name string, flags int, args ...any) (fs.File, error)
}
// CreateTruncFile creates or truncates a file on the machine's config directory
func (m *Machine) CreateTruncFile(name string, args ...any) (fs.File, error) {
return m.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, args...)
func (m *Machine) CreateTruncFile(name string, args ...any) (io.WriteCloser, error) {
return m.openWriter(name, os.O_CREATE|os.O_TRUNC, args...)
}
// CreateFile creates a file on the machine's config directory
func (m *Machine) CreateFile(name string, args ...any) (fs.File, error) {
return m.OpenFile(name, os.O_RDWR|os.O_CREATE, args...)
func (m *Machine) CreateFile(name string, args ...any) (io.WriteCloser, error) {
return m.openWriter(name, os.O_CREATE, args...)
}
func (m *Machine) openWriter(name string, flags int, args ...any) (io.WriteCloser, error) {
f, err := m.OpenFile(name, os.O_WRONLY|flags, args...)
if err != nil {
return nil, err
}
return f.(io.WriteCloser), nil
}
// RemoveFile deletes a file from the machine's config directory
@@ -48,6 +59,19 @@ func (m *Machine) ReadFile(name string, args ...any) ([]byte, error) {
return fs.ReadFile(base, fullName)
}
// WriteStringFile writes the given content to a file on the machine's config directory
func (m *Machine) WriteStringFile(value string, name string, args ...any) error {
f, err := m.CreateTruncFile(name, args...)
if err != nil {
return err
}
defer f.Close()
buf := bytes.NewBufferString(value)
_, err = buf.WriteTo(f)
return err
}
func (m *Machine) getFilename(name string, args ...any) string {
if len(args) > 0 {
name = fmt.Sprintf(name, args...)
+61 -84
View File
@@ -73,6 +73,60 @@ func (m *Machine) tryReadWireguardKeys(ring int) error {
}
}
// WriteWireguardKeys writes the wgN.key/wgN.pub files
func (m *Machine) WriteWireguardKeys(ring int) error {
var err error
var key, pub string
var ri *RingInfo
ri, _ = m.getRingInfo(ring)
if ri != nil {
key = ri.Keys.PrivateKey.String()
pub = ri.Keys.PublicKey.String()
}
switch {
case key == "":
return fs.ErrNotExist
case pub == "":
pub = ri.Keys.PrivateKey.Public().String()
}
err = m.WriteStringFile(key, "wg%v.key", ring)
if err != nil {
return err
}
err = m.WriteStringFile(pub, "wg%v.pub", ring)
if err != nil {
return err
}
return nil
}
// RemoveWireguardKeys deletes wgN.key and wgN.pub from
// the machine's config directory
func (m *Machine) RemoveWireguardKeys(ring int) error {
var err error
err = m.RemoveFile("wg%v.pub", ring)
switch {
case os.IsNotExist(err):
// ignore
case err != nil:
return err
}
err = m.RemoveFile("wg%v.key", ring)
if os.IsNotExist(err) {
// ignore
err = nil
}
return err
}
// GetWireguardConfig reads a wgN.conf file
func (m *Machine) GetWireguardConfig(ring int) (*wireguard.Config, error) {
data, err := m.ReadFile("wg%v.conf", ring)
@@ -197,90 +251,13 @@ func (m *Machine) applyZoneNodeID(zoneID, nodeID int) error {
return nil
}
func (*Machine) syncRingConfig(_ int) error {
// _, err := m.getRingNodes(ring)
return nil
}
func (m *Machine) createRingInfo(ring int, enabled bool) (*RingInfo, error) {
keys, err := wireguard.NewKeyPair()
if err != nil {
return nil, err
}
ri := &RingInfo{
Ring: ring,
Enabled: enabled,
Keys: keys,
}
err = m.applyRingInfo(ring, ri)
if err != nil {
return nil, err
}
return ri, nil
}
func (m *Machine) writeRingInfo(ri *RingInfo) error {
var err error
if m == nil || ri == nil {
return fs.ErrInvalid
}
err = m.writeRingInfoPrivate(ri.Ring, ri.Keys.PrivateKey)
if err != nil {
return err
}
err = m.writeRingInfoPublic(ri.Ring, ri.Keys.PublicKey)
if err != nil {
return err
}
if !ri.Enabled {
return m.deleteRingInfoConf(ri.Ring)
}
return m.writeRingInfoConf(ri.Ring, ri.Keys.PrivateKey)
}
func (m *Machine) writeRingInfoPrivate(ring int, _ wireguard.PrivateKey) error {
f, err := m.CreateTruncFile("wg%v.key", ring)
if err != nil {
return err
}
defer f.Close()
return nil
}
func (m *Machine) writeRingInfoPublic(ring int, _ wireguard.PublicKey) error {
f, err := m.CreateTruncFile("wg%v.pub", ring)
if err != nil {
return err
}
defer f.Close()
return nil
}
func (m *Machine) writeRingInfoConf(ring int, _ wireguard.PrivateKey) error {
f, err := m.CreateTruncFile("wg%v.conf", ring)
if err != nil {
return err
}
defer f.Close()
return nil
}
func (m *Machine) deleteRingInfoConf(ring int) error {
// RemoveWireguardConfig deletes wgN.conf from the machine's
// config directory.
func (m *Machine) RemoveWireguardConfig(ring int) error {
err := m.RemoveFile("wg%v.conf", ring)
switch err {
case os.ErrNotExist:
return nil
default:
return err
if os.IsNotExist(err) {
err = nil
}
return err
}
+7 -26
View File
@@ -3,6 +3,7 @@ package zones
import (
"io/fs"
"path/filepath"
"github.com/hack-pad/hackpadfs/os"
@@ -33,31 +34,6 @@ func (z *Zone) ForEachMachine(fn func(*Machine) bool) {
}
}
// SetGateway configures a machine to be the zone's ring0 gateway
func (z *Zone) SetGateway(gatewayID int, enabled bool) error {
var err error
var found bool
z.ForEachMachine(func(p *Machine) bool {
if p.ID == gatewayID {
found = true
err = p.SetGateway(enabled)
return true
}
return false
})
switch {
case err != nil:
return err
case !found:
return fs.ErrNotExist
default:
return nil
}
}
// Zones represents all zones in a cluster
type Zones struct {
dir fs.FS
@@ -130,7 +106,12 @@ func NewFS(dir fs.FS, domain string) (*Zones, error) {
// New builds a [Zones] tree using the given directory
func New(dir, domain string) (*Zones, error) {
base, err := os.NewFS().Sub(dir)
dir, err := filepath.Abs(dir)
if err != nil {
return nil, err
}
base, err := os.NewFS().Sub(dir[1:])
if err != nil {
return nil, err
}