Browse Source

zones: load wireguard key pairs on Machine.init()

Signed-off-by: Alejandro Mery <amery@jpi.io>
pull/1/head v0.3.1
Alejandro Mery 1 year ago
parent
commit
61374d4cc5
  1. 20
      pkg/zones/machine_rings.go
  2. 9
      pkg/zones/machine_scan.go
  3. 4
      pkg/zones/scan.go

20
pkg/zones/machine_rings.go

@ -61,6 +61,26 @@ func (m *Machine) GetWireguardKeys(ring int) (*wireguard.KeyPair, error) {
return kp, nil
}
func (m *Machine) tryReadWireguardKeys(ring int) error {
kp, err := m.GetWireguardKeys(ring)
switch {
case os.IsNotExist(err):
// ignore
return nil
case err != nil:
// something went wrong
return err
default:
// import keys
ri := &RingInfo{
Ring: ring,
Keys: kp,
}
return m.applyRingInfo(ring, ri)
}
}
// GetWireguardConfig reads a wgN.conf file
func (m *Machine) GetWireguardConfig(ring int) (*wireguard.Config, error) {
data, err := m.ReadFile("wg%v.conf", ring)

9
pkg/zones/machine_scan.go

@ -25,6 +25,15 @@ func (m *Machine) updatePublicAddresses() error {
return nil
}
func (m *Machine) init() error {
for i := 0; i < RingsCount; i++ {
if err := m.tryReadWireguardKeys(i); err != nil {
return err
}
}
return nil
}
func (m *Machine) scan() error {
for i := 0; i < RingsCount; i++ {
if err := m.tryApplyWireguardConfig(i); err != nil {

4
pkg/zones/scan.go

@ -129,6 +129,10 @@ func (z *Zone) scan() error {
Name: e.Name(),
}
if err := m.init(); err != nil {
return err
}
z.Machines = append(z.Machines, m)
}
}

Loading…
Cancel
Save