Compare commits

...

8 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
amery 0d14510958 zones: introduce RingAddressEncoder and RingZero/RingOne implementations
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-22 15:57:10 +00:00
amery c3b47ba812 zones: introduce ValidZoneID() and ValidNodeID()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-22 15:43:42 +00:00
amery 15f5aab449 zones: rename address.go to rings.go
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-22 15:43:40 +00:00
6 changed files with 276 additions and 49 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)
}
+30
View File
@@ -0,0 +1,30 @@
package wireguard
import (
"encoding/base64"
)
// BinaryKey is a binary blob
type BinaryKey []byte
func (k BinaryKey) String() string {
return base64.StdEncoding.EncodeToString(k)
}
// IsZero tells if the key hasn't been set
func (k BinaryKey) IsZero() bool {
return len(k) == 0
}
// BinaryKeyFromBase64 decodes a base64-based string into
// a [BinaryKey]
func BinaryKeyFromBase64(data string) (BinaryKey, error) {
b, err := base64.StdEncoding.DecodeString(data)
return BinaryKey(b), err
}
// KeyPair holds a Key pair
type KeyPair struct {
PrivateKey BinaryKey
PublicKey BinaryKey
}
-49
View File
@@ -1,49 +0,0 @@
package zones
import "net/netip"
// ParseRingZeroAddress extracts zone and node ID from a wg0 [netip.Addr]
func ParseRingZeroAddress(addr netip.Addr) (zoneID int, nodeID int, ok bool) {
if addr.IsValid() {
a4 := addr.As4()
if a4[0] == 10 && a4[1] == 0 {
return int(a4[2]), int(a4[3]), true
}
}
return 0, 0, false
}
// RingZeroAddress returns a wg0 IP address
func RingZeroAddress(zoneID, nodeID int) netip.Addr {
c := zoneID
d := nodeID
return netip.AddrFrom4([4]byte{
10, 0, uint8(c), uint8(d),
})
}
// ParseRingOneAddress extracts zone and node ID from a wg1 [netip.Addr]
func ParseRingOneAddress(addr netip.Addr) (zoneID int, nodeID int, ok bool) {
if addr.IsValid() {
a4 := addr.As4()
if a4[0] == 10 && a4[2] == 0 {
zoneID = int(a4[1] >> 4)
nodeID = int(a4[3])
return zoneID, nodeID, true
}
}
return 0, 0, false
}
// RingOneAddress returns a wg1 IP address
func RingOneAddress(zoneID, nodeID int) netip.Addr {
b := zoneID << 4
d := nodeID
return netip.AddrFrom4([4]byte{
10, uint8(b), 0, uint8(d),
})
}
+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
}
+83
View File
@@ -1,11 +1,86 @@
package zones
import (
"bytes"
"context"
"fmt"
"log"
"net/netip"
"os"
"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) {
timeout := 2 * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout)
@@ -26,5 +101,13 @@ func (m *Machine) updatePublicAddresses() error {
}
func (m *Machine) scan() error {
if err := m.tryWireguardZeroConfig(); err != nil {
return err
}
if err := m.tryWireguardOneConfig(); err != nil {
return err
}
return m.updatePublicAddresses()
}
+127
View File
@@ -0,0 +1,127 @@
package zones
import (
"net/netip"
"git.jpi.io/amery/jpictl/pkg/wireguard"
)
const (
// MaxZoneID indicates the highest ID allowed for a Zone
MaxZoneID = 0xf
// MaxNodeID indicates the highest Machine ID allowed within a Zone
MaxNodeID = 0xff - 1
// RingsCount indicates how many wireguard rings we have
RingsCount = 2
)
// RingInfo contains represents the Wireguard endpoint details
// for a Machine on a particular ring
type RingInfo struct {
Ring int
Enabled bool
Keys *wireguard.KeyPair
Address netip.Addr
}
// RingAddressEncoder provides encoder/decoder access for a particular
// Wireguard ring
type RingAddressEncoder struct {
ID int
Encode func(zoneID, nodeID int) (netip.Addr, bool)
Decode func(addr netip.Addr) (zoneID, nodeID int, ok bool)
}
var (
// RingZero is a wg0 address encoder/decoder
RingZero = RingAddressEncoder{
ID: 0,
Decode: ParseRingZeroAddress,
Encode: RingZeroAddress,
}
// RingOne is a wg1 address encoder/decoder
RingOne = RingAddressEncoder{
ID: 1,
Decode: ParseRingOneAddress,
Encode: RingOneAddress,
}
// Rings provides indexed access to the ring address encoders
Rings = [RingsCount]RingAddressEncoder{
RingZero,
RingOne,
}
)
// ValidZoneID checks if the given zoneID is a valid 4 bit zone number.
//
// 0 is reserved, and only allowed when composing CIDRs.
func ValidZoneID(zoneID int) bool {
switch {
case zoneID < 0 || zoneID > MaxZoneID:
return false
default:
return true
}
}
// ValidNodeID checks if the given nodeID is a valid 8 bit number.
// nodeID is unique within a Zone.
// 0 is reserved, and only allowed when composing CIDRs.
func ValidNodeID(nodeID int) bool {
switch {
case nodeID < 0 || nodeID > MaxNodeID:
return false
default:
return true
}
}
// ParseRingZeroAddress extracts zone and node ID from a wg0 [netip.Addr]
// wg0 addresses are of the form `10.0.{{zoneID}}.{{nodeID}}`
func ParseRingZeroAddress(addr netip.Addr) (zoneID int, nodeID int, ok bool) {
if addr.IsValid() {
a4 := addr.As4()
if a4[0] == 10 && a4[1] == 0 {
return int(a4[2]), int(a4[3]), true
}
}
return 0, 0, false
}
// RingZeroAddress returns a wg0 IP address
func RingZeroAddress(zoneID, nodeID int) (netip.Addr, bool) {
switch {
case !ValidZoneID(zoneID) || !ValidNodeID(nodeID):
return netip.Addr{}, false
default:
a4 := [4]uint8{10, 0, uint8(zoneID), uint8(nodeID)}
return netip.AddrFrom4(a4), true
}
}
// ParseRingOneAddress extracts zone and node ID from a wg1 [netip.Addr]
// wg1 addresses are of the form `10.{{zoneID << 4}}.{{nodeID}}`
func ParseRingOneAddress(addr netip.Addr) (zoneID int, nodeID int, ok bool) {
if addr.IsValid() {
a4 := addr.As4()
if a4[0] == 10 && a4[2] == 0 {
zoneID = int(a4[1] >> 4)
nodeID = int(a4[3])
return zoneID, nodeID, true
}
}
return 0, 0, false
}
// RingOneAddress returns a wg1 IP address
func RingOneAddress(zoneID, nodeID int) (netip.Addr, bool) {
switch {
case !ValidZoneID(zoneID) || !ValidNodeID(nodeID):
return netip.Addr{}, false
default:
a4 := [4]uint8{10, 0, uint8(zoneID << 4), uint8(nodeID)}
return netip.AddrFrom4(a4), true
}
}