Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1d8c818ec4 | |||
| 2f51a463b2 | |||
| 0c0cba6fb5 | |||
| 75206e4fa5 | |||
| b084e103b9 | |||
| 223edf846b |
@@ -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)
|
||||||
|
}
|
||||||
@@ -31,13 +31,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 PrivateKey
|
||||||
ListenPort uint16
|
ListenPort uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
// PeerConfig represents a [Peer] section
|
// PeerConfig represents a [Peer] section
|
||||||
type PeerConfig struct {
|
type PeerConfig struct {
|
||||||
PublicKey BinaryKey
|
PublicKey PublicKey
|
||||||
Endpoint EndpointAddress
|
Endpoint EndpointAddress
|
||||||
AllowedIPs []netip.Prefix
|
AllowedIPs []netip.Prefix
|
||||||
}
|
}
|
||||||
@@ -135,7 +135,7 @@ func (p interfaceConfig) Export() (InterfaceConfig, error) {
|
|||||||
ListenPort: p.ListenPort,
|
ListenPort: p.ListenPort,
|
||||||
}
|
}
|
||||||
|
|
||||||
out.PrivateKey, err = BinaryKeyFromBase64(p.PrivateKey)
|
out.PrivateKey, err = PrivateKeyFromBase64(p.PrivateKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = core.Wrap(err, "PrivateKey")
|
err = core.Wrap(err, "PrivateKey")
|
||||||
return InterfaceConfig{}, err
|
return InterfaceConfig{}, err
|
||||||
@@ -162,7 +162,7 @@ func (v *intermediateConfig) ExportPeer(i int) (PeerConfig, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// PublicKey
|
// PublicKey
|
||||||
out.PublicKey, err = BinaryKeyFromBase64(v.Peer.PublicKey[i])
|
out.PublicKey, err = PublicKeyFromBase64(v.Peer.PublicKey[i])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = core.Wrap(err, "PublicKey")
|
err = core.Wrap(err, "PublicKey")
|
||||||
return out, err
|
return out, err
|
||||||
|
|||||||
+75
-15
@@ -3,34 +3,94 @@ package wireguard
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
// BinaryKey is a binary blob
|
const (
|
||||||
type BinaryKey []byte
|
// PrivateKeySize is the length in bytes of a Wireguard Private Key
|
||||||
|
PrivateKeySize = 32
|
||||||
|
// PublicKeySize is the length in bytes of a Wireguard Public Key
|
||||||
|
PublicKeySize = 32
|
||||||
|
)
|
||||||
|
|
||||||
func (k BinaryKey) String() string {
|
var (
|
||||||
return base64.StdEncoding.EncodeToString(k)
|
// ErrInvalidKeySize indicates the key size is wrong
|
||||||
|
ErrInvalidKeySize = errors.New("invalid key size")
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
// PrivateKey is a binary Wireguard Private Key
|
||||||
|
PrivateKey []byte
|
||||||
|
// PublicKey is a binary Wireguard Public Key
|
||||||
|
PublicKey []byte
|
||||||
|
)
|
||||||
|
|
||||||
|
func (key PrivateKey) String() string {
|
||||||
|
return encodeKey(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pub PublicKey) String() string {
|
||||||
|
return encodeKey(pub)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsZero tells if the key hasn't been set
|
// IsZero tells if the key hasn't been set
|
||||||
func (k BinaryKey) IsZero() bool {
|
func (key PrivateKey) IsZero() bool {
|
||||||
return len(k) == 0
|
return len(key) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Equal checks if two keys are identical
|
// IsZero tells if the key hasn't been set
|
||||||
func (k BinaryKey) Equal(alter BinaryKey) bool {
|
func (pub PublicKey) IsZero() bool {
|
||||||
return bytes.Equal(k, alter)
|
return len(pub) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// BinaryKeyFromBase64 decodes a base64-based string into
|
// Equal checks if two private keys are identical
|
||||||
// a [BinaryKey]
|
func (key PrivateKey) Equal(alter PrivateKey) bool {
|
||||||
func BinaryKeyFromBase64(data string) (BinaryKey, error) {
|
return bytes.Equal(key, alter)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Equal checks if two public keys are identical
|
||||||
|
func (pub PublicKey) Equal(alter PublicKey) bool {
|
||||||
|
return bytes.Equal(pub, alter)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PrivateKeyFromBase64 decodes a base64-based string into
|
||||||
|
// a [PrivateKey]
|
||||||
|
func PrivateKeyFromBase64(data string) (PrivateKey, error) {
|
||||||
|
b, err := decodeKey(data, PrivateKeySize)
|
||||||
|
return b, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// PublicKeyFromBase64 decodes a base64-based string into
|
||||||
|
// a [PublicKey]
|
||||||
|
func PublicKeyFromBase64(data string) (PublicKey, error) {
|
||||||
|
b, err := decodeKey(data, PublicKeySize)
|
||||||
|
return b, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func encodeKey(b []byte) string {
|
||||||
|
switch {
|
||||||
|
case len(b) == 0:
|
||||||
|
return ""
|
||||||
|
default:
|
||||||
|
return base64.StdEncoding.EncodeToString(b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeKey(data string, size int) ([]byte, error) {
|
||||||
b, err := base64.StdEncoding.DecodeString(data)
|
b, err := base64.StdEncoding.DecodeString(data)
|
||||||
return BinaryKey(b), err
|
switch {
|
||||||
|
case err != nil:
|
||||||
|
return []byte{}, err
|
||||||
|
case len(b) != size:
|
||||||
|
err = ErrInvalidKeySize
|
||||||
|
return []byte{}, err
|
||||||
|
default:
|
||||||
|
return b, nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// KeyPair holds a Key pair
|
// KeyPair holds a Key pair
|
||||||
type KeyPair struct {
|
type KeyPair struct {
|
||||||
PrivateKey BinaryKey
|
PrivateKey PrivateKey
|
||||||
PublicKey BinaryKey
|
PublicKey PublicKey
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,104 @@
|
|||||||
|
package zones
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// WriteEnv generates environment variables for shell scripts
|
||||||
|
func (m *Zones) WriteEnv(w io.Writer) error {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
|
||||||
|
m.writeEnvVarFn(&buf, genEnvZones, "ZONES")
|
||||||
|
m.ForEachZone(func(z *Zone) bool {
|
||||||
|
m.writeEnvZone(&buf, z)
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
|
||||||
|
_, err := buf.WriteTo(w)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Zones) writeEnvZone(w io.Writer, z *Zone) {
|
||||||
|
zoneID := z.ID
|
||||||
|
|
||||||
|
// ZONE{zoneID}
|
||||||
|
m.writeEnvVar(w, genEnvZoneNodes(z), "ZONE%v", zoneID)
|
||||||
|
|
||||||
|
// ZONE{zoneID}_NAME
|
||||||
|
m.writeEnvVar(w, z.Name, "ZONE%v_%s", zoneID, "NAME")
|
||||||
|
|
||||||
|
// ZONE{zoneID}_GW
|
||||||
|
gatewayID := getRingZeroGatewayID(z)
|
||||||
|
m.writeEnvVar(w, fmt.Sprintf("%v", gatewayID), "ZONE%v_%s", zoneID, "GW")
|
||||||
|
|
||||||
|
// ZONE{zoneID}_IP
|
||||||
|
ip, _ := RingZeroAddress(zoneID, gatewayID)
|
||||||
|
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) {
|
||||||
|
var value string
|
||||||
|
|
||||||
|
if fn != nil {
|
||||||
|
value = fn(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
m.writeEnvVar(w, value, name, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*Zones) writeEnvVar(w io.Writer, value string, name string, args ...any) {
|
||||||
|
if len(args) > 0 {
|
||||||
|
name = fmt.Sprintf(name, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
if name != "" {
|
||||||
|
value = strings.TrimSpace(value)
|
||||||
|
|
||||||
|
_, _ = fmt.Fprintf(w, "%s=%q\n", name, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func genEnvZones(m *Zones) string {
|
||||||
|
s := make([]string, 0, len(m.Zones))
|
||||||
|
for _, z := range m.Zones {
|
||||||
|
s = append(s, fmt.Sprintf("%v", z.ID))
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Join(s, " ")
|
||||||
|
}
|
||||||
|
|
||||||
|
func genEnvZoneNodes(z *Zone) string {
|
||||||
|
s := make([]string, 0, len(z.Machines))
|
||||||
|
for _, p := range z.Machines {
|
||||||
|
s = append(s, p.Name)
|
||||||
|
}
|
||||||
|
return strings.Join(s, " ")
|
||||||
|
}
|
||||||
|
|
||||||
|
func getRingZeroGatewayID(z *Zone) int {
|
||||||
|
var firstNodeID, gatewayID int
|
||||||
|
|
||||||
|
z.ForEachMachine(func(p *Machine) bool {
|
||||||
|
nodeID := p.ID()
|
||||||
|
|
||||||
|
if firstNodeID == 0 {
|
||||||
|
firstNodeID = nodeID
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, found := p.getRingInfo(0); found {
|
||||||
|
gatewayID = nodeID
|
||||||
|
}
|
||||||
|
|
||||||
|
return gatewayID != 0
|
||||||
|
})
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case gatewayID == 0:
|
||||||
|
return firstNodeID
|
||||||
|
default:
|
||||||
|
return gatewayID
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -60,17 +60,19 @@ func (m *Machine) applyWireguardConfig(ring int, wg *wireguard.Config) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Machine) applyRingInfo(ring int, new *RingInfo) error {
|
func (m *Machine) getRingInfo(ring int) (*RingInfo, bool) {
|
||||||
var cur *RingInfo
|
|
||||||
|
|
||||||
for _, ri := range m.RingAddresses {
|
for _, ri := range m.RingAddresses {
|
||||||
if ri.Ring == ring {
|
if ri.Ring == ring {
|
||||||
cur = ri
|
return ri, true
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if cur == nil {
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Machine) applyRingInfo(ring int, new *RingInfo) error {
|
||||||
|
cur, found := m.getRingInfo(ring)
|
||||||
|
if !found {
|
||||||
// first, append
|
// first, append
|
||||||
m.RingAddresses = append(m.RingAddresses, new)
|
m.RingAddresses = append(m.RingAddresses, new)
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
+22
-8
@@ -22,6 +22,16 @@ func (z *Zone) String() string {
|
|||||||
return z.Name
|
return z.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ForEachMachine calls a function for each Machine in the zone
|
||||||
|
// until instructed to terminate the loop
|
||||||
|
func (z *Zone) ForEachMachine(fn func(*Machine) bool) {
|
||||||
|
for _, p := range z.Machines {
|
||||||
|
if fn(p) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 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
|
||||||
@@ -32,18 +42,22 @@ type Zones struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ForEachMachine calls a function for each Machine in the cluster
|
// ForEachMachine calls a function for each Machine in the cluster
|
||||||
|
// until instructed to terminate the loop
|
||||||
func (m *Zones) ForEachMachine(fn func(*Machine) bool) {
|
func (m *Zones) ForEachMachine(fn func(*Machine) bool) {
|
||||||
for _, z := range m.Zones {
|
m.ForEachZone(func(z *Zone) bool {
|
||||||
for _, p := range z.Machines {
|
var term bool
|
||||||
if fn(p) {
|
|
||||||
// terminate
|
z.ForEachMachine(func(p *Machine) bool {
|
||||||
return
|
term = fn(p)
|
||||||
}
|
return term
|
||||||
}
|
})
|
||||||
}
|
|
||||||
|
return term
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// ForEachZone calls a function for each Zone in the cluster
|
// ForEachZone calls a function for each Zone in the cluster
|
||||||
|
// until instructed to terminate the loop
|
||||||
func (m *Zones) ForEachZone(fn func(*Zone) bool) {
|
func (m *Zones) ForEachZone(fn func(*Zone) bool) {
|
||||||
for _, p := range m.Zones {
|
for _, p := range m.Zones {
|
||||||
if fn(p) {
|
if fn(p) {
|
||||||
|
|||||||
Reference in New Issue
Block a user