From b0f4be70477ca0399ee6c869ae960d70f7098a85 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Mon, 23 Oct 2023 22:41:17 +0000 Subject: [PATCH 01/10] dns: refactor GetRecords() to allow commands other than sync Signed-off-by: Alejandro Mery --- pkg/dns/error.go | 12 +++++++++ pkg/dns/manager.go | 63 +++++++++++++++++++++++++++++++++++++++++++--- pkg/dns/sync.go | 17 +++++-------- 3 files changed, 78 insertions(+), 14 deletions(-) create mode 100644 pkg/dns/error.go diff --git a/pkg/dns/error.go b/pkg/dns/error.go new file mode 100644 index 0000000..abe9126 --- /dev/null +++ b/pkg/dns/error.go @@ -0,0 +1,12 @@ +package dns + +import "errors" + +var ( + // ErrNoDNSProvider indicates a [libdns.Provider] wasn't assigned + // to the [Manager] + ErrNoDNSProvider = errors.New("dns provider not specified") + + // ErrNoDomain indicates a domain wasn't specified + ErrNoDomain = errors.New("domain not specified") +) diff --git a/pkg/dns/manager.go b/pkg/dns/manager.go index 8e29252..24dd62f 100644 --- a/pkg/dns/manager.go +++ b/pkg/dns/manager.go @@ -2,15 +2,16 @@ package dns import ( "context" - "errors" "io/fs" "net/netip" "strings" "darvaza.org/core" "darvaza.org/slog" - "git.jpi.io/amery/jpictl/pkg/cluster" + "github.com/libdns/libdns" "golang.org/x/net/publicsuffix" + + "git.jpi.io/amery/jpictl/pkg/cluster" ) // Manager is a DNS Manager instance @@ -71,7 +72,7 @@ func (mgr *Manager) setDefaults() error { } if mgr.domain == "" || mgr.suffix == "" { - return errors.New("domain not specified") + return ErrNoDomain } for _, opt := range opts { @@ -120,6 +121,62 @@ func NewManager(opts ...ManagerOption) (*Manager, error) { return mgr, nil } +// GetRecords pulls all the address records on DNS for our domain, +// optionally only those matching the given names. +func (mgr *Manager) GetRecords(ctx context.Context, names ...string) ([]libdns.Record, error) { + if mgr.p == nil { + return nil, ErrNoDNSProvider + } + + recs, err := mgr.p.GetRecords(ctx, mgr.domain) + switch { + case err != nil: + // failed + return nil, err + case len(recs) == 0: + // empty + return []libdns.Record{}, nil + case mgr.suffix == "" && len(names) == 0: + // unfiltered + return recs, nil + default: + // filtered + recs = mgr.filterRecords(recs, names...) + return recs, nil + } +} + +func (mgr *Manager) filterRecords(recs []libdns.Record, names ...string) []libdns.Record { + out := make([]libdns.Record, 0, len(recs)) + for _, rr := range recs { + name, ok := mgr.matchSuffix(rr) + switch { + case !ok: + // skip, wrong subdomain + continue + case len(names) == 0: + // unfiltered, take it + case !core.SliceContains(names, name): + // skip, not one of the requested names + continue + } + + out = append(out, rr) + } + + return out +} + +func (mgr *Manager) matchSuffix(rr libdns.Record) (string, bool) { + if mgr.suffix == "" { + // no suffix + return rr.Name, true + } + + // remove suffix + return strings.CutSuffix(rr.Name, mgr.suffix) +} + // AddHost registers a host func (mgr *Manager) AddHost(_ context.Context, zone string, id int, active bool, addrs ...netip.Addr) error { diff --git a/pkg/dns/sync.go b/pkg/dns/sync.go index 05b73cb..712022c 100644 --- a/pkg/dns/sync.go +++ b/pkg/dns/sync.go @@ -2,7 +2,6 @@ package dns import ( "context" - "errors" "net/netip" "sort" "strings" @@ -48,18 +47,14 @@ func SortSyncAddrSlice(s []SyncAddr) []SyncAddr { return s } -// GetRecords pulls all the address records on DNS for our domain -func (mgr *Manager) GetRecords(ctx context.Context) ([]SyncAddrRecord, error) { - if mgr.p == nil { - return nil, errors.New("dns provider not specified") - } - - recs, err := mgr.p.GetRecords(ctx, mgr.domain) +// GetSyncRecords pulls all the address records on DNS for our domain +func (mgr *Manager) GetSyncRecords(ctx context.Context) ([]SyncAddrRecord, error) { + recs, err := mgr.GetRecords(ctx) if err != nil { return nil, err } - return mgr.filteredRecords(recs) + return mgr.filteredSyncRecords(recs) } // AsSyncAddr converts a A or AAAA [libdns.Record] into a [SyncAddr] @@ -94,7 +89,7 @@ func (mgr *Manager) AsSyncAddr(rr libdns.Record) (SyncAddr, bool, error) { return out, true, nil } -func (mgr *Manager) filteredRecords(recs []libdns.Record) ([]SyncAddrRecord, error) { +func (mgr *Manager) filteredSyncRecords(recs []libdns.Record) ([]SyncAddrRecord, error) { // filter and convert cache := make(map[string][]SyncAddr) for _, rr := range recs { @@ -137,7 +132,7 @@ func (mgr *Manager) filteredRecords(recs []libdns.Record) ([]SyncAddrRecord, err // Sync updates all the address records on DNS for our domain func (mgr *Manager) Sync(ctx context.Context) error { - current, err := mgr.GetRecords(ctx) + current, err := mgr.GetSyncRecords(ctx) if err != nil { return core.Wrap(err, "GetRecords") } From c578990f8c67dff92d2dcdc9d65192b71834b75b Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Mon, 23 Oct 2023 21:35:59 +0000 Subject: [PATCH 02/10] jpictl: refactor dns command initialization Signed-off-by: Alejandro Mery --- cmd/jpictl/dns.go | 46 +++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/cmd/jpictl/dns.go b/cmd/jpictl/dns.go index 4431801..c3788e0 100644 --- a/cmd/jpictl/dns.go +++ b/cmd/jpictl/dns.go @@ -72,6 +72,29 @@ func populateDNSManager(mgr *dns.Manager, m *cluster.Cluster) error { return err } +// revive:disable:flag-parameter +func newDNSManagerCommand(_ *cobra.Command, + resolve bool, withCredentials bool) (*dns.Manager, error) { + // revive:enable:flag-parameter + var cred dns.Provider + + if withCredentials { + var err error + + cred, err = dns.DefaultDNSProvider() + if err != nil { + return nil, err + } + } + + m, err := cfg.LoadZones(resolve) + if err != nil { + return nil, err + } + + return newDNSManager(m, cred) +} + // Command var dnsCmd = &cobra.Command{ Use: "dns", @@ -81,13 +104,8 @@ var dnsWriteCmd = &cobra.Command{ Use: "write", Short: "dns write generates public DNS records", PreRun: setVerbosity, - RunE: func(_ *cobra.Command, _ []string) error { - m, err := cfg.LoadZones(true) - if err != nil { - return err - } - - mgr, err := newDNSManager(m, nil) + RunE: func(cmd *cobra.Command, _ []string) error { + mgr, err := newDNSManagerCommand(cmd, true, false) if err != nil { return err } @@ -101,18 +119,8 @@ var dnsSyncCmd = &cobra.Command{ Use: "sync", Short: "dns sync updates public DNS records", PreRun: setVerbosity, - RunE: func(_ *cobra.Command, _ []string) error { - cred, err := dns.DefaultDNSProvider() - if err != nil { - return err - } - - m, err := cfg.LoadZones(true) - if err != nil { - return err - } - - mgr, err := newDNSManager(m, cred) + RunE: func(cmd *cobra.Command, _ []string) error { + mgr, err := newDNSManagerCommand(cmd, true, true) if err != nil { return err } From 440dcde50a19f741b806f96f8ac17a9ccb0f86df Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Mon, 23 Oct 2023 23:13:59 +0000 Subject: [PATCH 03/10] jpictl: introduce `jpictl dns show` command to list dns entries Signed-off-by: Alejandro Mery --- cmd/jpictl/dns.go | 18 ++++++++++++++++++ pkg/dns/show.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 pkg/dns/show.go diff --git a/cmd/jpictl/dns.go b/cmd/jpictl/dns.go index c3788e0..9023730 100644 --- a/cmd/jpictl/dns.go +++ b/cmd/jpictl/dns.go @@ -132,9 +132,27 @@ var dnsSyncCmd = &cobra.Command{ }, } +var dnsShowCmd = &cobra.Command{ + Use: "show [...]", + Short: "dns show lists entries on DNS for our domain", + PreRun: setVerbosity, + RunE: func(cmd *cobra.Command, args []string) error { + mgr, err := newDNSManagerCommand(cmd, true, true) + if err != nil { + return err + } + + ctx, cancel := context.WithTimeout(context.Background(), DNSSyncTimeout) + defer cancel() + + return mgr.Show(ctx, args...) + }, +} + func init() { rootCmd.AddCommand(dnsCmd) dnsCmd.AddCommand(dnsWriteCmd) dnsCmd.AddCommand(dnsSyncCmd) + dnsCmd.AddCommand(dnsShowCmd) } diff --git a/pkg/dns/show.go b/pkg/dns/show.go new file mode 100644 index 0000000..24cba56 --- /dev/null +++ b/pkg/dns/show.go @@ -0,0 +1,29 @@ +package dns + +import ( + "context" + "fmt" + "time" + + "darvaza.org/core" +) + +// Show shows current DNS entries +func (mgr *Manager) Show(ctx context.Context, names ...string) error { + recs, err := mgr.GetRecords(ctx, names...) + if err != nil { + return core.Wrap(err, "GetRecords") + } + + for _, rr := range recs { + _, _ = fmt.Printf("%s\t%v\tIN\t%s\t%s\t; %s\n", + rr.Name, + int(rr.TTL/time.Second), + rr.Type, + rr.Value, + rr.ID) + } + + _, _ = fmt.Printf("; %v records\n", len(recs)) + return nil +} From 66178923a345d38c6e504639b7fcfcd1300751b9 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Tue, 24 Oct 2023 11:00:52 +0000 Subject: [PATCH 04/10] chore: update darvaza.org/resolver to support the darvaza.org/core update Signed-off-by: Alejandro Mery --- go.mod | 12 ++++++------ go.sum | 26 +++++++++++++------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/go.mod b/go.mod index 9d89c42..90f9d36 100644 --- a/go.mod +++ b/go.mod @@ -5,10 +5,10 @@ go 1.19 require ( asciigoat.org/ini v0.2.5 darvaza.org/core v0.10.0 - darvaza.org/resolver v0.5.4 - darvaza.org/sidecar v0.0.2 - darvaza.org/slog v0.5.3 - darvaza.org/slog/handlers/discard v0.4.5 + darvaza.org/resolver v0.5.8 + darvaza.org/sidecar v0.0.8 + darvaza.org/slog v0.5.4 + darvaza.org/slog/handlers/discard v0.4.6 github.com/gofrs/uuid/v5 v5.0.0 github.com/hack-pad/hackpadfs v0.2.1 github.com/libdns/cloudflare v0.1.0 @@ -23,8 +23,8 @@ require ( require ( asciigoat.org/core v0.3.9 // indirect - darvaza.org/slog/handlers/filter v0.4.5 // indirect - darvaza.org/slog/handlers/zerolog v0.4.5 // indirect + darvaza.org/slog/handlers/filter v0.4.6 // indirect + darvaza.org/slog/handlers/zerolog v0.4.6 // indirect github.com/BurntSushi/toml v1.3.2 // indirect github.com/chavacava/garif v0.1.0 // indirect github.com/fatih/color v1.15.0 // indirect diff --git a/go.sum b/go.sum index 444d649..25dc06d 100644 --- a/go.sum +++ b/go.sum @@ -4,18 +4,18 @@ asciigoat.org/ini v0.2.5 h1:4gRIp9rU+XQt8+HMqZO5R7GavMv9Yl2+N+je6djDIAE= asciigoat.org/ini v0.2.5/go.mod h1:gmXzJ9XFqf1NLk5nQkj04USQ4tMtdRJHNQX6vp3DzjU= darvaza.org/core v0.10.0 h1:/nQOSWnMgWW8ZJmv3AEdTgIK+Pg4lkPd+VNejL84q3M= darvaza.org/core v0.10.0/go.mod h1:72iWMVoXjMHjsPSlctDzA7yKzwXsj5dO+se6F9B3ERs= -darvaza.org/resolver v0.5.4 h1:dlSBNV14yYsp7Kg7ipwYOMNsLbrpeXa8Z0HBTa0Ryxs= -darvaza.org/resolver v0.5.4/go.mod h1:vHMkQUmHjaetFqG2ZLZJiQHsXEMGoTOFGm+NXwfndhE= -darvaza.org/sidecar v0.0.2 h1:4H8FUxc43kkLjxdShN1CoxLTcoHQsZjDVwm7kt6eIK0= -darvaza.org/sidecar v0.0.2/go.mod h1:yFC3Qt3j+uS7n9CMpLxwrA68z+FNJhENoenBc9zBJJo= -darvaza.org/slog v0.5.3 h1:sQzmZXgqRh9oFMKBwEYrEpucLvKJVZxaxa2bHIA6GJ0= -darvaza.org/slog v0.5.3/go.mod h1:59d+yi+C7gn4pDDuwbbOKawERpdXthFFk1Yc+Sv6XB0= -darvaza.org/slog/handlers/discard v0.4.5 h1:RRykOItNolHyiUav57lG/GFBL33rcljoa0nWTpY+T0g= -darvaza.org/slog/handlers/discard v0.4.5/go.mod h1:HYHfISQjMqcPbPoPZ92ib/u7s9JcXvF6OaygpPFwdF8= -darvaza.org/slog/handlers/filter v0.4.5 h1:CX1bMzldd67e3y3s3Sh4jK8Lyo0WMvTGBB2lD315jhc= -darvaza.org/slog/handlers/filter v0.4.5/go.mod h1:OuH9rHYg9CIErTJCZliMnFexBfP/HJ9PZ1V1VwSCZ1g= -darvaza.org/slog/handlers/zerolog v0.4.5 h1:W4cgGORx4wImr+RL96CWSQGTdkZzKX6YHXPSYJvdoB4= -darvaza.org/slog/handlers/zerolog v0.4.5/go.mod h1:mCoh/mIl8Nsa6Yu1Um7d7cos6RuEJzgaTXaX5LDRUao= +darvaza.org/resolver v0.5.8 h1:y410WQ3vRCgE7437eyA55cNMZRP32qYXiokLejkFQeg= +darvaza.org/resolver v0.5.8/go.mod h1:QnfX+eSZZZbmnE3n+6w4gfqXDH1Gj2MWJVQxhlQDHq8= +darvaza.org/sidecar v0.0.8 h1:vsWK2SZfBYzU999brmT8gzVeCRKbuNQZOVdG5zxjO6U= +darvaza.org/sidecar v0.0.8/go.mod h1:G96TMPge2jqpKMpaCWc9zwdfaJTmko7dMMWXwDsdocM= +darvaza.org/slog v0.5.4 h1:xzlWVzYh4tuZLnj4A9tOHXfn/SAEIkApXPvK3YDiW9g= +darvaza.org/slog v0.5.4/go.mod h1:QFtY3QoQ7xxww85umlEKPcMCNzqNrHYqnj53KehsmBU= +darvaza.org/slog/handlers/discard v0.4.6 h1:TatHJn34y6eKQzNRHSo6lGZnJg4SLOGaWstlvwwOyrE= +darvaza.org/slog/handlers/discard v0.4.6/go.mod h1:AG8WKr7m11NPPzvHW/b8nCT5RvYR9RZcIT/NWUOoMAo= +darvaza.org/slog/handlers/filter v0.4.6 h1:AI5AQDyXS534QeXIV54pAKxplA6AVZNr4H2PEmAXT0k= +darvaza.org/slog/handlers/filter v0.4.6/go.mod h1:MGTKdlnA/FanOn3GU2mltzwBn41HgSxxNeWUQEKFbl8= +darvaza.org/slog/handlers/zerolog v0.4.6 h1:Di+FXUD2R2pKUrynaidyXzS0WsrEiwbL11LQlQzwZv4= +darvaza.org/slog/handlers/zerolog v0.4.6/go.mod h1:r5B9/FQ256R3Wo5vFLOa2YarM2P8WOjVjFn8xHikNjk= github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/chavacava/garif v0.1.0 h1:2JHa3hbYf5D9dsgseMKAmc/MZ109otzgNFk5s87H9Pc= @@ -96,7 +96,7 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc= golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= -golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI= golang.org/x/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY= golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= From 7dac96f4744a19fd2a760987994a48c0e6025b78 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Tue, 24 Oct 2023 15:14:12 +0000 Subject: [PATCH 05/10] dns/show: refactor Record formatting Signed-off-by: Alejandro Mery --- pkg/dns/show.go | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/pkg/dns/show.go b/pkg/dns/show.go index 24cba56..c29a3f0 100644 --- a/pkg/dns/show.go +++ b/pkg/dns/show.go @@ -1,29 +1,53 @@ package dns import ( + "bytes" "context" "fmt" + "io" + "os" "time" "darvaza.org/core" + "github.com/libdns/libdns" ) // Show shows current DNS entries func (mgr *Manager) Show(ctx context.Context, names ...string) error { + var buf bytes.Buffer + recs, err := mgr.GetRecords(ctx, names...) if err != nil { return core.Wrap(err, "GetRecords") } for _, rr := range recs { - _, _ = fmt.Printf("%s\t%v\tIN\t%s\t%s\t; %s\n", - rr.Name, - int(rr.TTL/time.Second), - rr.Type, - rr.Value, - rr.ID) + _ = fmtRecord(&buf, rr) + _, _ = buf.WriteRune('\n') + } + _, _ = fmt.Fprintf(&buf, "; %v records\n", len(recs)) + + _, err = buf.WriteTo(os.Stdout) + return err +} + +func fmtRecord(w io.Writer, rr libdns.Record) error { + ttl := int(rr.TTL / time.Second) + if ttl < 1 { + ttl = 1 } - _, _ = fmt.Printf("; %v records\n", len(recs)) - return nil + _, err := fmt.Fprintf(w, "%s\t%v\tIN\t%s\t%s", + rr.Name, + ttl, + rr.Type, + rr.Value) + + if err == nil { + if rr.ID != "" { + _, err = fmt.Fprintf(w, "\t; %s", rr.ID) + } + } + + return err } From 356322bc94cde05139ecfff30ddc83dadab528f9 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Thu, 26 Oct 2023 18:34:42 +0000 Subject: [PATCH 06/10] dns/show: introduce writeRecords() helper to print a whole []libdns.Record Signed-off-by: Alejandro Mery --- pkg/dns/show.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pkg/dns/show.go b/pkg/dns/show.go index c29a3f0..d133a2c 100644 --- a/pkg/dns/show.go +++ b/pkg/dns/show.go @@ -14,20 +14,24 @@ import ( // Show shows current DNS entries func (mgr *Manager) Show(ctx context.Context, names ...string) error { - var buf bytes.Buffer - recs, err := mgr.GetRecords(ctx, names...) if err != nil { return core.Wrap(err, "GetRecords") } + return writeRecords(recs, os.Stdout) +} + +func writeRecords(recs []libdns.Record, w io.Writer) error { + var buf bytes.Buffer + for _, rr := range recs { _ = fmtRecord(&buf, rr) _, _ = buf.WriteRune('\n') } _, _ = fmt.Fprintf(&buf, "; %v records\n", len(recs)) - _, err = buf.WriteTo(os.Stdout) + _, err := buf.WriteTo(w) return err } From 9da49f2d86c55bcc5315ee0616c31b363c1a9f91 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Tue, 24 Oct 2023 17:12:53 +0000 Subject: [PATCH 07/10] dns/show: sort records v2: change Name to lower case and Type to upper case before comparing Signed-off-by: Alejandro Mery --- pkg/dns/record.go | 48 +++++++++++++++++++++++++++++++++++++++++++++++ pkg/dns/show.go | 1 + 2 files changed, 49 insertions(+) diff --git a/pkg/dns/record.go b/pkg/dns/record.go index 2b98905..1b9fc51 100644 --- a/pkg/dns/record.go +++ b/pkg/dns/record.go @@ -6,6 +6,7 @@ import ( "io" "net/netip" "sort" + "strings" "time" "darvaza.org/core" @@ -38,6 +39,53 @@ func SortAddrRecords(s []AddrRecord) []AddrRecord { return s } +// SortRecords sorts a slice of [libdns.Record], by Name, Type and Value +func SortRecords(s []libdns.Record) []libdns.Record { + sort.Slice(s, func(i, j int) bool { + return lessRecord(s[i], s[j]) + }) + return s +} + +func lessRecord(a, b libdns.Record) bool { + aName := strings.ToLower(a.Name) + bName := strings.ToLower(b.Name) + + switch { + case aName < bName: + return true + case aName > bName: + return false + } + + aType := strings.ToUpper(a.Type) + bType := strings.ToUpper(b.Type) + + switch { + case aType < bType: + return true + case aType > bType: + return false + case aType == "A", aType == "AAAA": + // IP Addresses + var aa, ba netip.Addr + + switch { + case aa.UnmarshalText([]byte(a.Value)) != nil: + // bad address on a + return true + case ba.UnmarshalText([]byte(b.Value)) != nil: + // bad address on b + return false + default: + return aa.Less(ba) + } + default: + // text + return a.Value < b.Value + } +} + // SortRegions sorts regions. first by length those 3-character // or shorter, and then by length. It's mostly aimed at // supporting ISO-3166 order diff --git a/pkg/dns/show.go b/pkg/dns/show.go index d133a2c..e6a7fbb 100644 --- a/pkg/dns/show.go +++ b/pkg/dns/show.go @@ -19,6 +19,7 @@ func (mgr *Manager) Show(ctx context.Context, names ...string) error { return core.Wrap(err, "GetRecords") } + SortRecords(recs) return writeRecords(recs, os.Stdout) } From 557f15657934317854f469d8106abcf22f60bfdb Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Thu, 26 Oct 2023 18:36:38 +0000 Subject: [PATCH 08/10] dns: refactor asSyncRecords() for direct access of the unsorted map Signed-off-by: Alejandro Mery --- pkg/dns/sync.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/pkg/dns/sync.go b/pkg/dns/sync.go index 712022c..57c6d83 100644 --- a/pkg/dns/sync.go +++ b/pkg/dns/sync.go @@ -54,7 +54,7 @@ func (mgr *Manager) GetSyncRecords(ctx context.Context) ([]SyncAddrRecord, error return nil, err } - return mgr.filteredSyncRecords(recs) + return mgr.asSyncRecords(recs) } // AsSyncAddr converts a A or AAAA [libdns.Record] into a [SyncAddr] @@ -89,9 +89,9 @@ func (mgr *Manager) AsSyncAddr(rr libdns.Record) (SyncAddr, bool, error) { return out, true, nil } -func (mgr *Manager) filteredSyncRecords(recs []libdns.Record) ([]SyncAddrRecord, error) { +func (mgr *Manager) asSyncRecordsMap(recs []libdns.Record) map[string][]SyncAddr { // filter and convert - cache := make(map[string][]SyncAddr) + out := make(map[string][]SyncAddr) for _, rr := range recs { addr, ok, err := mgr.AsSyncAddr(rr) switch { @@ -106,9 +106,14 @@ func (mgr *Manager) filteredSyncRecords(recs []libdns.Record) ([]SyncAddrRecord, Print() case ok: // store - cache[rr.Name] = append(cache[rr.Name], addr) + out[rr.Name] = append(out[rr.Name], addr) } } + return out +} + +func (mgr *Manager) asSyncRecords(recs []libdns.Record) ([]SyncAddrRecord, error) { + cache := mgr.asSyncRecordsMap(recs) // prepare records out := make([]SyncAddrRecord, len(cache)) From 052f89152cabd7217d82693bdc3772e824140a4d Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Thu, 26 Oct 2023 18:38:36 +0000 Subject: [PATCH 09/10] jpictl/dns: introduce add command to register new machines Signed-off-by: Alejandro Mery --- cmd/jpictl/dns.go | 35 ++++++++++++++++++++++++ pkg/dns/add.go | 69 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 pkg/dns/add.go diff --git a/cmd/jpictl/dns.go b/cmd/jpictl/dns.go index 9023730..f3a2cbe 100644 --- a/cmd/jpictl/dns.go +++ b/cmd/jpictl/dns.go @@ -2,9 +2,11 @@ package main import ( "context" + "net/netip" "os" "time" + "darvaza.org/core" "github.com/spf13/cobra" "git.jpi.io/amery/jpictl/pkg/cluster" @@ -149,10 +151,43 @@ var dnsShowCmd = &cobra.Command{ }, } +var dnsAddCmd = &cobra.Command{ + Use: "add ", + Short: "dns add registers a new machine on the public DNS", + Args: cobra.MinimumNArgs(2), + PreRun: setVerbosity, + RunE: func(cmd *cobra.Command, args []string) error { + var addrs []netip.Addr + + for _, s := range args[1:] { + addr, err := core.ParseAddr(s) + switch { + case err != nil: + return core.Wrap(err, s) + case !addr.IsValid(), addr.IsUnspecified(), addr.IsPrivate(), addr.IsMulticast(): + return core.Wrap(core.ErrInvalid, s) + default: + addrs = append(addrs, addr) + } + } + + mgr, err := newDNSManagerCommand(cmd, true, true) + if err != nil { + return err + } + + ctx, cancel := context.WithTimeout(context.Background(), DNSSyncTimeout) + defer cancel() + + return mgr.Add(ctx, args[0], addrs...) + }, +} + func init() { rootCmd.AddCommand(dnsCmd) dnsCmd.AddCommand(dnsWriteCmd) dnsCmd.AddCommand(dnsSyncCmd) dnsCmd.AddCommand(dnsShowCmd) + dnsCmd.AddCommand(dnsAddCmd) } diff --git a/pkg/dns/add.go b/pkg/dns/add.go new file mode 100644 index 0000000..3c9c6c4 --- /dev/null +++ b/pkg/dns/add.go @@ -0,0 +1,69 @@ +package dns + +import ( + "context" + "net/netip" + "os" + "time" + + "darvaza.org/core" + "github.com/libdns/libdns" +) + +// Add adds a machine to the DNS records +func (mgr *Manager) Add(ctx context.Context, name string, addrs ...netip.Addr) error { + // TODO: validate name + + cur, err := mgr.GetRecords(ctx, name) + if err != nil { + return core.Wrap(err, "GetRecords") + } + + // merge []SyncAddr for name + s := mgr.asSyncRecordsMap(cur)[name+mgr.suffix] + for _, addr := range addrs { + s = AppendSyncAddr(s, addr) + } + + return mgr.addSyncAddr(ctx, name, s) +} + +func (mgr *Manager) addSyncAddr(ctx context.Context, name string, s []SyncAddr) error { + var recs []libdns.Record + + for _, a := range s { + recs = append(recs, libdns.Record{ + ID: a.ID, + Name: name + mgr.suffix, + Type: core.IIf(a.Addr.Is6(), "AAAA", "A"), + TTL: time.Second, + Value: a.Addr.String(), + }) + } + + SortRecords(recs) + err := writeRecords(recs, os.Stdout) + if err != nil { + return err + } + + _, err = mgr.p.SetRecords(ctx, mgr.domain, recs) + return err +} + +// AppendSyncAddr appends a [netip.Addr] to a [SyncAddr] slice +// if the address is new. +func AppendSyncAddr(s []SyncAddr, addr netip.Addr) []SyncAddr { + for _, se := range s { + if se.Addr.Compare(addr) == 0 { + // found + return s + } + } + + s = append(s, SyncAddr{ + Addr: addr, + TTL: time.Second, + }) + return s +} From 142ea005773f27d56951675e3f8905337b3cbd67 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Thu, 26 Oct 2023 22:11:05 +0000 Subject: [PATCH 10/10] wireguard: fix KeyPair.Validate() PrivateKey and PublicKey are now fixed length arrays, so testing for len 0 is invalid Signed-off-by: Alejandro Mery --- pkg/wireguard/keys.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/pkg/wireguard/keys.go b/pkg/wireguard/keys.go index 532f64e..fe21f98 100644 --- a/pkg/wireguard/keys.go +++ b/pkg/wireguard/keys.go @@ -183,20 +183,14 @@ type KeyPair struct { // Validate checks the PublicKey matches the PrivateKey, // and sets the PublicKey if missing func (kp *KeyPair) Validate() error { - keyLen := len(kp.PrivateKey) - pubLen := len(kp.PublicKey) - switch { - case keyLen != PrivateKeySize: - // bad private key + case kp.PrivateKey.IsZero(): + // no private key return ErrInvalidPrivateKey - case pubLen == 0: + case kp.PublicKey.IsZero(): // no public key, set it kp.PublicKey = kp.PrivateKey.Public() return nil - case pubLen != PublicKeySize: - // bad public key - return ErrInvalidPublicKey case !kp.PrivateKey.Public().Equal(kp.PublicKey): // wrong public key return ErrInvalidPublicKey