From a5fab23e8f38d6cb653e053a7d3014be2dfdf67b Mon Sep 17 00:00:00 2001 From: Andrew Dunham Date: Tue, 27 Sep 2022 19:30:39 -0400 Subject: [PATCH] net/dns: format OSConfig correctly with no pointers (#5766) Fixes tailscale/tailscale#5669 Signed-off-by: Andrew Dunham --- net/dns/osconfig.go | 41 +++++++++++++++++++++++++++++++++++++ net/dns/osconfig_test.go | 44 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 net/dns/osconfig_test.go diff --git a/net/dns/osconfig.go b/net/dns/osconfig.go index 06b223e15..08be94e01 100644 --- a/net/dns/osconfig.go +++ b/net/dns/osconfig.go @@ -5,9 +5,12 @@ package dns import ( + "bufio" "errors" + "fmt" "net/netip" + "tailscale.com/types/logger" "tailscale.com/util/dnsname" ) @@ -97,6 +100,44 @@ func (a OSConfig) Equal(b OSConfig) bool { return true } +// Format implements the fmt.Formatter interface to ensure that Hosts is +// printed correctly (i.e. not as a bunch of pointers). +// +// Fixes https://github.com/tailscale/tailscale/issues/5669 +func (a OSConfig) Format(f fmt.State, verb rune) { + logger.ArgWriter(func(w *bufio.Writer) { + w.WriteString(`{Nameservers:[`) + for i, ns := range a.Nameservers { + if i != 0 { + w.WriteString(" ") + } + fmt.Fprintf(w, "%+v", ns) + } + w.WriteString(`] SearchDomains:[`) + for i, domain := range a.SearchDomains { + if i != 0 { + w.WriteString(" ") + } + fmt.Fprintf(w, "%+v", domain) + } + w.WriteString(`] MatchDomains:[`) + for i, domain := range a.MatchDomains { + if i != 0 { + w.WriteString(" ") + } + fmt.Fprintf(w, "%+v", domain) + } + w.WriteString(`] Hosts:[`) + for i, host := range a.Hosts { + if i != 0 { + w.WriteString(" ") + } + fmt.Fprintf(w, "%+v", host) + } + w.WriteString(`]}`) + }).Format(f, verb) +} + // ErrGetBaseConfigNotSupported is the error // OSConfigurator.GetBaseConfig returns when the OSConfigurator // doesn't support reading the underlying configuration out of the OS. diff --git a/net/dns/osconfig_test.go b/net/dns/osconfig_test.go new file mode 100644 index 000000000..4ad559af2 --- /dev/null +++ b/net/dns/osconfig_test.go @@ -0,0 +1,44 @@ +// Copyright (c) 2022 Tailscale Inc & AUTHORS All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package dns + +import ( + "fmt" + "net/netip" + "testing" + + "tailscale.com/util/dnsname" +) + +func TestOSConfigPrintable(t *testing.T) { + ocfg := OSConfig{ + Hosts: []*HostEntry{ + { + Addr: netip.AddrFrom4([4]byte{100, 1, 2, 3}), + Hosts: []string{"server", "client"}, + }, + { + Addr: netip.AddrFrom4([4]byte{100, 1, 2, 4}), + Hosts: []string{"otherhost"}, + }, + }, + Nameservers: []netip.Addr{ + netip.AddrFrom4([4]byte{8, 8, 8, 8}), + }, + SearchDomains: []dnsname.FQDN{ + dnsname.FQDN("foo.beta.tailscale.net."), + dnsname.FQDN("bar.beta.tailscale.net."), + }, + MatchDomains: []dnsname.FQDN{ + dnsname.FQDN("ts.com."), + }, + } + s := fmt.Sprintf("%+v", ocfg) + + const expected = `{Nameservers:[8.8.8.8] SearchDomains:[foo.beta.tailscale.net. bar.beta.tailscale.net.] MatchDomains:[ts.com.] Hosts:[&{Addr:100.1.2.3 Hosts:[server client]} &{Addr:100.1.2.4 Hosts:[otherhost]}]}` + if s != expected { + t.Errorf("format mismatch:\n got: %s\n want: %s", s, expected) + } +}