Compare commits

..

5 Commits

Author SHA1 Message Date
amery 4aee69385d zones: RingInfo [WIP]
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-22 15:57:18 +00:00
amery 6b9ad95c36 wireguard: introduce initial BinaryKey and KeyPair structs
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-22 15:57:18 +00:00
amery a38aee1219 zones: WIP
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-22 15:57:18 +00:00
amery 24f657224c jpictl: initial env command [WIP]
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-22 15:57:18 +00:00
amery d813175125 zones: WriteEnv() [WIP]
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-22 15:57:18 +00:00
10 changed files with 139 additions and 102 deletions
+27
View File
@@ -0,0 +1,27 @@
package main
import (
"os"
"github.com/spf13/cobra"
"git.jpi.io/amery/jpictl/pkg/zones"
)
// Command
var envCmd = &cobra.Command{
Use: "env",
Short: "generates environment variables for shell scripts",
RunE: func(_ *cobra.Command, _ []string) error {
m, err := zones.New(cfg.Base, cfg.Domain)
if err != nil {
return err
}
return m.WriteEnv(os.Stdout)
},
}
func init() {
rootCmd.AddCommand(envCmd)
}
+8 -6
View File
@@ -1,6 +1,7 @@
package wireguard package wireguard
import ( import (
"encoding/base64"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@@ -31,13 +32,13 @@ func (f *Config) Peers() int {
// InterfaceConfig represents the [Interface] section // InterfaceConfig represents the [Interface] section
type InterfaceConfig struct { type InterfaceConfig struct {
Address netip.Addr Address netip.Addr
PrivateKey BinaryKey PrivateKey []byte
ListenPort uint16 ListenPort uint16
} }
// PeerConfig represents a [Peer] section // PeerConfig represents a [Peer] section
type PeerConfig struct { type PeerConfig struct {
PublicKey BinaryKey PublicKey []byte
Endpoint EndpointAddress Endpoint EndpointAddress
AllowedIPs []netip.Prefix AllowedIPs []netip.Prefix
} }
@@ -128,19 +129,19 @@ type interfaceConfig struct {
} }
func (p interfaceConfig) Export() (InterfaceConfig, error) { func (p interfaceConfig) Export() (InterfaceConfig, error) {
var err error
out := InterfaceConfig{ out := InterfaceConfig{
Address: p.Address, Address: p.Address,
ListenPort: p.ListenPort, ListenPort: p.ListenPort,
} }
out.PrivateKey, err = BinaryKeyFromBase64(p.PrivateKey) b, err := base64.StdEncoding.DecodeString(p.PrivateKey)
if err != nil { if err != nil {
err = core.Wrap(err, "PrivateKey") err = core.Wrap(err, "PrivateKey")
return InterfaceConfig{}, err return InterfaceConfig{}, err
} }
out.PrivateKey = b
return out, nil return out, nil
} }
@@ -162,7 +163,8 @@ func (v *intermediateConfig) ExportPeer(i int) (PeerConfig, error) {
} }
// PublicKey // PublicKey
out.PublicKey, err = BinaryKeyFromBase64(v.Peer.PublicKey[i]) s = v.Peer.PublicKey[i]
out.PublicKey, err = base64.StdEncoding.DecodeString(s)
if err != nil { if err != nil {
err = core.Wrap(err, "PublicKey") err = core.Wrap(err, "PublicKey")
return out, err return out, err
-6
View File
@@ -1,7 +1,6 @@
package wireguard package wireguard
import ( import (
"bytes"
"encoding/base64" "encoding/base64"
) )
@@ -17,11 +16,6 @@ func (k BinaryKey) IsZero() bool {
return len(k) == 0 return len(k) == 0
} }
// Equal checks if two keys are identical
func (k BinaryKey) Equal(alter BinaryKey) bool {
return bytes.Equal(k, alter)
}
// BinaryKeyFromBase64 decodes a base64-based string into // BinaryKeyFromBase64 decodes a base64-based string into
// a [BinaryKey] // a [BinaryKey]
func BinaryKeyFromBase64(data string) (BinaryKey, error) { func BinaryKeyFromBase64(data string) (BinaryKey, error) {
+9
View File
@@ -0,0 +1,9 @@
// Package zones abstracts the cluster zones
package zones
import "io"
// WriteEnv generates environment variables for shell scripts
func (*Zones) WriteEnv(io.Writer) error {
return nil
}
+2 -3
View File
@@ -16,10 +16,9 @@ type Machine struct {
zone *Zone zone *Zone
id int id int
Name string `toml:"name"` Name string
PublicAddresses []netip.Addr `toml:"public,omitempty"` PublicAddresses []netip.Addr
RingAddresses []*RingInfo `toml:"rings,omitempty"`
} }
func (m *Machine) String() string { func (m *Machine) String() string {
-66
View File
@@ -1,66 +0,0 @@
package zones
import (
"bytes"
"fmt"
"os"
"darvaza.org/core"
"git.jpi.io/amery/jpictl/pkg/wireguard"
)
// GetWireguardConfig reads a wgN.conf file
func (m *Machine) GetWireguardConfig(ring int) (*wireguard.Config, error) {
data, err := m.ReadFile("wg%v.conf", ring)
if err != nil {
return nil, err
}
r := bytes.NewReader(data)
return wireguard.NewConfigFromReader(r)
}
func (m *Machine) tryApplyWireguardConfig(ring int) error {
wg, err := m.GetWireguardConfig(ring)
switch {
case os.IsNotExist(err):
return nil
case err != nil:
return err
default:
return m.applyWireguardConfig(ring, wg)
}
}
func (m *Machine) applyWireguardConfig(ring int, wg *wireguard.Config) error {
addr := wg.GetAddress()
zoneID, nodeID, ok := Rings[ring].Decode(addr)
if !ok {
return fmt.Errorf("%s: invalid wg%v address: %s", m.Name, ring, addr)
}
if err := m.applyZoneNodeID(zoneID, nodeID); err != nil {
err = core.Wrapf(err, "%s: wg%v:%s", m.Name, ring, addr)
return err
}
return nil
}
func (m *Machine) applyZoneNodeID(zoneID, nodeID int) error {
switch {
case zoneID == 0:
return fmt.Errorf("invalid %s", "zoneID")
case nodeID == 0:
return fmt.Errorf("invalid %s", "nodeID")
case m.ID() != nodeID:
return fmt.Errorf("invalid %s: %v ≠ %v", "zoneID", m.ID(), nodeID)
case m.zone.ID != 0 && m.zone.ID != zoneID:
return fmt.Errorf("invalid %s: %v ≠ %v", "zoneID", m.zone.ID, zoneID)
case m.zone.ID == 0:
m.zone.ID = zoneID
}
return nil
}
+81 -4
View File
@@ -1,11 +1,86 @@
package zones package zones
import ( import (
"bytes"
"context" "context"
"fmt"
"log"
"net/netip" "net/netip"
"os"
"time" "time"
"git.jpi.io/amery/jpictl/pkg/wireguard"
) )
// GetWireguardConfig reads a wgN.conf file
func (m *Machine) GetWireguardConfig(ring int) (*wireguard.Config, error) {
data, err := m.ReadFile("wg%v.conf", ring)
if err != nil {
return nil, err
}
r := bytes.NewReader(data)
return wireguard.NewConfigFromReader(r)
}
func (m *Machine) tryWireguardZeroConfig() error {
wg, err := m.GetWireguardConfig(0)
switch {
case os.IsNotExist(err):
return nil
case err != nil:
return err
default:
return m.applyWireguardZeroConfig(wg)
}
}
func (m *Machine) applyWireguardZeroConfig(wg *wireguard.Config) error {
addr := wg.GetAddress()
zoneID, nodeID, ok := ParseRingZeroAddress(addr)
if !ok {
return fmt.Errorf("%s: invalid %s address: %s", m.Name, "wg1", addr)
}
if err := m.applyZoneNodeID(zoneID, nodeID); err != nil {
return err
}
log.Println(m.Name, "wg0", addr, zoneID, nodeID)
return nil
}
func (m *Machine) tryWireguardOneConfig() error {
wg, err := m.GetWireguardConfig(1)
switch {
case os.IsNotExist(err):
return nil
case err != nil:
return err
default:
return m.applyWireguardOneConfig(wg)
}
}
func (m *Machine) applyWireguardOneConfig(wg *wireguard.Config) error {
addr := wg.GetAddress()
zoneID, nodeID, ok := ParseRingOneAddress(addr)
if !ok {
return fmt.Errorf("%s: invalid %s address: %s", m.Name, "wg1", addr)
}
if err := m.applyZoneNodeID(zoneID, nodeID); err != nil {
return err
}
log.Println(m.Name, "wg1", addr, zoneID, nodeID)
return nil
}
func (*Machine) applyZoneNodeID(_, _ int) error {
return nil
}
func (m *Machine) lookupNetIP() ([]netip.Addr, error) { func (m *Machine) lookupNetIP() ([]netip.Addr, error) {
timeout := 2 * time.Second timeout := 2 * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout) ctx, cancel := context.WithTimeout(context.Background(), timeout)
@@ -26,10 +101,12 @@ func (m *Machine) updatePublicAddresses() error {
} }
func (m *Machine) scan() error { func (m *Machine) scan() error {
for i := 0; i < RingsCount; i++ { if err := m.tryWireguardZeroConfig(); err != nil {
if err := m.tryApplyWireguardConfig(i); err != nil { return err
return err }
}
if err := m.tryWireguardOneConfig(); err != nil {
return err
} }
return m.updatePublicAddresses() return m.updatePublicAddresses()
+4 -4
View File
@@ -18,10 +18,10 @@ const (
// RingInfo contains represents the Wireguard endpoint details // RingInfo contains represents the Wireguard endpoint details
// for a Machine on a particular ring // for a Machine on a particular ring
type RingInfo struct { type RingInfo struct {
Ring int `toml:"ring"` Ring int
Enabled bool `toml:"enabled,omitempty"` Enabled bool
Keys *wireguard.KeyPair `toml:"keys,omitempty"` Keys *wireguard.KeyPair
Address netip.Addr `toml:"address,omitempty"` Address netip.Addr
} }
// RingAddressEncoder provides encoder/decoder access for a particular // RingAddressEncoder provides encoder/decoder access for a particular
+4 -3
View File
@@ -31,9 +31,10 @@ func (m *Zones) scan() error {
func (m *Zones) scanMachines() error { func (m *Zones) scanMachines() error {
var err error var err error
m.ForEachMachine(func(p *Machine) bool { m.ForEachMachine(func(p *Machine) {
err = p.scan() if err == nil {
return err != nil err = p.scan()
}
}) })
return err return err
} }
+4 -10
View File
@@ -32,24 +32,18 @@ type Zones struct {
} }
// ForEachMachine calls a function for each Machine in the cluster // ForEachMachine calls a function for each Machine in the cluster
func (m *Zones) ForEachMachine(fn func(*Machine) bool) { func (m *Zones) ForEachMachine(fn func(*Machine)) {
for _, z := range m.Zones { for _, z := range m.Zones {
for _, p := range z.Machines { for _, p := range z.Machines {
if fn(p) { fn(p)
// terminate
return
}
} }
} }
} }
// ForEachZone calls a function for each Zone in the cluster // ForEachZone calls a function for each Zone in the cluster
func (m *Zones) ForEachZone(fn func(*Zone) bool) { func (m *Zones) ForEachZone(fn func(*Zone)) {
for _, p := range m.Zones { for _, p := range m.Zones {
if fn(p) { fn(p)
// terminate
return
}
} }
} }