jpictl: introduce `jpictl dns show` command #27

Merged
amery merged 4 commits from pr-amery-dns-show into main 1 year ago
  1. 56
      cmd/jpictl/dns.go
  2. 12
      go.mod
  3. 26
      go.sum
  4. 12
      pkg/dns/error.go
  5. 63
      pkg/dns/manager.go
  6. 29
      pkg/dns/show.go
  7. 17
      pkg/dns/sync.go

56
cmd/jpictl/dns.go

@ -72,6 +72,29 @@ func populateDNSManager(mgr *dns.Manager, m *cluster.Cluster) error {
return err 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 // Command
var dnsCmd = &cobra.Command{ var dnsCmd = &cobra.Command{
Use: "dns", Use: "dns",
@ -81,13 +104,8 @@ var dnsWriteCmd = &cobra.Command{
Use: "write", Use: "write",
Short: "dns write generates public DNS records", Short: "dns write generates public DNS records",
PreRun: setVerbosity, PreRun: setVerbosity,
RunE: func(_ *cobra.Command, _ []string) error { RunE: func(cmd *cobra.Command, _ []string) error {
m, err := cfg.LoadZones(true) mgr, err := newDNSManagerCommand(cmd, true, false)
if err != nil {
return err
}
mgr, err := newDNSManager(m, nil)
if err != nil { if err != nil {
return err return err
} }
@ -101,18 +119,25 @@ var dnsSyncCmd = &cobra.Command{
Use: "sync", Use: "sync",
Short: "dns sync updates public DNS records", Short: "dns sync updates public DNS records",
PreRun: setVerbosity, PreRun: setVerbosity,
RunE: func(_ *cobra.Command, _ []string) error { RunE: func(cmd *cobra.Command, _ []string) error {
cred, err := dns.DefaultDNSProvider() mgr, err := newDNSManagerCommand(cmd, true, true)
if err != nil { if err != nil {
return err return err
} }
m, err := cfg.LoadZones(true) ctx, cancel := context.WithTimeout(context.Background(), DNSSyncTimeout)
if err != nil { defer cancel()
return err
}
mgr, err := newDNSManager(m, cred) return mgr.Sync(ctx)
},
}
var dnsShowCmd = &cobra.Command{
Use: "show [<name>...]",
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 { if err != nil {
return err return err
} }
@ -120,7 +145,7 @@ var dnsSyncCmd = &cobra.Command{
ctx, cancel := context.WithTimeout(context.Background(), DNSSyncTimeout) ctx, cancel := context.WithTimeout(context.Background(), DNSSyncTimeout)
defer cancel() defer cancel()
return mgr.Sync(ctx) return mgr.Show(ctx, args...)
}, },
} }
@ -129,4 +154,5 @@ func init() {
dnsCmd.AddCommand(dnsWriteCmd) dnsCmd.AddCommand(dnsWriteCmd)
dnsCmd.AddCommand(dnsSyncCmd) dnsCmd.AddCommand(dnsSyncCmd)
dnsCmd.AddCommand(dnsShowCmd)
} }

12
go.mod

@ -5,10 +5,10 @@ go 1.19
require ( require (
asciigoat.org/ini v0.2.5 asciigoat.org/ini v0.2.5
darvaza.org/core v0.10.0 darvaza.org/core v0.10.0
darvaza.org/resolver v0.5.4 darvaza.org/resolver v0.5.8
darvaza.org/sidecar v0.0.2 darvaza.org/sidecar v0.0.8
darvaza.org/slog v0.5.3 darvaza.org/slog v0.5.4
darvaza.org/slog/handlers/discard v0.4.5 darvaza.org/slog/handlers/discard v0.4.6
github.com/gofrs/uuid/v5 v5.0.0 github.com/gofrs/uuid/v5 v5.0.0
github.com/hack-pad/hackpadfs v0.2.1 github.com/hack-pad/hackpadfs v0.2.1
github.com/libdns/cloudflare v0.1.0 github.com/libdns/cloudflare v0.1.0
@ -23,8 +23,8 @@ require (
require ( require (
asciigoat.org/core v0.3.9 // indirect asciigoat.org/core v0.3.9 // indirect
darvaza.org/slog/handlers/filter v0.4.5 // indirect darvaza.org/slog/handlers/filter v0.4.6 // indirect
darvaza.org/slog/handlers/zerolog v0.4.5 // indirect darvaza.org/slog/handlers/zerolog v0.4.6 // indirect
github.com/BurntSushi/toml v1.3.2 // indirect github.com/BurntSushi/toml v1.3.2 // indirect
github.com/chavacava/garif v0.1.0 // indirect github.com/chavacava/garif v0.1.0 // indirect
github.com/fatih/color v1.15.0 // indirect github.com/fatih/color v1.15.0 // indirect

26
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= 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 h1:/nQOSWnMgWW8ZJmv3AEdTgIK+Pg4lkPd+VNejL84q3M=
darvaza.org/core v0.10.0/go.mod h1:72iWMVoXjMHjsPSlctDzA7yKzwXsj5dO+se6F9B3ERs= darvaza.org/core v0.10.0/go.mod h1:72iWMVoXjMHjsPSlctDzA7yKzwXsj5dO+se6F9B3ERs=
darvaza.org/resolver v0.5.4 h1:dlSBNV14yYsp7Kg7ipwYOMNsLbrpeXa8Z0HBTa0Ryxs= darvaza.org/resolver v0.5.8 h1:y410WQ3vRCgE7437eyA55cNMZRP32qYXiokLejkFQeg=
darvaza.org/resolver v0.5.4/go.mod h1:vHMkQUmHjaetFqG2ZLZJiQHsXEMGoTOFGm+NXwfndhE= darvaza.org/resolver v0.5.8/go.mod h1:QnfX+eSZZZbmnE3n+6w4gfqXDH1Gj2MWJVQxhlQDHq8=
darvaza.org/sidecar v0.0.2 h1:4H8FUxc43kkLjxdShN1CoxLTcoHQsZjDVwm7kt6eIK0= darvaza.org/sidecar v0.0.8 h1:vsWK2SZfBYzU999brmT8gzVeCRKbuNQZOVdG5zxjO6U=
darvaza.org/sidecar v0.0.2/go.mod h1:yFC3Qt3j+uS7n9CMpLxwrA68z+FNJhENoenBc9zBJJo= darvaza.org/sidecar v0.0.8/go.mod h1:G96TMPge2jqpKMpaCWc9zwdfaJTmko7dMMWXwDsdocM=
darvaza.org/slog v0.5.3 h1:sQzmZXgqRh9oFMKBwEYrEpucLvKJVZxaxa2bHIA6GJ0= darvaza.org/slog v0.5.4 h1:xzlWVzYh4tuZLnj4A9tOHXfn/SAEIkApXPvK3YDiW9g=
darvaza.org/slog v0.5.3/go.mod h1:59d+yi+C7gn4pDDuwbbOKawERpdXthFFk1Yc+Sv6XB0= darvaza.org/slog v0.5.4/go.mod h1:QFtY3QoQ7xxww85umlEKPcMCNzqNrHYqnj53KehsmBU=
darvaza.org/slog/handlers/discard v0.4.5 h1:RRykOItNolHyiUav57lG/GFBL33rcljoa0nWTpY+T0g= darvaza.org/slog/handlers/discard v0.4.6 h1:TatHJn34y6eKQzNRHSo6lGZnJg4SLOGaWstlvwwOyrE=
darvaza.org/slog/handlers/discard v0.4.5/go.mod h1:HYHfISQjMqcPbPoPZ92ib/u7s9JcXvF6OaygpPFwdF8= darvaza.org/slog/handlers/discard v0.4.6/go.mod h1:AG8WKr7m11NPPzvHW/b8nCT5RvYR9RZcIT/NWUOoMAo=
darvaza.org/slog/handlers/filter v0.4.5 h1:CX1bMzldd67e3y3s3Sh4jK8Lyo0WMvTGBB2lD315jhc= darvaza.org/slog/handlers/filter v0.4.6 h1:AI5AQDyXS534QeXIV54pAKxplA6AVZNr4H2PEmAXT0k=
darvaza.org/slog/handlers/filter v0.4.5/go.mod h1:OuH9rHYg9CIErTJCZliMnFexBfP/HJ9PZ1V1VwSCZ1g= darvaza.org/slog/handlers/filter v0.4.6/go.mod h1:MGTKdlnA/FanOn3GU2mltzwBn41HgSxxNeWUQEKFbl8=
darvaza.org/slog/handlers/zerolog v0.4.5 h1:W4cgGORx4wImr+RL96CWSQGTdkZzKX6YHXPSYJvdoB4= darvaza.org/slog/handlers/zerolog v0.4.6 h1:Di+FXUD2R2pKUrynaidyXzS0WsrEiwbL11LQlQzwZv4=
darvaza.org/slog/handlers/zerolog v0.4.5/go.mod h1:mCoh/mIl8Nsa6Yu1Um7d7cos6RuEJzgaTXaX5LDRUao= 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 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/chavacava/garif v0.1.0 h1:2JHa3hbYf5D9dsgseMKAmc/MZ109otzgNFk5s87H9Pc= 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= 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 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= 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 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY=
golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=

12
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")
)

63
pkg/dns/manager.go

@ -2,15 +2,16 @@ package dns
import ( import (
"context" "context"
"errors"
"io/fs" "io/fs"
"net/netip" "net/netip"
"strings" "strings"
"darvaza.org/core" "darvaza.org/core"
"darvaza.org/slog" "darvaza.org/slog"
"git.jpi.io/amery/jpictl/pkg/cluster" "github.com/libdns/libdns"
"golang.org/x/net/publicsuffix" "golang.org/x/net/publicsuffix"
"git.jpi.io/amery/jpictl/pkg/cluster"
) )
// Manager is a DNS Manager instance // Manager is a DNS Manager instance
@ -71,7 +72,7 @@ func (mgr *Manager) setDefaults() error {
} }
if mgr.domain == "" || mgr.suffix == "" { if mgr.domain == "" || mgr.suffix == "" {
return errors.New("domain not specified") return ErrNoDomain
} }
for _, opt := range opts { for _, opt := range opts {
@ -120,6 +121,62 @@ func NewManager(opts ...ManagerOption) (*Manager, error) {
return mgr, nil 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 // AddHost registers a host
func (mgr *Manager) AddHost(_ context.Context, zone string, id int, func (mgr *Manager) AddHost(_ context.Context, zone string, id int,
active bool, addrs ...netip.Addr) error { active bool, addrs ...netip.Addr) error {

29
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
}

17
pkg/dns/sync.go

@ -2,7 +2,6 @@ package dns
import ( import (
"context" "context"
"errors"
"net/netip" "net/netip"
"sort" "sort"
"strings" "strings"
@ -48,18 +47,14 @@ func SortSyncAddrSlice(s []SyncAddr) []SyncAddr {
return s return s
} }
// GetRecords pulls all the address records on DNS for our domain // GetSyncRecords pulls all the address records on DNS for our domain
func (mgr *Manager) GetRecords(ctx context.Context) ([]SyncAddrRecord, error) { func (mgr *Manager) GetSyncRecords(ctx context.Context) ([]SyncAddrRecord, error) {
if mgr.p == nil { recs, err := mgr.GetRecords(ctx)
return nil, errors.New("dns provider not specified")
}
recs, err := mgr.p.GetRecords(ctx, mgr.domain)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return mgr.filteredRecords(recs) return mgr.filteredSyncRecords(recs)
} }
// AsSyncAddr converts a A or AAAA [libdns.Record] into a [SyncAddr] // 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 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 // filter and convert
cache := make(map[string][]SyncAddr) cache := make(map[string][]SyncAddr)
for _, rr := range recs { 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 // Sync updates all the address records on DNS for our domain
func (mgr *Manager) Sync(ctx context.Context) error { func (mgr *Manager) Sync(ctx context.Context) error {
current, err := mgr.GetRecords(ctx) current, err := mgr.GetSyncRecords(ctx)
if err != nil { if err != nil {
return core.Wrap(err, "GetRecords") return core.Wrap(err, "GetRecords")
} }

Loading…
Cancel
Save