Compare commits

...

12 Commits

Author SHA1 Message Date
amery 6a2cbdffd0 WIP
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 16:04:01 +00:00
amery 81aafbc083 zones: change WriteEnv() to not fake gateways
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 16:03:25 +00:00
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
amery 7af8484acc zones: introduce Machine.OpenFile()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-24 21:14:33 +00:00
amery 0f1f1ce968 zones: introduce Machine.RemoveFile()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-24 21:14:33 +00:00
amery 5058f286c6 zones: switch to using hackpadfs/os.FS as the standard os.FS is incomplete
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-24 21:14:33 +00:00
amery 86075eb47f zones: move Machine.ReadFile to a dedicated machine_file.go
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-24 21:14:31 +00:00
7 changed files with 276 additions and 42 deletions
+1
View File
@@ -8,6 +8,7 @@ require (
darvaza.org/sidecar v0.0.0-20230721122716-b9c54b8adbaf darvaza.org/sidecar v0.0.0-20230721122716-b9c54b8adbaf
darvaza.org/slog v0.5.2 darvaza.org/slog v0.5.2
github.com/burntSushi/toml v0.3.1 github.com/burntSushi/toml v0.3.1
github.com/hack-pad/hackpadfs v0.2.1
github.com/mgechev/revive v1.3.2 github.com/mgechev/revive v1.3.2
github.com/spf13/cobra v1.7.0 github.com/spf13/cobra v1.7.0
golang.org/x/crypto v0.12.0 golang.org/x/crypto v0.12.0
+2
View File
@@ -26,6 +26,8 @@ github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBD
github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4= github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94= github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/hack-pad/hackpadfs v0.2.1 h1:FelFhIhv26gyjujoA/yeFO+6YGlqzmc9la/6iKMIxMw=
github.com/hack-pad/hackpadfs v0.2.1/go.mod h1:khQBuCEwGXWakkmq8ZiFUvUZz84ZkJ2KNwKvChs4OrU=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
+9 -15
View File
@@ -32,11 +32,14 @@ func (m *Zones) writeEnvZone(w io.Writer, z *Zone) {
// ZONE{zoneID}_GW // ZONE{zoneID}_GW
gatewayID := getRingZeroGatewayID(z) gatewayID := getRingZeroGatewayID(z)
m.writeEnvVar(w, fmt.Sprintf("%v", gatewayID), "ZONE%v_%s", zoneID, "GW") if gatewayID > 0 {
m.writeEnvVar(w, fmt.Sprintf("%v", gatewayID), "ZONE%v_%s", zoneID, "GW")
// ZONE{zoneID}_IP // ZONE{zoneID}_IP
ip, _ := RingZeroAddress(zoneID, gatewayID) if ip, ok := RingZeroAddress(zoneID, gatewayID); ok {
m.writeEnvVar(w, ip.String(), "ZONE%v_%s", zoneID, "IP") 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) { func (m *Zones) writeEnvVarFn(w io.Writer, fn func(*Zones) string, name string, args ...any) {
@@ -79,13 +82,9 @@ func genEnvZoneNodes(z *Zone) string {
} }
func getRingZeroGatewayID(z *Zone) int { func getRingZeroGatewayID(z *Zone) int {
var firstNodeID, gatewayID int var gatewayID int
z.ForEachMachine(func(p *Machine) bool { z.ForEachMachine(func(p *Machine) bool {
if firstNodeID == 0 {
firstNodeID = p.ID
}
if p.IsGateway() { if p.IsGateway() {
gatewayID = p.ID gatewayID = p.ID
} }
@@ -93,10 +92,5 @@ func getRingZeroGatewayID(z *Zone) int {
return gatewayID != 0 return gatewayID != 0
}) })
switch { return gatewayID
case gatewayID == 0:
return firstNodeID
default:
return gatewayID
}
} }
+18 -25
View File
@@ -1,10 +1,7 @@
package zones package zones
import ( import (
"fmt"
"io/fs"
"net/netip" "net/netip"
"path/filepath"
"strings" "strings"
) )
@@ -36,34 +33,30 @@ func (m *Machine) FullName() string {
return m.Name return m.Name
} }
// ReadFile reads a file from the machine's config directory
func (m *Machine) ReadFile(name string, args ...any) ([]byte, error) {
base := m.zone.zones.dir
fullName := m.getFilename(name, args...)
return fs.ReadFile(base, fullName)
}
func (m *Machine) getFilename(name string, args ...any) string {
if len(args) > 0 {
name = fmt.Sprintf(name, args...)
}
s := []string{
m.zone.Name,
m.Name,
name,
}
return filepath.Join(s...)
}
// IsGateway tells if the Machine is a ring0 gateway // IsGateway tells if the Machine is a ring0 gateway
func (m *Machine) IsGateway() bool { func (m *Machine) IsGateway() bool {
_, ok := m.getRingInfo(0) _, ok := m.getRingInfo(0)
return ok return ok
} }
// SetGateway enables/disables a Machine ring0 integration
func (m *Machine) SetGateway(enabled bool) error {
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) { func (m *Machine) getPeerByName(name string) (*Machine, bool) {
return m.zone.zones.GetMachineByName(name) return m.zone.zones.GetMachineByName(name)
} }
+87
View File
@@ -0,0 +1,87 @@
package zones
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
fs "github.com/hack-pad/hackpadfs"
)
// OpenFile opens a file on the machine's config directory with the specified flags
func (m *Machine) OpenFile(name string, flags int, args ...any) (fs.File, error) {
base := m.zone.zones.dir
fullName := m.getFilename(name, args...)
return fs.OpenFile(base, fullName, flags, 0644)
}
// CreateTruncFile creates or truncates a file on the machine's config directory
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) (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
func (m *Machine) RemoveFile(name string, args ...any) error {
base := m.zone.zones.dir
fullName := m.getFilename(name, args...)
err := fs.Remove(base, fullName)
switch {
case os.IsNotExist(err):
return nil
default:
return err
}
}
// ReadFile reads a file from the machine's config directory
func (m *Machine) ReadFile(name string, args ...any) ([]byte, error) {
base := m.zone.zones.dir
fullName := m.getFilename(name, args...)
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...)
}
s := []string{
m.zone.Name,
m.Name,
name,
}
return filepath.Join(s...)
}
+120
View File
@@ -3,6 +3,7 @@ package zones
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io/fs"
"os" "os"
"darvaza.org/core" "darvaza.org/core"
@@ -72,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 // GetWireguardConfig reads a wgN.conf file
func (m *Machine) GetWireguardConfig(ring int) (*wireguard.Config, error) { func (m *Machine) GetWireguardConfig(ring int) (*wireguard.Config, error) {
data, err := m.ReadFile("wg%v.conf", ring) data, err := m.ReadFile("wg%v.conf", ring)
@@ -195,3 +250,68 @@ func (m *Machine) applyZoneNodeID(zoneID, nodeID int) error {
return nil return nil
} }
// RemoveWireguardConfig deletes wgN.conf from the machine's
// config directory.
func (m *Machine) RemoveWireguardConfig(ring int) error {
err := m.RemoveFile("wg%v.conf", ring)
if os.IsNotExist(err) {
err = nil
}
return err
}
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.WriteWireguardKeys(ri.Ring)
if err != nil {
return err
}
if !ri.Enabled {
return m.RemoveWireguardConfig(ri.Ring)
}
return m.writeRingInfoConf(ri.Ring, ri.Keys.PrivateKey)
}
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
}
+39 -2
View File
@@ -3,7 +3,9 @@ package zones
import ( import (
"io/fs" "io/fs"
"os" "path/filepath"
"github.com/hack-pad/hackpadfs/os"
"darvaza.org/resolver" "darvaza.org/resolver"
) )
@@ -32,6 +34,31 @@ 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 // Zones represents all zones in a cluster
type Zones struct { type Zones struct {
dir fs.FS dir fs.FS
@@ -104,5 +131,15 @@ func NewFS(dir fs.FS, domain string) (*Zones, error) {
// New builds a [Zones] tree using the given directory // New builds a [Zones] tree using the given directory
func New(dir, domain string) (*Zones, error) { func New(dir, domain string) (*Zones, error) {
return NewFS(os.DirFS(dir), domain) dir, err := filepath.Abs(dir)
if err != nil {
return nil, err
}
base, err := os.NewFS().Sub(dir[1:])
if err != nil {
return nil, err
}
return NewFS(base, domain)
} }