Compare commits

..

2 Commits

Author SHA1 Message Date
amery a454938bd7 WIP
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-24 21:48:00 +00:00
amery 568fd71d89 zones: Machine.CreateFile() and Machine.CreateTruncFile()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-24 21:14:33 +00:00
2 changed files with 40 additions and 95 deletions
+4 -28
View File
@@ -1,9 +1,7 @@
package zones package zones
import ( import (
"bytes"
"fmt" "fmt"
"io"
"os" "os"
"path/filepath" "path/filepath"
@@ -19,22 +17,13 @@ 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 // CreateTruncFile creates or truncates a file on the machine's config directory
func (m *Machine) CreateTruncFile(name string, args ...any) (io.WriteCloser, error) { func (m *Machine) CreateTruncFile(name string, args ...any) (fs.File, error) {
return m.openWriter(name, os.O_CREATE|os.O_TRUNC, args...) return m.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, args...)
} }
// CreateFile creates a file on the machine's config directory // CreateFile creates a file on the machine's config directory
func (m *Machine) CreateFile(name string, args ...any) (io.WriteCloser, error) { func (m *Machine) CreateFile(name string, args ...any) (fs.File, error) {
return m.openWriter(name, os.O_CREATE, args...) return m.OpenFile(name, os.O_RDWR|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 // RemoveFile deletes a file from the machine's config directory
@@ -59,19 +48,6 @@ func (m *Machine) ReadFile(name string, args ...any) ([]byte, error) {
return fs.ReadFile(base, fullName) 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 { func (m *Machine) getFilename(name string, args ...any) string {
if len(args) > 0 { if len(args) > 0 {
name = fmt.Sprintf(name, args...) name = fmt.Sprintf(name, args...)
+36 -67
View File
@@ -73,60 +73,6 @@ 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)
@@ -251,17 +197,6 @@ 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 { func (*Machine) syncRingConfig(_ int) error {
// _, err := m.getRingNodes(ring) // _, err := m.getRingNodes(ring)
return nil return nil
@@ -294,18 +229,41 @@ func (m *Machine) writeRingInfo(ri *RingInfo) error {
return fs.ErrInvalid return fs.ErrInvalid
} }
err = m.WriteWireguardKeys(ri.Ring) err = m.writeRingInfoPrivate(ri.Ring, ri.Keys.PrivateKey)
if err != nil {
return err
}
err = m.writeRingInfoPublic(ri.Ring, ri.Keys.PublicKey)
if err != nil { if err != nil {
return err return err
} }
if !ri.Enabled { if !ri.Enabled {
return m.RemoveWireguardConfig(ri.Ring) return m.deleteRingInfoConf(ri.Ring)
} }
return m.writeRingInfoConf(ri.Ring, ri.Keys.PrivateKey) 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 { func (m *Machine) writeRingInfoConf(ring int, _ wireguard.PrivateKey) error {
f, err := m.CreateTruncFile("wg%v.conf", ring) f, err := m.CreateTruncFile("wg%v.conf", ring)
if err != nil { if err != nil {
@@ -315,3 +273,14 @@ func (m *Machine) writeRingInfoConf(ring int, _ wireguard.PrivateKey) error {
return nil return nil
} }
func (m *Machine) deleteRingInfoConf(ring int) error {
err := m.RemoveFile("wg%v.conf", ring)
switch err {
case os.ErrNotExist:
return nil
default:
return err
}
}