mirror of
https://github.com/mjl-/mox.git
synced 2025-07-14 05:34:38 +03:00
switch to slog.Logger for logging, for easier reuse of packages by external software
we don't want external software to include internal details like mlog. slog.Logger is/will be the standard. we still have mlog for its helper functions, and its handler that logs in concise logfmt used by mox. packages that are not meant for reuse still pass around mlog.Log for convenience. we use golang.org/x/exp/slog because we also support the previous Go toolchain version. with the next Go release, we'll switch to the builtin slog.
This commit is contained in:
@ -16,6 +16,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"golang.org/x/exp/slog"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
|
||||
@ -118,7 +120,7 @@ type Client struct {
|
||||
w *bufio.Writer
|
||||
tr *moxio.TraceReader // Kept for changing trace levels between cmd/auth/data.
|
||||
tw *moxio.TraceWriter
|
||||
log *mlog.Log
|
||||
log mlog.Log
|
||||
lastlog time.Time // For adding delta timestamps between log lines.
|
||||
cmds []string // Last or active command, for generating errors and metrics.
|
||||
cmdStart time.Time // Start of command.
|
||||
@ -237,7 +239,7 @@ type Opts struct {
|
||||
// with opportunistic TLS without PKIX verification by default. Recipient domains
|
||||
// can opt-in to PKIX verification by publishing an MTA-STS policy, or opt-in to
|
||||
// DANE verification by publishing DNSSEC-protected TLSA records in DNS.
|
||||
func New(ctx context.Context, log *mlog.Log, conn net.Conn, tlsMode TLSMode, tlsVerifyPKIX bool, ehloHostname, remoteHostname dns.Domain, opts Opts) (*Client, error) {
|
||||
func New(ctx context.Context, elog *slog.Logger, conn net.Conn, tlsMode TLSMode, tlsVerifyPKIX bool, ehloHostname, remoteHostname dns.Domain, opts Opts) (*Client, error) {
|
||||
ensureResult := func(r *tlsrpt.Result) *tlsrpt.Result {
|
||||
if r == nil {
|
||||
return &tlsrpt.Result{}
|
||||
@ -259,10 +261,10 @@ func New(ctx context.Context, log *mlog.Log, conn net.Conn, tlsMode TLSMode, tls
|
||||
recipientDomainResult: ensureResult(opts.RecipientDomainResult),
|
||||
hostResult: ensureResult(opts.HostResult),
|
||||
}
|
||||
c.log = log.Fields(mlog.Field("smtpclient", "")).MoreFields(func() []mlog.Pair {
|
||||
c.log = mlog.New("smtpclient", elog).WithFunc(func() []slog.Attr {
|
||||
now := time.Now()
|
||||
l := []mlog.Pair{
|
||||
mlog.Field("delta", now.Sub(c.lastlog)),
|
||||
l := []slog.Attr{
|
||||
slog.Duration("delta", now.Sub(c.lastlog)),
|
||||
}
|
||||
c.lastlog = now
|
||||
return l
|
||||
@ -280,7 +282,7 @@ func New(ctx context.Context, log *mlog.Log, conn net.Conn, tlsMode TLSMode, tls
|
||||
c.tlsResultAdd(1, 0, nil)
|
||||
c.conn = tlsconn
|
||||
tlsversion, ciphersuite := mox.TLSInfo(tlsconn)
|
||||
c.log.Debug("tls client handshake done", mlog.Field("tls", tlsversion), mlog.Field("ciphersuite", ciphersuite), mlog.Field("servername", remoteHostname))
|
||||
c.log.Debug("tls client handshake done", slog.String("tls", tlsversion), slog.String("ciphersuite", ciphersuite), slog.Any("servername", remoteHostname))
|
||||
c.tls = true
|
||||
} else {
|
||||
c.conn = conn
|
||||
@ -329,8 +331,8 @@ func (c *Client) tlsConfig() *tls.Config {
|
||||
// DANE verification.
|
||||
// daneRecords can be non-nil and empty, that's intended.
|
||||
if c.daneRecords != nil {
|
||||
verified, record, err := dane.Verify(c.log, c.daneRecords, cs, c.remoteHostname, c.daneMoreHostnames)
|
||||
c.log.Debugx("dane verification", err, mlog.Field("verified", verified), mlog.Field("record", record))
|
||||
verified, record, err := dane.Verify(c.log.Logger, c.daneRecords, cs, c.remoteHostname, c.daneMoreHostnames)
|
||||
c.log.Debugx("dane verification", err, slog.Bool("verified", verified), slog.Any("record", record))
|
||||
if verified {
|
||||
if c.daneVerifiedRecord != nil {
|
||||
*c.daneVerifiedRecord = record
|
||||
@ -426,7 +428,7 @@ func (c *Client) xerrorf(permanent bool, code int, secode, lastLine, format stri
|
||||
type timeoutWriter struct {
|
||||
conn net.Conn
|
||||
timeout time.Duration
|
||||
log *mlog.Log
|
||||
log mlog.Log
|
||||
}
|
||||
|
||||
func (w timeoutWriter) Write(buf []byte) (int, error) {
|
||||
@ -445,7 +447,7 @@ func (c *Client) readline() (string, error) {
|
||||
c.log.Errorx("setting read deadline", err)
|
||||
}
|
||||
|
||||
line, err := bufs.Readline(c.r)
|
||||
line, err := bufs.Readline(c.log, c.r)
|
||||
if err != nil {
|
||||
// See if this is a TLS alert from remote, and one other than 0 (which notifies
|
||||
// that the connection is being closed. If so, we register a TLS connection
|
||||
@ -463,7 +465,7 @@ func (c *Client) readline() (string, error) {
|
||||
return line, nil
|
||||
}
|
||||
|
||||
func (c *Client) xtrace(level mlog.Level) func() {
|
||||
func (c *Client) xtrace(level slog.Level) func() {
|
||||
c.xflush()
|
||||
c.tr.SetTrace(level)
|
||||
c.tw.SetTrace(level)
|
||||
@ -543,7 +545,7 @@ func (c *Client) readecode(ecodes bool) (code int, secode, lastLine string, text
|
||||
}
|
||||
}
|
||||
metricCommands.WithLabelValues(cmd, fmt.Sprintf("%d", co), sec).Observe(float64(time.Since(c.cmdStart)) / float64(time.Second))
|
||||
c.log.Debug("smtpclient command result", mlog.Field("cmd", cmd), mlog.Field("code", co), mlog.Field("secode", sec), mlog.Field("duration", time.Since(c.cmdStart)))
|
||||
c.log.Debug("smtpclient command result", slog.String("cmd", cmd), slog.Int("code", co), slog.String("secode", sec), slog.Duration("duration", time.Since(c.cmdStart)))
|
||||
}
|
||||
return co, sec, line, texts, nil
|
||||
}
|
||||
@ -726,7 +728,7 @@ func (c *Client) hello(ctx context.Context, tlsMode TLSMode, ehloHostname dns.Do
|
||||
|
||||
// Attempt TLS if remote understands STARTTLS and we aren't doing immediate TLS or if caller requires it.
|
||||
if c.extStartTLS && tlsMode == TLSOpportunistic || tlsMode == TLSRequiredStartTLS {
|
||||
c.log.Debug("starting tls client", mlog.Field("tlsmode", tlsMode), mlog.Field("servername", c.remoteHostname))
|
||||
c.log.Debug("starting tls client", slog.Any("tlsmode", tlsMode), slog.Any("servername", c.remoteHostname))
|
||||
c.cmds[0] = "starttls"
|
||||
c.cmdStart = time.Now()
|
||||
c.xwritelinef("STARTTLS")
|
||||
@ -772,14 +774,14 @@ func (c *Client) hello(ctx context.Context, tlsMode TLSMode, ehloHostname dns.Do
|
||||
|
||||
tlsversion, ciphersuite := mox.TLSInfo(nconn)
|
||||
c.log.Debug("starttls client handshake done",
|
||||
mlog.Field("tlsmode", tlsMode),
|
||||
mlog.Field("verifypkix", c.tlsVerifyPKIX),
|
||||
mlog.Field("verifydane", c.daneRecords != nil),
|
||||
mlog.Field("ignoretlsverifyerrors", c.ignoreTLSVerifyErrors),
|
||||
mlog.Field("tls", tlsversion),
|
||||
mlog.Field("ciphersuite", ciphersuite),
|
||||
mlog.Field("servername", c.remoteHostname),
|
||||
mlog.Field("danerecord", c.daneVerifiedRecord))
|
||||
slog.Any("tlsmode", tlsMode),
|
||||
slog.Bool("verifypkix", c.tlsVerifyPKIX),
|
||||
slog.Bool("verifydane", c.daneRecords != nil),
|
||||
slog.Bool("ignoretlsverifyerrors", c.ignoreTLSVerifyErrors),
|
||||
slog.String("tls", tlsversion),
|
||||
slog.String("ciphersuite", ciphersuite),
|
||||
slog.Any("servername", c.remoteHostname),
|
||||
slog.Any("danerecord", c.daneVerifiedRecord))
|
||||
c.tls = true
|
||||
// Track successful TLS connection. ../rfc/8460:515
|
||||
c.tlsResultAdd(1, 0, nil)
|
||||
@ -1171,7 +1173,7 @@ func (c *Client) Close() (rerr error) {
|
||||
c.xwriteline("QUIT")
|
||||
if err := c.conn.SetReadDeadline(time.Now().Add(5 * time.Second)); err != nil {
|
||||
c.log.Infox("setting read deadline for reading quit response", err)
|
||||
} else if _, err := bufs.Readline(c.r); err != nil {
|
||||
} else if _, err := bufs.Readline(c.log, c.r); err != nil {
|
||||
rerr = fmt.Errorf("reading response to quit command: %v", err)
|
||||
c.log.Debugx("reading quit response", err)
|
||||
}
|
||||
|
@ -21,6 +21,8 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"golang.org/x/exp/slog"
|
||||
|
||||
"github.com/mjl-/mox/dns"
|
||||
"github.com/mjl-/mox/mlog"
|
||||
"github.com/mjl-/mox/sasl"
|
||||
@ -33,9 +35,9 @@ var localhost = dns.Domain{ASCII: "localhost"}
|
||||
|
||||
func TestClient(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
log := mlog.New("smtpclient")
|
||||
log := mlog.New("smtpclient", nil)
|
||||
|
||||
mlog.SetConfig(map[string]mlog.Level{"": mlog.LevelTrace})
|
||||
mlog.SetConfig(map[string]slog.Level{"": mlog.LevelTrace})
|
||||
|
||||
type options struct {
|
||||
pipelining bool
|
||||
@ -281,7 +283,7 @@ func TestClient(t *testing.T) {
|
||||
result <- err
|
||||
panic("stop")
|
||||
}
|
||||
c, err := New(ctx, log, clientConn, opts.tlsMode, opts.tlsPKIX, localhost, opts.tlsHostname, Opts{Auth: auths, RootCAs: opts.roots})
|
||||
c, err := New(ctx, log.Logger, clientConn, opts.tlsMode, opts.tlsPKIX, localhost, opts.tlsHostname, Opts{Auth: auths, RootCAs: opts.roots})
|
||||
if (err == nil) != (expClientErr == nil) || err != nil && !errors.As(err, reflect.New(reflect.ValueOf(expClientErr).Type()).Interface()) && !errors.Is(err, expClientErr) {
|
||||
fail("new client: got err %v, expected %#v", err, expClientErr)
|
||||
}
|
||||
@ -382,13 +384,13 @@ test
|
||||
|
||||
func TestErrors(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
log := mlog.New("")
|
||||
log := mlog.New("smtpclient", nil)
|
||||
|
||||
// Invalid greeting.
|
||||
run(t, func(s xserver) {
|
||||
s.writeline("bogus") // Invalid, should be "220 <hostname>".
|
||||
}, func(conn net.Conn) {
|
||||
_, err := New(ctx, log, conn, TLSOpportunistic, false, localhost, zerohost, Opts{})
|
||||
_, err := New(ctx, log.Logger, conn, TLSOpportunistic, false, localhost, zerohost, Opts{})
|
||||
var xerr Error
|
||||
if err == nil || !errors.Is(err, ErrProtocol) || !errors.As(err, &xerr) || xerr.Permanent {
|
||||
panic(fmt.Errorf("got %#v, expected ErrProtocol without Permanent", err))
|
||||
@ -399,7 +401,7 @@ func TestErrors(t *testing.T) {
|
||||
run(t, func(s xserver) {
|
||||
s.conn.Close()
|
||||
}, func(conn net.Conn) {
|
||||
_, err := New(ctx, log, conn, TLSOpportunistic, false, localhost, zerohost, Opts{})
|
||||
_, err := New(ctx, log.Logger, conn, TLSOpportunistic, false, localhost, zerohost, Opts{})
|
||||
var xerr Error
|
||||
if err == nil || !errors.Is(err, io.ErrUnexpectedEOF) || !errors.As(err, &xerr) || xerr.Permanent {
|
||||
panic(fmt.Errorf("got %#v (%v), expected ErrUnexpectedEOF without Permanent", err, err))
|
||||
@ -410,7 +412,7 @@ func TestErrors(t *testing.T) {
|
||||
run(t, func(s xserver) {
|
||||
s.writeline("521 not accepting connections")
|
||||
}, func(conn net.Conn) {
|
||||
_, err := New(ctx, log, conn, TLSOpportunistic, false, localhost, zerohost, Opts{})
|
||||
_, err := New(ctx, log.Logger, conn, TLSOpportunistic, false, localhost, zerohost, Opts{})
|
||||
var xerr Error
|
||||
if err == nil || !errors.Is(err, ErrStatus) || !errors.As(err, &xerr) || !xerr.Permanent {
|
||||
panic(fmt.Errorf("got %#v, expected ErrStatus with Permanent", err))
|
||||
@ -421,7 +423,7 @@ func TestErrors(t *testing.T) {
|
||||
run(t, func(s xserver) {
|
||||
s.writeline("2200 mox.example") // Invalid, too many digits.
|
||||
}, func(conn net.Conn) {
|
||||
_, err := New(ctx, log, conn, TLSOpportunistic, false, localhost, zerohost, Opts{})
|
||||
_, err := New(ctx, log.Logger, conn, TLSOpportunistic, false, localhost, zerohost, Opts{})
|
||||
var xerr Error
|
||||
if err == nil || !errors.Is(err, ErrProtocol) || !errors.As(err, &xerr) || xerr.Permanent {
|
||||
panic(fmt.Errorf("got %#v, expected ErrProtocol without Permanent", err))
|
||||
@ -435,7 +437,7 @@ func TestErrors(t *testing.T) {
|
||||
s.writeline("250-mox.example")
|
||||
s.writeline("500 different code") // Invalid.
|
||||
}, func(conn net.Conn) {
|
||||
_, err := New(ctx, log, conn, TLSOpportunistic, false, localhost, zerohost, Opts{})
|
||||
_, err := New(ctx, log.Logger, conn, TLSOpportunistic, false, localhost, zerohost, Opts{})
|
||||
var xerr Error
|
||||
if err == nil || !errors.Is(err, ErrProtocol) || !errors.As(err, &xerr) || xerr.Permanent {
|
||||
panic(fmt.Errorf("got %#v, expected ErrProtocol without Permanent", err))
|
||||
@ -451,7 +453,7 @@ func TestErrors(t *testing.T) {
|
||||
s.readline("MAIL FROM:")
|
||||
s.writeline("550 5.7.0 not allowed")
|
||||
}, func(conn net.Conn) {
|
||||
c, err := New(ctx, log, conn, TLSOpportunistic, false, localhost, zerohost, Opts{})
|
||||
c, err := New(ctx, log.Logger, conn, TLSOpportunistic, false, localhost, zerohost, Opts{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -471,7 +473,7 @@ func TestErrors(t *testing.T) {
|
||||
s.readline("MAIL FROM:")
|
||||
s.writeline("451 bad sender")
|
||||
}, func(conn net.Conn) {
|
||||
c, err := New(ctx, log, conn, TLSOpportunistic, false, localhost, zerohost, Opts{})
|
||||
c, err := New(ctx, log.Logger, conn, TLSOpportunistic, false, localhost, zerohost, Opts{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -493,7 +495,7 @@ func TestErrors(t *testing.T) {
|
||||
s.readline("RCPT TO:")
|
||||
s.writeline("451")
|
||||
}, func(conn net.Conn) {
|
||||
c, err := New(ctx, log, conn, TLSOpportunistic, false, localhost, zerohost, Opts{})
|
||||
c, err := New(ctx, log.Logger, conn, TLSOpportunistic, false, localhost, zerohost, Opts{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -517,7 +519,7 @@ func TestErrors(t *testing.T) {
|
||||
s.readline("DATA")
|
||||
s.writeline("550 no!")
|
||||
}, func(conn net.Conn) {
|
||||
c, err := New(ctx, log, conn, TLSOpportunistic, false, localhost, zerohost, Opts{})
|
||||
c, err := New(ctx, log.Logger, conn, TLSOpportunistic, false, localhost, zerohost, Opts{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -537,7 +539,7 @@ func TestErrors(t *testing.T) {
|
||||
s.readline("STARTTLS")
|
||||
s.writeline("502 command not implemented")
|
||||
}, func(conn net.Conn) {
|
||||
_, err := New(ctx, log, conn, TLSRequiredStartTLS, true, localhost, dns.Domain{ASCII: "mox.example"}, Opts{})
|
||||
_, err := New(ctx, log.Logger, conn, TLSRequiredStartTLS, true, localhost, dns.Domain{ASCII: "mox.example"}, Opts{})
|
||||
var xerr Error
|
||||
if err == nil || !errors.Is(err, ErrTLS) || !errors.As(err, &xerr) || !xerr.Permanent {
|
||||
panic(fmt.Errorf("got %#v, expected ErrTLS with Permanent", err))
|
||||
@ -553,7 +555,7 @@ func TestErrors(t *testing.T) {
|
||||
s.readline("MAIL FROM:")
|
||||
s.writeline("451 enough")
|
||||
}, func(conn net.Conn) {
|
||||
c, err := New(ctx, log, conn, TLSSkip, false, localhost, dns.Domain{ASCII: "mox.example"}, Opts{})
|
||||
c, err := New(ctx, log.Logger, conn, TLSSkip, false, localhost, dns.Domain{ASCII: "mox.example"}, Opts{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -583,7 +585,7 @@ func TestErrors(t *testing.T) {
|
||||
s.readline("DATA")
|
||||
s.writeline("550 not now")
|
||||
}, func(conn net.Conn) {
|
||||
c, err := New(ctx, log, conn, TLSOpportunistic, false, localhost, zerohost, Opts{})
|
||||
c, err := New(ctx, log.Logger, conn, TLSOpportunistic, false, localhost, zerohost, Opts{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -613,7 +615,7 @@ func TestErrors(t *testing.T) {
|
||||
s.readline("MAIL FROM:")
|
||||
s.writeline("550 ok")
|
||||
}, func(conn net.Conn) {
|
||||
c, err := New(ctx, log, conn, TLSOpportunistic, false, localhost, zerohost, Opts{})
|
||||
c, err := New(ctx, log.Logger, conn, TLSOpportunistic, false, localhost, zerohost, Opts{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -6,6 +6,8 @@ import (
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"golang.org/x/exp/slog"
|
||||
|
||||
"github.com/mjl-/mox/dns"
|
||||
"github.com/mjl-/mox/mlog"
|
||||
"github.com/mjl-/mox/mox-"
|
||||
@ -51,7 +53,8 @@ type Dialer interface {
|
||||
// If we have fully specified local SMTP listener IPs, we set those for the
|
||||
// outgoing connection. The admin probably configured these same IPs in SPF, but
|
||||
// others possibly not.
|
||||
func Dial(ctx context.Context, log *mlog.Log, dialer Dialer, host dns.IPDomain, ips []net.IP, port int, dialedIPs map[string][]net.IP) (conn net.Conn, ip net.IP, rerr error) {
|
||||
func Dial(ctx context.Context, elog *slog.Logger, dialer Dialer, host dns.IPDomain, ips []net.IP, port int, dialedIPs map[string][]net.IP) (conn net.Conn, ip net.IP, rerr error) {
|
||||
log := mlog.New("smtpclient", elog)
|
||||
timeout := 30 * time.Second
|
||||
if deadline, ok := ctx.Deadline(); ok && len(ips) > 0 {
|
||||
timeout = time.Until(deadline) / time.Duration(len(ips))
|
||||
@ -61,7 +64,7 @@ func Dial(ctx context.Context, log *mlog.Log, dialer Dialer, host dns.IPDomain,
|
||||
var lastIP net.IP
|
||||
for _, ip := range ips {
|
||||
addr := net.JoinHostPort(ip.String(), fmt.Sprintf("%d", port))
|
||||
log.Debug("dialing host", mlog.Field("addr", addr))
|
||||
log.Debug("dialing host", slog.String("addr", addr))
|
||||
var laddr net.Addr
|
||||
for _, lip := range mox.Conf.Static.SpecifiedSMTPListenIPs {
|
||||
ipIs4 := ip.To4() != nil
|
||||
@ -73,12 +76,12 @@ func Dial(ctx context.Context, log *mlog.Log, dialer Dialer, host dns.IPDomain,
|
||||
}
|
||||
conn, err := dial(ctx, dialer, timeout, addr, laddr)
|
||||
if err == nil {
|
||||
log.Debug("connected to host", mlog.Field("host", host), mlog.Field("addr", addr), mlog.Field("laddr", laddr))
|
||||
log.Debug("connected to host", slog.Any("host", host), slog.String("addr", addr), slog.Any("laddr", laddr))
|
||||
name := host.String()
|
||||
dialedIPs[name] = append(dialedIPs[name], ip)
|
||||
return conn, ip, nil
|
||||
}
|
||||
log.Debugx("connection attempt", err, mlog.Field("host", host), mlog.Field("addr", addr), mlog.Field("laddr", laddr))
|
||||
log.Debugx("connection attempt", err, slog.Any("host", host), slog.String("addr", addr), slog.Any("laddr", laddr))
|
||||
lastErr = err
|
||||
lastIP = ip
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ import (
|
||||
func TestDialHost(t *testing.T) {
|
||||
// We mostly want to test that dialing a second time switches to the other address family.
|
||||
ctxbg := context.Background()
|
||||
log := mlog.New("smtpclient")
|
||||
log := mlog.New("smtpclient", nil)
|
||||
|
||||
resolver := dns.MockResolver{
|
||||
A: map[string][]string{
|
||||
@ -37,20 +37,20 @@ func TestDialHost(t *testing.T) {
|
||||
}
|
||||
|
||||
dialedIPs := map[string][]net.IP{}
|
||||
_, _, _, ips, dualstack, err := GatherIPs(ctxbg, log, resolver, ipdomain("dualstack.example"), dialedIPs)
|
||||
_, _, _, ips, dualstack, err := GatherIPs(ctxbg, log.Logger, resolver, ipdomain("dualstack.example"), dialedIPs)
|
||||
if err != nil || !reflect.DeepEqual(ips, []net.IP{net.ParseIP("10.0.0.1"), net.ParseIP("2001:db8::1")}) || !dualstack {
|
||||
t.Fatalf("expected err nil, address 10.0.0.1,2001:db8::1, dualstack true, got %v %v %v", err, ips, dualstack)
|
||||
}
|
||||
_, ip, err := Dial(ctxbg, log, nil, ipdomain("dualstack.example"), ips, 25, dialedIPs)
|
||||
_, ip, err := Dial(ctxbg, log.Logger, nil, ipdomain("dualstack.example"), ips, 25, dialedIPs)
|
||||
if err != nil || ip.String() != "10.0.0.1" {
|
||||
t.Fatalf("expected err nil, address 10.0.0.1, dualstack true, got %v %v %v", err, ip, dualstack)
|
||||
}
|
||||
|
||||
_, _, _, ips, dualstack, err = GatherIPs(ctxbg, log, resolver, ipdomain("dualstack.example"), dialedIPs)
|
||||
_, _, _, ips, dualstack, err = GatherIPs(ctxbg, log.Logger, resolver, ipdomain("dualstack.example"), dialedIPs)
|
||||
if err != nil || !reflect.DeepEqual(ips, []net.IP{net.ParseIP("2001:db8::1"), net.ParseIP("10.0.0.1")}) || !dualstack {
|
||||
t.Fatalf("expected err nil, address 2001:db8::1,10.0.0.1, dualstack true, got %v %v %v", err, ips, dualstack)
|
||||
}
|
||||
_, ip, err = Dial(ctxbg, log, nil, ipdomain("dualstack.example"), ips, 25, dialedIPs)
|
||||
_, ip, err = Dial(ctxbg, log.Logger, nil, ipdomain("dualstack.example"), ips, 25, dialedIPs)
|
||||
if err != nil || ip.String() != "2001:db8::1" {
|
||||
t.Fatalf("expected err nil, address 2001:db8::1, dualstack true, got %v %v %v", err, ip, dualstack)
|
||||
}
|
||||
|
@ -12,6 +12,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"golang.org/x/exp/slog"
|
||||
|
||||
"github.com/mjl-/adns"
|
||||
|
||||
"github.com/mjl-/mox/dns"
|
||||
@ -45,9 +47,11 @@ var (
|
||||
// were found, both the original and expanded next-hops must be authentic for DANE
|
||||
// to apply. For a non-IP with no MX records found, the authentic result can be
|
||||
// used to decide which of the names to use as TLSA base domain.
|
||||
func GatherDestinations(ctx context.Context, log *mlog.Log, resolver dns.Resolver, origNextHop dns.IPDomain) (haveMX, origNextHopAuthentic, expandedNextHopAuthentic bool, expandedNextHop dns.Domain, hosts []dns.IPDomain, permanent bool, err error) {
|
||||
func GatherDestinations(ctx context.Context, elog *slog.Logger, resolver dns.Resolver, origNextHop dns.IPDomain) (haveMX, origNextHopAuthentic, expandedNextHopAuthentic bool, expandedNextHop dns.Domain, hosts []dns.IPDomain, permanent bool, err error) {
|
||||
// ../rfc/5321:3824
|
||||
|
||||
log := mlog.New("smtpclient", elog)
|
||||
|
||||
// IP addresses are dialed directly, and don't have TLSA records.
|
||||
if len(origNextHop.IP) > 0 {
|
||||
return false, false, false, expandedNextHop, []dns.IPDomain{origNextHop}, false, nil
|
||||
@ -167,7 +171,9 @@ func GatherDestinations(ctx context.Context, log *mlog.Log, resolver dns.Resolve
|
||||
// GatherIPs looks up the IPs to try for connecting to host, with the IPs ordered
|
||||
// to take previous attempts into account. For use with DANE, the CNAME-expanded
|
||||
// name is returned, and whether the DNS responses were authentic.
|
||||
func GatherIPs(ctx context.Context, log *mlog.Log, resolver dns.Resolver, host dns.IPDomain, dialedIPs map[string][]net.IP) (authentic bool, expandedAuthentic bool, expandedHost dns.Domain, ips []net.IP, dualstack bool, rerr error) {
|
||||
func GatherIPs(ctx context.Context, elog *slog.Logger, resolver dns.Resolver, host dns.IPDomain, dialedIPs map[string][]net.IP) (authentic bool, expandedAuthentic bool, expandedHost dns.Domain, ips []net.IP, dualstack bool, rerr error) {
|
||||
log := mlog.New("smtpclient", elog)
|
||||
|
||||
if len(host.IP) > 0 {
|
||||
return false, false, dns.Domain{}, []net.IP{host.IP}, false, nil
|
||||
}
|
||||
@ -250,7 +256,7 @@ func GatherIPs(ctx context.Context, log *mlog.Log, resolver dns.Resolver, host d
|
||||
// Prefer "i" if it is the same as last and we should be preferring it.
|
||||
return preferPrev && ips[i].Equal(prevIP)
|
||||
})
|
||||
log.Debug("ordered ips for dialing", mlog.Field("ips", ips))
|
||||
log.Debug("ordered ips for dialing", slog.Any("ips", ips))
|
||||
}
|
||||
return
|
||||
}
|
||||
@ -268,7 +274,9 @@ func GatherIPs(ctx context.Context, log *mlog.Log, resolver dns.Resolver, host d
|
||||
// must do TLS, but not verify the remote TLS certificate.
|
||||
//
|
||||
// Returned values are always meaningful, also when an error was returned.
|
||||
func GatherTLSA(ctx context.Context, log *mlog.Log, resolver dns.Resolver, host dns.Domain, expandedAuthentic bool, expandedHost dns.Domain) (daneRequired bool, daneRecords []adns.TLSA, tlsaBaseDomain dns.Domain, err error) {
|
||||
func GatherTLSA(ctx context.Context, elog *slog.Logger, resolver dns.Resolver, host dns.Domain, expandedAuthentic bool, expandedHost dns.Domain) (daneRequired bool, daneRecords []adns.TLSA, tlsaBaseDomain dns.Domain, err error) {
|
||||
log := mlog.New("smtpclient", elog)
|
||||
|
||||
// ../rfc/7672:912
|
||||
// This function is only called when the lookup of host was authentic.
|
||||
|
||||
@ -288,32 +296,32 @@ func GatherTLSA(ctx context.Context, log *mlog.Log, resolver dns.Resolver, host
|
||||
}
|
||||
if len(l) == 0 || err != nil {
|
||||
daneRequired = err != nil
|
||||
log.Debugx("gathering tlsa records failed", err, mlog.Field("danerequired", daneRequired), mlog.Field("basedomain", tlsaBaseDomain))
|
||||
log.Debugx("gathering tlsa records failed", err, slog.Bool("danerequired", daneRequired), slog.Any("basedomain", tlsaBaseDomain))
|
||||
return daneRequired, nil, tlsaBaseDomain, err
|
||||
}
|
||||
daneRequired = len(l) > 0
|
||||
l = filterUsableTLSARecords(log, l)
|
||||
log.Debug("tlsa records exist", mlog.Field("danerequired", daneRequired), mlog.Field("records", l), mlog.Field("basedomain", tlsaBaseDomain))
|
||||
log.Debug("tlsa records exist", slog.Bool("danerequired", daneRequired), slog.Any("records", l), slog.Any("basedomain", tlsaBaseDomain))
|
||||
return daneRequired, l, tlsaBaseDomain, err
|
||||
}
|
||||
|
||||
// lookupTLSACNAME composes a TLSA domain name to lookup, follows CNAMEs and looks
|
||||
// up TLSA records. no TLSA records exist, a nil error is returned as it means
|
||||
// the host does not opt-in to DANE.
|
||||
func lookupTLSACNAME(ctx context.Context, log *mlog.Log, resolver dns.Resolver, port int, protocol string, host dns.Domain) (l []adns.TLSA, rerr error) {
|
||||
func lookupTLSACNAME(ctx context.Context, log mlog.Log, resolver dns.Resolver, port int, protocol string, host dns.Domain) (l []adns.TLSA, rerr error) {
|
||||
name := fmt.Sprintf("_%d._%s.%s", port, protocol, host.ASCII+".")
|
||||
for i := 0; ; i++ {
|
||||
cname, result, err := resolver.LookupCNAME(ctx, name)
|
||||
if dns.IsNotFound(err) {
|
||||
if !result.Authentic {
|
||||
log.Debugx("cname nxdomain result during tlsa lookup not authentic, not doing dane for host", err, mlog.Field("host", host), mlog.Field("name", name))
|
||||
log.Debugx("cname nxdomain result during tlsa lookup not authentic, not doing dane for host", err, slog.Any("host", host), slog.String("name", name))
|
||||
return nil, nil
|
||||
}
|
||||
break
|
||||
} else if err != nil {
|
||||
return nil, fmt.Errorf("looking up cname for tlsa candidate base domain: %w", err)
|
||||
} else if !result.Authentic {
|
||||
log.Debugx("cname result during tlsa lookup not authentic, not doing dane for host", err, mlog.Field("host", host), mlog.Field("name", name))
|
||||
log.Debugx("cname result during tlsa lookup not authentic, not doing dane for host", err, slog.Any("host", host), slog.String("name", name))
|
||||
return nil, nil
|
||||
}
|
||||
if i == 10 {
|
||||
@ -325,18 +333,18 @@ func lookupTLSACNAME(ctx context.Context, log *mlog.Log, resolver dns.Resolver,
|
||||
var err error
|
||||
l, result, err = resolver.LookupTLSA(ctx, 0, "", name)
|
||||
if dns.IsNotFound(err) || err == nil && len(l) == 0 {
|
||||
log.Debugx("no tlsa records for host, not doing dane", err, mlog.Field("host", host), mlog.Field("name", name), mlog.Field("authentic", result.Authentic))
|
||||
log.Debugx("no tlsa records for host, not doing dane", err, slog.Any("host", host), slog.String("name", name), slog.Bool("authentic", result.Authentic))
|
||||
return nil, nil
|
||||
} else if err != nil {
|
||||
return nil, fmt.Errorf("looking up tlsa records for tlsa candidate base domain: %w", err)
|
||||
} else if !result.Authentic {
|
||||
log.Debugx("tlsa lookup not authentic, not doing dane for host", err, mlog.Field("host", host), mlog.Field("name", name))
|
||||
log.Debugx("tlsa lookup not authentic, not doing dane for host", err, slog.Any("host", host), slog.String("name", name))
|
||||
return nil, nil
|
||||
}
|
||||
return l, nil
|
||||
}
|
||||
|
||||
func filterUsableTLSARecords(log *mlog.Log, l []adns.TLSA) []adns.TLSA {
|
||||
func filterUsableTLSARecords(log mlog.Log, l []adns.TLSA) []adns.TLSA {
|
||||
// Gather "usable" records. ../rfc/7672:708
|
||||
o := 0
|
||||
for _, r := range l {
|
||||
@ -368,12 +376,12 @@ func filterUsableTLSARecords(log *mlog.Log, l []adns.TLSA) []adns.TLSA {
|
||||
}
|
||||
case adns.TLSAMatchTypeSHA256:
|
||||
if len(r.CertAssoc) != sha256.Size {
|
||||
log.Debug("dane tlsa record with wrong data size for sha2-256", mlog.Field("got", len(r.CertAssoc)), mlog.Field("expect", sha256.Size))
|
||||
log.Debug("dane tlsa record with wrong data size for sha2-256", slog.Int("got", len(r.CertAssoc)), slog.Int("expect", sha256.Size))
|
||||
continue
|
||||
}
|
||||
case adns.TLSAMatchTypeSHA512:
|
||||
if len(r.CertAssoc) != sha512.Size {
|
||||
log.Debug("dane tlsa record with wrong data size for sha2-512", mlog.Field("got", len(r.CertAssoc)), mlog.Field("expect", sha512.Size))
|
||||
log.Debug("dane tlsa record with wrong data size for sha2-512", slog.Int("got", len(r.CertAssoc)), slog.Int("expect", sha512.Size))
|
||||
continue
|
||||
}
|
||||
default:
|
||||
|
@ -47,7 +47,7 @@ func ipdomains(s ...string) (l []dns.IPDomain) {
|
||||
// exist or has temporary error.
|
||||
func TestGatherDestinations(t *testing.T) {
|
||||
ctxbg := context.Background()
|
||||
log := mlog.New("smtpclient")
|
||||
log := mlog.New("smtpclient", nil)
|
||||
|
||||
resolver := dns.MockResolver{
|
||||
MX: map[string][]*net.MX{
|
||||
@ -89,7 +89,7 @@ func TestGatherDestinations(t *testing.T) {
|
||||
test := func(ipd dns.IPDomain, expHosts []dns.IPDomain, expDomain dns.Domain, expPerm, expAuthic, expExpAuthic bool, expErr error) {
|
||||
t.Helper()
|
||||
|
||||
_, authic, authicExp, ed, hosts, perm, err := GatherDestinations(ctxbg, log, resolver, ipd)
|
||||
_, authic, authicExp, ed, hosts, perm, err := GatherDestinations(ctxbg, log.Logger, resolver, ipd)
|
||||
if (err == nil) != (expErr == nil) || err != nil && !errors.Is(err, expErr) {
|
||||
// todo: could also check the individual errors? code currently does not have structured errors.
|
||||
t.Fatalf("gather hosts: %v, expected %v", err, expErr)
|
||||
@ -134,7 +134,7 @@ func TestGatherDestinations(t *testing.T) {
|
||||
|
||||
func TestGatherIPs(t *testing.T) {
|
||||
ctxbg := context.Background()
|
||||
log := mlog.New("smtpclient")
|
||||
log := mlog.New("smtpclient", nil)
|
||||
|
||||
resolver := dns.MockResolver{
|
||||
A: map[string][]string{
|
||||
@ -164,7 +164,7 @@ func TestGatherIPs(t *testing.T) {
|
||||
test := func(host dns.IPDomain, expAuthic, expAuthicExp bool, expHostExp dns.Domain, expIPs []net.IP, expErr any) {
|
||||
t.Helper()
|
||||
|
||||
authic, authicExp, hostExp, ips, _, err := GatherIPs(ctxbg, log, resolver, host, nil)
|
||||
authic, authicExp, hostExp, ips, _, err := GatherIPs(ctxbg, log.Logger, resolver, host, nil)
|
||||
if (err == nil) != (expErr == nil) || err != nil && !(errors.Is(err, expErr.(error)) || errors.As(err, &expErr)) {
|
||||
// todo: could also check the individual errors?
|
||||
t.Fatalf("gather hosts: %v, expected %v", err, expErr)
|
||||
@ -207,7 +207,7 @@ func TestGatherIPs(t *testing.T) {
|
||||
|
||||
func TestGatherTLSA(t *testing.T) {
|
||||
ctxbg := context.Background()
|
||||
log := mlog.New("smtpclient")
|
||||
log := mlog.New("smtpclient", nil)
|
||||
|
||||
record := func(usage, selector, matchType uint8) adns.TLSA {
|
||||
return adns.TLSA{
|
||||
@ -253,7 +253,7 @@ func TestGatherTLSA(t *testing.T) {
|
||||
test := func(host dns.Domain, expandedAuthentic bool, expandedHost dns.Domain, expDANERequired bool, expRecords []adns.TLSA, expBaseDom dns.Domain, expErr any) {
|
||||
t.Helper()
|
||||
|
||||
daneReq, records, baseDom, err := GatherTLSA(ctxbg, log, resolver, host, expandedAuthentic, expandedHost)
|
||||
daneReq, records, baseDom, err := GatherTLSA(ctxbg, log.Logger, resolver, host, expandedAuthentic, expandedHost)
|
||||
if (err == nil) != (expErr == nil) || err != nil && !(errors.Is(err, expErr.(error)) || errors.As(err, &expErr)) {
|
||||
// todo: could also check the individual errors?
|
||||
t.Fatalf("gather tlsa: %v, expected %v", err, expErr)
|
||||
|
Reference in New Issue
Block a user