Compare commits

...

27 Commits

Author SHA1 Message Date
amery a910bba406 Merge pull request 'cluster: introduce Machine.Inactive flag' (#38)
Reviewed-on: #38
2023-10-30 20:44:41 +01:00
amery 5ef6d45ef7 Merge pull request 'jpictl: fix cloud.yaml unmarshalling' (#32)
Reviewed-on: #32
2023-10-30 20:43:21 +01:00
amery 99998dc7e8 cluster: mark Machine as Inactive if the "region" file contains "none"
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-30 19:21:08 +00:00
amery 892d849740 cluster: introduce Machine.Inactive flag
if a Machine is Inactive, it won't be included on the DNS
aliases for the zone or it's regions.

v2:
- Machine.Active() renamed to Machine.IsActive()

Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-30 19:20:45 +00:00
amery 125a4c0dbe wireguard: implement EndpointAddress.UnmarshalText
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-30 19:13:41 +00:00
amery 7c811d7813 wireguard: implement UnmarshalText for PrivateKey and PublicKey
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-30 19:13:41 +00:00
amery 1580c09746 cluster: add Machine.ReadLines() shortcut
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-30 18:52:28 +00:00
amery a928ab8880 Merge pull request 'jpictl: add flags to control the default scan' (#34)
Reviewed-on: #34
2023-10-30 19:50:14 +01:00
amery 66fc213f64 Merge pull request 'cluster: improve defaults to ease initialisation of new machines' (#31)
Reviewed-on: #31
2023-10-30 19:48:40 +01:00
amery 944400249f Merge pull request 'jpictl: create machine directories on jpictl write' (#33)
Reviewed-on: #33
2023-10-30 19:47:37 +01:00
amery 1492061ab8 jpictl: create machine directories on jpictl write
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-30 18:46:48 +00:00
amery 57e1077a85 cluster: add MkdirAll() support
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-30 18:46:46 +00:00
amery 5a3d483f98 cluster: refactor Machine.ReadFile() and Machine.OpenFile()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-30 18:45:01 +00:00
amery 0ff17abd59 Merge pull request 'cluster: add top level ReadLines(), WriteStringFile() and RemoveFile() helpers' (#36)
Reviewed-on: #36
2023-10-30 19:42:50 +01:00
amery a742bad084 Merge pull request 'dns: sort zones when writing data' (#35)
Reviewed-on: #35
2023-10-30 19:41:16 +01:00
amery 629b6ee74f Merge pull request 'cluster: sort regions, for jpictl dump sake' (#37)
Reviewed-on: #37
2023-10-30 19:39:02 +01:00
amery 884b11d1f9 cluster: sort regions, for jpictl dump' sake
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-30 00:00:57 +00:00
amery 5bbe15ef24 cluster: move SortRegions() from dns to cluster
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-30 00:00:22 +00:00
amery fd1c57d377 cluster: introduce Cluster.ReadLines()
reading a file, splitting and trimming lines, and
allowing # to comment a line out

Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-29 23:28:57 +00:00
amery 2fd5947f1b cluster: introduce Cluster.WriteStringFile()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-29 23:28:45 +00:00
amery 14b3d91191 cluster: introduce Cluster.RemoveFile()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-29 23:27:04 +00:00
amery abe3005769 dns: sort zones when writing data
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-29 02:32:18 +00:00
amery 727fd67bc6 jpictl: add --domain/-D and --scan-dir/-d options
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-28 21:22:39 +00:00
amery b8e1b321e5 jpictl: add -S flag to ignore the config file and always scan
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-28 21:22:37 +00:00
amery 0c5429a681 jpictl: move verbosity handling to the log module
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-28 21:22:36 +00:00
amery e5639b2f4e cluster: generate ring keys on scan if missing
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-27 19:55:46 +00:00
amery 543824a54a cluster: allow empty wgN.conf files as markers to enable the ring
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-27 19:55:46 +00:00
18 changed files with 345 additions and 100 deletions
+40 -17
View File
@@ -12,6 +12,14 @@ const (
// DefaultConfigFile is read if -f/--config-file isn't specified.
// If it doesn't exist, m/ will be scanned
DefaultConfigFile = "cloud.yaml"
// DefaultClusterDir is the directory we will scan and write
// unless something else is indicated
DefaultClusterDir = "m"
// DefaultDomain indicates the domain to use unless
// something else is specified
DefaultDomain = "jpi.cloud"
)
// Config describes the repository
@@ -22,27 +30,34 @@ type Config struct {
ConfigFile string
}
var forceScan bool
var cfg = &Config{
Base: "m",
Domain: "jpi.cloud",
Base: DefaultClusterDir,
Domain: DefaultDomain,
}
// LoadZones loads all zones and machines in the config directory
// or file
func (cfg *Config) LoadZones(resolve bool) (*cluster.Cluster, error) {
// try config file first
zones, err := cluster.NewFromConfig(cfg.ConfigFile,
cluster.ResolvePublicAddresses(resolve),
cluster.WithLogger(log),
)
var zones *cluster.Cluster
var err error
switch {
case err == nil:
// file was good
return zones, nil
case !os.IsNotExist(err) || cfg.ConfigFile != DefaultConfigFile:
// file was bad
return nil, core.Wrap(err, "NewFromConfig(%q)", cfg.ConfigFile)
if !forceScan {
// try config file first
zones, err = cluster.NewFromConfig(cfg.ConfigFile,
cluster.ResolvePublicAddresses(resolve),
cluster.WithLogger(log),
)
switch {
case err == nil:
// file was good
return zones, nil
case !os.IsNotExist(err) || cfg.ConfigFile != DefaultConfigFile:
// file was bad
return nil, core.Wrap(err, "NewFromConfig(%q)", cfg.ConfigFile)
}
}
// default file doesn't exist. scan instead.
@@ -53,7 +68,15 @@ func (cfg *Config) LoadZones(resolve bool) (*cluster.Cluster, error) {
}
func init() {
rootCmd.PersistentFlags().
StringVarP(&cfg.ConfigFile, "config-file", "f",
DefaultConfigFile, "config file (JSON or YAML)")
flags := rootCmd.PersistentFlags()
flags.StringVarP(&cfg.Base, "scan-dir", "d",
DefaultClusterDir, "directory to scan for cluster data")
flags.StringVarP(&cfg.Domain, "domain", "D",
DefaultDomain, "domain to use for scanned data")
flags.StringVarP(&cfg.ConfigFile, "config-file", "f",
DefaultConfigFile, "config file (JSON or YAML)")
flags.BoolVarP(&forceScan, "force-scan", "S",
false, "ignore config file and scan the directory instead")
}
+1 -1
View File
@@ -52,7 +52,7 @@ func populateDNSManager(mgr *dns.Manager, m *cluster.Cluster) error {
m.ForEachZone(func(z *cluster.Zone) bool {
z.ForEachMachine(func(p *cluster.Machine) bool {
err = mgr.AddHost(ctx, z.Name, p.ID, true, p.PublicAddresses...)
err = mgr.AddHost(ctx, z.Name, p.ID, p.IsActive(), p.PublicAddresses...)
return err != nil
})
+21
View File
@@ -3,9 +3,13 @@ package main
import (
"fmt"
"darvaza.org/sidecar/pkg/logger/zerolog"
"darvaza.org/slog"
"github.com/spf13/cobra"
)
var log = zerolog.New(nil, slog.Error)
// fatal is a convenience wrapper for slog.Logger.Fatal().Print()
func fatal(err error, msg string, args ...any) {
l := log.Fatal()
@@ -19,3 +23,20 @@ func fatal(err error, msg string, args ...any) {
panic("unreachable")
}
var verbosity int
// setVerbosity replaces the global logger using the
// verbosity level specified via -v flags
func setVerbosity(_ *cobra.Command, _ []string) {
desired := int8(slog.Error) + int8(verbosity)
if desired > 6 {
desired = 6
}
log = zerolog.New(nil, slog.LogLevel(desired))
}
func init() {
rootCmd.PersistentFlags().CountVarP(&verbosity, "verbosity", "v",
"increase the verbosity level to Warn, Info or Debug")
}
+1 -18
View File
@@ -2,8 +2,6 @@
package main
import (
"darvaza.org/sidecar/pkg/logger/zerolog"
"darvaza.org/slog"
"github.com/spf13/cobra"
)
@@ -13,9 +11,7 @@ const (
)
var (
log = zerolog.New(nil, slog.Error)
verbosity int
rootCmd = &cobra.Command{
rootCmd = &cobra.Command{
Use: CmdName,
Short: "control tool for jpi.cloud",
}
@@ -26,16 +22,3 @@ func main() {
fatal(err, "")
}
}
func init() {
rootCmd.PersistentFlags().CountVarP(&verbosity, "verbosity", "v",
"increase the verbosity level to Warn, Info or Debug")
}
func setVerbosity(_ *cobra.Command, _ []string) {
desired := int8(slog.Error) + int8(verbosity)
if desired > 6 {
desired = 6
}
log = zerolog.New(nil, slog.LogLevel(desired))
}
+65
View File
@@ -1,9 +1,12 @@
package cluster
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"strings"
fs "github.com/hack-pad/hackpadfs"
)
@@ -40,6 +43,21 @@ func (m *Cluster) openWriter(name string, flags int, args ...any) (io.WriteClose
panic("unreachable")
}
// RemoveFile deletes a file from the cluster's config directory
func (m *Cluster) RemoveFile(name string, args ...any) error {
if len(args) > 0 {
name = fmt.Sprintf(name, args...)
}
err := fs.Remove(m.dir, name)
switch {
case os.IsNotExist(err):
return nil
default:
return err
}
}
// ReadFile reads a file from the cluster's config directory
func (m *Cluster) ReadFile(name string, args ...any) ([]byte, error) {
if len(args) > 0 {
@@ -48,3 +66,50 @@ func (m *Cluster) ReadFile(name string, args ...any) ([]byte, error) {
return fs.ReadFile(m.dir, name)
}
// ReadLines reads a file from the cluster's config directory,
// split by lines, trimmed, and accepting `#` to comment lines out.
func (m *Cluster) ReadLines(name string, args ...any) ([]string, error) {
var out []string
data, err := m.ReadFile(name, args...)
if err != nil {
return nil, err
}
sc := bufio.NewScanner(bytes.NewReader(data))
for sc.Scan() {
s := strings.TrimSpace(sc.Text())
switch {
case s == "", strings.HasPrefix(s, "#"):
// ignore
default:
// accepted
out = append(out, s)
}
}
return out, nil
}
// WriteStringFile writes the given content to a file on the machine's config directory
func (m *Cluster) 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
}
// MkdirAll creates directories relative to the cluster's config directory
func (m *Cluster) MkdirAll(name string, args ...any) error {
if len(args) > 0 {
name = fmt.Sprintf(name, args...)
}
return fs.MkdirAll(m.dir, name, 0755)
}
+4
View File
@@ -73,6 +73,10 @@ func (m *Cluster) scanMachines(opts *ScanOptions) error {
err = p.scan(opts)
return err != nil
})
m.ForEachMachine(func(p *Machine) bool {
err = p.scanWrapUp(opts)
return err != nil
})
return err
}
+6
View File
@@ -15,6 +15,7 @@ type Machine struct {
ID int
Name string `json:"-" yaml:"-"`
Inactive bool `json:"inactive,omitempty" yaml:"inactive,omitempty"`
CephMonitor bool `json:"ceph_monitor,omitempty" yaml:"ceph_monitor,omitempty"`
PublicAddresses []netip.Addr `json:"public,omitempty" yaml:"public,omitempty"`
Rings []*RingInfo `json:"rings,omitempty" yaml:"rings,omitempty"`
@@ -43,6 +44,11 @@ func (m *Machine) FullName() string {
return strings.Join(name, ".")
}
// IsActive indicates the machine is to be included in regions' DNS entries
func (m *Machine) IsActive() bool {
return !m.Inactive
}
// IsGateway tells if the Machine is a ring0 gateway
func (m *Machine) IsGateway() bool {
_, ok := m.getRingInfo(0)
+20 -21
View File
@@ -1,7 +1,6 @@
package cluster
import (
"bytes"
"fmt"
"io"
"os"
@@ -12,10 +11,9 @@ import (
// OpenFile opens a file on the machine's config directory with the specified flags
func (m *Machine) OpenFile(name string, flags int, args ...any) (fs.File, error) {
base := m.zone.zones.dir
fullName := m.getFilename(name, args...)
return fs.OpenFile(base, fullName, flags, 0644)
return m.zone.zones.OpenFile(fullName, flags)
}
// CreateTruncFile creates or truncates a file on the machine's config directory
@@ -43,37 +41,38 @@ func (m *Machine) openWriter(name string, flags int, args ...any) (io.WriteClose
// RemoveFile deletes a file from the machine's config directory
func (m *Machine) RemoveFile(name string, args ...any) error {
base := m.zone.zones.dir
fullName := m.getFilename(name, args...)
err := fs.Remove(base, fullName)
switch {
case os.IsNotExist(err):
return nil
default:
return err
}
return m.zone.zones.RemoveFile(fullName)
}
// ReadFile reads a file from the machine's config directory
func (m *Machine) ReadFile(name string, args ...any) ([]byte, error) {
base := m.zone.zones.dir
fullName := m.getFilename(name, args...)
return fs.ReadFile(base, fullName)
return m.zone.zones.ReadFile(fullName)
}
// ReadLines reads a file from the machine's config directory,
// split by lines, trimmed, and accepting `#` to comment lines out.
func (m *Machine) ReadLines(name string, args ...any) ([]string, error) {
fullName := m.getFilename(name, args...)
return m.zone.zones.ReadLines(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()
fullName := m.getFilename(name, args...)
buf := bytes.NewBufferString(value)
_, err = buf.WriteTo(f)
return err
return m.zone.zones.WriteStringFile(value, fullName)
}
// MkdirAll creates directories relative to the machine's config directory
func (m *Machine) MkdirAll(name string, args ...any) error {
fullName := m.getFilename(name, args...)
return m.zone.zones.MkdirAll(fullName)
}
func (m *Machine) getFilename(name string, args ...any) string {
+34 -7
View File
@@ -118,21 +118,31 @@ func (m *Machine) tryApplyWireguardConfig(ring int) error {
}
}
func (m *Machine) applyWireguardConfig(ring int, wg *wireguard.Config) error {
func (m *Machine) applyWireguardConfigNode(ring int, wg *wireguard.Config) error {
addr := wg.GetAddress()
zoneID, nodeID, ok := Rings[ring].Decode(addr)
if !ok {
return fmt.Errorf("%s: invalid address", addr)
}
if !core.IsZero(addr) {
zoneID, nodeID, ok := Rings[ring].Decode(addr)
if !ok {
return fmt.Errorf("%s: invalid address", addr)
}
if err := m.applyZoneNodeID(zoneID, nodeID); err != nil {
return core.Wrap(err, "%s: invalid address", addr)
if err := m.applyZoneNodeID(zoneID, nodeID); err != nil {
return core.Wrap(err, "%s: invalid address", addr)
}
}
if err := m.applyWireguardInterfaceConfig(ring, wg.Interface); err != nil {
return core.Wrap(err, "interface")
}
return nil
}
func (m *Machine) applyWireguardConfig(ring int, wg *wireguard.Config) error {
if err := m.applyWireguardConfigNode(ring, wg); err != nil {
return err
}
for _, peer := range wg.Peer {
err := m.applyWireguardPeerConfig(ring, peer)
switch {
@@ -230,6 +240,23 @@ func (m *Machine) applyZoneNodeID(zoneID, nodeID int) error {
return nil
}
func (m *Machine) setRingDefaults(ri *RingInfo) error {
if ri.Keys.PrivateKey.IsZero() {
m.info().
WithField("subsystem", "wireguard").
WithField("node", m.Name).
WithField("ring", ri.Ring).
Print("generating key pair")
kp, err := wireguard.NewKeyPair()
if err != nil {
return err
}
ri.Keys = kp
}
return nil
}
// RemoveWireguardConfig deletes wgN.conf from the machine's
// config directory.
func (m *Machine) RemoveWireguardConfig(ring int) error {
+42 -1
View File
@@ -3,6 +3,7 @@ package cluster
import (
"context"
"net/netip"
"os"
"strconv"
"strings"
"time"
@@ -68,7 +69,8 @@ func (m *Machine) setID() error {
return nil
}
func (m *Machine) scan(opts *ScanOptions) error {
// scan is called once we know about all zones and machine names
func (m *Machine) scan(_ *ScanOptions) error {
for i := 0; i < RingsCount; i++ {
if err := m.tryApplyWireguardConfig(i); err != nil {
m.error(err).
@@ -80,6 +82,45 @@ func (m *Machine) scan(opts *ScanOptions) error {
}
}
return m.loadInactive()
}
func (m *Machine) loadInactive() error {
data, err := m.ReadLines("region")
switch {
case os.IsNotExist(err):
// no file
return nil
case err != nil:
// read error
return err
default:
// look for "none"
for _, r := range data {
switch r {
case "none":
m.Inactive = true
default:
m.Inactive = false
}
}
return nil
}
}
// scanWrapUp is called once all machines have been scanned
func (m *Machine) scanWrapUp(opts *ScanOptions) error {
for _, ri := range m.Rings {
if err := m.setRingDefaults(ri); err != nil {
m.error(err).
WithField("subsystem", "wireguard").
WithField("node", m.Name).
WithField("ring", ri.Ring).
Print()
return err
}
}
if !opts.DontResolvePublicAddresses {
return m.UpdatePublicAddresses()
}
+5 -1
View File
@@ -32,7 +32,9 @@ func (r *Region) ForEachMachine(fn func(*Machine) bool) {
var term bool
z.ForEachMachine(func(p *Machine) bool {
term = fn(p)
if p.IsActive() {
term = fn(p)
}
return term
})
@@ -56,6 +58,7 @@ func (m *Cluster) initRegions(_ *ScanOptions) error {
// first regions defined by zones
m.ForEachZone(func(z *Zone) bool {
SortRegions(z.Regions)
for _, region := range z.Regions {
regions[region] = append(regions[region], z)
}
@@ -74,6 +77,7 @@ func (m *Cluster) initRegions(_ *ScanOptions) error {
m.finishRegion(r)
}
m.sortRegions()
return nil
}
+41
View File
@@ -0,0 +1,41 @@
package cluster
import "sort"
// SortRegions sorts regions. first by length those 3-character
// or shorter, and then by length. It's mostly aimed at
// supporting ISO-3166 order
func SortRegions(regions []string) []string {
sort.Slice(regions, func(i, j int) bool {
r1, r2 := regions[i], regions[j]
return regionLess(r1, r2)
})
return regions
}
func regionLess(r1, r2 string) bool {
switch {
case len(r1) < 4:
switch {
case len(r1) < len(r2):
return true
case len(r1) > len(r2):
return false
default:
return r1 < r2
}
case len(r2) < 4:
return false
default:
return r1 < r2
}
}
func (m *Cluster) sortRegions() {
sort.Slice(m.Regions, func(i, j int) bool {
r1 := m.Regions[i].Name
r2 := m.Regions[j].Name
return regionLess(r1, r2)
})
}
+1 -1
View File
@@ -41,7 +41,7 @@ func (ri *RingInfo) Merge(alter *RingInfo) error {
// can't disable via Merge
return fmt.Errorf("invalid %s: %v → %v", "enabled", ri.Enabled, alter.Enabled)
case !canMergeKeyPairs(ri.Keys, alter.Keys):
// incompatible keypairs
// incompatible key pairs
return fmt.Errorf("invalid %s: %s ≠ %s", "keys", ri.Keys, alter.Keys)
}
+15
View File
@@ -3,6 +3,7 @@ package cluster
// SyncAll updates all config files
func (m *Cluster) SyncAll() error {
for _, fn := range []func() error{
m.SyncMkdirAll,
m.SyncAllWireguard,
m.SyncAllCeph,
m.WriteHosts,
@@ -15,6 +16,20 @@ func (m *Cluster) SyncAll() error {
return nil
}
// SyncMkdirAll creates the directories needed to store files
// required to represent the cluster.
func (m *Cluster) SyncMkdirAll() error {
err := m.MkdirAll(".")
if err == nil {
m.ForEachMachine(func(p *Machine) bool {
err = p.MkdirAll(".")
return err != nil
})
}
return err
}
// SyncAllWireguard updates all wireguard config files
func (m *Cluster) SyncAllWireguard() error {
var err error
+13 -27
View File
@@ -11,6 +11,8 @@ import (
"darvaza.org/core"
"github.com/libdns/libdns"
"git.jpi.io/amery/jpictl/pkg/cluster"
)
func (mgr *Manager) fqdn(name string) string {
@@ -86,32 +88,6 @@ func lessRecord(a, b libdns.Record) bool {
}
}
// SortRegions sorts regions. first by length those 3-character
// or shorter, and then by length. It's mostly aimed at
// supporting ISO-3166 order
func SortRegions(regions []string) []string {
sort.Slice(regions, func(i, j int) bool {
r1, r2 := regions[i], regions[j]
switch {
case len(r1) < 4:
switch {
case len(r1) < len(r2):
return true
case len(r1) > len(r2):
return false
default:
return r1 < r2
}
case len(r2) < 4:
return false
default:
return r1 < r2
}
})
return regions
}
// AddrRecord represents an A or AAAA record
type AddrRecord struct {
Name string
@@ -172,7 +148,17 @@ func (mgr *Manager) genRegionsSorted() []string {
regions = append(regions, name)
}
return SortRegions(regions)
return cluster.SortRegions(regions)
}
func (mgr *Manager) genZonesSorted() []string {
zones := make([]string, 0, len(mgr.zones))
for name := range mgr.zones {
zones = append(zones, name)
}
sort.Strings(zones)
return zones
}
func (mgr *Manager) genAllAddrRecords() []AddrRecord {
+3 -2
View File
@@ -14,12 +14,13 @@ func (mgr *Manager) WriteTo(w io.Writer) (int64, error) {
cache := make(map[string][]netip.Addr)
// zones
for _, z := range mgr.zones {
for _, zoneName := range mgr.genZonesSorted() {
z := mgr.zones[zoneName]
mgr.writeZoneHosts(&buf, z)
// zone alias
addrs := mgr.genZoneAddresses(z)
zoneName := z.Name
rr := AddrRecord{
Name: mgr.fqdn(zoneName + mgr.suffix),
+11 -4
View File
@@ -107,6 +107,11 @@ func (ep EndpointAddress) String() string {
}
}
// UnmarshalText loads an endpoint address from text data
func (ep *EndpointAddress) UnmarshalText(b []byte) error {
return ep.FromString(string(b))
}
// FromString sets the EndpointAddress from a given "[host]:port"
func (ep *EndpointAddress) FromString(s string) error {
host, port, err := core.SplitHostPort(s)
@@ -175,10 +180,12 @@ func (p interfaceConfig) Export() (InterfaceConfig, error) {
ListenPort: p.ListenPort,
}
out.PrivateKey, err = PrivateKeyFromBase64(p.PrivateKey)
if err != nil {
err = core.Wrap(err, "PrivateKey")
return InterfaceConfig{}, err
if p.PrivateKey != "" {
out.PrivateKey, err = PrivateKeyFromBase64(p.PrivateKey)
if err != nil {
err = core.Wrap(err, "PrivateKey")
return InterfaceConfig{}, err
}
}
return out, nil
+22
View File
@@ -51,6 +51,28 @@ func (pub PublicKey) String() string {
}
}
// UnmarshalText loads the value from base64
func (key *PrivateKey) UnmarshalText(b []byte) error {
v, err := PrivateKeyFromBase64(string(b))
if err != nil {
return err
}
*key = v
return nil
}
// UnmarshalText loads the value from base64
func (pub *PublicKey) UnmarshalText(b []byte) error {
v, err := PublicKeyFromBase64(string(b))
if err != nil {
return err
}
*pub = v
return nil
}
// MarshalJSON encodes the key for JSON, omitting empty.
func (key PrivateKey) MarshalJSON() ([]byte, error) {
return encodeKeyJSON(key.String())